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

Yoast / wordpress-seo / 371e08baa29b2622088a3881f6df65320efa00b5

13 Nov 2025 09:45AM UTC coverage: 53.274% (-0.1%) from 53.388%
371e08baa29b2622088a3881f6df65320efa00b5

Pull #22697

github

web-flow
Merge 98b3f65a1 into a27514f8e
Pull Request #22697: Show yoast in elements tab in elementor

8502 of 15767 branches covered (53.92%)

Branch coverage included in aggregate %.

0 of 91 new or added lines in 6 files covered. (0.0%)

95 existing lines in 9 files now uncovered.

32208 of 60649 relevant lines covered (53.11%)

39377.33 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' ] );
×
NEW
169
                \add_action( 'elementor/editor/footer', [ $this, 'start_capturing' ], 0 );
×
NEW
170
                \add_action( 'elementor/editor/footer', [ $this, 'end_capturing' ], 999 );
×
171
        }
172

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

184
        /**
185
         * Start capturing buffer.
186
         *
187
         * @return void
188
         */
NEW
189
        public function start_capturing() {
×
NEW
190
                \ob_start();
×
191
        }
192

193
        /**
194
         * End capturing buffer and add the Yoast SEO button inside the Elements panel.
195
         *
196
         * @return void
197
         */
NEW
198
        public function end_capturing() {
×
NEW
199
                $output  = \ob_get_clean();
×
NEW
200
                $search  = '/(<(div|button) class="elementor-component-tab elementor-panel-navigation-tab" data-tab="global">.*<\/(div|button)>)/m';
×
NEW
201
                $replace = '${1}<${2} class="elementor-component-tab elementor-panel-navigation-tab" data-tab="yoast-seo-tab">Yoast SEO</${2}>';
×
202
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This comes from the output buffer, escaping it would break the output.
NEW
203
                echo \preg_replace(
×
NEW
204
                        $search,
×
NEW
205
                        $replace,
×
NEW
206
                        $output
×
NEW
207
                );
×
208
        }
209

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

212
        /**
213
         * Determines whether the metabox should be shown for the passed identifier.
214
         *
215
         * By default, the check is done for post types, but can also be used for taxonomies.
216
         *
217
         * @param string|null $identifier The identifier to check.
218
         * @param string      $type       The type of object to check. Defaults to post_type.
219
         *
220
         * @return bool Whether the metabox should be displayed.
221
         */
222
        public function display_metabox( $identifier = null, $type = 'post_type' ) {
×
223
                return WPSEO_Utils::is_metabox_active( $identifier, $type );
×
224
        }
225

226
        /**
227
         * Saves the WP SEO metadata for posts.
228
         *
229
         * Outputs JSON via wp_send_json then stops code execution.
230
         *
231
         * {@internal $_POST parameters are validated via sanitize_post_meta().}}
232
         *
233
         * @return void
234
         */
