From d8e437cb0b527723b5bbffa20e2568a9c415cf50 Mon Sep 17 00:00:00 2001
From: huubl <50170696+huubl@users.noreply.github.com>
Date: Wed, 10 Apr 2024 21:47:58 +0200
Subject: [PATCH 1/9] Add post classes next to wp-post-block in the editor
---
.../block-library/src/post-template/edit.js | 46 +++++++++++++++++--
.../block-library/src/post-template/index.php | 25 ++++++++++
2 files changed, 67 insertions(+), 4 deletions(-)
diff --git a/packages/block-library/src/post-template/edit.js b/packages/block-library/src/post-template/edit.js
index 3db9bf465fa05d..a0710677192a9a 100644
--- a/packages/block-library/src/post-template/edit.js
+++ b/packages/block-library/src/post-template/edit.js
@@ -27,9 +27,35 @@ const TEMPLATE = [
[ 'core/post-excerpt' ],
];
-function PostTemplateInnerBlocks() {
+function useClassNameFromBlockContext( postId, postType ) {
+ const post = useSelect(
+ ( 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
;
@@ -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,
},
} );
@@ -280,11 +312,17 @@ export default function PostTemplateEdit( {
{ blockContext.postId ===
( activeBlockContextId ||
blockContexts[ 0 ]?.postId ) ? (
-
+
) : null }
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() {
+ $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' );
From e0873a08d5972095e70e81e9dbab11b0ab3ca8f4 Mon Sep 17 00:00:00 2001
From: huubl <50170696+huubl@users.noreply.github.com>
Date: Thu, 11 Apr 2024 10:24:28 +0200
Subject: [PATCH 2/9] Move REST API functions to rest-api.php
---
lib/rest-api.php | 26 +++++++++++++++++++
.../block-library/src/post-template/index.php | 25 ------------------
2 files changed, 26 insertions(+), 25 deletions(-)
diff --git a/lib/rest-api.php b/lib/rest-api.php
index 04f521d132c461..a7008d6b6e87b1 100644
--- a/lib/rest-api.php
+++ b/lib/rest-api.php
@@ -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 $data Response object.
+ * @param WP_Post $post Post object.
+ *
+ * @return WP_REST_Response Response object.
+ */
+function gutenberg_add_post_class_to_api_response( $data, $post ) {
+ $data->data['post_class'] = get_post_class( '', $post->ID );
+
+ return $data;
+}
+
+/**
+ * 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 );
+ }
+}
+add_action( 'rest_api_init', 'gutenberg_add_post_class_to_all_post_types' );
diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php
index 7411e68bca7552..9126355c096a57 100644
--- a/packages/block-library/src/post-template/index.php
+++ b/packages/block-library/src/post-template/index.php
@@ -160,28 +160,3 @@ function register_block_core_post_template() {
);
}
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 ) {
- $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() {
- $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' );
From 5d1ac128ca87cbf488c932d21727c493810bc1c7 Mon Sep 17 00:00:00 2001
From: huubl <50170696+huubl@users.noreply.github.com>
Date: Thu, 11 Apr 2024 11:02:18 +0200
Subject: [PATCH 3/9] Rename parameter to match
https://developer.wordpress.org/reference/hooks/rest_prepare_this-post_type/
---
lib/rest-api.php | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/lib/rest-api.php b/lib/rest-api.php
index a7008d6b6e87b1..121d77d43e56a3 100644
--- a/lib/rest-api.php
+++ b/lib/rest-api.php
@@ -22,15 +22,15 @@ function gutenberg_register_global_styles_endpoints() {
/**
* Adds the post classes to the REST API response.
*
- * @param WP_REST_Response $data Response object.
+ * @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( $data, $post ) {
- $data->data['post_class'] = get_post_class( '', $post->ID );
+function gutenberg_add_post_class_to_api_response( $response, $post ) {
+ $response->data['post_class'] = get_post_class( '', $post->ID );
- return $data;
+ return $response;
}
/**
From aee3fcbc7e1eaee464f4c374bd1f3082b9e5faba Mon Sep 17 00:00:00 2001
From: huubl <50170696+huubl@users.noreply.github.com>
Date: Fri, 26 Apr 2024 16:18:04 +0200
Subject: [PATCH 4/9] Move REST API functions to
lib/compat/wordpress-6.6/rest-api.php
---
lib/compat/wordpress-6.6/rest-api.php | 26 ++++++++++++++++++++++++++
lib/rest-api.php | 26 --------------------------
2 files changed, 26 insertions(+), 26 deletions(-)
diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php
index bf462cd11ca4b4..31fcfb7abb1040 100644
--- a/lib/compat/wordpress-6.6/rest-api.php
+++ b/lib/compat/wordpress-6.6/rest-api.php
@@ -29,3 +29,29 @@ 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 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 );
+
+ 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 );
+ }
+}
+add_action( 'rest_api_init', 'gutenberg_add_post_class_to_all_post_types' );
diff --git a/lib/rest-api.php b/lib/rest-api.php
index 121d77d43e56a3..04f521d132c461 100644
--- a/lib/rest-api.php
+++ b/lib/rest-api.php
@@ -18,29 +18,3 @@ 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 );
-
- 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 );
- }
-}
-add_action( 'rest_api_init', 'gutenberg_add_post_class_to_all_post_types' );
From 3becf858697a2afa13a4ee3c9f7313a0ab813839 Mon Sep 17 00:00:00 2001
From: huubl <50170696+huubl@users.noreply.github.com>
Date: Fri, 26 Apr 2024 16:52:08 +0200
Subject: [PATCH 5/9] Rename post_class to class_list
---
lib/compat/wordpress-6.6/rest-api.php | 10 +++++-----
packages/block-library/src/post-template/edit.js | 6 +++---
2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php
index 31fcfb7abb1040..760c0417c974ff 100644
--- a/lib/compat/wordpress-6.6/rest-api.php
+++ b/lib/compat/wordpress-6.6/rest-api.php
@@ -38,8 +38,8 @@ function wp_api_template_access_controller( $args, $post_type ) {
*
* @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 );
+function gutenberg_add_class_list_to_api_response( $response, $post ) {
+ $response->data['class_list'] = get_post_class( '', $post->ID );
return $response;
}
@@ -47,11 +47,11 @@ function gutenberg_add_post_class_to_api_response( $response, $post ) {
/**
* Adds the post classes to all post types in the REST API.
*/
-function gutenberg_add_post_class_to_all_post_types() {
+function gutenberg_add_class_list_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 );
+ add_filter( "rest_prepare_{$post_type}", 'gutenberg_add_class_list_to_api_response', 10, 3 );
}
}
-add_action( 'rest_api_init', 'gutenberg_add_post_class_to_all_post_types' );
+add_action( 'rest_api_init', 'gutenberg_add_class_list_to_all_post_types' );
diff --git a/packages/block-library/src/post-template/edit.js b/packages/block-library/src/post-template/edit.js
index a0710677192a9a..4189fddf3f8ce4 100644
--- a/packages/block-library/src/post-template/edit.js
+++ b/packages/block-library/src/post-template/edit.js
@@ -38,12 +38,12 @@ function useClassNameFromBlockContext( postId, postType ) {
[ postType, postId ]
);
- const { post_class: postClass } = post;
+ const { class_list: classList } = post;
let classes = 'wp-block-post';
- if ( postClass ) {
- classes = classnames( classes, postClass );
+ if ( classList ) {
+ classes = classnames( classes, classList );
}
return classes;
}
From d642e45e310a92be0fe7203986aab0d5c2cc9f6e Mon Sep 17 00:00:00 2001
From: huubl <50170696+huubl@users.noreply.github.com>
Date: Fri, 26 Apr 2024 18:39:02 +0200
Subject: [PATCH 6/9] Use register_rest_field()
---
lib/compat/wordpress-6.6/rest-api.php | 37 +++++++++++++++++++--------
1 file changed, 26 insertions(+), 11 deletions(-)
diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php
index 760c0417c974ff..0109e33d212173 100644
--- a/lib/compat/wordpress-6.6/rest-api.php
+++ b/lib/compat/wordpress-6.6/rest-api.php
@@ -33,25 +33,40 @@ function wp_api_template_access_controller( $args, $post_type ) {
/**
* Adds the post classes to the REST API response.
*
- * @param WP_REST_Response $response Response object.
- * @param WP_Post $post Post object.
+ * @param array $post The response object data.
*
- * @return WP_REST_Response Response object.
+ * @return array
*/
-function gutenberg_add_class_list_to_api_response( $response, $post ) {
- $response->data['class_list'] = get_post_class( '', $post->ID );
+function gutenberg_add_class_list_to_api_response( $post ) {
- return $response;
+ if ( ! isset( $post['id'] ) ) {
+ return $post;
+ }
+
+ return get_post_class( array(), $post['id'] );
}
/**
- * Adds the post classes to all post types in the REST API.
+ * Adds the post classes to public post types in the REST API.
*/
-function gutenberg_add_class_list_to_all_post_types() {
+function gutenberg_add_class_list_to_public_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_class_list_to_api_response', 10, 3 );
+ 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_all_post_types' );
+add_action( 'rest_api_init', 'gutenberg_add_class_list_to_public_post_types' );
From f56989add3c95c055c0909d9525cb7f8383f8496 Mon Sep 17 00:00:00 2001
From: huubl <50170696+huubl@users.noreply.github.com>
Date: Mon, 29 Apr 2024 09:41:05 +0200
Subject: [PATCH 7/9] Return empty array if post id is not set
---
lib/compat/wordpress-6.6/rest-api.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php
index 0109e33d212173..ad8f61224f3660 100644
--- a/lib/compat/wordpress-6.6/rest-api.php
+++ b/lib/compat/wordpress-6.6/rest-api.php
@@ -40,7 +40,7 @@ function wp_api_template_access_controller( $args, $post_type ) {
function gutenberg_add_class_list_to_api_response( $post ) {
if ( ! isset( $post['id'] ) ) {
- return $post;
+ return array();
}
return get_post_class( array(), $post['id'] );
From c2b91d26e40b45b810a31e08b5b51710298f5d5e Mon Sep 17 00:00:00 2001
From: huubl <50170696+huubl@users.noreply.github.com>
Date: Tue, 30 Apr 2024 23:00:38 +0200
Subject: [PATCH 8/9] add argument: 'show_in_rest' => true
---
lib/compat/wordpress-6.6/rest-api.php | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php
index ad8f61224f3660..8526093dc99ddb 100644
--- a/lib/compat/wordpress-6.6/rest-api.php
+++ b/lib/compat/wordpress-6.6/rest-api.php
@@ -50,7 +50,13 @@ function gutenberg_add_class_list_to_api_response( $post ) {
* 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 ), 'names' );
+ $post_types = get_post_types(
+ array(
+ 'public' => true,
+ 'show_in_rest' => true,
+ ),
+ 'names'
+ );
if ( ! empty( $post_types ) ) {
register_rest_field(
From 090b1f516a9ed5ed5aacb724d85ec7fc8da4f3df Mon Sep 17 00:00:00 2001
From: huubl <50170696+huubl@users.noreply.github.com>
Date: Tue, 30 Apr 2024 23:34:04 +0200
Subject: [PATCH 9/9] Remove hook and add class list field to blockContexts
---
.../block-library/src/post-template/edit.js | 47 +++----------------
1 file changed, 7 insertions(+), 40 deletions(-)
diff --git a/packages/block-library/src/post-template/edit.js b/packages/block-library/src/post-template/edit.js
index 4189fddf3f8ce4..4302c44309423a 100644
--- a/packages/block-library/src/post-template/edit.js
+++ b/packages/block-library/src/post-template/edit.js
@@ -27,35 +27,9 @@ const TEMPLATE = [
[ 'core/post-excerpt' ],
];
-function useClassNameFromBlockContext( postId, postType ) {
- const post = useSelect(
- ( select ) =>
- select( coreStore ).getEditedEntityRecord(
- 'postType',
- postType,
- postId
- ),
- [ postType, postId ]
- );
-
- const { class_list: classList } = post;
-
- let classes = 'wp-block-post';
-
- if ( classList ) {
- classes = classnames( classes, classList );
- }
- return classes;
-}
-
-function PostTemplateInnerBlocks( { blockContextId, blockContextPostType } ) {
- const classes = useClassNameFromBlockContext(
- blockContextId,
- blockContextPostType
- );
-
+function PostTemplateInnerBlocks( { classList } ) {
const innerBlocksProps = useInnerBlocksProps(
- { className: classes },
+ { className: classnames( 'wp-block-post', classList ) },
{ template: TEMPLATE, __unstableDisableLayoutClassNames: true }
);
return ;
@@ -64,19 +38,14 @@ function PostTemplateInnerBlocks( { blockContextId, blockContextPostType } ) {
function PostTemplateBlockPreview( {
blocks,
blockContextId,
- blockContextPostType,
+ classList,
isHidden,
setActiveBlockContextId,
} ) {
- const classes = useClassNameFromBlockContext(
- blockContextId,
- blockContextPostType
- );
-
const blockPreviewProps = useBlockPreview( {
blocks,
props: {
- className: classes,
+ className: classnames( 'wp-block-post', classList ),
},
} );
@@ -245,6 +214,7 @@ export default function PostTemplateEdit( {
posts?.map( ( post ) => ( {
postType: post.type,
postId: post.id,
+ classList: post.class_list ?? '',
} ) ),
[ posts ]
);
@@ -313,16 +283,13 @@ export default function PostTemplateEdit( {
( activeBlockContextId ||
blockContexts[ 0 ]?.postId ) ? (
) : null }