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

Yoast / wordpress-seo / 8f7cceb1fa3474419825a75c6458e2dd4fccca80

15 May 2025 09:19AM UTC coverage: 52.537% (-0.5%) from 53.034%
8f7cceb1fa3474419825a75c6458e2dd4fccca80

Pull #22268

github

web-flow
Merge d395477cd into 88257803b
Pull Request #22268: 568 add guard to the loading scripts for features that are disabled in free

7875 of 13897 branches covered (56.67%)

Branch coverage included in aggregate %.

7 of 21 new or added lines in 3 files covered. (33.33%)

1 existing line in 1 file now uncovered.

29117 of 56514 relevant lines covered (51.52%)

42245.73 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
         * Whether the insights feature is enabled.
110
         *
111
         * @var bool
112
         */
113
        protected $is_insights_enabled;
114

115
        /**
116
         * Whether the cornerstone content feature is enabled.
117
         *
118
         * @var bool
119
         */
120
        protected $is_cornerstone_enabled;
121

122
        /**
123
         * Returns the conditionals based in which this loadable should be active.
124
         *
125
         * @return array
126
         */
127
        public static function get_conditionals() {
×
128
                return [ Elementor_Edit_Conditional::class ];
×
129
        }
130

131
        /**
132
         * Constructor.
133
         *
134
         * @param WPSEO_Admin_Asset_Manager $asset_manager The asset manager.
135
         * @param Options_Helper            $options       The options helper.
136
         * @param Capability_Helper         $capability    The capability helper.
137
         * @param Request_Post              $request_post  The Request_Post.
138
         */
139
        public function __construct(
×
140
                WPSEO_Admin_Asset_Manager $asset_manager,
141
                Options_Helper $options,
142
                Capability_Helper $capability,
143
                Request_Post $request_post
144
        ) {
145
                $this->asset_manager = $asset_manager;
×
146
                $this->options       = $options;
×
147
                $this->capability    = $capability;
×
148
                $this->request_post  = $request_post;
×
149

150
                $this->seo_analysis                 = new WPSEO_Metabox_Analysis_SEO();
×
151
                $this->readability_analysis         = new WPSEO_Metabox_Analysis_Readability();
×
152
                $this->inclusive_language_analysis  = new WPSEO_Metabox_Analysis_Inclusive_Language();
×
153
                $this->social_is_enabled            = $this->options->get( 'opengraph', false ) || $this->options->get( 'twitter', false );
×
154
                $this->is_advanced_metadata_enabled = $this->capability->current_user_can( 'wpseo_edit_advanced_metadata' ) || $this->options->get( 'disableadvanced_meta' ) === false;
×
NEW
155
                $this->is_insights_enabled          = $this->options->get( 'enable_metabox_insights', false );
×
NEW
156
                $this->is_cornerstone_enabled       = $this->options->get( 'enable_cornerstone_content', false );
×
157
        }
158

159
        /**
160
         * Initializes the integration.
161
         *
162
         * This is the place to register hooks and filters.
163
         *
164
         * @return void
165
         */
166
        public function register_hooks() {
×
167
                \add_action( 'wp_ajax_wpseo_elementor_save', [ $this, 'save_postdata' ] );
×
168

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

173
        /**
174
         * Registers our Elementor hooks.
175
         * This is done for pages with metabox on page load and not on ajax request.
176
         *
177
         * @return void
178
         */
179
        public function register_elementor_hooks() {
×
180
                if ( $this->get_metabox_post() === null || ! $this->display_metabox( $this->get_metabox_post()->post_type ) ) {
×
181
                        return;
×
182
                }
183

184
                \add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'init' ] );
×
185
        }
186

187
        /**
188
         * Initializes the integration.
189
         *
190
         * @return void
191
         */
192
        public function init() {
×
193
                $this->asset_manager->register_assets();
×
194
                $this->enqueue();
×
195
                $this->render_hidden_fields();
×
196
        }
197

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

200
        /**
201
         * Determines whether the metabox should be shown for the passed identifier.
202
         *
203
         * By default, the check is done for post types, but can also be used for taxonomies.
204
         *
205
         * @param string|null $identifier The identifier to check.
206
         * @param string      $type       The type of object to check. Defaults to post_type.
207
         *
208
         * @return bool Whether the metabox should be displayed.
209
         */
210
        public function display_metabox( $identifier = null, $type = 'post_type' ) {
×
211
                return WPSEO_Utils::is_metabox_active( $identifier, $type );
×
212
        }
213

