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

Yoast / wordpress-seo / 6638054992

25 Oct 2023 08:55AM UTC coverage: 49.106%. First build
6638054992

Pull #20653

github

vraja-pro
Merge remote-tracking branch 'origin/feature/upgrade-react-and-tests' into feature/upgrade-react-and-tests
Pull Request #20653: Feature/upgrade react and tests

64 of 157 new or added lines in 34 files covered. (40.76%)

13135 of 26748 relevant lines covered (49.11%)

3.96 hits per line

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

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

3
namespace Yoast\WP\SEO\Integrations;
4

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

30

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

36
        const PAGE = 'wpseo_page_settings';
37

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

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

52
        /**
53
         * Holds the disallowed settings, per option group.
54
         *
55
         * @var array
56
         */
57
        const DISALLOWED_SETTINGS = [
58
                'wpseo'        => [
59
                        'myyoast-oauth',
60
                        'semrush_tokens',
61
                        'custom_taxonomy_slugs',
62
                        'zapier_subscription',
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
        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(
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() {
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
                        // phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged -- This deprecation will be addressed later.
261
                        $post_action = \filter_input( \INPUT_POST, 'action', @\FILTER_SANITIZE_STRING );
×
262
                        $option_page = \filter_input( \INPUT_POST, 'option_page', @\FILTER_SANITIZE_STRING );
×
263
                        // phpcs:enable
264

265
                        if ( $post_action === 'update' && $option_page === self::PAGE ) {
×
266
                                \add_action( 'admin_init', [ $this, 'register_setting' ] );
×
267
                                \add_action( 'in_admin_header', [ $this, 'remove_notices' ], \PHP_INT_MAX );
×
268
                        }
269

270
                        return;
×
271
                }
272

273
                // Are we on the settings page?
274
                if ( $this->current_page_helper->get_current_yoast_seo_page() === self::PAGE ) {
×
275
                        \add_action( 'admin_init', [ $this, 'register_setting' ] );
×
276
                        \add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
×
277
                        \add_action( 'in_admin_header', [ $this, 'remove_notices' ], \PHP_INT_MAX );
×
278

279
                }
280
        }
281

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

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

325
                return $pages;
×
326
        }
327

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

351
                return $pages;
2✔
352
        }
353

354
        /**
355
         * Displays the page.
356
         */
357
        public function display_page() {
358
                echo '<div id="yoast-seo-settings"></div>';
×
359
        }
360

361
        /**
362
         * Enqueues the assets.
363
         *
364
         * @return void
365
         */
366
        public function enqueue_assets() {
367
                // Remove the emoji script as it is incompatible with both React and any contenteditable fields.
368
                \remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
×
369
                \wp_enqueue_media();
×
370
                $this->asset_manager->enqueue_script( 'new-settings' );
×
371
                $this->asset_manager->enqueue_style( 'new-settings' );
×
NEW
372
                if ( YoastSEO()->classes->get( Promotion_Manager::class )->is( 'black-friday-2023-promotion' ) ) {
×
NEW
373
                        $this->asset_manager->enqueue_style( 'black-friday-banner' );
×
374
                }
375
                $this->asset_manager->localize_script( 'new-settings', 'wpseoScriptData', $this->get_script_data() );
×
376
        }
377

378
        /**
379
         * Removes all current WP notices.
380
         *
381
         * @return void
382
         */
383
        public function remove_notices() {
384
                \remove_all_actions( 'admin_notices' );
×
385
                \remove_all_actions( 'user_admin_notices' );
×
386
                \remove_all_actions( 'network_admin_notices' );
×
387
                \remove_all_actions( 'all_admin_notices' );
×
388
        }
389

390
        /**
391
         * Creates the script data.
392
         *
393
         * @return array The script data.
394
         */
