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

Yoast / wordpress-seo / 87f6737967a595542f0afd5d24a54da7e9fe274d

04 Sep 2025 06:57AM UTC coverage: 52.799% (-0.001%) from 52.8%
87f6737967a595542f0afd5d24a54da7e9fe274d

push

github

web-flow
Merge pull request #22268 from Yoast/568-add-guard-to-the-loading-scripts-for-features-that-are-disabled-in-free

568 add guard to the loading scripts for features that are disabled in free

8079 of 15188 branches covered (53.19%)

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.

31218 of 59239 relevant lines covered (52.7%)

40305.27 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
                        'isAlwaysIntroductionV2'    => $this->is_elementor_version_compatible_with_introduction_v2(),
×
438
                        'postStatus'                => \get_post_status( $post_id ),
×
439
                        'postType'                  => \get_post_type( $post_id ),
×
440
                        'analysis'                  => [
×
441
                                'plugins' => $plugins_script_data,
×
442
                                'worker'  => $worker_script_data,
×
443
                        ],
×
444
                        'usedKeywordsNonce'         => \wp_create_nonce( 'wpseo-keyword-usage-and-post-types' ),
×
445
                        'isFrontPage'               => $is_front_page,
×
446
                ];
×
447

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

458
                $this->asset_manager->localize_script( 'elementor', 'wpseoScriptData', $script_data );
×
NEW
459
                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
460
                        $this->asset_manager->enqueue_user_language_script();
×
461
                }
462
        }
463

464
        /**
465
         * Checks whether the current Elementor version is compatible with our introduction v2.
466
         *
467
         * In version 3.30.0, Elementor removed the experimental flag for the editor v2.
468
         * Resulting in the editor v2 being the default.
469
         *
470
         * @return bool Whether the Elementor version is compatible with introduction v2.
471
         */
472
        private function is_elementor_version_compatible_with_introduction_v2(): bool {
×
473
                if ( ! \defined( 'ELEMENTOR_VERSION' ) ) {
×
474
                        return false;
×
475
                }
476

477
                // Take the semver version from their version string.
478
                $matches = [];
×
479
                $version = ( \preg_match( '/^([0-9]+.[0-9]+.[0-9]+)/', \ELEMENTOR_VERSION, $matches ) > 0 ) ? $matches[1] : \ELEMENTOR_VERSION;
×
480

481
                // Check if the version is 3.30.0 or higher. This is where the editor v2 was taken out of the experimental into the default state.
482
                return \version_compare( $version, '3.30.0', '>=' );
×
483
        }
484

485
        /**
486
         * Renders the metabox hidden fields.
487
         *
488
         * @return void
489
         */
490
        protected function render_hidden_fields() {
×
491
                // Wrap in a form with an action and post_id for the submit.
492
                \printf(
×
493
                        '<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" />',
×
494
                        \esc_url( \admin_url( 'admin-ajax.php' ) ),
×
495
                        \esc_attr( $this->get_metabox_post()->ID )
×
496
                );
×
497

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

502
                if ( $this->is_advanced_metadata_enabled ) {
×
503
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
504
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'advanced' );
×
505
                }
506

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

510
                if ( $this->social_is_enabled ) {
×
511
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
512
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'social' );
×
513
                }
514

515
                \printf(
×
516
                        '<input type="hidden" id="%1$s" name="%1$s" value="%2$s" />',
×
517
                        \esc_attr( WPSEO_Meta::$form_prefix . 'slug' ),
×
518
                        /**
519
                         * It is important that this slug value is the same as in the database.
520
                         * If the DB value is empty we can auto-generate a slug.
521
                         * But if not empty, we should not touch it anymore.
522
                         */
523
                        \esc_attr( $this->get_metabox_post()->post_name )
×
524
                );
×
525

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

529
                echo '</form>';
×
530
        }
531

532
        /**
533
         * Returns post in metabox context.
534
         *
535
         * @return WP_Post|null
536
         */
537
        protected function get_metabox_post() {
×
538
                if ( $this->post !== null ) {
×
539
                        return $this->post;
×
540
                }
541

542
                $this->post = $this->request_post->get_post();
×
543

544
                return $this->post;
×
545
        }
546

547
        /**
548
         * Passes variables to js for use with the post-scraper.
549
         *
550
         * @param string $permalink The permalink.
551
         *
552
         * @return array
553
         */
