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

Yoast / wordpress-seo / b961a40c4e03131cc8c75ccf8d5090e5e16f19f9

05 Jul 2024 01:16PM UTC coverage: 54.068% (+5.4%) from 48.645%
b961a40c4e03131cc8c75ccf8d5090e5e16f19f9

push

github

mhkuu
Merge branch 'trunk' of github.com:Yoast/wordpress-seo into feature/fix-assessments

7451 of 13463 branches covered (55.34%)

Branch coverage included in aggregate %.

287 of 380 new or added lines in 34 files covered. (75.53%)

1 existing line in 1 file now uncovered.

29380 of 54657 relevant lines covered (53.75%)

61031.68 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 Elementor\Controls_Manager;
6
use Elementor\Core\DocumentTypes\PageBase;
7
use WP_Post;
8
use WP_Screen;
9
use WPSEO_Admin_Asset_Manager;
10
use WPSEO_Admin_Recommended_Replace_Vars;
11
use WPSEO_Language_Utils;
12
use WPSEO_Meta;
13
use WPSEO_Metabox_Analysis_Inclusive_Language;
14
use WPSEO_Metabox_Analysis_Readability;
15
use WPSEO_Metabox_Analysis_SEO;
16
use WPSEO_Metabox_Formatter;
17
use WPSEO_Post_Metabox_Formatter;
18
use WPSEO_Replace_Vars;
19
use WPSEO_Shortlinker;
20
use WPSEO_Utils;
21
use Yoast\WP\SEO\Conditionals\Third_Party\Elementor_Edit_Conditional;
22
use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional;
23
use Yoast\WP\SEO\Editors\Application\Site\Website_Information_Repository;
24
use Yoast\WP\SEO\Helpers\Capability_Helper;
25
use Yoast\WP\SEO\Helpers\Options_Helper;
26
use Yoast\WP\SEO\Integrations\Integration_Interface;
27
use Yoast\WP\SEO\Presenters\Admin\Meta_Fields_Presenter;
28
use Yoast\WP\SEO\Promotions\Application\Promotion_Manager;
29

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

35
        /**
36
         * The identifier for the elementor tab.
37
         */
38
        public const YOAST_TAB = 'yoast-tab';
39

40
        /**
41
         * Represents the post.
42
         *
43
         * @var WP_Post|null
44
         */
45
        protected $post;
46

47
        /**
48
         * Represents the admin asset manager.
49
         *
50
         * @var WPSEO_Admin_Asset_Manager
51
         */
52
        protected $asset_manager;
53

54
        /**
55
         * Represents the options helper.
56
         *
57
         * @var Options_Helper
58
         */
59
        protected $options;
60

61
        /**
62
         * Represents the capability helper.
63
         *
64
         * @var Capability_Helper
65
         */
66
        protected $capability;
67

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

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

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

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

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

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

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

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

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

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

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

156
        /**
157
         * Registers our Elementor hooks.
158
         * This is done for pages with metabox on page load and not on ajax request.
159
         *
160
         * @return void
161
         */
162
        public function register_elementor_hooks() {
×
163

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
                // We are too late for elementor/init. We should see if we can be on time, or else this workaround works (we do always get the "else" though).
171
                if ( ! \did_action( 'elementor/init' ) ) {
×
172
                        \add_action( 'elementor/init', [ $this, 'add_yoast_panel_tab' ] );
×
173
                }
174
                else {
175
                        $this->add_yoast_panel_tab();
×
176
                }
177
                \add_action( 'elementor/documents/register_controls', [ $this, 'register_document_controls' ] );
×
178
        }
179

180
        /**
181
         * Initializes the integration.
182
         *
183
         * @return void
184
         */
185
        public function init() {
×
186
                $this->asset_manager->register_assets();
×
187
                $this->enqueue();
×
188
                $this->render_hidden_fields();
×
189
        }
190

191
        /**
192
         * Register a panel tab slug, in order to allow adding controls to this tab.
193
         *
194
         * @return void
195
         */
196
        public function add_yoast_panel_tab() {
×
197
                Controls_Manager::add_tab( $this::YOAST_TAB, 'Yoast SEO' );
×
198
        }
199

200
        /**
201
         * Register additional document controls.
202
         *
203
         * @param PageBase $document The PageBase document.
204
         *
205
         * @return void
206
         */
207
        public function register_document_controls( $document ) {
×
208
                // PageBase is the base class for documents like `post` `page` and etc.
209
                if ( ! $document instanceof PageBase || ! $document::get_property( 'has_elements' ) ) {
×
210
                        return;
×
211
                }
212

213
                // This is needed to get the tab to appear, but will be overwritten in the JavaScript.
214
                $document->start_controls_section(
×
215
                        'yoast_temporary_section',
×
216
                        [
217
                                'label' => 'Yoast SEO',
×
218
                                'tab'   => self::YOAST_TAB,
×
219
                        ]
220
                );
221

222
                $document->end_controls_section();
×
223
        }
224

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

227
        /**
228
         * Determines whether the metabox should be shown for the passed identifier.
229
         *
230
         * By default, the check is done for post types, but can also be used for taxonomies.
231
         *
232
         * @param string|null $identifier The identifier to check.
233
         * @param string      $type       The type of object to check. Defaults to post_type.
234
         *
235
         * @return bool Whether the metabox should be displayed.
236
         */
237
        public function display_metabox( $identifier = null, $type = 'post_type' ) {
×
238
                return WPSEO_Utils::is_metabox_active( $identifier, $type );
×
239
        }
240

241
        /**
242
         * Saves the WP SEO metadata for posts.
243
         *
244
         * Outputs JSON via wp_send_json then stops code execution.
245
         *
246
         * {@internal $_POST parameters are validated via sanitize_post_meta().}}
247
         *
248
         * @return void
249
         */
250
        public function save_postdata() {
×
251
                global $post;
×
252

253
                if ( ! isset( $_POST['post_id'] ) || ! \is_string( $_POST['post_id'] ) ) {
×
254
                        \wp_send_json_error( 'Bad Request', 400 );
×
255
                }
256

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

260
                if ( $post_id <= 0 ) {
×
261
                        \wp_send_json_error( 'Bad Request', 400 );
×
262
                }
263

264
                if ( ! \current_user_can( 'edit_post', $post_id ) ) {
×
265
                        \wp_send_json_error( 'Forbidden', 403 );
×
266
                }
267

268
                \check_ajax_referer( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
×
269

270
                // Bail if this is a multisite installation and the site has been switched.
271
                if ( \is_multisite() && \ms_is_switched() ) {
×
272
                        \wp_send_json_error( 'Switched multisite', 409 );
×
273
                }
274

275
                \clean_post_cache( $post_id );
×
276
                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- To setup the post we need to do this explicitly.
277
                $post = \get_post( $post_id );
×
278

279
                if ( ! \is_object( $post ) ) {
×
280
                        // Non-existent post.
281
                        \wp_send_json_error( 'Post not found', 400 );
×
282
                }
283

284
                \do_action( 'wpseo_save_compare_data', $post );
×
285

286
                // Initialize meta, amongst other things it registers sanitization.
287
                WPSEO_Meta::init();
×
288

289
                $social_fields = [];
×
290
                if ( $this->social_is_enabled ) {
×
291
                        $social_fields = WPSEO_Meta::get_meta_field_defs( 'social', $post->post_type );
×
292
                }
293

294
                // The below methods use the global post so make sure it is setup.
295
                \setup_postdata( $post );
×
296
                $meta_boxes = \apply_filters( 'wpseo_save_metaboxes', [] );
×
297
                $meta_boxes = \array_merge(
×
298
                        $meta_boxes,
×
299
                        WPSEO_Meta::get_meta_field_defs( 'general', $post->post_type ),
×
300
                        WPSEO_Meta::get_meta_field_defs( 'advanced', $post->post_type ),
×
301
                        $social_fields,
×
302
                        WPSEO_Meta::get_meta_field_defs( 'schema', $post->post_type )
×
303
                );
304

305
                foreach ( $meta_boxes as $key => $meta_box ) {
×
306
                        // If analysis is disabled remove that analysis score value from the DB.
307
                        if ( $this->is_meta_value_disabled( $key ) ) {
×
308
                                WPSEO_Meta::delete( $key, $post_id );
×
309
                                continue;
×
310
                        }
311

312
                        $data       = null;
×
313
                        $field_name = WPSEO_Meta::$form_prefix . $key;
×
314

315
                        if ( $meta_box['type'] === 'checkbox' ) {
×
316
                                $data = isset( $_POST[ $field_name ] ) ? 'on' : 'off';
×
317
                        }
318
                        else {
319
                                if ( isset( $_POST[ $field_name ] ) ) {
×
320
                                        // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: Sanitized through sanitize_post_meta.
321
                                        $data = \wp_unslash( $_POST[ $field_name ] );
×
322

323
                                        // For multi-select.
324
                                        if ( \is_array( $data ) ) {
×
325
                                                $data = \array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], $data );
×
326
                                        }
327

328
                                        if ( \is_string( $data ) ) {
×
329
                                                $data = ( $key !== 'canonical' ) ? WPSEO_Utils::sanitize_text_field( $data ) : WPSEO_Utils::sanitize_url( $data );
×
330
                                        }
331
                                }
332

333
                                // Reset options when no entry is present with multiselect - only applies to `meta-robots-adv` currently.
334
                                if ( ! isset( $_POST[ $field_name ] ) && ( $meta_box['type'] === 'multiselect' ) ) {
×
335
                                        $data = [];
×
336
                                }
337
                        }
338

339
                        if ( $data !== null ) {
×
340
                                WPSEO_Meta::set_value( $key, $data, $post_id );
×
341
                        }
342
                }
