Plugin Directory

Changeset 3285200


Ignore:
Timestamp:
04/30/2025 06:34:43 PM (11 months ago)
Author:
galbc
Message:

Update trunk to version 1.0.3

Location:
sapientseo/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • sapientseo/trunk/inc/api/create-post.php

    r3283788 r3285200  
    44}
    55
    6 // Register REST API Route
    7 add_action('rest_api_init', function() {
     6// 🔹 Shared Field Handler (ACF, categories, tags, SEO)
     7function 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
     52add_action('rest_api_init', function () {
    853    register_rest_route('sapientseo/v1', '/create-post', [
    954        'methods'  => 'POST',
     
    1358        }
    1459    ]);
     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    ]);
    1576});
    1677
    17 // Callback to create post
     78// 🔹 Create Post
    1879function sapient_seo_create_post(WP_REST_Request $request) {
    1980    $params  = $request->get_json_params();
    2081    $title   = sanitize_text_field($params['title'] ?? '');
    2182    $content = wp_kses_post($params['content'] ?? '');
     83    $status  = in_array($params['status'] ?? '', ['draft', 'publish']) ? $params['status'] : 'draft';
    2284
    2385    if (empty($title)) {
     
    2890        'post_title'   => $title,
    2991        'post_content' => $content,
    30         'post_status'  => 'draft',
     92        'post_status'  => $status,
    3193        'post_type'    => 'post',
    3294    ];
     
    41103    }
    42104
    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
     110function 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]);
    48116    }
    49117
    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'];
    67126    }
    68127
    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        ]);
    72135    }
    73136
    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
     142function 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]);
    79147    }
    80148
    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    ]);
    82154}
  • sapientseo/trunk/readme.txt

    r3285196 r3285200  
    44Tested up to: 6.8
    55Requires PHP: 7.4
    6 Stable tag: 1.0.2
     6Stable tag: 1.0.3
    77License: GPLv2 or later
    88License URI: https://www.gnu.org/licenses/gpl-2.0.html
  • sapientseo/trunk/sapientseo.php

    r3285196 r3285200  
    33 * Plugin Name: SapientSEO
    44 * Description: Connect your WordPress site to SapientSEO using secure custom REST API endpoints.
    5  * Version: 1.0.2
     5 * Version: 1.0.3
    66 * Author: SapientSEO
    77 * Plugin URI: https://sapientseo.ai
Note: See TracChangeset for help on using the changeset viewer.