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

Yoast / wordpress-seo / ded610fa0683f4de57873eeeed816009fcac5d5a

08 Aug 2024 09:23AM UTC coverage: 48.699% (-5.4%) from 54.068%
ded610fa0683f4de57873eeeed816009fcac5d5a

push

github

web-flow
Merge pull request #21522 from Yoast/1781-minor-233-update-notice-stays-on-yoast-tab-even-if-the-changes-are-saved

Elementor: fix timing issue after save and before closing the document

7479 of 13538 branches covered (55.24%)

Branch coverage included in aggregate %.

0 of 18 new or added lines in 1 file covered. (0.0%)

4422 existing lines in 144 files now uncovered.

25345 of 53864 relevant lines covered (47.05%)

42394.21 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_Language_Utils;
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_Shortlinker;
18
use WPSEO_Utils;
19
use Yoast\WP\SEO\Conditionals\Third_Party\Elementor_Edit_Conditional;
20
use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional;
21
use Yoast\WP\SEO\Editors\Application\Site\Website_Information_Repository;
22
use Yoast\WP\SEO\Elementor\Infrastructure\Request_Post;
23
use Yoast\WP\SEO\Helpers\Capability_Helper;
24
use Yoast\WP\SEO\Helpers\Options_Helper;
25
use Yoast\WP\SEO\Integrations\Integration_Interface;
26
use Yoast\WP\SEO\Presenters\Admin\Meta_Fields_Presenter;
27
use Yoast\WP\SEO\Promotions\Application\Promotion_Manager;
28

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

171
                \add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'init' ] );
×
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
        // Below is mostly copied from `class-metabox.php`. That constructor has side-effects we do not need.
186

187
        /**
188
         * Determines whether the metabox should be shown for the passed identifier.
189
         *
190
         * By default, the check is done for post types, but can also be used for taxonomies.
191
         *
192
         * @param string|null $identifier The identifier to check.
193
         * @param string      $type       The type of object to check. Defaults to post_type.
194
         *
195
         * @return bool Whether the metabox should be displayed.
196
         */
197
        public function display_metabox( $identifier = null, $type = 'post_type' ) {
×
198
                return WPSEO_Utils::is_metabox_active( $identifier, $type );
×
199
        }
200

201
        /**
202
         * Saves the WP SEO metadata for posts.
203
         *
204
         * Outputs JSON via wp_send_json then stops code execution.
205
         *
206
         * {@internal $_POST parameters are validated via sanitize_post_meta().}}
207
         *
208
         * @return void
209
         */
210
        public function save_postdata() {
×
211
                global $post;
×
212

213
                if ( ! isset( $_POST['post_id'] ) || ! \is_string( $_POST['post_id'] ) ) {
×
214
                        \wp_send_json_error( 'Bad Request', 400 );
×
215
                }
216

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

220
                if ( $post_id <= 0 ) {
×
221
                        \wp_send_json_error( 'Bad Request', 400 );
×
222
                }
223

224
                if ( ! \current_user_can( 'edit_post', $post_id ) ) {
×
225
                        \wp_send_json_error( 'Forbidden', 403 );
×
226
                }
227

228
                \check_ajax_referer( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
×
229

230
                // Bail if this is a multisite installation and the site has been switched.
231
                if ( \is_multisite() && \ms_is_switched() ) {
×
232
                        \wp_send_json_error( 'Switched multisite', 409 );
×
233
                }
234

235
                \clean_post_cache( $post_id );
×
236
                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- To setup the post we need to do this explicitly.
237
                $post = \get_post( $post_id );
×
238

239
                if ( ! \is_object( $post ) ) {
×
240
                        // Non-existent post.
241
                        \wp_send_json_error( 'Post not found', 400 );
×
242
                }
243

244
                \do_action( 'wpseo_save_compare_data', $post );
×
245

246
                // Initialize meta, amongst other things it registers sanitization.
247
                WPSEO_Meta::init();
×
248

249
                $social_fields = [];
×
250
                if ( $this->social_is_enabled ) {
×
251
                        $social_fields = WPSEO_Meta::get_meta_field_defs( 'social', $post->post_type );
×
252
                }
253

254
                // The below methods use the global post so make sure it is setup.
255
                \setup_postdata( $post );
×
256
                $meta_boxes = \apply_filters( 'wpseo_save_metaboxes', [] );
×
257
                $meta_boxes = \array_merge(
×
258
                        $meta_boxes,
×
259
                        WPSEO_Meta::get_meta_field_defs( 'general', $post->post_type ),
×
260
                        WPSEO_Meta::get_meta_field_defs( 'advanced', $post->post_type ),
×
261
                        $social_fields,
×
262
                        WPSEO_Meta::get_meta_field_defs( 'schema', $post->post_type )
×
UNCOV
263
                );
×
264

265
                foreach ( $meta_boxes as $key => $meta_box ) {
×
266
                        // If analysis is disabled remove that analysis score value from the DB.
267
                        if ( $this->is_meta_value_disabled( $key ) ) {
×
268
                                WPSEO_Meta::delete( $key, $post_id );
×
269
                                continue;
×
270
                        }
271

272
                        $data       = null;
×
273
                        $field_name = WPSEO_Meta::$form_prefix . $key;
×
274

275
                        if ( $meta_box['type'] === 'checkbox' ) {
×
276
                                $data = isset( $_POST[ $field_name ] ) ? 'on' : 'off';
×
277
                        }
278
                        else {
279
                                if ( isset( $_POST[ $field_name ] ) ) {
×
280
                                        // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: Sanitized through sanitize_post_meta.
281
                                        $data = \wp_unslash( $_POST[ $field_name ] );
×
282

283
                                        // For multi-select.
284
                                        if ( \is_array( $data ) ) {
×
285
                                                $data = \array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], $data );
×
286
                                        }
287

288
                                        if ( \is_string( $data ) ) {
×
289
                                                $data = ( $key !== 'canonical' ) ? WPSEO_Utils::sanitize_text_field( $data ) : WPSEO_Utils::sanitize_url( $data );
×
290
                                        }
291
                                }
292

293
                                // Reset options when no entry is present with multiselect - only applies to `meta-robots-adv` currently.
294
                                if ( ! isset( $_POST[ $field_name ] ) && ( $meta_box['type'] === 'multiselect' ) ) {
×
295
                                        $data = [];
×
296
                                }
297
                        }
298

299
                        if ( $data !== null ) {
×
300
                                WPSEO_Meta::set_value( $key, $data, $post_id );
×
301
                        }
302
                }
303

304
                if ( isset( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) && \is_string( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) ) {
×
305
                        $slug = \sanitize_title( \wp_unslash( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) );
×
306
                        if ( $post->post_name !== $slug ) {
×
307
                                $post_array              = $post->to_array();
×
308
                                $post_array['post_name'] = $slug;
×
309

310
                                $save_successful = \wp_insert_post( $post_array );
×
311
                                if ( \is_wp_error( $save_successful ) ) {
×
312
                                        \wp_send_json_error( 'Slug not saved', 400 );
×
313
                                }
314

315
                                // Update the post object to ensure we have the actual slug.
316
                                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Updating the post is needed to get the current slug.
317
                                $post = \get_post( $post_id );
×
318
                                if ( ! \is_object( $post ) ) {
×
319
                                        \wp_send_json_error( 'Updated slug not found', 400 );
×
320
                                }
321
                        }
322
                }
323

324
                \do_action( 'wpseo_saved_postdata' );
×
325

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

330
        /**
331
         * Determines if the given meta value key is disabled.
332
         *
333
         * @param string $key The key of the meta value.
334
         *
335
         * @return bool Whether the given meta value key is disabled.
336
         */
337
        public function is_meta_value_disabled( $key ) {
×
338
                if ( $key === 'linkdex' && ! $this->seo_analysis->is_enabled() ) {
×
339
                        return true;
×
340
                }
341

342
                if ( $key === 'content_score' && ! $this->readability_analysis->is_enabled() ) {
×
343
                        return true;
×
344
                }
345

346
                if ( $key === 'inclusive_language_score' && ! $this->inclusive_language_analysis->is_enabled() ) {
×
347
                        return true;
×
348
                }
349

350
                return false;
×
351
        }
352

353
        /**
354
         * Enqueues all the needed JS and CSS.
355
         *
356
         * @return void
357
         */
358
        public function enqueue() {
×
359
                $post_id = \get_queried_object_id();
×
360
                if ( empty( $post_id ) ) {
×
361
                        $post_id = 0;
×
362
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
363
                        if ( isset( $_GET['post'] ) && \is_string( $_GET['post'] ) ) {
×
364
                                // 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.
365
                                $post_id = (int) \wp_unslash( $_GET['post'] );
×
366
                        }
367
                }
368

369
                if ( $post_id !== 0 ) {
×
370
                        // Enqueue files needed for upload functionality.
371
                        \wp_enqueue_media( [ 'post' => $post_id ] );
×
372
                }
373

374
                $this->asset_manager->enqueue_style( 'admin-global' );
×
375
                $this->asset_manager->enqueue_style( 'metabox-css' );
×
376
                $this->asset_manager->enqueue_style( 'scoring' );
×
377
                $this->asset_manager->enqueue_style( 'monorepo' );
×
378
                $this->asset_manager->enqueue_style( 'admin-css' );
×
379
                $this->asset_manager->enqueue_style( 'ai-generator' );
×
380
                $this->asset_manager->enqueue_style( 'elementor' );
×
381

382
                $this->asset_manager->enqueue_script( 'admin-global' );
×
383
                $this->asset_manager->enqueue_script( 'elementor' );
×
384

385
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminGlobalL10n', \YoastSEO()->helpers->wincher->get_admin_global_links() );
×
386
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
×
387
                $this->asset_manager->localize_script( 'elementor', 'wpseoFeaturesL10n', WPSEO_Utils::retrieve_enabled_features() );
×
388

UNCOV
389
                $plugins_script_data = [
×
UNCOV
390
                        'replaceVars' => [
×
391
                                'no_parent_text'           => \__( '(no parent)', 'wordpress-seo' ),
×
392
                                'replace_vars'             => $this->get_replace_vars(),
×
393
                                'recommended_replace_vars' => $this->get_recommended_replace_vars(),
×
394
                                'hidden_replace_vars'      => $this->get_hidden_replace_vars(),
×
395
                                'scope'                    => $this->determine_scope(),
×
396
                                'has_taxonomies'           => $this->current_post_type_has_taxonomies(),
×
UNCOV
397
                        ],
×
UNCOV
398
                        'shortcodes'  => [
×
399
                                'wpseo_shortcode_tags'          => $this->get_valid_shortcode_tags(),
×
400
                                'wpseo_filter_shortcodes_nonce' => \wp_create_nonce( 'wpseo-filter-shortcodes' ),
×
UNCOV
401
                        ],
×
UNCOV
402
                ];
×
403

UNCOV
404
                $worker_script_data = [
×
405
                        'url'                     => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-analysis-worker' ),
×
406
                        'dependencies'            => \YoastSEO()->helpers->asset->get_dependency_urls_by_handle( 'yoast-seo-analysis-worker' ),
×
407
                        'keywords_assessment_url' => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-used-keywords-assessment' ),
×
408
                        'log_level'               => WPSEO_Utils::get_analysis_worker_log_level(),
×
409
                        // We need to make the feature flags separately available inside of the analysis web worker.
410
                        'enabled_features'        => WPSEO_Utils::retrieve_enabled_features(),
×
UNCOV
411
                ];
×
412

413
                $woocommerce_conditional = new WooCommerce_Conditional();
×
414
                $permalink               = $this->get_permalink();
×
415

UNCOV
416
                $script_data = [
×
417
                        'media'                     => [ 'choose_image' => \__( 'Use Image', 'wordpress-seo' ) ],
×
418
                        'metabox'                   => $this->get_metabox_script_data( $permalink ),
×
419
                        'userLanguageCode'          => WPSEO_Language_Utils::get_language( \get_user_locale() ),
×
UNCOV
420
                        'isPost'                    => true,
×
421
                        'isBlockEditor'             => WP_Screen::get()->is_block_editor(),
×
UNCOV
422
                        'isElementorEditor'         => true,
×
423
                        'isWooCommerceActive'       => $woocommerce_conditional->is_met(),
×
424
                        'postStatus'                => \get_post_status( $post_id ),
×
425
                        'postType'                  => \get_post_type( $post_id ),
×
UNCOV
426
                        'analysis'                  => [
×
427
                                'plugins' => $plugins_script_data,
×
428
                                'worker'  => $worker_script_data,
×
UNCOV
429
                        ],
×
430
                        'webinarIntroElementorUrl'  => WPSEO_Shortlinker::get( 'https://yoa.st/webinar-intro-elementor' ),
×
431
                        'usedKeywordsNonce'         => \wp_create_nonce( 'wpseo-keyword-usage-and-post-types' ),
×
UNCOV
432
                ];
×
433

434
                /**
435
                 * The website information repository.
436
                 *
437
                 * @var $repo Website_Information_Repository
438
                 */
439
                $repo             = \YoastSEO()->classes->get( Website_Information_Repository::class );
×
440
                $site_information = $repo->get_post_site_information();
×
441
                $site_information->set_permalink( $permalink );
×
442
                $script_data = \array_merge_recursive( $site_information->get_legacy_site_information(), $script_data );
×
443

444
                if ( \post_type_supports( $this->get_metabox_post()->post_type, 'thumbnail' ) ) {
×
445
                        $this->asset_manager->enqueue_style( 'featured-image' );
×
446

447
                        $script_data['featuredImage'] = [
×
448
                                'featured_image_notice' => \__( 'SEO issue: The featured image should be at least 200 by 200 pixels to be picked up by Facebook and other social media sites.', 'wordpress-seo' ),
×
UNCOV
449
                        ];
×
450
                }
451

452
                $this->asset_manager->localize_script( 'elementor', 'wpseoScriptData', $script_data );
×
453
                $this->asset_manager->enqueue_user_language_script();
×
454
        }
455

456
        /**
457
         * Renders the metabox hidden fields.
458
         *
459
         * @return void
460
         */
461
        protected function render_hidden_fields() {
×
462
                // Wrap in a form with an action and post_id for the submit.
463
                \printf(
×
464
                        '<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" />',
×
465
                        \esc_url( \admin_url( 'admin-ajax.php' ) ),
×
466
                        \esc_attr( $this->get_metabox_post()->ID )
×
UNCOV
467
                );
×
468

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

473
                if ( $this->is_advanced_metadata_enabled ) {
×
474
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
475
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'advanced' );
×
476
                }
477

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

481
                if ( $this->social_is_enabled ) {
×
482
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
483
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'social' );
×
484
                }
485

486
                \printf(
×
487
                        '<input type="hidden" id="%1$s" name="%1$s" value="%2$s" />',
×
488
                        \esc_attr( WPSEO_Meta::$form_prefix . 'slug' ),
×
489
                        \esc_attr( $this->get_post_slug() )
×
UNCOV
490
                );
×
491

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

495
                echo '</form>';
×
496
        }
497

498
        /**
499
         * Returns the slug for the post being edited.
500
         *
501
         * @return string
502
         */
503
        protected function get_post_slug() {
×
504
                $post = $this->get_metabox_post();
×
505

506
                // In case get_metabox_post returns null for whatever reason.
507
                if ( ! $post instanceof WP_Post ) {
×
508
                        return '';
×
509
                }
510

511
                // Drafts might not have a post_name unless the slug has been manually changed.
512
                // In this case we get it using get_sample_permalink.
513
                if ( ! $post->post_name ) {
×
514
                        $sample = \get_sample_permalink( $post );
×
515

516
                        // Since get_sample_permalink runs through filters, ensure that it has the expected return value.
517
                        if ( \is_array( $sample ) && \count( $sample ) === 2 && \is_string( $sample[1] ) ) {
×
518
                                return $sample[1];
×
519
                        }
520
                }
521

522
                return $post->post_name;
×
523
        }
524

525
        /**
526
         * Returns post in metabox context.
527
         *
528
         * @return WP_Post|null
529
         */
530
        protected function get_metabox_post() {
×
531
                if ( $this->post !== null ) {
×
532
                        return $this->post;
×
533
                }
534

535
                $this->post = $this->request_post->get_post();
×
536

537
                return $this->post;
×
538
        }
539

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

552
                $values = $post_formatter->get_values();
×
553

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

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

562
                return $values;
×
563
        }
564

565
        /**
566
         * Gets the permalink.
567
         *
568
         * @return string
569
         */
570
        protected function get_permalink(): string {
×
571
                $permalink = '';
×
572

573
                if ( \is_object( $this->get_metabox_post() ) ) {
×
574
                        $permalink = \get_sample_permalink( $this->get_metabox_post()->ID );
×
575
                        $permalink = $permalink[0];
×
576
                }
577

578
                return $permalink;
×
579
        }
580

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

592
                return ! $is_premium || \version_compare( $premium_version, '21.8-RC0', '>=' );
×
593
        }
594

595
        /**
596
         * Prepares the replace vars for localization.
597
         *
598
         * @return array Replace vars.
599
         */
600
        protected function get_replace_vars() {
×
601
                $cached_replacement_vars = [];
×
602

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

634
                foreach ( $vars_to_cache as $var ) {
×
635
                        $cached_replacement_vars[ $var ] = \wpseo_replace_vars( '%%' . $var . '%%', $this->get_metabox_post() );
×
636
                }
637

638
                // Merge custom replace variables with the WordPress ones.
639
                return \array_merge( $cached_replacement_vars, $this->get_custom_replace_vars( $this->get_metabox_post() ) );
×
640
        }
641

642
        /**
643
         * Prepares the recommended replace vars for localization.
644
         *
645
         * @return array Recommended replacement variables.
646
         */
647
        protected function get_recommended_replace_vars() {
×
648
                $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
×
649

650
                // What is recommended depends on the current context.
651
                $post_type = $recommended_replace_vars->determine_for_post( $this->get_metabox_post() );
×
652

653
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
654
        }
655

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

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

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

690
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
691

692
                        if ( \is_string( $taxonomy ) ) { // If attachment, see https://core.trac.wordpress.org/ticket/37368 .
×
693
                                $taxonomy_name = $taxonomy;
×
694
                                $taxonomy      = \get_taxonomy( $taxonomy_name );
×
695
                        }
696

697
                        if ( $taxonomy->_builtin && $taxonomy->public ) {
×
698
                                continue;
×
699
                        }
700

701
                        $custom_replace_vars[ $taxonomy_name ] = [
×
702
                                'name'        => $taxonomy->name,
×
703
                                'description' => $taxonomy->description,
×
UNCOV
704
                        ];
×
705
                }
706

707
                return $custom_replace_vars;
×
708
        }
709

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

720
                // If no post object is passed, return the empty custom_replace_vars array.
721
                if ( ! \is_object( $post ) ) {
×
722
                        return $custom_replace_vars;
×
723
                }
724

725
                $custom_fields = \get_post_custom( $post->ID );
×
726

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

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

744
                        // Skip custom fields that are not used, new ones will be fetched dynamically.
745
                        if ( ! \in_array( $custom_field_name, $fields_to_include, true ) ) {
×
746
                                continue;
×
747
                        }
748

749
                        // Skip custom field values that are serialized.
750
                        if ( \is_serialized( $custom_field[0] ) ) {
×
751
                                continue;
×
752
                        }
753

754
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
755
                }
756

757
                return $custom_replace_vars;
×
758
        }
759

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

771
                return 'post';
×
772
        }
773

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

782
                return ! empty( $post_taxonomies );
×
783
        }
784

785
        /**
786
         * Returns an array with shortcode tags for all registered shortcodes.
787
         *
788
         * @return array
789
         */
790
        protected function get_valid_shortcode_tags() {
×
791
                $shortcode_tags = [];
×
792

793
                foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
×
794
                        $shortcode_tags[] = $tag;
×
795
                }
796

797
                return $shortcode_tags;
×
798
        }
799
}
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