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

Yoast / wordpress-seo / dff4c17b297ab0115178c38a2b7606a73464e56e

28 May 2024 08:36AM UTC coverage: 48.313% (-4.4%) from 52.735%
dff4c17b297ab0115178c38a2b7606a73464e56e

push

github

vraja-pro
remove unused import

7359 of 13368 branches covered (55.05%)

Branch coverage included in aggregate %.

24962 of 53531 relevant lines covered (46.63%)

41960.62 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\Actions\Alert_Dismissal_Action;
22
use Yoast\WP\SEO\Conditionals\Third_Party\Elementor_Edit_Conditional;
23
use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional;
24
use Yoast\WP\SEO\Editors\Framework\Metadata_Groups;
25
use Yoast\WP\SEO\Helpers\Capability_Helper;
26
use Yoast\WP\SEO\Helpers\Options_Helper;
27
use Yoast\WP\SEO\Integrations\Integration_Interface;
28
use Yoast\WP\SEO\Introductions\Infrastructure\Wistia_Embed_Permission_Repository;
29
use Yoast\WP\SEO\Presenters\Admin\Meta_Fields_Presenter;
30
use Yoast\WP\SEO\Promotions\Application\Promotion_Manager;
31

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

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

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

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

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

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

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

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

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

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

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

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

112
        /**
113
         * Holds the metadata groups.
114
         *
115
         * @var Metadata_Groups
116
         */
117
        protected $metadata_groups;
118

119
        /**
120
         * Returns the conditionals based in which this loadable should be active.
121
         *
122
         * @return array
123
         */
124
        public static function get_conditionals() {
×
125
                return [ Elementor_Edit_Conditional::class ];
×
126
        }
127

128
        /**
129
         * Constructor.
130
         *
131
         * @param WPSEO_Admin_Asset_Manager $asset_manager     The asset manager.
132
         * @param Options_Helper            $options           The options helper.
133
         * @param Capability_Helper         $capability        The capability helper.
134
         * @param Promotion_Manager         $promotion_manager The promotion manager.
135
         * @param Metadata_Groups           $metadata_groups   The metadata groups.
136
         */
137
        public function __construct(
×
138
                WPSEO_Admin_Asset_Manager $asset_manager,
139
                Options_Helper $options,
140
                Capability_Helper $capability,
141
                Promotion_Manager $promotion_manager,
142
                Metadata_Groups $metadata_groups
143
        ) {
144
                $this->asset_manager     = $asset_manager;
×
145
                $this->options           = $options;
×
146
                $this->capability        = $capability;
×
147
                $this->promotion_manager = $promotion_manager;
×
148
                $this->metadata_groups   = $metadata_groups;
×
149

150
                $this->seo_analysis                 = new WPSEO_Metabox_Analysis_SEO();
×
151
                $this->readability_analysis         = new WPSEO_Metabox_Analysis_Readability();
×
152
                $this->inclusive_language_analysis  = new WPSEO_Metabox_Analysis_Inclusive_Language();
×
153
                $this->social_is_enabled            = $this->options->get( 'opengraph', false ) || $this->options->get( 'twitter', false );
×
154
                $this->is_advanced_metadata_enabled = $this->capability->current_user_can( 'wpseo_edit_advanced_metadata' ) || $this->options->get( 'disableadvanced_meta' ) === false;
×
155
        }
156

157
        /**
158
         * Initializes the integration.
159
         *
160
         * This is the place to register hooks and filters.
161
         *
162
         * @return void
163
         */
164
        public function register_hooks() {
×
165
                \add_action( 'wp_ajax_wpseo_elementor_save', [ $this, 'save_postdata' ] );
×
166

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

171
        /**
172
         * Registers our Elementor hooks.
173
         * This is done for pages with metabox on page load and not on ajax request.
174
         *
175
         * @return void
176
         */
177
        public function register_elementor_hooks() {
×
178

179
                if ( $this->get_metabox_post() === null || ! $this->display_metabox( $this->get_metabox_post()->post_type ) ) {
×
180
                        return;
×
181
                }
182

183
                \add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'init' ] );
×
184

185
                // 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).
186
                if ( ! \did_action( 'elementor/init' ) ) {
×
187
                        \add_action( 'elementor/init', [ $this, 'add_yoast_panel_tab' ] );
×
188
                }
189
                else {
190
                        $this->add_yoast_panel_tab();
×
191
                }
192
                \add_action( 'elementor/documents/register_controls', [ $this, 'register_document_controls' ] );
×
193
        }
194

195
        /**
196
         * Initializes the integration.
197
         *
198
         * @return void
199
         */
200
        public function init() {
×
201
                $this->asset_manager->register_assets();
×
202
                $this->enqueue();
×
203
                $this->render_hidden_fields();
×
204
        }
205

206
        /**
207
         * Register a panel tab slug, in order to allow adding controls to this tab.
208
         *
209
         * @return void
210
         */
211
        public function add_yoast_panel_tab() {
×
212
                Controls_Manager::add_tab( $this::YOAST_TAB, 'Yoast SEO' );
×
213
        }
214

215
        /**
216
         * Register additional document controls.
217
         *
218
         * @param PageBase $document The PageBase document.
219
         *
220
         * @return void
221
         */
222
        public function register_document_controls( $document ) {
×
223
                // PageBase is the base class for documents like `post` `page` and etc.
224
                if ( ! $document instanceof PageBase || ! $document::get_property( 'has_elements' ) ) {
×
225
                        return;
×
226
                }
227

228
                // This is needed to get the tab to appear, but will be overwritten in the JavaScript.
229
                $document->start_controls_section(
×
230
                        'yoast_temporary_section',
×
231
                        [
×
232
                                'label' => 'Yoast SEO',
×
233
                                'tab'   => self::YOAST_TAB,
×
234
                        ]
×
235
                );
×
236

237
                $document->end_controls_section();
×
238
        }
239

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

242
        /**
243
         * Determines whether the metabox should be shown for the passed identifier.
244
         *
245
         * By default, the check is done for post types, but can also be used for taxonomies.
246
         *
247
         * @param string|null $identifier The identifier to check.
248
         * @param string      $type       The type of object to check. Defaults to post_type.
249
         *
250
         * @return bool Whether the metabox should be displayed.
251
         */
252
        public function display_metabox( $identifier = null, $type = 'post_type' ) {
×
253
                return WPSEO_Utils::is_metabox_active( $identifier, $type );
×
254
        }
255

256
        /**
257
         * Saves the WP SEO metadata for posts.
258
         *
259
         * Outputs JSON via wp_send_json then stops code execution.
260
         *
261
         * {@internal $_POST parameters are validated via sanitize_post_meta().}}
262
         *
263
         * @return void
264
         */
265
        public function save_postdata() {
×
266
                global $post;
×
267

268
                if ( ! isset( $_POST['post_id'] ) || ! \is_string( $_POST['post_id'] ) ) {
×
269
                        \wp_send_json_error( 'Bad Request', 400 );
×
270
                }
271

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

275
                if ( $post_id <= 0 ) {
×
276
                        \wp_send_json_error( 'Bad Request', 400 );
×
277
                }
278

279
                if ( ! \current_user_can( 'edit_post', $post_id ) ) {
×
280
                        \wp_send_json_error( 'Forbidden', 403 );
×
281
                }
282

283
                \check_ajax_referer( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
×
284

285
                // Bail if this is a multisite installation and the site has been switched.
286
                if ( \is_multisite() && \ms_is_switched() ) {
×
287
                        \wp_send_json_error( 'Switched multisite', 409 );
×
288
                }
289

290
                \clean_post_cache( $post_id );
×
291
                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- To setup the post we need to do this explicitly.
292
                $post = \get_post( $post_id );
×
293

294
                if ( ! \is_object( $post ) ) {
×
295
                        // Non-existent post.
296
                        \wp_send_json_error( 'Post not found', 400 );
×
297
                }
298

299
                \do_action( 'wpseo_save_compare_data', $post );
×
300

301
                // Initialize meta, amongst other things it registers sanitization.
302
                WPSEO_Meta::init();
×
303

304
                $social_fields = [];
×
305
                if ( $this->social_is_enabled ) {
×
306
                        $social_fields = WPSEO_Meta::get_meta_field_defs( 'social', $post->post_type );
×
307
                }
308

309
                // The below methods use the global post so make sure it is setup.
310
                \setup_postdata( $post );
×
311
                $meta_boxes = \apply_filters( 'wpseo_save_metaboxes', [] );
×
312
                $meta_boxes = \array_merge(
×
313
                        $meta_boxes,
×
314
                        WPSEO_Meta::get_meta_field_defs( 'general', $post->post_type ),
×
315
                        WPSEO_Meta::get_meta_field_defs( 'advanced', $post->post_type ),
×
316
                        $social_fields,
×
317
                        WPSEO_Meta::get_meta_field_defs( 'schema', $post->post_type )
×
318
                );
×
319

320
                foreach ( $meta_boxes as $key => $meta_box ) {
×
321
                        // If analysis is disabled remove that analysis score value from the DB.
322
                        if ( $this->is_meta_value_disabled( $key ) ) {
×
323
                                WPSEO_Meta::delete( $key, $post_id );
×
324
                                continue;
×
325
                        }
326

327
                        $data       = null;
×
328
                        $field_name = WPSEO_Meta::$form_prefix . $key;
×
329

330
                        if ( $meta_box['type'] === 'checkbox' ) {
×
331
                                $data = isset( $_POST[ $field_name ] ) ? 'on' : 'off';
×
332
                        }
333
                        else {
334
                                if ( isset( $_POST[ $field_name ] ) ) {
×
335
                                        // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: Sanitized through sanitize_post_meta.
336
                                        $data = \wp_unslash( $_POST[ $field_name ] );
×
337

338
                                        // For multi-select.
339
                                        if ( \is_array( $data ) ) {
×
340
                                                $data = \array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], $data );
×
341
                                        }
342

343
                                        if ( \is_string( $data ) ) {
×
344
                                                $data = ( $key !== 'canonical' ) ? WPSEO_Utils::sanitize_text_field( $data ) : WPSEO_Utils::sanitize_url( $data );
×
345
                                        }
346
                                }
347

348
                                // Reset options when no entry is present with multiselect - only applies to `meta-robots-adv` currently.
349
                                if ( ! isset( $_POST[ $field_name ] ) && ( $meta_box['type'] === 'multiselect' ) ) {
×
350
                                        $data = [];
×
351
                                }
352
                        }
353

354
                        if ( $data !== null ) {
×
355
                                WPSEO_Meta::set_value( $key, $data, $post_id );
×
356
                        }
357
                }
358

359
                if ( isset( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) && \is_string( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) ) {
×
360
                        $slug = \sanitize_title( \wp_unslash( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) );
×
361
                        if ( $post->post_name !== $slug ) {
×
362
                                $post_array              = $post->to_array();
×
363
                                $post_array['post_name'] = $slug;
×
364

365
                                $save_successful = \wp_insert_post( $post_array );
×
366
                                if ( \is_wp_error( $save_successful ) ) {
×
367
                                        \wp_send_json_error( 'Slug not saved', 400 );
×
368
                                }
369

370
                                // Update the post object to ensure we have the actual slug.
371
                                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Updating the post is needed to get the current slug.
372
                                $post = \get_post( $post_id );
×
373
                                if ( ! \is_object( $post ) ) {
×
374
                                        \wp_send_json_error( 'Updated slug not found', 400 );
×
375
                                }
376
                        }
377
                }
378

379
                \do_action( 'wpseo_saved_postdata' );
×
380

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

385
        /**
386
         * Determines if the given meta value key is disabled.
387
         *
388
         * @param string $key The key of the meta value.
389
         *
390
         * @return bool Whether the given meta value key is disabled.
391
         */
