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

Yoast / wordpress-seo / 82d59e4a8c3dc05ddcdda20845822ec23f6c7797

24 Sep 2024 07:36AM UTC coverage: 49.375% (-5.2%) from 54.538%
82d59e4a8c3dc05ddcdda20845822ec23f6c7797

push

github

YoastBot
Bump version to 23.5

7511 of 13516 branches covered (55.57%)

Branch coverage included in aggregate %.

25687 of 53720 relevant lines covered (47.82%)

42516.31 hits per line

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

28.26
/src/integrations/settings-integration.php
1
<?php
2

3
namespace Yoast\WP\SEO\Integrations;
4

5
use WP_Post;
6
use WP_Post_Type;
7
use WP_Taxonomy;
8
use WP_User;
9
use WPSEO_Addon_Manager;
10
use WPSEO_Admin_Asset_Manager;
11
use WPSEO_Admin_Editor_Specific_Replace_Vars;
12
use WPSEO_Admin_Recommended_Replace_Vars;
13
use WPSEO_Option_Titles;
14
use WPSEO_Options;
15
use WPSEO_Replace_Vars;
16
use WPSEO_Shortlinker;
17
use WPSEO_Sitemaps_Router;
18
use Yoast\WP\SEO\Conditionals\Settings_Conditional;
19
use Yoast\WP\SEO\Config\Schema_Types;
20
use Yoast\WP\SEO\Content_Type_Visibility\Application\Content_Type_Visibility_Dismiss_Notifications;
21
use Yoast\WP\SEO\Helpers\Current_Page_Helper;
22
use Yoast\WP\SEO\Helpers\Language_Helper;
23
use Yoast\WP\SEO\Helpers\Options_Helper;
24
use Yoast\WP\SEO\Helpers\Post_Type_Helper;
25
use Yoast\WP\SEO\Helpers\Product_Helper;
26
use Yoast\WP\SEO\Helpers\Schema\Article_Helper;
27
use Yoast\WP\SEO\Helpers\Taxonomy_Helper;
28
use Yoast\WP\SEO\Helpers\User_Helper;
29
use Yoast\WP\SEO\Helpers\Woocommerce_Helper;
30
use Yoast\WP\SEO\Promotions\Application\Promotion_Manager;
31

32
/**
33
 * Class Settings_Integration.
34
 */