343

344
                if ( isset( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) && \is_string( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) ) {
×
345
                        $slug = \sanitize_title( \wp_unslash( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) );
×
346
                        if ( $post->post_name !== $slug ) {
×
347
                                $post_array              = $post->to_array();
×
348
                                $post_array['post_name'] = $slug;
×
349

350
                                $save_successful = \wp_insert_post( $post_array );
×
351
                                if ( \is_wp_error( $save_successful ) ) {
×
352
                                        \wp_send_json_error( 'Slug not saved', 400 );
×
353
                                }
354

355
                                // Update the post object to ensure we have the actual slug.
356
                                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Updating the post is needed to get the current slug.
357
                                $post = \get_post( $post_id );
×
358
                                if ( ! \is_object( $post ) ) {
×
359
                                        \wp_send_json_error( 'Updated slug not found', 400 );
×
360
                                }
361
                        }
362
                }
363

364
                \do_action( 'wpseo_saved_postdata' );
×
365

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

370
        /**
371
         * Determines if the given meta value key is disabled.
372
         *
373
         * @param string $key The key of the meta value.
374
         *
375
         * @return bool Whether the given meta value key is disabled.
376
         */
377
        public function is_meta_value_disabled( $key ) {
×
378
                if ( $key === 'linkdex' && ! $this->seo_analysis->is_enabled() ) {
×
379
                        return true;
×
380
                }
381

382
                if ( $key === 'content_score' && ! $this->readability_analysis->is_enabled() ) {
×
383
                        return true;
×
384
                }
385

386
                if ( $key === 'inclusive_language_score' && ! $this->inclusive_language_analysis->is_enabled() ) {
×
387
                        return true;
×
388
                }
389

390
                return false;
×
391
        }
392

393
        /**
394
         * Enqueues all the needed JS and CSS.
395
         *
396
         * @return void
397
         */
