• 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
/inc/class-wpseo-meta.php
1
<?php
2
/**
3
 * WPSEO plugin file.
4
 *
5
 * @package WPSEO\Internals
6
 * @since   1.5.0
7
 */
8

9
use Yoast\WP\SEO\Config\Schema_Types;
10
use Yoast\WP\SEO\Helpers\Schema\Article_Helper;
11
use Yoast\WP\SEO\Repositories\Indexable_Repository;
12

13
/**
14
 * This class implements defaults and value validation for all WPSEO Post Meta values.
15
 *
16
 * Some guidelines:
17
 * - To update a meta value, you can just use update_post_meta() with the full (prefixed) meta key
18
 *   or the convenience method WPSEO_Meta::set_value() with the internal key.
19
 *   All updates will be automatically validated.
20
 *   Meta values will only be saved to the database if they are *not* the same as the default to
21
 *   keep database load low.
22
 * - To retrieve a WPSEO meta value, you **must** use WPSEO_Meta::get_value() which will always return a
23
 *   string value, either the saved value or the default.
24
 *   This method can also retrieve a complete set of WPSEO meta values for one specific post, see
25
 *   the method documentation for the parameters.
26
 *
27
 * {@internal Unfortunately there isn't a filter available to hook into before returning the results
28
 *            for get_post_meta(), get_post_custom() and the likes. That would have been the
29
 *            preferred solution.}}
30
 *
31
 * {@internal All WP native get_meta() results get cached internally, so no need to cache locally.}}
32
 * {@internal Use $key when the key is the WPSEO internal name (without prefix), $meta_key when it
33
 *            includes the prefix.}}
34
 */