235
        public function save_postdata() {
×
236
                global $post;
×
237

238
                if ( ! isset( $_POST['post_id'] ) || ! \is_string( $_POST['post_id'] ) ) {
×
239
                        \wp_send_json_error( 'Bad Request', 400 );
×
240
                }
241

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

245
                if ( $post_id <= 0 ) {
×
246
                        \wp_send_json_error( 'Bad Request', 400 );
×
247
                }
248

249
                if ( ! \current_user_can( 'edit_post', $post_id ) ) {
×
250
                        \wp_send_json_error( 'Forbidden', 403 );
×
251
                }
252

253
                \check_ajax_referer( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
×
254

255
                // Bail if this is a multisite installation and the site has been switched.
256
                if ( \is_multisite() && \ms_is_switched() ) {
×
257
                        \wp_send_json_error( 'Switched multisite', 409 );
×
258
                }
259

260
                \clean_post_cache( $post_id );
×
261
                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- To setup the post we need to do this explicitly.
262
                $post = \get_post( $post_id );
×
263

264
                if ( ! \is_object( $post ) ) {
×
265
                        // Non-existent post.
266
                        \wp_send_json_error( 'Post not found', 400 );
×
267
                }
268

269
                \do_action( 'wpseo_save_compare_data', $post );
×
270

271
                // Initialize meta, amongst other things it registers sanitization.
272
                WPSEO_Meta::init();
×
273

274
                $social_fields = [];
×
275
                if ( $this->social_is_enabled ) {
×
276
                        $social_fields = WPSEO_Meta::get_meta_field_defs( 'social', $post->post_type );
×
277
                }
278

279
                // The below methods use the global post so make sure it is setup.
280
                \setup_postdata( $post );
×
281
                $meta_boxes = \apply_filters( 'wpseo_save_metaboxes', [] );
×
282
                $meta_boxes = \array_merge(
×
283
                        $meta_boxes,
×
284
                        WPSEO_Meta::get_meta_field_defs( 'general', $post->post_type ),
×
285
                        WPSEO_Meta::get_meta_field_defs( 'advanced', $post->post_type ),
×
286
                        $social_fields,
×
287
                        WPSEO_Meta::get_meta_field_defs( 'schema', $post->post_type )
×
288
                );
×
289

290
                foreach ( $meta_boxes as $key => $meta_box ) {
×
291
                        // If analysis is disabled remove that analysis score value from the DB.
292
                        if ( $this->is_meta_value_disabled( $key ) ) {
×
293
                                WPSEO_Meta::delete( $key, $post_id );
×
294
                                continue;
×
295
                        }
296

297
                        $data       = null;
×
298
                        $field_name = WPSEO_Meta::$form_prefix . $key;
×
299

300
                        if ( $meta_box['type'] === 'checkbox' ) {
×
301
                                $data = isset( $_POST[ $field_name ] ) ? 'on' : 'off';
×
302
                        }
303
                        else {
304
                                if ( isset( $_POST[ $field_name ] ) ) {
×
305
                                        // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: Sanitized through sanitize_post_meta.
306
                                        $data = \wp_unslash( $_POST[ $field_name ] );
×
307

308
                                        // For multi-select.
309
                                        if ( \is_array( $data ) ) {
×
310
                                                $data = \array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], $data );
×
311
                                        }
312

313
                                        if ( \is_string( $data ) ) {
×
314
                                                $data = ( $key !== 'canonical' ) ? WPSEO_Utils::sanitize_text_field( $data ) : WPSEO_Utils::sanitize_url( $data );
×
315
                                        }
316
                                }
317

318
                                // Reset options when no entry is present with multiselect - only applies to `meta-robots-adv` currently.
319
                                if ( ! isset( $_POST[ $field_name ] ) && ( $meta_box['type'] === 'multiselect' ) ) {
×
320
                                        $data = [];
×
321
                                }
322
                        }
323

324
                        if ( $data !== null ) {
×
325
                                WPSEO_Meta::set_value( $key, $data, $post_id );
×
326
                        }
327
                }
328

329
                if ( isset( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) && \is_string( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) ) {
×
330
                        $slug = \sanitize_title( \wp_unslash( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) );
×
331
                        if ( $post->post_name !== $slug ) {
×
332
                                $post_array              = $post->to_array();
×
333
                                $post_array['post_name'] = $slug;
×
334

335
                                $save_successful = \wp_insert_post( $post_array );
×
336
                                if ( \is_wp_error( $save_successful ) ) {
×
337
                                        \wp_send_json_error( 'Slug not saved', 400 );
×
338
                                }
339

340
                                // Update the post object to ensure we have the actual slug.
341
                                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Updating the post is needed to get the current slug.
342
                                $post = \get_post( $post_id );
×
343
                                if ( ! \is_object( $post ) ) {
×
344
                                        \wp_send_json_error( 'Updated slug not found', 400 );
×
345
                                }
346
                        }
347
                }
348

349
                \do_action( 'wpseo_saved_postdata' );
×
350

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

355
        /**
356
         * Determines if the given meta value key is disabled.
357
         *
358
         * @param string $key The key of the meta value.
359
         *
360
         * @return bool Whether the given meta value key is disabled.
361
         */