214
        /**
215
         * Saves the WP SEO metadata for posts.
216
         *
217
         * Outputs JSON via wp_send_json then stops code execution.
218
         *
219
         * {@internal $_POST parameters are validated via sanitize_post_meta().}}
220
         *
221
         * @return void
222
         */
223
        public function save_postdata() {
×
224
                global $post;
×
225

226
                if ( ! isset( $_POST['post_id'] ) || ! \is_string( $_POST['post_id'] ) ) {
×
227
                        \wp_send_json_error( 'Bad Request', 400 );
×
228
                }
229

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

233
                if ( $post_id <= 0 ) {
×
234
                        \wp_send_json_error( 'Bad Request', 400 );
×
235
                }
236

237
                if ( ! \current_user_can( 'edit_post', $post_id ) ) {
×
238
                        \wp_send_json_error( 'Forbidden', 403 );
×
239
                }
240

241
                \check_ajax_referer( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
×
242

243
                // Bail if this is a multisite installation and the site has been switched.
244
                if ( \is_multisite() && \ms_is_switched() ) {
×
245
                        \wp_send_json_error( 'Switched multisite', 409 );
×
246
                }
247

248
                \clean_post_cache( $post_id );
×
249
                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- To setup the post we need to do this explicitly.
250
                $post = \get_post( $post_id );
×
251

252
                if ( ! \is_object( $post ) ) {
×
253
                        // Non-existent post.
254
                        \wp_send_json_error( 'Post not found', 400 );
×
255
                }
256

257
                \do_action( 'wpseo_save_compare_data', $post );
×
258

259
                // Initialize meta, amongst other things it registers sanitization.
260
                WPSEO_Meta::init();
×
261

262
                $social_fields = [];
×
263
                if ( $this->social_is_enabled ) {
×
264
                        $social_fields = WPSEO_Meta::get_meta_field_defs( 'social', $post->post_type );
×
265
                }
266

267
                // The below methods use the global post so make sure it is setup.
268
                \setup_postdata( $post );
×
269
                $meta_boxes = \apply_filters( 'wpseo_save_metaboxes', [] );
×
270
                $meta_boxes = \array_merge(
×
271
                        $meta_boxes,
×
272
                        WPSEO_Meta::get_meta_field_defs( 'general', $post->post_type ),
×
273
                        WPSEO_Meta::get_meta_field_defs( 'advanced', $post->post_type ),
×
274
                        $social_fields,
×
275
                        WPSEO_Meta::get_meta_field_defs( 'schema', $post->post_type )
×
276
                );
×
277

278
                foreach ( $meta_boxes as $key => $meta_box ) {
×
279
                        // If analysis is disabled remove that analysis score value from the DB.
280
                        if ( $this->is_meta_value_disabled( $key ) ) {
×
281
                                WPSEO_Meta::delete( $key, $post_id );
×
282
                                continue;
×
283
                        }
284

285
                        $data       = null;
×
286
                        $field_name = WPSEO_Meta::$form_prefix . $key;
×
287

288
                        if ( $meta_box['type'] === 'checkbox' ) {
×
289
                                $data = isset( $_POST[ $field_name ] ) ? 'on' : 'off';
×
290
                        }
291
                        else {
292
                                if ( isset( $_POST[ $field_name ] ) ) {
×
293
                                        // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: Sanitized through sanitize_post_meta.
294
                                        $data = \wp_unslash( $_POST[ $field_name ] );
×
295

296
                                        // For multi-select.
297
                                        if ( \is_array( $data ) ) {
×
298
                                                $data = \array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], $data );
×
299
                                        }
300

301
                                        if ( \is_string( $data ) ) {
×
302
                                                $data = ( $key !== 'canonical' ) ? WPSEO_Utils::sanitize_text_field( $data ) : WPSEO_Utils::sanitize_url( $data );
×
303
                                        }
304
                                }
305

306
                                // Reset options when no entry is present with multiselect - only applies to `meta-robots-adv` currently.
307
                                if ( ! isset( $_POST[ $field_name ] ) && ( $meta_box['type'] === 'multiselect' ) ) {
×
308
                                        $data = [];
×
309
                                }
310
                        }
311

312
                        if ( $data !== null ) {
×
313
                                WPSEO_Meta::set_value( $key, $data, $post_id );
×
314
                        }
315
                }
316