395
        protected function get_script_data() {
396
                $default_setting_values = $this->get_default_setting_values();
×
397
                $settings               = $this->get_settings( $default_setting_values );
×
398
                $post_types             = $this->post_type_helper->get_indexable_post_type_objects();
×
399
                $taxonomies             = $this->taxonomy_helper->get_indexable_taxonomy_objects();
×
400

401
                // Check if attachments are included in indexation.
402
                if ( ! \array_key_exists( 'attachment', $post_types ) ) {
×
403
                        // Always include attachments in the settings, to let the user enable them again.
404
                        $attachment_object = \get_post_type_object( 'attachment' );
×
405
                        if ( ! empty( $attachment_object ) ) {
×
406
                                $post_types['attachment'] = $attachment_object;
×
407
                        }
408
                }
409
                // Check if post formats are included in indexation.
410
                if ( ! \array_key_exists( 'post_format', $taxonomies ) ) {
×
411
                        // Always include post_format in the settings, to let the user enable them again.
412
                        $post_format_object = \get_taxonomy( 'post_format' );
×
413
                        if ( ! empty( $post_format_object ) ) {
×
414
                                $taxonomies['post_format'] = $post_format_object;
×
415
                        }
416
                }
417

418
                $transformed_post_types = $this->transform_post_types( $post_types );
×
419
                $transformed_taxonomies = $this->transform_taxonomies( $taxonomies, \array_keys( $transformed_post_types ) );
×
420

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

424
                return [
425
                        'settings'                       => $this->transform_settings( $settings ),
×
426
                        'defaultSettingValues'           => $default_setting_values,
×
427
                        'disabledSettings'               => $this->get_disabled_settings( $settings ),
×
428
                        'endpoint'                       => \admin_url( 'options.php' ),
×
429
                        'nonce'                          => \wp_create_nonce( self::PAGE . '-options' ),
×
430
                        'separators'                     => WPSEO_Option_Titles::get_instance()->get_separator_options_for_display(),
×
431
                        'replacementVariables'           => $this->get_replacement_variables(),
×
432
                        'schema'                         => $this->get_schema( $transformed_post_types ),
×
433
                        'preferences'                    => $this->get_preferences( $settings ),
×
434
                        'linkParams'                     => WPSEO_Shortlinker::get_query_params(),
×
435
                        'postTypes'                      => $transformed_post_types,
×
436
                        'taxonomies'                     => $transformed_taxonomies,
×
437
                        'fallbacks'                      => $this->get_fallbacks(),
×
438
                        'showNewContentTypeNotification' => $show_new_content_type_notification,
×
439
                ];
440
        }
441

442
        /**
443
         * Retrieves the preferences.
444
         *
445
         * @param array $settings The settings.
446
         *
447
         * @return array The preferences.
448
         */