362
        public function is_meta_value_disabled( $key ) {
×
363
                if ( $key === 'linkdex' && ! $this->seo_analysis->is_enabled() ) {
×
364
                        return true;
×
365
                }
366

367
                if ( $key === 'content_score' && ! $this->readability_analysis->is_enabled() ) {
×
368
                        return true;
×
369
                }
370

371
                if ( $key === 'inclusive_language_score' && ! $this->inclusive_language_analysis->is_enabled() ) {
×
372
                        return true;
×
373
                }
374

375
                return false;
×
376
        }
377

378
        /**
379
         * Enqueues all the needed JS and CSS.
380
         *
381
         * @return void
382
         */
383
        public function enqueue() {
×
384
                $post_id = \get_queried_object_id();
×
385
                if ( empty( $post_id ) ) {
×
386
                        $post_id = 0;
×
387
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
388
                        if ( isset( $_GET['post'] ) && \is_string( $_GET['post'] ) ) {
×
389
                                // 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.
390
                                $post_id = (int) \wp_unslash( $_GET['post'] );
×
391
                        }
392
                }
393

394
                if ( $post_id !== 0 ) {
×
395
                        // Enqueue files needed for upload functionality.
396
                        \wp_enqueue_media( [ 'post' => $post_id ] );
×
397
                }
398

399
                $this->asset_manager->enqueue_style( 'admin-global' );
×
400
                $this->asset_manager->enqueue_style( 'metabox-css' );
×
401
                if ( $this->readability_analysis->is_enabled() ) {
×
402
                        $this->asset_manager->enqueue_style( 'scoring' );
×
403
                }
404
                $this->asset_manager->enqueue_style( 'monorepo' );
×
405
                $this->asset_manager->enqueue_style( 'admin-css' );
×
406
                $this->asset_manager->enqueue_style( 'ai-generator' );
×
407
                $this->asset_manager->enqueue_style( 'elementor' );
×
408

409
                $this->asset_manager->enqueue_script( 'admin-global' );
×
410
                $this->asset_manager->enqueue_script( 'elementor' );
×
411

412
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminGlobalL10n', \YoastSEO()->helpers->wincher->get_admin_global_links() );
×
413
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
×
414
                $this->asset_manager->localize_script( 'elementor', 'wpseoFeaturesL10n', WPSEO_Utils::retrieve_enabled_features() );
×
415

416
                $plugins_script_data = [
×
417
                        'replaceVars' => [
×
418
                                'replace_vars'             => $this->get_replace_vars(),
×
419
                                'recommended_replace_vars' => $this->get_recommended_replace_vars(),
×
420
                                'hidden_replace_vars'      => $this->get_hidden_replace_vars(),
×
421
                                'scope'                    => $this->determine_scope(),
×
422
                                'has_taxonomies'           => $this->current_post_type_has_taxonomies(),
×
423
                        ],
×
424
                        'shortcodes'  => [
×
425
                                'wpseo_shortcode_tags'          => $this->get_valid_shortcode_tags(),
×
426
                                'wpseo_filter_shortcodes_nonce' => \wp_create_nonce( 'wpseo-filter-shortcodes' ),
×
427
                        ],
×
428
                ];
×
429

430
                $worker_script_data = [
×
431
                        'url'                     => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-analysis-worker' ),
×
432
                        'dependencies'            => \YoastSEO()->helpers->asset->get_dependency_urls_by_handle( 'yoast-seo-analysis-worker' ),
×
433
                        'keywords_assessment_url' => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-used-keywords-assessment' ),
×
434
                        'log_level'               => WPSEO_Utils::get_analysis_worker_log_level(),
×
435
                        // We need to make the feature flags separately available inside of the analysis web worker.
436
                        'enabled_features'        => WPSEO_Utils::retrieve_enabled_features(),
×
437
                ];
×
438

439
                $permalink        = $this->get_permalink();
×
440
                $page_on_front    = (int) \get_option( 'page_on_front' );
×
441
                $homepage_is_page = \get_option( 'show_on_front' ) === 'page';
×
442
                $is_front_page    = $homepage_is_page && $page_on_front === $post_id;
×
443

444
                $script_data = [
×
445
                        'metabox'                   => $this->get_metabox_script_data( $permalink ),
×
446
                        'isPost'                    => true,
×
447
                        'isBlockEditor'             => WP_Screen::get()->is_block_editor(),
×
448
                        'isElementorEditor'         => true,
×
449
                        'isAlwaysIntroductionV2'    => $this->is_elementor_version_compatible_with_introduction_v2(),
×
450
                        'postStatus'                => \get_post_status( $post_id ),
×
451
                        'postType'                  => \get_post_type( $post_id ),
×
452
                        'analysis'                  => [
×
453
                                'plugins' => $plugins_script_data,
×
454
                                'worker'  => $worker_script_data,
×
455
                        ],
×
456
                        'usedKeywordsNonce'         => \wp_create_nonce( 'wpseo-keyword-usage-and-post-types' ),
×
457
                        'isFrontPage'               => $is_front_page,
×
458
                ];
×
459

460
                /**
461
                 * The website information repository.
462
                 *
463
                 * @var Website_Information_Repository $repo
464
                 */
465
                $repo             = \YoastSEO()->classes->get( Website_Information_Repository::class );
×
466
                $site_information = $repo->get_post_site_information();
×
467
                $site_information->set_permalink( $permalink );
×
468
                $script_data = \array_merge_recursive( $site_information->get_legacy_site_information(), $script_data );
×
469

470
                $this->asset_manager->localize_script( 'elementor', 'wpseoScriptData', $script_data );
×
471
        }
