Commit 84789199ef for wordpress.org
commit 84789199ef774ede9481b9cd23e5913f87ff4ed8
Author: oandregal <oandregal@git.wordpress.org>
Date: Mon Oct 19 14:33:49 2026 +0000
View config endpoint: add align to author and extract utility.
Props oandregal, jorgefilipecosta.
Part of #65981.
Built from https://develop.svn.wordpress.org/trunk@64503
git-svn-id: http://core.svn.wordpress.org/trunk@63653 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/rest-api.php b/wp-includes/rest-api.php
index 36b5b23de9..d8517d2ddf 100644
--- a/wp-includes/rest-api.php
+++ b/wp-includes/rest-api.php
@@ -3227,6 +3227,80 @@ function rest_default_additional_properties_to_false( $schema ) {
return $schema;
}
+/**
+ * Recursively casts empty arrays to objects where the schema types them as objects.
+ *
+ * PHP cannot distinguish an empty associative array from an empty list, so
+ * `json_encode()` always serializes `array()` as a JSON array (`[]`). A REST
+ * schema, however, may type a value as an object, which must encode as `{}`.
+ * This walks the value against its schema and casts any empty, object-typed
+ * array to an object. Non-empty associative arrays already encode as objects,
+ * so they are left as arrays and only recursed into to fix any nested empty
+ * objects.
+ *
+ * Union schemas (`oneOf`/`anyOf`) are handled only for the empty-array case:
+ * an empty value is cast to an object when any branch allows an object. Such
+ * values are not recursed into.
+ *
+ * @since 7.2.0
+ * @access private
+ *
+ * @param mixed $value The value to normalize.
+ * @param array $schema The schema node describing the value.
+ * @return mixed The normalized value, with empty object-typed arrays cast to objects.
+ */
+function _rest_cast_empty_objects_from_schema( $value, $schema ) {
+ if ( ! is_array( $value ) || ! is_array( $schema ) ) {
+ return $value;
+ }
+
+ if ( isset( $schema['oneOf'] ) || isset( $schema['anyOf'] ) ) {
+ $branches = $schema['oneOf'] ?? $schema['anyOf'];
+ if ( array() === $value ) {
+ foreach ( $branches as $branch ) {
+ if ( is_array( $branch ) && in_array( 'object', (array) ( $branch['type'] ?? array() ), true ) ) {
+ return (object) array();
+ }
+ }
+ }
+ return $value;
+ }
+
+ $types = (array) ( $schema['type'] ?? array() );
+
+ if ( in_array( 'array', $types, true ) && isset( $schema['items'] ) ) {
+ foreach ( $value as $index => $item ) {
+ $value[ $index ] = _rest_cast_empty_objects_from_schema( $item, $schema['items'] );
+ }
+ return $value;
+ }
+
+ if ( in_array( 'object', $types, true ) ) {
+ if ( isset( $schema['properties'] ) ) {
+ foreach ( $schema['properties'] as $property => $property_schema ) {
+ if ( array_key_exists( $property, $value ) ) {
+ $value[ $property ] = _rest_cast_empty_objects_from_schema( $value[ $property ], $property_schema );
+ }
+ }
+ }
+ if ( isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) {
+ foreach ( $value as $key => $item ) {
+ if ( isset( $schema['properties'][ $key ] ) ) {
+ continue;
+ }
+ $value[ $key ] = _rest_cast_empty_objects_from_schema( $item, $schema['additionalProperties'] );
+ }
+ }
+
+ // Empty object-typed arrays must serialize as {} to match the schema.
+ if ( array() === $value ) {
+ return (object) array();
+ }
+ }
+
+ return $value;
+}
+
/**
* Gets the REST API route for a post.
*
diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-view-config-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-view-config-controller.php
index da379cc6c8..44de983ab4 100644
--- a/wp-includes/rest-api/endpoints/class-wp-rest-view-config-controller.php
+++ b/wp-includes/rest-api/endpoints/class-wp-rest-view-config-controller.php
@@ -155,10 +155,10 @@ class WP_REST_View_Config_Controller extends WP_REST_Controller {
'kind' => $kind,
'name' => $name,
'version' => WP_View_Config_Data::LATEST_VERSION,
- 'default_view' => $this->cast_empty_objects( $config['default_view'], $schema['properties']['default_view'] ),
- 'default_layouts' => $this->cast_empty_objects( $config['default_layouts'], $schema['properties']['default_layouts'] ),
- 'view_list' => $this->cast_empty_objects( $config['view_list'], $schema['properties']['view_list'] ),
- 'form' => $this->cast_empty_objects( $config['form'], $schema['properties']['form'] ),
+ 'default_view' => _rest_cast_empty_objects_from_schema( $config['default_view'], $schema['properties']['default_view'] ),
+ 'default_layouts' => _rest_cast_empty_objects_from_schema( $config['default_layouts'], $schema['properties']['default_layouts'] ),
+ 'view_list' => _rest_cast_empty_objects_from_schema( $config['view_list'], $schema['properties']['view_list'] ),
+ 'form' => _rest_cast_empty_objects_from_schema( $config['form'], $schema['properties']['form'] ),
);
return rest_ensure_response( $response );
@@ -168,75 +168,17 @@ class WP_REST_View_Config_Controller extends WP_REST_Controller {
* Recursively casts empty arrays to objects where the schema types them as
* objects.
*
- * PHP cannot distinguish an empty associative array from an empty list, so
- * `json_encode()` always serializes `array()` as a JSON array (`[]`). The
- * REST schema, however, types several values as objects, which must encode
- * as `{}`. This walks the value against its schema and casts any empty,
- * object-typed array to an object. Non-empty associative arrays already
- * encode as objects, so they are left as arrays and only recursed into to
- * fix any nested empty objects.
- *
- * Union schemas (`oneOf`/`anyOf`) are handled only for the empty-array case:
- * an empty value is cast to an object when any branch allows an object. Such
- * values are not recursed into, which is sufficient for the form schema
- * where they never contain empty nested objects.
- *
* @since 7.1.0
+ * @since 7.2.0 Moved the logic to _rest_cast_empty_objects_from_schema().
+ *
+ * @see _rest_cast_empty_objects_from_schema()
*
* @param mixed $value The value to normalize.
* @param array $schema The schema node describing the value.
* @return mixed The normalized value, with empty object-typed arrays cast to objects.
*/
protected function cast_empty_objects( $value, $schema ) {
- if ( ! is_array( $value ) || ! is_array( $schema ) ) {
- return $value;
- }
-
- if ( isset( $schema['oneOf'] ) || isset( $schema['anyOf'] ) ) {
- $branches = $schema['oneOf'] ?? $schema['anyOf'];
- if ( array() === $value ) {
- foreach ( $branches as $branch ) {
- if ( is_array( $branch ) && in_array( 'object', (array) ( $branch['type'] ?? array() ), true ) ) {
- return (object) array();
- }
- }
- }
- return $value;
- }
-
- $types = (array) ( $schema['type'] ?? array() );
-
- if ( in_array( 'array', $types, true ) && isset( $schema['items'] ) ) {
- foreach ( $value as $index => $item ) {
- $value[ $index ] = $this->cast_empty_objects( $item, $schema['items'] );
- }
- return $value;
- }
-
- if ( in_array( 'object', $types, true ) ) {
- if ( isset( $schema['properties'] ) ) {
- foreach ( $schema['properties'] as $property => $property_schema ) {
- if ( array_key_exists( $property, $value ) ) {
- $value[ $property ] = $this->cast_empty_objects( $value[ $property ], $property_schema );
- }
- }
- }
- if ( isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) {
- foreach ( $value as $key => $item ) {
- if ( isset( $schema['properties'][ $key ] ) ) {
- continue;
- }
- $value[ $key ] = $this->cast_empty_objects( $item, $schema['additionalProperties'] );
- }
- }
-
- // Empty object-typed arrays must serialize as {} to match the schema.
- if ( array() === $value ) {
- return (object) array();
- }
- }
-
- return $value;
+ return _rest_cast_empty_objects_from_schema( $value, $schema );
}
/**
diff --git a/wp-includes/version.php b/wp-includes/version.php
index 97ddbf69c2..70620ae1ed 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '7.2-alpha-64502';
+$wp_version = '7.2-alpha-64503';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
diff --git a/wp-includes/view-config.php b/wp-includes/view-config.php
index e87558331b..bf10553500 100644
--- a/wp-includes/view-config.php
+++ b/wp-includes/view-config.php
@@ -342,6 +342,7 @@ function _wp_get_entity_view_config_posttype_wp_block( $data ) {
'styles' => array(
'author' => array(
'width' => '1%',
+ 'align' => 'start',
),
),
),