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

Yoast / wordpress-seo / dd6e866a9e6d253114633104d9e3858d807178ba

19 Jun 2024 10:03AM UTC coverage: 48.628% (-4.3%) from 52.936%
dd6e866a9e6d253114633104d9e3858d807178ba

push

github

web-flow
Merge pull request #21431 from Yoast/21429-update-copy-in-the-introduction-and-consent-modals

Updates the copy for the introduction and consent modals

7441 of 13454 branches covered (55.31%)

Branch coverage included in aggregate %.

0 of 3 new or added lines in 2 files covered. (0.0%)

3718 existing lines in 107 files now uncovered.

25100 of 53464 relevant lines covered (46.95%)

62392.47 hits per line

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

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

8
/**
9
 * Class WPSEO_Import_WPSEO.
10
 *
11
 * Class with functionality to import & clean wpSEO.de post metadata.
12
 */
13
class WPSEO_Import_WPSEO extends WPSEO_Plugin_Importer {
14

15
        /**
16
         * The plugin name.
17
         *
18
         * @var string
19
         */
20
        protected $plugin_name = 'wpSEO.de';
21

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

29
        /**
30
         * Array of meta keys to detect and import.
31
         *
32
         * @var array
33
         */
34
        protected $clone_keys = [
35
                [
36
                        'old_key' => '_wpseo_edit_description',
37
                        'new_key' => 'metadesc',
38
                ],
39
                [
40
                        'old_key' => '_wpseo_edit_title',
41
                        'new_key' => 'title',
42
                ],
43
                [
44
                        'old_key' => '_wpseo_edit_canonical',
45
                        'new_key' => 'canonical',
46
                ],
47
                [
48
                        'old_key' => '_wpseo_edit_og_title',
49
                        'new_key' => 'opengraph-title',
50
                ],
51
                [
52
                        'old_key' => '_wpseo_edit_og_description',
53
                        'new_key' => 'opengraph-description',
54
                ],
55
                [
56
                        'old_key' => '_wpseo_edit_og_image',
57
                        'new_key' => 'opengraph-image',
58
                ],
59
                [
60
                        'old_key' => '_wpseo_edit_twittercard_title',
61
                        'new_key' => 'twitter-title',
62
                ],
63
                [
64
                        'old_key' => '_wpseo_edit_twittercard_description',
65
                        'new_key' => 'twitter-description',
66
                ],
67
                [
68
                        'old_key' => '_wpseo_edit_twittercard_image',
69
                        'new_key' => 'twitter-image',
70
                ],
71
        ];
72

73
        /**
74
         * The values 1 - 6 are the configured values from wpSEO. This array will map the values of wpSEO to our values.
75
         *
76
         * There are some double array like 1-6 and 3-4. The reason is they only set the index value. The follow value is
77
         * the default we use in the cases there isn't a follow value present.
78
         *
79
         * @var array
80
         */
81
        private $robot_values = [
82
                // In wpSEO: index, follow.
83
                1 => [
84
                        'index'  => 2,
85
                        'follow' => 0,
86
                ],
87
                // In wpSEO: index, nofollow.
88
                2 => [
89
                        'index'  => 2,
90
                        'follow' => 1,
91
                ],
92
                // In wpSEO: noindex.
93
                3 => [
94
                        'index'  => 1,
95
                        'follow' => 0,
96
                ],
97
                // In wpSEO: noindex, follow.
98
                4 => [
99
                        'index'  => 1,
100
                        'follow' => 0,
101
                ],
102
                // In wpSEO: noindex, nofollow.
103
                5 => [
104
                        'index'  => 1,
105
                        'follow' => 1,
106
                ],
107
                // In wpSEO: index.
108
                6 => [
109
                        'index'  => 2,
110
                        'follow' => 0,
111
                ],
112
        ];
113

114
        /**
115
         * Imports wpSEO settings.
116
         *
117
         * @return bool Import success status.
118
         */
UNCOV
119
        protected function import() {
×
UNCOV
120
                $status = parent::import();
×
UNCOV
121
                if ( $status ) {
×
UNCOV
122
                        $this->import_post_robots();
×
UNCOV
123
                        $this->import_taxonomy_metas();
×
124
                }
125

UNCOV
126
                return $status;
×
127
        }
128

129
        /**
130
         * Removes wpseo.de post meta's.
131
         *
132
         * @return bool Cleanup status.
133
         */
UNCOV
134
        protected function cleanup() {
×
UNCOV
135
                $this->cleanup_term_meta();
×
UNCOV
136
                $result = $this->cleanup_post_meta();
×
UNCOV
137
                return $result;
×
138
        }
139

140
        /**
141
         * Detects whether there is post meta data to import.
142
         *
143
         * @return bool Boolean indicating whether there is something to import.
144
         */
UNCOV
145
        protected function detect() {
×
UNCOV
146
                if ( parent::detect() ) {
×
UNCOV
147
                        return true;
×
148
                }
149

UNCOV
150
                global $wpdb;
×
UNCOV
151
                $count = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->options} WHERE option_name LIKE 'wpseo_category_%'" );
×
UNCOV
152
                if ( $count !== '0' ) {
×
UNCOV
153
                        return true;
×
154
                }
155

UNCOV
156
                return false;
×
157
        }