449
        protected function get_preferences( $settings ) {
450
                $shop_page_id             = $this->woocommerce_helper->get_shop_page_id();
×
451
                $homepage_is_latest_posts = \get_option( 'show_on_front' ) === 'posts';
×
452
                $page_on_front            = \get_option( 'page_on_front' );
×
453
                $page_for_posts           = \get_option( 'page_for_posts' );
×
454

455
                if ( empty( $page_on_front ) ) {
×
456
                        $page_on_front = $page_for_posts;
×
457
                }
458

459
                return [
460
                        'isPremium'                     => $this->product_helper->is_premium(),
×
461
                        'isRtl'                         => \is_rtl(),
×
462
                        'isNetworkAdmin'                => \is_network_admin(),
×
463
                        'isMainSite'                    => \is_main_site(),
×
NEW
464
                        'isMultisite'                   => \is_multisite(),
×
465
                        'isWooCommerceActive'           => $this->woocommerce_helper->is_active(),
×
466
                        'isLocalSeoActive'              => \defined( 'WPSEO_LOCAL_FILE' ),
×
467
                        'isNewsSeoActive'               => \defined( 'WPSEO_NEWS_FILE' ),
×
NEW
468
                        'promotions'                    => YoastSEO()->classes->get( Promotion_Manager::class )->get_current_promotions(),
×
469
                        'siteUrl'                       => \get_bloginfo( 'url' ),
×
470
                        'siteTitle'                     => \get_bloginfo( 'name' ),
×
471
                        'sitemapUrl'                    => WPSEO_Sitemaps_Router::get_base_url( 'sitemap_index.xml' ),
×
472
                        'hasWooCommerceShopPage'        => $shop_page_id !== -1,
473
                        'editWooCommerceShopPageUrl'    => \get_edit_post_link( $shop_page_id, 'js' ),
×
474
                        'wooCommerceShopPageSettingUrl' => \get_admin_url( null, 'admin.php?page=wc-settings&tab=products' ),
×
475
                        'homepageIsLatestPosts'         => $homepage_is_latest_posts,
×
476
                        'homepagePageEditUrl'           => \get_edit_post_link( $page_on_front, 'js' ),
×
477
                        'homepagePostsEditUrl'          => \get_edit_post_link( $page_for_posts, 'js' ),
×
478
                        'createUserUrl'                 => \admin_url( 'user-new.php' ),
×
479
                        'createPageUrl'                 => \admin_url( 'post-new.php?post_type=page' ),
×
480
                        'editUserUrl'                   => \admin_url( 'user-edit.php' ),
×
481
                        'editTaxonomyUrl'               => \admin_url( 'edit-tags.php' ),
×
482
                        'generalSettingsUrl'            => \admin_url( 'options-general.php' ),
×
483
                        'companyOrPersonMessage'        => \apply_filters( 'wpseo_knowledge_graph_setting_msg', '' ),
×
484
                        'currentUserId'                 => \get_current_user_id(),
×
485
                        'canCreateUsers'                => \current_user_can( 'create_users' ),
×
486
                        'canCreatePages'                => \current_user_can( 'edit_pages' ),
×
487
                        'canEditUsers'                  => \current_user_can( 'edit_users' ),
×
488
                        'canManageOptions'              => \current_user_can( 'manage_options' ),
×
489
                        'userLocale'                    => \str_replace( '_', '-', \get_user_locale() ),
×
490
                        'pluginUrl'                     => \plugins_url( '', \WPSEO_FILE ),
×
491
                        'showForceRewriteTitlesSetting' => ! \current_theme_supports( 'title-tag' ) && ! ( \function_exists( 'wp_is_block_theme' ) && \wp_is_block_theme() ),
×
492
                        'upsellSettings'                => $this->get_upsell_settings(),
×
493
                        'siteRepresentsPerson'          => $this->get_site_represents_person( $settings ),
×
494
                        'siteBasicsPolicies'            => $this->get_site_basics_policies( $settings ),
×
495
                ];
496
        }
497

498
        /**
499
         * Retrieves the currently represented person.
500
         *
501
         * @param array $settings The settings.
502
         *
503
         * @return array The currently represented person's ID and name.
504
         */
505
        protected function get_site_represents_person( $settings ) {
506
                $person = [
507
                        'id'   => false,
×
508
                        'name' => '',
509
                ];
510

511
                if ( isset( $settings['wpseo_titles']['company_or_person_user_id'] ) ) {
×
512
                        $person['id'] = $settings['wpseo_titles']['company_or_person_user_id'];
×
513
                        $user         = \get_userdata( $person['id'] );
×
514
                        if ( $user instanceof WP_User ) {
×
515
                                $person['name'] = $user->get( 'display_name' );
×
516
                        }
517
                }
518

519
                return $person;
×
520
        }
521

522
        /**
523
         * Get site policy data.
524
         *
525
         * @param array $settings The settings.
526
         *
527
         * @return array The policy data.
528
         */
