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

Yoast / wordpress-seo / 4c5f9780cef7c22b7f9bfbace943d71badc638ed

08 Oct 2024 09:39AM UTC coverage: 54.494% (-0.04%) from 54.538%
4c5f9780cef7c22b7f9bfbace943d71badc638ed

push

github

YoastBot
Bump version to 23.6 on free

7502 of 13498 branches covered (55.58%)

Branch coverage included in aggregate %.

29530 of 54458 relevant lines covered (54.23%)

41951.32 hits per line

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

0.0
/src/integrations/third-party/elementor.php
1
<?php
2

3
namespace Yoast\WP\SEO\Integrations\Third_Party;
4

5
use WP_Post;
6
use WP_Screen;
7
use WPSEO_Admin_Asset_Manager;
8
use WPSEO_Admin_Recommended_Replace_Vars;
9
use WPSEO_Meta;
10
use WPSEO_Metabox_Analysis_Inclusive_Language;
11
use WPSEO_Metabox_Analysis_Readability;
12
use WPSEO_Metabox_Analysis_SEO;
13
use WPSEO_Metabox_Formatter;
14
use WPSEO_Post_Metabox_Formatter;
15
use WPSEO_Replace_Vars;
16
use WPSEO_Utils;
17
use Yoast\WP\SEO\Conditionals\Third_Party\Elementor_Edit_Conditional;
18
use Yoast\WP\SEO\Editors\Application\Site\Website_Information_Repository;
19
use Yoast\WP\SEO\Elementor\Infrastructure\Request_Post;
20
use Yoast\WP\SEO\Helpers\Capability_Helper;
21
use Yoast\WP\SEO\Helpers\Options_Helper;
22
use Yoast\WP\SEO\Integrations\Integration_Interface;
23
use Yoast\WP\SEO\Presenters\Admin\Meta_Fields_Presenter;
24
use Yoast\WP\SEO\Promotions\Application\Promotion_Manager;
25

26
/**
27
 * Integrates the Yoast SEO metabox in the Elementor editor.
28
 */
29
class Elementor implements Integration_Interface {
30

31
        /**
32
         * Represents the post.
33
         *
34
         * @var WP_Post|null
35
         */
36
        protected $post;
37

38
        /**
39
         * Represents the admin asset manager.
40
         *
41
         * @var WPSEO_Admin_Asset_Manager
42
         */
43
        protected $asset_manager;
44

45
        /**
46
         * Represents the options helper.
47
         *
48
         * @var Options_Helper
49
         */
50
        protected $options;
51

52
        /**
53
         * Represents the capability helper.
54
         *
55
         * @var Capability_Helper
56
         */
57
        protected $capability;
58

59
        /**
60
         * Holds the Request_Post.
61
         *
62
         * @var Request_Post
63
         */
64
        private $request_post;
65

66
        /**
67
         * Holds whether the socials are enabled.
68
         *
69
         * @var bool
70
         */
71
        protected $social_is_enabled;
72

73
        /**
74
         * Holds whether the advanced settings are enabled.
75
         *
76
         * @var bool
77
         */
78
        protected $is_advanced_metadata_enabled;
79

80
        /**
81
         * Helper to determine whether or not the SEO analysis is enabled.
82
         *
83
         * @var WPSEO_Metabox_Analysis_SEO
84
         */
85
        protected $seo_analysis;
86

87
        /**
88
         * Helper to determine whether or not the readability analysis is enabled.
89
         *
90
         * @var WPSEO_Metabox_Analysis_Readability
91
         */
92
        protected $readability_analysis;
93

94
        /**
95
         * Helper to determine whether or not the inclusive language analysis is enabled.
96
         *
97
         * @var WPSEO_Metabox_Analysis_Inclusive_Language
98
         */
99
        protected $inclusive_language_analysis;
100

101
        /**
102
         * Holds the promotion manager.
103
         *
104
         * @var Promotion_Manager
105
         */
106
        protected $promotion_manager;
107

108
        /**
109
         * Returns the conditionals based in which this loadable should be active.
110
         *
111
         * @return array
112
         */
113
        public static function get_conditionals() {
×
114
                return [ Elementor_Edit_Conditional::class ];
×
115
        }
116

117
        /**
118
         * Constructor.
119
         *
120
         * @param WPSEO_Admin_Asset_Manager $asset_manager The asset manager.
121
         * @param Options_Helper            $options       The options helper.
122
         * @param Capability_Helper         $capability    The capability helper.
123
         * @param Request_Post              $request_post  The Request_Post.
124
         */
125
        public function __construct(
×
126
                WPSEO_Admin_Asset_Manager $asset_manager,
127
                Options_Helper $options,
128
                Capability_Helper $capability,
129
                Request_Post $request_post
130
        ) {
131
                $this->asset_manager = $asset_manager;
×
132
                $this->options       = $options;
×
133
                $this->capability    = $capability;
×
134
                $this->request_post  = $request_post;
×
135

136
                $this->seo_analysis                 = new WPSEO_Metabox_Analysis_SEO();
×
137
                $this->readability_analysis         = new WPSEO_Metabox_Analysis_Readability();
×
138
                $this->inclusive_language_analysis  = new WPSEO_Metabox_Analysis_Inclusive_Language();
×
139
                $this->social_is_enabled            = $this->options->get( 'opengraph', false ) || $this->options->get( 'twitter', false );
×
140
                $this->is_advanced_metadata_enabled = $this->capability->current_user_can( 'wpseo_edit_advanced_metadata' ) || $this->options->get( 'disableadvanced_meta' ) === false;
×
141
        }
142

143
        /**
144
         * Initializes the integration.
145
         *
146
         * This is the place to register hooks and filters.
147
         *
148
         * @return void
149
         */
150
        public function register_hooks() {
×
151
                \add_action( 'wp_ajax_wpseo_elementor_save', [ $this, 'save_postdata' ] );
×
152

153
                // We need to delay the post type lookup to give other plugins a chance to register custom post types.
154
                \add_action( 'init', [ $this, 'register_elementor_hooks' ], \PHP_INT_MAX );
×
155
        }
156

157
        /**
158
         * Registers our Elementor hooks.
159
         * This is done for pages with metabox on page load and not on ajax request.
160
         *
161
         * @return void
162
         */
163
        public function register_elementor_hooks() {
×
164
                if ( $this->get_metabox_post() === null || ! $this->display_metabox( $this->get_metabox_post()->post_type ) ) {
×
165
                        return;
×
166
                }
167

168
                \add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'init' ] );