398
        public function enqueue() {
×
399
                $post_id = \get_queried_object_id();
×
400
                if ( empty( $post_id ) ) {
×
401
                        $post_id = 0;
×
402
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
403
                        if ( isset( $_GET['post'] ) && \is_string( $_GET['post'] ) ) {
×
404
                                // 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.
405
                                $post_id = (int) \wp_unslash( $_GET['post'] );
×
406
                        }
407
                }
408

409
                if ( $post_id !== 0 ) {
×
410
                        // Enqueue files needed for upload functionality.
411
                        \wp_enqueue_media( [ 'post' => $post_id ] );
×
412
                }
413

414
                $this->asset_manager->enqueue_style( 'admin-global' );
×
415
                $this->asset_manager->enqueue_style( 'metabox-css' );
×
416
                $this->asset_manager->enqueue_style( 'scoring' );
×
417
                $this->asset_manager->enqueue_style( 'monorepo' );
×
418
                $this->asset_manager->enqueue_style( 'admin-css' );
×
419
                $this->asset_manager->enqueue_style( 'ai-generator' );
×
420
                $this->asset_manager->enqueue_style( 'elementor' );
×
421

422
                $this->asset_manager->enqueue_script( 'admin-global' );
×
423
                $this->asset_manager->enqueue_script( 'elementor' );
×
424

425
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminGlobalL10n', \YoastSEO()->helpers->wincher->get_admin_global_links() );
×
426
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
×
427
                $this->asset_manager->localize_script( 'elementor', 'wpseoFeaturesL10n', WPSEO_Utils::retrieve_enabled_features() );
×
428

429
                $plugins_script_data = [
430
                        'replaceVars' => [
431
                                'no_parent_text'           => \__( '(no parent)', 'wordpress-seo' ),
×
432
                                'replace_vars'             => $this->get_replace_vars(),
×
433
                                'recommended_replace_vars' => $this->get_recommended_replace_vars(),
×
434
                                'hidden_replace_vars'      => $this->get_hidden_replace_vars(),
×
435
                                'scope'                    => $this->determine_scope(),
×
436
                                'has_taxonomies'           => $this->current_post_type_has_taxonomies(),
×
437
                        ],
438
                        'shortcodes'  => [
439
                                'wpseo_shortcode_tags'          => $this->get_valid_shortcode_tags(),
×
440
                                'wpseo_filter_shortcodes_nonce' => \wp_create_nonce( 'wpseo-filter-shortcodes' ),
×
441
                        ],
442
                ];
443

444
                $worker_script_data = [
445
                        'url'                     => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-analysis-worker' ),
×
446
                        'dependencies'            => \YoastSEO()->helpers->asset->get_dependency_urls_by_handle( 'yoast-seo-analysis-worker' ),
×
447
                        'keywords_assessment_url' => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-used-keywords-assessment' ),
×
448
                        'log_level'               => WPSEO_Utils::get_analysis_worker_log_level(),
×
449
                        // We need to make the feature flags separately available inside of the analysis web worker.
450
                        'enabled_features'        => WPSEO_Utils::retrieve_enabled_features(),
×
451
                ];
452

453
                $woocommerce_conditional = new WooCommerce_Conditional();
×
NEW
454
                $permalink               = $this->get_permalink();
×
455

456
                $script_data = [
457
                        'media'                     => [ 'choose_image' => \__( 'Use Image', 'wordpress-seo' ) ],
×
NEW
458
                        'metabox'                   => $this->get_metabox_script_data( $permalink ),
×
459
                        'userLanguageCode'          => WPSEO_Language_Utils::get_language( \get_user_locale() ),
×
460
                        'isPost'                    => true,
461
                        'isBlockEditor'             => WP_Screen::get()->is_block_editor(),
×
462
                        'isElementorEditor'         => true,
463
                        'isWooCommerceActive'       => $woocommerce_conditional->is_met(),
×
464
                        'postStatus'                => \get_post_status( $post_id ),
×
465
                        'postType'                  => \get_post_type( $post_id ),
×
466
                        'analysis'                  => [
467
                                'plugins' => $plugins_script_data,
×
468
                                'worker'  => $worker_script_data,
×
469
                        ],
470
                        'webinarIntroElementorUrl'  => WPSEO_Shortlinker::get( 'https://yoa.st/webinar-intro-elementor' ),
×
471
                        'usedKeywordsNonce'         => \wp_create_nonce( 'wpseo-keyword-usage-and-post-types' ),
×
472
                ];
473

474
                /**
475
                 * The website information repository.
476
                 *
477
                 * @var $repo Website_Information_Repository
478
                 */
NEW
479
                $repo             = \YoastSEO()->classes->get( Website_Information_Repository::class );
×
NEW
480
                $site_information = $repo->get_post_site_information();
×
NEW
481
                $site_information->set_permalink( $permalink );
×
NEW
482
                $script_data = \array_merge_recursive( $site_information->get_legacy_site_information(), $script_data );
×
483

484
                if ( \post_type_supports( $this->get_metabox_post()->post_type, 'thumbnail' ) ) {
×
485
                        $this->asset_manager->enqueue_style( 'featured-image' );
×
486

487
                        $script_data['featuredImage'] = [
×
488
                                'featured_image_notice' => \__( 'SEO issue: The featured image should be at least 200 by 200 pixels to be picked up by Facebook and other social media sites.', 'wordpress-seo' ),
×
489
                        ];
490
                }
491

492
                $this->asset_manager->localize_script( 'elementor', 'wpseoScriptData', $script_data );
×
493
                $this->asset_manager->enqueue_user_language_script();
×
494
        }