529
        private function get_site_basics_policies( $settings ) {
530
                $policies = [];
×
531

532

533
                $policies = $this->maybe_add_policy( $policies, $settings['wpseo_titles']['publishing_principles_id'], 'publishing_principles_id' );
×
534
                $policies = $this->maybe_add_policy( $policies, $settings['wpseo_titles']['ownership_funding_info_id'], 'ownership_funding_info_id' );
×
535
                $policies = $this->maybe_add_policy( $policies, $settings['wpseo_titles']['actionable_feedback_policy_id'], 'actionable_feedback_policy_id' );
×
536
                $policies = $this->maybe_add_policy( $policies, $settings['wpseo_titles']['corrections_policy_id'], 'corrections_policy_id' );
×
537
                $policies = $this->maybe_add_policy( $policies, $settings['wpseo_titles']['ethics_policy_id'], 'ethics_policy_id' );
×
538
                $policies = $this->maybe_add_policy( $policies, $settings['wpseo_titles']['diversity_policy_id'], 'diversity_policy_id' );
×
539
                $policies = $this->maybe_add_policy( $policies, $settings['wpseo_titles']['diversity_staffing_report_id'], 'diversity_staffing_report_id' );
×
540

541
                return $policies;
×
542
        }
543

544
        /**
545
         * Adds policy data if it is present.
546
         *
547
         * @param array  $policies The existing policy data.
548
         * @param int    $policy   The policy id to check.
549
         * @param string $key      The option key name.
550
         *
551
         * @return array The policy data.
552
         */
553
        private function maybe_add_policy( $policies, $policy, $key ) {
554
                $policy_array = [
555
                        'id'   => 0,
×
556
                        'name' => \__( 'None', 'wordpress-seo' ),
×
557
                ];
558

559
                if ( isset( $policy ) && \is_int( $policy ) ) {
×
560
                        $policy_array['id'] = $policy;
×
561
                        $post               = \get_post( $policy );
×
562
                        if ( $post instanceof \WP_Post ) {
×
563
                                if ( $post->post_status !== 'publish' || $post->post_password !== '' ) {
×
564
                                        return $policies;
×
565
                                }
566
                                $policy_array['name'] = $post->post_title;
×
567
                        }
568
                }
569

570
                $policies[ $key ] = $policy_array;
×
571

572
                return $policies;
×
573
        }
574

575
        /**
576
         * Returns settings for the Call to Buy (CTB) buttons.
577
         *
578
         * @return string[] The array of CTB settings.
579
         */
580
        public function get_upsell_settings() {
581
                return [
582
                        'actionId'     => 'load-nfd-ctb',
×
583
                        'premiumCtbId' => 'f6a84663-465f-4cb5-8ba5-f7a6d72224b2',
584
                ];
585
        }
586

587
        /**
588
         * Retrieves the default setting values.
589
         *
590
         * These default values are currently being used in the UI for dummy fields.
591
         * Dummy fields should not expose or reflect the actual data.
592
         *
593
         * @return array The default setting values.
594
         */
595
        protected function get_default_setting_values() {
596
                $defaults = [];
×
597

598
                // Add Yoast settings.
599
                foreach ( WPSEO_Options::$options as $option_name => $instance ) {
×
600
                        if ( \in_array( $option_name, self::ALLOWED_OPTION_GROUPS, true ) ) {
×
601
                                $option_instance          = WPSEO_Options::get_option_instance( $option_name );
×
602
                                $defaults[ $option_name ] = ( $option_instance ) ? $option_instance->get_defaults() : [];
×
603
                        }
604
                }
605
                // Add WP settings.
606
                foreach ( self::WP_OPTIONS as $option_name ) {
×
607
                        $defaults[ $option_name ] = '';
×
608
                }
609

610
                // Remove disallowed settings.
611
                foreach ( self::DISALLOWED_SETTINGS as $option_name => $disallowed_settings ) {
×
612
                        foreach ( $disallowed_settings as $disallowed_setting ) {
×
613
                                unset( $defaults[ $option_name ][ $disallowed_setting ] );
×
614
                        }
615
                }
616

617
                return $defaults;
×
618
        }
619

620
        /**
621
         * Retrieves the settings and their values.
622
         *
623
         * @param array $default_setting_values The default setting values.
624
         *
625
         * @return array The settings.
626
         */
