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

Yoast / wordpress-seo / 71506449b43aef591ba916913e1e481c70e916c4

24 Sep 2024 07:41AM UTC coverage: 54.538% (+0.04%) from 54.503%
71506449b43aef591ba916913e1e481c70e916c4

push

github

YoastBot
Bump version to 23.5 on free

7511 of 13516 branches covered (55.57%)

Branch coverage included in aggregate %.

29694 of 54702 relevant lines covered (54.28%)

41754.08 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 )
×
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

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

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

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

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

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

442
                $this->asset_manager->localize_script( 'elementor', 'wpseoScriptData', $script_data );
×
443
                $this->asset_manager->enqueue_user_language_script();
×
444
        }
445

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

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

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

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

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

476
                \printf(
×
477
                        '<input type="hidden" id="%1$s" name="%1$s" value="%2$s" />',
×
478
                        \esc_attr( WPSEO_Meta::$form_prefix . 'slug' ),
×
479
                        \esc_attr( $this->get_post_slug() )
×
480
                );
481

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

485
                echo '</form>';
×
486
        }
487

488
        /**
489
         * Returns the slug for the post being edited.
490
         *
491
         * @return string
492
         */
493
        protected function get_post_slug() {
×
494
                $post = $this->get_metabox_post();
×
495

496
                // In case get_metabox_post returns null for whatever reason.
497
                if ( ! $post instanceof WP_Post ) {
×
498
                        return '';
×
499
                }
500

501
                // Drafts might not have a post_name unless the slug has been manually changed.
502
                // In this case we get it using get_sample_permalink.
503
                if ( ! $post->post_name ) {
×
504
                        $sample = \get_sample_permalink( $post );
×
505

506
                        // Since get_sample_permalink runs through filters, ensure that it has the expected return value.
507
                        if ( \is_array( $sample ) && \count( $sample ) === 2 && \is_string( $sample[1] ) ) {
×
508
                                return $sample[1];
×
509
                        }
510
                }
511

512
                return $post->post_name;
×
513
        }
514

515
        /**
516
         * Returns post in metabox context.
517
         *
518
         * @return WP_Post|null
519
         */
520
        protected function get_metabox_post() {
×
521
                if ( $this->post !== null ) {
×
522
                        return $this->post;
×
523
                }
524

525
                $this->post = $this->request_post->get_post();
×
526

527
                return $this->post;
×
528
        }
529

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

542
                $values = $post_formatter->get_values();
×
543

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

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

552
                return $values;
×
553
        }
554

555
        /**
556
         * Gets the permalink.
557
         *
558
         * @return string
559
         */
560
        protected function get_permalink(): string {
×
561
                $permalink = '';
×
562

563
                if ( \is_object( $this->get_metabox_post() ) ) {
×
564
                        $permalink = \get_sample_permalink( $this->get_metabox_post()->ID );
×
565
                        $permalink = $permalink[0];
×
566
                }
567

568
                return $permalink;
×
569
        }
570

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

582
                return ! $is_premium || \version_compare( $premium_version, '21.8-RC0', '>=' );
×
583
        }
584

585
        /**
586
         * Prepares the replace vars for localization.
587
         *
588
         * @return array Replace vars.
589
         */
590
        protected function get_replace_vars() {
×
591
                $cached_replacement_vars = [];
×
592

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

624
                foreach ( $vars_to_cache as $var ) {
×
625
                        $cached_replacement_vars[ $var ] = \wpseo_replace_vars( '%%' . $var . '%%', $this->get_metabox_post() );
×
626
                }
627

628
                // Merge custom replace variables with the WordPress ones.
629
                return \array_merge( $cached_replacement_vars, $this->get_custom_replace_vars( $this->get_metabox_post() ) );
×
630
        }
631

632
        /**
633
         * Prepares the recommended replace vars for localization.
634
         *
635
         * @return array Recommended replacement variables.
636
         */
637
        protected function get_recommended_replace_vars() {
×
638
                $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
×
639

640
                // What is recommended depends on the current context.
641
                $post_type = $recommended_replace_vars->determine_for_post( $this->get_metabox_post() );
×
642

643
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
644
        }
645

646
        /**
647
         * Returns the list of replace vars that should be hidden inside the editor.
648
         *
649
         * @return string[] The hidden replace vars.
650
         */
651
        protected function get_hidden_replace_vars() {
×
652
                return ( new WPSEO_Replace_Vars() )->get_hidden_replace_vars();
×
653
        }
654

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

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

680
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
681

682
                        if ( \is_string( $taxonomy ) ) { // If attachment, see https://core.trac.wordpress.org/ticket/37368 .
×
683
                                $taxonomy_name = $taxonomy;
×
684
                                $taxonomy      = \get_taxonomy( $taxonomy_name );
×
685
                        }
686

687
                        if ( $taxonomy->_builtin && $taxonomy->public ) {
×
688
                                continue;
×
689
                        }
690

691
                        $custom_replace_vars[ $taxonomy_name ] = [
×
692
                                'name'        => $taxonomy->name,
×
693
                                'description' => $taxonomy->description,
×
694
                        ];
695
                }
696

697
                return $custom_replace_vars;
×
698
        }
699

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

710
                // If no post object is passed, return the empty custom_replace_vars array.
711
                if ( ! \is_object( $post ) ) {
×
712
                        return $custom_replace_vars;
×
713
                }
714

715
                $custom_fields = \get_post_custom( $post->ID );
×
716

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

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

734
                        // Skip custom fields that are not used, new ones will be fetched dynamically.
735
                        if ( ! \in_array( $custom_field_name, $fields_to_include, true ) ) {
×
736
                                continue;
×
737
                        }
738

739
                        // Skip custom field values that are serialized.
740
                        if ( \is_serialized( $custom_field[0] ) ) {
×
741
                                continue;
×
742
                        }
743

744
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
745
                }
746

747
                return $custom_replace_vars;
×
748
        }
749

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

761
                return 'post';
×
762
        }
763

764
        /**
765
         * Determines whether or not the current post type has registered taxonomies.
766
         *
767
         * @return bool Whether the current post type has taxonomies.
768
         */
769
        protected function current_post_type_has_taxonomies() {
×
770
                $post_taxonomies = \get_object_taxonomies( $this->get_metabox_post()->post_type );
×
771

772
                return ! empty( $post_taxonomies );
×
773
        }
774

775
        /**
776
         * Returns an array with shortcode tags for all registered shortcodes.
777
         *
778
         * @return array
779
         */
780
        protected function get_valid_shortcode_tags() {
×
781
                $shortcode_tags = [];
×
782

783
                foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
×
784
                        $shortcode_tags[] = $tag;
×
785
                }
786

787
                return $shortcode_tags;
×
788
        }
789
}
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