472

473
        /**
474
         * Checks whether the current Elementor version is compatible with our introduction v2.
475
         *
476
         * In version 3.30.0, Elementor removed the experimental flag for the editor v2.
477
         * Resulting in the editor v2 being the default.
478
         *
479
         * @return bool Whether the Elementor version is compatible with introduction v2.
480
         */
481
        private function is_elementor_version_compatible_with_introduction_v2(): bool {
×
482
                if ( ! \defined( 'ELEMENTOR_VERSION' ) ) {
×
483
                        return false;
×
484
                }
485

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

490
                // 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.
491
                return \version_compare( $version, '3.30.0', '>=' );
×
492
        }
493

494
        /**
495
         * Renders the metabox hidden fields.
496
         *
497
         * @return void
498
         */
499
        protected function render_hidden_fields() {
×
500
                // Wrap in a form with an action and post_id for the submit.
501
                \printf(
×
502
                        '<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" />',
×
503
                        \esc_url( \admin_url( 'admin-ajax.php' ) ),
×
504
                        \esc_attr( $this->get_metabox_post()->ID )
×
505
                );
×
506

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

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

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

519
                if ( $this->social_is_enabled ) {
×
520
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
521
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'social' );
×
522
                }
523

524
                \printf(
×
525
                        '<input type="hidden" id="%1$s" name="%1$s" value="%2$s" />',
×
526
                        \esc_attr( WPSEO_Meta::$form_prefix . 'slug' ),
×
527
                        /**
528
                         * It is important that this slug value is the same as in the database.
529
                         * If the DB value is empty we can auto-generate a slug.
530
                         * But if not empty, we should not touch it anymore.
531
                         */
532
                        \esc_attr( $this->get_metabox_post()->post_name )
×
533
                );
×
534

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

538
                echo '</form>';
×
539
        }
540

541
        /**
542
         * Returns post in metabox context.
543
         *
544
         * @return WP_Post|null
545
         */
546
        protected function get_metabox_post() {
×
547
                if ( $this->post !== null ) {
×
548
                        return $this->post;
×
549
                }
550

551
                $this->post = $this->request_post->get_post();
×
552

553
                return $this->post;
×
554
        }
555

556
        /**
557
         * Passes variables to js for use with the post-scraper.
558
         *
559
         * @param string $permalink The permalink.
560
         *
561
         * @return array
562
         */
