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

Yoast / wordpress-seo / 5066322038

pending completion
5066322038

push

github

GitHub
Merge pull request #20316 from Yoast/JRF/ghactions-run-more-selectively

2550 of 29012 relevant lines covered (8.79%)

0.32 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\Helpers\Capability_Helper;
24
use Yoast\WP\SEO\Helpers\Options_Helper;
25
use Yoast\WP\SEO\Integrations\Integration_Interface;
26
use Yoast\WP\SEO\Presenters\Admin\Meta_Fields_Presenter;
27

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

33
        /**
34
         * The identifier for the elementor tab.
35
         */
36
        const YOAST_TAB = 'yoast-tab';
37

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

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

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

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

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

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

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

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

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

101
        /**
102
         * Returns the conditionals based in which this loadable should be active.
103
         *
104
         * @return array
105
         */
106
        public static function get_conditionals() {
107
                return [ Elementor_Edit_Conditional::class ];
×
108
        }
109

110
        /**
111
         * Constructor.
112
         *
113
         * @param WPSEO_Admin_Asset_Manager $asset_manager The asset manager.
114
         * @param Options_Helper            $options       The options helper.
115
         * @param Capability_Helper         $capability    The capability helper.
116
         */
117
        public function __construct(
118
                WPSEO_Admin_Asset_Manager $asset_manager,
119
                Options_Helper $options,
120
                Capability_Helper $capability
121
        ) {
122
                $this->asset_manager = $asset_manager;
×
123
                $this->options       = $options;
×
124
                $this->capability    = $capability;
×
125

126
                $this->seo_analysis                 = new WPSEO_Metabox_Analysis_SEO();
×
127
                $this->readability_analysis         = new WPSEO_Metabox_Analysis_Readability();
×
128
                $this->inclusive_language_analysis  = new WPSEO_Metabox_Analysis_Inclusive_Language();
×
129
                $this->social_is_enabled            = $this->options->get( 'opengraph', false ) || $this->options->get( 'twitter', false );
×
130
                $this->is_advanced_metadata_enabled = $this->capability->current_user_can( 'wpseo_edit_advanced_metadata' ) || $this->options->get( 'disableadvanced_meta' ) === false;
×
131
        }
132

133
        /**
134
         * Initializes the integration.
135
         *
136
         * This is the place to register hooks and filters.
137
         *
138
         * @return void
139
         */