×
169
        }
170

171
        /**
172
         * Initializes the integration.
173
         *
174
         * @return void
175
         */
176
        public function init() {
×
177
                $this->asset_manager->register_assets();
×
178
                $this->enqueue();
×
179
                $this->render_hidden_fields();
×
180
        }
181

182
        // Below is mostly copied from `class-metabox.php`. That constructor has side-effects we do not need.
183

184
        /**
185
         * Determines whether the metabox should be shown for the passed identifier.
186
         *
187
         * By default, the check is done for post types, but can also be used for taxonomies.
188
         *
189
         * @param string|null $identifier The identifier to check.
190
         * @param string      $type       The type of object to check. Defaults to post_type.
191
         *
192
         * @return bool Whether the metabox should be displayed.
193
         */
194
        public function display_metabox( $identifier = null, $type = 'post_type' ) {
×
195
                return WPSEO_Utils::is_metabox_active( $identifier, $type );
×
196
        }
197

198
        /**
199
         * Saves the WP SEO metadata for posts.
200
         *
201
         * Outputs JSON via wp_send_json then stops code execution.
202
         *
203
         * {@internal $_POST parameters are validated via sanitize_post_meta().}}
204
         *
205
         * @return void
206
         */
207
        public function save_postdata() {
×
208
                global $post;
×
209

210
                if ( ! isset( $_POST['post_id'] ) || ! \is_string( $_POST['post_id'] ) ) {
×
211
                        \wp_send_json_error( 'Bad Request', 400 );
×
212
                }
213

214
                // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: No sanitization needed because we cast to an integer.
215
                $post_id = (int) \wp_unslash( $_POST['post_id'] );
×
216

217
                if ( $post_id <= 0 ) {
×
218
                        \wp_send_json_error( 'Bad Request', 400 );
×
219
                }
220

221
                if ( ! \current_user_can( 'edit_post', $post_id ) ) {
×
222
                        \wp_send_json_error( 'Forbidden', 403 );
×
223
                }
224

225
                \check_ajax_referer( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
×
226

227
                // Bail if this is a multisite installation and the site has been switched.
228
                if ( \is_multisite() && \ms_is_switched() ) {
×
229
                        \wp_send_json_error( 'Switched multisite', 409 );
×
230
                }
231

232
                \clean_post_cache( $post_id );
×
233
                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- To setup the post we need to do this explicitly.
234
                $post = \get_post( $post_id );
×
235

236
                if ( ! \is_object( $post ) ) {
×
237
                        // Non-existent post.
238
                        \wp_send_json_error( 'Post not found', 400 );
×
239
                }
240

241
                \do_action( 'wpseo_save_compare_data', $post );
×
242

243
                // Initialize meta, amongst other things it registers sanitization.
244
                WPSEO_Meta::init();
×
245

246
                $social_fields = [];
×
247
                if ( $this->social_is_enabled ) {
×
248
                        $social_fields = WPSEO_Meta::get_meta_field_defs( 'social', $post->post_type );
×
249
                }
250

251
                // The below methods use the global post so make sure it is setup.
252
                \setup_postdata( $post );
×
253
                $meta_boxes = \apply_filters( 'wpseo_save_metaboxes', [] );
×
254
                $meta_boxes = \array_merge(
×
255
                        $meta_boxes,
×
256
                        WPSEO_Meta::get_meta_field_defs( 'general', $post->post_type ),
×
257
                        WPSEO_Meta::get_meta_field_defs( 'advanced', $post->post_type ),
×
258
                        $social_fields,
×
259
                        WPSEO_Meta::get_meta_field_defs( 'schema', $post->post_type )
×
260
                );
261

262
                foreach ( $meta_boxes as $key => $meta_box ) {
×
263
                        // If analysis is disabled remove that analysis score value from the DB.
264
                        if ( $this->is_meta_value_disabled( $key ) ) {
×
265
                                WPSEO_Meta::delete( $key, $post_id );
×
266
                                continue;
×
267
                        }
268

269
                        $data       = null;
×
270
                        $field_name = WPSEO_Meta::$form_prefix . $key;
×
271

272
                        if ( $meta_box['type'] === 'checkbox' ) {
×
273
                                $data = isset( $_POST[ $field_name ] ) ? 'on' : 'off';
×
274
                        }
275
                        else {
276
                                if ( isset( $_POST[ $field_name ] ) ) {
×
277
                                        // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: Sanitized through sanitize_post_meta.
278
                                        $data = \wp_unslash( $_POST[ $field_name ] );
×
279

280
                                        // For multi-select.
281
                                        if ( \is_array( $data ) ) {
×
282
                                                $data = \array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], $data );
×
283
                                        }
284

285
                                        if ( \is_string( $data ) ) {
×
286
                                                $data = ( $key !== 'canonical' ) ? WPSEO_Utils::sanitize_text_field( $data ) : WPSEO_Utils::sanitize_url( $data );
×
287
                                        }
288
                                }
289

290
                                // Reset options when no entry is present with multiselect - only applies to `meta-robots-adv` currently.
291
                                if ( ! isset( $_POST[ $field_name ] ) && ( $meta_box['type'] === 'multiselect' ) ) {
×
292
                                        $data = [];
×
293
                                }
294
                        }
295

296
                        if ( $data !== null ) {
×
297
                                WPSEO_Meta::set_value( $key, $data, $post_id );
×
298
                        }
299
                }
