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

Yoast / wordpress-seo / b6b82d7fac65ed06cb8f9ce8524a18d73854de80

10 Jun 2026 10:54AM UTC coverage: 50.275%. First build
b6b82d7fac65ed06cb8f9ce8524a18d73854de80

Pull #23344

github

vraja-pro
fix: php cs
Pull Request #23344: Add Elementor V4 atomic editor content extraction

0 of 19 new or added lines in 2 files covered. (0.0%)

20868 of 41508 relevant lines covered (50.27%)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

169
                \add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'init' ] );
×
170
                \add_action( 'elementor/editor/footer', [ $this, 'start_output_buffering' ], 0 );
×
171
                \add_action( 'elementor/editor/footer', [ $this, 'inject_yoast_tab' ], 999 );
×
172
        }
173

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

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

194
        /**
195
         * Injects the Yoast SEO tab into the Elements panel of the Elementor editor.
196
         *
197
         * @return void
198
         */
199
        public function inject_yoast_tab() {
×
200
                $output = \ob_get_clean();
×
201

202
                // If the buffer is empty or the call failed, bail out.
203
                if ( empty( $output ) ) {
×
204
                        return;
×
205
                }
206

207
                $search  = '/(<(div|button) class="elementor-component-tab elementor-panel-navigation-tab" data-tab="global">.*<\/(div|button)>)/m';
×
208
                $replace = '${1}<${2} class="elementor-component-tab elementor-panel-navigation-tab" data-tab="yoast-seo-tab">Yoast SEO</${2}>';
×
209

210
                $modified_output = \preg_replace( $search, $replace, $output );
×
211

212
                // Check if preg_replace failed. If so, fallback to original output.
213
                if ( $modified_output === null ) {
×
214
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Already escaped output.
215
                        echo $output;
×
216
                        return;
×
217
                }
218

219
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Already escaped output.
220
                echo $modified_output;
×
221
        }
222

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

225
        /**
226
         * Determines whether the metabox should be shown for the passed identifier.
227
         *
228
         * By default, the check is done for post types, but can also be used for taxonomies.
229
         *
230
         * @param string|null $identifier The identifier to check.
231
         * @param string      $type       The type of object to check. Defaults to post_type.
232
         *
233
         * @return bool Whether the metabox should be displayed.
234
         */
235
        public function display_metabox( $identifier = null, $type = 'post_type' ) {
×
236
                return WPSEO_Utils::is_metabox_active( $identifier, $type );
×
237
        }
238

239
        /**
240
         * Saves the WP SEO metadata for posts.
241
         *
242
         * Outputs JSON via wp_send_json then stops code execution.
243
         *
244
         * {@internal $_POST parameters are validated via sanitize_post_meta().}}
245
         *
246
         * @return void
247
         */
248
        public function save_postdata() {
×
249
                global $post;
×
250

251
                if ( ! isset( $_POST['post_id'] ) || ! \is_string( $_POST['post_id'] ) ) {
×
252
                        \wp_send_json_error( 'Bad Request', 400 );
×
253
                }
254

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

258
                if ( $post_id <= 0 ) {
×
259
                        \wp_send_json_error( 'Bad Request', 400 );
×
260
                }
261

262
                if ( ! \current_user_can( 'edit_post', $post_id ) ) {
×
263
                        \wp_send_json_error( 'Forbidden', 403 );
×
264
                }
265

266
                \check_ajax_referer( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
×
267

268
                // Bail if this is a multisite installation and the site has been switched.
269
                if ( \is_multisite() && \ms_is_switched() ) {
×
270
                        \wp_send_json_error( 'Switched multisite', 409 );
×
271
                }
272

273
                \clean_post_cache( $post_id );
×
274
                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- To setup the post we need to do this explicitly.
275
                $post = \get_post( $post_id );
×
276

277
                if ( ! \is_object( $post ) ) {
×
278
                        // Non-existent post.
279
                        \wp_send_json_error( 'Post not found', 400 );
×
280
                }
281

282
                \do_action( 'wpseo_save_compare_data', $post );
×
283

284
                // Initialize meta, amongst other things it registers sanitization.
285
                WPSEO_Meta::init();
×
286

287
                $social_fields = [];
×
288
                if ( $this->social_is_enabled ) {
×
289
                        $social_fields = WPSEO_Meta::get_meta_field_defs( 'social', $post->post_type );
×
290
                }
291

292
                // The below methods use the global post so make sure it is setup.
293
                \setup_postdata( $post );
×
294
                $meta_boxes = \apply_filters( 'wpseo_save_metaboxes', [] );
×
295
                $meta_boxes = \array_merge(
×
296
                        $meta_boxes,
×
297
                        WPSEO_Meta::get_meta_field_defs( 'general', $post->post_type ),
×
298
                        WPSEO_Meta::get_meta_field_defs( 'advanced', $post->post_type ),
×
299
                        $social_fields,
×
300
                        WPSEO_Meta::get_meta_field_defs( 'schema', $post->post_type ),
×
301
                );
×
302

303
                foreach ( $meta_boxes as $key => $meta_box ) {
×
304
                        // If analysis is disabled remove that analysis score value from the DB.
305
                        if ( $this->is_meta_value_disabled( $key ) ) {
×
306
                                WPSEO_Meta::delete( $key, $post_id );
×
307
                                continue;
×
308
                        }
309

310
                        $data       = null;
×
311
                        $field_name = WPSEO_Meta::$form_prefix . $key;
×
312

313
                        if ( $meta_box['type'] === 'checkbox' ) {
×
314
                                $data = isset( $_POST[ $field_name ] ) ? 'on' : 'off';
×
315
                        }
316
                        else {
317
                                if ( isset( $_POST[ $field_name ] ) ) {
×
318
                                        // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: Sanitized through sanitize_post_meta.
319
                                        $data = \wp_unslash( $_POST[ $field_name ] );
×
320

321
                                        // For multi-select.
322
                                        if ( \is_array( $data ) ) {
×
323
                                                $data = \array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], $data );
×
324
                                        }
325

326
                                        if ( \is_string( $data ) ) {
×
327
                                                $data = ( $key !== 'canonical' ) ? WPSEO_Utils::sanitize_text_field( $data ) : WPSEO_Utils::sanitize_url( $data );
×
328
                                        }
329
                                }
330

331
                                // Reset options when no entry is present with multiselect - only applies to `meta-robots-adv` currently.
332
                                if ( ! isset( $_POST[ $field_name ] ) && ( $meta_box['type'] === 'multiselect' ) ) {
×
333
                                        $data = [];
×
334
                                }
335
                        }
336

337
                        if ( $data !== null ) {
×
338
                                WPSEO_Meta::set_value( $key, $data, $post_id );
×
339
                        }
340
                }
341

342
                if ( isset( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) && \is_string( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) ) {
×
343
                        $slug = \sanitize_title( \wp_unslash( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) );
×
344
                        if ( $post->post_name !== $slug ) {
×
345
                                $post_array              = $post->to_array();
×
346
                                $post_array['post_name'] = $slug;
×
347

348
                                $save_successful = \wp_insert_post( $post_array );
×
349
                                if ( \is_wp_error( $save_successful ) ) {
×
350
                                        \wp_send_json_error( 'Slug not saved', 400 );
×
351
                                }
352

353
                                // Update the post object to ensure we have the actual slug.
354
                                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Updating the post is needed to get the current slug.
355
                                $post = \get_post( $post_id );
×
356
                                if ( ! \is_object( $post ) ) {
×
357
                                        \wp_send_json_error( 'Updated slug not found', 400 );
×
358
                                }
359
                        }
360
                }
361

362
                \do_action( 'wpseo_saved_postdata' );
×
363

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

368
        /**
369
         * Determines if the given meta value key is disabled.
370
         *
371
         * @param string $key The key of the meta value.
372
         *
373
         * @return bool Whether the given meta value key is disabled.
374
         */
375
        public function is_meta_value_disabled( $key ) {
×
376
                if ( $key === 'linkdex' && ! $this->seo_analysis->is_enabled() ) {
×
377
                        return true;
×
378
                }
379

380
                if ( $key === 'content_score' && ! $this->readability_analysis->is_enabled() ) {
×
381
                        return true;
×
382
                }
383

384
                if ( $key === 'inclusive_language_score' && ! $this->inclusive_language_analysis->is_enabled() ) {
×
385
                        return true;
×
386
                }
387

388
                return false;
×
389
        }
390

391
        /**
392
         * Enqueues all the needed JS and CSS.
393
         *
394
         * @return void
395
         */
396
        public function enqueue() {
×
397
                $post_id = \get_queried_object_id();
×
398
                if ( empty( $post_id ) ) {
×
399
                        $post_id = 0;
×
400
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
401
                        if ( isset( $_GET['post'] ) && \is_string( $_GET['post'] ) ) {
×
402
                                // 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.
403
                                $post_id = (int) \wp_unslash( $_GET['post'] );
×
404
                        }
405
                }
406

407
                if ( $post_id !== 0 ) {
×
408
                        // Enqueue files needed for upload functionality.
409
                        \wp_enqueue_media( [ 'post' => $post_id ] );
×
410
                }
411

412
                $this->asset_manager->enqueue_style( 'admin-global' );
×
413
                $this->asset_manager->enqueue_style( 'metabox-css' );
×
414
                if ( $this->readability_analysis->is_enabled() ) {
×
415
                        $this->asset_manager->enqueue_style( 'scoring' );
×
416
                }
417
                $this->asset_manager->enqueue_style( 'monorepo' );
×
418
                $this->asset_manager->enqueue_style( 'admin-css' );
×
419
                $this->asset_manager->enqueue_style( 'ai-generator' );
×
420
                $this->asset_manager->enqueue_style( 'elementor' );
×
421

422
                $this->asset_manager->enqueue_script( 'admin-global' );
×
423
                $this->asset_manager->enqueue_script( 'elementor' );
×
424

NEW
425
                if ( $this->is_elementor_v4_atomic_active() ) {
×
NEW
426
                        $this->asset_manager->enqueue_script( 'elementor-v4' );
×
427
                }
428

429
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminGlobalL10n', \YoastSEO()->helpers->wincher->get_admin_global_links() );
×
430
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
×
431
                $this->asset_manager->localize_script( 'elementor', 'wpseoFeaturesL10n', WPSEO_Utils::retrieve_enabled_features() );
×
432

433
                $plugins_script_data = [
×
434
                        'replaceVars' => [
×
435
                                'replace_vars'             => $this->get_replace_vars(),
×
436
                                'recommended_replace_vars' => $this->get_recommended_replace_vars(),
×
437
                                'hidden_replace_vars'      => $this->get_hidden_replace_vars(),
×
438
                                'scope'                    => $this->determine_scope(),
×
439
                                'has_taxonomies'           => $this->current_post_type_has_taxonomies(),
×
440
                        ],
×
441
                        'shortcodes'  => [
×
442
                                'wpseo_shortcode_tags'          => $this->get_valid_shortcode_tags(),
×
443
                                'wpseo_filter_shortcodes_nonce' => \wp_create_nonce( 'wpseo-filter-shortcodes' ),
×
444
                        ],
×
445
                ];
×
446

447
                $worker_script_data = [
×
448
                        'url'                     => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-analysis-worker' ),
×
449
                        'dependencies'            => \YoastSEO()->helpers->asset->get_dependency_urls_by_handle( 'yoast-seo-analysis-worker' ),
×
450
                        'keywords_assessment_url' => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-used-keywords-assessment' ),
×
451
                        'log_level'               => WPSEO_Utils::get_analysis_worker_log_level(),
×
452
                        // We need to make the feature flags separately available inside of the analysis web worker.
453
                        'enabled_features'        => WPSEO_Utils::retrieve_enabled_features(),
×
454
                ];
×
455

456
                $permalink        = $this->get_permalink();
×
457
                $page_on_front    = (int) \get_option( 'page_on_front' );
×
458
                $homepage_is_page = \get_option( 'show_on_front' ) === 'page';
×
459
                $is_front_page    = $homepage_is_page && $page_on_front === $post_id;
×
460

461
                $script_data = [
×
462
                        'metabox'                   => $this->get_metabox_script_data( $permalink ),
×
463
                        'isPost'                    => true,
×
464
                        'isBlockEditor'             => WP_Screen::get()->is_block_editor(),
×
465
                        'isElementorEditor'         => true,
×
466
                        'isAlwaysIntroductionV2'    => $this->is_elementor_version_compatible_with_introduction_v2(),
×
NEW
467
                        'isElementorV4Atomic'       => $this->is_elementor_v4_atomic_active(),
×
468
                        'postStatus'                => \get_post_status( $post_id ),
×
469
                        'postType'                  => \get_post_type( $post_id ),
×
470
                        'analysis'                  => [
×
471
                                'plugins' => $plugins_script_data,
×
472
                                'worker'  => $worker_script_data,
×
473
                        ],
×
474
                        'usedKeywordsNonce'         => \wp_create_nonce( 'wpseo-keyword-usage-and-post-types' ),
×
475
                        'isFrontPage'               => $is_front_page,
×
476
                ];
×
477

478
                /**
479
                 * The website information repository.
480
                 *
481
                 * @var Website_Information_Repository $repo
482
                 */
483
                $repo             = \YoastSEO()->classes->get( Website_Information_Repository::class );
×
484
                $site_information = $repo->get_post_site_information();
×
485
                $site_information->set_permalink( $permalink );
×
486
                $script_data = \array_merge_recursive( $site_information->get_legacy_site_information(), $script_data );
×
487

488
                $this->asset_manager->localize_script( 'elementor', 'wpseoScriptData', $script_data );
×
489
        }
490

491
        /**
492
         * Checks whether the current Elementor version is compatible with our introduction v2.
493
         *
494
         * In version 3.30.0, Elementor removed the experimental flag for the editor v2.
495
         * Resulting in the editor v2 being the default.
496
         *
497
         * @return bool Whether the Elementor version is compatible with introduction v2.
498
         */
499
        private function is_elementor_version_compatible_with_introduction_v2(): bool {
×
500
                if ( ! \defined( 'ELEMENTOR_VERSION' ) ) {
×
501
                        return false;
×
502
                }
503

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

508
                // 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.
509
                return \version_compare( $version, '3.30.0', '>=' );
×
510
        }
511

512
        /**
513
         * Checks whether Elementor V4 (the atomic editor) is currently active on this site.
514
         *
515
         * Returns true only when all of the following hold: Elementor >= 4.0.0 is loaded,
516
         * its experiments manager is accessible, and the `e_opt_in_v4` experiment is active.
517
         * Defensive at each step so older Elementor versions (or partial installs) never throw.
518
         *
519
         * @return bool Whether the V4 atomic editor path should be used.
520
         */
NEW
521
        private function is_elementor_v4_atomic_active(): bool {
×
NEW
522
                if ( ! \defined( 'ELEMENTOR_VERSION' ) ) {
×
NEW
523
                        return false;
×
524
                }
525

NEW
526
                $matches = [];
×
NEW
527
                $version = ( \preg_match( '/^([0-9]+\.[0-9]+\.[0-9]+)/', \ELEMENTOR_VERSION, $matches ) > 0 ) ? $matches[1] : \ELEMENTOR_VERSION;
×
NEW
528
                if ( \version_compare( $version, '4.0.0', '<' ) ) {
×
NEW
529
                        return false;
×
530
                }
531

NEW
532
                if ( ! \class_exists( '\Elementor\Plugin' ) ) {
×
NEW
533
                        return false;
×
534
                }
NEW
535
                $elementor = Plugin::$instance ?? null;
×
NEW
536
                if ( $elementor === null || ! isset( $elementor->experiments ) ) {
×
NEW
537
                        return false;
×
538
                }
NEW
539
                if ( ! \method_exists( $elementor->experiments, 'is_feature_active' ) ) {
×
NEW
540
                        return false;
×
541
                }
542

NEW
543
                return (bool) $elementor->experiments->is_feature_active( 'e_opt_in_v4' );
×
544
        }
545

546
        /**
547
         * Renders the metabox hidden fields.
548
         *
549
         * @return void
550
         */
551
        protected function render_hidden_fields() {
×
552
                // Wrap in a form with an action and post_id for the submit.
553
                \printf(
×
554
                        '<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" />',
×
555
                        \esc_url( \admin_url( 'admin-ajax.php' ) ),
×
556
                        \esc_attr( $this->get_metabox_post()->ID ),
×
557
                );
×
558

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

563
                if ( $this->is_advanced_metadata_enabled ) {
×
564
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
565
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'advanced' );
×
566
                }
567

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

571
                if ( $this->social_is_enabled ) {
×
572
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
573
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'social' );
×
574
                }
575

576
                \printf(
×
577
                        '<input type="hidden" id="%1$s" name="%1$s" value="%2$s" />',
×
578
                        \esc_attr( WPSEO_Meta::$form_prefix . 'slug' ),
×
579
                        /**
580
                         * It is important that this slug value is the same as in the database.
581
                         * If the DB value is empty we can auto-generate a slug.
582
                         * But if not empty, we should not touch it anymore.
583
                         */
584
                        \esc_attr( $this->get_metabox_post()->post_name ),
×
585
                );
×
586

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

590
                echo '</form>';
×
591
        }
592

593
        /**
594
         * Returns post in metabox context.
595
         *
596
         * @return WP_Post|null
597
         */
598
        protected function get_metabox_post() {
×
599
                if ( $this->post !== null ) {
×
600
                        return $this->post;
×
601
                }
602

603
                $this->post = $this->request_post->get_post();
×
604

605
                return $this->post;
×
606
        }
607

608
        /**
609
         * Passes variables to js for use with the post-scraper.
610
         *
611
         * @param string $permalink The permalink.
612
         *
613
         * @return array
614
         */
615
        protected function get_metabox_script_data( $permalink ) {
×
616
                $post_formatter = new WPSEO_Metabox_Formatter(
×
617
                        new WPSEO_Post_Metabox_Formatter( $this->get_metabox_post(), [], $permalink ),
×
618
                );
×
619

620
                $values = $post_formatter->get_values();
×
621

622
                /** This filter is documented in admin/filters/class-cornerstone-filter.php. */
623
                $post_types = \apply_filters( 'wpseo_cornerstone_post_types', \YoastSEO()->helpers->post_type->get_accessible_post_types() );
×
624
                if ( $values['cornerstoneActive'] && ! \in_array( $this->get_metabox_post()->post_type, $post_types, true ) ) {
×
625
                        $values['cornerstoneActive'] = false;
×
626
                }
627

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

630
                return $values;
×
631
        }
632

633
        /**
634
         * Gets the permalink.
635
         *
636
         * @return string
637
         */
638
        protected function get_permalink(): string {
×
639
                $permalink = '';
×
640

641
                if ( \is_object( $this->get_metabox_post() ) ) {
×
642
                        $permalink = \get_sample_permalink( $this->get_metabox_post()->ID );
×
643
                        $permalink = $permalink[0];
×
644
                }
645

646
                return $permalink;
×
647
        }
648

649
        /**
650
         * Checks whether the highlighting functionality is available for Elementor:
651
         * - in Free it's always available (as an upsell).
652
         * - in Premium it's available as long as the version is 21.8-RC0 or above.
653
         *
654
         * @return bool Whether the highlighting functionality is available.
655
         */
656
        private function is_highlighting_available() {
×
657
                $is_premium      = \YoastSEO()->helpers->product->is_premium();
×
658
                $premium_version = \YoastSEO()->helpers->product->get_premium_version();
×
659

660
                return ! $is_premium || \version_compare( $premium_version, '21.8-RC0', '>=' );
×
661
        }
662

663
        /**
664
         * Prepares the replace vars for localization.
665
         *
666
         * @return array Replace vars.
667
         */
668
        protected function get_replace_vars() {
×
669
                $cached_replacement_vars = [];
×
670

671
                $vars_to_cache = [
×
672
                        'date',
×
673
                        'id',
×
674
                        'sitename',
×
675
                        'sitedesc',
×
676
                        'sep',
×
677
                        'page',
×
678
                        'currentyear',
×
679
                        'currentdate',
×
680
                        'currentmonth',
×
681
                        'currentday',
×
682
                        'tag',
×
683
                        'category',
×
684
                        'category_title',
×
685
                        'primary_category',
×
686
                        'pt_single',
×
687
                        'pt_plural',
×
688
                        'modified',
×
689
                        'name',
×
690
                        'user_description',
×
691
                        'pagetotal',
×
692
                        'pagenumber',
×
693
                        'post_year',
×
694
                        'post_month',
×
695
                        'post_day',
×
696
                        'author_first_name',
×
697
                        'author_last_name',
×
698
                        'permalink',
×
699
                        'post_content',
×
700
                ];
×
701

702
                foreach ( $vars_to_cache as $var ) {
×
703
                        $cached_replacement_vars[ $var ] = \wpseo_replace_vars( '%%' . $var . '%%', $this->get_metabox_post() );
×
704
                }
705

706
                // Merge custom replace variables with the WordPress ones.
707
                return \array_merge( $cached_replacement_vars, $this->get_custom_replace_vars( $this->get_metabox_post() ) );
×
708
        }
709

710
        /**
711
         * Prepares the recommended replace vars for localization.
712
         *
713
         * @return array Recommended replacement variables.
714
         */
715
        protected function get_recommended_replace_vars() {
×
716
                $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
×
717

718
                // What is recommended depends on the current context.
719
                $post_type = $recommended_replace_vars->determine_for_post( $this->get_metabox_post() );
×
720

721
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
722
        }
723

724
        /**
725
         * Returns the list of replace vars that should be hidden inside the editor.
726
         *
727
         * @return string[] The hidden replace vars.
728
         */
729
        protected function get_hidden_replace_vars() {
×
730
                return ( new WPSEO_Replace_Vars() )->get_hidden_replace_vars();
×
731
        }
732

733
        /**
734
         * Gets the custom replace variables for custom taxonomies and fields.
735
         *
736
         * @param WP_Post $post The post to check for custom taxonomies and fields.
737
         *
738
         * @return array Array containing all the replacement variables.
739
         */
740
        protected function get_custom_replace_vars( $post ) {
×
741
                return [
×
742
                        'custom_fields'     => $this->get_custom_fields_replace_vars( $post ),
×
743
                        'custom_taxonomies' => $this->get_custom_taxonomies_replace_vars( $post ),
×
744
                ];
×
745
        }
746

747
        /**
748
         * Gets the custom replace variables for custom taxonomies.
749
         *
750
         * @param WP_Post $post The post to check for custom taxonomies.
751
         *
752
         * @return array Array containing all the replacement variables.
753
         */
754
        protected function get_custom_taxonomies_replace_vars( $post ) {
×
755
                $taxonomies          = \get_object_taxonomies( $post, 'objects' );
×
756
                $custom_replace_vars = [];
×
757

758
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
759

760
                        if ( \is_string( $taxonomy ) ) { // If attachment, see https://core.trac.wordpress.org/ticket/37368 .
×
761
                                $taxonomy_name = $taxonomy;
×
762
                                $taxonomy      = \get_taxonomy( $taxonomy_name );
×
763
                        }
764

765
                        if ( $taxonomy->_builtin && $taxonomy->public ) {
×
766
                                continue;
×
767
                        }
768

769
                        $custom_replace_vars[ $taxonomy_name ] = [
×
770
                                'name'        => $taxonomy->name,
×
771
                                'description' => $taxonomy->description,
×
772
                        ];
×
773
                }
774

775
                return $custom_replace_vars;
×
776
        }
777

778
        /**
779
         * Gets the custom replace variables for custom fields.
780
         *
781
         * @param WP_Post $post The post to check for custom fields.
782
         *
783
         * @return array Array containing all the replacement variables.
784
         */
785
        protected function get_custom_fields_replace_vars( $post ) {
×
786
                $custom_replace_vars = [];
×
787

788
                // If no post object is passed, return the empty custom_replace_vars array.
789
                if ( ! \is_object( $post ) ) {
×
790
                        return $custom_replace_vars;
×
791
                }
792

793
                $custom_fields = \get_post_custom( $post->ID );
×
794

795
                // Simply concatenate all fields containing replace vars so we can handle them all with a single regex find.
796
                $replace_vars_fields = \implode(
×
797
                        ' ',
×
798
                        [
×
799
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->title,
×
800
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->meta_description,
×
801
                        ],
×
802
                );
×
803

804
                \preg_match_all( '/%%cf_([A-Za-z0-9_]+)%%/', $replace_vars_fields, $matches );
×
805
                $fields_to_include = $matches[1];
×
806
                foreach ( $custom_fields as $custom_field_name => $custom_field ) {
×
807
                        // Skip private custom fields.
808
                        if ( \substr( $custom_field_name, 0, 1 ) === '_' ) {
×
809
                                continue;
×
810
                        }
811

812
                        // Skip custom fields that are not used, new ones will be fetched dynamically.
813
                        if ( ! \in_array( $custom_field_name, $fields_to_include, true ) ) {
×
814
                                continue;
×
815
                        }
816

817
                        // Skip custom field values that are serialized.
818
                        if ( \is_serialized( $custom_field[0] ) ) {
×
819
                                continue;
×
820
                        }
821

822
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
823
                }
824

825
                return $custom_replace_vars;
×
826
        }
827

828
        /**
829
         * Determines the scope based on the post type.
830
         * This can be used by the replacevar plugin to determine if a replacement needs to be executed.
831
         *
832
         * @return string String describing the current scope.
833
         */
834
        protected function determine_scope() {
×
835
                if ( $this->get_metabox_post()->post_type === 'page' ) {
×
836
                        return 'page';
×
837
                }
838

839
                return 'post';
×
840
        }
841

842
        /**
843
         * Determines whether or not the current post type has registered taxonomies.
844
         *
845
         * @return bool Whether the current post type has taxonomies.
846
         */
847
        protected function current_post_type_has_taxonomies() {
×
848
                $post_taxonomies = \get_object_taxonomies( $this->get_metabox_post()->post_type );
×
849

850
                return ! empty( $post_taxonomies );
×
851
        }
852

853
        /**
854
         * Returns an array with shortcode tags for all registered shortcodes.
855
         *
856
         * @return array
857
         */
858
        protected function get_valid_shortcode_tags() {
×
859
                $shortcode_tags = [];
×
860

861
                foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
×
862
                        $shortcode_tags[] = $tag;
×
863
                }
864

865
                return $shortcode_tags;
×
866
        }
867
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc