Skip to content

Commit

Permalink
Add: Footnotes support for other CPT's. (#57353)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta authored Jan 22, 2024
1 parent d7bba1b commit 8cd2a9d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
3 changes: 2 additions & 1 deletion packages/block-library/src/footnotes/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ export default function FootnotesEdit( { context: { postType, postId } } ) {
'meta',
postId
);
const footnotesSupported = 'string' === typeof meta?.footnotes;
const footnotes = meta?.footnotes ? JSON.parse( meta.footnotes ) : [];
const blockProps = useBlockProps();

if ( postType !== 'post' && postType !== 'page' ) {
if ( ! footnotesSupported ) {
return (
<div { ...blockProps }>
<Placeholder
Expand Down
10 changes: 7 additions & 3 deletions packages/block-library/src/footnotes/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
privateApis,
} from '@wordpress/block-editor';
import { useSelect, useDispatch, useRegistry } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';
import { createBlock, store as blocksStore } from '@wordpress/blocks';

/**
Expand All @@ -38,12 +39,12 @@ export const format = {
},
interactive: true,
contentEditable: false,
[ usesContextKey ]: [ 'postType' ],
[ usesContextKey ]: [ 'postType', 'postId' ],
edit: function Edit( {
value,
onChange,
isObjectActive,
context: { postType },
context: { postType, postId },
} ) {
const registry = useRegistry();
const {
Expand Down Expand Up @@ -74,14 +75,17 @@ export const format = {
return parentCoreBlocks && parentCoreBlocks.length > 0;
}, [] );

const [ meta ] = useEntityProp( 'postType', postType, 'meta', postId );
const footnotesSupported = 'string' === typeof meta?.footnotes;

const { selectionChange, insertBlock } =
useDispatch( blockEditorStore );

if ( ! hasFootnotesBlockType ) {
return null;
}

if ( postType !== 'post' && postType !== 'page' ) {
if ( ! footnotesSupported ) {
return null;
}

Expand Down
31 changes: 20 additions & 11 deletions packages/block-library/src/footnotes/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,26 @@ function render_block_core_footnotes( $attributes, $content, $block ) {
* @since 6.3.0
*/
function register_block_core_footnotes() {
foreach ( array( 'post', 'page' ) as $post_type ) {
register_post_meta(
$post_type,
'footnotes',
array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'revisions_enabled' => true,
)
);
$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',
Expand Down

1 comment on commit 8cd2a9d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 8cd2a9d.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7612604153
📝 Reported issues:

Please sign in to comment.