554
        protected function get_metabox_script_data( $permalink ) {
×
555
                $post_formatter = new WPSEO_Metabox_Formatter(
×
556
                        new WPSEO_Post_Metabox_Formatter( $this->get_metabox_post(), [], $permalink )
×
557
                );
×
558

559
                $values = $post_formatter->get_values();
×
560

561
                /** This filter is documented in admin/filters/class-cornerstone-filter.php. */
562
                $post_types = \apply_filters( 'wpseo_cornerstone_post_types', \YoastSEO()->helpers->post_type->get_accessible_post_types() );
×
563
                if ( $values['cornerstoneActive'] && ! \in_array( $this->get_metabox_post()->post_type, $post_types, true ) ) {
×
564
                        $values['cornerstoneActive'] = false;
×
565
                }
566

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

569
                return $values;
×
570
        }
571

572
        /**
573
         * Gets the permalink.
574
         *
575
         * @return string
576
         */
577
        protected function get_permalink(): string {
×
578
                $permalink = '';
×
579

580
                if ( \is_object( $this->get_metabox_post() ) ) {
×
581
                        $permalink = \get_sample_permalink( $this->get_metabox_post()->ID );
×
582
                        $permalink = $permalink[0];
×
583
                }
584

585
                return $permalink;
×
586
        }
587

588
        /**
589
         * Checks whether the highlighting functionality is available for Elementor:
590
         * - in Free it's always available (as an upsell).
591
         * - in Premium it's available as long as the version is 21.8-RC0 or above.
592
         *
593
         * @return bool Whether the highlighting functionality is available.
594
         */
595
        private function is_highlighting_available() {
×
596
                $is_premium      = \YoastSEO()->helpers->product->is_premium();
×
597
                $premium_version = \YoastSEO()->helpers->product->get_premium_version();
×
598

599
                return ! $is_premium || \version_compare( $premium_version, '21.8-RC0', '>=' );
×
600
        }
601

602
        /**
603
         * Prepares the replace vars for localization.
604
         *
605
         * @return array Replace vars.
606
         */
607
        protected function get_replace_vars() {
×
608
                $cached_replacement_vars = [];
×
609

610
                $vars_to_cache = [
×
611
                        'date',
×
612
                        'id',
×
613
                        'sitename',
×
614
                        'sitedesc',
×
615
                        'sep',
×
616
                        'page',
×
617
                        'currentyear',
×
618
                        'currentdate',
×
619
                        'currentmonth',
×
620
                        'currentday',
×
621
                        'tag',
×
622
                        'category',
×
623
                        'category_title',
×
624
                        'primary_category',
×
625
                        'pt_single',
×
626
                        'pt_plural',
×
627
                        'modified',
×
628
                        'name',
×
629
                        'user_description',
×
630
                        'pagetotal',
×
631
                        'pagenumber',
×
632
                        'post_year',
×
633
                        'post_month',
×
634
                        'post_day',
×
635
                        'author_first_name',
×
636
                        'author_last_name',
×
637
                        'permalink',
×
638
                        'post_content',
×
639
                ];
×
640

641
                foreach ( $vars_to_cache as $var ) {
×
642
                        $cached_replacement_vars[ $var ] = \wpseo_replace_vars( '%%' . $var . '%%', $this->get_metabox_post() );
×
643
                }
644

645
                // Merge custom replace variables with the WordPress ones.
646
                return \array_merge( $cached_replacement_vars, $this->get_custom_replace_vars( $this->get_metabox_post() ) );
×
647
        }
648

649
        /**
650
         * Prepares the recommended replace vars for localization.
651
         *
652
         * @return array Recommended replacement variables.
653
         */
654
        protected function get_recommended_replace_vars() {
×
655
                $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
×
656

657
                // What is recommended depends on the current context.
658
                $post_type = $recommended_replace_vars->determine_for_post( $this->get_metabox_post() );
×
659

660
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
661
        }
662

663
        /**
664
         * Returns the list of replace vars that should be hidden inside the editor.
665
         *
666
         * @return string[] The hidden replace vars.
667
         */
668
        protected function get_hidden_replace_vars() {
×
669
                return ( new WPSEO_Replace_Vars() )->get_hidden_replace_vars();
×
670
        }
671

672
        /**
673
         * Gets the custom replace variables for custom taxonomies and fields.
674
         *
675
         * @param WP_Post $post The post to check for custom taxonomies and fields.
676
         *
677
         * @return array Array containing all the replacement variables.
678
         */