35
class Settings_Integration implements Integration_Interface {
36

37
        public const PAGE = 'wpseo_page_settings';
38

39
        /**
40
         * Holds the included WordPress options.
41
         *
42
         * @var string[]
43
         */
44
        public const WP_OPTIONS = [ 'blogdescription' ];
45

46
        /**
47
         * Holds the allowed option groups.
48
         *
49
         * @var array
50
         */
51
        public const ALLOWED_OPTION_GROUPS = [ 'wpseo', 'wpseo_titles', 'wpseo_social' ];
52

53
        /**
54
         * Holds the disallowed settings, per option group.
55
         *
56
         * @var array
57
         */
58
        public const DISALLOWED_SETTINGS = [
59
                'wpseo'        => [
60
                        'myyoast-oauth',
61
                        'semrush_tokens',
62
                        'custom_taxonomy_slugs',
63
                        'import_cursors',
64
                        'workouts_data',
65
                        'configuration_finished_steps',
66
                        'importing_completed',
67
                        'wincher_tokens',
68
                        'least_readability_ignore_list',
69
                        'least_seo_score_ignore_list',
70
                        'most_linked_ignore_list',
71
                        'least_linked_ignore_list',
72
                        'indexables_page_reading_list',
73
                        'show_new_content_type_notification',
74
                        'new_post_types',
75
                        'new_taxonomies',
76
                ],
77
                'wpseo_titles' => [
78
                        'company_logo_meta',
79
                        'person_logo_meta',
80
                ],
81
        ];
82

83
        /**
84
         * Holds the disabled on multisite settings, per option group.
85
         *
86
         * @var array
87
         */
88
        public const DISABLED_ON_MULTISITE_SETTINGS = [
89
                'wpseo' => [
90
                        'deny_search_crawling',
91
                        'deny_wp_json_crawling',
92
                        'deny_adsbot_crawling',
93
                        'deny_ccbot_crawling',
94
                        'deny_google_extended_crawling',
95
                        'deny_gptbot_crawling',
96
                ],
97
        ];
98

99
        /**
100
         * Holds the WPSEO_Admin_Asset_Manager.
101
         *
102
         * @var WPSEO_Admin_Asset_Manager
103
         */
104
        protected $asset_manager;
105

106
        /**
107
         * Holds the WPSEO_Replace_Vars.
108
         *
109
         * @var WPSEO_Replace_Vars
110
         */
111
        protected $replace_vars;
112

113
        /**
114
         * Holds the Schema_Types.
115
         *
116
         * @var Schema_Types
117
         */
118
        protected $schema_types;
119

120
        /**
121
         * Holds the Current_Page_Helper.
122
         *
123
         * @var Current_Page_Helper
124
         */
125
        protected $current_page_helper;
126

127
        /**
128
         * Holds the Post_Type_Helper.
129
         *
130
         * @var Post_Type_Helper
131
         */
132
        protected $post_type_helper;
133

134
        /**
135
         * Holds the Language_Helper.
136
         *
137
         * @var Language_Helper
138
         */
139
        protected $language_helper;
140

141
        /**
142
         * Holds the Taxonomy_Helper.
143
         *
144
         * @var Taxonomy_Helper
145
         */
146
        protected $taxonomy_helper;
147

148
        /**
149
         * Holds the Product_Helper.
150
         *
151
         * @var Product_Helper
152
         */
153
        protected $product_helper;
154

155
        /**
156
         * Holds the Woocommerce_Helper.
157
         *
158
         * @var Woocommerce_Helper
159
         */
160
        protected $woocommerce_helper;
161

162
        /**
163
         * Holds the Article_Helper.
164
         *
165
         * @var Article_Helper
166
         */
167
        protected $article_helper;
168

169
        /**
170
         * Holds the User_Helper.
171
         *
172
         * @var User_Helper
173
         */
174
        protected $user_helper;
175

176
        /**
177
         * Holds the Options_Helper instance.
178
         *
179
         * @var Options_Helper
180
         */
181
        protected $options;
182

183
        /**
184
         * Holds the Content_Type_Visibility_Dismiss_Notifications instance.
185
         *
186
         * @var Content_Type_Visibility_Dismiss_Notifications
187
         */
188
        protected $content_type_visibility;
189

190
        /**
191
         * Constructs Settings_Integration.
192
         *
193
         * @param WPSEO_Admin_Asset_Manager                     $asset_manager           The WPSEO_Admin_Asset_Manager.
194
         * @param WPSEO_Replace_Vars                            $replace_vars            The WPSEO_Replace_Vars.
195
         * @param Schema_Types                                  $schema_types            The Schema_Types.
196
         * @param Current_Page_Helper                           $current_page_helper     The Current_Page_Helper.
197
         * @param Post_Type_Helper                              $post_type_helper        The Post_Type_Helper.
198
         * @param Language_Helper                               $language_helper         The Language_Helper.
199
         * @param Taxonomy_Helper                               $taxonomy_helper         The Taxonomy_Helper.
200
         * @param Product_Helper                                $product_helper          The Product_Helper.
201
         * @param Woocommerce_Helper                            $woocommerce_helper      The Woocommerce_Helper.
202
         * @param Article_Helper                                $article_helper          The Article_Helper.
203
         * @param User_Helper                                   $user_helper             The User_Helper.
204
         * @param Options_Helper                                $options                 The options helper.
205
         * @param Content_Type_Visibility_Dismiss_Notifications $content_type_visibility The Content_Type_Visibility_Dismiss_Notifications instance.
206
         */
207
        public function __construct(
2✔
208
                WPSEO_Admin_Asset_Manager $asset_manager,
209
                WPSEO_Replace_Vars $replace_vars,
210
                Schema_Types $schema_types,
211
                Current_Page_Helper $current_page_helper,
212
                Post_Type_Helper $post_type_helper,
213
                Language_Helper $language_helper,
214
                Taxonomy_Helper $taxonomy_helper,
215
                Product_Helper $product_helper,
216
                Woocommerce_Helper $woocommerce_helper,
217
                Article_Helper $article_helper,
218
                User_Helper $user_helper,
219
                Options_Helper $options,
220
                Content_Type_Visibility_Dismiss_Notifications $content_type_visibility
221
        ) {
1✔
222
                $this->asset_manager           = $asset_manager;
2✔
223
                $this->replace_vars            = $replace_vars;
2✔
224
                $this->schema_types            = $schema_types;
2✔
225
                $this->current_page_helper     = $current_page_helper;
2✔
226
                $this->taxonomy_helper         = $taxonomy_helper;
2✔
227
                $this->post_type_helper        = $post_type_helper;
2✔
228
                $this->language_helper         = $language_helper;
2✔
229
                $this->product_helper          = $product_helper;
2✔
230
                $this->woocommerce_helper      = $woocommerce_helper;
2✔
231
                $this->article_helper          = $article_helper;
2✔
232
                $this->user_helper             = $user_helper;
2✔
233
                $this->options                 = $options;
2✔
234
                $this->content_type_visibility = $content_type_visibility;
2✔
235
        }
1✔
236

237
        /**
238
         * Returns the conditionals based on which this loadable should be active.
239
         *
240
         * @return array
241
         */
242
        public static function get_conditionals() {
2✔
243
                return [ Settings_Conditional::class ];
2✔
244
        }
245

246
        /**
247
         * Initializes the integration.
248
         *
249
         * This is the place to register hooks and filters.
250
         *
251
         * @return void
252
         */
253
        public function register_hooks() {
×
254
                // Add page.
255
                \add_filter( 'wpseo_submenu_pages', [ $this, 'add_page' ] );
×
256
                \add_filter( 'admin_menu', [ $this, 'add_settings_saved_page' ] );
×
257

258
                // Are we saving the settings?
259
                if ( $this->current_page_helper->get_current_admin_page() === 'options.php' ) {
×
260
                        $post_action = '';
×
261
                        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Reason: We are not processing form information.
262
                        if ( isset( $_POST['action'] ) && \is_string( $_POST['action'] ) ) {
×
263
                                // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are not processing form information.
264
                                $post_action = \wp_unslash( $_POST['action'] );
×
265
                        }
266
                        $option_page = '';
×
267
                        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Reason: We are not processing form information.
268
                        if ( isset( $_POST['option_page'] ) && \is_string( $_POST['option_page'] ) ) {
×
269
                                // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are not processing form information.
270
                                $option_page = \wp_unslash( $_POST['option_page'] );
×
271
                        }
272

273
                        if ( $post_action === 'update' && $option_page === self::PAGE ) {
×
274
                                \add_action( 'admin_init', [ $this, 'register_setting' ] );
×
275
                                \add_action( 'in_admin_header', [ $this, 'remove_notices' ], \PHP_INT_MAX );
×
276
                        }
277

278
                        return;
×
279
                }
280

281
                // Are we on the settings page?
282
                if ( $this->current_page_helper->get_current_yoast_seo_page() === self::PAGE ) {
×
283
                        \add_action( 'admin_init', [ $this, 'register_setting' ] );
×
284
                        \add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
×
285
                        \add_action( 'in_admin_header', [ $this, 'remove_notices' ], \PHP_INT_MAX );
×
286
                }
287
        }
288

289
        /**
290
         * Registers the different options under the setting.
291
         *
292
         * @return void
293
         */
294
        public function register_setting() {
×
295
                foreach ( WPSEO_Options::$options as $name => $instance ) {
×
296
                        if ( \in_array( $name, self::ALLOWED_OPTION_GROUPS, true ) ) {
×
297
                                \register_setting( self::PAGE, $name );
×
298
                        }
299
                }
300
                // Only register WP options when the user is allowed to manage them.
301
                if ( \current_user_can( 'manage_options' ) ) {
×
302
                        foreach ( self::WP_OPTIONS as $name ) {
×
303
                                \register_setting( self::PAGE, $name );
×
304
                        }
305
                }
306
        }
307

308
        /**
309
         * Adds the page.
310
         *
311
         * @param array $pages The pages.
312
         *
313
         * @return array The pages.
314
         */
315
        public function add_page( $pages ) {
×
316
                \array_splice(
×
317
                        $pages,
×
318
                        1,
×
319
                        0,
×
320
                        [
321
                                [
322
                                        'wpseo_dashboard',
×
323
                                        '',
×
324
                                        \__( 'Settings', 'wordpress-seo' ),
×
325
                                        'wpseo_manage_options',
×
326
                                        self::PAGE,
×
327
                                        [ $this, 'display_page' ],
×
328
                                ],
329
                        ]
330
                );
331

332
                return $pages;
×
333
        }
334

335
        /**
336
         * Adds a dummy page.
337
         *
338
         * Because the options route NEEDS to redirect to something.
339
         *
340
         * @param array $pages The pages.
341
         *
342
         * @return array The pages.
343
         */
344
        public function add_settings_saved_page( $pages ) {
2✔
345
                \add_submenu_page(
2✔
346
                        '',
2✔
347
                        '',
2✔
348
                        '',
2✔
349
                        'wpseo_manage_options',
2✔
350
                        self::PAGE . '_saved',
2✔
351
                        static function () {
1✔
352
                                // Add success indication to HTML response.
353
                                $success = empty( \get_settings_errors() ) ? 'true' : 'false';
×
354
                                echo \esc_html( "{{ yoast-success: $success }}" );
×
355
                        }
2✔
356
                );
1✔
357

358
                return $pages;
2✔
359
        }
360

361
        /**
362
         * Displays the page.
363
         *
364
         * @return void
365
         */
366
        public function display_page() {
×
367
                echo '<div id="yoast-seo-settings"></div>';
×
368
        }
369

370
        /**
371
         * Enqueues the assets.
372
         *
373
         * @return void
374
         */
375
        public function enqueue_assets() {
×
376
                // Remove the emoji script as it is incompatible with both React and any contenteditable fields.
377
                \remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
×
378
                \wp_enqueue_media();
×
379
                $this->asset_manager->enqueue_script( 'new-settings' );
×
380
                $this->asset_manager->enqueue_style( 'new-settings' );
×
381
                if ( \YoastSEO()->classes->get( Promotion_Manager::class )->is( 'black-friday-2023-promotion' ) ) {
×
382
                        $this->asset_manager->enqueue_style( 'black-friday-banner' );
×
383
                }
384
                $this->asset_manager->localize_script( 'new-settings', 'wpseoScriptData', $this->get_script_data() );
×
385
        }
386

387
        /**
388
         * Removes all current WP notices.
389
         *
390
         * @return void
391
         */
392
        public function remove_notices() {
×
393
                \remove_all_actions( 'admin_notices' );
×
394
                \remove_all_actions( 'user_admin_notices' );
×
395
                \remove_all_actions( 'network_admin_notices' );
×
396
                \remove_all_actions( 'all_admin_notices' );
×
397
        }
398

399
        /**
400
         * Creates the script data.
401
         *
402
         * @return array The script data.
403
         */
404
        protected function get_script_data() {
×
405
                $default_setting_values = $this->get_default_setting_values();
×
406
                $settings               = $this->get_settings( $default_setting_values );
×
407
                $post_types             = $this->post_type_helper->get_indexable_post_type_objects();
×
408
                $taxonomies             = $this->taxonomy_helper->get_indexable_taxonomy_objects();
×
409

410
                // Check if attachments are included in indexation.
411
                if ( ! \array_key_exists( 'attachment', $post_types ) ) {
×
412
                        // Always include attachments in the settings, to let the user enable them again.
413
                        $attachment_object = \get_post_type_object( 'attachment' );
×
414
                        if ( ! empty( $attachment_object ) ) {
×
415
                                $post_types['attachment'] = $attachment_object;
×
416
                        }
417
                }
418
                // Check if post formats are included in indexation.
419
                if ( ! \array_key_exists( 'post_format', $taxonomies ) ) {
×
420
                        // Always include post_format in the settings, to let the user enable them again.
421
                        $post_format_object = \get_taxonomy( 'post_format' );
×
422
                        if ( ! empty( $post_format_object ) ) {
×
423
                                $taxonomies['post_format'] = $post_format_object;
×
424
                        }
425
                }
426

427
                $transformed_post_types = $this->transform_post_types( $post_types );
×
428
                $transformed_taxonomies = $this->transform_taxonomies( $taxonomies, \array_keys( $transformed_post_types ) );
×
429

430
                // Check if there is a new content type to show notification only once in the settings.
431
                $show_new_content_type_notification = $this->content_type_visibility->maybe_add_settings_notification();
×
432

433
                return [
434
                        'settings'                       => $this->transform_settings( $settings ),
×
435
                        'defaultSettingValues'           => $default_setting_values,
×
436
                        'disabledSettings'               => $this->get_disabled_settings( $settings ),
×
437
                        'endpoint'                       => \admin_url( 'options.php' ),
×
438
                        'nonce'                          => \wp_create_nonce( self::PAGE . '-options' ),
×
439
                        'separators'                     => WPSEO_Option_Titles::get_instance()->get_separator_options_for_display(),
×
440
                        'replacementVariables'           => $this->get_replacement_variables(),
×
441
                        'schema'                         => $this->get_schema( $transformed_post_types ),
×
442
                        'preferences'                    => $this->get_preferences( $settings ),
×
443
                        'linkParams'                     => WPSEO_Shortlinker::get_query_params(),
×
444
                        'postTypes'                      => $transformed_post_types,
×
445
                        'taxonomies'                     => $transformed_taxonomies,
×
446
                        'fallbacks'                      => $this->get_fallbacks(),
×
447
                        'showNewContentTypeNotification' => $show_new_content_type_notification,
×
448
                ];
449
        }
450

451
        /**
452
         * Retrieves the preferences.
453
         *
454
         * @param array $settings The settings.
455
         *
456
         * @return array The preferences.
457
         */
458
        protected function get_preferences( $settings ) {
×
459
                $shop_page_id             = $this->woocommerce_helper->get_shop_page_id();
×
460
                $homepage_is_latest_posts = \get_option( 'show_on_front' ) === 'posts';
×
461
                $page_on_front            = \get_option( 'page_on_front' );
×
462
                $page_for_posts           = \get_option( 'page_for_posts' );
×
463

464
                $addon_manager          = new WPSEO_Addon_Manager();
×
465
                $woocommerce_seo_active = \is_plugin_active( $addon_manager->get_plugin_file( WPSEO_Addon_Manager::WOOCOMMERCE_SLUG ) );
×
466

467
                if ( empty( $page_on_front ) ) {
×
468
                        $page_on_front = $page_for_posts;
×
469
                }
470

471
                $business_settings_url = \get_admin_url( null, 'admin.php?page=wpseo_local' );
×
472
                if ( \defined( 'WPSEO_LOCAL_FILE' ) ) {
×
473
                        $local_options      = \get_option( 'wpseo_local' );
×
474
                        $multiple_locations = $local_options['use_multiple_locations'];
×
475
                        $same_organization  = $local_options['multiple_locations_same_organization'];
×
476
                        if ( $multiple_locations === 'on' && $same_organization !== 'on' ) {
×
477
                                $business_settings_url = \get_admin_url( null, 'edit.php?post_type=wpseo_locations' );
×
478
                        }
479
                }
480

481
                return [
482
                        'isPremium'                     => $this->product_helper->is_premium(),
×
483
                        'isRtl'                         => \is_rtl(),
×
484
                        'isNetworkAdmin'                => \is_network_admin(),
×
485
                        'isMainSite'                    => \is_main_site(),
×
486
                        'isMultisite'                   => \is_multisite(),
×
487
                        'isWooCommerceActive'           => $this->woocommerce_helper->is_active(),
×
488
                        'isLocalSeoActive'              => \defined( 'WPSEO_LOCAL_FILE' ),
×
489
                        'isNewsSeoActive'               => \defined( 'WPSEO_NEWS_FILE' ),
×
490
                        'isWooCommerceSEOActive'        => $woocommerce_seo_active,
×
491
                        'promotions'                    => \YoastSEO()->classes->get( Promotion_Manager::class )->get_current_promotions(),
×
492
                        'siteUrl'                       => \get_bloginfo( 'url' ),
×
493
                        'siteTitle'                     => \get_bloginfo( 'name' ),
×
494
                        'sitemapUrl'                    => WPSEO_Sitemaps_Router::get_base_url( 'sitemap_index.xml' ),
×
495
                        'hasWooCommerceShopPage'        => $shop_page_id !== -1,
496
                        'editWooCommerceShopPageUrl'    => \get_edit_post_link( $shop_page_id, 'js' ),
×
497
                        'wooCommerceShopPageSettingUrl' => \get_admin_url( null, 'admin.php?page=wc-settings&tab=products' ),
×
498
                        'localSeoPageSettingUrl'        => $business_settings_url,
×
499
                        'homepageIsLatestPosts'         => $homepage_is_latest_posts,
×
500
                        'homepagePageEditUrl'           => \get_edit_post_link( $page_on_front, 'js' ),
×
501
                        'homepagePostsEditUrl'          => \get_edit_post_link( $page_for_posts, 'js' ),
×
502
                        'createUserUrl'                 => \admin_url( 'user-new.php' ),
×
503
                        'createPageUrl'                 => \admin_url( 'post-new.php?post_type=page' ),
×
504
                        'editUserUrl'                   => \admin_url( 'user-edit.php' ),
×
505
                        'editTaxonomyUrl'               => \admin_url( 'edit-tags.php' ),
×
506
                        'generalSettingsUrl'            => \admin_url( 'options-general.php' ),
×
507
                        'companyOrPersonMessage'        => \apply_filters( 'wpseo_knowledge_graph_setting_msg', '' ),
×
508
                        'currentUserId'                 => \get_current_user_id(),
×
509
                        'canCreateUsers'                => \current_user_can( 'create_users' ),
×
510
                        'canCreatePages'                => \current_user_can( 'edit_pages' ),
×
511
                        'canEditUsers'                  => \current_user_can( 'edit_users' ),
×
512
                        'canManageOptions'              => \current_user_can( 'manage_options' ),
×
513
                        'userLocale'                    => \str_replace( '_', '-', \get_user_locale() ),
×
514
                        'pluginUrl'                     => \plugins_url( '', \WPSEO_FILE ),
×
515
                        'showForceRewriteTitlesSetting' => ! \current_theme_supports( 'title-tag' ) && ! ( \function_exists( 'wp_is_block_theme' ) && \wp_is_block_theme() ),
×
516
                        'upsellSettings'                => $this->get_upsell_settings(),
×
517
                        'siteRepresentsPerson'          => $this->get_site_represents_person( $settings ),
×
518
                        'siteBasicsPolicies'            => $this->get_site_basics_policies( $settings ),
×
519
                ];
520
        }
521

522
        /**
523
         * Retrieves the currently represented person.
524
         *
525
         * @param array $settings The settings.
526
         *
527
         * @return array The currently represented person's ID and name.
528
         */
529
        protected function get_site_represents_person( $settings ) {
×
530
                $person = [
531
                        'id'   => false,
×
532
                        'name' => '',
533
                ];
534

535
                if ( isset( $settings['wpseo_titles']['company_or_person_user_id'] ) ) {
×
536
                        $person['id'] = $settings['wpseo_titles']['company_or_person_user_id'];
×
537
                        $user         = \get_userdata( $person['id'] );
×
538
                        if ( $user instanceof WP_User ) {
×
539
                                $person['name'] = $user->get( 'display_name' );
×
540
                        }
541
                }
542

543
                return $person;
×
544
        }
545

546
        /**
547
         * Get site policy data.
548
         *
549
         * @param array $settings The settings.
550
         *
551
         * @return array The policy data.
552
         */
553
        private function get_site_basics_policies( $settings ) {
×
554
                $policies = [];
×
555

556
                $policies = $this->maybe_add_policy( $policies, $settings['wpseo_titles']['publishing_principles_id'], 'publishing_principles_id' );
×
557
                $policies = $this->maybe_add_policy( $policies, $settings['wpseo_titles']['ownership_funding_info_id'], 'ownership_funding_info_id' );
×
558
                $policies = $this->maybe_add_policy( $policies, $settings['wpseo_titles']['actionable_feedback_policy_id'], 'actionable_feedback_policy_id' );
×
559
                $policies = $this->maybe_add_policy( $policies, $settings['wpseo_titles']['corrections_policy_id'], 'corrections_policy_id' );
×
560
                $policies = $this->maybe_add_policy( $policies, $settings['wpseo_titles']['ethics_policy_id'], 'ethics_policy_id' );
×
561
                $policies = $this->maybe_add_policy( $policies, $settings['wpseo_titles']['diversity_policy_id'], 'diversity_policy_id' );
×
562
                $policies = $this->maybe_add_policy( $policies, $settings['wpseo_titles']['diversity_staffing_report_id'], 'diversity_staffing_report_id' );
×
563

564
                return $policies;
×
565
        }
566

567
        /**
568
         * Adds policy data if it is present.
569
         *
570
         * @param array  $policies The existing policy data.
571
         * @param int    $policy   The policy id to check.
572
         * @param string $key      The option key name.
573
         *
574
         * @return array<int,string> The policy data.
575
         */
576
        private function maybe_add_policy( $policies, $policy, $key ) {
×
577
                $policy_array = [
578
                        'id'   => 0,
×
579
                        'name' => \__( 'None', 'wordpress-seo' ),
×
580
                ];
581

582
                if ( isset( $policy ) && \is_int( $policy ) ) {
×
583
                        $policy_array['id'] = $policy;
×
584
                        $post               = \get_post( $policy );
×
585
                        if ( $post instanceof WP_Post ) {
×
586
                                if ( $post->post_status !== 'publish' || $post->post_password !== '' ) {
×
587
                                        return $policies;
×
588
                                }
589
                                $policy_array['name'] = $post->post_title;
×
590
                        }
591
                }
592

593
                $policies[ $key ] = $policy_array;
×
594

595
                return $policies;
×
596
        }
597

598
        /**
599
         * Returns settings for the Call to Buy (CTB) buttons.
600
         *
601
         * @return array<string> The array of CTB settings.
602
         */
603
        public function get_upsell_settings() {
×
604
                return [
605
                        'actionId'     => 'load-nfd-ctb',
×
606
                        'premiumCtbId' => 'f6a84663-465f-4cb5-8ba5-f7a6d72224b2',
607
                ];
608
        }
609

610
        /**
611
         * Retrieves the default setting values.
612
         *
613
         * These default values are currently being used in the UI for dummy fields.
614
         * Dummy fields should not expose or reflect the actual data.
615
         *
616
         * @return array The default setting values.
617
         */
618
        protected function get_default_setting_values() {
×
619
                $defaults = [];
×
620

621
                // Add Yoast settings.
622
                foreach ( WPSEO_Options::$options as $option_name => $instance ) {
×
623
                        if ( \in_array( $option_name, self::ALLOWED_OPTION_GROUPS, true ) ) {
×
624
                                $option_instance          = WPSEO_Options::get_option_instance( $option_name );
×
625
                                $defaults[ $option_name ] = ( $option_instance ) ? $option_instance->get_defaults() : [];
×
626
                        }
627
                }
628
                // Add WP settings.
629
                foreach ( self::WP_OPTIONS as $option_name ) {
×
630
                        $defaults[ $option_name ] = '';
×
631
                }
632

633
                // Remove disallowed settings.
634
                foreach ( self::DISALLOWED_SETTINGS as $option_name => $disallowed_settings ) {
×
635
                        foreach ( $disallowed_settings as $disallowed_setting ) {
×
636
                                unset( $defaults[ $option_name ][ $disallowed_setting ] );
×
637
                        }
638
                }
639

640
                if ( \defined( 'WPSEO_LOCAL_FILE' ) ) {
×
641
                        $defaults = $this->get_defaults_from_local_seo( $defaults );
×
642
                }
643

644
                return $defaults;
×
645
        }
646

647
        /**
648
         * Retrieves the organization schema values from Local SEO for defaults in Site representation fields.
649
         * Specifically for the org-vat-id, org-tax-id, org-email and org-phone options.
650
         *
651
         * @param array<string|int|bool> $defaults The settings defaults.
652
         *
653
         * @return array<string|int|bool> The settings defaults with local seo overides.
654
         */
655
        protected function get_defaults_from_local_seo( $defaults ) {
8✔
656
                $local_options      = \get_option( 'wpseo_local' );
8✔
657
                $multiple_locations = $local_options['use_multiple_locations'];
8✔
658
                $same_organization  = $local_options['multiple_locations_same_organization'];
8✔
659
                $shared_info        = $local_options['multiple_locations_shared_business_info'];
8✔
660
                if ( $multiple_locations !== 'on' || ( $multiple_locations === 'on' && $same_organization === 'on' && $shared_info === 'on' ) ) {
8✔
661
                        $defaults['wpseo_titles']['org-vat-id'] = $local_options['location_vat_id'];
6✔
662
                        $defaults['wpseo_titles']['org-tax-id'] = $local_options['location_tax_id'];
6✔
663
                        $defaults['wpseo_titles']['org-email']  = $local_options['location_email'];
6✔
664
                        $defaults['wpseo_titles']['org-phone']  = $local_options['location_phone'];
6✔
665
                }
666

667
                if ( \wpseo_has_primary_location() ) {
8✔
668
                        $primary_location = $local_options['multiple_locations_primary_location'];
4✔
669

670
                        $location_keys = [
2✔
671
                                'org-phone'  => [
4✔
672
                                        'is_overridden' => '_wpseo_is_overridden_business_phone',
2✔
673
                                        'value'         => '_wpseo_business_phone',
2✔
674
                                ],
2✔
675
                                'org-email'  => [
2✔
676
                                        'is_overridden' => '_wpseo_is_overridden_business_email',
2✔
677
                                        'value'         => '_wpseo_business_email',
2✔
678
                                ],
2✔
679
                                'org-tax-id' => [
2✔
680
                                        'is_overridden' => '_wpseo_is_overridden_business_tax_id',
2✔
681
                                        'value'         => '_wpseo_business_tax_id',
2✔
682
                                ],
2✔
683
                                'org-vat-id' => [
2✔
684
                                        'is_overridden' => '_wpseo_is_overridden_business_vat_id',
2✔
685
                                        'value'         => '_wpseo_business_vat_id',
2✔
686
                                ],
2✔
687
                        ];
2✔
688

689
                        foreach ( $location_keys as $key => $meta_keys ) {
4✔
690
                                $is_overridden = ( $shared_info === 'on' ) ? \get_post_meta( $primary_location, $meta_keys['is_overridden'], true ) : false;
4✔
691
                                if ( $is_overridden === 'on' || $shared_info !== 'on' ) {
4✔
692
                                        $post_meta_value                  = \get_post_meta( $primary_location, $meta_keys['value'], true );
4✔
693
                                        $defaults['wpseo_titles'][ $key ] = ( $post_meta_value ) ? $post_meta_value : '';
4✔
694
                                }
695
                        }
696
                }
697

698
                return $defaults;
8✔
699
        }
700

701
        /**
702
         * Retrieves the settings and their values.
703
         *
704
         * @param array $default_setting_values The default setting values.
705
         *
706
         * @return array The settings.
707
         */
708
        protected function get_settings( $default_setting_values ) {
×
709
                $settings = [];
×
710

711
                // Add Yoast settings.
712
                foreach ( WPSEO_Options::$options as $option_name => $instance ) {
×
713
                        if ( \in_array( $option_name, self::ALLOWED_OPTION_GROUPS, true ) ) {
×
714
                                $settings[ $option_name ] = \array_merge( $default_setting_values[ $option_name ], WPSEO_Options::get_option( $option_name ) );
×
715
                        }
716
                }
717
                // Add WP settings.
718
                foreach ( self::WP_OPTIONS as $option_name ) {
×
719
                        $settings[ $option_name ] = \get_option( $option_name );
×
720
                }
721

722
                // Remove disallowed settings.
723
                foreach ( self::DISALLOWED_SETTINGS as $option_name => $disallowed_settings ) {
×
724
                        foreach ( $disallowed_settings as $disallowed_setting ) {
×
725
                                unset( $settings[ $option_name ][ $disallowed_setting ] );
×
726
                        }
727
                }
728

729
                return $settings;
×
730
        }
731

732
        /**
733
         * Transforms setting values.
734
         *
735
         * @param array $settings The settings.
736
         *
737
         * @return array The settings.
738
         */
739
        protected function transform_settings( $settings ) {
×
740
                if ( isset( $settings['wpseo_titles']['breadcrumbs-sep'] ) ) {
×
741
                        /**
742
                         * The breadcrumbs separator default value is the HTML entity `&raquo;`.
743
                         * Which does not get decoded in our JS, while it did in our Yoast form. Decode it here as an exception.
744
                         */
745
                        $settings['wpseo_titles']['breadcrumbs-sep'] = \html_entity_decode(
×
746
                                $settings['wpseo_titles']['breadcrumbs-sep'],
×
747
                                ( \ENT_NOQUOTES | \ENT_HTML5 ),
×
748
                                'UTF-8'
×
749
                        );
750
                }
751

752
                /**
753
                 * Decode some WP options.
754
                 */
755
                $settings['blogdescription'] = \html_entity_decode(
×
756
                        $settings['blogdescription'],
×
757
                        ( \ENT_NOQUOTES | \ENT_HTML5 ),
×
758
                        'UTF-8'
×
759
                );
760

761
                return $settings;
×
762
        }
763

764
        /**
765
         * Retrieves the disabled settings.
766
         *
767
         * @param array $settings The settings.
768
         *
769
         * @return array The settings.
770
         */
771
        protected function get_disabled_settings( $settings ) {
×
772
                $disabled_settings = [];
×
773
                $site_language     = $this->language_helper->get_language();
×
774

775
                foreach ( WPSEO_Options::$options as $option_name => $instance ) {
×
776
                        if ( ! \in_array( $option_name, self::ALLOWED_OPTION_GROUPS, true ) ) {
×
777
                                continue;
×
778
                        }
779

780
                        $disabled_settings[ $option_name ] = [];
×
781
                        $option_instance                   = WPSEO_Options::get_option_instance( $option_name );
×
782
                        if ( $option_instance === false ) {
×
783
                                continue;
×
784
                        }
785
                        foreach ( $settings[ $option_name ] as $setting_name => $setting_value ) {
×
786
                                if ( $option_instance->is_disabled( $setting_name ) ) {
×
787
                                        $disabled_settings[ $option_name ][ $setting_name ] = 'network';
×
788
                                }
789
                        }
790
                }
791

792
                // Remove disabled on multisite settings.
793
                if ( \is_multisite() ) {
×
794
                        foreach ( self::DISABLED_ON_MULTISITE_SETTINGS as $option_name => $disabled_ms_settings ) {
×
795
                                if ( \array_key_exists( $option_name, $disabled_settings ) ) {
×
796
                                        foreach ( $disabled_ms_settings as $disabled_ms_setting ) {
×
797
                                                $disabled_settings[ $option_name ][ $disabled_ms_setting ] = 'multisite';
×
798
                                        }
799
                                }
800
                        }
801
                }
802

803
                if ( \array_key_exists( 'wpseo', $disabled_settings ) && ! $this->language_helper->has_inclusive_language_support( $site_language ) ) {
×
804
                        $disabled_settings['wpseo']['inclusive_language_analysis_active'] = 'language';
×
805
                }
806

807
                return $disabled_settings;
×
808
        }
809

810
        /**
811
         * Retrieves the replacement variables.
812
         *
813
         * @return array The replacement variables.
814
         */
815
        protected function get_replacement_variables() {
×
816
                $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
×
817
                $specific_replace_vars    = new WPSEO_Admin_Editor_Specific_Replace_Vars();
×
818
                $replacement_variables    = $this->replace_vars->get_replacement_variables_with_labels();
×
819

820
                return [
821
                        'variables'   => $replacement_variables,
×
822
                        'recommended' => $recommended_replace_vars->get_recommended_replacevars(),
×
823
                        'specific'    => $specific_replace_vars->get(),
×
824
                        'shared'      => $specific_replace_vars->get_generic( $replacement_variables ),
×
825
                ];
826
        }
827

828
        /**
829
         * Retrieves the schema.
830
         *
831
         * @param array $post_types The post types.
832
         *
833
         * @return array The schema.
834
         */
835
        protected function get_schema( array $post_types ) {
×
836
                $schema = [];
×
837

838
                foreach ( $this->schema_types->get_article_type_options() as $article_type ) {
×
839
                        $schema['articleTypes'][ $article_type['value'] ] = [
×
840
                                'label' => $article_type['name'],
×
841
                                'value' => $article_type['value'],
×
842
                        ];
843
                }
844

845
                foreach ( $this->schema_types->get_page_type_options() as $page_type ) {
×
846
                        $schema['pageTypes'][ $page_type['value'] ] = [
×
847
                                'label' => $page_type['name'],
×
848
                                'value' => $page_type['value'],
×
849
                        ];
850
                }
851

852
                $schema['articleTypeDefaults'] = [];
×
853
                $schema['pageTypeDefaults']    = [];
×
854
                foreach ( $post_types as $name => $post_type ) {
×
855
                        $schema['articleTypeDefaults'][ $name ] = WPSEO_Options::get_default( 'wpseo_titles', "schema-article-type-$name" );
×
856
                        $schema['pageTypeDefaults'][ $name ]    = WPSEO_Options::get_default( 'wpseo_titles', "schema-page-type-$name" );
×
857
                }
858

859
                return $schema;
×
860
        }
861

862
        /**
863
         * Transforms the post types, to represent them.
864
         *
865
         * @param WP_Post_Type[] $post_types The WP_Post_Type array to transform.
866
         *
867
         * @return array The post types.
868
         */
869
        protected function transform_post_types( $post_types ) {
4✔
870
                $transformed    = [];
4✔
871
                $new_post_types = $this->options->get( 'new_post_types', [] );
4✔
872
                foreach ( $post_types as $post_type ) {
4✔
873
                        $transformed[ $post_type->name ] = [
4✔
874
                                'name'                 => $post_type->name,
4✔
875
                                'route'                => $this->get_route( $post_type->name, $post_type->rewrite, $post_type->rest_base ),
4✔
876
                                'label'                => $post_type->label,
4✔
877
                                'singularLabel'        => $post_type->labels->singular_name,
4✔
878
                                'hasArchive'           => $this->post_type_helper->has_archive( $post_type ),
4✔
879
                                'hasSchemaArticleType' => $this->article_helper->is_article_post_type( $post_type->name ),
4✔
880
                                'menuPosition'         => $post_type->menu_position,
4✔
881
                                'isNew'                => \in_array( $post_type->name, $new_post_types, true ),
4✔
882
                        ];
2✔
883
                }
884

885
                \uasort( $transformed, [ $this, 'compare_post_types' ] );
4✔
886

887
                return $transformed;
4✔
888
        }
889

890
        /**
891
         * Compares two post types.
892
         *
893
         * @param array $a The first post type.
894
         * @param array $b The second post type.
895
         *
896
         * @return int The order.
897
         */
898
        protected function compare_post_types( $a, $b ) {
×
899
                if ( $a['menuPosition'] === null && $b['menuPosition'] !== null ) {
×
900
                        return 1;
×
901
                }
902
                if ( $a['menuPosition'] !== null && $b['menuPosition'] === null ) {
×
903
                        return -1;
×
904
                }
905

906
                if ( $a['menuPosition'] === null && $b['menuPosition'] === null ) {
×
907
                        // No position specified, order alphabetically by label.
908
                        return \strnatcmp( $a['label'], $b['label'] );
×
909
                }
910

911
                return ( ( $a['menuPosition'] < $b['menuPosition'] ) ? -1 : 1 );
×
912
        }
913

914
        /**
915
         * Transforms the taxonomies, to represent them.
916
         *
917
         * @param WP_Taxonomy[] $taxonomies      The WP_Taxonomy array to transform.
918
         * @param string[]      $post_type_names The post type names.
919
         *
920
         * @return array The taxonomies.
921
         */
922
        protected function transform_taxonomies( $taxonomies, $post_type_names ) {
4✔
923
                $transformed    = [];
4✔
924
                $new_taxonomies = $this->options->get( 'new_taxonomies', [] );
4✔
925
                foreach ( $taxonomies as $taxonomy ) {
4✔
926
                        $transformed[ $taxonomy->name ] = [
4✔
927
                                'name'          => $taxonomy->name,
4✔
928
                                'route'         => $this->get_route( $taxonomy->name, $taxonomy->rewrite, $taxonomy->rest_base ),
4✔
929
                                'label'         => $taxonomy->label,
4✔
930
                                'showUi'        => $taxonomy->show_ui,
4✔
931
                                'singularLabel' => $taxonomy->labels->singular_name,
4✔
932
                                'postTypes'     => \array_filter(
4✔
933
                                        $taxonomy->object_type,
4✔
934
                                        static function ( $object_type ) use ( $post_type_names ) {
2✔
935
                                                return \in_array( $object_type, $post_type_names, true );
4✔
936
                                        }
2✔
937
                                ),
4✔
938
                                'isNew'         => \in_array( $taxonomy->name, $new_taxonomies, true ),
2✔
939
                        ];
2✔
940
                }
941

942
                \uasort(
4✔
943
                        $transformed,
2✔
944
                        static function ( $a, $b ) {
2✔
945
                                return \strnatcmp( $a['label'], $b['label'] );
946
                        }
2✔
947
                );
4✔
948

949
                return $transformed;
950
        }
951

952
        /**
953
         * Gets the route from a name, rewrite and rest_base.
954
         *
955
         * @param string $name      The name.
956
         * @param array  $rewrite   The rewrite data.
957
         * @param string $rest_base The rest base.
958
         *
959
         * @return string The route.
960
         */
961
        protected function get_route( $name, $rewrite, $rest_base ) {
×
962
                $route = $name;
×
963
                if ( isset( $rewrite['slug'] ) ) {
964
                        $route = $rewrite['slug'];
×
965
                }
966
                if ( ! empty( $rest_base ) ) {
967
                        $route = $rest_base;
968
                }
969
                // Always strip leading slashes.
970
                while ( \substr( $route, 0, 1 ) === '/' ) {
971
                        $route = \substr( $route, 1 );
972
                }
973

974
                return \rawurlencode( $route );
975
        }
976

977
        /**
978
         * Retrieves the fallbacks.
979
         *
980
         * @return array The fallbacks.
981
         */
982
        protected function get_fallbacks() {
×
983
                $site_logo_id = \get_option( 'site_logo' );
×
984
                if ( ! $site_logo_id ) {
985
                        $site_logo_id = \get_theme_mod( 'custom_logo' );
×
986
                }
987
                if ( ! $site_logo_id ) {
988
                        $site_logo_id = '0';
989
                }
990

991
                return [
992
                        'siteLogoId' => $site_logo_id,
993
                ];
×
994
        }
995
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc