Skip to content

Commit

Permalink
Add: Footnotes support for other CPT's.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Dec 22, 2023
1 parent 47b7afb commit 6648c03
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

0 comments on commit 6648c03

Please sign in to comment.