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
26 changes: 26 additions & 0 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,29 @@ function gutenberg_register_global_styles_endpoints() {
$global_styles_controller->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_global_styles_endpoints' );

/**
* Adds the post classes to the REST API response.
*
* @param WP_REST_Response $response Response object.
* @param WP_Post $post Post object.
*
* @return WP_REST_Response Response object.
*/
function gutenberg_add_post_class_to_api_response( $response, $post ) {
$response->data['post_class'] = get_post_class( '', $post->ID );
huubl marked this conversation as resolved.
Show resolved Hide resolved

return $response;
}

/**
* Adds the post classes to all post types in the REST API.
*/
function gutenberg_add_post_class_to_all_post_types() {
$post_types = get_post_types( array( 'public' => true ), 'names' );

foreach ( $post_types as $post_type ) {
add_filter( "rest_prepare_{$post_type}", 'gutenberg_add_post_class_to_api_response', 10, 3 );
huubl marked this conversation as resolved.
Show resolved Hide resolved
}
}
add_action( 'rest_api_init', 'gutenberg_add_post_class_to_all_post_types' );
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
Loading