317
                if ( isset( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) && \is_string( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) ) {
×
318
                        $slug = \sanitize_title( \wp_unslash( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) );
×
319
                        if ( $post->post_name !== $slug ) {
×
320
                                $post_array              = $post->to_array();
×
321
                                $post_array['post_name'] = $slug;
×
322

323
                                $save_successful = \wp_insert_post( $post_array );
×
324
                                if ( \is_wp_error( $save_successful ) ) {
×
325
                                        \wp_send_json_error( 'Slug not saved', 400 );
×
326
                                }
327

328
                                // Update the post object to ensure we have the actual slug.
329
                                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Updating the post is needed to get the current slug.
330
                                $post = \get_post( $post_id );
×
331
                                if ( ! \is_object( $post ) ) {
×
332
                                        \wp_send_json_error( 'Updated slug not found', 400 );
×
333
                                }
334
                        }
335
                }
336

337
                \do_action( 'wpseo_saved_postdata' );
×
338

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

343
        /**
344
         * Determines if the given meta value key is disabled.
345
         *
346
         * @param string $key The key of the meta value.
347
         *
348
         * @return bool Whether the given meta value key is disabled.
349
         */
350
        public function is_meta_value_disabled( $key ) {
×
351
                if ( $key === 'linkdex' && ! $this->seo_analysis->is_enabled() ) {
×
352
                        return true;
×
353
                }
354

355
                if ( $key === 'content_score' && ! $this->readability_analysis->is_enabled() ) {
×
356
                        return true;
×
357
                }
358

359
                if ( $key === 'inclusive_language_score' && ! $this->inclusive_language_analysis->is_enabled() ) {
×
360
                        return true;
×
361
                }
362

363
                return false;
×
364
        }
365

366
        /**
367
         * Enqueues all the needed JS and CSS.
368
         *
369
         * @return void
370
         */
371
        public function enqueue() {
×
372
                $post_id = \get_queried_object_id();
×
373
                if ( empty( $post_id ) ) {
×
374
                        $post_id = 0;
×
375
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
376
                        if ( isset( $_GET['post'] ) && \is_string( $_GET['post'] ) ) {
×
377
                                // 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.
378
                                $post_id = (int) \wp_unslash( $_GET['post'] );
×
379
                        }
380
                }
381

382
                if ( $post_id !== 0 ) {
×
383
                        // Enqueue files needed for upload functionality.
384
                        \wp_enqueue_media( [ 'post' => $post_id ] );
×
385
                }
386

387
                $this->asset_manager->enqueue_style( 'admin-global' );
×
388
                $this->asset_manager->enqueue_style( 'metabox-css' );
×
NEW
389
                if ( $this->readability_analysis->is_enabled() ) {
×
NEW
390
                        $this->asset_manager->enqueue_style( 'scoring' );
×
391
                }
392
                $this->asset_manager->enqueue_style( 'monorepo' );
×
393
                $this->asset_manager->enqueue_style( 'admin-css' );
×
394
                $this->asset_manager->enqueue_style( 'ai-generator' );
×
395
                $this->asset_manager->enqueue_style( 'elementor' );
×
396

397
                $this->asset_manager->enqueue_script( 'admin-global' );
×
398
                $this->asset_manager->enqueue_script( 'elementor' );
×
399

400
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminGlobalL10n', \YoastSEO()->helpers->wincher->get_admin_global_links() );
×
401
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
×
402
                $this->asset_manager->localize_script( 'elementor', 'wpseoFeaturesL10n', WPSEO_Utils::retrieve_enabled_features() );
×
403

404
                $plugins_script_data = [
×
405
                        'replaceVars' => [
×
406
                                'replace_vars'             => $this->get_replace_vars(),
×
407
                                'recommended_replace_vars' => $this->get_recommended_replace_vars(),
×
408
                                'hidden_replace_vars'      => $this->get_hidden_replace_vars(),
×
409
                                'scope'                    => $this->determine_scope(),
×
410
                                'has_taxonomies'           => $this->current_post_type_has_taxonomies(),
×
411
                        ],
×
412
                        'shortcodes'  => [
×
413
                                'wpseo_shortcode_tags'          => $this->get_valid_shortcode_tags(),
×
414
                                'wpseo_filter_shortcodes_nonce' => \wp_create_nonce( 'wpseo-filter-shortcodes' ),
×
415
                        ],
×
416
                ];
×
417

418
                $worker_script_data = [
×
419
                        'url'                     => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-analysis-worker' ),
×
420
                        'dependencies'            => \YoastSEO()->helpers->asset->get_dependency_urls_by_handle( 'yoast-seo-analysis-worker' ),
×
421
                        'keywords_assessment_url' => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-used-keywords-assessment' ),
×
422
                        'log_level'               => WPSEO_Utils::get_analysis_worker_log_level(),
×
423
                        // We need to make the feature flags separately available inside of the analysis web worker.
424
                        'enabled_features'        => WPSEO_Utils::retrieve_enabled_features(),
×
425
                ];
×
426

427
                $permalink        = $this->get_permalink();
×
428
                $page_on_front    = (int) \get_option( 'page_on_front' );
×
429
                $homepage_is_page = \get_option( 'show_on_front' ) === 'page';
×
430
                $is_front_page    = $homepage_is_page && $page_on_front === $post_id;
×
431

432
                $script_data = [
×
433
                        'metabox'                   => $this->get_metabox_script_data( $permalink ),
×
434
                        'isPost'                    => true,
×
435
                        'isBlockEditor'             => WP_Screen::get()->is_block_editor(),
×
436
                        'isElementorEditor'         => true,
×
437
                        'postStatus'                => \get_post_status( $post_id ),
×
438
                        'postType'                  => \get_post_type( $post_id ),
×
439
                        'analysis'                  => [
×
440
                                'plugins' => $plugins_script_data,
×
441
                                'worker'  => $worker_script_data,
×
442
                        ],
×
443
                        'usedKeywordsNonce'         => \wp_create_nonce( 'wpseo-keyword-usage-and-post-types' ),
×
444
                        'isFrontPage'               => $is_front_page,
×
445
                ];
×
446

447
                /**
448
                 * The website information repository.
449
                 *
450
                 * @var Website_Information_Repository $repo
451
                 */
452
                $repo             = \YoastSEO()->classes->get( Website_Information_Repository::class );
×
453
                $site_information = $repo->get_post_site_information();
×
454
                $site_information->set_permalink( $permalink );
×
455
                $script_data = \array_merge_recursive( $site_information->get_legacy_site_information(), $script_data );
×
456

457
                $this->asset_manager->localize_script( 'elementor', 'wpseoScriptData', $script_data );
×
NEW
458
                if ( $this->readability_analysis->is_enabled() || $this->inclusive_language_analysis->is_enabled() || $this->seo_analysis->is_enabled() || $this->is_insights_enabled || $this->is_cornerstone_enabled ) {
×
NEW
459
                        $this->asset_manager->enqueue_user_language_script();
×
460
                }
461
        }
