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

Yoast / wordpress-seo / 5796844141

pending completion
5796844141

push

github

web-flow
Merge pull request #20546 from Yoast/feature/ai

Introduce the Introductions functionality

135 of 148 new or added lines in 20 files covered. (91.22%)

622 existing lines in 3 files now uncovered.

12419 of 26427 relevant lines covered (46.99%)

3.46 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
         * This is done for pages with metabox on page load and not on ajax request.
150
         */
151
        public function register_elementor_hooks() {
152

153
                if ( $this->get_metabox_post() === null || ! $this->display_metabox( $this->get_metabox_post()->post_type ) ) {
×
154
                        return;
×
155
                }
156

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

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

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

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

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

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

207
                $document->end_controls_section();
×
208
        }
209

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

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

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

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

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

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

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

253
                \check_ajax_referer( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
×
254

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

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

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

269
                \do_action( 'wpseo_save_compare_data', $post );
×
270

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

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

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

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

297
                        $data       = null;
×
298
                        $field_name = WPSEO_Meta::$form_prefix . $key;
×
299

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

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

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

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

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

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

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

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

349
                \do_action( 'wpseo_saved_postdata' );
×
350

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

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

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

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

375
                return false;
×
376
        }
377

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

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

399
                $this->asset_manager->enqueue_style( 'admin-global' );
×
400
                $this->asset_manager->enqueue_style( 'metabox-css' );
×
401
                $this->asset_manager->enqueue_style( 'scoring' );
×
402
                $this->asset_manager->enqueue_style( 'monorepo' );
×
403
                $this->asset_manager->enqueue_style( 'admin-css' );
×
NEW
404
                $this->asset_manager->enqueue_style( 'ai-generator' );
×
UNCOV
405
                $this->asset_manager->enqueue_style( 'elementor' );
×
406

407
                $this->asset_manager->enqueue_script( 'admin-global' );
×
408
                $this->asset_manager->enqueue_script( 'elementor' );
×
409

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

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

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

438
                $alert_dismissal_action = \YoastSEO()->classes->get( Alert_Dismissal_Action::class );
×
439
                $dismissed_alerts       = $alert_dismissal_action->all_dismissed();
×
440

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

460
                if ( \post_type_supports( $this->get_metabox_post()->post_type, 'thumbnail' ) ) {
×
461
                        $this->asset_manager->enqueue_style( 'featured-image' );
×
462

463
                        $script_data['featuredImage'] = [
×
464
                                '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' ),
×
465
                        ];
466
                }
467

468
                $this->asset_manager->localize_script( 'elementor', 'wpseoScriptData', $script_data );
×
469
                $this->asset_manager->enqueue_user_language_script();
×
470
        }
471

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

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

489
                if ( $this->is_advanced_metadata_enabled ) {
×
490
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
491
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'advanced' );
×
492
                }
493

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

497
                if ( $this->social_is_enabled ) {
×
498
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
499
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'social' );
×
500
                }
501

502
                \printf(
×
503
                        '<input type="hidden" id="%1$s" name="%1$s" value="%2$s" />',
×
504
                        \esc_attr( WPSEO_Meta::$form_prefix . 'slug' ),
×
505
                        \esc_attr( $this->get_post_slug() )
×
506
                );
507

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

511
                echo '</form>';
×
512
        }
513

514
        /**
515
         * Returns the slug for the post being edited.
516
         *
517
         * @return string
518
         */
519
        protected function get_post_slug() {
520
                $post = $this->get_metabox_post();
×
521

522
                // In case get_metabox_post returns null for whatever reason.
523
                if ( ! $post instanceof WP_Post ) {
×
524
                        return '';
×
525
                }
526

527
                // Drafts might not have a post_name unless the slug has been manually changed.
528
                // In this case we get it using get_sample_permalink.
529
                if ( ! $post->post_name ) {
×
530
                        $sample = \get_sample_permalink( $post );
×
531

532
                        // Since get_sample_permalink runs through filters, ensure that it has the expected return value.
533
                        if ( \is_array( $sample ) && \count( $sample ) === 2 && \is_string( $sample[1] ) ) {
×
534
                                return $sample[1];
×
535
                        }
536
                }
537

538
                return $post->post_name;
×
539
        }
540

541
        /**
542
         * Returns post in metabox context.
543
         *
544
         * @return WP_Post|null
545
         */
546
        protected function get_metabox_post() {
547
                if ( $this->post !== null ) {
×
548
                        return $this->post;
×
549
                }
550

551
                $post = null;
×
552
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
553
                if ( isset( $_GET['post'] ) && \is_numeric( $_GET['post'] ) ) {
×
554
                        // 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.
555
                        $post = (int) \wp_unslash( $_GET['post'] );
×
556
                }
557

558
                if ( ! empty( $post ) ) {
×
559
                        $this->post = \get_post( $post );
×
560

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

564
                if ( isset( $GLOBALS['post'] ) ) {
×
565
                        $this->post = $GLOBALS['post'];
×
566

567
                        return $this->post;
×
568
                }
569

570
                return null;
×
571
        }
572

573
        /**
574
         * Passes variables to js for use with the post-scraper.
575
         *
576
         * @return array
577
         */
578
        protected function get_metabox_script_data() {
579
                $permalink = '';
×
580

581
                if ( \is_object( $this->get_metabox_post() ) ) {
×
582
                        $permalink = \get_sample_permalink( $this->get_metabox_post()->ID );
×
583
                        $permalink = $permalink[0];
×
584
                }
585

586
                $post_formatter = new WPSEO_Metabox_Formatter(
×
587
                        new WPSEO_Post_Metabox_Formatter( $this->get_metabox_post(), [], $permalink )
×
588
                );
589

590
                $values = $post_formatter->get_values();
×
591

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

598
                return $values;
×
599
        }
600

601
        /**
602
         * Prepares the replace vars for localization.
603
         *
604
         * @return array Replace vars.
605
         */
606
        protected function get_replace_vars() {
607
                $cached_replacement_vars = [];
×
608

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

640
                foreach ( $vars_to_cache as $var ) {
×
641
                        $cached_replacement_vars[ $var ] = \wpseo_replace_vars( '%%' . $var . '%%', $this->get_metabox_post() );
×
642
                }
643

644
                // Merge custom replace variables with the WordPress ones.
645
                return \array_merge( $cached_replacement_vars, $this->get_custom_replace_vars( $this->get_metabox_post() ) );
×
646
        }
647

648
        /**
649
         * Prepares the recommended replace vars for localization.
650
         *
651
         * @return array Recommended replacement variables.
652
         */
653
        protected function get_recommended_replace_vars() {
654
                $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
×
655

656
                // What is recommended depends on the current context.
657
                $post_type = $recommended_replace_vars->determine_for_post( $this->get_metabox_post() );
×
658

659
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
660
        }
661

662
        /**
663
         * Returns the list of replace vars that should be hidden inside the editor.
664
         *
665
         * @return string[] The hidden replace vars.
666
         */
667
        protected function get_hidden_replace_vars() {
668
                return ( new WPSEO_Replace_Vars() )->get_hidden_replace_vars();
×
669
        }
670

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

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

696
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
697

698
                        if ( \is_string( $taxonomy ) ) { // If attachment, see https://core.trac.wordpress.org/ticket/37368 .
×
699
                                $taxonomy_name = $taxonomy;
×
700
                                $taxonomy      = \get_taxonomy( $taxonomy_name );
×
701
                        }
702

703
                        if ( $taxonomy->_builtin && $taxonomy->public ) {
×
704
                                continue;
×
705
                        }
706

707
                        $custom_replace_vars[ $taxonomy_name ] = [
×
708
                                'name'        => $taxonomy->name,
×
709
                                'description' => $taxonomy->description,
×
710
                        ];
711
                }
712

713
                return $custom_replace_vars;
×
714
        }
715

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

726
                // If no post object is passed, return the empty custom_replace_vars array.
727
                if ( ! \is_object( $post ) ) {
×
728
                        return $custom_replace_vars;
×
729
                }
730

731
                $custom_fields = \get_post_custom( $post->ID );
×
732

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

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

750
                        // Skip custom fields that are not used, new ones will be fetched dynamically.
751
                        if ( ! \in_array( $custom_field_name, $fields_to_include, true ) ) {
×
752
                                continue;
×
753
                        }
754

755
                        // Skip custom field values that are serialized.
756
                        if ( \is_serialized( $custom_field[0] ) ) {
×
757
                                continue;
×
758
                        }
759

760
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
761
                }
762

763
                return $custom_replace_vars;
×
764
        }
765

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

777
                return 'post';
×
778
        }
779

780
        /**
781
         * Determines whether or not the current post type has registered taxonomies.
782
         *
783
         * @return bool Whether the current post type has taxonomies.
784
         */
785
        protected function current_post_type_has_taxonomies() {
786
                $post_taxonomies = \get_object_taxonomies( $this->get_metabox_post()->post_type );
×
787

788
                return ! empty( $post_taxonomies );
×
789
        }
790

791
        /**
792
         * Returns an array with shortcode tags for all registered shortcodes.
793
         *
794
         * @return array
795
         */
796
        protected function get_valid_shortcode_tags() {
797
                $shortcode_tags = [];
×
798

799
                foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
×
800
                        $shortcode_tags[] = $tag;
×
801
                }
802

803
                return $shortcode_tags;
×
804
        }
805
}
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