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

Yoast / wordpress-seo / 8a623e5726e762ee356a5d62655dc07143b1be70

22 Sep 2025 02:26PM UTC coverage: 52.716% (-0.5%) from 53.205%
8a623e5726e762ee356a5d62655dc07143b1be70

Pull #22440

github

web-flow
Merge afb6d32ba into 1559468c9
Pull Request #22440: Re organize ai code

8079 of 15197 branches covered (53.16%)

Branch coverage included in aggregate %.

20 of 40 new or added lines in 8 files covered. (50.0%)

437 existing lines in 12 files now uncovered.

31227 of 59365 relevant lines covered (52.6%)

40219.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/integrations/third-party/elementor.php
1
<?php
2

3
namespace Yoast\WP\SEO\Integrations\Third_Party;
4

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

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

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

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

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

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

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

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

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

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

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

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

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

108
        /**
109
         * Whether the insights feature is enabled.
110
         *
111
         * @var bool
112
         */
113
        protected $is_insights_enabled;
114

115
        /**
116
         * Whether the cornerstone content feature is enabled.
117
         *
118
         * @var bool
119
         */
120
        protected $is_cornerstone_enabled;
121

122
        /**
123
         * Returns the conditionals based in which this loadable should be active.
124
         *
125
         * @return array
126
         */
127
        public static function get_conditionals() {
×
128
                return [ Elementor_Edit_Conditional::class ];
×
129
        }
130

131
        /**
132
         * Constructor.
133
         *
134
         * @param WPSEO_Admin_Asset_Manager $asset_manager The asset manager.
135
         * @param Options_Helper            $options       The options helper.
136
         * @param Capability_Helper         $capability    The capability helper.
137
         * @param Request_Post              $request_post  The Request_Post.
138
         */
139
        public function __construct(
×
140
                WPSEO_Admin_Asset_Manager $asset_manager,
141
                Options_Helper $options,
142
                Capability_Helper $capability,
143
                Request_Post $request_post
144
        ) {
145
                $this->asset_manager = $asset_manager;
×
146
                $this->options       = $options;
×
147
                $this->capability    = $capability;
×
148
                $this->request_post  = $request_post;
×
149

150
                $this->seo_analysis                 = new WPSEO_Metabox_Analysis_SEO();
×
151
                $this->readability_analysis         = new WPSEO_Metabox_Analysis_Readability();
×
152
                $this->inclusive_language_analysis  = new WPSEO_Metabox_Analysis_Inclusive_Language();
×
153
                $this->social_is_enabled            = $this->options->get( 'opengraph', false ) || $this->options->get( 'twitter', false );
×
154
                $this->is_advanced_metadata_enabled = $this->capability->current_user_can( 'wpseo_edit_advanced_metadata' ) || $this->options->get( 'disableadvanced_meta' ) === false;
×
155
                $this->is_insights_enabled          = $this->options->get( 'enable_metabox_insights', false );
×
156
                $this->is_cornerstone_enabled       = $this->options->get( 'enable_cornerstone_content', false );
×
157
        }
158

159
        /**
160
         * Initializes the integration.
161
         *
162
         * This is the place to register hooks and filters.
163
         *
164
         * @return void
165
         */
166
        public function register_hooks() {
×
167
                \add_action( 'wp_ajax_wpseo_elementor_save', [ $this, 'save_postdata' ] );
×
168

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

173
        /**
174
         * Registers our Elementor hooks.
175
         * This is done for pages with metabox on page load and not on ajax request.
176
         *
177
         * @return void
178
         */
179
        public function register_elementor_hooks() {
×
180
                if ( $this->get_metabox_post() === null || ! $this->display_metabox( $this->get_metabox_post()->post_type ) ) {
×
181
                        return;
×
182
                }
183

184
                \add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'init' ] );
×
185
        }
186

187
        /**
188
         * Initializes the integration.
189
         *
190
         * @return void
191
         */
192
        public function init() {
×
193
                $this->asset_manager->register_assets();
×
194
                $this->enqueue();
×
195
                $this->render_hidden_fields();
×
196
        }
197

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

200
        /**
201
         * Determines whether the metabox should be shown for the passed identifier.
202
         *
203
         * By default, the check is done for post types, but can also be used for taxonomies.
204
         *
205
         * @param string|null $identifier The identifier to check.
206
         * @param string      $type       The type of object to check. Defaults to post_type.
207
         *
208
         * @return bool Whether the metabox should be displayed.
209
         */
210
        public function display_metabox( $identifier = null, $type = 'post_type' ) {
×
211
                return WPSEO_Utils::is_metabox_active( $identifier, $type );
×
212
        }
