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
47 changes: 47 additions & 0 deletions lib/compat/wordpress-6.6/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,50 @@ function wp_api_template_access_controller( $args, $post_type ) {
}
}
add_filter( 'register_post_type_args', 'wp_api_template_access_controller', 10, 2 );

/**
* Adds the post classes to the REST API response.
*
* @param array $post The response object data.
*
* @return array
*/
function gutenberg_add_class_list_to_api_response( $post ) {

if ( ! isset( $post['id'] ) ) {
return array();
}

return get_post_class( array(), $post['id'] );
}

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

if ( ! empty( $post_types ) ) {
register_rest_field(
$post_types,
'class_list',
array(
'get_callback' => 'gutenberg_add_class_list_to_api_response',
'schema' => array(
'description' => __( 'An array of the class names for the post container element.', 'gutenberg' ),
'type' => 'array',
'items' => array(
'type' => 'string',
),
),
)
);
}
}
add_action( 'rest_api_init', 'gutenberg_add_class_list_to_public_post_types' );
13 changes: 9 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,9 @@ const TEMPLATE = [
[ 'core/post-excerpt' ],
];

function PostTemplateInnerBlocks() {
function PostTemplateInnerBlocks( { classList } ) {
const innerBlocksProps = useInnerBlocksProps(
{ className: 'wp-block-post' },
{ className: classnames( 'wp-block-post', classList ) },
{ template: TEMPLATE, __unstableDisableLayoutClassNames: true }
);
return <li { ...innerBlocksProps } />;
Expand All @@ -38,13 +38,14 @@ function PostTemplateInnerBlocks() {
function PostTemplateBlockPreview( {
blocks,
blockContextId,
classList,
isHidden,
setActiveBlockContextId,
} ) {
const blockPreviewProps = useBlockPreview( {
blocks,
props: {
className: 'wp-block-post',
className: classnames( 'wp-block-post', classList ),
},
} );

Expand Down Expand Up @@ -213,6 +214,7 @@ export default function PostTemplateEdit( {
posts?.map( ( post ) => ( {
postType: post.type,
postId: post.id,
classList: post.class_list ?? '',
} ) ),
[ posts ]
);
Expand Down Expand Up @@ -280,11 +282,14 @@ export default function PostTemplateEdit( {
{ blockContext.postId ===
( activeBlockContextId ||
blockContexts[ 0 ]?.postId ) ? (
<PostTemplateInnerBlocks />
<PostTemplateInnerBlocks
classList={ blockContext.classList }
/>
) : null }
<MemoizedPostTemplateBlockPreview
blocks={ blocks }
blockContextId={ blockContext.postId }
classList={ blockContext.classList }
setActiveBlockContextId={
setActiveBlockContextId
}
Expand Down
Loading