679
        protected function get_custom_replace_vars( $post ) {
×
680
                return [
×
681
                        'custom_fields'     => $this->get_custom_fields_replace_vars( $post ),
×
682
                        'custom_taxonomies' => $this->get_custom_taxonomies_replace_vars( $post ),
×
683
                ];
×
684
        }
685

686
        /**
687
         * Gets the custom replace variables for custom taxonomies.
688
         *
689
         * @param WP_Post $post The post to check for custom taxonomies.
690
         *
691
         * @return array Array containing all the replacement variables.
692
         */
693
        protected function get_custom_taxonomies_replace_vars( $post ) {
×
694
                $taxonomies          = \get_object_taxonomies( $post, 'objects' );
×
695
                $custom_replace_vars = [];
×
696

697
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
698

699
                        if ( \is_string( $taxonomy ) ) { // If attachment, see https://core.trac.wordpress.org/ticket/37368 .
×
700
                                $taxonomy_name = $taxonomy;
×
701
                                $taxonomy      = \get_taxonomy( $taxonomy_name );
×
702
                        }
703

704
                        if ( $taxonomy->_builtin && $taxonomy->public ) {
×
705
                                continue;
×
706
                        }
707

708
                        $custom_replace_vars[ $taxonomy_name ] = [
×
709
                                'name'        => $taxonomy->name,
×
710
                                'description' => $taxonomy->description,
×
711
                        ];
×
712
                }
713

714
                return $custom_replace_vars;
×
715
        }
716

717
        /**
718
         * Gets the custom replace variables for custom fields.
719
         *
720
         * @param WP_Post $post The post to check for custom fields.
721
         *
722
         * @return array Array containing all the replacement variables.
723
         */
724
        protected function get_custom_fields_replace_vars( $post ) {
×
725
                $custom_replace_vars = [];
×
726

727
                // If no post object is passed, return the empty custom_replace_vars array.
728
                if ( ! \is_object( $post ) ) {
×
729
                        return $custom_replace_vars;
×
730
                }
731

732
                $custom_fields = \get_post_custom( $post->ID );
×
733

734
                // Simply concatenate all fields containing replace vars so we can handle them all with a single regex find.
735
                $replace_vars_fields = \implode(
×
736
                        ' ',
×
737
                        [
×
738
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->title,
×
739
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->meta_description,
×
740
                        ]
×
741
                );
×
742

743
                \preg_match_all( '/%%cf_([A-Za-z0-9_]+)%%/', $replace_vars_fields, $matches );
×
744
                $fields_to_include = $matches[1];
×
745
                foreach ( $custom_fields as $custom_field_name => $custom_field ) {
×
746
                        // Skip private custom fields.
747
                        if ( \substr( $custom_field_name, 0, 1 ) === '_' ) {
×
748
                                continue;
×
749
                        }
750

751
                        // Skip custom fields that are not used, new ones will be fetched dynamically.
752
                        if ( ! \in_array( $custom_field_name, $fields_to_include, true ) ) {
×
753
                                continue;
×
754
                        }
755

756
                        // Skip custom field values that are serialized.
757
                        if ( \is_serialized( $custom_field[0] ) ) {
×
758
                                continue;
×
759
                        }
760

761
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
762
                }
763

764
                return $custom_replace_vars;
×
765
        }
766

767
        /**
768
         * Determines the scope based on the post type.
769
         * This can be used by the replacevar plugin to determine if a replacement needs to be executed.
770
         *
771
         * @return string String describing the current scope.
772
         */
773
        protected function determine_scope() {
×
774
                if ( $this->get_metabox_post()->post_type === 'page' ) {
×
775
                        return 'page';
×
776
                }
777

778
                return 'post';
×
779
        }
780

781
        /**
782
         * Determines whether or not the current post type has registered taxonomies.
783
         *
784
         * @return bool Whether the current post type has taxonomies.
785
         */
786
        protected function current_post_type_has_taxonomies() {
×
787
                $post_taxonomies = \get_object_taxonomies( $this->get_metabox_post()->post_type );
×
788

789
                return ! empty( $post_taxonomies );
×
790
        }
791

792
        /**
793
         * Returns an array with shortcode tags for all registered shortcodes.
794
         *
795
         * @return array
796
         */
797
        protected function get_valid_shortcode_tags() {
×
798
                $shortcode_tags = [];
×
799

800
                foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
×
801
                        $shortcode_tags[] = $tag;
×
802
                }
803

804
                return $shortcode_tags;
×
805
        }
806
}
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