300

301
                if ( isset( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) && \is_string( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) ) {
×
302
                        $slug = \sanitize_title( \wp_unslash( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) );
×
303
                        if ( $post->post_name !== $slug ) {
×
304
                                $post_array              = $post->to_array();
×
305
                                $post_array['post_name'] = $slug;
×
306

307
                                $save_successful = \wp_insert_post( $post_array );
×
308
                                if ( \is_wp_error( $save_successful ) ) {
×
309
                                        \wp_send_json_error( 'Slug not saved', 400 );
×
310
                                }
311

312
                                // Update the post object to ensure we have the actual slug.
313
                                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Updating the post is needed to get the current slug.
314
                                $post = \get_post( $post_id );
×
315
                                if ( ! \is_object( $post ) ) {
×
316
                                        \wp_send_json_error( 'Updated slug not found', 400 );
×
317
                                }
318
                        }
319
                }
320

321
                \do_action( 'wpseo_saved_postdata' );
×
322

323
                // Output the slug, because it is processed by WP and we need the actual slug again.
324
                \wp_send_json_success( [ 'slug' => $post->post_name ] );
×
325
        }
326

327
        /**
328
         * Determines if the given meta value key is disabled.
329
         *
330
         * @param string $key The key of the meta value.
331
         *
332
         * @return bool Whether the given meta value key is disabled.
333
         */