392
        public function is_meta_value_disabled( $key ) {
×
393
                if ( $key === 'linkdex' && ! $this->seo_analysis->is_enabled() ) {
×
394
                        return true;
×
395
                }
396

397
                if ( $key === 'content_score' && ! $this->readability_analysis->is_enabled() ) {
×
398
                        return true;
×
399
                }
400

401
                if ( $key === 'inclusive_language_score' && ! $this->inclusive_language_analysis->is_enabled() ) {
×
402
                        return true;
×
403
                }
404

405
                return false;
×
406
        }
407

408
        /**
409
         * Enqueues all the needed JS and CSS.
410
         *
411
         * @return void
412
         */
413
        public function enqueue() {
×
414
                $post_id = \get_queried_object_id();
×
415
                if ( empty( $post_id ) ) {
×
416
                        $post_id = 0;
×
417
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
418
                        if ( isset( $_GET['post'] ) && \is_string( $_GET['post'] ) ) {
×
419
                                // 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.
420
                                $post_id = (int) \wp_unslash( $_GET['post'] );
×
421
                        }
422
                }
423

424
                if ( $post_id !== 0 ) {
×
425
                        // Enqueue files needed for upload functionality.
426
                        \wp_enqueue_media( [ 'post' => $post_id ] );
×
427
                }
428

429
                $this->asset_manager->enqueue_style( 'admin-global' );
×
430
                $this->asset_manager->enqueue_style( 'metabox-css' );
×
431
                $this->asset_manager->enqueue_style( 'scoring' );
×
432
                $this->asset_manager->enqueue_style( 'monorepo' );
×
433
                $this->asset_manager->enqueue_style( 'admin-css' );
×
434
                $this->asset_manager->enqueue_style( 'ai-generator' );
×
435
                $this->asset_manager->enqueue_style( 'elementor' );
×
436

437
                $this->asset_manager->enqueue_script( 'admin-global' );
×
438
                $this->asset_manager->enqueue_script( 'elementor' );
×
439

440
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminGlobalL10n', \YoastSEO()->helpers->wincher->get_admin_global_links() );
×
441
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
×
442
                $this->asset_manager->localize_script( 'elementor', 'wpseoFeaturesL10n', WPSEO_Utils::retrieve_enabled_features() );
×
443

444
                $plugins_script_data = [
×
445
                        'replaceVars' => [
×
446
                                'no_parent_text'           => \__( '(no parent)', 'wordpress-seo' ),
×
447
                                'replace_vars'             => $this->get_replace_vars(),
×
448
                                'recommended_replace_vars' => $this->get_recommended_replace_vars(),
×
449
                                'hidden_replace_vars'      => $this->get_hidden_replace_vars(),
×
450
                                'scope'                    => $this->determine_scope(),
×
451
                                'has_taxonomies'           => $this->current_post_type_has_taxonomies(),
×
452
                        ],
×
453
                        'shortcodes'  => [
×
454
                                'wpseo_shortcode_tags'          => $this->get_valid_shortcode_tags(),
×
455
                                'wpseo_filter_shortcodes_nonce' => \wp_create_nonce( 'wpseo-filter-shortcodes' ),
×
456
                        ],
×
457
                ];
×
458

459
                $worker_script_data = [
×
460
                        'url'                     => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-analysis-worker' ),
×
461
                        'dependencies'            => \YoastSEO()->helpers->asset->get_dependency_urls_by_handle( 'yoast-seo-analysis-worker' ),
×
462
                        'keywords_assessment_url' => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-used-keywords-assessment' ),
×
463
                        'log_level'               => WPSEO_Utils::get_analysis_worker_log_level(),
×
464
                        // We need to make the feature flags separately available inside of the analysis web worker.
465
                        'enabled_features'        => WPSEO_Utils::retrieve_enabled_features(),
×
466
                ];
×
467

468
                $alert_dismissal_action  = \YoastSEO()->classes->get( Alert_Dismissal_Action::class );
×
469
                $dismissed_alerts        = $alert_dismissal_action->all_dismissed();
×
470
                $woocommerce_conditional = new WooCommerce_Conditional();
×
471

472
                $script_data = [
×
473
                        'media'                     => [ 'choose_image' => \__( 'Use Image', 'wordpress-seo' ) ],
×
474
                        'metabox'                   => $this->get_metabox_script_data(),
×
475
                        'userLanguageCode'          => WPSEO_Language_Utils::get_language( \get_user_locale() ),
×
476
                        'isPost'                    => true,
×
477
                        'isBlockEditor'             => WP_Screen::get()->is_block_editor(),
×
478
                        'isElementorEditor'         => true,
×
479
                        'isWooCommerceActive'       => $woocommerce_conditional->is_met(),
×
480
                        'postStatus'                => \get_post_status( $post_id ),
×
481
                        'postType'                  => \get_post_type( $post_id ),
×
482
                        'analysis'                  => [
×
483
                                'plugins' => $plugins_script_data,
×
484
                                'worker'  => $worker_script_data,
×
485
                        ],
×
486
                        'dismissedAlerts'           => $dismissed_alerts,
×
487
                        'webinarIntroElementorUrl'  => WPSEO_Shortlinker::get( 'https://yoa.st/webinar-intro-elementor' ),
×
488
                        'currentPromotions'         => $this->promotion_manager->get_current_promotions(),
×
489
                        'usedKeywordsNonce'         => \wp_create_nonce( 'wpseo-keyword-usage-and-post-types' ),
×
490
                        'linkParams'                => WPSEO_Shortlinker::get_query_params(),
×
491
                        'pluginUrl'                 => \plugins_url( '', \WPSEO_FILE ),
×
492
                        'wistiaEmbedPermission'     => \YoastSEO()->classes->get( Wistia_Embed_Permission_Repository::class )->get_value_for_user( \get_current_user_id() ),
×
493
                ];
×
494

495
                if ( \post_type_supports( $this->get_metabox_post()->post_type, 'thumbnail' ) ) {
×
496
                        $this->asset_manager->enqueue_style( 'featured-image' );
×
497

498
                        $script_data['featuredImage'] = [
×
499
                                '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' ),
×
500
                        ];
×
501
                }
