• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 6987097851

25 Nov 2023 04:49AM UTC coverage: 49.206% (-0.1%) from 49.302%
6987097851

push

github

web-flow
Merge pull request #20878 from Yoast/JRF/ghactions-minor-tweak

GH Actions: update a few links in inline comments

15305 of 31104 relevant lines covered (49.21%)

4.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.92
/admin/import/plugins/class-import-rankmath.php
1
<?php
2
/**
3
 * File with the class to handle data from RankMath.
4
 *
5
 * @package WPSEO\Admin\Import\Plugins
6
 */
7

8
/**
9
 * Class with functionality to import RankMath post metadata.
10
 */
11
class WPSEO_Import_RankMath extends WPSEO_Plugin_Importer {
12

13
        /**
14
         * The plugin name.
15
         *
16
         * @var string
17
         */
18
        protected $plugin_name = 'RankMath';
19

20
        /**
21
         * Meta key, used in SQL LIKE clause for delete query.
22
         *
23
         * @var string
24
         */
25
        protected $meta_key = 'rank_math_%';
26

27
        /**
28
         * Array of meta keys to detect and import.
29
         *
30
         * @var array
31
         */
32
        protected $clone_keys = [
33
                [
34
                        'old_key' => 'rank_math_description',
35
                        'new_key' => 'metadesc',
36
                ],
37
                [
38
                        'old_key' => 'rank_math_title',
39
                        'new_key' => 'title',
40
                ],
41
                [
42
                        'old_key' => 'rank_math_canonical_url',
43
                        'new_key' => 'canonical',
44
                ],
45
                [
46
                        'old_key' => 'rank_math_primary_category',
47
                        'new_key' => 'primary_category',
48
                ],
49
                [
50
                        'old_key' => 'rank_math_facebook_title',
51
                        'new_key' => 'opengraph-title',
52
                ],
53
                [
54
                        'old_key' => 'rank_math_facebook_description',
55
                        'new_key' => 'opengraph-description',
56
                ],
57
                [
58
                        'old_key' => 'rank_math_facebook_image',
59
                        'new_key' => 'opengraph-image',
60
                ],
61
                [
62
                        'old_key' => 'rank_math_facebook_image_id',
63
                        'new_key' => 'opengraph-image-id',
64
                ],
65
                [
66
                        'old_key' => 'rank_math_twitter_title',
67
                        'new_key' => 'twitter-title',
68
                ],
69
                [
70
                        'old_key' => 'rank_math_twitter_description',
71
                        'new_key' => 'twitter-description',
72
                ],
73
                [
74
                        'old_key' => 'rank_math_twitter_image',
75
                        'new_key' => 'twitter-image',
76
                ],
77
                [
78
                        'old_key' => 'rank_math_twitter_image_id',
79
                        'new_key' => 'twitter-image-id',
80
                ],
81
                [
82
                        'old_key' => 'rank_math_focus_keyword',
83
                        'new_key' => 'focuskw',
84
                ],
85
        ];
86

87
        /**
88
         * Handles post meta data to import.
89
         *
90
         * @return bool Import success status.
91
         */
92
        protected function import() {
4✔
93
                global $wpdb;
4✔
94
                // Replace % with %% as their variables are the same except for that.
95
                $wpdb->query( "UPDATE $wpdb->postmeta SET meta_value = REPLACE( meta_value, '%', '%%' ) WHERE meta_key IN ( 'rank_math_description', 'rank_math_title' )" );
4✔
96

97
                $this->import_meta_robots();
4✔
98
                $return = $this->meta_keys_clone( $this->clone_keys );
4✔
99

100
                // Return %% to % so our import is non-destructive.
101
                $wpdb->query( "UPDATE $wpdb->postmeta SET meta_value = REPLACE( meta_value, '%%', '%' ) WHERE meta_key IN ( 'rank_math_description', 'rank_math_title' )" );
4✔
102

103
                if ( $return ) {
4✔
104
                        $this->import_settings();
4✔
105
                }
106

107
                return $return;
4✔
108
        }
109

110
        /**
111
         * RankMath stores robots meta quite differently, so we have to parse it out.
112
         */
113
        private function import_meta_robots() {
×
114
                global $wpdb;
×
115
                $post_metas = $wpdb->get_results( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = 'rank_math_robots'" );
×
116
                foreach ( $post_metas as $post_meta ) {
×
117
                        // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- Reason: We can't control the form in which Rankmath sends the data.
118
                        $robots_values = unserialize( $post_meta->meta_value );
×
119
                        foreach ( [ 'noindex', 'nofollow' ] as $directive ) {
×
120
                                $directive_key = array_search( $directive, $robots_values, true );
×
121
                                if ( $directive_key !== false ) {
×
122
                                        update_post_meta( $post_meta->post_id, '_yoast_wpseo_meta-robots-' . $directive, 1 );
×
123
                                        unset( $robots_values[ $directive_key ] );
×
124
                                }
125
                        }
126
                        if ( count( $robots_values ) > 0 ) {
×
127
                                $value = implode( ',', $robots_values );
×
128
                                update_post_meta( $post_meta->post_id, '_yoast_wpseo_meta-robots-adv', $value );
×
129
                        }
130
                }
131
        }
132

133
        /**
134
         * Imports some of the RankMath settings.
135
         */
136
        private function import_settings() {
4✔
137
                $settings = [
2✔
138
                        'title_separator'      => 'separator',
4✔
139
                        'homepage_title'       => 'title-home-wpseo',
2✔
140
                        'homepage_description' => 'metadesc-home-wpseo',
2✔
141
                        'author_archive_title' => 'title-author-wpseo',
2✔
142
                        'date_archive_title'   => 'title-archive-wpseo',
2✔
143
                        'search_title'         => 'title-search-wpseo',
2✔
144
                        '404_title'            => 'title-404-wpseo',
2✔
145
                        'pt_post_title'        => 'title-post',
2✔
146
                        'pt_page_title'        => 'title-page',
2✔
147
                ];
2✔
148
                $options  = get_option( 'rank-math-options-titles' );
4✔
149

150
                foreach ( $settings as $import_setting_key => $setting_key ) {
4✔
151
                        if ( ! empty( $options[ $import_setting_key ] ) ) {
4✔
152
                                $value = $options[ $import_setting_key ];
4✔
153
                                // Make sure replace vars work.
154
                                $value = str_replace( '%', '%%', $value );
4✔
155
                                WPSEO_Options::set( $setting_key, $value );
4✔
156
                        }
157
                }
158
        }
2✔
159

160
        /**
161
         * Removes the plugin data from the database.
162
         *
163
         * @return bool Cleanup status.
164
         */
165
        protected function cleanup() {
4✔
166
                $return = parent::cleanup();
4✔
167
                if ( $return ) {
4✔
168
                        global $wpdb;
4✔
169
                        $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'rank-math-%'" );
4✔
170
                        $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '%rank_math%'" );
4✔
171
                }
172

173
                return $return;
4✔
174
        }
175
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc