PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.3
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.3
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.3, at src/integrations/third-party/elementor.php

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