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

Yoast / wordpress-seo / 462e0f71f9446f2042110d8a77728188c469f94f

12 Nov 2025 12:42PM UTC coverage: 53.287% (-0.1%) from 53.388%
462e0f71f9446f2042110d8a77728188c469f94f

Pull #22697

github

web-flow
Merge d79ee2f1a into b92ba24f1
Pull Request #22697: Show yoast in elements tab in elementor

8502 of 15757 branches covered (53.96%)

Branch coverage included in aggregate %.

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

93 existing lines in 8 files now uncovered.

32208 of 60641 relevant lines covered (53.11%)

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

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

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

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

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

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

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

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

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

251
                \check_ajax_referer( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
×
252

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

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

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

267
                \do_action( 'wpseo_save_compare_data', $post );
×
268

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

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

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

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

295
                        $data       = null;
×
296
                        $field_name = WPSEO_Meta::$form_prefix . $key;
×
297

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

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

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

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

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

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

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

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

347
                \do_action( 'wpseo_saved_postdata' );
×
348

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

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

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

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

373
                return false;
×
374
        }
375

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

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

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

407
                $this->asset_manager->enqueue_script( 'admin-global' );
×
408
                $this->asset_manager->enqueue_script( 'elementor' );
×
409

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

536
                echo '</form>';
×
537
        }
538

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

549
                $this->post = $this->request_post->get_post();
×
550

551
                return $this->post;
×
552
        }
553

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

566
                $values = $post_formatter->get_values();
×
567

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

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

576
                return $values;
×
577
        }
578

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

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

592
                return $permalink;
×
593
        }
594

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

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

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

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

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

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

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

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

667
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
668
        }
669

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

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

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

704
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
705

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

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

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

721
                return $custom_replace_vars;
×
722
        }
723

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

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

739
                $custom_fields = \get_post_custom( $post->ID );
×
740

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

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

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

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

768
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
769
                }
770

771
                return $custom_replace_vars;
×
772
        }
773

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

785
                return 'post';
×
786
        }
787

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

796
                return ! empty( $post_taxonomies );
×
797
        }
798

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

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

811
                return $shortcode_tags;
×
812
        }
813
}
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