Changeset 3285200
- Timestamp:
- 04/30/2025 06:34:43 PM (11 months ago)
- Location:
- sapientseo/trunk
- Files:
-
- 3 edited
-
inc/api/create-post.php (modified) (4 diffs)
-
readme.txt (modified) (1 diff)
-
sapientseo.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
sapientseo/trunk/inc/api/create-post.php
r3283788 r3285200 4 4 } 5 5 6 // Register REST API Route 7 add_action('rest_api_init', function() { 6 // 🔹 Shared Field Handler (ACF, categories, tags, SEO) 7 function sapientseo_apply_fields($post_id, $params) { 8 // ACF fields 9 if (function_exists('update_field') && !empty($params['acf']) && is_array($params['acf'])) { 10 foreach ($params['acf'] as $field_key => $value) { 11 update_field($field_key, $value, $post_id); 12 } 13 } 14 15 // Categories (only if they exist) 16 if (!empty($params['categories']) && is_array($params['categories'])) { 17 $category_ids = []; 18 foreach ($params['categories'] as $cat) { 19 $term = get_term_by('name', sanitize_text_field($cat), 'category'); 20 if ($term && !is_wp_error($term)) { 21 $category_ids[] = $term->term_id; 22 } 23 } 24 if (!empty($category_ids)) { 25 wp_set_post_categories($post_id, $category_ids, false); 26 } 27 } 28 29 // Tags (only if they exist) 30 if (!empty($params['tags']) && is_array($params['tags'])) { 31 $existing_tags = []; 32 foreach ($params['tags'] as $tag_name) { 33 $term = get_term_by('name', sanitize_text_field($tag_name), 'post_tag'); 34 if ($term && !is_wp_error($term)) { 35 $existing_tags[] = $term->name; 36 } 37 } 38 if (!empty($existing_tags)) { 39 wp_set_post_terms($post_id, $existing_tags, 'post_tag', false); 40 } 41 } 42 43 // SEO metadata 44 if (!empty($params['seo']) && is_array($params['seo'])) { 45 foreach ($params['seo'] as $meta_key => $meta_value) { 46 update_post_meta($post_id, $meta_key, sanitize_text_field($meta_value)); 47 } 48 } 49 } 50 51 // 🔹 Register REST API Routes 52 add_action('rest_api_init', function () { 8 53 register_rest_route('sapientseo/v1', '/create-post', [ 9 54 'methods' => 'POST', … … 13 58 } 14 59 ]); 60 61 register_rest_route('sapientseo/v1', '/update-post', [ 62 'methods' => 'POST', 63 'callback' => 'sapient_seo_update_post', 64 'permission_callback' => function ($request) { 65 return sapient_seo_api_key_permission($request); 66 } 67 ]); 68 69 register_rest_route('sapientseo/v1', '/posts/status', [ 70 'methods' => 'GET', 71 'callback' => 'sapient_seo_get_post_status', 72 'permission_callback' => function ($request) { 73 return sapient_seo_api_key_permission($request); 74 } 75 ]); 15 76 }); 16 77 17 // Callback to create post78 // 🔹 Create Post 18 79 function sapient_seo_create_post(WP_REST_Request $request) { 19 80 $params = $request->get_json_params(); 20 81 $title = sanitize_text_field($params['title'] ?? ''); 21 82 $content = wp_kses_post($params['content'] ?? ''); 83 $status = in_array($params['status'] ?? '', ['draft', 'publish']) ? $params['status'] : 'draft'; 22 84 23 85 if (empty($title)) { … … 28 90 'post_title' => $title, 29 91 'post_content' => $content, 30 'post_status' => 'draft',92 'post_status' => $status, 31 93 'post_type' => 'post', 32 94 ]; … … 41 103 } 42 104 43 // 🔹 Update ACF fields 44 if (function_exists('update_field') && !empty($params['acf']) && is_array($params['acf'])) { 45 foreach ($params['acf'] as $field_key => $value) { 46 update_field($field_key, $value, $post_id); 47 } 105 sapientseo_apply_fields($post_id, $params); 106 return rest_ensure_response(['post_id' => $post_id, 'message' => 'Post created successfully.']); 107 } 108 109 // 🔹 Update Post 110 function sapient_seo_update_post(WP_REST_Request $request) { 111 $params = $request->get_json_params(); 112 $post_id = intval($params['post_id'] ?? 0); 113 114 if (!$post_id || get_post_status($post_id) === false) { 115 return new WP_Error('invalid_post', 'Post not found.', ['status' => 404]); 48 116 } 49 117 50 // 🔹 Assign categories (Handle WP_Error properly) 51 if (!empty($params['categories']) && is_array($params['categories'])) { 52 $category_ids = []; 53 foreach ($params['categories'] as $cat) { 54 $term = get_term_by('name', sanitize_text_field($cat), 'category'); 55 if ($term) { 56 $category_ids[] = $term->term_id; 57 } else { 58 $new_term = wp_insert_term(sanitize_text_field($cat), 'category'); 59 if (!is_wp_error($new_term) && isset($new_term['term_id'])) { 60 $category_ids[] = $new_term['term_id']; 61 } 62 } 63 } 64 if (!empty($category_ids)) { 65 wp_set_post_categories($post_id, $category_ids, false); 66 } 118 $post_data = [ 119 'ID' => $post_id, 120 'post_title' => sanitize_text_field($params['title'] ?? ''), 121 'post_content' => wp_kses_post($params['content'] ?? ''), 122 ]; 123 124 if (isset($params['status']) && in_array($params['status'], ['draft', 'publish'])) { 125 $post_data['post_status'] = $params['status']; 67 126 } 68 127 69 // 🔹 Assign tags 70 if (!empty($params['tags']) && is_array($params['tags'])) { 71 wp_set_post_terms($post_id, array_map('sanitize_text_field', $params['tags']), 'post_tag', false); 128 $result = wp_update_post($post_data, true); 129 if (is_wp_error($result)) { 130 return rest_ensure_response([ 131 'error' => 'post_update_failed', 132 'message' => 'Could not update post.', 133 'status' => 500 134 ]); 72 135 } 73 136 74 // 🔹 Update SEO metadata 75 if (!empty($params['seo']) && is_array($params['seo'])) { 76 foreach ($params['seo'] as $meta_key => $meta_value) { 77 update_post_meta($post_id, $meta_key, sanitize_text_field($meta_value)); 78 } 137 sapientseo_apply_fields($post_id, $params); 138 return rest_ensure_response(['post_id' => $post_id, 'message' => 'Post updated successfully.']); 139 } 140 141 // 🔹 Get Post Status 142 function sapient_seo_get_post_status(WP_REST_Request $request) { 143 $post_id = intval($request->get_param('post_id')); 144 145 if (!$post_id || get_post_status($post_id) === false) { 146 return new WP_Error('invalid_post', 'Post not found.', ['status' => 404]); 79 147 } 80 148 81 return rest_ensure_response(['post_id' => $post_id, 'message' => 'Post created successfully.']); 149 $status = get_post_status($post_id); 150 return rest_ensure_response([ 151 'post_id' => $post_id, 152 'status' => $status 153 ]); 82 154 } -
sapientseo/trunk/readme.txt
r3285196 r3285200 4 4 Tested up to: 6.8 5 5 Requires PHP: 7.4 6 Stable tag: 1.0. 26 Stable tag: 1.0.3 7 7 License: GPLv2 or later 8 8 License URI: https://www.gnu.org/licenses/gpl-2.0.html -
sapientseo/trunk/sapientseo.php
r3285196 r3285200 3 3 * Plugin Name: SapientSEO 4 4 * Description: Connect your WordPress site to SapientSEO using secure custom REST API endpoints. 5 * Version: 1.0. 25 * Version: 1.0.3 6 6 * Author: SapientSEO 7 7 * Plugin URI: https://sapientseo.ai
Note: See TracChangeset
for help on using the changeset viewer.