627
        protected function get_settings( $default_setting_values ) {
628
                $settings = [];
×
629

630
                // Add Yoast settings.
631
                foreach ( WPSEO_Options::$options as $option_name => $instance ) {
×
632
                        if ( \in_array( $option_name, self::ALLOWED_OPTION_GROUPS, true ) ) {
×
633
                                $settings[ $option_name ] = \array_merge( $default_setting_values[ $option_name ], WPSEO_Options::get_option( $option_name ) );
×
634
                        }
635
                }
636
                // Add WP settings.
637
                foreach ( self::WP_OPTIONS as $option_name ) {
×
638
                        $settings[ $option_name ] = \get_option( $option_name );
×
639
                }
640

641
                // Remove disallowed settings.
642
                foreach ( self::DISALLOWED_SETTINGS as $option_name => $disallowed_settings ) {
×
643
                        foreach ( $disallowed_settings as $disallowed_setting ) {
×
644
                                unset( $settings[ $option_name ][ $disallowed_setting ] );
×
645
                        }
646
                }
647

648
                return $settings;
×
649
        }
650

651
        /**
652
         * Transforms setting values.
653
         *
654
         * @param array $settings The settings.
655
         *
656
         * @return array The settings.
657
         */
658
        protected function transform_settings( $settings ) {
659
                if ( isset( $settings['wpseo_titles']['breadcrumbs-sep'] ) ) {
×
660
                        /**
661
                         * The breadcrumbs separator default value is the HTML entity `&raquo;`.
662
                         * Which does not get decoded in our JS, while it did in our Yoast form. Decode it here as an exception.
663
                         */
664
                        $settings['wpseo_titles']['breadcrumbs-sep'] = \html_entity_decode(
×
665
                                $settings['wpseo_titles']['breadcrumbs-sep'],
×
666
                                ( \ENT_NOQUOTES | \ENT_HTML5 ),
×
667
                                'UTF-8'
×
668
                        );
669
                }
670

671
                /**
672
                 * Decode some WP options.
673
                 */
674
                $settings['blogdescription'] = \html_entity_decode(
×
675
                        $settings['blogdescription'],
×
676
                        ( \ENT_NOQUOTES | \ENT_HTML5 ),
×
677
                        'UTF-8'
×
678
                );
679

680
                return $settings;
×
681
        }
682

683
        /**
684
         * Retrieves the disabled settings.
685
         *
686
         * @param array $settings The settings.
687
         *
688
         * @return array The settings.
689
         */
690
        protected function get_disabled_settings( $settings ) {
691
                $disabled_settings = [];
×
692
                $site_language     = $this->language_helper->get_language();
×
693

694
                foreach ( WPSEO_Options::$options as $option_name => $instance ) {
×
695
                        if ( ! \in_array( $option_name, self::ALLOWED_OPTION_GROUPS, true ) ) {
×
696
                                continue;
×
697
                        }
698

699
                        $disabled_settings[ $option_name ] = [];
×
700
                        $option_instance                   = WPSEO_Options::get_option_instance( $option_name );
×
701
                        if ( $option_instance === false ) {
×
702
                                continue;
×
703
                        }
704
                        foreach ( $settings[ $option_name ] as $setting_name => $setting_value ) {
×
705
                                if ( $option_instance->is_disabled( $setting_name ) ) {
×
706
                                        $disabled_settings[ $option_name ][ $setting_name ] = 'network';
×
707
                                }
708
                        }
709
                }
710

711
                // Remove disabled on multisite settings.
712
                if ( \is_multisite() ) {
×
713
                        foreach ( self::DISABLED_ON_MULTISITE_SETTINGS as $option_name => $disabled_ms_settings ) {
×
714
                                if ( \array_key_exists( $option_name, $disabled_settings ) ) {
×
715
                                        foreach ( $disabled_ms_settings as $disabled_ms_setting ) {
×
716
                                                $disabled_settings[ $option_name ][ $disabled_ms_setting ] = 'multisite';
×
717
                                        }
718
                                }
719
                        }
720
                }
721

722
                if ( \array_key_exists( 'wpseo', $disabled_settings ) && ! $this->language_helper->has_inclusive_language_support( $site_language ) ) {
×
723
                        $disabled_settings['wpseo']['inclusive_language_analysis_active'] = 'language';
×
724
                }
725

726
                return $disabled_settings;
×
727
        }
