From 8d731a12baaf569d170eabe4f90d14e2ec6922af Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Thu, 1 Feb 2024 14:04:13 +0000 Subject: [PATCH] Add: Footnotes support in core. --- src/wp-includes/blocks/footnotes.php | 21 ---------------- src/wp-includes/class-wp-post-type.php | 3 +++ src/wp-includes/default-filters.php | 3 +++ src/wp-includes/post.php | 35 ++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/wp-includes/blocks/footnotes.php b/src/wp-includes/blocks/footnotes.php index 0cd2ad73ef3d4..dd76bf75bc47b 100644 --- a/src/wp-includes/blocks/footnotes.php +++ b/src/wp-includes/blocks/footnotes.php @@ -68,27 +68,6 @@ function render_block_core_footnotes( $attributes, $content, $block ) { * @since 6.3.0 */ function register_block_core_footnotes() { - $post_types = get_post_types( - array( - 'show_in_rest' => true, - 'public' => true, - ) - ); - foreach ( $post_types as $post_type ) { - // Only register the meta field if the post type supports the editor, custom fields, and revisions. - if ( post_type_supports( $post_type, 'editor' ) && post_type_supports( $post_type, 'custom-fields' ) && post_type_supports( $post_type, 'revisions' ) ) { - register_post_meta( - $post_type, - 'footnotes', - array( - 'show_in_rest' => true, - 'single' => true, - 'type' => 'string', - 'revisions_enabled' => true, - ) - ); - } - } register_block_type_from_metadata( __DIR__ . '/footnotes', array( diff --git a/src/wp-includes/class-wp-post-type.php b/src/wp-includes/class-wp-post-type.php index 7a2769ed88327..3e291d86b473b 100644 --- a/src/wp-includes/class-wp-post-type.php +++ b/src/wp-includes/class-wp-post-type.php @@ -669,6 +669,9 @@ public function add_supports() { add_post_type_support( $this->name, $args ); } } + if ( post_type_supports( $this->name, 'editor' ) && post_type_supports( $this->name, 'custom-fields' ) && post_type_supports( $this->name, 'revisions' ) && ! post_type_supports( $this->name, 'footnotes' ) ) { + add_post_type_support( $this->name, 'footnotes' ); + } unset( $this->supports ); } elseif ( false !== $this->supports ) { // Add default features. diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 7ebc3a1d3bfe1..58779dd133d97 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -734,6 +734,9 @@ // CPT wp_block custom postmeta field. add_action( 'init', 'wp_create_initial_post_meta' ); +// Registers the footnotes meta field for post types that support it. +add_action( 'init', '_wp_register_footnotes_meta_field', 100 ); + // Include revisioned meta when considering whether a post revision has changed. add_filter( 'wp_save_post_revision_post_has_changed', 'wp_check_revisioned_meta_fields_have_changed', 10, 3 ); diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 69a63625524d1..2501bc9c290d9 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -8150,3 +8150,38 @@ function wp_create_initial_post_meta() { ) ); } + +/** + * Registers the footnotes meta field for post types that support it. + * + * @internal + * @since 6.5.0 + * + * @link https://github.com/WordPress/gutenberg/pull/57353 + */ +function _wp_register_footnotes_meta_field() { + $post_types = get_post_types( + array( + 'show_in_rest' => true, + 'public' => true, + ) + ); + foreach ( $post_types as $post_type ) { + // Only register the meta field if the post type supports the editor, custom fields, and revisions. + if ( post_type_supports( $post_type, 'editor' ) && post_type_supports( $post_type, 'custom-fields' ) && post_type_supports( $post_type, 'revisions' ) && post_type_supports( $post_type, 'footnotes' ) ) { + $post_type_meta_keys = get_registered_meta_keys( 'post', $post_type ); + if ( ! isset( $post_type_meta_keys['footnotes'] ) ) { + register_post_meta( + $post_type, + 'footnotes', + array( + 'show_in_rest' => true, + 'single' => true, + 'type' => 'string', + 'revisions_enabled' => true, + ) + ); + } + } + } +}