-
Notifications
You must be signed in to change notification settings - Fork 3k
Make footnotes support opt-in. #6043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -662,6 +662,23 @@ public function set_props( $args ) { | |
*/ | ||
public function add_supports() { | ||
if ( ! empty( $this->supports ) ) { | ||
/* | ||
* Move footnotes last. | ||
* | ||
* Footnotes are a special case and require other features to be supported: | ||
* 'editor', 'revisions' and 'custom-fields'. This moves the footnotes to | ||
* be added last so that the other features are already added before the | ||
* check in `add_post_type_support` is made. | ||
*/ | ||
if ( isset( $this->supports['footnotes'] ) ) { | ||
$footnotes = $this->supports['footnotes']; | ||
unset( $this->supports['footnotes'] ); | ||
$this->supports['footnotes'] = $footnotes; | ||
} elseif ( in_array( 'footnotes', $this->supports, true ) ) { | ||
$index = array_search( 'footnotes', $this->supports, true ); | ||
unset( $this->supports[ $index ] ); | ||
$this->supports[] = 'footnotes'; | ||
} | ||
Comment on lines
+665
to
+681
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest moving this code to its method and then invoking it here before the loop. What's going on here as a very specific purpose beyond but in support of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That said, +1 for the approach. |
||
foreach ( $this->supports as $feature => $args ) { | ||
if ( is_array( $args ) ) { | ||
add_post_type_support( $this->name, $feature, $args ); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ function create_initial_post_types() { | |
'rewrite' => false, | ||
'query_var' => false, | ||
'delete_with_user' => true, | ||
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ), | ||
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats', 'footnotes' ), | ||
'show_in_rest' => true, | ||
'rest_base' => 'posts', | ||
'rest_controller_class' => 'WP_REST_Posts_Controller', | ||
|
@@ -62,7 +62,7 @@ function create_initial_post_types() { | |
'rewrite' => false, | ||
'query_var' => false, | ||
'delete_with_user' => true, | ||
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ), | ||
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions', 'footnotes' ), | ||
'show_in_rest' => true, | ||
'rest_base' => 'pages', | ||
'rest_controller_class' => 'WP_REST_Posts_Controller', | ||
|
@@ -2215,13 +2215,36 @@ function add_post_type_support( $post_type, $feature, ...$args ) { | |
global $_wp_post_type_features; | ||
|
||
$features = (array) $feature; | ||
if ( in_array( 'footnotes', $features, true ) ) { | ||
$index = array_search( 'footnotes', $features, true ); | ||
unset( $features[ $index ] ); | ||
$features[] = 'footnotes'; | ||
} | ||
Comment on lines
+2218
to
+2222
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, this code will run 2x, as it's also being done in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking more about this ... How about moving the "move to last" code in What might the impact be within |
||
|
||
foreach ( $features as $feature ) { | ||
if ( $args ) { | ||
$_wp_post_type_features[ $post_type ][ $feature ] = $args; | ||
} else { | ||
$_wp_post_type_features[ $post_type ][ $feature ] = true; | ||
} | ||
} | ||
|
||
/* | ||
* Special case for footnotes. | ||
* | ||
* If footnotes are supported, the post type must also support `editor`, `revisions` and `custom-fields`. | ||
*/ | ||
if ( in_array( 'footnotes', $features, true ) ) { | ||
if ( | ||
! isset( | ||
$_wp_post_type_features[ $post_type ]['editor'], | ||
$_wp_post_type_features[ $post_type ]['revisions'], | ||
$_wp_post_type_features[ $post_type ]['custom-fields'] | ||
) | ||
) { | ||
unset( $_wp_post_type_features[ $post_type ]['footnotes'] ); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -2237,6 +2260,11 @@ function add_post_type_support( $post_type, $feature, ...$args ) { | |
function remove_post_type_support( $post_type, $feature ) { | ||
global $_wp_post_type_features; | ||
|
||
if ( in_array( $feature, array( 'editor', 'revisions', 'custom-fields' ), true ) ) { | ||
// Also remove footnotes support if any of the required features are removed. | ||
unset( $_wp_post_type_features[ $post_type ]['footnotes'] ); | ||
} | ||
|
||
unset( $_wp_post_type_features[ $post_type ][ $feature ] ); | ||
} | ||
|
||
|
@@ -2274,6 +2302,22 @@ function get_all_post_type_supports( $post_type ) { | |
function post_type_supports( $post_type, $feature ) { | ||
global $_wp_post_type_features; | ||
|
||
if ( 'footnotes' === $feature ) { | ||
/* | ||
* Footnotes require additional items to be supported. | ||
* | ||
* The post type must also support `editor`, `revisions` and `custom-fields`. | ||
*/ | ||
$required_features = array( 'footnotes', 'editor', 'revisions', 'custom-fields' ); | ||
foreach ( $required_features as $required_feature ) { | ||
if ( ! isset( $_wp_post_type_features[ $post_type ][ $required_feature ] ) ) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given the snowflake code in If it's decided to keep this then similar code will probably be needed in |
||
return ( isset( $_wp_post_type_features[ $post_type ][ $feature ] ) ); | ||
} | ||
|
||
|
@@ -8208,3 +8252,36 @@ function wp_create_initial_post_meta() { | |
) | ||
); | ||
} | ||
|
||
/** | ||
* Registers the footnotes meta field for post types that support it. | ||
* | ||
* @since 6.5.0 | ||
* | ||
* @link https://github.com/WordPress/gutenberg/pull/57353 | ||
*/ | ||
function wp_register_footnotes_meta_field() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this use |
||
$post_types = get_post_types( array( 'show_in_rest' => true ) ); | ||
$post_types = array_filter( $post_types, 'is_post_type_viewable' ); | ||
foreach ( $post_types as $post_type ) { | ||
if ( ! post_type_supports( $post_type, 'footnotes' ) ) { | ||
// Post type does not support footnotes, continue. | ||
continue; | ||
} | ||
$post_type_meta_keys = get_registered_meta_keys( 'post', $post_type ); | ||
if ( isset( $post_type_meta_keys['footnotes'] ) ) { | ||
// Footnotes meta key is already registered, continue. | ||
continue; | ||
} | ||
register_post_meta( | ||
$post_type, | ||
'footnotes', | ||
array( | ||
'show_in_rest' => true, | ||
'single' => true, | ||
'type' => 'string', | ||
'revisions_enabled' => true, | ||
) | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will cause the tests to fail but is required for testing
wp_register_footnotes_meta_field
works as expected.