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

Yoast / wordpress-seo / 5889346063

17 Aug 2023 09:46AM UTC coverage: 47.129% (+0.1%) from 46.99%
5889346063

push

github

hdvos
Merge branch 'trunk' into feature/html-parser

84 of 86 new or added lines in 5 files covered. (97.67%)

606 existing lines in 1 file now uncovered.

12493 of 26508 relevant lines covered (47.13%)

3.45 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\Introductions\Infrastructure\Wistia_Embed_Permission_Repository;
27
use Yoast\WP\SEO\Presenters\Admin\Meta_Fields_Presenter;
28

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

148
        /**
149
         * Registers our Elementor hooks.
150
         * This is done for pages with metabox on page load and not on ajax request.
151
         */
152
        public function register_elementor_hooks() {
153

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

350
                \do_action( 'wpseo_saved_postdata' );
×
351

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

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

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

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

376
                return false;
×
377
        }
378

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

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

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

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

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

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

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

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

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

463
                if ( \post_type_supports( $this->get_metabox_post()->post_type, 'thumbnail' ) ) {
×
464
                        $this->asset_manager->enqueue_style( 'featured-image' );
×
465

466
                        $script_data['featuredImage'] = [
×
467
                                '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' ),
×
468
                        ];
469
                }
470

471
                $this->asset_manager->localize_script( 'elementor', 'wpseoScriptData', $script_data );
×
472
                $this->asset_manager->enqueue_user_language_script();
×
473
        }
474

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

488
                \wp_nonce_field( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
×
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(), 'general' );
×
491

492
                if ( $this->is_advanced_metadata_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(), 'advanced' );
×
495
                }
496

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

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

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

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

514
                echo '</form>';
×
515
        }
516

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

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

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

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

541
                return $post->post_name;
×
542
        }
543

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

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

561
                if ( ! empty( $post ) ) {
×
562
                        $this->post = \get_post( $post );
×
563

564
                        return $this->post;
×
565
                }
566

567
                if ( isset( $GLOBALS['post'] ) ) {
×
568
                        $this->post = $GLOBALS['post'];
×
569

570
                        return $this->post;
×
571
                }
572

573
                return null;
×
574
        }
575

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

584
                if ( \is_object( $this->get_metabox_post() ) ) {
×
585
                        $permalink = \get_sample_permalink( $this->get_metabox_post()->ID );
×
586
                        $permalink = $permalink[0];
×
587
                }
588

589
                $post_formatter = new WPSEO_Metabox_Formatter(
×
590
                        new WPSEO_Post_Metabox_Formatter( $this->get_metabox_post(), [], $permalink )
×
591
                );
592

593
                $values = $post_formatter->get_values();
×
594

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

601
                return $values;
×
602
        }
603

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

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

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

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

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

659
                // What is recommended depends on the current context.
660
                $post_type = $recommended_replace_vars->determine_for_post( $this->get_metabox_post() );
×
661

662
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
663
        }
664

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

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

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

699
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
700

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

706
                        if ( $taxonomy->_builtin && $taxonomy->public ) {
×
707
                                continue;
×
708
                        }
709

710
                        $custom_replace_vars[ $taxonomy_name ] = [
×
711
                                'name'        => $taxonomy->name,
×
712
                                'description' => $taxonomy->description,
×
713
                        ];
714
                }
715

716
                return $custom_replace_vars;
×
717
        }
718

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

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

734
                $custom_fields = \get_post_custom( $post->ID );
×
735

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

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

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

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

763
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
764
                }
765

766
                return $custom_replace_vars;
×
767
        }
768

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

780
                return 'post';
×
781
        }
782

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

791
                return ! empty( $post_taxonomies );
×
792
        }
793

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

802
                foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
×
803
                        $shortcode_tags[] = $tag;
×
804
                }
805

806
                return $shortcode_tags;
×
807
        }
808
}
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