PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.0
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / integrations / third-party / elementor.php

elementor.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.0, at src/integrations/third-party/elementor.php

769 lines 23.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_Asset_Yoast_Components_L10n;
11 use WPSEO_Admin_Recommended_Replace_Vars;
12 use WPSEO_Language_Utils;
13 use WPSEO_Meta;
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_Utils;
20 use Yoast\WP\SEO\Actions\Alert_Dismissal_Action;
21 use Yoast\WP\SEO\Conditionals\Admin\Estimated_Reading_Time_Conditional;
22 use Yoast\WP\SEO\Conditionals\Third_Party\Elementor_Edit_Conditional;
23 use Yoast\WP\SEO\Config\Wincher_Links;
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\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 * Represents the estimated_reading_time_conditional.
97 *
98 * @var Estimated_Reading_Time_Conditional
99 */
100 protected $estimated_reading_time_conditional;
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 * @param Estimated_Reading_Time_Conditional $estimated_reading_time_conditional The Estimated Reading Time
118 * conditional.
119 */
120 public function __construct(
121 WPSEO_Admin_Asset_Manager $asset_manager,
122 Options_Helper $options,
123 Capability_Helper $capability,
124 Estimated_Reading_Time_Conditional $estimated_reading_time_conditional
125 ) {
126 $this->asset_manager = $asset_manager;
127 $this->options = $options;
128 $this->capability = $capability;
129
130 $this->seo_analysis = new WPSEO_Metabox_Analysis_SEO();
131 $this->readability_analysis = new WPSEO_Metabox_Analysis_Readability();
132 $this->social_is_enabled = $this->options->get( 'opengraph', false ) || $this->options->get( 'twitter', false );
133 $this->is_advanced_metadata_enabled = $this->capability->current_user_can( 'wpseo_edit_advanced_metadata' ) || $this->options->get( 'disableadvanced_meta' ) === false;
134 $this->estimated_reading_time_conditional = $estimated_reading_time_conditional;
135 }
136
137 /**
138 * Initializes the integration.
139 *
140 * This is the place to register hooks and filters.
141 *
142 * @return void
143 */
144 public function register_hooks() {
145 \add_action( 'wp_ajax_wpseo_elementor_save', [ $this, 'save_postdata' ] );
146
147 // We need to delay the post type lookup to give other plugins a chance to register custom post types.
148 \add_action( 'init', [ $this, 'register_elementor_hooks' ], \PHP_INT_MAX );
149 }
150
151 /**
152 * Registers our Elementor hooks.
153 */
154 public function register_elementor_hooks() {
155 if ( ! $this->display_metabox( $this->get_metabox_post()->post_type ) ) {
156 return;
157 }
158
159 \add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'init' ] );
160
161 // 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).
162 if ( ! \did_action( 'elementor/init' ) ) {
163 \add_action( 'elementor/init', [ $this, 'add_yoast_panel_tab' ] );
164 }
165 else {
166 $this->add_yoast_panel_tab();
167 }
168 \add_action( 'elementor/documents/register_controls', [ $this, 'register_document_controls' ] );
169 }
170
171 /**
172 * Initializes the integration.
173 *
174 * @return void
175 */
176 public function init() {
177 $this->asset_manager->register_assets();
178 $this->enqueue();
179 $this->render_hidden_fields();
180 }
181
182 /**
183 * Register a panel tab slug, in order to allow adding controls to this tab.
184 */
185 public function add_yoast_panel_tab() {
186 Controls_Manager::add_tab( $this::YOAST_TAB, \__( 'Yoast SEO', 'wordpress-seo' ) );
187 }
188
189 /**
190 * Register additional document controls.
191 *
192 * @param PageBase $document The PageBase document.
193 */
194 public function register_document_controls( $document ) {
195 // PageBase is the base class for documents like `post` `page` and etc.
196 if ( ! $document instanceof PageBase || ! $document::get_property( 'has_elements' ) ) {
197 return;
198 }
199
200 // This is needed to get the tab to appear, but will be overwritten in the JavaScript.
201 $document->start_controls_section(
202 'yoast_temporary_section',
203 [
204 'label' => \__( 'Yoast SEO', 'wordpress-seo' ),
205 'tab' => self::YOAST_TAB,
206 ]
207 );
208
209 $document->end_controls_section();
210 }
211
212 // Below is mostly copied from `class-metabox.php`. That constructor has side-effects we do not need.
213
214 /**
215 * Determines whether the metabox should be shown for the passed identifier.
216 *
217 * By default the check is done for post types, but can also be used for taxonomies.
218 *
219 * @param string|null $identifier The identifier to check.
220 * @param string $type The type of object to check. Defaults to post_type.
221 *
222 * @return bool Whether or not the metabox should be displayed.
223 */
224 public function display_metabox( $identifier = null, $type = 'post_type' ) {
225 return WPSEO_Utils::is_metabox_active( $identifier, $type );
226 }
227
228 /**
229 * Saves the WP SEO metadata for posts.
230 *
231 * Outputs JSON via wp_send_json then stops code execution.
232 *
233 * {@internal $_POST parameters are validated via sanitize_post_meta().}}
234 *
235 * @return void
236 */
237 public function save_postdata() {
238 global $post;
239
240 $post_id = \filter_input( \INPUT_POST, 'post_id', \FILTER_SANITIZE_NUMBER_INT );
241
242 if ( ! \current_user_can( 'manage_options' ) ) {
243 \wp_send_json_error( 'Unauthorized', 401 );
244 }
245
246 \check_ajax_referer( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
247
248 // Bail if this is a multisite installation and the site has been switched.
249 if ( \is_multisite() && \ms_is_switched() ) {
250 \wp_send_json_error( 'Switched multisite', 409 );
251 }
252
253 \clean_post_cache( $post_id );
254 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- To setup the post we need to do this explicitly.
255 $post = \get_post( $post_id );
256
257 if ( ! \is_object( $post ) ) {
258 // Non-existent post.
259 \wp_send_json_error( 'Post not found', 400 );
260 }
261
262 \do_action( 'wpseo_save_compare_data', $post );
263
264 // Initialize meta, amongst other things it registers sanitization.
265 WPSEO_Meta::init();
266
267 $social_fields = [];
268 if ( $this->social_is_enabled ) {
269 $social_fields = WPSEO_Meta::get_meta_field_defs( 'social', $post->post_type );
270 }
271
272 // The below methods use the global post so make sure it is setup.
273 \setup_postdata( $post );
274 $meta_boxes = \apply_filters( 'wpseo_save_metaboxes', [] );
275 $meta_boxes = \array_merge(
276 $meta_boxes,
277 WPSEO_Meta::get_meta_field_defs( 'general', $post->post_type ),
278 WPSEO_Meta::get_meta_field_defs( 'advanced', $post->post_type ),
279 $social_fields,
280 WPSEO_Meta::get_meta_field_defs( 'schema', $post->post_type )
281 );
282
283 foreach ( $meta_boxes as $key => $meta_box ) {
284 // If analysis is disabled remove that analysis score value from the DB.
285 if ( $this->is_meta_value_disabled( $key ) ) {
286 WPSEO_Meta::delete( $key, $post_id );
287 continue;
288 }
289
290 $data = null;
291 $field_name = WPSEO_Meta::$form_prefix . $key;
292
293 if ( $meta_box['type'] === 'checkbox' ) {
294 $data = isset( $_POST[ $field_name ] ) ? 'on' : 'off';
295 }
296 else {
297 if ( isset( $_POST[ $field_name ] ) ) {
298 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: Sanitized through sanitize_post_meta.
299 $data = \wp_unslash( $_POST[ $field_name ] );
300
301 // For multi-select.
302 if ( \is_array( $data ) ) {
303 $data = \array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], $data );
304 }
305
306 if ( \is_string( $data ) ) {
307 $data = ( $key !== 'canonical' ) ? WPSEO_Utils::sanitize_text_field( $data ) : WPSEO_Utils::sanitize_url( $data );
308 }
309 }
310
311 // Reset options when no entry is present with multiselect - only applies to `meta-robots-adv` currently.
312 if ( ! isset( $_POST[ $field_name ] ) && ( $meta_box['type'] === 'multiselect' ) ) {
313 $data = [];
314 }
315 }
316
317 if ( $data !== null ) {
318 WPSEO_Meta::set_value( $key, $data, $post_id );
319 }
320 }
321
322 // Saving the WP post to save the slug.
323 $slug = \filter_input( \INPUT_POST, WPSEO_Meta::$form_prefix . 'slug', \FILTER_SANITIZE_STRING );
324 if ( $post->post_name !== $slug ) {
325 $post_array = $post->to_array();
326 $post_array['post_name'] = $slug;
327
328 $save_successful = \wp_insert_post( $post_array );
329 if ( \is_wp_error( $save_successful ) ) {
330 \wp_send_json_error( 'Slug not saved', 400 );
331 }
332
333 // Update the post object to ensure we have the actual slug.
334 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Updating the post is needed to get the current slug.
335 $post = \get_post( $post_id );
336 if ( ! \is_object( $post ) ) {
337 \wp_send_json_error( 'Updated slug not found', 400 );
338 }
339 }
340
341 \do_action( 'wpseo_saved_postdata' );
342
343 // Output the slug, because it is processed by WP and we need the actual slug again.
344 \wp_send_json_success( [ 'slug' => $post->post_name ] );
345 }
346
347 /**
348 * Determines if the given meta value key is disabled.
349 *
350 * @param string $key The key of the meta value.
351 *
352 * @return bool Whether the given meta value key is disabled.
353 */
354 public function is_meta_value_disabled( $key ) {
355 if ( $key === 'linkdex' && ! $this->seo_analysis->is_enabled() ) {
356 return true;
357 }
358
359 if ( $key === 'content_score' && ! $this->readability_analysis->is_enabled() ) {
360 return true;
361 }
362
363 return false;
364 }
365
366 /**
367 * Enqueues all the needed JS and CSS.
368 *
369 * @return void
370 */
371 public function enqueue() {
372 $post_id = \get_queried_object_id();
373 if ( empty( $post_id ) ) {
374 $post_id = \sanitize_text_field( \filter_input( \INPUT_GET, 'post' ) );
375 }
376
377 if ( $post_id !== 0 ) {
378 // Enqueue files needed for upload functionality.
379 \wp_enqueue_media( [ 'post' => $post_id ] );
380 }
381
382 $this->asset_manager->enqueue_style( 'admin-global' );
383 $this->asset_manager->enqueue_style( 'metabox-css' );
384 $this->asset_manager->enqueue_style( 'scoring' );
385 $this->asset_manager->enqueue_style( 'select2' );
386 $this->asset_manager->enqueue_style( 'monorepo' );
387 $this->asset_manager->enqueue_style( 'admin-css' );
388 $this->asset_manager->enqueue_style( 'elementor' );
389
390 $this->asset_manager->enqueue_script( 'admin-global' );
391 $this->asset_manager->enqueue_script( 'elementor' );
392
393 $yoast_components_l10n = new WPSEO_Admin_Asset_Yoast_Components_L10n();
394 $yoast_components_l10n->localize_script( 'elementor' );
395
396 $this->asset_manager->localize_script( 'elementor', 'wpseoAdminGlobalL10n', \YoastSEO()->helpers->wincher->get_admin_global_links() );
397 $this->asset_manager->localize_script( 'elementor', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
398 $this->asset_manager->localize_script( 'elementor', 'wpseoFeaturesL10n', WPSEO_Utils::retrieve_enabled_features() );
399
400 $plugins_script_data = [
401 'replaceVars' => [
402 'no_parent_text' => \__( '(no parent)', 'wordpress-seo' ),
403 'replace_vars' => $this->get_replace_vars(),
404 'recommended_replace_vars' => $this->get_recommended_replace_vars(),
405 'hidden_replace_vars' => $this->get_hidden_replace_vars(),
406 'scope' => $this->determine_scope(),
407 'has_taxonomies' => $this->current_post_type_has_taxonomies(),
408 ],
409 'shortcodes' => [
410 'wpseo_filter_shortcodes_nonce' => \wp_create_nonce( 'wpseo-filter-shortcodes' ),
411 'wpseo_shortcode_tags' => $this->get_valid_shortcode_tags(),
412 ],
413 ];
414
415 $worker_script_data = [
416 'url' => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-analysis-worker' ),
417 'dependencies' => \YoastSEO()->helpers->asset->get_dependency_urls_by_handle( 'yoast-seo-analysis-worker' ),
418 'keywords_assessment_url' => \YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-used-keywords-assessment' ),
419 'log_level' => WPSEO_Utils::get_analysis_worker_log_level(),
420 // We need to make the feature flags separately available inside of the analysis web worker.
421 'enabled_features' => WPSEO_Utils::retrieve_enabled_features(),
422 ];
423
424 $alert_dismissal_action = \YoastSEO()->classes->get( Alert_Dismissal_Action::class );
425 $dismissed_alerts = $alert_dismissal_action->all_dismissed();
426
427 $script_data = [
428 'media' => [ 'choose_image' => \__( 'Use Image', 'wordpress-seo' ) ],
429 'metabox' => $this->get_metabox_script_data(),
430 'userLanguageCode' => WPSEO_Language_Utils::get_language( \get_user_locale() ),
431 'isPost' => true,
432 'isBlockEditor' => WP_Screen::get()->is_block_editor(),
433 'isElementorEditor' => true,
434 'postStatus' => get_post_status( $post_id ),
435 'analysis' => [
436 'plugins' => $plugins_script_data,
437 'worker' => $worker_script_data,
438 'estimatedReadingTimeEnabled' => $this->estimated_reading_time_conditional->is_met(),
439 ],
440 'dismissedAlerts' => $dismissed_alerts,
441 ];
442
443 if ( \post_type_supports( $this->get_metabox_post()->post_type, 'thumbnail' ) ) {
444 $this->asset_manager->enqueue_style( 'featured-image' );
445
446 $script_data['featuredImage'] = [
447 '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' ),
448 ];
449 }
450
451 $this->asset_manager->localize_script( 'elementor', 'wpseoScriptData', $script_data );
452 $this->asset_manager->enqueue_user_language_script();
453 }
454
455 /**
456 * Renders the metabox hidden fields.
457 *
458 * @return void
459 */
460 protected function render_hidden_fields() {
461 // Wrap in a form with an action and post_id for the submit.
462 \printf(
463 '<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" />',
464 \esc_url( \admin_url( 'admin-ajax.php' ) ),
465 \esc_attr( $this->get_metabox_post()->ID )
466 );
467
468 \wp_nonce_field( 'wpseo_elementor_save', '_wpseo_elementor_nonce' );
469 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
470 echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'general' );
471
472 if ( $this->is_advanced_metadata_enabled ) {
473 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
474 echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'advanced' );
475 }
476
477 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
478 echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'schema', $this->get_metabox_post()->post_type );
479
480 if ( $this->social_is_enabled ) {
481 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: Meta_Fields_Presenter->present is considered safe.
482 echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'social' );
483 }
484
485 \printf(
486 '<input type="hidden" id="%1$s" name="%1$s" value="%2$s" />',
487 \esc_attr( WPSEO_Meta::$form_prefix . 'slug' ),
488 \esc_attr( $this->get_post_slug() )
489 );
490
491 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output should be escaped in the filter.
492 echo \apply_filters( 'wpseo_elementor_hidden_fields', '' );
493
494 echo '</form>';
495 }
496
497 /**
498 * Returns the slug for the post being edited.
499 *
500 * @return string
501 */
502 protected function get_post_slug() {
503 $post = $this->get_metabox_post();
504
505 // In case get_metabox_post returns null for whatever reason.
506 if ( ! $post instanceof WP_Post ) {
507 return '';
508 }
509
510 // Drafts might not have a post_name unless the slug has been manually changed.
511 // In this case we get it using get_sample_permalink.
512 if ( ! $post->post_name ) {
513 $sample = \get_sample_permalink( $post );
514
515 // Since get_sample_permalink runs through filters, ensure that it has the expected return value.
516 if ( is_array( $sample ) && count( $sample ) === 2 && is_string( $sample[1] ) ) {
517 return $sample[1];
518 }
519 }
520
521 return $post->post_name;
522 }
523
524 /**
525 * Returns post in metabox context.
526 *
527 * @return WP_Post|null
528 */
529 protected function get_metabox_post() {
530 if ( $this->post !== null ) {
531 return $this->post;
532 }
533
534 $post = \filter_input( \INPUT_GET, 'post' );
535 if ( ! empty( $post ) ) {
536 $post_id = (int) WPSEO_Utils::validate_int( $post );
537
538 $this->post = \get_post( $post_id );
539
540 return $this->post;
541 }
542
543 if ( isset( $GLOBALS['post'] ) ) {
544 $this->post = $GLOBALS['post'];
545
546 return $this->post;
547 }
548
549 return null;
550 }
551
552 /**
553 * Passes variables to js for use with the post-scraper.
554 *
555 * @return array
556 */
557 protected function get_metabox_script_data() {
558 $permalink = '';
559
560 if ( \is_object( $this->get_metabox_post() ) ) {
561 $permalink = \get_sample_permalink( $this->get_metabox_post()->ID );
562 $permalink = $permalink[0];
563 }
564
565 $post_formatter = new WPSEO_Metabox_Formatter(
566 new WPSEO_Post_Metabox_Formatter( $this->get_metabox_post(), [], $permalink )
567 );
568
569 $values = $post_formatter->get_values();
570
571 /** This filter is documented in admin/filters/class-cornerstone-filter.php. */
572 $post_types = \apply_filters( 'wpseo_cornerstone_post_types', \YoastSEO()->helpers->post_type->get_accessible_post_types() );
573 if ( $values['cornerstoneActive'] && ! \in_array( $this->get_metabox_post()->post_type, $post_types, true ) ) {
574 $values['cornerstoneActive'] = false;
575 }
576
577 return $values;
578 }
579
580 /**
581 * Prepares the replace vars for localization.
582 *
583 * @return array Replace vars.
584 */
585 protected function get_replace_vars() {
586 $cached_replacement_vars = [];
587
588 $vars_to_cache = [
589 'date',
590 'id',
591 'sitename',
592 'sitedesc',
593 'sep',
594 'page',
595 'currentyear',
596 'currentdate',
597 'currentmonth',
598 'currentday',
599 'tag',
600 'category',
601 'category_title',
602 'primary_category',
603 'pt_single',
604 'pt_plural',
605 'modified',
606 'name',
607 'user_description',
608 'pagetotal',
609 'pagenumber',
610 'post_year',
611 'post_month',
612 'post_day',
613 'author_first_name',
614 'author_last_name',
615 'permalink',
616 'post_content',
617 ];
618
619 foreach ( $vars_to_cache as $var ) {
620 $cached_replacement_vars[ $var ] = \wpseo_replace_vars( '%%' . $var . '%%', $this->get_metabox_post() );
621 }
622
623 // Merge custom replace variables with the WordPress ones.
624 return \array_merge( $cached_replacement_vars, $this->get_custom_replace_vars( $this->get_metabox_post() ) );
625 }
626
627 /**
628 * Prepares the recommended replace vars for localization.
629 *
630 * @return array Recommended replacement variables.
631 */
632 protected function get_recommended_replace_vars() {
633 $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
634
635 // What is recommended depends on the current context.
636 $post_type = $recommended_replace_vars->determine_for_post( $this->get_metabox_post() );
637
638 return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
639 }
640
641 /**
642 * Returns the list of replace vars that should be hidden inside the editor.
643 *
644 * @return string[] The hidden replace vars.
645 */
646 protected function get_hidden_replace_vars() {
647 return ( new WPSEO_Replace_Vars() )->get_hidden_replace_vars();
648 }
649
650 /**
651 * Gets the custom replace variables for custom taxonomies and fields.
652 *
653 * @param WP_Post $post The post to check for custom taxonomies and fields.
654 *
655 * @return array Array containing all the replacement variables.
656 */
657 protected function get_custom_replace_vars( $post ) {
658 return [
659 'custom_fields' => $this->get_custom_fields_replace_vars( $post ),
660 'custom_taxonomies' => $this->get_custom_taxonomies_replace_vars( $post ),
661 ];
662 }
663
664 /**
665 * Gets the custom replace variables for custom taxonomies.
666 *
667 * @param WP_Post $post The post to check for custom taxonomies.
668 *
669 * @return array Array containing all the replacement variables.
670 */
671 protected function get_custom_taxonomies_replace_vars( $post ) {
672 $taxonomies = \get_object_taxonomies( $post, 'objects' );
673 $custom_replace_vars = [];
674
675 foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
676
677 if ( \is_string( $taxonomy ) ) { // If attachment, see https://core.trac.wordpress.org/ticket/37368 .
678 $taxonomy_name = $taxonomy;
679 $taxonomy = \get_taxonomy( $taxonomy_name );
680 }
681
682 if ( $taxonomy->_builtin && $taxonomy->public ) {
683 continue;
684 }
685
686 $custom_replace_vars[ $taxonomy_name ] = [
687 'name' => $taxonomy->name,
688 'description' => $taxonomy->description,
689 ];
690 }
691
692 return $custom_replace_vars;
693 }
694
695 /**
696 * Gets the custom replace variables for custom fields.
697 *
698 * @param WP_Post $post The post to check for custom fields.
699 *
700 * @return array Array containing all the replacement variables.
701 */
702 protected function get_custom_fields_replace_vars( $post ) {
703 $custom_replace_vars = [];
704
705 // If no post object is passed, return the empty custom_replace_vars array.
706 if ( ! \is_object( $post ) ) {
707 return $custom_replace_vars;
708 }
709
710 $custom_fields = \get_post_custom( $post->ID );
711
712 foreach ( $custom_fields as $custom_field_name => $custom_field ) {
713 // Skip private custom fields.
714 if ( \substr( $custom_field_name, 0, 1 ) === '_' ) {
715 continue;
716 }
717
718 // Skip custom field values that are serialized.
719 if ( \is_serialized( $custom_field[0] ) ) {
720 continue;
721 }
722
723 $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
724 }
725
726 return $custom_replace_vars;
727 }
728
729 /**
730 * Determines the scope based on the post type.
731 * This can be used by the replacevar plugin to determine if a replacement needs to be executed.
732 *
733 * @return string String describing the current scope.
734 */
735 protected function determine_scope() {
736 if ( $this->get_metabox_post()->post_type === 'page' ) {
737 return 'page';
738 }
739
740 return 'post';
741 }
742
743 /**
744 * Determines whether or not the current post type has registered taxonomies.
745 *
746 * @return bool Whether the current post type has taxonomies.
747 */
748 protected function current_post_type_has_taxonomies() {
749 $post_taxonomies = \get_object_taxonomies( $this->get_metabox_post()->post_type );
750
751 return ! empty( $post_taxonomies );
752 }
753
754 /**
755 * Returns an array with shortcode tags for all registered shortcodes.
756 *
757 * @return array
758 */
759 protected function get_valid_shortcode_tags() {
760 $shortcode_tags = [];
761
762 foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
763 $shortcode_tags[] = $tag;
764 }
765
766 return $shortcode_tags;
767 }
768 }
769