140
        public function register_hooks() {
141
                \add_action( 'wp_ajax_wpseo_elementor_save', [ $this, 'save_postdata' ] );
×
142

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

147
        /**
148
         * Registers our Elementor hooks.
149
         */
150
        public function register_elementor_hooks() {
151
                if ( ! $this->display_metabox( $this->get_metabox_post()->post_type ) ) {
×
152
                        return;
×
153
                }
154

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

157
                // 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).
158
                if ( ! \did_action( 'elementor/init' ) ) {
×
159
                        \add_action( 'elementor/init', [ $this, 'add_yoast_panel_tab' ] );
×
160
                }
161
                else {
162
                        $this->add_yoast_panel_tab();
×
163
                }
164
                \add_action( 'elementor/documents/register_controls', [ $this, 'register_document_controls' ] );
×
165
        }
166

167
        /**
168
         * Initializes the integration.
169
         *
170
         * @return void
171
         */
172
        public function init() {
173
                $this->asset_manager->register_assets();
×
174
                $this->enqueue();
×
175
                $this->render_hidden_fields();
×
176
        }
177

178
        /**
179
         * Register a panel tab slug, in order to allow adding controls to this tab.
180
         */
181
        public function add_yoast_panel_tab() {
182
                Controls_Manager::add_tab( $this::YOAST_TAB, 'Yoast SEO' );
×
183
        }
184

185
        /**
186
         * Register additional document controls.
187
         *
188
         * @param PageBase $document The PageBase document.
189
         */
190
        public function register_document_controls( $document ) {
191
                // PageBase is the base class for documents like `post` `page` and etc.
192
                if ( ! $document instanceof PageBase || ! $document::get_property( 'has_elements' ) ) {
×
193
                        return;
×
194
                }
195

196
                // This is needed to get the tab to appear, but will be overwritten in the JavaScript.
197
                $document->start_controls_section(
×
198
                        'yoast_temporary_section',
×
199
                        [
×
200
                                'label' => 'Yoast SEO',
×
201
                                'tab'   => self::YOAST_TAB,
×
202
                        ]
×
203
                );
×
204

205
                $document->end_controls_section();
×
206
        }
207

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

210
        /**
211
         * Determines whether the metabox should be shown for the passed identifier.
212
         *
213
         * By default, the check is done for post types, but can also be used for taxonomies.
214
         *
215
         * @param string|null $identifier The identifier to check.
216
         * @param string      $type       The type of object to check. Defaults to post_type.
217
         *
218
         * @return bool Whether the metabox should be displayed.
219
         */
220
        public function display_metabox( $identifier = null, $type = 'post_type' ) {
221
                return WPSEO_Utils::is_metabox_active( $identifier, $type );
×
222
        }
223

224
        /**
225
         * Saves the WP SEO metadata for posts.
226
         *
227
         * Outputs JSON via wp_send_json then stops code execution.
228
         *
229
         * {@internal $_POST parameters are validated via sanitize_post_meta().}}
230
         *
231
         * @return void
232
         */
233
        public function save_postdata() {
234
                global $post;
×
235

236
                if ( ! isset( $_POST['post_id'] ) || ! \is_string( $_POST['post_id'] ) ) {
×
237
                        \wp_send_json_error( 'Bad Request', 400 );
×
238
                }
239

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

243
                if ( $post_id <= 0 ) {
×
244
                        \wp_send_json_error( 'Bad Request', 400 );
×
245
                }
246

247
                if ( ! \current_user_can( 'edit_post', $post_id ) ) {
×
248
                        \wp_send_json_error( 'Forbidden', 403 );
×
249
                }
250

251
                \check_ajax_referer( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
×
252

253
                // Bail if this is a multisite installation and the site has been switched.
254
                if ( \is_multisite() && \ms_is_switched() ) {
×
255
                        \wp_send_json_error( 'Switched multisite', 409 );
×
256
                }
257

258
                \clean_post_cache( $post_id );
×
259
                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- To setup the post we need to do this explicitly.
260
                $post = \get_post( $post_id );
×
261

262
                if ( ! \is_object( $post ) ) {
×
263
                        // Non-existent post.
264
                        \wp_send_json_error( 'Post not found', 400 );
×
265
                }
266

267
                \do_action( 'wpseo_save_compare_data', $post );
×
268

269
                // Initialize meta, amongst other things it registers sanitization.
270
                WPSEO_Meta::init();
×
271

272
                $social_fields = [];
×
273
                if ( $this->social_is_enabled ) {
×
274
                        $social_fields = WPSEO_Meta::get_meta_field_defs( 'social', $post->post_type );
×
275
                }
276

277
                // The below methods use the global post so make sure it is setup.
278
                \setup_postdata( $post );
×
279
                $meta_boxes = \apply_filters( 'wpseo_save_metaboxes', [] );
×
280
                $meta_boxes = \array_merge(
×
281
                        $meta_boxes,
×
282
                        WPSEO_Meta::get_meta_field_defs( 'general', $post->post_type ),
×
283
                        WPSEO_Meta::get_meta_field_defs( 'advanced', $post->post_type ),
×
284
                        $social_fields,
×
285
                        WPSEO_Meta::get_meta_field_defs( 'schema', $post->post_type )
×
286
                );
×
287

288
                foreach ( $meta_boxes as $key => $meta_box ) {
×
289
                        // If analysis is disabled remove that analysis score value from the DB.
290
                        if ( $this->is_meta_value_disabled( $key ) ) {
×
291
                                WPSEO_Meta::delete( $key, $post_id );
×
292
                                continue;
×
293
                        }
294

295
                        $data       = null;
×
296
                        $field_name = WPSEO_Meta::$form_prefix . $key;
×
297

298
                        if ( $meta_box['type'] === 'checkbox' ) {
×
299
                                $data = isset( $_POST[ $field_name ] ) ? 'on' : 'off';
×
300
                        }
301
                        else {
302
                                if ( isset( $_POST[ $field_name ] ) ) {
×
303
                                        // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: Sanitized through sanitize_post_meta.
304
                                        $data = \wp_unslash( $_POST[ $field_name ] );
×
305

306
                                        // For multi-select.
307
                                        if ( \is_array( $data ) ) {
×
308
                                                $data = \array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], $data );
×
309
                                        }
310

311
                                        if ( \is_string( $data ) ) {
×
312
                                                $data = ( $key !== 'canonical' ) ? WPSEO_Utils::sanitize_text_field( $data ) : WPSEO_Utils::sanitize_url( $data );
×
313
                                        }
314
                                }
315

316
                                // Reset options when no entry is present with multiselect - only applies to `meta-robots-adv` currently.
317
                                if ( ! isset( $_POST[ $field_name ] ) && ( $meta_box['type'] === 'multiselect' ) ) {
×
318
                                        $data = [];
×
319
                                }
320
                        }
321

322
                        if ( $data !== null ) {
×
323
                                WPSEO_Meta::set_value( $key, $data, $post_id );
×
324
                        }
325
                }
326

327
                if ( isset( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) && \is_string( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) ) {
×
328
                        $slug = \sanitize_title( \wp_unslash( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) );
×
329
                        if ( $post->post_name !== $slug ) {
×
330
                                $post_array              = $post->to_array();
×
331
                                $post_array['post_name'] = $slug;
×
332

333
                                $save_successful = \wp_insert_post( $post_array );
×
334
                                if ( \is_wp_error( $save_successful ) ) {
×
335
                                        \wp_send_json_error( 'Slug not saved', 400 );
×
336
                                }
337

338
                                // Update the post object to ensure we have the actual slug.
339
                                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Updating the post is needed to get the current slug.
340
                                $post = \get_post( $post_id );
×
341
                                if ( ! \is_object( $post ) ) {
×
342
                                        \wp_send_json_error( 'Updated slug not found', 400 );
×
343
                                }
344
                        }
345
                }
346

347
                \do_action( 'wpseo_saved_postdata' );
×
348

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

353
        /**
354
         * Determines if the given meta value key is disabled.
355
         *
356
         * @param string $key The key of the meta value.
357
         *
358
         * @return bool Whether the given meta value key is disabled.
359
         */
360
        public function is_meta_value_disabled( $key ) {
361
                if ( $key === 'linkdex' && ! $this->seo_analysis->is_enabled() ) {
×
362
                        return true;
×
363
                }
364

365
                if ( $key === 'content_score' && ! $this->readability_analysis->is_enabled() ) {
×
366
                        return true;
×
367
                }
368

369
                if ( $key === 'inclusive_language_score' && ! $this->inclusive_language_analysis->is_enabled() ) {
×
370
                        return true;
×
371
                }
372

373
                return false;
×
374
        }
375

376
        /**
377
         * Enqueues all the needed JS and CSS.
378
         *
379
         * @return void
380
         */
381
        public function enqueue() {
382
                $post_id = \get_queried_object_id();
×
383
                if ( empty( $post_id ) ) {
×
384
                        $post_id = 0;
×
385
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
386
                        if ( isset( $_GET['post'] ) && \is_string( $_GET['post'] ) ) {
×
387
                                // 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.
388
                                $post_id = (int) \wp_unslash( $_GET['post'] );
×
389
                        }
390
                }
391

392
                if ( $post_id !== 0 ) {
×
393
                        // Enqueue files needed for upload functionality.
394
                        \wp_enqueue_media( [ 'post' => $post_id ] );
×
395
                }
396

397
                $this->asset_manager->enqueue_style( 'admin-global' );
×
398
                $this->asset_manager->enqueue_style( 'metabox-css' );
×
399
                $this->asset_manager->enqueue_style( 'scoring' );
×
400
                $this->asset_manager->enqueue_style( 'monorepo' );
×
401
                $this->asset_manager->enqueue_style( 'admin-css' );
×
402
                $this->asset_manager->enqueue_style( 'elementor' );
×
403

404
                $this->asset_manager->enqueue_script( 'admin-global' );
×
405
                $this->asset_manager->enqueue_script( 'elementor' );
×
406

407
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminGlobalL10n', \YoastSEO()->helpers->wincher->get_admin_global_links() );
×
408
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
×
409
                $this->asset_manager->localize_script( 'elementor', 'wpseoFeaturesL10n', WPSEO_Utils::retrieve_enabled_features() );
×
410

411
                $plugins_script_data = [
×
412
                        'replaceVars' => [
×
413
                                'no_parent_text'           => \__( '(no parent)', 'wordpress-seo' ),
×
414
                                'replace_vars'             => $this->get_replace_vars(),
×
415
                                'recommended_replace_vars' => $this->get_recommended_replace_vars(),
×
416
                                'hidden_replace_vars'      => $this->get_hidden_replace_vars(),
×
417
                                'scope'                    => $this->determine_scope(),
×
418
                                'has_taxonomies'           => $this->current_post_type_has_taxonomies(),
×
419
                        ],
×
420
                        'shortcodes'  => [
×
421
                                'wpseo_filter_shortcodes_nonce' => \wp_create_nonce( 'wpseo-filter-shortcodes' ),
×
422
                                'wpseo_shortcode_tags'          => $this->get_valid_shortcode_tags(),
×
423
                        ],
×
424
                ];
×
425

426
                $worker_script_data = [
×
427
                        'url'                     => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-analysis-worker' ),
×
428
                        'dependencies'            => \YoastSEO()->helpers->asset->get_dependency_urls_by_handle( 'yoast-seo-analysis-worker' ),
×
429
                        'keywords_assessment_url' => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-used-keywords-assessment' ),
×
430
                        'log_level'               => WPSEO_Utils::get_analysis_worker_log_level(),
×
431
                        // We need to make the feature flags separately available inside of the analysis web worker.
432
                        'enabled_features'        => WPSEO_Utils::retrieve_enabled_features(),
×
433
                ];
×
434

435
                $alert_dismissal_action = \YoastSEO()->classes->get( Alert_Dismissal_Action::class );
×
436
                $dismissed_alerts       = $alert_dismissal_action->all_dismissed();
×
437

438
                $script_data = [
×
439
                        'media'                    => [ 'choose_image' => \__( 'Use Image', 'wordpress-seo' ) ],
×
440
                        'metabox'                  => $this->get_metabox_script_data(),
×
441
                        'userLanguageCode'         => WPSEO_Language_Utils::get_language( \get_user_locale() ),
×
442
                        'isPost'                   => true,
×
443
                        'isBlockEditor'            => WP_Screen::get()->is_block_editor(),
×
444
                        'isElementorEditor'        => true,
×
445
                        'postStatus'               => \get_post_status( $post_id ),
×
446
                        'analysis'                 => [
×
447
                                'plugins' => $plugins_script_data,
×
448
                                'worker'  => $worker_script_data,
×
449
                        ],
×
450
                        'dismissedAlerts'          => $dismissed_alerts,
×
451
                        'webinarIntroElementorUrl' => WPSEO_Shortlinker::get( 'https://yoa.st/webinar-intro-elementor' ),
×
452
                        'usedKeywordsNonce'        => \wp_create_nonce( 'wpseo-keyword-usage-and-post-types' ),
×
453
                ];
×
454

455
                if ( \post_type_supports( $this->get_metabox_post()->post_type, 'thumbnail' ) ) {
×
456
                        $this->asset_manager->enqueue_style( 'featured-image' );
×
457

458
                        $script_data['featuredImage'] = [
×
459
                                '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' ),
×
460
                        ];
×
461
                }
462

463
                $this->asset_manager->localize_script( 'elementor', 'wpseoScriptData', $script_data );
×
464
                $this->asset_manager->enqueue_user_language_script();
×
465
        }
