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

Yoast / wordpress-seo / dd6e866a9e6d253114633104d9e3858d807178ba

19 Jun 2024 10:03AM UTC coverage: 48.628% (-4.3%) from 52.936%
dd6e866a9e6d253114633104d9e3858d807178ba

push

github

web-flow
Merge pull request #21431 from Yoast/21429-update-copy-in-the-introduction-and-consent-modals

Updates the copy for the introduction and consent modals

7441 of 13454 branches covered (55.31%)

Branch coverage included in aggregate %.

0 of 3 new or added lines in 2 files covered. (0.0%)

3718 existing lines in 107 files now uncovered.

25100 of 53464 relevant lines covered (46.95%)

62392.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/integrations/third-party/elementor.php
1
<?php
2

3
namespace Yoast\WP\SEO\Integrations\Third_Party;
4

5
use Elementor\Controls_Manager;
6
use Elementor\Core\DocumentTypes\PageBase;
7
use WP_Post;
8
use WP_Screen;
9
use WPSEO_Admin_Asset_Manager;
10
use WPSEO_Admin_Recommended_Replace_Vars;
11
use WPSEO_Language_Utils;
12
use WPSEO_Meta;
13
use WPSEO_Metabox_Analysis_Inclusive_Language;
14
use WPSEO_Metabox_Analysis_Readability;
15
use WPSEO_Metabox_Analysis_SEO;
16
use WPSEO_Metabox_Formatter;
17
use WPSEO_Post_Metabox_Formatter;
18
use WPSEO_Replace_Vars;
19
use WPSEO_Shortlinker;
20
use WPSEO_Utils;
21
use Yoast\WP\SEO\Actions\Alert_Dismissal_Action;
22
use Yoast\WP\SEO\Conditionals\Third_Party\Elementor_Edit_Conditional;
23
use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional;
24
use Yoast\WP\SEO\Helpers\Capability_Helper;
25
use Yoast\WP\SEO\Helpers\Options_Helper;
26
use Yoast\WP\SEO\Integrations\Integration_Interface;
27
use Yoast\WP\SEO\Introductions\Infrastructure\Wistia_Embed_Permission_Repository;
28
use Yoast\WP\SEO\Presenters\Admin\Meta_Fields_Presenter;
29
use Yoast\WP\SEO\Promotions\Application\Promotion_Manager;
30

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

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

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

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

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

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

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

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

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

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

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

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

111
        /**
112
         * Returns the conditionals based in which this loadable should be active.
113
         *
114
         * @return array
115
         */
116
        public static function get_conditionals() {
×
117
                return [ Elementor_Edit_Conditional::class ];
×
118
        }
119

120
        /**
121
         * Constructor.
122
         *
123
         * @param WPSEO_Admin_Asset_Manager $asset_manager     The asset manager.
124
         * @param Options_Helper            $options           The options helper.
125
         * @param Capability_Helper         $capability        The capability helper.
126
         * @param Promotion_Manager         $promotion_manager The promotion manager.
127
         */
128
        public function __construct(
×
129
                WPSEO_Admin_Asset_Manager $asset_manager,
130
                Options_Helper $options,
131
                Capability_Helper $capability,
132
                Promotion_Manager $promotion_manager
133
        ) {
134
                $this->asset_manager     = $asset_manager;
×
135
                $this->options           = $options;
×
136
                $this->capability        = $capability;
×
137
                $this->promotion_manager = $promotion_manager;
×
138

139
                $this->seo_analysis                 = new WPSEO_Metabox_Analysis_SEO();
×
140
                $this->readability_analysis         = new WPSEO_Metabox_Analysis_Readability();
×
141
                $this->inclusive_language_analysis  = new WPSEO_Metabox_Analysis_Inclusive_Language();
×
142
                $this->social_is_enabled            = $this->options->get( 'opengraph', false ) || $this->options->get( 'twitter', false );
×
143
                $this->is_advanced_metadata_enabled = $this->capability->current_user_can( 'wpseo_edit_advanced_metadata' ) || $this->options->get( 'disableadvanced_meta' ) === false;
×
144
        }
145

146
        /**
147
         * Initializes the integration.
148
         *
149
         * This is the place to register hooks and filters.
150
         *
151
         * @return void
152
         */
