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

Yoast / wordpress-seo / 8b43089c625103ac862fa11105ec20207cf5705a

13 Aug 2024 11:22AM UTC coverage: 51.228% (+0.2%) from 51.025%
8b43089c625103ac862fa11105ec20207cf5705a

Pull #21314

github

web-flow
Merge c66634528 into 82a18954b
Pull Request #21314: Don't overwrite user settings when the respective features are disabled

152 of 164 new or added lines in 10 files covered. (92.68%)

197 existing lines in 6 files now uncovered.

16378 of 31971 relevant lines covered (51.23%)

3.92 hits per line

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

28.34
/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
                        'zapier_subscription',
64
                        'import_cursors',
65
                        'workouts_data',
66
                        'configuration_finished_steps',
67
                        'importing_completed',
68
                        'wincher_tokens',
69
                        'least_readability_ignore_list',
70
                        'least_seo_score_ignore_list',
71
                        'most_linked_ignore_list',
72
                        'least_linked_ignore_list',
73
                        'indexables_page_reading_list',
74
                        'show_new_content_type_notification',
75
                        'new_post_types',
76
                        'new_taxonomies',
77
                ],
78
                'wpseo_titles' => [
79
                        'company_logo_meta',
80
                        'person_logo_meta',
81
                ],
82
        ];
83

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
279
                        return;
×
280
                }
281

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

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

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

UNCOV
333
                return $pages;
×
334
        }
335

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

359
                return $pages;
2✔
360
        }
361

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
544
                return $person;
×
545
        }
546

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

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

UNCOV
565
                return $policies;
×
566
        }
567

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

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

UNCOV
594
                $policies[ $key ] = $policy_array;
×
595

596
                return $policies;
×
597
        }
598

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

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

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

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

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

UNCOV
645
                return $defaults;
×
646
        }
647

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

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

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

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

699
                return $defaults;
8✔
700
        }
701

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

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

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

UNCOV
730
                return $settings;
×
731
        }
732

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

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

UNCOV
762
                return $settings;
×
763
        }
764

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

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

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

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

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

808
                return $disabled_settings;
×
809
        }
810

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

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

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

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

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

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

UNCOV
860
                return $schema;
×
861
        }
862

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

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

888
                return $transformed;
4✔
889
        }
890

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

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

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

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

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

950
                return $transformed;
951
        }
952

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

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

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

992
                return [
993
                        'siteLogoId' => $site_logo_id,
994
                ];
995
        }
996
}
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