466

467
        /**
468
         * Renders the metabox hidden fields.
469
         *
470
         * @return void
471
         */
472
        protected function render_hidden_fields() {
473
                // Wrap in a form with an action and post_id for the submit.
474
                \printf(
×
475
                        '<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" />',
×
476
                        \esc_url( \admin_url( 'admin-ajax.php' ) ),
×
477
                        \esc_attr( $this->get_metabox_post()->ID )
×
478
                );
×
479

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

484
                if ( $this->is_advanced_metadata_enabled ) {
×
485
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
486
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'advanced' );
×
487
                }
488

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

492
                if ( $this->social_is_enabled ) {
×
493
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
494
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'social' );
×
495
                }
496

497
                \printf(
×
498
                        '<input type="hidden" id="%1$s" name="%1$s" value="%2$s" />',
×
499
                        \esc_attr( WPSEO_Meta::$form_prefix . 'slug' ),
×
500
                        \esc_attr( $this->get_post_slug() )
×
501
                );
×
502

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

506
                echo '</form>';
×
507
        }
508

509
        /**
510
         * Returns the slug for the post being edited.
511
         *
512
         * @return string
513
         */
514
        protected function get_post_slug() {
515
                $post = $this->get_metabox_post();
×
516

517
                // In case get_metabox_post returns null for whatever reason.
518
                if ( ! $post instanceof WP_Post ) {
×
519
                        return '';
×
520
                }
521

522
                // Drafts might not have a post_name unless the slug has been manually changed.
523
                // In this case we get it using get_sample_permalink.
524
                if ( ! $post->post_name ) {
×
525
                        $sample = \get_sample_permalink( $post );
×
526

527
                        // Since get_sample_permalink runs through filters, ensure that it has the expected return value.
528
                        if ( \is_array( $sample ) && \count( $sample ) === 2 && \is_string( $sample[1] ) ) {
×
529
                                return $sample[1];
×
530
                        }
531
                }
532

533
                return $post->post_name;
×
534
        }