502

503
                $this->asset_manager->localize_script( 'elementor', 'wpseoScriptData', $script_data );
×
504
                $this->asset_manager->enqueue_user_language_script();
×
505
        }
506

507
        /**
508
         * Renders the metabox hidden fields.
509
         *
510
         * @return void
511
         */
512
        protected function render_hidden_fields() {
×
513
                // Wrap in a form with an action and post_id for the submit.
514
                \printf(
×
515
                        '<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" />',
×
516
                        \esc_url( \admin_url( 'admin-ajax.php' ) ),
×
517
                        \esc_attr( $this->get_metabox_post()->ID )
×
518
                );
×
519

520
                \wp_nonce_field( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
×
521

522
                $groups = $this->metadata_groups->get_post_metadata_groups();
×
523

524
                foreach ( $groups as $group ) {
×
525
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in class.
526
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), $group, $this->get_metabox_post()->post_type );
×
527
                }
528

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

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

538
                echo '</form>';
×
539
        }
540

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

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

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

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

565
                return $post->post_name;
×
566
        }
567

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

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

585
                if ( ! empty( $post ) ) {
×
586
                        $this->post = \get_post( $post );
×
587

588
                        return $this->post;
×
589
                }
590

591
                if ( isset( $GLOBALS['post'] ) ) {
×
592
                        $this->post = $GLOBALS['post'];
×
593

594
                        return $this->post;
×
595
                }
596

597
                return null;
×
598
        }
599

600
        /**
601
         * Passes variables to js for use with the post-scraper.
602
         *
603
         * @return array
604
         */
605
        protected function get_metabox_script_data() {
×
606
                $permalink = '';
×
607

608
                if ( \is_object( $this->get_metabox_post() ) ) {
×
609
                        $permalink = \get_sample_permalink( $this->get_metabox_post()->ID );
×
610
                        $permalink = $permalink[0];
×
611
                }
612

613
                $post_formatter = new WPSEO_Metabox_Formatter(
×
614
                        new WPSEO_Post_Metabox_Formatter( $this->get_metabox_post(), [], $permalink )
×
615
                );
×
616

617
                $values = $post_formatter->get_values();
×
618

619
                /** This filter is documented in admin/filters/class-cornerstone-filter.php. */
620
                $post_types = \apply_filters( 'wpseo_cornerstone_post_types', \YoastSEO()->helpers->post_type->get_accessible_post_types() );
×
621
                if ( $values['cornerstoneActive'] && ! \in_array( $this->get_metabox_post()->post_type, $post_types, true ) ) {
×
622
                        $values['cornerstoneActive'] = false;
×
623
                }
624

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

627
                return $values;
×
628
        }
629

630
        /**
631
         * Checks whether the highlighting functionality is available for Elementor:
632
         * - in Free it's always available (as an upsell).
633
         * - in Premium it's available as long as the version is 21.8-RC0 or above.
634
         *
635
         * @return bool Whether the highlighting functionality is available.
636
         */
637
        private function is_highlighting_available() {
×
638
                $is_premium      = \YoastSEO()->helpers->product->is_premium();
×
639
                $premium_version = \YoastSEO()->helpers->product->get_premium_version();
×
640

641
                return ! $is_premium || \version_compare( $premium_version, '21.8-RC0', '>=' );
×
642
        }
643

644
        /**
645
         * Prepares the replace vars for localization.
646
         *
647
         * @return array Replace vars.
648
         */
649
        protected function get_replace_vars() {
×
650
                $cached_replacement_vars = [];
×
651

652
                $vars_to_cache = [
×
653
                        'date',
×
654
                        'id',
×
655
                        'sitename',
×
656
                        'sitedesc',
×
657
                        'sep',
×
658
                        'page',
×
659
                        'currentyear',
×
660
                        'currentdate',
×
661
                        'currentmonth',
×
662
                        'currentday',
×
663
                        'tag',
×
664
                        'category',
×
665
                        'category_title',
×
666
                        'primary_category',
×
667
                        'pt_single',
×
668
                        'pt_plural',
×
669
                        'modified',
×
670
                        'name',
×
671
                        'user_description',
×
672
                        'pagetotal',
×
673
                        'pagenumber',
×
674
                        'post_year',
×
675
                        'post_month',
×
676
                        'post_day',
×
677
                        'author_first_name',
×
678
                        'author_last_name',
×
679
                        'permalink',
×
680
                        'post_content',
×
681
                ];
×
682

683
                foreach ( $vars_to_cache as $var ) {
×
684
                        $cached_replacement_vars[ $var ] = \wpseo_replace_vars( '%%' . $var . '%%', $this->get_metabox_post() );
×
685
                }
686

687
                // Merge custom replace variables with the WordPress ones.
688
                return \array_merge( $cached_replacement_vars, $this->get_custom_replace_vars( $this->get_metabox_post() ) );
×
689
        }
690

691
        /**
692
         * Prepares the recommended replace vars for localization.
693
         *
694
         * @return array Recommended replacement variables.
695
         */
696
        protected function get_recommended_replace_vars() {
×
697
                $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
×
698

699
                // What is recommended depends on the current context.
700
                $post_type = $recommended_replace_vars->determine_for_post( $this->get_metabox_post() );
×
701

702
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
703
        }
704

705
        /**
706
         * Returns the list of replace vars that should be hidden inside the editor.
707
         *
708
         * @return string[] The hidden replace vars.
709
         */
710
        protected function get_hidden_replace_vars() {
×
711
                return ( new WPSEO_Replace_Vars() )->get_hidden_replace_vars();
×
712
        }
713

714
        /**
715
         * Gets the custom replace variables for custom taxonomies and fields.
716
         *
717
         * @param WP_Post $post The post to check for custom taxonomies and fields.
718
         *
719
         * @return array Array containing all the replacement variables.
720
         */
721
        protected function get_custom_replace_vars( $post ) {
×
722
                return [
×
723
                        'custom_fields'     => $this->get_custom_fields_replace_vars( $post ),
×
724
                        'custom_taxonomies' => $this->get_custom_taxonomies_replace_vars( $post ),
×
725
                ];
×
726
        }
727

728
        /**
729
         * Gets the custom replace variables for custom taxonomies.
730
         *
731
         * @param WP_Post $post The post to check for custom taxonomies.
732
         *
733
         * @return array Array containing all the replacement variables.
734
         */
735
        protected function get_custom_taxonomies_replace_vars( $post ) {
×
736
                $taxonomies          = \get_object_taxonomies( $post, 'objects' );
×
737
                $custom_replace_vars = [];
×
738

739
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
740

741
                        if ( \is_string( $taxonomy ) ) { // If attachment, see https://core.trac.wordpress.org/ticket/37368 .
×
742
                                $taxonomy_name = $taxonomy;
×
743
                                $taxonomy      = \get_taxonomy( $taxonomy_name );
×
744
                        }
745

746
                        if ( $taxonomy->_builtin && $taxonomy->public ) {
×
747
                                continue;
×
748
                        }
749

750
                        $custom_replace_vars[ $taxonomy_name ] = [
×
751
                                'name'        => $taxonomy->name,
×
752
                                'description' => $taxonomy->description,
×
753
                        ];
×
754
                }
755

756
                return $custom_replace_vars;
×
757
        }
758

759
        /**
760
         * Gets the custom replace variables for custom fields.
761
         *
762
         * @param WP_Post $post The post to check for custom fields.
763
         *
764
         * @return array Array containing all the replacement variables.
765
         */
766
        protected function get_custom_fields_replace_vars( $post ) {
×
767
                $custom_replace_vars = [];
×
768

769
                // If no post object is passed, return the empty custom_replace_vars array.
770
                if ( ! \is_object( $post ) ) {
×
771
                        return $custom_replace_vars;
×
772
                }
773

774
                $custom_fields = \get_post_custom( $post->ID );
×
775

776
                // Simply concatenate all fields containing replace vars so we can handle them all with a single regex find.
777
                $replace_vars_fields = \implode(
×
778
                        ' ',
×
779
                        [
×
780
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->title,
×
781
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->meta_description,
×
782
                        ]
×
783
                );
×
784

785
                \preg_match_all( '/%%cf_([A-Za-z0-9_]+)%%/', $replace_vars_fields, $matches );
×
786
                $fields_to_include = $matches[1];
×
787
                foreach ( $custom_fields as $custom_field_name => $custom_field ) {
×
788
                        // Skip private custom fields.
789
                        if ( \substr( $custom_field_name, 0, 1 ) === '_' ) {
×
790
                                continue;
×
791
                        }
792

793
                        // Skip custom fields that are not used, new ones will be fetched dynamically.
794
                        if ( ! \in_array( $custom_field_name, $fields_to_include, true ) ) {
×
795
                                continue;
×
796
                        }
797

798
                        // Skip custom field values that are serialized.
799
                        if ( \is_serialized( $custom_field[0] ) ) {
×
800
                                continue;
×
801
                        }
802

803
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
804
                }
805

806
                return $custom_replace_vars;
×
807
        }
808

809
        /**
810
         * Determines the scope based on the post type.
811
         * This can be used by the replacevar plugin to determine if a replacement needs to be executed.
812
         *
813
         * @return string String describing the current scope.
814
         */
815
        protected function determine_scope() {
×
816
                if ( $this->get_metabox_post()->post_type === 'page' ) {
×
817
                        return 'page';
×
818
                }
819

820
                return 'post';
×
821
        }
822

823
        /**
824
         * Determines whether or not the current post type has registered taxonomies.
825
         *
826
         * @return bool Whether the current post type has taxonomies.
827
         */
828
        protected function current_post_type_has_taxonomies() {
×
829
                $post_taxonomies = \get_object_taxonomies( $this->get_metabox_post()->post_type );
×
830

831
                return ! empty( $post_taxonomies );
×
832
        }
833

834
        /**
835
         * Returns an array with shortcode tags for all registered shortcodes.
836
         *
837
         * @return array
838
         */
839
        protected function get_valid_shortcode_tags() {
×
840
                $shortcode_tags = [];
×
841

842
                foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
×
843
                        $shortcode_tags[] = $tag;
×
844
                }
845

846
                return $shortcode_tags;
×
847
        }
848
}
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