495

496
        /**
497
         * Renders the metabox hidden fields.
498
         *
499
         * @return void
500
         */
501
        protected function render_hidden_fields() {
×
502
                // Wrap in a form with an action and post_id for the submit.
503
                \printf(
×
504
                        '<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" />',
×
505
                        \esc_url( \admin_url( 'admin-ajax.php' ) ),
×
506
                        \esc_attr( $this->get_metabox_post()->ID )
×
507
                );
508

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

513
                if ( $this->is_advanced_metadata_enabled ) {
×
514
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
515
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'advanced' );
×
516
                }
517

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

521
                if ( $this->social_is_enabled ) {
×
522
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
523
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'social' );
×
524
                }
525

526
                \printf(
×
527
                        '<input type="hidden" id="%1$s" name="%1$s" value="%2$s" />',
×
528
                        \esc_attr( WPSEO_Meta::$form_prefix . 'slug' ),
×
529
                        \esc_attr( $this->get_post_slug() )
×
530
                );
531

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

535
                echo '</form>';
×
536
        }
537

538
        /**
539
         * Returns the slug for the post being edited.
540
         *
541
         * @return string
542
         */
543
        protected function get_post_slug() {
×
544
                $post = $this->get_metabox_post();
×
545

546
                // In case get_metabox_post returns null for whatever reason.
547
                if ( ! $post instanceof WP_Post ) {
×
548
                        return '';
×
549
                }
550

551
                // Drafts might not have a post_name unless the slug has been manually changed.
552
                // In this case we get it using get_sample_permalink.
553
                if ( ! $post->post_name ) {
×
554
                        $sample = \get_sample_permalink( $post );
×
555

556
                        // Since get_sample_permalink runs through filters, ensure that it has the expected return value.
557
                        if ( \is_array( $sample ) && \count( $sample ) === 2 && \is_string( $sample[1] ) ) {
×
558
                                return $sample[1];
×
559
                        }
560
                }
561

562
                return $post->post_name;
×
563
        }
564

565
        /**
566
         * Returns post in metabox context.
567
         *
568
         * @return WP_Post|null
569
         */
570
        protected function get_metabox_post() {
×
571
                if ( $this->post !== null ) {
×
572
                        return $this->post;
×
573
                }
574

575
                $post = null;
×
576
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
577
                if ( isset( $_GET['post'] ) && \is_numeric( $_GET['post'] ) ) {
×
578
                        // 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.
579
                        $post = (int) \wp_unslash( $_GET['post'] );
×
580
                }
581

582
                if ( ! empty( $post ) ) {
×
583
                        $this->post = \get_post( $post );
×
584

585
                        return $this->post;
×
586
                }
587

588
                if ( isset( $GLOBALS['post'] ) ) {
×
589
                        $this->post = $GLOBALS['post'];
×
590

591
                        return $this->post;
×
592
                }
593

594
                return null;
×
595
        }
596

597
        /**
598
         * Passes variables to js for use with the post-scraper.
599
         *
600
         * @param string $permalink The permalink.
601
         *
602
         * @return array
603
         */
NEW
604
        protected function get_metabox_script_data( $permalink ) {
×
605
                $post_formatter = new WPSEO_Metabox_Formatter(
×
606
                        new WPSEO_Post_Metabox_Formatter( $this->get_metabox_post(), [], $permalink )
×
607
                );
608

609
                $values = $post_formatter->get_values();
×
610

611
                /** This filter is documented in admin/filters/class-cornerstone-filter.php. */
612
                $post_types = \apply_filters( 'wpseo_cornerstone_post_types', \YoastSEO()->helpers->post_type->get_accessible_post_types() );
×
613
                if ( $values['cornerstoneActive'] && ! \in_array( $this->get_metabox_post()->post_type, $post_types, true ) ) {
×
614
                        $values['cornerstoneActive'] = false;
×
615
                }
616

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

619
                return $values;
×
620
        }
