@@ -13,7 +13,11 @@ class Post extends RestApiEndpoint
1313{
1414 public const NAMESPACE = 'modularity-frontend-form/v1 ' ;
1515 public const ROUTE = 'submit/post ' ;
16- public const KEY = 'submit-post ' ;
16+ public const KEY = 'submitForm ' ;
17+
18+ public function __construct (
19+ private \WpService \WpService $ wpService
20+ ) {}
1721
1822 /**
1923 * Registers a REST route
@@ -28,23 +32,10 @@ public function handleRegisterRestRoute(): bool
2832 'permission_callback ' => array ($ this , 'permissionCallback ' ),
2933 'args ' => [
3034 'module-id ' => [
31- 'description ' => __ ('The module id that the request originates from ' , 'municipio ' ),
32- 'type ' => 'integer ' ,
33- 'format ' => 'uri ' ,
34- 'required ' => true
35- ],
36- 'data ' => [
37- 'description ' => __ ('Description. ' , 'municipio ' ),
38- 'type ' => 'string ' ,
39- 'required ' => false ,
40- 'default ' => null
41- ],
42- 'return ' => [
43- 'description ' => __ ('Return ' , 'municipio ' ),
44- 'type ' => 'string ' ,
45- 'enum ' => ['html ' , 'src ' , 'id ' ],
46- 'required ' => false ,
47- 'default ' => 'html '
35+ 'description ' => __ ('The module id that the request originates from ' , 'municipio ' ),
36+ 'type ' => 'integer ' ,
37+ 'format ' => 'uri ' ,
38+ 'required ' => false
4839 ]
4940 ]
5041 ));
@@ -61,18 +52,30 @@ public function handleRegisterRestRoute(): bool
6152 public function handleRequest (WP_REST_Request $ request ): WP_REST_Response
6253 {
6354 $ params = $ request ->get_json_params ();
64- $ a = $ params ['url ' ] ?? null ;
55+ $ a = $ params ['url ' ] ?? null ;
56+
57+ $ insert = $ this ->insertPost ();
6558
66- /* if (is_wp_error($a )) {
59+ if (is_wp_error ($ insert )) {
6760 $ error = new WP_Error (
6861 $ a ->get_error_code (),
6962 $ a ->get_error_message (),
7063 array ('status ' => WP_Http::BAD_REQUEST )
7164 );
7265 return rest_ensure_response ($ error );
73- }*/
66+ } elseif (is_numeric ($ insert )) {
67+ return rest_ensure_response ([
68+ 'status ' => 'success ' ,
69+ 'message ' => __ ('Post created successfully ' , 'municipio ' ),
70+ 'postId ' => $ insert ,
71+ ]);
72+ }
7473
75- return rest_ensure_response ($ a );
74+ return rest_ensure_response (new WP_Error (
75+ 502 ,
76+ __ ('Unexpected result from post creation ' , 'municipio ' ),
77+ array ('status ' => WP_Http::BAD_REQUEST )
78+ ));
7679 }
7780
7881 /**
@@ -84,4 +87,35 @@ public function permissionCallback(): bool
8487 {
8588 return true ; //May be changed to check for specific capabilities
8689 }
90+
91+ /**
92+ * Handles the request to insert a post
93+ *
94+ * @param int|null $moduleID The module ID
95+ * @param array|null $fieldMeta The field meta data
96+ *
97+ * @return WP_Error|int The result of the post insertion
98+ */
99+ private function insertPost ($ moduleID = null , $ fieldMeta = null ): WP_Error |int {
100+
101+ $ result = $ this ->wpService ->wpInsertPost ([
102+ 'post_title ' => 'Test post ' ,
103+ 'post_type ' => 'post ' ,
104+ 'post_status ' => 'publish ' ,
105+ 'meta_input ' => [
106+ 'module_id ' => $ moduleID ,
107+ 'field_meta ' => $ fieldMeta ,
108+ ],
109+ ]);
110+
111+ if (is_wp_error ($ result )) {
112+ return $ result ;
113+ }
114+
115+ if (is_numeric ($ result )) {
116+ return $ result ;
117+ }
118+
119+ throw new \Exception ('Unexpected result from post creation: ' . print_r ($ result , true ));
120+ }
87121}
0 commit comments