213

214
        /**
215
         * Saves the WP SEO metadata for posts.
216
         *
217
         * Outputs JSON via wp_send_json then stops code execution.
218
         *
219
         * {@internal $_POST parameters are validated via sanitize_post_meta().}}
220
         *
221
         * @return void
222
         */
223
        public function save_postdata() {
×
224
                global $post;
×
225

226
                if ( ! isset( $_POST['post_id'] ) || ! \is_string( $_POST['post_id'] ) ) {
×
227
                        \wp_send_json_error( 'Bad Request', 400 );
×
228
                }
229

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

233
                if ( $post_id <= 0 ) {
×
234
                        \wp_send_json_error( 'Bad Request', 400 );
×
235
                }
236

237
                if ( ! \current_user_can( 'edit_post', $post_id ) ) {
×
238
                        \wp_send_json_error( 'Forbidden', 403 );
×
239
                }
240

241
                \check_ajax_referer( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
×
242

243
                // Bail if this is a multisite installation and the site has been switched.
244
                if ( \is_multisite() && \ms_is_switched() ) {
×
245
                        \wp_send_json_error( 'Switched multisite', 409 );
×
246
                }
247

248
                \clean_post_cache( $post_id );
×
249
                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- To setup the post we need to do this explicitly.
250
                $post = \get_post( $post_id );
×
251

252
                if ( ! \is_object( $post ) ) {
×
253
                        // Non-existent post.
254
                        \wp_send_json_error( 'Post not found', 400 );
×
255
                }
256

257
                \do_action( 'wpseo_save_compare_data', $post );
×
258

259
                // Initialize meta, amongst other things it registers sanitization.
260
                WPSEO_Meta::init();
×
261

262
                $social_fields = [];
×
263
                if ( $this->social_is_enabled ) {
×
264
                        $social_fields = WPSEO_Meta::get_meta_field_defs( 'social', $post->post_type );
×
265
                }
266

267
                // The below methods use the global post so make sure it is setup.
268
                \setup_postdata( $post );
×
269
                $meta_boxes = \apply_filters( 'wpseo_save_metaboxes', [] );
×
270
                $meta_boxes = \array_merge(
×
271
                        $meta_boxes,
×
272
                        WPSEO_Meta::get_meta_field_defs( 'general', $post->post_type ),
×
273
                        WPSEO_Meta::get_meta_field_defs( 'advanced', $post->post_type ),
×
274
                        $social_fields,
×
275
                        WPSEO_Meta::get_meta_field_defs( 'schema', $post->post_type )
×
276
                );
×
277

278
                foreach ( $meta_boxes as $key => $meta_box ) {
×
279
                        // If analysis is disabled remove that analysis score value from the DB.
280
                        if ( $this->is_meta_value_disabled( $key ) ) {
×
281
                                WPSEO_Meta::delete( $key, $post_id );
×
282
                                continue;
×
283
                        }
284

285
                        $data       = null;
×
286
                        $field_name = WPSEO_Meta::$form_prefix . $key;
×
287

288
                        if ( $meta_box['type'] === 'checkbox' ) {
×
289
                                $data = isset( $_POST[ $field_name ] ) ? 'on' : 'off';
×
290
                        }
291
                        else {
292
                                if ( isset( $_POST[ $field_name ] ) ) {
×
293
                                        // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: Sanitized through sanitize_post_meta.
294
                                        $data = \wp_unslash( $_POST[ $field_name ] );
×
295

296
                                        // For multi-select.
297
                                        if ( \is_array( $data ) ) {
×
298
                                                $data = \array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], $data );
×
299
                                        }
300

301
                                        if ( \is_string( $data ) ) {
×
302
                                                $data = ( $key !== 'canonical' ) ? WPSEO_Utils::sanitize_text_field( $data ) : WPSEO_Utils::sanitize_url( $data );
×
303
                                        }
304
                                }
305

306
                                // Reset options when no entry is present with multiselect - only applies to `meta-robots-adv` currently.
307
                                if ( ! isset( $_POST[ $field_name ] ) && ( $meta_box['type'] === 'multiselect' ) ) {
×
308
                                        $data = [];
×
309
                                }
310
                        }
311

312
                        if ( $data !== null ) {
×
313
                                WPSEO_Meta::set_value( $key, $data, $post_id );
×
314
                        }
315
                }
316

317
                if ( isset( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) && \is_string( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) ) {
×
318
                        $slug = \sanitize_title( \wp_unslash( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) );
×
319
                        if ( $post->post_name !== $slug ) {
×
320
                                $post_array              = $post->to_array();
×
321
                                $post_array['post_name'] = $slug;
×
322

323
                                $save_successful = \wp_insert_post( $post_array );
×
324
                                if ( \is_wp_error( $save_successful ) ) {
×
325
                                        \wp_send_json_error( 'Slug not saved', 400 );
×
326
                                }
327

328
                                // Update the post object to ensure we have the actual slug.
329
                                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Updating the post is needed to get the current slug.
330
                                $post = \get_post( $post_id );
×
331
                                if ( ! \is_object( $post ) ) {
×
332
                                        \wp_send_json_error( 'Updated slug not found', 400 );
×
333
                                }
334
                        }
335
                }
336

337
                \do_action( 'wpseo_saved_postdata' );
×
338

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

343
        /**
344
         * Determines if the given meta value key is disabled.
345
         *
346
         * @param string $key The key of the meta value.
347
         *
348
         * @return bool Whether the given meta value key is disabled.
349
         */
350
        public function is_meta_value_disabled( $key ) {
×
351
                if ( $key === 'linkdex' && ! $this->seo_analysis->is_enabled() ) {
×
352
                        return true;
×
353
                }
354

355
                if ( $key === 'content_score' && ! $this->readability_analysis->is_enabled() ) {
×
356
                        return true;
×
357
                }
358

359
                if ( $key === 'inclusive_language_score' && ! $this->inclusive_language_analysis->is_enabled() ) {
×
360
                        return true;
×
361
                }
362

363
                return false;
×
364
        }
365

366
        /**
367
         * Enqueues all the needed JS and CSS.
368
         *
369
         * @return void
370
         */
371
        public function enqueue() {
×
372
                $post_id = \get_queried_object_id();
×
373
                if ( empty( $post_id ) ) {
×
374
                        $post_id = 0;
×
375
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
376
                        if ( isset( $_GET['post'] ) && \is_string( $_GET['post'] ) ) {
×
377
                                // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized,WordPress.Security.NonceVerification.Recommended -- Reason: No sanitization needed because we cast to an integer,We are not processing form information.
378
                                $post_id = (int) \wp_unslash( $_GET['post'] );
×
379
                        }
380
                }
381

382
                if ( $post_id !== 0 ) {
×
383
                        // Enqueue files needed for upload functionality.
384
                        \wp_enqueue_media( [ 'post' => $post_id ] );
×
385
                }
386

387
                $this->asset_manager->enqueue_style( 'admin-global' );
×
388
                $this->asset_manager->enqueue_style( 'metabox-css' );
×
389
                if ( $this->readability_analysis->is_enabled() ) {
×
390
                        $this->asset_manager->enqueue_style( 'scoring' );
×
391
                }
392
                $this->asset_manager->enqueue_style( 'monorepo' );
×
393
                $this->asset_manager->enqueue_style( 'admin-css' );
×
394
                $this->asset_manager->enqueue_style( 'ai-generator' );
×
395
                $this->asset_manager->enqueue_style( 'elementor' );
×
396

397
                $this->asset_manager->enqueue_script( 'admin-global' );
×
398
                $this->asset_manager->enqueue_script( 'elementor' );
×
399

400
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminGlobalL10n', \YoastSEO()->helpers->wincher->get_admin_global_links() );
×
401
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
×
402
                $this->asset_manager->localize_script( 'elementor', 'wpseoFeaturesL10n', WPSEO_Utils::retrieve_enabled_features() );
×
403

404
                $plugins_script_data = [
×
405
                        'replaceVars' => [
×
406
                                'replace_vars'             => $this->get_replace_vars(),
×
407
                                'recommended_replace_vars' => $this->get_recommended_replace_vars(),
×
408
                                'hidden_replace_vars'      => $this->get_hidden_replace_vars(),
×
409
                                'scope'                    => $this->determine_scope(),
×
410
                                'has_taxonomies'           => $this->current_post_type_has_taxonomies(),
×
411
                        ],
×
412
                        'shortcodes'  => [
×
413
                                'wpseo_shortcode_tags'          => $this->get_valid_shortcode_tags(),
×
414
                                'wpseo_filter_shortcodes_nonce' => \wp_create_nonce( 'wpseo-filter-shortcodes' ),
×
415
                        ],
×
416
                ];
×
417

418
                $worker_script_data = [
×
419
                        'url'                     => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-analysis-worker' ),
×
420
                        'dependencies'            => \YoastSEO()->helpers->asset->get_dependency_urls_by_handle( 'yoast-seo-analysis-worker' ),
×
421
                        'keywords_assessment_url' => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-used-keywords-assessment' ),
×
422
                        'log_level'               => WPSEO_Utils::get_analysis_worker_log_level(),
×
423
                        // We need to make the feature flags separately available inside of the analysis web worker.
424
                        'enabled_features'        => WPSEO_Utils::retrieve_enabled_features(),
×
425
                ];
×
426

427
                $permalink        = $this->get_permalink();
×
428
                $page_on_front    = (int) \get_option( 'page_on_front' );
×
429
                $homepage_is_page = \get_option( 'show_on_front' ) === 'page';
×
430
                $is_front_page    = $homepage_is_page && $page_on_front === $post_id;
×
431

432
                $script_data = [
×
433
                        'metabox'                   => $this->get_metabox_script_data( $permalink ),
×
434
                        'isPost'                    => true,
×
435
                        'isBlockEditor'             => WP_Screen::get()->is_block_editor(),
×
436
                        'isElementorEditor'         => true,
×
437
                        'isAlwaysIntroductionV2'    => $this->is_elementor_version_compatible_with_introduction_v2(),
×
438
                        'postStatus'                => \get_post_status( $post_id ),
×
439
                        'postType'                  => \get_post_type( $post_id ),
×
440
                        'analysis'                  => [
×
441
                                'plugins' => $plugins_script_data,
×
442
                                'worker'  => $worker_script_data,
×
443
                        ],
×
444
                        'usedKeywordsNonce'         => \wp_create_nonce( 'wpseo-keyword-usage-and-post-types' ),
×
445
                        'isFrontPage'               => $is_front_page,
×
446
                ];
×
447

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

458
                $this->asset_manager->localize_script( 'elementor', 'wpseoScriptData', $script_data );
×
459
        }
460

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

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

478
                // 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.
479
                return \version_compare( $version, '3.30.0', '>=' );
×
480
        }
481

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

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

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

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

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

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

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

UNCOV
526
                echo '</form>';
×
527
        }
528

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

539
                $this->post = $this->request_post->get_post();
×
540

UNCOV
541
                return $this->post;
×
542
        }
543

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

556
                $values = $post_formatter->get_values();
×
557

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

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

UNCOV
566
                return $values;
×
567
        }
568

569
        /**
570
         * Gets the permalink.
571
         *
572
         * @return string
573
         */
UNCOV
574
        protected function get_permalink(): string {
×
UNCOV
575
                $permalink = '';
×
576

577
                if ( \is_object( $this->get_metabox_post() ) ) {
×
578
                        $permalink = \get_sample_permalink( $this->get_metabox_post()->ID );
×
UNCOV
579
                        $permalink = $permalink[0];
×
580
                }
581

582
                return $permalink;
×
583
        }
584

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

596
                return ! $is_premium || \version_compare( $premium_version, '21.8-RC0', '>=' );
×
597
        }
598

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

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

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

642
                // Merge custom replace variables with the WordPress ones.
UNCOV
643
                return \array_merge( $cached_replacement_vars, $this->get_custom_replace_vars( $this->get_metabox_post() ) );
×
644
        }
645

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

654
                // What is recommended depends on the current context.
655
                $post_type = $recommended_replace_vars->determine_for_post( $this->get_metabox_post() );
×
656

UNCOV
657
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
658
        }
659

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

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

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

694
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
695

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

701
                        if ( $taxonomy->_builtin && $taxonomy->public ) {
×
UNCOV
702
                                continue;
×
703
                        }
704

705
                        $custom_replace_vars[ $taxonomy_name ] = [
×
UNCOV
706
                                'name'        => $taxonomy->name,
×
UNCOV
707
                                'description' => $taxonomy->description,
×
708
                        ];
×
709
                }
710

711
                return $custom_replace_vars;
×
712
        }
713

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

724
                // If no post object is passed, return the empty custom_replace_vars array.
725
                if ( ! \is_object( $post ) ) {
×
UNCOV
726
                        return $custom_replace_vars;
×
727
                }
728

729
                $custom_fields = \get_post_custom( $post->ID );
×
730

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

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

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

753
                        // Skip custom field values that are serialized.
UNCOV
754
                        if ( \is_serialized( $custom_field[0] ) ) {
×
UNCOV
755
                                continue;
×
756
                        }
757

758
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
759
                }
760

761
                return $custom_replace_vars;
×
762
        }
763

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

775
                return 'post';
×
776
        }
777

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

786
                return ! empty( $post_taxonomies );
×
787
        }
788

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

797
                foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
×
798
                        $shortcode_tags[] = $tag;
×
799
                }
800

801
                return $shortcode_tags;
×
802
        }
803
}
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