334
        public function is_meta_value_disabled( $key ) {
×
335
                if ( $key === 'linkdex' && ! $this->seo_analysis->is_enabled() ) {
×
336
                        return true;
×
337
                }
338

339
                if ( $key === 'content_score' && ! $this->readability_analysis->is_enabled() ) {
×
340
                        return true;
×
341
                }
342

343
                if ( $key === 'inclusive_language_score' && ! $this->inclusive_language_analysis->is_enabled() ) {
×
344
                        return true;
×
345
                }
346

347
                return false;
×
348
        }
349

350
        /**
351
         * Enqueues all the needed JS and CSS.
352
         *
353
         * @return void
354
         */
355
        public function enqueue() {
×
356
                $post_id = \get_queried_object_id();
×
357
                if ( empty( $post_id ) ) {
×
358
                        $post_id = 0;
×
359
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
360
                        if ( isset( $_GET['post'] ) && \is_string( $_GET['post'] ) ) {
×
361
                                // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized,WordPress.Security.NonceVerification.Recommended -- Reason: No sanitization needed because we cast to an integer,We are not processing form information.
362
                                $post_id = (int) \wp_unslash( $_GET['post'] );
×
363
                        }
364
                }
365

366
                if ( $post_id !== 0 ) {
×
367
                        // Enqueue files needed for upload functionality.
368
                        \wp_enqueue_media( [ 'post' => $post_id ] );
×
369
                }
370

371
                $this->asset_manager->enqueue_style( 'admin-global' );
×
372
                $this->asset_manager->enqueue_style( 'metabox-css' );
×
373
                $this->asset_manager->enqueue_style( 'scoring' );
×
374
                $this->asset_manager->enqueue_style( 'monorepo' );
×
375
                $this->asset_manager->enqueue_style( 'admin-css' );
×
376
                $this->asset_manager->enqueue_style( 'ai-generator' );
×
377
                $this->asset_manager->enqueue_style( 'elementor' );
×
378

379
                $this->asset_manager->enqueue_script( 'admin-global' );
×
380
                $this->asset_manager->enqueue_script( 'elementor' );
×
381

382
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminGlobalL10n', \YoastSEO()->helpers->wincher->get_admin_global_links() );
×
383
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
×
384
                $this->asset_manager->localize_script( 'elementor', 'wpseoFeaturesL10n', WPSEO_Utils::retrieve_enabled_features() );
×
385

386
                $plugins_script_data = [
387
                        'replaceVars' => [
388
                                'replace_vars'             => $this->get_replace_vars(),
×
389
                                'recommended_replace_vars' => $this->get_recommended_replace_vars(),
×
390
                                'hidden_replace_vars'      => $this->get_hidden_replace_vars(),
×
391
                                'scope'                    => $this->determine_scope(),
×
392
                                'has_taxonomies'           => $this->current_post_type_has_taxonomies(),
×
393
                        ],
394
                        'shortcodes'  => [
395
                                'wpseo_shortcode_tags'          => $this->get_valid_shortcode_tags(),
×
396
                                'wpseo_filter_shortcodes_nonce' => \wp_create_nonce( 'wpseo-filter-shortcodes' ),
×
397
                        ],
398
                ];
399

400
                $worker_script_data = [
401
                        'url'                     => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-analysis-worker' ),
×
402
                        'dependencies'            => \YoastSEO()->helpers->asset->get_dependency_urls_by_handle( 'yoast-seo-analysis-worker' ),
×
403
                        'keywords_assessment_url' => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-used-keywords-assessment' ),
×
404
                        'log_level'               => WPSEO_Utils::get_analysis_worker_log_level(),
×
405
                        // We need to make the feature flags separately available inside of the analysis web worker.
406
                        'enabled_features'        => WPSEO_Utils::retrieve_enabled_features(),
×
407
                ];
408

409
                $permalink = $this->get_permalink();
×
410

411
                $script_data = [
412
                        'metabox'                   => $this->get_metabox_script_data( $permalink ),
×
413
                        'isPost'                    => true,
414
                        'isBlockEditor'             => WP_Screen::get()->is_block_editor(),
×
415
                        'isElementorEditor'         => true,
416
                        'postStatus'                => \get_post_status( $post_id ),
×
417
                        'postType'                  => \get_post_type( $post_id ),
×
418
                        'analysis'                  => [
419
                                'plugins' => $plugins_script_data,
×
420
                                'worker'  => $worker_script_data,
×
421
                        ],
422
                        'usedKeywordsNonce'         => \wp_create_nonce( 'wpseo-keyword-usage-and-post-types' ),
×
423
                ];
424

425
                /**
426
                 * The website information repository.
427
                 *
428
                 * @var $repo Website_Information_Repository
429
                 */
430
                $repo             = \YoastSEO()->classes->get( Website_Information_Repository::class );
×
431
                $site_information = $repo->get_post_site_information();
×
432
                $site_information->set_permalink( $permalink );
×
433
                $script_data = \array_merge_recursive( $site_information->get_legacy_site_information(), $script_data );
×
434

435
                $this->asset_manager->localize_script( 'elementor', 'wpseoScriptData', $script_data );
×
436
                $this->asset_manager->enqueue_user_language_script();
×
437
        }
