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

Yoast / wordpress-seo / bc3a4e966336e035c87bf7445627c054d34ed794

13 Sep 2024 11:03AM UTC coverage: 49.552% (-2.0%) from 51.551%
bc3a4e966336e035c87bf7445627c054d34ed794

push

github

web-flow
Merge pull request #21634 from Yoast/fix-primary-taxonomy-picker-deprecation-warning

Fix SelectControl deprecation warning

7511 of 13482 branches covered (55.71%)

Branch coverage included in aggregate %.

25755 of 53652 relevant lines covered (48.0%)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

169
                \add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'init' ] );
×
170
        }
171

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

322
                \do_action( 'wpseo_saved_postdata' );
×
323

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

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

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

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

348
                return false;
×
349
        }
350

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

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

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

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

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

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

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

410
                $permalink = $this->get_permalink();
×
411

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

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

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

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

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

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

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

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

471
                \printf(
×
472
                        '<input type="hidden" id="%1$s" name="%1$s" value="%2$s" />',
×
473
                        \esc_attr( WPSEO_Meta::$form_prefix . 'slug' ),
×
474
                        \esc_attr( $this->get_post_slug() )
×
475
                );
×
476

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

480
                echo '</form>';
×
481
        }
482

483
        /**
484
         * Returns the slug for the post being edited.
485
         *
486
         * @return string
487
         */
488
        protected function get_post_slug() {
×
489
                $post = $this->get_metabox_post();
×
490

491
                // In case get_metabox_post returns null for whatever reason.
492
                if ( ! $post instanceof WP_Post ) {
×
493
                        return '';
×
494
                }
495

496
                // Drafts might not have a post_name unless the slug has been manually changed.
497
                // In this case we get it using get_sample_permalink.
498
                if ( ! $post->post_name ) {
×
499
                        $sample = \get_sample_permalink( $post );
×
500

501
                        // Since get_sample_permalink runs through filters, ensure that it has the expected return value.
502
                        if ( \is_array( $sample ) && \count( $sample ) === 2 && \is_string( $sample[1] ) ) {
×
503
                                return $sample[1];
×
504
                        }
505
                }
506

507
                return $post->post_name;
×
508
        }
509

510
        /**
511
         * Returns post in metabox context.
512
         *
513
         * @return WP_Post|null
514
         */
515
        protected function get_metabox_post() {
×
516
                if ( $this->post !== null ) {
×
517
                        return $this->post;
×
518
                }
519

520
                $this->post = $this->request_post->get_post();
×
521

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

525
        /**
526
         * Passes variables to js for use with the post-scraper.
527
         *
528
         * @param string $permalink The permalink.
529
         *
530
         * @return array
531
         */
532
        protected function get_metabox_script_data( $permalink ) {
×
533
                $post_formatter = new WPSEO_Metabox_Formatter(
×
534
                        new WPSEO_Post_Metabox_Formatter( $this->get_metabox_post(), [], $permalink )
×
535
                );
×
536

537
                $values = $post_formatter->get_values();
×
538

539
                /** This filter is documented in admin/filters/class-cornerstone-filter.php. */
540
                $post_types = \apply_filters( 'wpseo_cornerstone_post_types', \YoastSEO()->helpers->post_type->get_accessible_post_types() );
×
541
                if ( $values['cornerstoneActive'] && ! \in_array( $this->get_metabox_post()->post_type, $post_types, true ) ) {
×
542
                        $values['cornerstoneActive'] = false;
×
543
                }
544

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

547
                return $values;
×
548
        }
549

550
        /**
551
         * Gets the permalink.
552
         *
553
         * @return string
554
         */
555
        protected function get_permalink(): string {
×
556
                $permalink = '';
×
557

558
                if ( \is_object( $this->get_metabox_post() ) ) {
×
559
                        $permalink = \get_sample_permalink( $this->get_metabox_post()->ID );
×
560
                        $permalink = $permalink[0];
×
561
                }
562

563
                return $permalink;
×
564
        }
565

566
        /**
567
         * Checks whether the highlighting functionality is available for Elementor:
568
         * - in Free it's always available (as an upsell).
569
         * - in Premium it's available as long as the version is 21.8-RC0 or above.
570
         *
571
         * @return bool Whether the highlighting functionality is available.
572
         */
573
        private function is_highlighting_available() {
×
574
                $is_premium      = \YoastSEO()->helpers->product->is_premium();
×
575
                $premium_version = \YoastSEO()->helpers->product->get_premium_version();
×
576

577
                return ! $is_premium || \version_compare( $premium_version, '21.8-RC0', '>=' );
×
578
        }
579

580
        /**
581
         * Prepares the replace vars for localization.
582
         *
583
         * @return array Replace vars.
584
         */
585
        protected function get_replace_vars() {
×
586
                $cached_replacement_vars = [];
×
587

588
                $vars_to_cache = [
×
589
                        'date',
×
590
                        'id',
×
591
                        'sitename',
×
592
                        'sitedesc',
×
593
                        'sep',
×
594
                        'page',
×
595
                        'currentyear',
×
596
                        'currentdate',
×
597
                        'currentmonth',
×
598
                        'currentday',
×
599
                        'tag',
×
600
                        'category',
×
601
                        'category_title',
×
602
                        'primary_category',
×
603
                        'pt_single',
×
604
                        'pt_plural',
×
605
                        'modified',
×
606
                        'name',
×
607
                        'user_description',
×
608
                        'pagetotal',
×
609
                        'pagenumber',
×
610
                        'post_year',
×
611
                        'post_month',
×
612
                        'post_day',
×
613
                        'author_first_name',
×
614
                        'author_last_name',
×
615
                        'permalink',
×
616
                        'post_content',
×
617
                ];
×
618

619
                foreach ( $vars_to_cache as $var ) {
×
620
                        $cached_replacement_vars[ $var ] = \wpseo_replace_vars( '%%' . $var . '%%', $this->get_metabox_post() );
×
621
                }
622

623
                // Merge custom replace variables with the WordPress ones.
624
                return \array_merge( $cached_replacement_vars, $this->get_custom_replace_vars( $this->get_metabox_post() ) );
×
625
        }
626

627
        /**
628
         * Prepares the recommended replace vars for localization.
629
         *
630
         * @return array Recommended replacement variables.
631
         */
632
        protected function get_recommended_replace_vars() {
×
633
                $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
×
634

635
                // What is recommended depends on the current context.
636
                $post_type = $recommended_replace_vars->determine_for_post( $this->get_metabox_post() );
×
637

638
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
639
        }
640

641
        /**
642
         * Returns the list of replace vars that should be hidden inside the editor.
643
         *
644
         * @return string[] The hidden replace vars.
645
         */
646
        protected function get_hidden_replace_vars() {
×
647
                return ( new WPSEO_Replace_Vars() )->get_hidden_replace_vars();
×
648
        }
649

650
        /**
651
         * Gets the custom replace variables for custom taxonomies and fields.
652
         *
653
         * @param WP_Post $post The post to check for custom taxonomies and fields.
654
         *
655
         * @return array Array containing all the replacement variables.
656
         */
657
        protected function get_custom_replace_vars( $post ) {
×
658
                return [
×
659
                        'custom_fields'     => $this->get_custom_fields_replace_vars( $post ),
×
660
                        'custom_taxonomies' => $this->get_custom_taxonomies_replace_vars( $post ),
×
661
                ];
×
662
        }
663

664
        /**
665
         * Gets the custom replace variables for custom taxonomies.
666
         *
667
         * @param WP_Post $post The post to check for custom taxonomies.
668
         *
669
         * @return array Array containing all the replacement variables.
670
         */
671
        protected function get_custom_taxonomies_replace_vars( $post ) {
×
672
                $taxonomies          = \get_object_taxonomies( $post, 'objects' );
×
673
                $custom_replace_vars = [];
×
674

675
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
676

677
                        if ( \is_string( $taxonomy ) ) { // If attachment, see https://core.trac.wordpress.org/ticket/37368 .
×
678
                                $taxonomy_name = $taxonomy;
×
679
                                $taxonomy      = \get_taxonomy( $taxonomy_name );
×
680
                        }
681

682
                        if ( $taxonomy->_builtin && $taxonomy->public ) {
×
683
                                continue;
×
684
                        }
685

686
                        $custom_replace_vars[ $taxonomy_name ] = [
×
687
                                'name'        => $taxonomy->name,
×
688
                                'description' => $taxonomy->description,
×
689
                        ];
×
690
                }
691

692
                return $custom_replace_vars;
×
693
        }
694

695
        /**
696
         * Gets the custom replace variables for custom fields.
697
         *
698
         * @param WP_Post $post The post to check for custom fields.
699
         *
700
         * @return array Array containing all the replacement variables.
701
         */
702
        protected function get_custom_fields_replace_vars( $post ) {
×
703
                $custom_replace_vars = [];
×
704

705
                // If no post object is passed, return the empty custom_replace_vars array.
706
                if ( ! \is_object( $post ) ) {
×
707
                        return $custom_replace_vars;
×
708
                }
709

710
                $custom_fields = \get_post_custom( $post->ID );
×
711

712
                // Simply concatenate all fields containing replace vars so we can handle them all with a single regex find.
713
                $replace_vars_fields = \implode(
×
714
                        ' ',
×
715
                        [
×
716
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->title,
×
717
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->meta_description,
×
718
                        ]
×
719
                );
×
720

721
                \preg_match_all( '/%%cf_([A-Za-z0-9_]+)%%/', $replace_vars_fields, $matches );
×
722
                $fields_to_include = $matches[1];
×
723
                foreach ( $custom_fields as $custom_field_name => $custom_field ) {
×
724
                        // Skip private custom fields.
725
                        if ( \substr( $custom_field_name, 0, 1 ) === '_' ) {
×
726
                                continue;
×
727
                        }
728

729
                        // Skip custom fields that are not used, new ones will be fetched dynamically.
730
                        if ( ! \in_array( $custom_field_name, $fields_to_include, true ) ) {
×
731
                                continue;
×
732
                        }
733

734
                        // Skip custom field values that are serialized.
735
                        if ( \is_serialized( $custom_field[0] ) ) {
×
736
                                continue;
×
737
                        }
738

739
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
740
                }
741

742
                return $custom_replace_vars;
×
743
        }
744

745
        /**
746
         * Determines the scope based on the post type.
747
         * This can be used by the replacevar plugin to determine if a replacement needs to be executed.
748
         *
749
         * @return string String describing the current scope.
750
         */
751
        protected function determine_scope() {
×
752
                if ( $this->get_metabox_post()->post_type === 'page' ) {
×
753
                        return 'page';
×
754
                }
755

756
                return 'post';
×
757
        }
758

759
        /**
760
         * Determines whether or not the current post type has registered taxonomies.
761
         *
762
         * @return bool Whether the current post type has taxonomies.
763
         */
764
        protected function current_post_type_has_taxonomies() {
×
765
                $post_taxonomies = \get_object_taxonomies( $this->get_metabox_post()->post_type );
×
766

767
                return ! empty( $post_taxonomies );
×
768
        }
769

770
        /**
771
         * Returns an array with shortcode tags for all registered shortcodes.
772
         *
773
         * @return array
774
         */
775
        protected function get_valid_shortcode_tags() {
×
776
                $shortcode_tags = [];
×
777

778
                foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
×
779
                        $shortcode_tags[] = $tag;
×
780
                }
781

782
                return $shortcode_tags;
×
783
        }
784
}
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