535

536
        /**
537
         * Returns post in metabox context.
538
         *
539
         * @return WP_Post|null
540
         */
541
        protected function get_metabox_post() {
542
                if ( $this->post !== null ) {
×
543
                        return $this->post;
×
544
                }
545

546
                $post = null;
×
547
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
548
                if ( isset( $_GET['post'] ) && \is_string( $_GET['post'] ) ) {
×
549
                        // 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.
550
                        $post = (int) \wp_unslash( $_GET['post'] );
×
551
                }
552

553
                if ( ! empty( $post ) ) {
×
554
                        $this->post = \get_post( $post );
×
555

556
                        return $this->post;
×
557
                }
558

559
                if ( isset( $GLOBALS['post'] ) ) {
×
560
                        $this->post = $GLOBALS['post'];
×
561

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

565
                return null;
×
566
        }
567

568
        /**
569
         * Passes variables to js for use with the post-scraper.
570
         *
571
         * @return array
572
         */
573
        protected function get_metabox_script_data() {
574
                $permalink = '';
×
575

576
                if ( \is_object( $this->get_metabox_post() ) ) {
×
577
                        $permalink = \get_sample_permalink( $this->get_metabox_post()->ID );
×
578
                        $permalink = $permalink[0];
×
579
                }
580

581
                $post_formatter = new WPSEO_Metabox_Formatter(
×
582
                        new WPSEO_Post_Metabox_Formatter( $this->get_metabox_post(), [], $permalink )
×
583
                );
×
584

585
                $values = $post_formatter->get_values();
×
586

587
                /** This filter is documented in admin/filters/class-cornerstone-filter.php. */
588
                $post_types = \apply_filters( 'wpseo_cornerstone_post_types', \YoastSEO()->helpers->post_type->get_accessible_post_types() );
×
589
                if ( $values['cornerstoneActive'] && ! \in_array( $this->get_metabox_post()->post_type, $post_types, true ) ) {
×
590
                        $values['cornerstoneActive'] = false;
×
591
                }
592

593
                return $values;
×
594
        }
595

596
        /**
597
         * Prepares the replace vars for localization.
598
         *
599
         * @return array Replace vars.
600
         */
601
        protected function get_replace_vars() {
602
                $cached_replacement_vars = [];
×
603

604
                $vars_to_cache = [
×
605
                        'date',
×
606
                        'id',
×
607
                        'sitename',
×
608
                        'sitedesc',
×
609
                        'sep',
×
610
                        'page',
×
611
                        'currentyear',
×
612
                        'currentdate',
×
613
                        'currentmonth',
×
614
                        'currentday',
×
615
                        'tag',
×
616
                        'category',
×
617
                        'category_title',
×
618
                        'primary_category',
×
619
                        'pt_single',
×
620
                        'pt_plural',
×
621
                        'modified',
×
622
                        'name',
×
623
                        'user_description',
×
624
                        'pagetotal',
×
625
                        'pagenumber',
×
626
                        'post_year',
×
627
                        'post_month',
×
628
                        'post_day',
×
629
                        'author_first_name',
×
630
                        'author_last_name',
×
631
                        'permalink',
×
632
                        'post_content',
×
633
                ];
×
634

635
                foreach ( $vars_to_cache as $var ) {
×
636
                        $cached_replacement_vars[ $var ] = \wpseo_replace_vars( '%%' . $var . '%%', $this->get_metabox_post() );
×
637
                }
638

639
                // Merge custom replace variables with the WordPress ones.
640
                return \array_merge( $cached_replacement_vars, $this->get_custom_replace_vars( $this->get_metabox_post() ) );
×
641
        }
642

643
        /**
644
         * Prepares the recommended replace vars for localization.
645
         *
646
         * @return array Recommended replacement variables.
647
         */
648
        protected function get_recommended_replace_vars() {
649
                $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
×
650

651
                // What is recommended depends on the current context.
652
                $post_type = $recommended_replace_vars->determine_for_post( $this->get_metabox_post() );
×
653

654
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
655
        }
656

657
        /**
658
         * Returns the list of replace vars that should be hidden inside the editor.
659
         *
660
         * @return string[] The hidden replace vars.
661
         */
662
        protected function get_hidden_replace_vars() {
663
                return ( new WPSEO_Replace_Vars() )->get_hidden_replace_vars();
×
664
        }
665

666
        /**
667
         * Gets the custom replace variables for custom taxonomies and fields.
668
         *
669
         * @param WP_Post $post The post to check for custom taxonomies and fields.
670
         *
671
         * @return array Array containing all the replacement variables.
672
         */
673
        protected function get_custom_replace_vars( $post ) {
674
                return [
×
675
                        'custom_fields'     => $this->get_custom_fields_replace_vars( $post ),
×
676
                        'custom_taxonomies' => $this->get_custom_taxonomies_replace_vars( $post ),
×
677
                ];
×
678
        }
679

680
        /**
681
         * Gets the custom replace variables for custom taxonomies.
682
         *
683
         * @param WP_Post $post The post to check for custom taxonomies.
684
         *
685
         * @return array Array containing all the replacement variables.
686
         */
687
        protected function get_custom_taxonomies_replace_vars( $post ) {
688
                $taxonomies          = \get_object_taxonomies( $post, 'objects' );
×
689
                $custom_replace_vars = [];
×
690

691
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
692

693
                        if ( \is_string( $taxonomy ) ) { // If attachment, see https://core.trac.wordpress.org/ticket/37368 .
×
694
                                $taxonomy_name = $taxonomy;
×
695
                                $taxonomy      = \get_taxonomy( $taxonomy_name );
×
696
                        }
697

698
                        if ( $taxonomy->_builtin && $taxonomy->public ) {
×
699
                                continue;
×
700
                        }
701

702
                        $custom_replace_vars[ $taxonomy_name ] = [
×
703
                                'name'        => $taxonomy->name,
×
704
                                'description' => $taxonomy->description,
×
705
                        ];
×
706
                }
707

708
                return $custom_replace_vars;
×
709
        }