438

439
        /**
440
         * Renders the metabox hidden fields.
441
         *
442
         * @return void
443
         */
444
        protected function render_hidden_fields() {
×
445
                // Wrap in a form with an action and post_id for the submit.
446
                \printf(
×
447
                        '<form id="yoast-form" method="post" action="%1$s"><input type="hidden" name="action" value="wpseo_elementor_save" /><input type="hidden" id="post_ID" name="post_id" value="%2$s" />',
×
448
                        \esc_url( \admin_url( 'admin-ajax.php' ) ),
×
449
                        \esc_attr( $this->get_metabox_post()->ID )
×
450
                );
451

452
                \wp_nonce_field( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
×
453
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
454
                echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'general' );
×
455

456
                if ( $this->is_advanced_metadata_enabled ) {
×
457
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
458
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'advanced' );
×
459
                }
460

461
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
462
                echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'schema', $this->get_metabox_post()->post_type );
×
463

464
                if ( $this->social_is_enabled ) {
×
465
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
466
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'social' );
×
467
                }
468

469
                \printf(
×
470
                        '<input type="hidden" id="%1$s" name="%1$s" value="%2$s" />',
×
471
                        \esc_attr( WPSEO_Meta::$form_prefix . 'slug' ),
×
472
                        \esc_attr( $this->get_post_slug() )
×
473
                );
474

475
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output should be escaped in the filter.
476
                echo \apply_filters( 'wpseo_elementor_hidden_fields', '' );
×
477

478
                echo '</form>';
×
479
        }
480

481
        /**
482
         * Returns the slug for the post being edited.
483
         *
484
         * @return string
485
         */
486
        protected function get_post_slug() {
×
487
                $post = $this->get_metabox_post();
×
488

489
                // In case get_metabox_post returns null for whatever reason.
490
                if ( ! $post instanceof WP_Post ) {
×
491
                        return '';
×
492
                }
493

494
                // Drafts might not have a post_name unless the slug has been manually changed.
495
                // In this case we get it using get_sample_permalink.
496
                if ( ! $post->post_name ) {
×
497
                        $sample = \get_sample_permalink( $post );
×
498

499
                        // Since get_sample_permalink runs through filters, ensure that it has the expected return value.
500
                        if ( \is_array( $sample ) && \count( $sample ) === 2 && \is_string( $sample[1] ) ) {
×
501
                                return $sample[1];
×
502
                        }
503
                }
504

505
                return $post->post_name;
×
506
        }
507

508
        /**
509
         * Returns post in metabox context.
510
         *
511
         * @return WP_Post|null
512
         */
513
        protected function get_metabox_post() {
×
514
                if ( $this->post !== null ) {
×
515
                        return $this->post;
×
516
                }
517

518
                $this->post = $this->request_post->get_post();
×
519

520
                return $this->post;
×
521
        }
522

523
        /**
524
         * Passes variables to js for use with the post-scraper.
525
         *
526
         * @param string $permalink The permalink.
527
         *
528
         * @return array
529
         */
