• 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

15.79
/inc/options/class-wpseo-options.php
1
<?php
2
/**
3
 * WPSEO plugin file.
4
 *
5
 * @package WPSEO\Internals\Options
6
 */
7

8
/**
9
 * Overall Option Management class.
10
 *
11
 * Instantiates all the options and offers a number of utility methods to work with the options.
12
 */
13
class WPSEO_Options {
14

15
        /**
16
         * The option values.
17
         *
18
         * @var array|null
19
         */
20
        protected static $option_values = null;
21

22
        /**
23
         * Options this class uses.
24
         *
25
         * @var array Array format: (string) option_name  => (string) name of concrete class for the option.
26
         */
27
        public static $options = [
28
                'wpseo'               => 'WPSEO_Option_Wpseo',
29
                'wpseo_titles'        => 'WPSEO_Option_Titles',
30
                'wpseo_social'        => 'WPSEO_Option_Social',
31
                'wpseo_ms'            => 'WPSEO_Option_MS',
32
                'wpseo_taxonomy_meta' => 'WPSEO_Taxonomy_Meta',
33
        ];
34

35
        /**
36
         * Array of instantiated option objects.
37
         *
38
         * @var array
39
         */
40
        protected static $option_instances = [];
41

42
        /**
43
         * Array with the option names.
44
         *
45
         * @var array
46
         */
47
        protected static $option_names = [];
48

49
        /**
50
         * Instance of this class.
51
         *
52
         * @var WPSEO_Options
53
         */
54
        protected static $instance;
55

56
        /**
57
         * Instantiate all the WPSEO option management classes.
58
         */
59
        protected function __construct() {
2✔
60
                $this->register_hooks();
2✔
61

62
                foreach ( static::$options as $option_class ) {
2✔
63
                        static::register_option( call_user_func( [ $option_class, 'get_instance' ] ) );
2✔
64
                }
65
        }
1✔
66

67
        /**
68
         * Register our hooks.
69
         *
70
         * @return void
71
         */
72
        public function register_hooks() {
2✔
73
                add_action( 'registered_taxonomy', [ $this, 'clear_cache' ] );
2✔
74
                add_action( 'unregistered_taxonomy', [ $this, 'clear_cache' ] );
2✔
75
                add_action( 'registered_post_type', [ $this, 'clear_cache' ] );
2✔
76
                add_action( 'unregistered_post_type', [ $this, 'clear_cache' ] );
2✔
77
        }
1✔
78

79
        /**
80
         * Get the singleton instance of this class.
81
         *
82
         * @return object
83
         */
84
        public static function get_instance() {
7✔
85
                if ( ! ( static::$instance instanceof self ) ) {
7✔
86
                        static::$instance = new self();
×
87
                }
88

89
                return static::$instance;
7✔
90
        }
91

92
        /**
93
         * Registers an option to the options list.
94
         *
95
         * @param WPSEO_Option $option_instance Instance of the option.
96
         *
97
         * @return void
98
         */
99
        public static function register_option( WPSEO_Option $option_instance ) {
×
100
                $option_name = $option_instance->get_option_name();
×
101

102
                if ( $option_instance->multisite_only && ! static::is_multisite() ) {
×
103
                        unset( static::$options[ $option_name ], static::$option_names[ $option_name ] );
×
104

105
                        return;
×
106
                }
107

108
                $is_already_registered = array_key_exists( $option_name, static::$options );
×
109
                if ( ! $is_already_registered ) {
×
110
                        static::$options[ $option_name ] = get_class( $option_instance );
×
111
                }
112

113
                if ( $option_instance->include_in_all === true ) {
×
114
                        static::$option_names[ $option_name ] = $option_name;
×
115
                }
116

117
                static::$option_instances[ $option_name ] = $option_instance;
×
118

119
                if ( ! $is_already_registered ) {
×
120
                        static::clear_cache();
×
121
                }
122
        }
123

124
        /**
125
         * Get the group name of an option for use in the settings form.
126
         *
127
         * @param string $option_name The option for which you want to retrieve the option group name.
128
         *
129
         * @return string|bool
130
         */
131
        public static function get_group_name( $option_name ) {
×
132
                if ( isset( static::$option_instances[ $option_name ] ) ) {
×
133
                        return static::$option_instances[ $option_name ]->group_name;
×
134
                }
135

136
                return false;
×
137
        }
138

139
        /**
140
         * Get a specific default value for an option.
141
         *
142
         * @param string $option_name The option for which you want to retrieve a default.
143
         * @param string $key         The key within the option who's default you want.
144
         *
145
         * @return mixed
146
         */
147
        public static function get_default( $option_name, $key ) {
×
148
                if ( isset( static::$option_instances[ $option_name ] ) ) {
×
149
                        $defaults = static::$option_instances[ $option_name ]->get_defaults();
×
150
                        if ( isset( $defaults[ $key ] ) ) {
×
151
                                return $defaults[ $key ];
×
152
                        }
153
                }
154

155
                return null;
×
156
        }
157

158
        /**
159
         * Update a site_option.
160
         *
161
         * @param string $option_name The option name of the option to save.
162
         * @param mixed  $value       The new value for the option.
163
         *
164
         * @return bool
165
         */
166
        public static function update_site_option( $option_name, $value ) {
×
167
                if ( is_multisite() && isset( static::$option_instances[ $option_name ] ) ) {
×
168
                        return static::$option_instances[ $option_name ]->update_site_option( $value );
×
169
                }
170

171
                return false;
×
172
        }
173

174
        /**
175
         * Get the instantiated option instance.
176
         *
177
         * @param string $option_name The option for which you want to retrieve the instance.
178
         *
179
         * @return object|bool
180
         */
181
        public static function get_option_instance( $option_name ) {
×
182
                if ( isset( static::$option_instances[ $option_name ] ) ) {
×
183
                        return static::$option_instances[ $option_name ];
×
184
                }
185

186
                return false;
×
187
        }
188

189
        /**
190
         * Retrieve an array of the options which should be included in get_all() and reset().
191
         *
192
         * @return array Array of option names.
193
         */
194
        public static function get_option_names() {
×
195
                $option_names = array_values( static::$option_names );
×
196
                if ( $option_names === [] ) {
×
197
                        foreach ( static::$option_instances as $option_name => $option_object ) {
×
198
                                if ( $option_object->include_in_all === true ) {
×
199
                                        $option_names[] = $option_name;
×
200
                                }
201
                        }
202
                }
203

204
                /**
205
                 * Filter: wpseo_options - Allow developers to change the option name to include.
206
                 *
207
                 * @param array $option_names The option names to include in get_all and reset().
208
                 */
209
                return apply_filters( 'wpseo_options', $option_names );
×
210
        }
211

212
        /**
213
         * Retrieve all the options for the SEO plugin in one go.
214
         *
215
         * @return array Array combining the values of all the options.
216
         */
UNCOV
217
        public static function get_all() {
×
UNCOV
218
                static::$option_values = static::get_options( static::get_option_names() );
×
219

UNCOV
220
                return static::$option_values;
×
221
        }
222

223
        /**
224
         * Retrieve one or more options for the SEO plugin.
225
         *
226
         * @param array $option_names An array of option names of the options you want to get.
227
         *
228
         * @return array Array combining the values of the requested options.
229
         */
UNCOV
230
        public static function get_options( array $option_names ) {
×
UNCOV
231
                $options      = [];
×
UNCOV
232
                $option_names = array_filter( $option_names, 'is_string' );
×
UNCOV
233
                foreach ( $option_names as $option_name ) {
×
UNCOV
234
                        if ( isset( static::$option_instances[ $option_name ] ) ) {
×
UNCOV
235
                                $option = static::get_option( $option_name );
×
236

UNCOV
237
                                if ( $option !== null ) {
×
UNCOV
238
                                        $options = array_merge( $options, $option );
×
239
                                }
240
                        }
241
                }
242

UNCOV
243
                return $options;
×
244
        }
245

246
        /**
247
         * Retrieve a single option for the SEO plugin.
248
         *
249
         * @param string $option_name The name of the option you want to get.
250
         *
251
         * @return array Array containing the requested option.
252
         */
UNCOV
253
        public static function get_option( $option_name ) {
×
UNCOV
254
                $option = null;
×
UNCOV
255
                if ( is_string( $option_name ) && ! empty( $option_name ) ) {
×
UNCOV
256
                        if ( isset( static::$option_instances[ $option_name ] ) ) {
×
UNCOV
257
                                if ( static::$option_instances[ $option_name ]->multisite_only !== true ) {
×
UNCOV
258
                                        $option = get_option( $option_name );
×
259
                                }
260
                                else {
261
                                        $option = get_site_option( $option_name );
×
262
                                }
263
                        }
264
                }
265

UNCOV
266
                return $option;
×
267
        }
268

269
        /**
270
         * Retrieve a single field from any option for the SEO plugin. Keys are always unique.
271
         *
272
         * @param string $key           The key it should return.
273
         * @param mixed  $default_value The default value that should be returned if the key isn't set.
274
         *
275
         * @return mixed Returns value if found, $default_value if not.
276
         */
277
        public static function get( $key, $default_value = null ) {
2✔
278
                if ( static::$option_values === null ) {
2✔
279
                        static::prime_cache();
2✔
280
                }
281
                if ( isset( static::$option_values[ $key ] ) ) {
2✔
UNCOV
282
                        return static::$option_values[ $key ];
×
283
                }
284

285
                return $default_value;
2✔
286
        }
287

288
        /**
289
         * Resets the cache to null.
290
         *
291
         * @return void
292
         */
293
        public static function clear_cache() {
2✔
294
                static::$option_values = null;
2✔
295
        }
1✔
296

297
        /**
298
         * Primes our cache.
299
         *
300
         * @return void
301
         */
302
        private static function prime_cache() {
×
303
                static::$option_values = static::get_all();
×
304
                static::$option_values = static::add_ms_option( static::$option_values );
×
305
        }
306

307
        /**
308
         * Retrieve a single field from an option for the SEO plugin.
309
         *
310
         * @param string $key   The key to set.
311
         * @param mixed  $value The value to set.
312
         *
313
         * @return mixed|null Returns value if found, $default if not.
314
         */
315
        public static function set( $key, $value ) {
2✔
316
                $lookup_table = static::get_lookup_table();
2✔
317

318
                if ( isset( $lookup_table[ $key ] ) ) {
2✔
319
                        return static::save_option( $lookup_table[ $key ], $key, $value );
×
320
                }
321

322
                $patterns = static::get_pattern_table();
2✔
323
                foreach ( $patterns as $pattern => $option ) {
2✔
324
                        if ( strpos( $key, $pattern ) === 0 ) {
2✔
325
                                return static::save_option( $option, $key, $value );
×
326
                        }
327
                }
328

329
                static::$option_values[ $key ] = $value;
2✔
330
        }
1✔
331

332
        /**
333
         * Get an option only if it's been auto-loaded.
334
         *
335
         * @param string $option        The option to retrieve.
336
         * @param mixed  $default_value A default value to return.
337
         *
338
         * @return mixed
339
         */
340
        public static function get_autoloaded_option( $option, $default_value = false ) {
×
341
                $value = wp_cache_get( $option, 'options' );
×
342
                if ( $value === false ) {
×
343
                        $passed_default = func_num_args() > 1;
×
344

345
                        // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Using WP native filter.
346
                        return apply_filters( "default_option_{$option}", $default_value, $option, $passed_default );
×
347
                }
348

349
                // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Using WP native filter.
350
                return apply_filters( "option_{$option}", maybe_unserialize( $value ), $option );
×
351
        }
352

353
        /**
354
         * Run the clean up routine for one or all options.
355
         *
356
         * @param array|string|null $option_name     Optional. the option you want to clean or an array of
357
         *                                           option names for the options you want to clean.
358
         *                                           If not set, all options will be cleaned.
359
         * @param string|null       $current_version Optional. Version from which to upgrade, if not set,
360
         *                                           version specific upgrades will be disregarded.
361
         *
362
         * @return void
363
         */
364
        public static function clean_up( $option_name = null, $current_version = null ) {
×
365
                if ( isset( $option_name ) && is_string( $option_name ) && $option_name !== '' ) {
×
366
                        if ( isset( static::$option_instances[ $option_name ] ) ) {
×
367
                                static::$option_instances[ $option_name ]->clean( $current_version );
×
368
                        }
369
                }
370
                elseif ( isset( $option_name ) && is_array( $option_name ) && $option_name !== [] ) {
×
371
                        foreach ( $option_name as $option ) {
×
372
                                if ( isset( static::$option_instances[ $option ] ) ) {
×
373
                                        static::$option_instances[ $option ]->clean( $current_version );
×
374
                                }
375
                        }
376
                        unset( $option );
×
377
                }
378
                else {
379
                        foreach ( static::$option_instances as $instance ) {
×
380
                                $instance->clean( $current_version );
×
381
                        }
382
                        unset( $instance );
×
383

384
                        // If we've done a full clean-up, we can safely remove this really old option.
385
                        delete_option( 'wpseo_indexation' );
×
386
                }
387
        }
388

389
        /**
390
         * Check that all options exist in the database and add any which don't.
391
         *
392
         * @return void
393
         */
394
        public static function ensure_options_exist() {
×
395
                foreach ( static::$option_instances as $instance ) {
×
396
                        $instance->maybe_add_option();
×
397
                }
398
        }
399

400
        /**
401
         * Initialize some options on first install/activate/reset.
402
         *
403
         * @return void
404
         */
405
        public static function initialize() {
×
406
                /* Force WooThemes to use Yoast SEO data. */
407
                if ( function_exists( 'woo_version_init' ) ) {
×
408
                        update_option( 'seo_woo_use_third_party_data', 'true' );
×
409
                }
410
        }
411

412
        /**
413
         * Reset all options to their default values and rerun some tests.
414
         *
415
         * @return void
416
         */
417
        public static function reset() {
×
418
                if ( ! is_multisite() ) {
×
419
                        $option_names = static::get_option_names();
×
420
                        if ( is_array( $option_names ) && $option_names !== [] ) {
×
421
                                foreach ( $option_names as $option_name ) {
×
422
                                        delete_option( $option_name );
×
423
                                        update_option( $option_name, get_option( $option_name ) );
×
424
                                }
425
                        }
426
                        unset( $option_names );
×
427
                }
428
                else {
429
                        // Reset MS blog based on network default blog setting.
430
                        static::reset_ms_blog( get_current_blog_id() );
×
431
                }
432

433
                static::initialize();
×
434
        }
435

436
        /**
437
         * Initialize default values for a new multisite blog.
438
         *
439
         * @param bool $force_init Whether to always do the initialization routine (title/desc test).
440
         *
441
         * @return void
442
         */
443
        public static function maybe_set_multisite_defaults( $force_init = false ) {
×
444
                $option = get_option( 'wpseo' );
×
445

446
                if ( is_multisite() ) {
×
447
                        if ( $option['ms_defaults_set'] === false ) {
×
448
                                static::reset_ms_blog( get_current_blog_id() );
×
449
                                static::initialize();
×
450
                        }
451
                        elseif ( $force_init === true ) {
×
452
                                static::initialize();
×
453
                        }
454
                }
455
        }
456

457
        /**
458
         * Reset all options for a specific multisite blog to their default values based upon a
459
         * specified default blog if one was chosen on the network page or the plugin defaults if it was not.
460
         *
461
         * @param int|string $blog_id Blog id of the blog for which to reset the options.
462
         *
463
         * @return void
464
         */
465
        public static function reset_ms_blog( $blog_id ) {
×
466
                if ( is_multisite() ) {
×
467
                        $options      = get_site_option( 'wpseo_ms' );
×
468
                        $option_names = static::get_option_names();
×
469

470
                        if ( is_array( $option_names ) && $option_names !== [] ) {
×
471
                                $base_blog_id = $blog_id;
×
472
                                if ( $options['defaultblog'] !== '' && $options['defaultblog'] !== 0 ) {
×
473
                                        $base_blog_id = $options['defaultblog'];
×
474
                                }
475

476
                                foreach ( $option_names as $option_name ) {
×
477
                                        delete_blog_option( $blog_id, $option_name );
×
478

479
                                        $new_option = get_blog_option( $base_blog_id, $option_name );
×
480

481
                                        /* Remove sensitive, theme dependent and site dependent info. */
482
                                        if ( isset( static::$option_instances[ $option_name ] ) && static::$option_instances[ $option_name ]->ms_exclude !== [] ) {
×
483
                                                foreach ( static::$option_instances[ $option_name ]->ms_exclude as $key ) {
×
484
                                                        unset( $new_option[ $key ] );
×
485
                                                }
486
                                        }
487

488
                                        if ( $option_name === 'wpseo' ) {
×
489
                                                $new_option['ms_defaults_set'] = true;
×
490
                                        }
491

492
                                        update_blog_option( $blog_id, $option_name, $new_option );
×
493
                                }
494
                        }
495
                }
496
        }
497

498
        /**
499
         * Saves the option to the database.
500
         *
501
         * @param string $wpseo_options_group_name The name for the wpseo option group in the database.
502
         * @param string $option_name              The name for the option to set.
503
         * @param mixed  $option_value             The value for the option.
504
         *
505
         * @return bool Returns true if the option is successfully saved in the database.
506
         */
507
        public static function save_option( $wpseo_options_group_name, $option_name, $option_value ) {
×
508
                $options                 = static::get_option( $wpseo_options_group_name );
×
509
                $options[ $option_name ] = $option_value;
×
510

511
                if ( isset( static::$option_instances[ $wpseo_options_group_name ] ) && static::$option_instances[ $wpseo_options_group_name ]->multisite_only === true ) {
×
512
                        static::update_site_option( $wpseo_options_group_name, $options );
×
513
                }
514
                else {
515
                        update_option( $wpseo_options_group_name, $options );
×
516
                }
517

518
                // Check if everything got saved properly.
519
                $saved_option = static::get_option( $wpseo_options_group_name );
×
520

521
                // Clear our cache.
522
                static::clear_cache();
×
523

524
                return $saved_option[ $option_name ] === $options[ $option_name ];
×
525
        }
526

527
        /**
528
         * Adds the multisite options to the option stack if relevant.
529
         *
530
         * @param array $option The currently present options settings.
531
         *
532
         * @return array Options possibly including multisite.
533
         */
534
        protected static function add_ms_option( $option ) {
×
535
                if ( ! is_multisite() ) {
×
536
                        return $option;
×
537
                }
538

539
                $ms_option = static::get_option( 'wpseo_ms' );
×
540
                if ( $ms_option === null ) {
×
541
                        return $option;
×
542
                }
543

544
                return array_merge( $option, $ms_option );
×
545
        }
546

547
        /**
548
         * Checks if installation is multisite.
549
         *
550
         * @return bool True when is multisite.
551
         */
552
        protected static function is_multisite() {
×
553
                static $is_multisite;
×
554

555
                if ( $is_multisite === null ) {
×
556
                        $is_multisite = is_multisite();
×
557
                }
558

559
                return $is_multisite;
×
560
        }
561

562
        /**
563
         * Retrieves a lookup table to find in which option_group a key is stored.
564
         *
565
         * @return array The lookup table.
566
         */
567
        private static function get_lookup_table() {
×
568
                $lookup_table = [];
×
569

570
                foreach ( array_keys( static::$options ) as $option_name ) {
×
571
                        $full_option = static::get_option( $option_name );
×
572
                        foreach ( $full_option as $key => $value ) {
×
573
                                $lookup_table[ $key ] = $option_name;
×
574
                        }
575
                }
576

577
                return $lookup_table;
×
578
        }
579

580
        /**
581
         * Retrieves a lookup table to find in which option_group a key is stored.
582
         *
583
         * @return array The lookup table.
584
         */
585
        private static function get_pattern_table() {
×
586
                $pattern_table = [];
×
587
                foreach ( static::$options as $option_name => $option_class ) {
×
588
                        $instance = call_user_func( [ $option_class, 'get_instance' ] );
×
589
                        foreach ( $instance->get_patterns() as $key ) {
×
590
                                $pattern_table[ $key ] = $option_name;
×
591
                        }
592
                }
593

594
                return $pattern_table;
×
595
        }
596
}
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