710

711
        /**
712
         * Gets the custom replace variables for custom fields.
713
         *
714
         * @param WP_Post $post The post to check for custom fields.
715
         *
716
         * @return array Array containing all the replacement variables.
717
         */
718
        protected function get_custom_fields_replace_vars( $post ) {
719
                $custom_replace_vars = [];
×
720

721
                // If no post object is passed, return the empty custom_replace_vars array.
722
                if ( ! \is_object( $post ) ) {
×
723
                        return $custom_replace_vars;
×
724
                }
725

726
                $custom_fields = \get_post_custom( $post->ID );
×
727

728
                // Simply concatenate all fields containing replace vars so we can handle them all with a single regex find.
729
                $replace_vars_fields = \implode(
×
730
                        ' ',
×
731
                        [
×
732
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->title,
×
733
                                \YoastSEO()->meta->for_post( $post->ID )->presentation->meta_description,
×
734
                        ]
×
735
                );
×
736

737
                \preg_match_all( '/%%cf_([A-Za-z0-9_]+)%%/', $replace_vars_fields, $matches );
×
738
                $fields_to_include = $matches[1];
×
739
                foreach ( $custom_fields as $custom_field_name => $custom_field ) {
×
740
                        // Skip private custom fields.
741
                        if ( \substr( $custom_field_name, 0, 1 ) === '_' ) {
×
742
                                continue;
×
743
                        }
744

745
                        // Skip custom fields that are not used, new ones will be fetched dynamically.
746
                        if ( ! \in_array( $custom_field_name, $fields_to_include, true ) ) {
×
747
                                continue;
×
748
                        }
749

750
                        // Skip custom field values that are serialized.
751
                        if ( \is_serialized( $custom_field[0] ) ) {
×
752
                                continue;
×
753
                        }
754

755
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
756
                }
757

758
                return $custom_replace_vars;
×
759
        }
760

761
        /**
762
         * Determines the scope based on the post type.
763
         * This can be used by the replacevar plugin to determine if a replacement needs to be executed.
764
         *
765
         * @return string String describing the current scope.
766
         */
767
        protected function determine_scope() {
768
                if ( $this->get_metabox_post()->post_type === 'page' ) {
×
769
                        return 'page';
×
770
                }
771

772
                return 'post';
×
773
        }
774

775
        /**
776
         * Determines whether or not the current post type has registered taxonomies.
777
         *
778
         * @return bool Whether the current post type has taxonomies.
779
         */
780
        protected function current_post_type_has_taxonomies() {
781
                $post_taxonomies = \get_object_taxonomies( $this->get_metabox_post()->post_type );
×
782

783
                return ! empty( $post_taxonomies );
×
784
        }
785

786
        /**
787
         * Returns an array with shortcode tags for all registered shortcodes.
788
         *
789
         * @return array
790
         */
791
        protected function get_valid_shortcode_tags() {
792
                $shortcode_tags = [];
×
793

794
                foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
×
795
                        $shortcode_tags[] = $tag;
×
796
                }
797

798
                return $shortcode_tags;
×
799
        }
800
}
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