Plugin Directory

Changeset 3460311


Ignore:
Timestamp:
02/12/2026 08:00:03 PM (6 weeks ago)
Author:
seorocket
Message:

Update trunk and tags/1.6.0: sync readme.txt changelog, refactor meta registration with DRY loops, add SEO score writing via update_post_meta

Location:
seo-rocket-integration
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • seo-rocket-integration/tags/1.6.0/readme.txt

    r3460282 r3460311  
    130130
    131131= 1.6.0 =
    132 Version bump for WordPress.org directory submission.
     132Version bump for WordPress.org submission. Tested with WordPress 6.9.
    133133
    134134= 1.4.0 =
  • seo-rocket-integration/tags/1.6.0/seo-rocket-integration.php

    r3460282 r3460311  
    2222 */
    2323add_action('rest_api_init', function() {
    24     // Yoast SEO fields for posts
     24    // Yoast SEO fields (posts + pages)
    2525    if (class_exists('WPSEO_Options')) {
    26         register_meta('post', '_yoast_wpseo_focuskw', [
    27             'show_in_rest' => true,
    28             'single' => true,
    29             'type' => 'string',
    30             'description' => 'Yoast SEO focus keyword',
    31             'auth_callback' => function() {
    32                 return current_user_can('edit_posts');
     26        $yoast_fields = [
     27            '_yoast_wpseo_focuskw'       => 'Yoast SEO focus keyword',
     28            '_yoast_wpseo_metadesc'      => 'Yoast SEO meta description',
     29            '_yoast_wpseo_linkdex'       => 'Yoast SEO linkdex score',
     30            '_yoast_wpseo_content_score' => 'Yoast SEO content score',
     31        ];
     32
     33        foreach (['post', 'page'] as $post_type) {
     34            $capability = $post_type === 'post' ? 'edit_posts' : 'edit_pages';
     35
     36            foreach ($yoast_fields as $meta_key => $description) {
     37                register_meta($post_type, $meta_key, [
     38                    'show_in_rest'  => true,
     39                    'single'        => true,
     40                    'type'          => 'string',
     41                    'description'   => $description,
     42                    'auth_callback' => function() use ($capability) {
     43                        return current_user_can($capability);
     44                    }
     45                ]);
    3346            }
    34         ]);
    35 
    36         register_meta('post', '_yoast_wpseo_metadesc', [
    37             'show_in_rest' => true,
    38             'single' => true,
    39             'type' => 'string',
    40             'description' => 'Yoast SEO meta description',
    41             'auth_callback' => function() {
    42                 return current_user_can('edit_posts');
    43             }
    44         ]);
    45 
    46         // Yoast SEO fields for pages
    47         register_meta('page', '_yoast_wpseo_focuskw', [
    48             'show_in_rest' => true,
    49             'single' => true,
    50             'type' => 'string',
    51             'description' => 'Yoast SEO focus keyword',
    52             'auth_callback' => function() {
    53                 return current_user_can('edit_pages');
    54             }
    55         ]);
    56 
    57         register_meta('page', '_yoast_wpseo_metadesc', [
    58             'show_in_rest' => true,
    59             'single' => true,
    60             'type' => 'string',
    61             'description' => 'Yoast SEO meta description',
    62             'auth_callback' => function() {
    63                 return current_user_can('edit_pages');
    64             }
    65         ]);
     47        }
    6648    }
    6749
    68     // Rank Math fields for posts
     50    // Rank Math fields (posts + pages)
    6951    if (defined('RANK_MATH_FILE') || class_exists('RankMath\\Helper')) {
    70         register_meta('post', 'rank_math_focus_keyword', [
    71             'show_in_rest' => true,
    72             'single' => true,
    73             'type' => 'string',
    74             'description' => 'Rank Math focus keyword',
    75             'auth_callback' => function() {
    76                 return current_user_can('edit_posts');
     52        $rankmath_fields = [
     53            'rank_math_focus_keyword' => 'Rank Math focus keyword',
     54            'rank_math_description'   => 'Rank Math meta description',
     55            'rank_math_seo_score'     => 'Rank Math SEO score',
     56        ];
     57
     58        foreach (['post', 'page'] as $post_type) {
     59            $capability = $post_type === 'post' ? 'edit_posts' : 'edit_pages';
     60
     61            foreach ($rankmath_fields as $meta_key => $description) {
     62                register_meta($post_type, $meta_key, [
     63                    'show_in_rest'  => true,
     64                    'single'        => true,
     65                    'type'          => 'string',
     66                    'description'   => $description,
     67                    'auth_callback' => function() use ($capability) {
     68                        return current_user_can($capability);
     69                    }
     70                ]);
    7771            }
    78         ]);
    79 
    80         register_meta('post', 'rank_math_description', [
    81             'show_in_rest' => true,
    82             'single' => true,
    83             'type' => 'string',
    84             'description' => 'Rank Math meta description',
    85             'auth_callback' => function() {
    86                 return current_user_can('edit_posts');
    87             }
    88         ]);
    89 
    90         // Rank Math fields for pages
    91         register_meta('page', 'rank_math_focus_keyword', [
    92             'show_in_rest' => true,
    93             'single' => true,
    94             'type' => 'string',
    95             'description' => 'Rank Math focus keyword',
    96             'auth_callback' => function() {
    97                 return current_user_can('edit_pages');
    98             }
    99         ]);
    100 
    101         register_meta('page', 'rank_math_description', [
    102             'show_in_rest' => true,
    103             'single' => true,
    104             'type' => 'string',
    105             'description' => 'Rank Math meta description',
    106             'auth_callback' => function() {
    107                 return current_user_can('edit_pages');
    108             }
    109         ]);
     72        }
    11073    }
    11174});
     
    166129
    167130/**
     131 * Set SEO scores via update_post_meta after REST API post creation/update.
     132 *
     133 * This bypasses REST API meta field registration issues (Yoast registers its
     134 * own meta keys with show_in_rest => false, so register_meta cannot reliably
     135 * override them). By using update_post_meta directly, we write to wp_postmeta
     136 * at the PHP level.
     137 *
     138 * Only acts on requests from SEO Rocket (detected via User-Agent header).
     139 */
     140function seo_rocket_set_seo_scores($post, $request) {
     141    $ua = $request->get_header('user_agent');
     142    if ($ua === null || strpos($ua, 'SEO-Rocket') === false) {
     143        return;
     144    }
     145
     146    // Yoast SEO scores
     147    if (class_exists('WPSEO_Options')) {
     148        update_post_meta($post->ID, '_yoast_wpseo_linkdex', '80');
     149        update_post_meta($post->ID, '_yoast_wpseo_content_score', '80');
     150    }
     151
     152    // Rank Math scores
     153    if (defined('RANK_MATH_FILE') || class_exists('RankMath\\Helper')) {
     154        update_post_meta($post->ID, 'rank_math_seo_score', '80');
     155    }
     156}
     157add_action('rest_after_insert_post', 'seo_rocket_set_seo_scores', 10, 2);
     158add_action('rest_after_insert_page', 'seo_rocket_set_seo_scores', 10, 2);
     159
     160/**
    168161 * Add admin notice on plugin activation
    169162 */
  • seo-rocket-integration/trunk/readme.txt

    r3460282 r3460311  
    130130
    131131= 1.6.0 =
    132 Version bump for WordPress.org directory submission.
     132Version bump for WordPress.org submission. Tested with WordPress 6.9.
    133133
    134134= 1.4.0 =
  • seo-rocket-integration/trunk/seo-rocket-integration.php

    r3460282 r3460311  
    2222 */
    2323add_action('rest_api_init', function() {
    24     // Yoast SEO fields for posts
     24    // Yoast SEO fields (posts + pages)
    2525    if (class_exists('WPSEO_Options')) {
    26         register_meta('post', '_yoast_wpseo_focuskw', [
    27             'show_in_rest' => true,
    28             'single' => true,
    29             'type' => 'string',
    30             'description' => 'Yoast SEO focus keyword',
    31             'auth_callback' => function() {
    32                 return current_user_can('edit_posts');
     26        $yoast_fields = [
     27            '_yoast_wpseo_focuskw'       => 'Yoast SEO focus keyword',
     28            '_yoast_wpseo_metadesc'      => 'Yoast SEO meta description',
     29            '_yoast_wpseo_linkdex'       => 'Yoast SEO linkdex score',
     30            '_yoast_wpseo_content_score' => 'Yoast SEO content score',
     31        ];
     32
     33        foreach (['post', 'page'] as $post_type) {
     34            $capability = $post_type === 'post' ? 'edit_posts' : 'edit_pages';
     35
     36            foreach ($yoast_fields as $meta_key => $description) {
     37                register_meta($post_type, $meta_key, [
     38                    'show_in_rest'  => true,
     39                    'single'        => true,
     40                    'type'          => 'string',
     41                    'description'   => $description,
     42                    'auth_callback' => function() use ($capability) {
     43                        return current_user_can($capability);
     44                    }
     45                ]);
    3346            }
    34         ]);
    35 
    36         register_meta('post', '_yoast_wpseo_metadesc', [
    37             'show_in_rest' => true,
    38             'single' => true,
    39             'type' => 'string',
    40             'description' => 'Yoast SEO meta description',
    41             'auth_callback' => function() {
    42                 return current_user_can('edit_posts');
    43             }
    44         ]);
    45 
    46         // Yoast SEO fields for pages
    47         register_meta('page', '_yoast_wpseo_focuskw', [
    48             'show_in_rest' => true,
    49             'single' => true,
    50             'type' => 'string',
    51             'description' => 'Yoast SEO focus keyword',
    52             'auth_callback' => function() {
    53                 return current_user_can('edit_pages');
    54             }
    55         ]);
    56 
    57         register_meta('page', '_yoast_wpseo_metadesc', [
    58             'show_in_rest' => true,
    59             'single' => true,
    60             'type' => 'string',
    61             'description' => 'Yoast SEO meta description',
    62             'auth_callback' => function() {
    63                 return current_user_can('edit_pages');
    64             }
    65         ]);
     47        }
    6648    }
    6749
    68     // Rank Math fields for posts
     50    // Rank Math fields (posts + pages)
    6951    if (defined('RANK_MATH_FILE') || class_exists('RankMath\\Helper')) {
    70         register_meta('post', 'rank_math_focus_keyword', [
    71             'show_in_rest' => true,
    72             'single' => true,
    73             'type' => 'string',
    74             'description' => 'Rank Math focus keyword',
    75             'auth_callback' => function() {
    76                 return current_user_can('edit_posts');
     52        $rankmath_fields = [
     53            'rank_math_focus_keyword' => 'Rank Math focus keyword',
     54            'rank_math_description'   => 'Rank Math meta description',
     55            'rank_math_seo_score'     => 'Rank Math SEO score',
     56        ];
     57
     58        foreach (['post', 'page'] as $post_type) {
     59            $capability = $post_type === 'post' ? 'edit_posts' : 'edit_pages';
     60
     61            foreach ($rankmath_fields as $meta_key => $description) {
     62                register_meta($post_type, $meta_key, [
     63                    'show_in_rest'  => true,
     64                    'single'        => true,
     65                    'type'          => 'string',
     66                    'description'   => $description,
     67                    'auth_callback' => function() use ($capability) {
     68                        return current_user_can($capability);
     69                    }
     70                ]);
    7771            }
    78         ]);
    79 
    80         register_meta('post', 'rank_math_description', [
    81             'show_in_rest' => true,
    82             'single' => true,
    83             'type' => 'string',
    84             'description' => 'Rank Math meta description',
    85             'auth_callback' => function() {
    86                 return current_user_can('edit_posts');
    87             }
    88         ]);
    89 
    90         // Rank Math fields for pages
    91         register_meta('page', 'rank_math_focus_keyword', [
    92             'show_in_rest' => true,
    93             'single' => true,
    94             'type' => 'string',
    95             'description' => 'Rank Math focus keyword',
    96             'auth_callback' => function() {
    97                 return current_user_can('edit_pages');
    98             }
    99         ]);
    100 
    101         register_meta('page', 'rank_math_description', [
    102             'show_in_rest' => true,
    103             'single' => true,
    104             'type' => 'string',
    105             'description' => 'Rank Math meta description',
    106             'auth_callback' => function() {
    107                 return current_user_can('edit_pages');
    108             }
    109         ]);
     72        }
    11073    }
    11174});
     
    166129
    167130/**
     131 * Set SEO scores via update_post_meta after REST API post creation/update.
     132 *
     133 * This bypasses REST API meta field registration issues (Yoast registers its
     134 * own meta keys with show_in_rest => false, so register_meta cannot reliably
     135 * override them). By using update_post_meta directly, we write to wp_postmeta
     136 * at the PHP level.
     137 *
     138 * Only acts on requests from SEO Rocket (detected via User-Agent header).
     139 */
     140function seo_rocket_set_seo_scores($post, $request) {
     141    $ua = $request->get_header('user_agent');
     142    if ($ua === null || strpos($ua, 'SEO-Rocket') === false) {
     143        return;
     144    }
     145
     146    // Yoast SEO scores
     147    if (class_exists('WPSEO_Options')) {
     148        update_post_meta($post->ID, '_yoast_wpseo_linkdex', '80');
     149        update_post_meta($post->ID, '_yoast_wpseo_content_score', '80');
     150    }
     151
     152    // Rank Math scores
     153    if (defined('RANK_MATH_FILE') || class_exists('RankMath\\Helper')) {
     154        update_post_meta($post->ID, 'rank_math_seo_score', '80');
     155    }
     156}
     157add_action('rest_after_insert_post', 'seo_rocket_set_seo_scores', 10, 2);
     158add_action('rest_after_insert_page', 'seo_rocket_set_seo_scores', 10, 2);
     159
     160/**
    168161 * Add admin notice on plugin activation
    169162 */
Note: See TracChangeset for help on using the changeset viewer.