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

Yoast / wordpress-seo / 61994a685e3bfb1c87a3471136a933256eab063a

06 Aug 2024 07:56AM UTC coverage: 48.697% (-5.4%) from 54.072%
61994a685e3bfb1c87a3471136a933256eab063a

push

github

YoastBot
Bump version to 23.2

7460 of 13482 branches covered (55.33%)

Branch coverage included in aggregate %.

25255 of 53699 relevant lines covered (47.03%)

42524.45 hits per line

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

0.0
/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() {
×
93
                global $wpdb;
×
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' )" );
×
96

97
                $this->import_meta_robots();
×
98
                $return = $this->meta_keys_clone( $this->clone_keys );
×
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' )" );
×
102

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

107
                return $return;
×
108
        }
109

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

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

154
                foreach ( $settings as $import_setting_key => $setting_key ) {
×
155
                        if ( ! empty( $options[ $import_setting_key ] ) ) {
×
156
                                $value = $options[ $import_setting_key ];
×
157
                                // Make sure replace vars work.
158
                                $value = str_replace( '%', '%%', $value );
×
159
                                WPSEO_Options::set( $setting_key, $value );
×
160
                        }
161
                }
162
        }
163

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

177
                return $return;
×
178
        }
179
}
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