462

463
        /**
464
         * Renders the metabox hidden fields.
465
         *
466
         * @return void
467
         */
468
        protected function render_hidden_fields() {
×
469
                // Wrap in a form with an action and post_id for the submit.
470
                \printf(
×
471
                        '<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" />',
×
472
                        \esc_url( \admin_url( 'admin-ajax.php' ) ),
×
473
                        \esc_attr( $this->get_metabox_post()->ID )
×
474
                );
×
475

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

480
                if ( $this->is_advanced_metadata_enabled ) {
×
481
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
482
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'advanced' );
×
483
                }
484

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

488
                if ( $this->social_is_enabled ) {
×
489
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
490
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'social' );
×
491
                }
492

493
                \printf(
×
494
                        '<input type="hidden" id="%1$s" name="%1$s" value="%2$s" />',
×
495
                        \esc_attr( WPSEO_Meta::$form_prefix . 'slug' ),
×
496
                        /**
497
                         * It is important that this slug value is the same as in the database.
498
                         * If the DB value is empty we can auto-generate a slug.
499
                         * But if not empty, we should not touch it anymore.
500
                         */
501
                        \esc_attr( $this->get_metabox_post()->post_name )
×
502
                );
×
503

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

507
                echo '</form>';
×
508
        }
509

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

520
                $this->post = $this->request_post->get_post();
×
521

522
                return $this->post;
×
523
        }
524

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

537
                $values = $post_formatter->get_values();
×
538

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

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

547
                return $values;
×
548
        }
549

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

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

563
                return $permalink;
×
564
        }
565

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

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

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

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

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

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

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

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

638
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
639
        }
640

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

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

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

675
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
676

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

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

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

692
                return $custom_replace_vars;
×
693
        }
694

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

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

710
                $custom_fields = \get_post_custom( $post->ID );
×
711

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

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

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

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

739
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
740
                }
741

742
                return $custom_replace_vars;
×
743
        }
744

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

756
                return 'post';
×
757
        }
758

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

767
                return ! empty( $post_taxonomies );
×
768
        }
769

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

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

782
                return $shortcode_tags;
×
783
        }
784
}
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