158

159
        /**
160
         * Imports the robot values from WPSEO plugin. These have to be converted to the Yoast format.
161
         *
162
         * @return void
163
         */
UNCOV
164
        private function import_post_robots() {
×
UNCOV
165
                $query_posts = new WP_Query( 'post_type=any&meta_key=_wpseo_edit_robots&order=ASC&fields=ids&nopaging=true' );
×
166

UNCOV
167
                if ( ! empty( $query_posts->posts ) ) {
×
UNCOV
168
                        foreach ( array_values( $query_posts->posts ) as $post_id ) {
×
UNCOV
169
                                $this->import_post_robot( $post_id );
×
170
                        }
171
                }
172
        }
173

174
        /**
175
         * Gets the wpSEO robot value and map this to Yoast SEO values.
176
         *
177
         * @param int $post_id The post id of the current post.
178
         *
179
         * @return void
180
         */
UNCOV
181
        private function import_post_robot( $post_id ) {
×
UNCOV
182
                $wpseo_robots = get_post_meta( $post_id, '_wpseo_edit_robots', true );
×
UNCOV
183
                $robot_value  = $this->get_robot_value( $wpseo_robots );
×
184

185
                // Saving the new meta values for Yoast SEO.
UNCOV
186
                $this->maybe_save_post_meta( 'meta-robots-noindex', $robot_value['index'], $post_id );
×
UNCOV
187
                $this->maybe_save_post_meta( 'meta-robots-nofollow', $robot_value['follow'], $post_id );
×
188
        }
189

190
        /**
191
         * Imports the taxonomy metas from wpSEO.
192
         *
193
         * @return void
194
         */
UNCOV
195
        private function import_taxonomy_metas() {
×
UNCOV
196
                $terms    = get_terms(
×
UNCOV
197
                        [
×
UNCOV
198
                                'taxonomy'   => get_taxonomies(),
×
UNCOV
199
                                'hide_empty' => false,
×
UNCOV
200
                        ]
×
UNCOV
201
                );
×
UNCOV
202
                $tax_meta = get_option( 'wpseo_taxonomy_meta' );
×
203

UNCOV
204
                foreach ( $terms as $term ) {
×
UNCOV
205
                        $this->import_taxonomy_description( $tax_meta, $term->taxonomy, $term->term_id );
×
UNCOV
206
                        $this->import_taxonomy_robots( $tax_meta, $term->taxonomy, $term->term_id );
×
207
                }
208

UNCOV
209
                update_option( 'wpseo_taxonomy_meta', $tax_meta );
×
210
        }
211

212
        /**
213
         * Imports the meta description to Yoast SEO.
214
         *
215
         * @param array  $tax_meta The array with the current metadata.
216
         * @param string $taxonomy String with the name of the taxonomy.
217
         * @param string $term_id  The ID of the current term.
218
         *
219
         * @return void
220
         */