728

729
        /**
730
         * Retrieves the replacement variables.
731
         *
732
         * @return array The replacement variables.
733
         */
734
        protected function get_replacement_variables() {
735
                $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
×
736
                $specific_replace_vars    = new WPSEO_Admin_Editor_Specific_Replace_Vars();
×
737
                $replacement_variables    = $this->replace_vars->get_replacement_variables_with_labels();
×
738

739
                return [
740
                        'variables'   => $replacement_variables,
×
741
                        'recommended' => $recommended_replace_vars->get_recommended_replacevars(),
×
742
                        'specific'    => $specific_replace_vars->get(),
×
743
                        'shared'      => $specific_replace_vars->get_generic( $replacement_variables ),
×
744
                ];
745
        }
746

747
        /**
748
         * Retrieves the schema.
749
         *
750
         * @param array $post_types The post types.
751
         *
752
         * @return array The schema.
753
         */
754
        protected function get_schema( array $post_types ) {
755
                $schema = [];
×
756

757
                foreach ( $this->schema_types->get_article_type_options() as $article_type ) {
×
758
                        $schema['articleTypes'][ $article_type['value'] ] = [
×
759
                                'label' => $article_type['name'],
×
760
                                'value' => $article_type['value'],
×
761
                        ];
762
                }
763

764
                foreach ( $this->schema_types->get_page_type_options() as $page_type ) {
×
765
                        $schema['pageTypes'][ $page_type['value'] ] = [
×
766
                                'label' => $page_type['name'],
×
767
                                'value' => $page_type['value'],
×
768
                        ];
769
                }
770

771
                $schema['articleTypeDefaults'] = [];
×
772
                $schema['pageTypeDefaults']    = [];
×
773
                foreach ( $post_types as $name => $post_type ) {
×
774
                        $schema['articleTypeDefaults'][ $name ] = WPSEO_Options::get_default( 'wpseo_titles', "schema-article-type-$name" );
×
775
                        $schema['pageTypeDefaults'][ $name ]    = WPSEO_Options::get_default( 'wpseo_titles', "schema-page-type-$name" );
×
776
                }
777

778
                return $schema;
×
779
        }
780

781
        /**
782
         * Transforms the post types, to represent them.
783
         *
784
         * @param WP_Post_Type[] $post_types The WP_Post_Type array to transform.
785
         *
786
         * @return array The post types.
787
         */
788
        protected function transform_post_types( $post_types ) {
789
                $transformed    = [];
4✔
790
                $new_post_types = $this->options->get( 'new_post_types', [] );
4✔
791
                foreach ( $post_types as $post_type ) {
4✔
792
                        $transformed[ $post_type->name ] = [
4✔
793
                                'name'                 => $post_type->name,
4✔
794
                                'route'                => $this->get_route( $post_type->name, $post_type->rewrite, $post_type->rest_base ),
4✔
795
                                'label'                => $post_type->label,
4✔
796
                                'singularLabel'        => $post_type->labels->singular_name,
4✔
797
                                'hasArchive'           => $this->post_type_helper->has_archive( $post_type ),
4✔
798
                                'hasSchemaArticleType' => $this->article_helper->is_article_post_type( $post_type->name ),
4✔
799
                                'menuPosition'         => $post_type->menu_position,
4✔
800
                                'isNew'                => \in_array( $post_type->name, $new_post_types, true ),
4✔
801
                        ];
2✔
802
                }
803

804
                \uasort( $transformed, [ $this, 'compare_post_types' ] );
4✔
805

806
                return $transformed;
4✔
807
        }
808

809
        /**
810
         * Compares two post types.
811
         *
812
         * @param array $a The first post type.
813
         * @param array $b The second post type.
814
         *
815
         * @return int The order.
816
         */
