Changeset 2960346
- Timestamp:
- 08/30/2023 03:22:07 AM (3 years ago)
- File:
-
- 1 edited
-
contentify-ai/trunk/index.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
contentify-ai/trunk/index.php
r2947498 r2960346 3 3 Plugin Name: Contentify AI 4 4 Description: This plugin is a Contentify Editor AI (Co-Editor). It publishes and optimize the content that Contentify Writer AI (Co-Writer) generates. 5 Version: 1.3. 05 Version: 1.3.1 6 6 Author: Contentify Team 7 7 Text Domain: contentify-ai … … 156 156 } 157 157 158 // Callback function to update an existing blog 159 public function update_blog($data) 160 { 161 $post_id = $data->get_param('post_id'); 162 $api_key = $data->get_param('api_key'); 163 $title = $data->get_param('title'); 164 $content = $data->get_param('content'); 165 $status = $data->get_param('status'); 166 $keyword = $data->get_param('keyword'); 167 $seo_title = $data->get_param('seo_title'); 168 $seo_description = $data->get_param('seo_description'); 169 $category = $data->get_param('category'); 170 $image = $data->get_param('image'); 171 172 // Validate the API key 173 if (!$this->validate_api_key($api_key)) { 174 return new WP_Error('invalid_api_key', __('The API key provided is invalid', 'contentify-ai'), array('status' => 401)); 175 } 176 177 // Check if the post exists 178 $post = get_post($post_id); 179 if (!$post) { 180 return new WP_Error('post_not_found', __('The specified post ID does not exist', 'contentify-ai'), array('status' => 404)); 181 } 182 183 // Get the user by login or email 184 $user = get_user_by('login', $author); 185 if (!$user) { 186 $user = get_user_by('email', $author); 187 } 188 $author_id = $user->ID; 189 190 // Validate post status 191 $status = strtolower($status); 192 if (!isset(get_post_statuses()[$status])) 193 $status = 'publish'; 194 195 // Category 196 $term_id = 0; 197 if (!empty($category)) { 198 $term = get_term_by('name', $category, 'category'); 199 if (!$term instanceof WP_Term) { 200 $term = wp_insert_term($category, 'category'); 201 if (is_array($term)) 202 $term_id = $term['term_id']; 203 } else { 204 $term_id = $term->term_id; 205 } 206 } 207 208 // Update the post 209 $post_args = array( 210 'ID' => $post_id, 211 'post_title' => $title, 212 'post_content' => $content, 213 'post_status' => $status, 214 'post_author' => $author_id, 215 'post_category' => array($term_id) 216 ); 217 wp_update_post($post_args); 218 219 // Update Yoast SEO keyword, title, and description 220 update_post_meta($post_id, '_yoast_wpseo_focuskw', $keyword); 221 update_post_meta($post_id, '_yoast_wpseo_title', $seo_title); 222 update_post_meta($post_id, '_yoast_wpseo_metadesc', $seo_description); 223 224 if (!empty($image)) { 225 $attachment_id = $this->cai_upload_from_url($image, $title); 226 if ($attachment_id) { 227 set_post_thumbnail($post_id, $attachment_id); 228 } 229 } 230 231 // Return the updated post ID 232 return array('updated_post_id' => $post_id); 233 } 158 // Callback function to update an existing blog 159 public function update_blog($data) { 160 $post_id = $data->get_param('post_id'); 161 $api_key = $data->get_param('api_key'); 162 $title = $data->get_param('title'); 163 $content = $data->get_param('content'); 164 $status = $data->get_param('status'); 165 $keyword = $data->get_param('keyword'); 166 $seo_title = $data->get_param('seo_title'); 167 $seo_description = $data->get_param('seo_description'); 168 $category = $data->get_param('category'); 169 $image = $data->get_param('image'); 170 171 // Validate the API key 172 if (!$this->validate_api_key($api_key)) { 173 return new WP_Error('invalid_api_key', __('The API key provided is invalid', 'contentify-ai'), array('status' => 401)); 174 } 175 176 // Check if the post exists 177 $post = get_post($post_id); 178 if (!$post) { 179 return new WP_Error('post_not_found', __('The specified post ID does not exist', 'contentify-ai'), array('status' => 404)); 180 } 181 182 // Get the user by login or email 183 $user = get_user_by('login', $author); 184 if (!$user) { 185 $user = get_user_by('email', $author); 186 } 187 $author_id = $user->ID; 188 189 // Validate post status 190 $status = strtolower($status); 191 if (!isset(get_post_statuses()[$status])) 192 $status = 'publish'; 193 194 // Category 195 $term_id = 0; 196 if (!empty($category)) { 197 $term = get_term_by('name', $category, 'category'); 198 if (!$term instanceof WP_Term) { 199 $term = wp_insert_term($category, 'category'); 200 if (is_array($term)) 201 $term_id = $term['term_id']; 202 } else { 203 $term_id = $term->term_id; 204 } 205 } 206 207 // Temporarily remove the content filter 208 remove_filter('content_save_pre', 'wp_filter_post_kses'); 209 210 // Update the post 211 $post_args = array( 212 'ID' => $post_id, 213 'post_title' => $title, 214 'post_content' => $content, 215 'post_status' => $status, 216 'post_author' => $author_id, 217 'post_category' => array($term_id) 218 ); 219 wp_update_post($post_args); 220 221 // Re-add the content filter 222 add_filter('content_save_pre', 'wp_filter_post_kses'); 223 224 // Update Yoast SEO keyword, title, and description 225 update_post_meta($post_id, '_yoast_wpseo_focuskw', $keyword); 226 update_post_meta($post_id, '_yoast_wpseo_title', $seo_title); 227 update_post_meta($post_id, '_yoast_wpseo_metadesc', $seo_description); 228 229 if (!empty($image)) { 230 $attachment_id = $this->cai_upload_from_url($image, $title); 231 if ($attachment_id) { 232 set_post_thumbnail($post_id, $attachment_id); 233 } 234 } 235 236 // Return the updated post ID 237 return array('updated_post_id' => $post_id); 238 } 239 234 240 235 241 … … 345 351 } 346 352 } 353 354 remove_filter('content_save_pre', 'wp_filter_post_kses'); 355 347 356 348 357 // Create the post … … 356 365 ) 357 366 ); 367 368 369 add_filter('content_save_pre', 'wp_filter_post_kses'); 370 358 371 359 372
Note: See TracChangeset
for help on using the changeset viewer.