530
        protected function get_metabox_script_data( $permalink ) {
×
531
                $post_formatter = new WPSEO_Metabox_Formatter(
×
532
                        new WPSEO_Post_Metabox_Formatter( $this->get_metabox_post(), [], $permalink )
×
533
                );
534

535
                $values = $post_formatter->get_values();
×
536

537
                /** This filter is documented in admin/filters/class-cornerstone-filter.php. */
538
                $post_types = \apply_filters( 'wpseo_cornerstone_post_types', \YoastSEO()->helpers->post_type->get_accessible_post_types() );
×
539
                if ( $values['cornerstoneActive'] && ! \in_array( $this->get_metabox_post()->post_type, $post_types, true ) ) {
×
540
                        $values['cornerstoneActive'] = false;
×
541
                }
542

543
                $values['elementorMarkerStatus'] = $this->is_highlighting_available() ? 'enabled' : 'hidden';
×
544

545
                return $values;
×
546
        }
547

548
        /**
549
         * Gets the permalink.
550
         *
551
         * @return string
552
         */
553
        protected function get_permalink(): string {
×
554
                $permalink = '';
×
555

556
                if ( \is_object( $this->get_metabox_post() ) ) {
×
557
                        $permalink = \get_sample_permalink( $this->get_metabox_post()->ID );
×
558
                        $permalink = $permalink[0];
×
559
                }
560

561
                return $permalink;
×
562
        }
563

564
        /**
565
         * Checks whether the highlighting functionality is available for Elementor:
566
         * - in Free it's always available (as an upsell).
567
         * - in Premium it's available as long as the version is 21.8-RC0 or above.
568
         *
569
         * @return bool Whether the highlighting functionality is available.
570
         */
571
        private function is_highlighting_available() {
×
572
                $is_premium      = \YoastSEO()->helpers->product->is_premium();
×
573
                $premium_version = \YoastSEO()->helpers->product->get_premium_version();
×
574

575
                return ! $is_premium || \version_compare( $premium_version, '21.8-RC0', '>=' );
×
576
        }
577

578
        /**
579
         * Prepares the replace vars for localization.
580
         *
581
         * @return array Replace vars.
582
         */
583
        protected function get_replace_vars() {
×
584
                $cached_replacement_vars = [];
×
585

586
                $vars_to_cache = [
587
                        'date',
×
588
                        'id',
589
                        'sitename',
590
                        'sitedesc',
591
                        'sep',
592
                        'page',
593
                        'currentyear',
594
                        'currentdate',
595
                        'currentmonth',
596
                        'currentday',
597
                        'tag',
598
                        'category',
599
                        'category_title',
600
                        'primary_category',
601
                        'pt_single',
602
                        'pt_plural',
603
                        'modified',
604
                        'name',
605
                        'user_description',
606
                        'pagetotal',
607
                        'pagenumber',
608
                        'post_year',
609
                        'post_month',
610
                        'post_day',
611
                        'author_first_name',
612
                        'author_last_name',
613
                        'permalink',
614
                        'post_content',
615
                ];
616

617
                foreach ( $vars_to_cache as $var ) {
×
618
                        $cached_replacement_vars[ $var ] = \wpseo_replace_vars( '%%' . $var . '%%', $this->get_metabox_post() );
×
619
                }
620

621
                // Merge custom replace variables with the WordPress ones.
622
                return \array_merge( $cached_replacement_vars, $this->get_custom_replace_vars( $this->get_metabox_post() ) );
×
623
        }
624

625
        /**
626
         * Prepares the recommended replace vars for localization.
627
         *
628
         * @return array Recommended replacement variables.
629
         */
630
        protected function get_recommended_replace_vars() {
×
631
                $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
×
632

633
                // What is recommended depends on the current context.
634
                $post_type = $recommended_replace_vars->determine_for_post( $this->get_metabox_post() );
×
635

636
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
637
        }
638

639
        /**
640
         * Returns the list of replace vars that should be hidden inside the editor.
641
         *
642
         * @return string[] The hidden replace vars.
643
         */
644
        protected function get_hidden_replace_vars() {
×
645
                return ( new WPSEO_Replace_Vars() )->get_hidden_replace_vars();
×
646
        }
647

648
        /**
649
         * Gets the custom replace variables for custom taxonomies and fields.
650
         *
651
         * @param WP_Post $post The post to check for custom taxonomies and fields.
652
         *
653
         * @return array Array containing all the replacement variables.
654
         */