621

622
        /**
623
         * Gets the permalink.
624
         *
625
         * @return string
626
         */
NEW
627
        protected function get_permalink(): string {
×
NEW
628
                $permalink = '';
×
629

NEW
630
                if ( \is_object( $this->get_metabox_post() ) ) {
×
NEW
631
                        $permalink = \get_sample_permalink( $this->get_metabox_post()->ID );
×
NEW
632
                        $permalink = $permalink[0];
×
633
                }
634

NEW
635
                return $permalink;
×
636
        }
637

638
        /**
639
         * Checks whether the highlighting functionality is available for Elementor:
640
         * - in Free it's always available (as an upsell).
641
         * - in Premium it's available as long as the version is 21.8-RC0 or above.
642
         *
643
         * @return bool Whether the highlighting functionality is available.
644
         */
645
        private function is_highlighting_available() {
×
646
                $is_premium      = \YoastSEO()->helpers->product->is_premium();
×
647
                $premium_version = \YoastSEO()->helpers->product->get_premium_version();
×
648

649
                return ! $is_premium || \version_compare( $premium_version, '21.8-RC0', '>=' );
×
650
        }
651

652
        /**
653
         * Prepares the replace vars for localization.
654
         *
655
         * @return array Replace vars.
656
         */
657
        protected function get_replace_vars() {
×
658
                $cached_replacement_vars = [];
×
659

660
                $vars_to_cache = [
661
                        'date',
×
662
                        'id',
663
                        'sitename',
664
                        'sitedesc',
665
                        'sep',
666
                        'page',
667
                        'currentyear',
668
                        'currentdate',
669
                        'currentmonth',
670
                        'currentday',
671
                        'tag',
672
                        'category',
673
                        'category_title',
674
                        'primary_category',
675
                        'pt_single',
676
                        'pt_plural',
677
                        'modified',
678
                        'name',
679
                        'user_description',
680
                        'pagetotal',
681
                        'pagenumber',
682
                        'post_year',
683
                        'post_month',
684
                        'post_day',
685
                        'author_first_name',
686
                        'author_last_name',
687
                        'permalink',
688
                        'post_content',
689
                ];
690

691
                foreach ( $vars_to_cache as $var ) {
×
692
                        $cached_replacement_vars[ $var ] = \wpseo_replace_vars( '%%' . $var . '%%', $this->get_metabox_post() );
×
693
                }
694

695
                // Merge custom replace variables with the WordPress ones.
696
                return \array_merge( $cached_replacement_vars, $this->get_custom_replace_vars( $this->get_metabox_post() ) );
×
697
        }
698

699
        /**
700
         * Prepares the recommended replace vars for localization.
701
         *
702
         * @return array Recommended replacement variables.
703
         */
704
        protected function get_recommended_replace_vars() {
×
705
                $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
×
706

707
                // What is recommended depends on the current context.
708
                $post_type = $recommended_replace_vars->determine_for_post( $this->get_metabox_post() );
×
709

710
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
711
        }
712

713
        /**
714
         * Returns the list of replace vars that should be hidden inside the editor.
715
         *
716
         * @return string[] The hidden replace vars.
717
         */
718
        protected function get_hidden_replace_vars() {
×
719
                return ( new WPSEO_Replace_Vars() )->get_hidden_replace_vars();
×
720
        }
721

722
        /**
723
         * Gets the custom replace variables for custom taxonomies and fields.
724
         *
725
         * @param WP_Post $post The post to check for custom taxonomies and fields.
726
         *
727
         * @return array Array containing all the replacement variables.
728
         */
729
        protected function get_custom_replace_vars( $post ) {
×
730
                return [
731
                        'custom_fields'     => $this->get_custom_fields_replace_vars( $post ),
×
732
                        'custom_taxonomies' => $this->get_custom_taxonomies_replace_vars( $post ),
×
733
                ];
734
        }