UNCOV
221
        private function import_taxonomy_description( &$tax_meta, $taxonomy, $term_id ) {
×
UNCOV
222
                $description = get_option( 'wpseo_' . $taxonomy . '_' . $term_id, false );
×
UNCOV
223
                if ( $description !== false ) {
×
224
                        // Import description.
UNCOV
225
                        $tax_meta[ $taxonomy ][ $term_id ]['wpseo_desc'] = $description;
×
226
                }
227
        }
228

229
        /**
230
         * Imports the robot value to Yoast SEO.
231
         *
232
         * @param array  $tax_meta The array with the current metadata.
233
         * @param string $taxonomy String with the name of the taxonomy.
234
         * @param string $term_id  The ID of the current term.
235
         *
236
         * @return void
237
         */
UNCOV
238
        private function import_taxonomy_robots( &$tax_meta, $taxonomy, $term_id ) {
×
UNCOV
239
                $wpseo_robots = get_option( 'wpseo_' . $taxonomy . '_' . $term_id . '_robots', false );
×
UNCOV
240
                if ( $wpseo_robots === false ) {
×
UNCOV
241
                        return;
×
242
                }
243
                // The value 1, 2 and 6 are the index values in wpSEO.
UNCOV
244
                $new_robot_value = 'noindex';
×
245

UNCOV
246
                if ( in_array( (int) $wpseo_robots, [ 1, 2, 6 ], true ) ) {
×
UNCOV
247
                        $new_robot_value = 'index';
×
248
                }
249

UNCOV
250
                $tax_meta[ $taxonomy ][ $term_id ]['wpseo_noindex'] = $new_robot_value;
×
251
        }
252

253
        /**
254
         * Deletes the wpSEO taxonomy meta data.
255
         *
256
         * @param string $taxonomy String with the name of the taxonomy.
257
         * @param string $term_id  The ID of the current term.
258
         *
259
         * @return void
260
         */
UNCOV
261
        private function delete_taxonomy_metas( $taxonomy, $term_id ) {
×
UNCOV
262
                delete_option( 'wpseo_' . $taxonomy . '_' . $term_id );
×
UNCOV
263
                delete_option( 'wpseo_' . $taxonomy . '_' . $term_id . '_robots' );
×
264
        }
265

266
        /**
267
         * Gets the robot config by given wpSEO robots value.
268
         *
269
         * @param string $wpseo_robots The value in wpSEO that needs to be converted to the Yoast format.
270
         *
271
         * @return string The correct robot value.
272
         */
UNCOV
273
        private function get_robot_value( $wpseo_robots ) {
×
UNCOV
274
                if ( array_key_exists( $wpseo_robots, $this->robot_values ) ) {
×
UNCOV
275
                        return $this->robot_values[ $wpseo_robots ];
×
276
                }
277

UNCOV
278
                return $this->robot_values[1];
×
279
        }
280

281
        /**
282
         * Deletes wpSEO postmeta from the database.
283
         *
284
         * @return bool Cleanup status.
285
         */
UNCOV
286
        private function cleanup_post_meta() {
×
UNCOV
287
                global $wpdb;
×
288

289
                // If we get to replace the data, let's do some proper cleanup.
UNCOV
290
                return $wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE '_wpseo_edit_%'" );
×
291
        }
292

293
        /**
294
         * Cleans up the wpSEO term meta.
295
         *
296
         * @return void
297
         */
UNCOV
298
        private function cleanup_term_meta() {
×
UNCOV
299
                $terms = get_terms(
×
UNCOV
300
                        [
×
UNCOV
301
                                'taxonomy'   => get_taxonomies(),
×
UNCOV
302
                                'hide_empty' => false,
×
UNCOV
303
                        ]
×
UNCOV
304
                );
×
305

UNCOV
306
                foreach ( $terms as $term ) {
×
UNCOV
307
                        $this->delete_taxonomy_metas( $term->taxonomy, $term->term_id );
×
308
                }
309
        }
310
}
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

© 2025 Coveralls, Inc