563
        protected function get_metabox_script_data( $permalink ) {
×
564
                $post_formatter = new WPSEO_Metabox_Formatter(
×
565
                        new WPSEO_Post_Metabox_Formatter( $this->get_metabox_post(), [], $permalink )
×
566
                );
×
567

568
                $values = $post_formatter->get_values();
×
569

570
                /** This filter is documented in admin/filters/class-cornerstone-filter.php. */
571
                $post_types = \apply_filters( 'wpseo_cornerstone_post_types', \YoastSEO()->helpers->post_type->get_accessible_post_types() );
×
572
                if ( $values['cornerstoneActive'] && ! \in_array( $this->get_metabox_post()->post_type, $post_types, true ) ) {
×
573
                        $values['cornerstoneActive'] = false;
×
574
                }
575

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

578
                return $values;
×
579
        }
580

581
        /**
582
         * Gets the permalink.
583
         *
584
         * @return string
585
         */
586
        protected function get_permalink(): string {
×
587
                $permalink = '';
×
588

589
                if ( \is_object( $this->get_metabox_post() ) ) {
×
590
                        $permalink = \get_sample_permalink( $this->get_metabox_post()->ID );
×
591
                        $permalink = $permalink[0];
×
592
                }
593

594
                return $permalink;
×
595
        }
596

597
        /**
598
         * Checks whether the highlighting functionality is available for Elementor:
599
         * - in Free it's always available (as an upsell).
600
         * - in Premium it's available as long as the version is 21.8-RC0 or above.
601
         *
602
         * @return bool Whether the highlighting functionality is available.
603
         */
604
        private function is_highlighting_available() {
×
605
                $is_premium      = \YoastSEO()->helpers->product->is_premium();
×
606
                $premium_version = \YoastSEO()->helpers->product->get_premium_version();
×
607

608
                return ! $is_premium || \version_compare( $premium_version, '21.8-RC0', '>=' );
×
609
        }
610

611
        /**
612
         * Prepares the replace vars for localization.
613
         *
614
         * @return array Replace vars.
615
         */
616
        protected function get_replace_vars() {
×
617
                $cached_replacement_vars = [];
×
618

619
                $vars_to_cache = [
×
620
                        'date',
×
621
                        'id',
×
622
                        'sitename',
×
623
                        'sitedesc',
×
624
                        'sep',
×
625
                        'page',
×
626
                        'currentyear',
×
627
                        'currentdate',
×
628
                        'currentmonth',
×
629
                        'currentday',
×
630
                        'tag',
×
631
                        'category',
×
632
                        'category_title',
×
633
                        'primary_category',
×
634
                        'pt_single',
×
635
                        'pt_plural',
×
636
                        'modified',
×
637
                        'name',
×
638
                        'user_description',
×
639
                        'pagetotal',
×
640
                        'pagenumber',
×
641
                        'post_year',
×
642
                        'post_month',
×
643
                        'post_day',
×
644
                        'author_first_name',
×
645
                        'author_last_name',
×
646
                        'permalink',
×
647
                        'post_content',
×
648
                ];
×
649

650
                foreach ( $vars_to_cache as $var ) {
×
651
                        $cached_replacement_vars[ $var ] = \wpseo_replace_vars( '%%' . $var . '%%', $this->get_metabox_post() );
×
652
                }
653

654
                // Merge custom replace variables with the WordPress ones.
655
                return \array_merge( $cached_replacement_vars, $this->get_custom_replace_vars( $this->get_metabox_post() ) );
×
656
        }
657

658
        /**
659
         * Prepares the recommended replace vars for localization.
660
         *
661
         * @return array Recommended replacement variables.
662
         */
663
        protected function get_recommended_replace_vars() {
×
664
                $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
×
665

666
                // What is recommended depends on the current context.
667
                $post_type = $recommended_replace_vars->determine_for_post( $this->get_metabox_post() );
×
668

669
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
670
        }
671

672
        /**
673
         * Returns the list of replace vars that should be hidden inside the editor.
674
         *
675
         * @return string[] The hidden replace vars.
676
         */
677
        protected function get_hidden_replace_vars() {
×
678
                return ( new WPSEO_Replace_Vars() )->get_hidden_replace_vars();
×
679
        }
680

681
        /**
682
         * Gets the custom replace variables for custom taxonomies and fields.
683
         *
684
         * @param WP_Post $post The post to check for custom taxonomies and fields.
685
         *
686
         * @return array Array containing all the replacement variables.
687
         */
688
        protected function get_custom_replace_vars( $post ) {
×
689
                return [
×
690
                        'custom_fields'     => $this->get_custom_fields_replace_vars( $post ),
×
691
                        'custom_taxonomies' => $this->get_custom_taxonomies_replace_vars( $post ),
×
692
                ];
×
693
        }
694

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

706
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
707

708
                        if ( \is_string( $taxonomy ) ) { // If attachment, see https://core.trac.wordpress.org/ticket/37368 .
×
709
                                $taxonomy_name = $taxonomy;
×
710
                                $taxonomy      = \get_taxonomy( $taxonomy_name );
×
711
                        }
712

713
                        if ( $taxonomy->_builtin && $taxonomy->public ) {
×
714
                                continue;
×
715
                        }
716

717
                        $custom_replace_vars[ $taxonomy_name ] = [
×
718
                                'name'        => $taxonomy->name,
×
719
                                'description' => $taxonomy->description,
×
720
                        ];
×
721
                }
722

723
                return $custom_replace_vars;
×
724
        }
725

726
        /**
727
         * Gets the custom replace variables for custom fields.
728
         *
729
         * @param WP_Post $post The post to check for custom fields.
730
         *
731
         * @return array Array containing all the replacement variables.
732
         */
733
        protected function get_custom_fields_replace_vars( $post ) {
×
734
                $custom_replace_vars = [];
×
735

736
                // If no post object is passed, return the empty custom_replace_vars array.
737
                if ( ! \is_object( $post ) ) {
×
738
                        return $custom_replace_vars;
×
739
                }
740

741
                $custom_fields = \get_post_custom( $post->ID );
×
742

743
                // Simply concatenate all fields containing replace vars so we can handle them all with a single regex find.
744
                $replace_vars_fields = \implode(
×
745
                        ' ',
×
746
                        [
×
747
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->title,
×
748
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->meta_description,
×
749
                        ]
×
750
                );
×
751

752
                \preg_match_all( '/%%cf_([A-Za-z0-9_]+)%%/', $replace_vars_fields, $matches );
×
753
                $fields_to_include = $matches[1];
×
754
                foreach ( $custom_fields as $custom_field_name => $custom_field ) {
×
755
                        // Skip private custom fields.
756
                        if ( \substr( $custom_field_name, 0, 1 ) === '_' ) {
×
757
                                continue;
×
758
                        }
759

760
                        // Skip custom fields that are not used, new ones will be fetched dynamically.
761
                        if ( ! \in_array( $custom_field_name, $fields_to_include, true ) ) {
×
762
                                continue;
×
763
                        }
764

765
                        // Skip custom field values that are serialized.
766
                        if ( \is_serialized( $custom_field[0] ) ) {
×
767
                                continue;
×
768
                        }
769

770
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
771
                }
772

773
                return $custom_replace_vars;
×
774
        }
775

776
        /**
777
         * Determines the scope based on the post type.
778
         * This can be used by the replacevar plugin to determine if a replacement needs to be executed.
779
         *
780
         * @return string String describing the current scope.
781
         */
782
        protected function determine_scope() {
×
783
                if ( $this->get_metabox_post()->post_type === 'page' ) {
×
784
                        return 'page';
×
785
                }
786

787
                return 'post';
×
788
        }
789

790
        /**
791
         * Determines whether or not the current post type has registered taxonomies.
792
         *
793
         * @return bool Whether the current post type has taxonomies.
794
         */
795
        protected function current_post_type_has_taxonomies() {
×
796
                $post_taxonomies = \get_object_taxonomies( $this->get_metabox_post()->post_type );
×
797

798
                return ! empty( $post_taxonomies );
×
799
        }
800

801
        /**
802
         * Returns an array with shortcode tags for all registered shortcodes.
803
         *
804
         * @return array
805
         */
806
        protected function get_valid_shortcode_tags() {
×
807
                $shortcode_tags = [];
×
808

809
                foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
×
810
                        $shortcode_tags[] = $tag;
×
811
                }
812

813
                return $shortcode_tags;
×
814
        }
815
}
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