@@ -86,12 +86,9 @@ public function enqueue_assets() {
8686 'classifai-quick-draft-js ' ,
8787 'classifaiQuickDraft ' ,
8888 [
89- 'nonce ' => wp_create_nonce ( 'classifai_quick_draft ' ),
90- 'restUrl ' => rest_url ( 'classifai/v1/ ' ),
9189 'createContent ' => __ ( 'Create Draft from Prompt ' , 'classifai ' ),
9290 'generating ' => __ ( 'Generating... ' , 'classifai ' ),
9391 'error ' => __ ( 'Error generating content. Please try again. ' , 'classifai ' ),
94- 'currentUserId ' => get_current_user_id (),
9592 ]
9693 );
9794
@@ -116,17 +113,18 @@ public function register_endpoints() {
116113 'permission_callback ' => [ $ this , 'permissions_check ' ],
117114 'args ' => [
118115 'title ' => [
116+ 'required ' => true ,
119117 'type ' => 'string ' ,
120118 'sanitize_callback ' => 'sanitize_text_field ' ,
121119 'validate_callback ' => 'rest_validate_request_arg ' ,
122- 'description ' => esc_html__ ( 'The title of the article . ' , 'classifai ' ),
120+ 'description ' => esc_html__ ( 'The title of the post . ' , 'classifai ' ),
123121 ],
124122 'content ' => [
125123 'required ' => true ,
126124 'type ' => 'string ' ,
127125 'sanitize_callback ' => 'sanitize_textarea_field ' ,
128126 'validate_callback ' => 'rest_validate_request_arg ' ,
129- 'description ' => esc_html__ ( 'The prompt content for generation. ' , 'classifai ' ),
127+ 'description ' => esc_html__ ( 'The prompt to use for content generation. ' , 'classifai ' ),
130128 ],
131129 ],
132130 ]
@@ -139,12 +137,19 @@ public function register_endpoints() {
139137 * @return WP_Error|bool
140138 */
141139 public function permissions_check () {
142- // Ensure user can create posts
140+ // Ensure user can create posts.
143141 if ( ! current_user_can ( 'edit_posts ' ) ) {
144142 return false ;
145143 }
146144
147- // Ensure the feature is enabled
145+ $ post_type_obj = get_post_type_object ( 'post ' );
146+
147+ // Ensure the post type is allowed in REST endpoints.
148+ if ( empty ( $ post_type_obj ) || empty ( $ post_type_obj ->show_in_rest ) ) {
149+ return false ;
150+ }
151+
152+ // Ensure the Feature is enabled.
148153 if ( ! $ this ->content_generation ->is_feature_enabled () ) {
149154 return new WP_Error ( 'not_enabled ' , esc_html__ ( 'Content Generation is not currently enabled. ' , 'classifai ' ) );
150155 }
@@ -162,22 +167,26 @@ public function endpoint_callback( WP_REST_Request $request ) {
162167 $ title = $ request ->get_param ( 'title ' );
163168 $ content = $ request ->get_param ( 'content ' );
164169
165- // Create a new auto-draft post
170+ if ( empty ( $ title ) || empty ( $ content ) ) {
171+ return new WP_Error ( 'missing_required_parameters ' , esc_html__ ( 'Title and content are required. ' , 'classifai ' ) );
172+ }
173+
174+ // Create a new auto-draft post.
166175 $ post_data = [
167- 'post_title ' => $ title ? $ title : '' ,
176+ 'post_title ' => $ title ,
168177 'post_content ' => '' ,
169178 'post_status ' => 'auto-draft ' ,
170179 'post_type ' => 'post ' ,
171180 'post_author ' => get_current_user_id (),
172181 ];
173182
174- $ post_id = wp_insert_post ( $ post_data );
183+ $ post_id = wp_insert_post ( $ post_data, true );
175184
176185 if ( is_wp_error ( $ post_id ) ) {
177186 return new WP_Error ( 'post_creation_failed ' , esc_html__ ( 'Failed to create draft post. ' , 'classifai ' ) );
178187 }
179188
180- // Generate content using the existing content generation logic
189+ // Generate content using the existing content generation logic.
181190 $ result = $ this ->content_generation ->run (
182191 $ post_id ,
183192 'create_content ' ,
@@ -188,12 +197,12 @@ public function endpoint_callback( WP_REST_Request $request ) {
188197 );
189198
190199 if ( is_wp_error ( $ result ) ) {
191- // Clean up the post if generation failed
200+ // Clean up the post if generation failed.
192201 wp_delete_post ( $ post_id , true );
193202 return $ result ;
194203 }
195204
196- // Update the post with generated content
205+ // Update the post with generated content.
197206 $ updated_post = [
198207 'ID ' => $ post_id ,
199208 'post_content ' => $ result ,
0 commit comments