153
        public function register_hooks() {
×
154
                \add_action( 'wp_ajax_wpseo_elementor_save', [ $this, 'save_postdata' ] );
×
155

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

160
        /**
161
         * Registers our Elementor hooks.
162
         * This is done for pages with metabox on page load and not on ajax request.
163
         *
164
         * @return void
165
         */
166
        public function register_elementor_hooks() {
×
167

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

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

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

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

195
        /**
196
         * Register a panel tab slug, in order to allow adding controls to this tab.
197
         *
198
         * @return void
199
         */
200
        public function add_yoast_panel_tab() {
×
201
                Controls_Manager::add_tab( $this::YOAST_TAB, 'Yoast SEO' );
×
202
        }
203

204
        /**
205
         * Register additional document controls.
206
         *
207
         * @param PageBase $document The PageBase document.
208
         *
209
         * @return void
210
         */
211
        public function register_document_controls( $document ) {
×
212
                // PageBase is the base class for documents like `post` `page` and etc.
213
                if ( ! $document instanceof PageBase || ! $document::get_property( 'has_elements' ) ) {
×
214
                        return;
×
215
                }
216

217
                // This is needed to get the tab to appear, but will be overwritten in the JavaScript.
218
                $document->start_controls_section(
×
219
                        'yoast_temporary_section',
×
UNCOV
220
                        [
×
221
                                'label' => 'Yoast SEO',
×
222
                                'tab'   => self::YOAST_TAB,
×
UNCOV
223
                        ]
×
UNCOV
224
                );
×
225

226
                $document->end_controls_section();
×
227
        }
228

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

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

245
        /**
246
         * Saves the WP SEO metadata for posts.
247
         *
248
         * Outputs JSON via wp_send_json then stops code execution.
249
         *
250
         * {@internal $_POST parameters are validated via sanitize_post_meta().}}
251
         *
252
         * @return void
253
         */
254
        public function save_postdata() {
×
255
                global $post;
×
256

257
                if ( ! isset( $_POST['post_id'] ) || ! \is_string( $_POST['post_id'] ) ) {
×
258
                        \wp_send_json_error( 'Bad Request', 400 );
×
259
                }
260

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

264
                if ( $post_id <= 0 ) {
×
265
                        \wp_send_json_error( 'Bad Request', 400 );
×
266
                }
267

268
                if ( ! \current_user_can( 'edit_post', $post_id ) ) {
×
269
                        \wp_send_json_error( 'Forbidden', 403 );
×
270
                }
271

272
                \check_ajax_referer( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
×
273

274
                // Bail if this is a multisite installation and the site has been switched.
275
                if ( \is_multisite() && \ms_is_switched() ) {
×
276
                        \wp_send_json_error( 'Switched multisite', 409 );
×
277
                }
278

279
                \clean_post_cache( $post_id );
×
280
                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- To setup the post we need to do this explicitly.
281
                $post = \get_post( $post_id );
×
282

283
                if ( ! \is_object( $post ) ) {
×
284
                        // Non-existent post.
285
                        \wp_send_json_error( 'Post not found', 400 );
×
286
                }
287

288
                \do_action( 'wpseo_save_compare_data', $post );
×
289

290
                // Initialize meta, amongst other things it registers sanitization.
291
                WPSEO_Meta::init();
×
292

293
                $social_fields = [];
×
294
                if ( $this->social_is_enabled ) {
×
295
                        $social_fields = WPSEO_Meta::get_meta_field_defs( 'social', $post->post_type );
×
296
                }
297

298
                // The below methods use the global post so make sure it is setup.
299
                \setup_postdata( $post );
×
300
                $meta_boxes = \apply_filters( 'wpseo_save_metaboxes', [] );
×
301
                $meta_boxes = \array_merge(
×
302
                        $meta_boxes,
×
303
                        WPSEO_Meta::get_meta_field_defs( 'general', $post->post_type ),
×
304
                        WPSEO_Meta::get_meta_field_defs( 'advanced', $post->post_type ),
×
305
                        $social_fields,
×
306
                        WPSEO_Meta::get_meta_field_defs( 'schema', $post->post_type )
×
UNCOV
307
                );
×
308

309
                foreach ( $meta_boxes as $key => $meta_box ) {
×
310
                        // If analysis is disabled remove that analysis score value from the DB.
311
                        if ( $this->is_meta_value_disabled( $key ) ) {
×
312
                                WPSEO_Meta::delete( $key, $post_id );
×
313
                                continue;
×
314
                        }
315

316
                        $data       = null;
×
317
                        $field_name = WPSEO_Meta::$form_prefix . $key;
×
318

319
                        if ( $meta_box['type'] === 'checkbox' ) {
×
320
                                $data = isset( $_POST[ $field_name ] ) ? 'on' : 'off';
×
321
                        }
322
                        else {
323
                                if ( isset( $_POST[ $field_name ] ) ) {
×
324
                                        // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: Sanitized through sanitize_post_meta.
325
                                        $data = \wp_unslash( $_POST[ $field_name ] );
×
326

327
                                        // For multi-select.
328
                                        if ( \is_array( $data ) ) {
×
329
                                                $data = \array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], $data );
×
330
                                        }
331

332
                                        if ( \is_string( $data ) ) {
×
333
                                                $data = ( $key !== 'canonical' ) ? WPSEO_Utils::sanitize_text_field( $data ) : WPSEO_Utils::sanitize_url( $data );
×
334
                                        }
335
                                }
336

337
                                // Reset options when no entry is present with multiselect - only applies to `meta-robots-adv` currently.
338
                                if ( ! isset( $_POST[ $field_name ] ) && ( $meta_box['type'] === 'multiselect' ) ) {
×
339
                                        $data = [];
×
340
                                }
341
                        }
342

343
                        if ( $data !== null ) {
×
344
                                WPSEO_Meta::set_value( $key, $data, $post_id );
×
345
                        }
346
                }
347

348
                if ( isset( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) && \is_string( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) ) {
×
349
                        $slug = \sanitize_title( \wp_unslash( $_POST[ WPSEO_Meta::$form_prefix . 'slug' ] ) );
×
350
                        if ( $post->post_name !== $slug ) {
×
351
                                $post_array              = $post->to_array();
×
352
                                $post_array['post_name'] = $slug;
×
353

354
                                $save_successful = \wp_insert_post( $post_array );
×
355
                                if ( \is_wp_error( $save_successful ) ) {
×
356
                                        \wp_send_json_error( 'Slug not saved', 400 );
×
357
                                }
358

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

368
                \do_action( 'wpseo_saved_postdata' );
×
369

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

374
        /**
375
         * Determines if the given meta value key is disabled.
376
         *
377
         * @param string $key The key of the meta value.
378
         *
379
         * @return bool Whether the given meta value key is disabled.
380
         */
381
        public function is_meta_value_disabled( $key ) {
×
382
                if ( $key === 'linkdex' && ! $this->seo_analysis->is_enabled() ) {
×
383
                        return true;
×
384
                }
385

386
                if ( $key === 'content_score' && ! $this->readability_analysis->is_enabled() ) {
×
387
                        return true;
×
388
                }
389

390
                if ( $key === 'inclusive_language_score' && ! $this->inclusive_language_analysis->is_enabled() ) {
×
391
                        return true;
×
392
                }
393

394
                return false;
×
395
        }
396

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

413
                if ( $post_id !== 0 ) {
×
414
                        // Enqueue files needed for upload functionality.
415
                        \wp_enqueue_media( [ 'post' => $post_id ] );
×
416
                }
417

418
                $this->asset_manager->enqueue_style( 'admin-global' );
×
419
                $this->asset_manager->enqueue_style( 'metabox-css' );
×
420
                $this->asset_manager->enqueue_style( 'scoring' );
×
421
                $this->asset_manager->enqueue_style( 'monorepo' );
×
422
                $this->asset_manager->enqueue_style( 'admin-css' );
×
423
                $this->asset_manager->enqueue_style( 'ai-generator' );
×
424
                $this->asset_manager->enqueue_style( 'elementor' );
×
425

426
                $this->asset_manager->enqueue_script( 'admin-global' );
×
427
                $this->asset_manager->enqueue_script( 'elementor' );
×
428

429
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminGlobalL10n', \YoastSEO()->helpers->wincher->get_admin_global_links() );
×
430
                $this->asset_manager->localize_script( 'elementor', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
×
431
                $this->asset_manager->localize_script( 'elementor', 'wpseoFeaturesL10n', WPSEO_Utils::retrieve_enabled_features() );
×
432

UNCOV
433
                $plugins_script_data = [
×
UNCOV
434
                        'replaceVars' => [
×
435
                                'no_parent_text'           => \__( '(no parent)', 'wordpress-seo' ),
×
436
                                'replace_vars'             => $this->get_replace_vars(),
×
437
                                'recommended_replace_vars' => $this->get_recommended_replace_vars(),
×
438
                                'hidden_replace_vars'      => $this->get_hidden_replace_vars(),
×
439
                                'scope'                    => $this->determine_scope(),
×
440
                                'has_taxonomies'           => $this->current_post_type_has_taxonomies(),
×
UNCOV
441
                        ],
×
UNCOV
442
                        'shortcodes'  => [
×
443
                                'wpseo_shortcode_tags'          => $this->get_valid_shortcode_tags(),
×
444
                                'wpseo_filter_shortcodes_nonce' => \wp_create_nonce( 'wpseo-filter-shortcodes' ),
×
UNCOV
445
                        ],
×
UNCOV
446
                ];
×
447

UNCOV
448
                $worker_script_data = [
×
449
                        'url'                     => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-analysis-worker' ),
×
450
                        'dependencies'            => \YoastSEO()->helpers->asset->get_dependency_urls_by_handle( 'yoast-seo-analysis-worker' ),
×
451
                        'keywords_assessment_url' => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-used-keywords-assessment' ),
×
452
                        'log_level'               => WPSEO_Utils::get_analysis_worker_log_level(),
×
453
                        // We need to make the feature flags separately available inside of the analysis web worker.
454
                        'enabled_features'        => WPSEO_Utils::retrieve_enabled_features(),
×
UNCOV
455
                ];
×
456

457
                $alert_dismissal_action  = \YoastSEO()->classes->get( Alert_Dismissal_Action::class );
×
458
                $dismissed_alerts        = $alert_dismissal_action->all_dismissed();
×
459
                $woocommerce_conditional = new WooCommerce_Conditional();
×
460

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

484
                if ( \post_type_supports( $this->get_metabox_post()->post_type, 'thumbnail' ) ) {
×
485
                        $this->asset_manager->enqueue_style( 'featured-image' );
×
486

487
                        $script_data['featuredImage'] = [
×
488
                                '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' ),
×
UNCOV
489
                        ];
×
490
                }
491

492
                $this->asset_manager->localize_script( 'elementor', 'wpseoScriptData', $script_data );
×
493
                $this->asset_manager->enqueue_user_language_script();
×
494
        }
495

496
        /**
497
         * Renders the metabox hidden fields.
498
         *
499
         * @return void
500
         */
501
        protected function render_hidden_fields() {
×
502
                // Wrap in a form with an action and post_id for the submit.
503
                \printf(
×
504
                        '<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" />',
×
505
                        \esc_url( \admin_url( 'admin-ajax.php' ) ),
×
506
                        \esc_attr( $this->get_metabox_post()->ID )
×
UNCOV
507
                );
×
508

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

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

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

521
                if ( $this->social_is_enabled ) {
×
522
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
523
                        echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'social' );
×
524
                }
525

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

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

535
                echo '</form>';
×
536
        }
537

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

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

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

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

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

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

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

582
                if ( ! empty( $post ) ) {
×
583
                        $this->post = \get_post( $post );
×
584

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

588
                if ( isset( $GLOBALS['post'] ) ) {
×
589
                        $this->post = $GLOBALS['post'];
×
590

591
                        return $this->post;
×
592
                }
593

594
                return null;
×
595
        }
596

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

605
                if ( \is_object( $this->get_metabox_post() ) ) {
×
606
                        $permalink = \get_sample_permalink( $this->get_metabox_post()->ID );
×
607
                        $permalink = $permalink[0];
×
608
                }
609

610
                $post_formatter = new WPSEO_Metabox_Formatter(
×
611
                        new WPSEO_Post_Metabox_Formatter( $this->get_metabox_post(), [], $permalink )
×
UNCOV
612
                );
×
613

614
                $values = $post_formatter->get_values();
×
615

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

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

624
                return $values;
×
625
        }
626

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

638
                return ! $is_premium || \version_compare( $premium_version, '21.8-RC0', '>=' );
×
639
        }
640

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

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

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

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

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

696
                // What is recommended depends on the current context.
697
                $post_type = $recommended_replace_vars->determine_for_post( $this->get_metabox_post() );
×
698

699
                return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
×
700
        }
701

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

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

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

736
                foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
×
737

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

743
                        if ( $taxonomy->_builtin && $taxonomy->public ) {
×
744
                                continue;
×
745
                        }
746

747
                        $custom_replace_vars[ $taxonomy_name ] = [
×
748
                                'name'        => $taxonomy->name,
×
749
                                'description' => $taxonomy->description,
×
UNCOV
750
                        ];
×
751
                }
752

753
                return $custom_replace_vars;
×
754
        }
755

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

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

771
                $custom_fields = \get_post_custom( $post->ID );
×
772

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

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

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

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

800
                        $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
×
801
                }
802

803
                return $custom_replace_vars;
×
804
        }
805

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

817
                return 'post';
×
818
        }
819

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

828
                return ! empty( $post_taxonomies );
×
829
        }
830

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

839
                foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
×
840
                        $shortcode_tags[] = $tag;
×
841
                }
842

843
                return $shortcode_tags;
×
844
        }
845
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc