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

Yoast / wordpress-seo / 555722276c5d045f658b7dd88fb5ee08630e1093

12 Nov 2025 11:12AM UTC coverage: 53.328% (-0.06%) from 53.388%
555722276c5d045f658b7dd88fb5ee08630e1093

Pull #22697

github

web-flow
Merge 94d318e70 into 68afd79ba
Pull Request #22697: Show yoast in elements tab in elementor

8497 of 15710 branches covered (54.09%)

Branch coverage included in aggregate %.

0 of 80 new or added lines in 5 files covered. (0.0%)

1 existing line in 1 file now uncovered.

32187 of 60580 relevant lines covered (53.13%)

39422.17 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
         */
NEW
187
        public function start_capturing() {
×
NEW
188
                ob_start();
×
189
        }
190

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

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

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

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

234
                if ( ! isset( $_POST['post_id'] ) || ! \is_string( $_POST['post_id'] ) ) {
×
235
                        \wp_send_json_error( 'Bad Request', 400 );
×
236
                }
237

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

241
                if ( $post_id <= 0 ) {
×
242
                        \wp_send_json_error( 'Bad Request', 400 );
×
243
                }
244

245
                if ( ! \current_user_can( 'edit_post', $post_id ) ) {
×
246
                        \wp_send_json_error( 'Forbidden', 403 );
×
247
                }
248

249
                \check_ajax_referer( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
×
250

251
                // Bail if this is a multisite installation and the site has been switched.
252
                if ( \is_multisite() && \ms_is_switched() ) {
×
253
                        \wp_send_json_error( 'Switched multisite', 409 );
×
254
                }
255

256
                \clean_post_cache( $post_id );
×
257
                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- To setup the post we need to do this explicitly.
258
                $post = \get_post( $post_id );
×
259

260
                if ( ! \is_object( $post ) ) {
×
261
                        // Non-existent post.
262
                        \wp_send_json_error( 'Post not found', 400 );
×
263
                }
264

265
                \do_action( 'wpseo_save_compare_data', $post );
×
266

267
                // Initialize meta, amongst other things it registers sanitization.
268
                WPSEO_Meta::init();
×
269

270
                $social_fields = [];
×
271
                if ( $this->social_is_enabled ) {
×
272
                        $social_fields = WPSEO_Meta::get_meta_field_defs( 'social', $post->post_type );
×
273
                }
274

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

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

293
                        $data       = null;
×
294
                        $field_name = WPSEO_Meta::$form_prefix . $key;
×
295

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

304
                                        // For multi-select.
305
                                        if ( \is_array( $data ) ) {
×
306
                                                $data = \array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], $data );
×
307
                                        }
308

309
                                        if ( \is_string( $data ) ) {
×
310
                                                $data = ( $key !== 'canonical' ) ? WPSEO_Utils::sanitize_text_field( $data ) : WPSEO_Utils::sanitize_url( $data );
×
311
                                        }
312
                                }
313

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

320
                        if ( $data !== null ) {
×
321
                                WPSEO_Meta::set_value( $key, $data, $post_id );
×
322
                        }
323
                }
324

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

331
                                $save_successful = \wp_insert_post( $post_array );
×
332
                                if ( \is_wp_error( $save_successful ) ) {
×
333
                                        \wp_send_json_error( 'Slug not saved', 400 );
×
334
                                }
335

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

345
                \do_action( 'wpseo_saved_postdata' );
×
346

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

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

363
                if ( $key === 'content_score' && ! $this->readability_analysis->is_enabled() ) {
×
364
                        return true;
×
365
                }
366

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

371
                return false;
×
372
        }
373

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

390
                if ( $post_id !== 0 ) {
×
391
                        // Enqueue files needed for upload functionality.
392
                        \wp_enqueue_media( [ 'post' => $post_id ] );
×
393
                }
394

395
                $this->asset_manager->enqueue_style( 'admin-global' );
×
396
                $this->asset_manager->enqueue_style( 'metabox-css' );
×
397
                if ( $this->readability_analysis->is_enabled() ) {
×
398
                        $this->asset_manager->enqueue_style( 'scoring' );
×
399
                }
400
                $this->asset_manager->enqueue_style( 'monorepo' );
×
401
                $this->asset_manager->enqueue_style( 'admin-css' );
×
402
                $this->asset_manager->enqueue_style( 'ai-generator' );
×
403
                $this->asset_manager->enqueue_style( 'elementor' );
×
404

405
                $this->asset_manager->enqueue_script( 'admin-global' );
