Skip to content
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

Output post classes in the editor #60642

Merged
merged 9 commits into from
May 1, 2024
46 changes: 42 additions & 4 deletions packages/block-library/src/post-template/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,35 @@ const TEMPLATE = [
[ 'core/post-excerpt' ],
];

function PostTemplateInnerBlocks() {
function useClassNameFromBlockContext( postId, postType ) {
const post = useSelect(
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
( select ) =>
select( coreStore ).getEditedEntityRecord(
'postType',
postType,
postId
),
[ postType, postId ]
);

const { post_class: postClass } = post;

let classes = 'wp-block-post';

if ( postClass ) {
classes = classnames( classes, postClass );
}
return classes;
}

function PostTemplateInnerBlocks( { blockContextId, blockContextPostType } ) {
const classes = useClassNameFromBlockContext(
blockContextId,
blockContextPostType
);

const innerBlocksProps = useInnerBlocksProps(
{ className: 'wp-block-post' },
{ className: classes },
{ template: TEMPLATE, __unstableDisableLayoutClassNames: true }
);
return <li { ...innerBlocksProps } />;
Expand All @@ -38,13 +64,19 @@ function PostTemplateInnerBlocks() {
function PostTemplateBlockPreview( {
blocks,
blockContextId,
blockContextPostType,
isHidden,
setActiveBlockContextId,
} ) {
const classes = useClassNameFromBlockContext(
blockContextId,
blockContextPostType
);

const blockPreviewProps = useBlockPreview( {
blocks,
props: {
className: 'wp-block-post',
className: classes,
},
} );

Expand Down Expand Up @@ -280,11 +312,17 @@ export default function PostTemplateEdit( {
{ blockContext.postId ===
( activeBlockContextId ||
blockContexts[ 0 ]?.postId ) ? (
<PostTemplateInnerBlocks />
<PostTemplateInnerBlocks
blockContextId={ blockContext.postId }
blockContextPostType={
blockContext.postType
}
/>
) : null }
<MemoizedPostTemplateBlockPreview
blocks={ blocks }
blockContextId={ blockContext.postId }
blockContextPostType={ blockContext.postType }
setActiveBlockContextId={
setActiveBlockContextId
}
Expand Down
25 changes: 25 additions & 0 deletions packages/block-library/src/post-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,28 @@
);
}
add_action( 'init', 'register_block_core_post_template' );

/**
* Adds the post classes to the REST API response.
*
* @since 6.6.0?
*/
function add_post_class_to_api( $data, $post, $context ) {

Check failure on line 169 in packages/block-library/src/post-template/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

The function name 'add_post_class_to_api()' is invalid. In this file, PHP function names must either match one of the allowed prefixes exactly or begin with one of them, followed by an underscore. The allowed prefixes are: 'block_core_post_template', 'render_block_core_post_template', 'register_block_core_post_template'.

Check warning on line 169 in packages/block-library/src/post-template/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Unused function parameter $context.
$data->data['post_class'] = get_post_class( '', $post->ID );

return $data;
}

/**
* Adds the post classes to all post types in the REST API.
*
* @since 6.6.0?
*/
function add_post_class_to_all_post_types() {

Check failure on line 180 in packages/block-library/src/post-template/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

The function name 'add_post_class_to_all_post_types()' is invalid. In this file, PHP function names must either match one of the allowed prefixes exactly or begin with one of them, followed by an underscore. The allowed prefixes are: 'block_core_post_template', 'render_block_core_post_template', 'register_block_core_post_template'.
$post_types = get_post_types( array( 'public' => true ), 'names' );

foreach ( $post_types as $post_type ) {
add_filter( "rest_prepare_{$post_type}", 'add_post_class_to_api', 10, 3 );
}
}
add_action( 'rest_api_init', 'add_post_class_to_all_post_types' );
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
Loading