35
class WPSEO_Meta {
36

37
        /**
38
         * Prefix for all WPSEO meta values in the database.
39
         *
40
         * {@internal If at any point this would change, quite apart from an upgrade routine,
41
         *            this also will need to be changed in the wpml-config.xml file.}}
42
         *
43
         * @var string
44
         */
45
        public static $meta_prefix = '_yoast_wpseo_';
46

47
        /**
48
         * Prefix for all WPSEO meta value form field names and ids.
49
         *
50
         * @var string
51
         */
52
        public static $form_prefix = 'yoast_wpseo_';
53

54
        /**
55
         * Allowed length of the meta description.
56
         *
57
         * @var int
58
         */
59
        public static $meta_length = 156;
60

61
        /**
62
         * Reason the meta description is not the default length.
63
         *
64
         * @var string
65
         */
66
        public static $meta_length_reason = '';
67

68
        /**
69
         * Meta box field definitions for the meta box form.
70
         *
71
         * {@internal
72
         * - Titles, help texts, description text and option labels are added via a translate_meta_boxes() method
73
         *   in the relevant child classes (WPSEO_Metabox and WPSEO_Social_admin) as they are only needed there.
74
         * - Beware: even though the meta keys are divided into subsets, they still have to be uniquely named!}}
75
         *
76
         * @var array
77
         *            Array format:
78
         *                (required)       'type'          => (string) field type. i.e. text / textarea / checkbox /
79
         *                                                    radio / select / multiselect / upload etc.
80
         *                (required)       'title'         => (string) table row title.
81
         *                (recommended)    'default_value' => (string|array) default value for the field.
82
         *                                                    IMPORTANT:
83
         *                                                    - if the field has options, the default has to be the
84
         *                                                      key of one of the options.
85
         *                                                    - if the field is a text field, the default **has** to be
86
         *                                                      an empty string as otherwise the user can't save
87
         *                                                      an empty value/delete the meta value.
88
         *                                                    - if the field is a checkbox, the only valid values
89
         *                                                      are 'on' or 'off'.
90
         *                (semi-required)   'options'      => (array) options for used with (multi-)select and radio
91
         *                                                    fields, required if that's the field type.
92
         *                                                    key = (string) value which will be saved to db.
93
         *                                                    value = (string) text label for the option.
94
         *                (optional)        'autocomplete' => (bool) whether autocomplete is on for text fields,
95
         *                                                    defaults to true.
96
         *                (optional)        'class'        => (string) classname(s) to add to the actual <input> tag.
97
         *                (optional)        'description'  => (string) description to show underneath the field.
98
         *                (optional)        'expl'         => (string) label for a checkbox.
99
         *                (optional)        'help'         => (string) help text to show on mouse over ? image.
100
         *                (optional)        'rows'         => (int) number of rows for a textarea, defaults to 3.
101
         *                (optional)        'placeholder'  => (string) Currently only used by add-on plugins.
102
         *                (optional)        'serialized'   => (bool) whether the value is expected to be serialized,
103
         *                                                     i.e. an array or object, defaults to false.
104
         *                                                     Currently only used by add-on plugins.
105
         */
106
        public static $meta_fields = [
107
                'general'  => [
108
                        'focuskw' => [
109
                                'type'  => 'hidden',
110
                                'title' => '',
111
                        ],
112
                        'title' => [
113
                                'type'          => 'hidden',
114
                                'title'         => '', // Translation added later.
115
                                'default_value' => '',
116
                                'description'   => '', // Translation added later.
117
                                'help'          => '', // Translation added later.
118
                        ],
119
                        'metadesc' => [
120
                                'type'          => 'hidden',
121
                                'title'         => '', // Translation added later.
122
                                'default_value' => '',
123
                                'class'         => 'metadesc',
124
                                'rows'          => 2,
125
                                'description'   => '', // Translation added later.
126
                                'help'          => '', // Translation added later.
127
                        ],
128
                        'linkdex' => [
129
                                'type'          => 'hidden',
130
                                'title'         => 'linkdex',
131
                                'default_value' => '0',
132
                                'description'   => '',
133
                        ],
134
                        'content_score' => [
135
                                'type'          => 'hidden',
136
                                'title'         => 'content_score',
137
                                'default_value' => '0',
138
                                'description'   => '',
139
                        ],
140
                        'inclusive_language_score' => [
141
                                'type'          => 'hidden',
142
                                'title'         => 'inclusive_language_score',
143
                                'default_value' => '0',
144
                                'description'   => '',
145
                        ],
146
                        'is_cornerstone' => [
147
                                'type'          => 'hidden',
148
                                'title'         => 'is_cornerstone',
149
                                'default_value' => 'false',
150
                                'description'   => '',
151
                        ],
152
                ],
153
                'advanced' => [
154
                        'meta-robots-noindex'  => [
155
                                'type'          => 'hidden',
156
                                'title'         => '', // Translation added later.
157
                                'default_value' => '0', // = post-type default.
158
                                'options'       => [
159
                                        '0' => '', // Post type default - translation added later.
160
                                        '2' => '', // Index - translation added later.
161
                                        '1' => '', // No-index - translation added later.
162
                                ],
163
                        ],
164
                        'meta-robots-nofollow' => [
165
                                'type'          => 'hidden',
166
                                'title'         => '', // Translation added later.
167
                                'default_value' => '0', // = follow.
168
                                'options'       => [
169
                                        '0' => '', // Follow - translation added later.
170
                                        '1' => '', // No-follow - translation added later.
171
                                ],
172
                        ],
173
                        'meta-robots-adv'      => [
174
                                'type'          => 'hidden',
175
                                'title'         => '', // Translation added later.
176
                                'default_value' => '',
177
                                'description'   => '', // Translation added later.
178
                                'options'       => [
179
                                        'noimageindex' => '', // Translation added later.
180
                                        'noarchive'    => '', // Translation added later.
181
                                        'nosnippet'    => '', // Translation added later.
182
                                ],
183
                        ],
184
                        'bctitle'              => [
185
                                'type'          => 'hidden',
186
                                'title'         => '', // Translation added later.
187
                                'default_value' => '',
188
                                'description'   => '', // Translation added later.
189
                        ],
190
                        'canonical'            => [
191
                                'type'          => 'hidden',
192
                                'title'         => '', // Translation added later.
193
                                'default_value' => '',
194
                                'description'   => '', // Translation added later.
195
                        ],
196
                        'redirect'             => [
197
                                'type'          => 'url',
198
                                'title'         => '', // Translation added later.
199
                                'default_value' => '',
200
                                'description'   => '', // Translation added later.
201
                        ],
202
                ],
203
                'social'   => [],
204
                'schema'   => [
205
                        'schema_page_type'    => [
206
                                'type'    => 'hidden',
207
                                'title'   => '',
208
                                'options' => Schema_Types::PAGE_TYPES,
209
                        ],
210
                        'schema_article_type' => [
211
                                'type'          => 'hidden',
212
                                'title'         => '',
213
                                'hide_on_pages' => true,
214
                                'options'       => Schema_Types::ARTICLE_TYPES,
215
                        ],
216
                ],
217
                /* Fields we should validate & save, but not show on any form. */
218
                'non_form' => [
219
                        'linkdex' => [
220
                                'type'          => null,
221
                                'default_value' => '0',
222
                        ],
223
                        'zapier_trigger_sent' => [
224
                                'type'          => null,
225
                                'default_value' => '0',
226
                        ],
227
                ],
228
        ];
229

230
        /**
231
         * Helper property - reverse index of the definition array.
232
         *
233
         * Format: [full meta key including prefix]    => array
234
         *         ['subset']    => (string) primary index
235
         *         ['key']       => (string) internal key
236
         *
237
         * @var array
238
         */
239
        public static $fields_index = [];
240

241
        /**
242
         * Helper property - array containing only the defaults in the format:
243
         * [full meta key including prefix]    => (string) default value
244
         *
245
         * @var array
246
         */
247
        public static $defaults = [];
248

249
        /**
250
         * Helper property to define the social network meta field definitions - networks.
251
         *
252
         * @var array
253
         */
254
        private static $social_networks = [
255
                'opengraph'  => 'opengraph',
256
                'twitter'    => 'twitter',
257
        ];
258

259
        /**
260
         * Helper property to define the social network meta field definitions - fields and their type.
261
         *
262
         * @var array
263
         */
264
        private static $social_fields = [
265
                'title'       => 'hidden',
266
                'description' => 'hidden',
267
                'image'       => 'hidden',
268
                'image-id'    => 'hidden',
269
        ];
270

271
        /**
272
         * Register our actions and filters.
273
         *
274
         * @return void
275
         */
276
        public static function init() {
×
277
                foreach ( self::$social_networks as $option => $network ) {
×
278
                        if ( WPSEO_Options::get( $option, false ) === true ) {
×
279
                                foreach ( self::$social_fields as $box => $type ) {
×
280
                                        self::$meta_fields['social'][ $network . '-' . $box ] = [
×
281
                                                'type'          => $type,
×
282
                                                'title'         => '', // Translation added later.
×
283
                                                'default_value' => '',
×
284
                                                'description'   => '', // Translation added later.
×
285
                                        ];
286
                                }
287
                        }
288
                }
289
                unset( $option, $network, $box, $type );
×
290

291
                /**
292
                 * Allow add-on plugins to register their meta fields for management by this class.
293
                 * Calls to add_filter() must be made before plugins_loaded prio 14.
294
                 */
295
                $extra_fields = apply_filters( 'add_extra_wpseo_meta_fields', [] );
×
296
                if ( is_array( $extra_fields ) ) {
×
297
                        self::$meta_fields = self::array_merge_recursive_distinct( $extra_fields, self::$meta_fields );
×
298
                }
299
                unset( $extra_fields );
×
300

301
                foreach ( self::$meta_fields as $subset => $field_group ) {
×
302
                        foreach ( $field_group as $key => $field_def ) {
×
303

304
                                register_meta(
×
305
                                        'post',
×
306
                                        self::$meta_prefix . $key,
×
307
                                        [ 'sanitize_callback' => [ self::class, 'sanitize_post_meta' ] ]
×
308
                                );
309

310
                                // Set the $fields_index property for efficiency.
311
                                self::$fields_index[ self::$meta_prefix . $key ] = [
×
312
                                        'subset' => $subset,
×
313
                                        'key'    => $key,
×
314
                                ];
315

316
                                // Set the $defaults property for efficiency.
317
                                if ( isset( $field_def['default_value'] ) ) {
×
318
                                        self::$defaults[ self::$meta_prefix . $key ] = $field_def['default_value'];
×
319
                                }
320
                                else {
321
                                        // Meta will always be a string, so let's make the meta meta default also a string.
322
                                        self::$defaults[ self::$meta_prefix . $key ] = '';
×
323
                                }
324
                        }
325
                }
326
                unset( $subset, $field_group, $key, $field_def );
×
327

328
                self::filter_schema_article_types();
×
329

330
                add_filter( 'update_post_metadata', [ self::class, 'remove_meta_if_default' ], 10, 5 );
×
331
                add_filter( 'add_post_metadata', [ self::class, 'dont_save_meta_if_default' ], 10, 4 );
×
332
        }
333

334
        /**
335
         * Retrieve the meta box form field definitions for the given tab and post type.
336
         *
337
         * @param string $tab       Tab for which to retrieve the field definitions.
338
         * @param string $post_type Post type of the current post.
339
         *
340
         * @return array Array containing the meta box field definitions.
341
         */
342
        public static function get_meta_field_defs( $tab, $post_type = 'post' ) {
×
343
                if ( ! isset( self::$meta_fields[ $tab ] ) ) {
×
344
                        return [];
×
345
                }
346

347
                $field_defs = self::$meta_fields[ $tab ];
×
348

349
                switch ( $tab ) {
350
                        case 'non-form':
×
351
                                // Prevent non-form fields from being passed to forms.
352
                                $field_defs = [];
×
353
                                break;
×
354

355
                        case 'advanced':
×
356
                                global $post;
×
357

358
                                if ( ! WPSEO_Capability_Utils::current_user_can( 'wpseo_edit_advanced_metadata' ) && WPSEO_Options::get( 'disableadvanced_meta' ) ) {
×
359
                                        return [];
×
360
                                }
361

362
                                $post_type = '';
×
363
                                if ( isset( $post->post_type ) ) {
×
364
                                        $post_type = $post->post_type;
×
365
                                }
366
                                elseif ( ! isset( $post->post_type ) && isset( $_GET['post_type'] ) ) {
×
367
                                        $post_type = sanitize_text_field( $_GET['post_type'] );
×
368
                                }
369

370
                                if ( $post_type === '' ) {
×
371
                                        return [];
×
372
                                }
373

374
                                /* Adjust the no-index text strings based on the post type. */
375
                                $post_type_object = get_post_type_object( $post_type );
×
376

377
                                $field_defs['meta-robots-noindex']['title']        = sprintf( $field_defs['meta-robots-noindex']['title'], $post_type_object->labels->singular_name );
×
378
                                $field_defs['meta-robots-noindex']['options']['0'] = sprintf( $field_defs['meta-robots-noindex']['options']['0'], ( ( WPSEO_Options::get( 'noindex-' . $post_type, false ) === true ) ? $field_defs['meta-robots-noindex']['options']['1'] : $field_defs['meta-robots-noindex']['options']['2'] ), $post_type_object->label );
×
379
                                $field_defs['meta-robots-nofollow']['title']       = sprintf( $field_defs['meta-robots-nofollow']['title'], $post_type_object->labels->singular_name );
×
380

381
                                /* Don't show the breadcrumb title field if breadcrumbs aren't enabled. */
382
                                if ( WPSEO_Options::get( 'breadcrumbs-enable', false ) !== true && ! current_theme_supports( 'yoast-seo-breadcrumbs' ) ) {
×
383
                                        unset( $field_defs['bctitle'] );
×
384
                                }
385

386
                                if ( empty( $post->ID ) || ( ! empty( $post->ID ) && self::get_value( 'redirect', $post->ID ) === '' ) ) {
×
387
                                        unset( $field_defs['redirect'] );
×
388
                                }
389
                                break;
×
390

391
                        case 'schema':
×
392
                                if ( ! WPSEO_Capability_Utils::current_user_can( 'wpseo_edit_advanced_metadata' ) && WPSEO_Options::get( 'disableadvanced_meta' ) ) {
×
393
                                        return [];
×
394
                                }
395

396
                                $field_defs['schema_page_type']['default'] = WPSEO_Options::get( 'schema-page-type-' . $post_type );
×
397

398
                                $article_helper = new Article_Helper();
×
399
                                if ( $article_helper->is_article_post_type( $post_type ) ) {
×
400
                                        $default_schema_article_type = WPSEO_Options::get( 'schema-article-type-' . $post_type );
×
401

402
                                        /** This filter is documented in inc/options/class-wpseo-option-titles.php */
403
                                        $allowed_article_types = apply_filters( 'wpseo_schema_article_types', Schema_Types::ARTICLE_TYPES );
×
404

405
                                        if ( ! array_key_exists( $default_schema_article_type, $allowed_article_types ) ) {
×
406
                                                $default_schema_article_type = WPSEO_Options::get_default( 'wpseo_titles', 'schema-article-type-' . $post_type );
×
407
                                        }
408
                                        $field_defs['schema_article_type']['default'] = $default_schema_article_type;
×
409
                                }
410
                                else {
411
                                        unset( $field_defs['schema_article_type'] );
×
412
                                }
413

414
                                break;
×
415
                }
416

417
                /**
418
                 * Filter the WPSEO metabox form field definitions for a tab.
419
                 * {tab} can be 'general', 'advanced' or 'social'.
420
                 *
421
                 * @param array  $field_defs Metabox form field definitions.
422
                 * @param string $post_type  Post type of the post the metabox is for, defaults to 'post'.
423
                 *
424
                 * @return array
425
                 */
426
                return apply_filters( 'wpseo_metabox_entries_' . $tab, $field_defs, $post_type );
×
427
        }
428

429
        /**
430
         * Validate the post meta values.
431
         *
432
         * @param mixed  $meta_value The new value.
433
         * @param string $meta_key   The full meta key (including prefix).
434
         *
435
         * @return string Validated meta value.
436
         */
437
        public static function sanitize_post_meta( $meta_value, $meta_key ) {
×
438
                $field_def = self::$meta_fields[ self::$fields_index[ $meta_key ]['subset'] ][ self::$fields_index[ $meta_key ]['key'] ];
×
439
                $clean     = self::$defaults[ $meta_key ];
×
440

441
                switch ( true ) {
442
                        case ( $meta_key === self::$meta_prefix . 'linkdex' ):
×
443
                                $int = WPSEO_Utils::validate_int( $meta_value );
×
444
                                if ( $int !== false && $int >= 0 ) {
×
445
                                        $clean = strval( $int ); // Convert to string to make sure default check works.
×
446
                                }
447
                                break;
×
448

449
                        case ( $field_def['type'] === 'checkbox' ):
×
450
                                // Only allow value if it's one of the predefined options.
451
                                if ( in_array( $meta_value, [ 'on', 'off' ], true ) ) {
×
452
                                        $clean = $meta_value;
×
453
                                }
454
                                break;
×
455

456
                        case ( $field_def['type'] === 'select' || $field_def['type'] === 'radio' ):
×
457
                                // Only allow value if it's one of the predefined options.
458
                                if ( isset( $field_def['options'][ $meta_value ] ) ) {
×
459
                                        $clean = $meta_value;
×
460
                                }
461
                                break;
×
462

463
                        case ( $field_def['type'] === 'hidden' && $meta_key === self::$meta_prefix . 'meta-robots-adv' ):
×
464
                                $clean = self::validate_meta_robots_adv( $meta_value );
×
465
                                break;
×
466

467
                        case ( $field_def['type'] === 'url' || $meta_key === self::$meta_prefix . 'canonical' ):
×
468
                                // Validate as url(-part).
469
                                $url = WPSEO_Utils::sanitize_url( $meta_value );
×
470
                                if ( $url !== '' ) {
×
471
                                        $clean = $url;
×
472
                                }
473
                                break;
×
474

475
                        case ( $field_def['type'] === 'upload' && in_array( $meta_key, [ self::$meta_prefix . 'opengraph-image', self::$meta_prefix . 'twitter-image' ], true ) ):
×
476
                                // Validate as url.
477
                                $url = WPSEO_Utils::sanitize_url( $meta_value, [ 'http', 'https', 'ftp', 'ftps' ] );
×
478
                                if ( $url !== '' ) {
×
479
                                        $clean = $url;
×
480
                                }
481
                                break;
×
482

483
                        case ( $field_def['type'] === 'hidden' && $meta_key === self::$meta_prefix . 'is_cornerstone' ):
×
484
                                $clean = $meta_value;
×
485

486
                                /*
487
                                 * This used to be a checkbox, then became a hidden input.
488
                                 * To make sure the value remains consistent, we cast 'true' to '1'.
489
                                 */
490
                                if ( $meta_value === 'true' ) {
×
491
                                        $clean = '1';
×
492
                                }
493
                                break;
×
494

495
                        case ( $field_def['type'] === 'hidden' && isset( $field_def['options'] ) ):
×
496
                                // Only allow value if it's one of the predefined options.
497
                                if ( isset( $field_def['options'][ $meta_value ] ) ) {
×
498
                                        $clean = $meta_value;
×
499
                                }
500
                                break;
×
501

502
                        case ( $field_def['type'] === 'textarea' ):
×
503
                                if ( is_string( $meta_value ) ) {
×
504
                                        // Remove line breaks and tabs.
505
                                        // @todo [JRF => Yoast] Verify that line breaks and the likes aren't allowed/recommended in meta header fields.
506
                                        $meta_value = str_replace( [ "\n", "\r", "\t", '  ' ], ' ', $meta_value );
×
507
                                        $clean      = WPSEO_Utils::sanitize_text_field( trim( $meta_value ) );
×
508
                                }
509
                                break;
×
510

511
                        case ( $field_def['type'] === 'multiselect' ):
×
512
                                $clean = $meta_value;
×
513
                                break;
×
514

515
                        case ( $field_def['type'] === 'text' ):
×
516
                        default:
517
                                if ( is_string( $meta_value ) ) {
×
518
                                        $clean = WPSEO_Utils::sanitize_text_field( trim( $meta_value ) );
×
519
                                }
520

521
                                break;
×
522
                }
523

524
                $clean = apply_filters( 'wpseo_sanitize_post_meta_' . $meta_key, $clean, $meta_value, $field_def, $meta_key );
×
525

526
                return $clean;
×
527
        }
528

529
        /**
530
         * Validate a meta-robots-adv meta value.
531
         *
532
         * @todo [JRF => Yoast] Verify that this logic for the prioritisation is correct.
533
         *
534
         * @param array|string $meta_value The value to validate.
535
         *
536
         * @return string Clean value.
537
         */
UNCOV
538
        public static function validate_meta_robots_adv( $meta_value ) {
×
UNCOV
539
                $clean   = self::$meta_fields['advanced']['meta-robots-adv']['default_value'];
×
UNCOV
540
                $options = self::$meta_fields['advanced']['meta-robots-adv']['options'];
×
541

UNCOV
542
                if ( is_string( $meta_value ) ) {
×
UNCOV
543
                        $meta_value = explode( ',', $meta_value );
×
544
                }
545

UNCOV
546
                if ( is_array( $meta_value ) && $meta_value !== [] ) {
×
UNCOV
547
                        $meta_value = array_map( 'trim', $meta_value );
×
548

549
                        // Individual selected entries.
UNCOV
550
                        $cleaning = [];
×
UNCOV
551
                        foreach ( $meta_value as $value ) {
×
UNCOV
552
                                if ( isset( $options[ $value ] ) ) {
×
UNCOV
553
                                        $cleaning[] = $value;
×
554
                                }
555
                        }
556

UNCOV
557
                        if ( $cleaning !== [] ) {
×
UNCOV
558
                                $clean = implode( ',', $cleaning );
×
559
                        }
UNCOV
560
                        unset( $cleaning, $value );
×
561
                }
562

UNCOV
563
                return $clean;
×
564
        }
565

566
        /**
567
         * Prevent saving of default values and remove potential old value from the database if replaced by a default.
568
         *
569
         * @param bool   $check      The current status to allow updating metadata for the given type.
570
         * @param int    $object_id  ID of the current object for which the meta is being updated.
571
         * @param string $meta_key   The full meta key (including prefix).
572
         * @param string $meta_value New meta value.
573
         * @param string $prev_value The old meta value.
574
         *
575
         * @return bool|null True = stop saving, null = continue saving.
576
         */
UNCOV
577
        public static function remove_meta_if_default( $check, $object_id, $meta_key, $meta_value, $prev_value = '' ) {
×
578
                /* If it's one of our meta fields, check against default. */
UNCOV
579
                if ( isset( self::$fields_index[ $meta_key ] ) && self::meta_value_is_default( $meta_key, $meta_value ) === true ) {
×
UNCOV
580
                        if ( $prev_value !== '' ) {
×
581
                                delete_post_meta( $object_id, $meta_key, $prev_value );
×
582
                        }
583
                        else {
UNCOV
584
                                delete_post_meta( $object_id, $meta_key );
×
585
                        }
586

UNCOV
587
                        return true; // Stop saving the value.
×
588
                }
589

590
                return $check; // Go on with the normal execution (update) in meta.php.
×
591
        }
592

593
        /**
594
         * Prevent adding of default values to the database.
595
         *
596
         * @param bool   $check      The current status to allow adding metadata for the given type.
597
         * @param int    $object_id  ID of the current object for which the meta is being added.
598
         * @param string $meta_key   The full meta key (including prefix).
599
         * @param string $meta_value New meta value.
600
         *
601
         * @return bool|null True = stop saving, null = continue saving.
602
         */
UNCOV
603
        public static function dont_save_meta_if_default( $check, $object_id, $meta_key, $meta_value ) {
×
604
                /* If it's one of our meta fields, check against default. */
UNCOV
605
                if ( isset( self::$fields_index[ $meta_key ] ) && self::meta_value_is_default( $meta_key, $meta_value ) === true ) {
×
UNCOV
606
                        return true; // Stop saving the value.
×
607
                }
608

UNCOV
609
                return $check; // Go on with the normal execution (add) in meta.php.
×
610
        }
611

612
        /**
613
         * Is the given meta value the same as the default value ?
614
         *
615
         * @param string $meta_key   The full meta key (including prefix).
616
         * @param mixed  $meta_value The value to check.
617
         *
618
         * @return bool
619
         */
UNCOV
620
        public static function meta_value_is_default( $meta_key, $meta_value ) {
×
UNCOV
621
                return ( isset( self::$defaults[ $meta_key ] ) && $meta_value === self::$defaults[ $meta_key ] );
×
622
        }
623

624
        /**
625
         * Get a custom post meta value.
626
         *
627
         * Returns the default value if the meta value has not been set.
628
         *
629
         * {@internal Unfortunately there isn't a filter available to hook into before returning
630
         *            the results for get_post_meta(), get_post_custom() and the likes. That
631
         *            would have been the preferred solution.}}
632
         *
633
         * @param string $key    Internal key of the value to get (without prefix).
634
         * @param int    $postid Post ID of the post to get the value for.
635
         *
636
         * @return string All 'normal' values returned from get_post_meta() are strings.
637
         *                Objects and arrays are possible, but not used by this plugin
638
         *                and therefore discarted (except when the special 'serialized' field def
639
         *                value is set to true - only used by add-on plugins for now).
640
         *                Will return the default value if no value was found.
641
         *                Will return empty string if no default was found (not one of our keys) or
642
         *                if the post does not exist.
643
         */
UNCOV
644
        public static function get_value( $key, $postid = 0 ) {
×
UNCOV
645
                global $post;
×
646

UNCOV
647
                $postid = absint( $postid );
×
UNCOV
648
                if ( $postid === 0 ) {
×
UNCOV
649
                        if ( ( isset( $post ) && is_object( $post ) ) && ( isset( $post->post_status ) && $post->post_status !== 'auto-draft' ) ) {
×
UNCOV
650
                                $postid = $post->ID;
×
651
                        }
652
                        else {
653
                                return '';
×
654
                        }
655
                }
656

UNCOV
657
                $custom    = get_post_custom( $postid ); // Array of strings or empty array.
×
UNCOV
658
                $table_key = self::$meta_prefix . $key;
×
659

660
                // Populate the field_def using the field_index lookup array.
UNCOV
661
                $field_def = [];
×
UNCOV
662
                if ( isset( self::$fields_index[ $table_key ] ) ) {
×
UNCOV
663
                        $field_def = self::$meta_fields[ self::$fields_index[ $table_key ]['subset'] ][ self::$fields_index[ $table_key ]['key'] ];
×
664
                }
665

666
                // Check if we have a custom post meta entry.
UNCOV
667
                if ( isset( $custom[ $table_key ][0] ) ) {
×
UNCOV
668
                        $unserialized = maybe_unserialize( $custom[ $table_key ][0] );
×
669

670
                        // Check if it is already unserialized.
UNCOV
671
                        if ( $custom[ $table_key ][0] === $unserialized ) {
×
UNCOV
672
                                return $custom[ $table_key ][0];
×
673
                        }
674

675
                        // Check whether we need to unserialize it.
UNCOV
676
                        if ( isset( $field_def['serialized'] ) && $field_def['serialized'] === true ) {
×
677
                                // Ok, serialize value expected/allowed.
UNCOV
678
                                return $unserialized;
×
679
                        }
680
                }
681

682
                // Meta was either not found or found, but object/array while not allowed to be.
UNCOV
683
                if ( isset( self::$defaults[ self::$meta_prefix . $key ] ) ) {
×
684
                        // Update the default value to the current post type.
685
                        switch ( $key ) {
UNCOV
686
                                case 'schema_page_type':
×
UNCOV
687
                                case 'schema_article_type':
×
UNCOV
688
                                        return '';
×
689
                        }
690

UNCOV
691
                        return self::$defaults[ self::$meta_prefix . $key ];
×
692
                }
693

694
                /*
695
                 * Shouldn't ever happen, means not one of our keys as there will always be a default available
696
                 * for all our keys.
697
                 */
UNCOV
698
                return '';
×
699
        }
700

701
        /**
702
         * Update a meta value for a post.
703
         *
704
         * @param string $key        The internal key of the meta value to change (without prefix).
705
         * @param mixed  $meta_value The value to set the meta to.
706
         * @param int    $post_id    The ID of the post to change the meta for.
707
         *
708
         * @return bool Whether the value was changed.
709
         */
UNCOV
710
        public static function set_value( $key, $meta_value, $post_id ) {
×
711
                /*
712
                 * Slash the data, because `update_metadata` will unslash it and we have already unslashed it.
713
                 * Related issue: https://github.com/Yoast/YoastSEO.js/issues/2158
714
                 */
UNCOV
715
                $meta_value = wp_slash( $meta_value );
×
716

UNCOV
717
                return update_post_meta( $post_id, self::$meta_prefix . $key, $meta_value );
×
718
        }
719

720
        /**
721
         * Deletes a meta value for a post.
722
         *
723
         * @param string $key     The internal key of the meta value to change (without prefix).
724
         * @param int    $post_id The ID of the post to delete the meta for.
725
         *
726
         * @return bool Whether the delete was successful or not.
727
         */
728
        public static function delete( $key, $post_id ) {
×
729
                return delete_post_meta( $post_id, self::$meta_prefix . $key );
×
730
        }
731

732
        /**
733
         * Used for imports, this functions imports the value of $old_metakey into $new_metakey for those post
734
         * where no WPSEO meta data has been set.
735
         * Optionally deletes the $old_metakey values.
736
         *
737
         * @param string $old_metakey The old key of the meta value.
738
         * @param string $new_metakey The new key, usually the WPSEO meta key (including prefix).
739
         * @param bool   $delete_old  Whether to delete the old meta key/value-sets.
740
         *
741
         * @return void
742
         */
743
        public static function replace_meta( $old_metakey, $new_metakey, $delete_old = false ) {
×
744
                global $wpdb;
×
745

746
                /*
747
                 * Get only those rows where no wpseo meta values exist for the same post
748
                 * (with the exception of linkdex as that will be set independently of whether the post has been edited).
749
                 *
750
                 * {@internal Query is pretty well optimized this way.}}
751
                 */
752
                $query  = $wpdb->prepare(
×
753
                        "
754
                                SELECT `a`.*
755
                                FROM {$wpdb->postmeta} AS a
×
756
                                WHERE `a`.`meta_key` = %s
757
                                        AND NOT        EXISTS (
758
                                                SELECT DISTINCT `post_id` , count( `meta_id` ) AS count
759
                                                FROM {$wpdb->postmeta} AS b
×
760
                                                WHERE `a`.`post_id` = `b`.`post_id`
761
                                                        AND `meta_key` LIKE %s
762
                                                        AND `meta_key` <> %s
763
                                                GROUP BY `post_id`
764
                                        )
765
                                ;",
766
                        $old_metakey,
×
767
                        $wpdb->esc_like( self::$meta_prefix . '%' ),
×
768
                        self::$meta_prefix . 'linkdex'
×
769
                );
770
                $oldies = $wpdb->get_results( $query );
×
771

772
                if ( is_array( $oldies ) && $oldies !== [] ) {
×
773
                        foreach ( $oldies as $old ) {
×
774
                                update_post_meta( $old->post_id, $new_metakey, $old->meta_value );
×
775
                        }
776
                }
777

778
                // Delete old keys.
779
                if ( $delete_old === true ) {
×
780
                        delete_post_meta_by_key( $old_metakey );
×
781
                }
782
        }
783

784
        /**
785
         * General clean-up of the saved meta values.
786
         * - Remove potentially lingering old meta keys;
787
         * - Remove all default and invalid values.
788
         *
789
         * @return void
790
         */
791
        public static function clean_up() {
×
792
                global $wpdb;
×
793

794
                /*
795
                 * Clean up '_yoast_wpseo_meta-robots'.
796
                 *
797
                 * Retrieve all '_yoast_wpseo_meta-robots' meta values and convert if no new values found.
798
                 *
799
                 * {@internal Query is pretty well optimized this way.}}
800
                 *
801
                 * @todo [JRF => Yoast] Find out all possible values which the old '_yoast_wpseo_meta-robots' could contain
802
                 * to convert the data correctly.
803
                 */
804
                $query  = $wpdb->prepare(
×
805
                        "
806
                                SELECT `a`.*
807
                                FROM {$wpdb->postmeta} AS a
×
808
                                WHERE `a`.`meta_key` = %s
809
                                        AND NOT        EXISTS (
810
                                                SELECT DISTINCT `post_id` , count( `meta_id` ) AS count
811
                                                FROM {$wpdb->postmeta} AS b
×
812
                                                WHERE `a`.`post_id` = `b`.`post_id`
813
                                                        AND ( `meta_key` = %s
814
                                                        OR `meta_key` = %s )
815
                                                GROUP BY `post_id`
816
                                        )
817
                                ;",
818
                        self::$meta_prefix . 'meta-robots',
×
819
                        self::$meta_prefix . 'meta-robots-noindex',
×
820
                        self::$meta_prefix . 'meta-robots-nofollow'
×
821
                );
822
                $oldies = $wpdb->get_results( $query );
×
823

824
                if ( is_array( $oldies ) && $oldies !== [] ) {
×
825
                        foreach ( $oldies as $old ) {
×
826
                                $old_values = explode( ',', $old->meta_value );
×
827
                                foreach ( $old_values as $value ) {
×
828
                                        if ( $value === 'noindex' ) {
×
829
                                                update_post_meta( $old->post_id, self::$meta_prefix . 'meta-robots-noindex', 1 );
×
830
                                        }
831
                                        elseif ( $value === 'nofollow' ) {
×
832
                                                update_post_meta( $old->post_id, self::$meta_prefix . 'meta-robots-nofollow', 1 );
×
833
                                        }
834
                                }
835
                        }
836
                }
837
                unset( $query, $oldies, $old, $old_values, $value );
×
838

839
                // Delete old keys.
840
                delete_post_meta_by_key( self::$meta_prefix . 'meta-robots' );
×
841

842
                /*
843
                 * Remove all default values and (most) invalid option values.
844
                 * Invalid option values for the multiselect (meta-robots-adv) field will be dealt with seperately.
845
                 *
846
                 * {@internal Some of the defaults have changed in v1.5, but as the defaults will
847
                 *            be removed and new defaults will now automatically be passed when no
848
                 *            data found, this update is automatic (as long as we remove the old
849
                 *            values which we do in the below routine).}}
850
                 *
851
                 * {@internal Unfortunately we can't use the normal delete_meta() with key/value combination
852
                 *            as '' (empty string) values will be ignored and would result in all metas
853
                 *            with that key being deleted, not just the empty fields.
854
                 *            Still, the below implementation is largely based on the delete_meta() function.}}
855
                 */
856
                $query = [];
×
857

858
                foreach ( self::$meta_fields as $subset => $field_group ) {
×
859
                        foreach ( $field_group as $key => $field_def ) {
×
860
                                if ( ! isset( $field_def['default_value'] ) ) {
×
861
                                        continue;
×
862
                                }
863

864
                                if ( isset( $field_def['options'] ) && is_array( $field_def['options'] ) && $field_def['options'] !== [] ) {
×
865
                                        $valid = $field_def['options'];
×
866
                                        // Remove the default value from the valid options.
867
                                        unset( $valid[ $field_def['default_value'] ] );
×
868
                                        $valid = array_keys( $valid );
×
869

870
                                        $query[] = $wpdb->prepare(
×
871
                                                "( meta_key = %s AND meta_value NOT IN ( '" . implode( "','", esc_sql( $valid ) ) . "' ) )",
×
872
                                                self::$meta_prefix . $key
×
873
                                        );
874
                                        unset( $valid );
×
875
                                }
876
                                elseif ( is_string( $field_def['default_value'] ) && $field_def['default_value'] !== '' ) {
×
877
                                        $query[] = $wpdb->prepare(
×
878
                                                '( meta_key = %s AND meta_value = %s )',
×
879
                                                self::$meta_prefix . $key,
×
880
                                                $field_def['default_value']
×
881
                                        );
882
                                }
883
                                else {
884
                                        $query[] = $wpdb->prepare(
×
885
                                                "( meta_key = %s AND meta_value = '' )",
×
886
                                                self::$meta_prefix . $key
×
887
                                        );
888
                                }
889
                        }
890
                }
891
                unset( $subset, $field_group, $key, $field_def );
×
892

893
                $query    = "SELECT meta_id FROM {$wpdb->postmeta} WHERE " . implode( ' OR ', $query ) . ';';
×
894
                $meta_ids = $wpdb->get_col( $query );
×
895

896
                if ( is_array( $meta_ids ) && $meta_ids !== [] ) {
×
897
                        // WP native action.
898
                        do_action( 'delete_post_meta', $meta_ids, null, null, null );
×
899

900
                        $query = "DELETE FROM {$wpdb->postmeta} WHERE meta_id IN( " . implode( ',', $meta_ids ) . ' )';
×
901
                        $count = $wpdb->query( $query );
×
902

903
                        if ( $count ) {
×
904
                                foreach ( $meta_ids as $object_id ) {
×
905
                                        wp_cache_delete( $object_id, 'post_meta' );
×
906
                                }
907

908
                                // WP native action.
909
                                do_action( 'deleted_post_meta', $meta_ids, null, null, null );
×
910
                        }
911
                }
912
                unset( $query, $meta_ids, $count, $object_id );
×
913

914
                /*
915
                 * Deal with the multiselect (meta-robots-adv) field.
916
                 *
917
                 * Removes invalid option combinations, such as 'none,noarchive'.
918
                 *
919
                 * Default values have already been removed, so we should have a small result set and
920
                 * (hopefully) even smaller set of invalid results.
921
                 */
922
                $query  = $wpdb->prepare(
×
923
                        "SELECT meta_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = %s",
×
924
                        self::$meta_prefix . 'meta-robots-adv'
×
925
                );
926
                $oldies = $wpdb->get_results( $query );
×
927

928
                if ( is_array( $oldies ) && $oldies !== [] ) {
×
929
                        foreach ( $oldies as $old ) {
×
930
                                $clean = self::validate_meta_robots_adv( $old->meta_value );
×
931

932
                                if ( $clean !== $old->meta_value ) {
×
933
                                        if ( $clean !== self::$meta_fields['advanced']['meta-robots-adv']['default_value'] ) {
×
934
                                                update_metadata_by_mid( 'post', $old->meta_id, $clean );
×
935
                                        }
936
                                        else {
937
                                                delete_metadata_by_mid( 'post', $old->meta_id );
×
938
                                        }
939
                                }
940
                        }
941
                }
942
                unset( $query, $oldies, $old, $clean );
×
943

944
                do_action( 'wpseo_meta_clean_up' );
×
945
        }
946

947
        /**
948
         * Recursively merge a variable number of arrays, using the left array as base,
949
         * giving priority to the right array.
950
         *
951
         * Difference with native array_merge_recursive():
952
         * array_merge_recursive converts values with duplicate keys to arrays rather than
953
         * overwriting the value in the first array with the duplicate value in the second array.
954
         *
955
         * array_merge_recursive_distinct does not change the data types of the values in the arrays.
956
         * Matching keys' values in the second array overwrite those in the first array, as is the
957
         * case with array_merge.
958
         *
959
         * Freely based on information found on http://www.php.net/manual/en/function.array-merge-recursive.php
960
         *
961
         * {@internal Should be moved to a general utility class.}}
962
         *
963
         * @return array
964
         */
UNCOV
965
        public static function array_merge_recursive_distinct() {
×
966

UNCOV
967
                $arrays = func_get_args();
×
UNCOV
968
                if ( count( $arrays ) < 2 ) {
×
969
                        if ( $arrays === [] ) {
×
970
                                return [];
×
971
                        }
972
                        else {
973
                                return $arrays[0];
×
974
                        }
975
                }
976

UNCOV
977
                $merged = array_shift( $arrays );
×
978

UNCOV
979
                foreach ( $arrays as $array ) {
×
UNCOV
980
                        foreach ( $array as $key => $value ) {
×
UNCOV
981
                                if ( is_array( $value ) && ( isset( $merged[ $key ] ) && is_array( $merged[ $key ] ) ) ) {
×
UNCOV
982
                                        $merged[ $key ] = self::array_merge_recursive_distinct( $merged[ $key ], $value );
×
983
                                }
984
                                else {
UNCOV
985
                                        $merged[ $key ] = $value;
×
986
                                }
987
                        }
UNCOV
988
                        unset( $key, $value );
×
989
                }
990

UNCOV
991
                return $merged;
×
992
        }
993

994
        /**
995
         * Counts the total of all the keywords being used for posts except the given one.
996
         *
997
         * @param string $keyword The keyword to be counted.
998
         * @param int    $post_id The id of the post to which the keyword belongs.
999
         *
1000
         * @return array
1001
         */
1002
        public static function keyword_usage( $keyword, $post_id ) {
×
1003

1004
                if ( empty( $keyword ) ) {
×
1005
                        return [];
×
1006
                }
1007

1008
                /**
1009
                 * The indexable repository.
1010
                 *
1011
                 * @var Indexable_Repository
1012
                 */
1013
                $repository = YoastSEO()->classes->get( Indexable_Repository::class );
×
1014

1015
                $post_ids = $repository->query()
×
1016
                        ->select( 'object_id' )
×
1017
                        ->where( 'primary_focus_keyword', $keyword )
×
1018
                        ->where( 'object_type', 'post' )
×
1019
                        ->where_not_equal( 'object_id', $post_id )
×
1020
                        ->where_not_equal( 'post_status', 'trash' )
×
1021
                        ->limit( 2 )    // Limit to 2 results to save time and resources.
×
1022
                        ->find_array();
×
1023

1024
                // Get object_id from each subarray in $post_ids.
1025
                $post_ids = ( is_array( $post_ids ) ) ? array_column( $post_ids, 'object_id' ) : [];
×
1026

1027
                /*
1028
                 * If Premium is installed, get the additional keywords as well.
1029
                 * We only check for the additional keywords if we've not already found two.
1030
                 * In that case there's no use for an additional query as we already know
1031
                 * that the keyword has been used multiple times before.
1032
                 */
1033
                if ( count( $post_ids ) < 2 ) {
×
1034
                        /**
1035
                         * Allows enhancing the array of posts' that share their focus keywords with the post's focus keywords.
1036
                         *
1037
                         * @param array  $post_ids The array of posts' ids that share their related keywords with the post.
1038
                         * @param string $keyword  The keyword to search for.
1039
                         * @param int    $post_id  The id of the post the keyword is associated to.
1040
                         */
1041
                        $post_ids = apply_filters( 'wpseo_posts_for_focus_keyword', $post_ids, $keyword, $post_id );
×
1042
                }
1043

1044
                return $post_ids;
×
1045
        }
1046

1047
        /**
1048
         * Returns the post types for the given post ids.
1049
         *
1050
         * @param array $post_ids The post ids to get the post types for.
1051
         *
1052
         * @return array The post types.
1053
         */
1054
        public static function post_types_for_ids( $post_ids ) {
×
1055

1056
                /**
1057
                 * The indexable repository.
1058
                 *
1059
                 * @var Indexable_Repository
1060
                 */
1061
                $repository = YoastSEO()->classes->get( Indexable_Repository::class );
×
1062

1063
                // Check if post ids is not empty.
1064
                if ( ! empty( $post_ids ) ) {
×
1065
                        // Get the post subtypes for the posts that share the keyword.
1066
                        $post_types = $repository->query()
×
1067
                                ->select( 'object_sub_type' )
×
1068
                                ->where_in( 'object_id', $post_ids )
×
1069
                                ->find_array();
×
1070

1071
                        // Get object_sub_type from each subarray in $post_ids.
1072
                        $post_types = array_column( $post_types, 'object_sub_type' );
×
1073
                }
1074
                else {
1075
                        $post_types = [];
×
1076
                }
1077

1078
                return $post_types;
×
1079
        }
1080

1081
        /**
1082
         * Filter the schema article types.
1083
         *
1084
         * @return void
1085
         */
1086
        public static function filter_schema_article_types() {
×
1087
                /** This filter is documented in inc/options/class-wpseo-option-titles.php */
1088
                self::$meta_fields['schema']['schema_article_type']['options'] = apply_filters( 'wpseo_schema_article_types', self::$meta_fields['schema']['schema_article_type']['options'] );
×
1089
        }
1090
}
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