655
        protected function get_custom_replace_vars( $post ) {
×
656
                return [
657
                        'custom_fields'     => $this->get_custom_fields_replace_vars( $post ),
×
658
                        'custom_taxonomies' => $this->get_custom_taxonomies_replace_vars( $post ),
×
659
                ];
660
        }
661

662
        /**
663
         * Gets the custom replace variables for custom taxonomies.
664
         *
665
         * @param WP_Post $post The post to check for custom taxonomies.
666
         *
667
         * @return array Array containing all the replacement variables.
668
         */
669
        protected function get_custom_taxonomies_replace_vars( $post ) {
×
670
                $taxonomies          = \get_object_taxonomies( $post, 'objects' );
×
671
                $custom_replace_vars = [];
×
672

673
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
674

675
                        if ( \is_string( $taxonomy ) ) { // If attachment, see https://core.trac.wordpress.org/ticket/37368 .
×
676
                                $taxonomy_name = $taxonomy;
×
677
                                $taxonomy      = \get_taxonomy( $taxonomy_name );
×
678
                        }
679

680
                        if ( $taxonomy->_builtin && $taxonomy->public ) {
×
681
                                continue;
×
682
                        }
683

684
                        $custom_replace_vars[ $taxonomy_name ] = [
×
685
                                'name'        => $taxonomy->name,
×
686
                                'description' => $taxonomy->description,
×
687
                        ];
688
                }
689

690
                return $custom_replace_vars;
×
691
        }
692

693
        /**
694
         * Gets the custom replace variables for custom fields.
695
         *
696
         * @param WP_Post $post The post to check for custom fields.
697
         *
698
         * @return array Array containing all the replacement variables.
699
         */
700
        protected function get_custom_fields_replace_vars( $post ) {
×
701
                $custom_replace_vars = [];
×
702

703
                // If no post object is passed, return the empty custom_replace_vars array.
704
                if ( ! \is_object( $post ) ) {
×
705
                        return $custom_replace_vars;
×
706
                }
707

708
                $custom_fields = \get_post_custom( $post->ID );
×
709

710
                // Simply concatenate all fields containing replace vars so we can handle them all with a single regex find.
711
                $replace_vars_fields = \implode(
×
712
                        ' ',
×
713
                        [
714
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->title,
×
715
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->meta_description,
×
716
                        ]
717
                );
718

719
                \preg_match_all( '/%%cf_([A-Za-z0-9_]+)%%/', $replace_vars_fields, $matches );
×
720
                $fields_to_include = $matches[1];
×
721
                foreach ( $custom_fields as $custom_field_name => $custom_field ) {
×
722
                        // Skip private custom fields.
723
                        if ( \substr( $custom_field_name, 0, 1 ) === '_' ) {
×
724
                                continue;
×
725
                        }
726

727
                        // Skip custom fields that are not used, new ones will be fetched dynamically.
728
                        if ( ! \in_array( $custom_field_name, $fields_to_include, true ) ) {
×
729
                                continue;
×
730
                        }
731

732
                        // Skip custom field values that are serialized.
733
                        if ( \is_serialized( $custom_field[0] ) ) {
×
734
                                continue;
×
735
                        }
736

737
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
738
                }
739

740
                return $custom_replace_vars;
×
741
        }
742

743
        /**
744
         * Determines the scope based on the post type.
745
         * This can be used by the replacevar plugin to determine if a replacement needs to be executed.
746
         *
747
         * @return string String describing the current scope.
748
         */
749
        protected function determine_scope() {
×
750
                if ( $this->get_metabox_post()->post_type === 'page' ) {
×
751
                        return 'page';
×
752
                }
753

754
                return 'post';
×
755
        }
756

757
        /**
758
         * Determines whether or not the current post type has registered taxonomies.
759
         *
760
         * @return bool Whether the current post type has taxonomies.
761
         */
762
        protected function current_post_type_has_taxonomies() {
×
763
                $post_taxonomies = \get_object_taxonomies( $this->get_metabox_post()->post_type );
×
764

765
                return ! empty( $post_taxonomies );
×
766
        }
767

768
        /**
769
         * Returns an array with shortcode tags for all registered shortcodes.
770
         *
771
         * @return array
772
         */
773
        protected function get_valid_shortcode_tags() {
×
774
                $shortcode_tags = [];
×
775

776
                foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
×
777
                        $shortcode_tags[] = $tag;
×
778
                }
779

780
                return $shortcode_tags;
×
781
        }
782
}
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