×
406
                $this->asset_manager->enqueue_script( 'elementor' );
×
407

408
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminGlobalL10n', \YoastSEO()->helpers->wincher->get_admin_global_links() );
×
409
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
×
410
                $this->asset_manager->localize_script( 'elementor', 'wpseoFeaturesL10n', WPSEO_Utils::retrieve_enabled_features() );
×
411

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

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

435
                $permalink        = $this->get_permalink();
×
436
                $page_on_front    = (int) \get_option( 'page_on_front' );
×
437
                $homepage_is_page = \get_option( 'show_on_front' ) === 'page';
×
438
                $is_front_page    = $homepage_is_page && $page_on_front === $post_id;
×
439

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

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

466
                $this->asset_manager->localize_script( 'elementor', 'wpseoScriptData', $script_data );
×
467
        }
468

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

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

486
                // 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.
487
                return \version_compare( $version, '3.30.0', '>=' );
×
488
        }
489

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

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

507
                if ( $this->is_advanced_metadata_enabled ) {
×
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(), 'advanced' );
×
510
                }
511

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(), 'schema', $this->get_metabox_post()->post_type );
×
514

515
                if ( $this->social_is_enabled ) {
×
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(), 'social' );
×
518
                }
519

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

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

534
                echo '</form>';
×
535
        }
536

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

547
                $this->post = $this->request_post->get_post();
×
548

549
                return $this->post;
×
550
        }
551

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

564
                $values = $post_formatter->get_values();
×
565

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

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

574
                return $values;
×
575
        }
576

577
        /**
578
         * Gets the permalink.
579
         *
580
         * @return string
581
         */
582
        protected function get_permalink(): string {
×
583
                $permalink = '';
×
584

585
                if ( \is_object( $this->get_metabox_post() ) ) {
×
586
                        $permalink = \get_sample_permalink( $this->get_metabox_post()->ID );
×
587
                        $permalink = $permalink[0];
×
588
                }
589

590
                return $permalink;
×
591
        }
592

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

604
                return ! $is_premium || \version_compare( $premium_version, '21.8-RC0', '>=' );
×
605
        }
606

607
        /**
608
         * Prepares the replace vars for localization.
609
         *
610
         * @return array Replace vars.
611
         */
612
        protected function get_replace_vars() {
×
613
                $cached_replacement_vars = [];
×
614

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

646
                foreach ( $vars_to_cache as $var ) {
×
647
                        $cached_replacement_vars[ $var ] = \wpseo_replace_vars( '%%' . $var . '%%', $this->get_metabox_post() );
×
648
                }
649

650
                // Merge custom replace variables with the WordPress ones.
651
                return \array_merge( $cached_replacement_vars, $this->get_custom_replace_vars( $this->get_metabox_post() ) );
×
652
        }
653

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

662
                // What is recommended depends on the current context.
663
                $post_type = $recommended_replace_vars->determine_for_post( $this->get_metabox_post() );
×
664

665
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
666
        }
667

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

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

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

702
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
703

704
                        if ( \is_string( $taxonomy ) ) { // If attachment, see https://core.trac.wordpress.org/ticket/37368 .
×
705
                                $taxonomy_name = $taxonomy;
×
706
                                $taxonomy      = \get_taxonomy( $taxonomy_name );
×
707
                        }
708

709
                        if ( $taxonomy->_builtin && $taxonomy->public ) {
×
710
                                continue;
×
711
                        }
712

713
                        $custom_replace_vars[ $taxonomy_name ] = [
×
714
                                'name'        => $taxonomy->name,
×
715
                                'description' => $taxonomy->description,
×
716
                        ];
×
717
                }
718

719
                return $custom_replace_vars;
×
720
        }
721

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

732
                // If no post object is passed, return the empty custom_replace_vars array.
733
                if ( ! \is_object( $post ) ) {
×
734
                        return $custom_replace_vars;
×
735
                }
736

737
                $custom_fields = \get_post_custom( $post->ID );
×
738

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

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

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

761
                        // Skip custom field values that are serialized.
762
                        if ( \is_serialized( $custom_field[0] ) ) {
×
763
                                continue;
×
764
                        }
765

766
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
767
                }
768

769
                return $custom_replace_vars;
×
770
        }
771

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

783
                return 'post';
×
784
        }
785

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

794
                return ! empty( $post_taxonomies );
×
795
        }
796

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

805
                foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
×
806
                        $shortcode_tags[] = $tag;
×
807
                }
808

809
                return $shortcode_tags;
×
810
        }
811
}
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