735

736
        /**
737
         * Gets the custom replace variables for custom taxonomies.
738
         *
739
         * @param WP_Post $post The post to check for custom taxonomies.
740
         *
741
         * @return array Array containing all the replacement variables.
742
         */
743
        protected function get_custom_taxonomies_replace_vars( $post ) {
×
744
                $taxonomies          = \get_object_taxonomies( $post, 'objects' );
×
745
                $custom_replace_vars = [];
×
746

747
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
748

749
                        if ( \is_string( $taxonomy ) ) { // If attachment, see https://core.trac.wordpress.org/ticket/37368 .
×
750
                                $taxonomy_name = $taxonomy;
×
751
                                $taxonomy      = \get_taxonomy( $taxonomy_name );
×
752
                        }
753

754
                        if ( $taxonomy->_builtin && $taxonomy->public ) {
×
755
                                continue;
×
756
                        }
757

758
                        $custom_replace_vars[ $taxonomy_name ] = [
×
759
                                'name'        => $taxonomy->name,
×
760
                                'description' => $taxonomy->description,
×
761
                        ];
762
                }
763

764
                return $custom_replace_vars;
×
765
        }
766

767
        /**
768
         * Gets the custom replace variables for custom fields.
769
         *
770
         * @param WP_Post $post The post to check for custom fields.
771
         *
772
         * @return array Array containing all the replacement variables.
773
         */
774
        protected function get_custom_fields_replace_vars( $post ) {
×
775
                $custom_replace_vars = [];
×
776

777
                // If no post object is passed, return the empty custom_replace_vars array.
778
                if ( ! \is_object( $post ) ) {
×
779
                        return $custom_replace_vars;
×
780
                }
781

782
                $custom_fields = \get_post_custom( $post->ID );
×
783

784
                // Simply concatenate all fields containing replace vars so we can handle them all with a single regex find.
785
                $replace_vars_fields = \implode(
×
786
                        ' ',
×
787
                        [
788
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->title,
×
789
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->meta_description,
×
790
                        ]
791
                );
792

793
                \preg_match_all( '/%%cf_([A-Za-z0-9_]+)%%/', $replace_vars_fields, $matches );
×
794
                $fields_to_include = $matches[1];
×
795
                foreach ( $custom_fields as $custom_field_name => $custom_field ) {
×
796
                        // Skip private custom fields.
797
                        if ( \substr( $custom_field_name, 0, 1 ) === '_' ) {
×
798
                                continue;
×
799
                        }
800

801
                        // Skip custom fields that are not used, new ones will be fetched dynamically.
802
                        if ( ! \in_array( $custom_field_name, $fields_to_include, true ) ) {
×
803
                                continue;
×
804
                        }
805

806
                        // Skip custom field values that are serialized.
807
                        if ( \is_serialized( $custom_field[0] ) ) {
×
808
                                continue;
×
809
                        }
810

811
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
812
                }
813

814
                return $custom_replace_vars;
×
815
        }
816

817
        /**
818
         * Determines the scope based on the post type.
819
         * This can be used by the replacevar plugin to determine if a replacement needs to be executed.
820
         *
821
         * @return string String describing the current scope.
822
         */
823
        protected function determine_scope() {
×
824
                if ( $this->get_metabox_post()->post_type === 'page' ) {
×
825
                        return 'page';
×
826
                }
827

828
                return 'post';
×
829
        }
830

831
        /**
832
         * Determines whether or not the current post type has registered taxonomies.
833
         *
834
         * @return bool Whether the current post type has taxonomies.
835
         */
836
        protected function current_post_type_has_taxonomies() {
×
837
                $post_taxonomies = \get_object_taxonomies( $this->get_metabox_post()->post_type );
×
838

839
                return ! empty( $post_taxonomies );
×
840
        }
841

842
        /**
843
         * Returns an array with shortcode tags for all registered shortcodes.
844
         *
845
         * @return array
846
         */
847
        protected function get_valid_shortcode_tags() {
×
848
                $shortcode_tags = [];
×
849

850
                foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
×
851
                        $shortcode_tags[] = $tag;
×
852
                }
853

854
                return $shortcode_tags;
×
855
        }
856
}
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