Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,19 @@ public function filter_response_by_context( $data, $context ) {
unset( $data['title']['rendered'] );
unset( $data['content']['rendered'] );

// Add the core wp_pattern_sync_status meta as top level property to the response.
$data['wp_pattern_sync_status'] = $data['meta']['wp_pattern_sync_status'] ?? '';
unset( $data['meta']['wp_pattern_sync_status'] );
/*
* Add the core wp_pattern_sync_status meta as top level property to the response.
* Handle both array and object (stdClass) meta values.
*/
if ( is_array( $data['meta'] ) && isset( $data['meta']['wp_pattern_sync_status'] ) ) {
$data['wp_pattern_sync_status'] = $data['meta']['wp_pattern_sync_status'];
unset( $data['meta']['wp_pattern_sync_status'] );
} elseif ( is_object( $data['meta'] ) && property_exists( $data['meta'], 'wp_pattern_sync_status' ) ) {
$data['wp_pattern_sync_status'] = $data['meta']->wp_pattern_sync_status;
unset( $data['meta']->wp_pattern_sync_status );
} else {
$data['wp_pattern_sync_status'] = '';
}
return $data;
}

Expand Down
12 changes: 11 additions & 1 deletion src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public function register_field() {
*
* @param int $object_id Object ID to fetch meta for.
* @param WP_REST_Request $request Full details about the request.
* @return array Array containing the meta values keyed by name.
* @return array|stdClass Array containing the meta values keyed by name,
* or stdClass if empty to ensure JSON object encoding.
*/
public function get_value( $object_id, $request ) {
$fields = $this->get_registered_fields();
Expand Down Expand Up @@ -105,6 +106,11 @@ public function get_value( $object_id, $request ) {
$response[ $name ] = $value;
}

// Use stdClass so that JSON result is {} and not [].
if ( empty( $response ) ) {
return new stdClass();
}

return $response;
}

Expand Down Expand Up @@ -582,6 +588,10 @@ public static function prepare_value( $value, $request, $args ) {
* @return array|false The meta array, if valid, false otherwise.
*/
public function check_meta_is_array( $value, $request, $param ) {
if ( is_object( $value ) ) {
$value = (array) $value;
}

if ( ! is_array( $value ) ) {
return false;
}
Expand Down
Loading