817
        protected function compare_post_types( $a, $b ) {
818
                if ( $a['menuPosition'] === null && $b['menuPosition'] !== null ) {
×
819
                        return 1;
×
820
                }
821
                if ( $a['menuPosition'] !== null && $b['menuPosition'] === null ) {
×
822
                        return -1;
×
823
                }
824

825
                if ( $a['menuPosition'] === null && $b['menuPosition'] === null ) {
×
826
                        // No position specified, order alphabetically by label.
827
                        return \strnatcmp( $a['label'], $b['label'] );
×
828
                }
829

830
                return ( ( $a['menuPosition'] < $b['menuPosition'] ) ? -1 : 1 );
×
831
        }
832

833
        /**
834
         * Transforms the taxonomies, to represent them.
835
         *
836
         * @param WP_Taxonomy[] $taxonomies      The WP_Taxonomy array to transform.
837
         * @param string[]      $post_type_names The post type names.
838
         *
839
         * @return array The taxonomies.
840
         */
841
        protected function transform_taxonomies( $taxonomies, $post_type_names ) {
842
                $transformed    = [];
4✔
843
                $new_taxonomies = $this->options->get( 'new_taxonomies', [] );
4✔
844
                foreach ( $taxonomies as $taxonomy ) {
4✔
845
                        $transformed[ $taxonomy->name ] = [
4✔
846
                                'name'          => $taxonomy->name,
4✔
847
                                'route'         => $this->get_route( $taxonomy->name, $taxonomy->rewrite, $taxonomy->rest_base ),
4✔
848
                                'label'         => $taxonomy->label,
4✔
849
                                'showUi'        => $taxonomy->show_ui,
4✔
850
                                'singularLabel' => $taxonomy->labels->singular_name,
4✔
851
                                'postTypes'     => \array_filter(
4✔
852
                                        $taxonomy->object_type,
4✔
853
                                        static function ( $object_type ) use ( $post_type_names ) {
2✔
854
                                                return \in_array( $object_type, $post_type_names, true );
4✔
855
                                        }
2✔
856
                                ),
4✔
857
                                'isNew'         => \in_array( $taxonomy->name, $new_taxonomies, true ),
2✔
858
                        ];
2✔
859
                }
860

861
                \uasort(
4✔
862
                        $transformed,
2✔
863
                        static function ( $a, $b ) {
2✔
864
                                return \strnatcmp( $a['label'], $b['label'] );
865
                        }
2✔
866
                );
4✔
867

868
                return $transformed;
869
        }
870

871
        /**
872
         * Gets the route from a name, rewrite and rest_base.
873
         *
874
         * @param string $name      The name.
875
         * @param array  $rewrite   The rewrite data.
876
         * @param string $rest_base The rest base.
877
         *
878
         * @return string The route.
879
         */
880
        protected function get_route( $name, $rewrite, $rest_base ) {
881
                $route = $name;
×
882
                if ( isset( $rewrite['slug'] ) ) {
883
                        $route = $rewrite['slug'];
×
884
                }
885
                if ( ! empty( $rest_base ) ) {
886
                        $route = $rest_base;
887
                }
888
                // Always strip leading slashes.
889
                while ( \substr( $route, 0, 1 ) === '/' ) {
890
                        $route = \substr( $route, 1 );
891
                }
892

893
                return \rawurlencode( $route );
894
        }
895

896
        /**
897
         * Retrieves the fallbacks.
898
         *
899
         * @return array The fallbacks.
900
         */
901
        protected function get_fallbacks() {
902
                $site_logo_id = \get_option( 'site_logo' );
×
903
                if ( ! $site_logo_id ) {
904
                        $site_logo_id = \get_theme_mod( 'custom_logo' );
×
905
                }
906
                if ( ! $site_logo_id ) {
907
                        $site_logo_id = '0';
908
                }
909

910
                return [
911
                        'siteLogoId' => $site_logo_id,
912
                ];
913
        }
914
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc