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

Yoast / wordpress-seo / a8f33c02f78e5d12ba12155dc79d01d3213cf3c8

21 Mar 2025 09:31AM UTC coverage: 53.637% (-1.3%) from 54.892%
a8f33c02f78e5d12ba12155dc79d01d3213cf3c8

Pull #22077

github

enricobattocchi
Update composer.lock
Pull Request #22077: Drop compatibility with PHP 7.2 and 7.3

7988 of 14099 branches covered (56.66%)

Branch coverage included in aggregate %.

30726 of 58079 relevant lines covered (52.9%)

41058.51 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' ] );
×
169
        }
170

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

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

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

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

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

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

217
                if ( $post_id <= 0 ) {
×
218
                        \wp_send_json_error( 'Bad Request', 400 );
×
219
                }
220

221
                if ( ! \current_user_can( 'edit_post', $post_id ) ) {
×
222
                        \wp_send_json_error( 'Forbidden', 403 );
×
223
                }
224

225
                \check_ajax_referer( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
×
226

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

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

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

241
                \do_action( 'wpseo_save_compare_data', $post );
×
242

243
                // Initialize meta, amongst other things it registers sanitization.
244
                WPSEO_Meta::init();
×
245

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

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

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

269
                        $data       = null;
×
270
                        $field_name = WPSEO_Meta::$form_prefix . $key;
×
271

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

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

285
                                        if ( \is_string( $data ) ) {
×
286
                                                $data = ( $key !== 'canonical' ) ? WPSEO_Utils::sanitize_text_field( $data ) : WPSEO_Utils::sanitize_url( $data );
×
287
                                        }
288
                                }
289

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

296
                        if ( $data !== null ) {
×
297
                                WPSEO_Meta::set_value( $key, $data, $post_id );
×
298
                        }
299
                }
300

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

307
                                $save_successful = \wp_insert_post( $post_array );
×
308
                                if ( \is_wp_error( $save_successful ) ) {
×
309
                                        \wp_send_json_error( 'Slug not saved', 400 );
×
310
                                }
311

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

321
                \do_action( 'wpseo_saved_postdata' );
×
322

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

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

339
                if ( $key === 'content_score' && ! $this->readability_analysis->is_enabled() ) {
×
340
                        return true;
×
341
                }
342

343
                if ( $key === 'inclusive_language_score' && ! $this->inclusive_language_analysis->is_enabled() ) {
×
344
                        return true;
×
345
                }
346

347
                return false;
×
348
        }
349

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

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

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

379
                $this->asset_manager->enqueue_script( 'admin-global' );
×
380
                $this->asset_manager->enqueue_script( 'elementor' );
×
381

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

386
                $plugins_script_data = [
×
387
                        'replaceVars' => [
×
388
                                'replace_vars'             => $this->get_replace_vars(),
×
389
                                'recommended_replace_vars' => $this->get_recommended_replace_vars(),
×
390
                                'hidden_replace_vars'      => $this->get_hidden_replace_vars(),
×
391
                                'scope'                    => $this->determine_scope(),
×
392
                                'has_taxonomies'           => $this->current_post_type_has_taxonomies(),
×
393
                        ],
×
394
                        'shortcodes'  => [
×
395
                                'wpseo_shortcode_tags'          => $this->get_valid_shortcode_tags(),
×
396
                                'wpseo_filter_shortcodes_nonce' => \wp_create_nonce( 'wpseo-filter-shortcodes' ),
×
397
                        ],
×
398
                ];
×
399

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

409
                $permalink        = $this->get_permalink();
×
410
                $page_on_front    = (int) \get_option( 'page_on_front' );
×
411
                $homepage_is_page = \get_option( 'show_on_front' ) === 'page';
×
412
                $is_front_page    = $homepage_is_page && $page_on_front === $post_id;
×
413

414
                $script_data = [
×
415
                        'metabox'                   => $this->get_metabox_script_data( $permalink ),
×
416
                        'isPost'                    => true,
×
417
                        'isBlockEditor'             => WP_Screen::get()->is_block_editor(),
×
418
                        'isElementorEditor'         => true,
×
419
                        'postStatus'                => \get_post_status( $post_id ),
×
420
                        'postType'                  => \get_post_type( $post_id ),
×
421
                        'analysis'                  => [
×
422
                                'plugins' => $plugins_script_data,
×
423
                                'worker'  => $worker_script_data,
×
424
                        ],
×
425
                        'usedKeywordsNonce'         => \wp_create_nonce( 'wpseo-keyword-usage-and-post-types' ),
×
426
                        'isFrontPage'               => $is_front_page,
×
427
                ];
×
428

429
                /**
430
                 * The website information repository.
431
                 *
432
                 * @var $repo Website_Information_Repository
433
                 */
434
                $repo             = \YoastSEO()->classes->get( Website_Information_Repository::class );
×
435
                $site_information = $repo->get_post_site_information();
×
436
                $site_information->set_permalink( $permalink );
×
437
                $script_data = \array_merge_recursive( $site_information->get_legacy_site_information(), $script_data );
×
438

439
                $this->asset_manager->localize_script( 'elementor', 'wpseoScriptData', $script_data );
×
440
                $this->asset_manager->enqueue_user_language_script();
×
441
        }
442

443
        /**
444
         * Renders the metabox hidden fields.
445
         *
446
         * @return void
447
         */
448
        protected function render_hidden_fields() {
×
449
                // Wrap in a form with an action and post_id for the submit.
450
                \printf(
×
451
                        '<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" />',
×
452
                        \esc_url( \admin_url( 'admin-ajax.php' ) ),
×
453
                        \esc_attr( $this->get_metabox_post()->ID )
×
454
                );
×
455

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

460
                if ( $this->is_advanced_metadata_enabled ) {
×
461
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
462
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'advanced' );
×
463
                }
464

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

468
                if ( $this->social_is_enabled ) {
×
469
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
470
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'social' );
×
471
                }
472

473
                \printf(
×
474
                        '<input type="hidden" id="%1$s" name="%1$s" value="%2$s" />',
×
475
                        \esc_attr( WPSEO_Meta::$form_prefix . 'slug' ),
×
476
                        /**
477
                         * It is important that this slug value is the same as in the database.
478
                         * If the DB value is empty we can auto-generate a slug.
479
                         * But if not empty, we should not touch it anymore.
480
                         */
481
                        \esc_attr( $this->get_metabox_post()->post_name )
×
482
                );
×
483

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

487
                echo '</form>';
×
488
        }
489

490
        /**
491
         * Returns post in metabox context.
492
         *
493
         * @return WP_Post|null
494
         */
495
        protected function get_metabox_post() {
×
496
                if ( $this->post !== null ) {
×
497
                        return $this->post;
×
498
                }
499

500
                $this->post = $this->request_post->get_post();
×
501

502
                return $this->post;
×
503
        }
504

505
        /**
506
         * Passes variables to js for use with the post-scraper.
507
         *
508
         * @param string $permalink The permalink.
509
         *
510
         * @return array
511
         */
512
        protected function get_metabox_script_data( $permalink ) {
×
513
                $post_formatter = new WPSEO_Metabox_Formatter(
×
514
                        new WPSEO_Post_Metabox_Formatter( $this->get_metabox_post(), [], $permalink )
×
515
                );
×
516

517
                $values = $post_formatter->get_values();
×
518

519
                /** This filter is documented in admin/filters/class-cornerstone-filter.php. */
520
                $post_types = \apply_filters( 'wpseo_cornerstone_post_types', \YoastSEO()->helpers->post_type->get_accessible_post_types() );
×
521
                if ( $values['cornerstoneActive'] && ! \in_array( $this->get_metabox_post()->post_type, $post_types, true ) ) {
×
522
                        $values['cornerstoneActive'] = false;
×
523
                }
524

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

527
                return $values;
×
528
        }
529

530
        /**
531
         * Gets the permalink.
532
         *
533
         * @return string
534
         */
535
        protected function get_permalink(): string {
×
536
                $permalink = '';
×
537

538
                if ( \is_object( $this->get_metabox_post() ) ) {
×
539
                        $permalink = \get_sample_permalink( $this->get_metabox_post()->ID );
×
540
                        $permalink = $permalink[0];
×
541
                }
542

543
                return $permalink;
×
544
        }
545

546
        /**
547
         * Checks whether the highlighting functionality is available for Elementor:
548
         * - in Free it's always available (as an upsell).
549
         * - in Premium it's available as long as the version is 21.8-RC0 or above.
550
         *
551
         * @return bool Whether the highlighting functionality is available.
552
         */
553
        private function is_highlighting_available() {
×
554
                $is_premium      = \YoastSEO()->helpers->product->is_premium();
×
555
                $premium_version = \YoastSEO()->helpers->product->get_premium_version();
×
556

557
                return ! $is_premium || \version_compare( $premium_version, '21.8-RC0', '>=' );
×
558
        }
559

560
        /**
561
         * Prepares the replace vars for localization.
562
         *
563
         * @return array Replace vars.
564
         */
565
        protected function get_replace_vars() {
×
566
                $cached_replacement_vars = [];
×
567

568
                $vars_to_cache = [
×
569
                        'date',
×
570
                        'id',
×
571
                        'sitename',
×
572
                        'sitedesc',
×
573
                        'sep',
×
574
                        'page',
×
575
                        'currentyear',
×
576
                        'currentdate',
×
577
                        'currentmonth',
×
578
                        'currentday',
×
579
                        'tag',
×
580
                        'category',
×
581
                        'category_title',
×
582
                        'primary_category',
×
583
                        'pt_single',
×
584
                        'pt_plural',
×
585
                        'modified',
×
586
                        'name',
×
587
                        'user_description',
×
588
                        'pagetotal',
×
589
                        'pagenumber',
×
590
                        'post_year',
×
591
                        'post_month',
×
592
                        'post_day',
×
593
                        'author_first_name',
×
594
                        'author_last_name',
×
595
                        'permalink',
×
596
                        'post_content',
×
597
                ];
×
598

599
                foreach ( $vars_to_cache as $var ) {
×
600
                        $cached_replacement_vars[ $var ] = \wpseo_replace_vars( '%%' . $var . '%%', $this->get_metabox_post() );
×
601
                }
602

603
                // Merge custom replace variables with the WordPress ones.
604
                return \array_merge( $cached_replacement_vars, $this->get_custom_replace_vars( $this->get_metabox_post() ) );
×
605
        }
606

607
        /**
608
         * Prepares the recommended replace vars for localization.
609
         *
610
         * @return array Recommended replacement variables.
611
         */
612
        protected function get_recommended_replace_vars() {
×
613
                $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
×
614

615
                // What is recommended depends on the current context.
616
                $post_type = $recommended_replace_vars->determine_for_post( $this->get_metabox_post() );
×
617

618
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
619
        }
620

621
        /**
622
         * Returns the list of replace vars that should be hidden inside the editor.
623
         *
624
         * @return string[] The hidden replace vars.
625
         */
626
        protected function get_hidden_replace_vars() {
×
627
                return ( new WPSEO_Replace_Vars() )->get_hidden_replace_vars();
×
628
        }
629

630
        /**
631
         * Gets the custom replace variables for custom taxonomies and fields.
632
         *
633
         * @param WP_Post $post The post to check for custom taxonomies and fields.
634
         *
635
         * @return array Array containing all the replacement variables.
636
         */
637
        protected function get_custom_replace_vars( $post ) {
×
638
                return [
×
639
                        'custom_fields'     => $this->get_custom_fields_replace_vars( $post ),
×
640
                        'custom_taxonomies' => $this->get_custom_taxonomies_replace_vars( $post ),
×
641
                ];
×
642
        }
643

644
        /**
645
         * Gets the custom replace variables for custom taxonomies.
646
         *
647
         * @param WP_Post $post The post to check for custom taxonomies.
648
         *
649
         * @return array Array containing all the replacement variables.
650
         */
651
        protected function get_custom_taxonomies_replace_vars( $post ) {
×
652
                $taxonomies          = \get_object_taxonomies( $post, 'objects' );
×
653
                $custom_replace_vars = [];
×
654

655
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
656

657
                        if ( \is_string( $taxonomy ) ) { // If attachment, see https://core.trac.wordpress.org/ticket/37368 .
×
658
                                $taxonomy_name = $taxonomy;
×
659
                                $taxonomy      = \get_taxonomy( $taxonomy_name );
×
660
                        }
661

662
                        if ( $taxonomy->_builtin && $taxonomy->public ) {
×
663
                                continue;
×
664
                        }
665

666
                        $custom_replace_vars[ $taxonomy_name ] = [
×
667
                                'name'        => $taxonomy->name,
×
668
                                'description' => $taxonomy->description,
×
669
                        ];
×
670
                }
671

672
                return $custom_replace_vars;
×
673
        }
674

675
        /**
676
         * Gets the custom replace variables for custom fields.
677
         *
678
         * @param WP_Post $post The post to check for custom fields.
679
         *
680
         * @return array Array containing all the replacement variables.
681
         */
682
        protected function get_custom_fields_replace_vars( $post ) {
×
683
                $custom_replace_vars = [];
×
684

685
                // If no post object is passed, return the empty custom_replace_vars array.
686
                if ( ! \is_object( $post ) ) {
×
687
                        return $custom_replace_vars;
×
688
                }
689

690
                $custom_fields = \get_post_custom( $post->ID );
×
691

692
                // Simply concatenate all fields containing replace vars so we can handle them all with a single regex find.
693
                $replace_vars_fields = \implode(
×
694
                        ' ',
×
695
                        [
×
696
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->title,
×
697
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->meta_description,
×
698
                        ]
×
699
                );
×
700

701
                \preg_match_all( '/%%cf_([A-Za-z0-9_]+)%%/', $replace_vars_fields, $matches );
×
702
                $fields_to_include = $matches[1];
×
703
                foreach ( $custom_fields as $custom_field_name => $custom_field ) {
×
704
                        // Skip private custom fields.
705
                        if ( \substr( $custom_field_name, 0, 1 ) === '_' ) {
×
706
                                continue;
×
707
                        }
708

709
                        // Skip custom fields that are not used, new ones will be fetched dynamically.
710
                        if ( ! \in_array( $custom_field_name, $fields_to_include, true ) ) {
×
711
                                continue;
×
712
                        }
713

714
                        // Skip custom field values that are serialized.
715
                        if ( \is_serialized( $custom_field[0] ) ) {
×
716
                                continue;
×
717
                        }
718

719
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
720
                }
721

722
                return $custom_replace_vars;
×
723
        }
724

725
        /**
726
         * Determines the scope based on the post type.
727
         * This can be used by the replacevar plugin to determine if a replacement needs to be executed.
728
         *
729
         * @return string String describing the current scope.
730
         */
731
        protected function determine_scope() {
×
732
                if ( $this->get_metabox_post()->post_type === 'page' ) {
×
733
                        return 'page';
×
734
                }
735

736
                return 'post';
×
737
        }
738

739
        /**
740
         * Determines whether or not the current post type has registered taxonomies.
741
         *
742
         * @return bool Whether the current post type has taxonomies.
743
         */
744
        protected function current_post_type_has_taxonomies() {
×
745
                $post_taxonomies = \get_object_taxonomies( $this->get_metabox_post()->post_type );
×
746

747
                return ! empty( $post_taxonomies );
×
748
        }
749

750
        /**
751
         * Returns an array with shortcode tags for all registered shortcodes.
752
         *
753
         * @return array
754
         */
755
        protected function get_valid_shortcode_tags() {
×
756
                $shortcode_tags = [];
×
757

758
                foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
×
759
                        $shortcode_tags[] = $tag;
×
760
                }
761

762
                return $shortcode_tags;
×
763
        }
764
}
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