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

Yoast / wordpress-seo / 6638054992

25 Oct 2023 08:55AM UTC coverage: 49.106%. First build
6638054992

Pull #20653

github

vraja-pro
Merge remote-tracking branch 'origin/feature/upgrade-react-and-tests' into feature/upgrade-react-and-tests
Pull Request #20653: Feature/upgrade react and tests

64 of 157 new or added lines in 34 files covered. (40.76%)

13135 of 26748 relevant lines covered (49.11%)

3.96 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\Helpers\Capability_Helper;
25
use Yoast\WP\SEO\Helpers\Options_Helper;
26
use Yoast\WP\SEO\Integrations\Integration_Interface;
27
use Yoast\WP\SEO\Introductions\Infrastructure\Wistia_Embed_Permission_Repository;
28
use Yoast\WP\SEO\Presenters\Admin\Meta_Fields_Presenter;
29
use Yoast\WP\SEO\Promotions\Application\Promotion_Manager;
30

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

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

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

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

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

62
        /**
63
         * Represents the capability helper.
64
         *
65
         * @var Capability_Helper
66
         */
67
        protected $capability;
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 Promotion_Manager         $promotion_manager The promotion manager.
127
         */
128
        public function __construct(
129
                WPSEO_Admin_Asset_Manager $asset_manager,
130
                Options_Helper $options,
131
                Capability_Helper $capability,
132
                Promotion_Manager $promotion_manager
133
        ) {
134
                $this->asset_manager     = $asset_manager;
×
135
                $this->options           = $options;
×
136
                $this->capability        = $capability;
×
137
                $this->promotion_manager = $promotion_manager;
×
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
        public function register_elementor_hooks() {
165

166
                if ( $this->get_metabox_post() === null || ! $this->display_metabox( $this->get_metabox_post()->post_type ) ) {
×
167
                        return;
×
168
                }
169

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

172
                // 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).
173
                if ( ! \did_action( 'elementor/init' ) ) {
×
174
                        \add_action( 'elementor/init', [ $this, 'add_yoast_panel_tab' ] );
×
175
                }
176
                else {
177
                        $this->add_yoast_panel_tab();
×
178
                }
179
                \add_action( 'elementor/documents/register_controls', [ $this, 'register_document_controls' ] );
×
180
        }
181

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

193
        /**
194
         * Register a panel tab slug, in order to allow adding controls to this tab.
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
        public function register_document_controls( $document ) {
206
                // PageBase is the base class for documents like `post` `page` and etc.
207
                if ( ! $document instanceof PageBase || ! $document::get_property( 'has_elements' ) ) {
×
208
                        return;
×
209
                }
210

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

220
                $document->end_controls_section();
×
221
        }
222

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

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

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

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

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

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

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

266
                \check_ajax_referer( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
×
267

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

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

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

282
                \do_action( 'wpseo_save_compare_data', $post );
×
283

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

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

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

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

310
                        $data       = null;
×
311
                        $field_name = WPSEO_Meta::$form_prefix . $key;
×
312

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

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

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

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

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

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

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

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

362
                \do_action( 'wpseo_saved_postdata' );
×
363

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

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

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

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

388
                return false;
×
389
        }
390

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

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

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

420
                $this->asset_manager->enqueue_script( 'admin-global' );
×
421
                $this->asset_manager->enqueue_script( 'elementor' );
×
422

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

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

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

450
                $alert_dismissal_action  = \YoastSEO()->classes->get( Alert_Dismissal_Action::class );
×
451
                $dismissed_alerts        = $alert_dismissal_action->all_dismissed();
×
452
                $woocommerce_conditional = new WooCommerce_Conditional();
×
453

454
                $script_data = [
455
                        'media'                     => [ 'choose_image' => \__( 'Use Image', 'wordpress-seo' ) ],
×
456
                        'metabox'                   => $this->get_metabox_script_data(),
×
457
                        'userLanguageCode'          => WPSEO_Language_Utils::get_language( \get_user_locale() ),
×
458
                        'isPost'                    => true,
459
                        'isBlockEditor'             => WP_Screen::get()->is_block_editor(),
×
460
                        'isElementorEditor'         => true,
461
                        'isWooCommerceActive'       => $woocommerce_conditional->is_met(),
×
462
                        'postStatus'                => \get_post_status( $post_id ),
×
463
                        'postType'                  => \get_post_type( $post_id ),
×
464
                        'analysis'                  => [
465
                                'plugins' => $plugins_script_data,
×
466
                                'worker'  => $worker_script_data,
×
467
                        ],
468
                        'dismissedAlerts'           => $dismissed_alerts,
×
469
                        'webinarIntroElementorUrl'  => WPSEO_Shortlinker::get( 'https://yoa.st/webinar-intro-elementor' ),
×
NEW
470
                        'currentPromotions'         => $this->promotion_manager->get_current_promotions(),
×
471
                        'usedKeywordsNonce'         => \wp_create_nonce( 'wpseo-keyword-usage-and-post-types' ),
×
472
                        'linkParams'                => WPSEO_Shortlinker::get_query_params(),
×
473
                        'pluginUrl'                 => \plugins_url( '', \WPSEO_FILE ),
×
474
                        'wistiaEmbedPermission'     => \YoastSEO()->classes->get( Wistia_Embed_Permission_Repository::class )->get_value_for_user( \get_current_user_id() ),
×
475
                ];
476

477
                if ( \post_type_supports( $this->get_metabox_post()->post_type, 'thumbnail' ) ) {
×
478
                        $this->asset_manager->enqueue_style( 'featured-image' );
×
479

480
                        $script_data['featuredImage'] = [
×
481
                                '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' ),
×
482
                        ];
483
                }
484

485
                $this->asset_manager->localize_script( 'elementor', 'wpseoScriptData', $script_data );
×
486
                $this->asset_manager->enqueue_user_language_script();
×
487
        }
488

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

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

506
                if ( $this->is_advanced_metadata_enabled ) {
×
507
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
508
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'advanced' );
×
509
                }
510

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

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

519
                \printf(
×
520
                        '<input type="hidden" id="%1$s" name="%1$s" value="%2$s" />',
×
521
                        \esc_attr( WPSEO_Meta::$form_prefix . 'slug' ),
×
522
                        \esc_attr( $this->get_post_slug() )
×
523
                );
524

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

528
                echo '</form>';
×
529
        }
530

531
        /**
532
         * Returns the slug for the post being edited.
533
         *
534
         * @return string
535
         */
536
        protected function get_post_slug() {
537
                $post = $this->get_metabox_post();
×
538

539
                // In case get_metabox_post returns null for whatever reason.
540
                if ( ! $post instanceof WP_Post ) {
×
541
                        return '';
×
542
                }
543

544
                // Drafts might not have a post_name unless the slug has been manually changed.
545
                // In this case we get it using get_sample_permalink.
546
                if ( ! $post->post_name ) {
×
547
                        $sample = \get_sample_permalink( $post );
×
548

549
                        // Since get_sample_permalink runs through filters, ensure that it has the expected return value.
550
                        if ( \is_array( $sample ) && \count( $sample ) === 2 && \is_string( $sample[1] ) ) {
×
551
                                return $sample[1];
×
552
                        }
553
                }
554

555
                return $post->post_name;
×
556
        }
557

558
        /**
559
         * Returns post in metabox context.
560
         *
561
         * @return WP_Post|null
562
         */
563
        protected function get_metabox_post() {
564
                if ( $this->post !== null ) {
×
565
                        return $this->post;
×
566
                }
567

568
                $post = null;
×
569
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
570
                if ( isset( $_GET['post'] ) && \is_numeric( $_GET['post'] ) ) {
×
571
                        // 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.
572
                        $post = (int) \wp_unslash( $_GET['post'] );
×
573
                }
574

575
                if ( ! empty( $post ) ) {
×
576
                        $this->post = \get_post( $post );
×
577

578
                        return $this->post;
×
579
                }
580

581
                if ( isset( $GLOBALS['post'] ) ) {
×
582
                        $this->post = $GLOBALS['post'];
×
583

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

587
                return null;
×
588
        }
589

590
        /**
591
         * Passes variables to js for use with the post-scraper.
592
         *
593
         * @return array
594
         */
595
        protected function get_metabox_script_data() {
596
                $permalink = '';
×
597

598
                if ( \is_object( $this->get_metabox_post() ) ) {
×
599
                        $permalink = \get_sample_permalink( $this->get_metabox_post()->ID );
×
600
                        $permalink = $permalink[0];
×
601
                }
602

603
                $post_formatter = new WPSEO_Metabox_Formatter(
×
604
                        new WPSEO_Post_Metabox_Formatter( $this->get_metabox_post(), [], $permalink )
×
605
                );
606

607
                $values = $post_formatter->get_values();
×
608

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

615
                return $values;
×
616
        }
617

618
        /**
619
         * Prepares the replace vars for localization.
620
         *
621
         * @return array Replace vars.
622
         */
623
        protected function get_replace_vars() {
624
                $cached_replacement_vars = [];
×
625

626
                $vars_to_cache = [
627
                        'date',
×
628
                        'id',
629
                        'sitename',
630
                        'sitedesc',
631
                        'sep',
632
                        'page',
633
                        'currentyear',
634
                        'currentdate',
635
                        'currentmonth',
636
                        'currentday',
637
                        'tag',
638
                        'category',
639
                        'category_title',
640
                        'primary_category',
641
                        'pt_single',
642
                        'pt_plural',
643
                        'modified',
644
                        'name',
645
                        'user_description',
646
                        'pagetotal',
647
                        'pagenumber',
648
                        'post_year',
649
                        'post_month',
650
                        'post_day',
651
                        'author_first_name',
652
                        'author_last_name',
653
                        'permalink',
654
                        'post_content',
655
                ];
656

657
                foreach ( $vars_to_cache as $var ) {
×
658
                        $cached_replacement_vars[ $var ] = \wpseo_replace_vars( '%%' . $var . '%%', $this->get_metabox_post() );
×
659
                }
660

661
                // Merge custom replace variables with the WordPress ones.
662
                return \array_merge( $cached_replacement_vars, $this->get_custom_replace_vars( $this->get_metabox_post() ) );
×
663
        }
664

665
        /**
666
         * Prepares the recommended replace vars for localization.
667
         *
668
         * @return array Recommended replacement variables.
669
         */
670
        protected function get_recommended_replace_vars() {
671
                $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
×
672

673
                // What is recommended depends on the current context.
674
                $post_type = $recommended_replace_vars->determine_for_post( $this->get_metabox_post() );
×
675

676
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
677
        }
678

679
        /**
680
         * Returns the list of replace vars that should be hidden inside the editor.
681
         *
682
         * @return string[] The hidden replace vars.
683
         */
684
        protected function get_hidden_replace_vars() {
685
                return ( new WPSEO_Replace_Vars() )->get_hidden_replace_vars();
×
686
        }
687

688
        /**
689
         * Gets the custom replace variables for custom taxonomies and fields.
690
         *
691
         * @param WP_Post $post The post to check for custom taxonomies and fields.
692
         *
693
         * @return array Array containing all the replacement variables.
694
         */
695
        protected function get_custom_replace_vars( $post ) {
696
                return [
697
                        'custom_fields'     => $this->get_custom_fields_replace_vars( $post ),
×
698
                        'custom_taxonomies' => $this->get_custom_taxonomies_replace_vars( $post ),
×
699
                ];
700
        }
701

702
        /**
703
         * Gets the custom replace variables for custom taxonomies.
704
         *
705
         * @param WP_Post $post The post to check for custom taxonomies.
706
         *
707
         * @return array Array containing all the replacement variables.
708
         */
709
        protected function get_custom_taxonomies_replace_vars( $post ) {
710
                $taxonomies          = \get_object_taxonomies( $post, 'objects' );
×
711
                $custom_replace_vars = [];
×
712

713
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
714

715
                        if ( \is_string( $taxonomy ) ) { // If attachment, see https://core.trac.wordpress.org/ticket/37368 .
×
716
                                $taxonomy_name = $taxonomy;
×
717
                                $taxonomy      = \get_taxonomy( $taxonomy_name );
×
718
                        }
719

720
                        if ( $taxonomy->_builtin && $taxonomy->public ) {
×
721
                                continue;
×
722
                        }
723

724
                        $custom_replace_vars[ $taxonomy_name ] = [
×
725
                                'name'        => $taxonomy->name,
×
726
                                'description' => $taxonomy->description,
×
727
                        ];
728
                }
729

730
                return $custom_replace_vars;
×
731
        }
732

733
        /**
734
         * Gets the custom replace variables for custom fields.
735
         *
736
         * @param WP_Post $post The post to check for custom fields.
737
         *
738
         * @return array Array containing all the replacement variables.
739
         */
740
        protected function get_custom_fields_replace_vars( $post ) {
741
                $custom_replace_vars = [];
×
742

743
                // If no post object is passed, return the empty custom_replace_vars array.
744
                if ( ! \is_object( $post ) ) {
×
745
                        return $custom_replace_vars;
×
746
                }
747

748
                $custom_fields = \get_post_custom( $post->ID );
×
749

750
                // Simply concatenate all fields containing replace vars so we can handle them all with a single regex find.
751
                $replace_vars_fields = \implode(
×
752
                        ' ',
×
753
                        [
754
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->title,
×
755
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->meta_description,
×
756
                        ]
757
                );
758

759
                \preg_match_all( '/%%cf_([A-Za-z0-9_]+)%%/', $replace_vars_fields, $matches );
×
760
                $fields_to_include = $matches[1];
×
761
                foreach ( $custom_fields as $custom_field_name => $custom_field ) {
×
762
                        // Skip private custom fields.
763
                        if ( \substr( $custom_field_name, 0, 1 ) === '_' ) {
×
764
                                continue;
×
765
                        }
766

767
                        // Skip custom fields that are not used, new ones will be fetched dynamically.
768
                        if ( ! \in_array( $custom_field_name, $fields_to_include, true ) ) {
×
769
                                continue;
×
770
                        }
771

772
                        // Skip custom field values that are serialized.
773
                        if ( \is_serialized( $custom_field[0] ) ) {
×
774
                                continue;
×
775
                        }
776

777
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
778
                }
779

780
                return $custom_replace_vars;
×
781
        }
782

783
        /**
784
         * Determines the scope based on the post type.
785
         * This can be used by the replacevar plugin to determine if a replacement needs to be executed.
786
         *
787
         * @return string String describing the current scope.
788
         */
789
        protected function determine_scope() {
790
                if ( $this->get_metabox_post()->post_type === 'page' ) {
×
791
                        return 'page';
×
792
                }
793

794
                return 'post';
×
795
        }
796

797
        /**
798
         * Determines whether or not the current post type has registered taxonomies.
799
         *
800
         * @return bool Whether the current post type has taxonomies.
801
         */
802
        protected function current_post_type_has_taxonomies() {
803
                $post_taxonomies = \get_object_taxonomies( $this->get_metabox_post()->post_type );
×
804

805
                return ! empty( $post_taxonomies );
×
806
        }
807

808
        /**
809
         * Returns an array with shortcode tags for all registered shortcodes.
810
         *
811
         * @return array
812
         */
813
        protected function get_valid_shortcode_tags() {
814
                $shortcode_tags = [];
×
815

816
                foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
×
817
                        $shortcode_tags[] = $tag;
×
818
                }
819

820
                return $shortcode_tags;
×
821
        }
822
}
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

© 2025 Coveralls, Inc