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

Block patterns REST endpoint: use snake case for field names #40254

Merged
merged 2 commits into from
Apr 12, 2022
Merged
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 @@ -113,19 +113,19 @@ public function get_items( $request ) { // phpcs:ignore VariableAnalysis.CodeAna
public function prepare_item_for_response( $item, $request ) {
$fields = $this->get_fields_for_response( $request );
$keys = array(
'name',
'title',
'description',
'viewportWidth',
'blockTypes',
'categories',
'keywords',
'content',
'name' => 'name',
'title' => 'title',
'description' => 'description',
'viewportWidth' => 'viewport_width',
'blockTypes' => 'block_types',
'categories' => 'categories',
'keywords' => 'keywords',
'content' => 'content',
);
$data = array();
foreach ( $keys as $key ) {
if ( isset( $item[ $key ] ) && rest_is_field_included( $key, $fields ) ) {
$data[ $key ] = $item[ $key ];
foreach ( $keys as $item_key => $rest_key ) {
if ( isset( $item[ $item_key ] ) && rest_is_field_included( $rest_key, $fields ) ) {
$data[ $rest_key ] = $item[ $item_key ];
}
}

Expand All @@ -148,49 +148,49 @@ public function get_item_schema() {
'title' => 'block-pattern',
'type' => 'object',
'properties' => array(
'name' => array(
'name' => array(
'description' => __( 'The pattern name.', 'gutenberg' ),
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'embed' ),
),
'title' => array(
'title' => array(
'description' => __( 'The pattern title, in human readable format.', 'gutenberg' ),
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'embed' ),
),
'description' => array(
'description' => array(
'description' => __( 'The pattern detailed description.', 'gutenberg' ),
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'embed' ),
),
'viewportWidth' => array(
'viewport_width' => array(
'description' => __( 'The pattern viewport width for inserter preview.', 'gutenberg' ),
'type' => 'number',
'readonly' => true,
'context' => array( 'view', 'embed' ),
),
'blockTypes' => array(
'block_types' => array(
'description' => __( 'Block types that the pattern is intended to be used with.', 'gutenberg' ),
'type' => 'array',
'readonly' => true,
'context' => array( 'view', 'embed' ),
),
'categories' => array(
'categories' => array(
'description' => __( 'The pattern category slugs.', 'gutenberg' ),
'type' => 'array',
'readonly' => true,
'context' => array( 'view', 'embed' ),
),
'keywords' => array(
'keywords' => array(
'description' => __( 'The pattern keywords.', 'gutenberg' ),
'type' => 'array',
'readonly' => true,
'context' => array( 'view', 'embed' ),
),
'content' => array(
'content' => array(
'description' => __( 'The pattern content.', 'gutenberg' ),
'type' => 'string',
'readonly' => true,
Expand Down
16 changes: 14 additions & 2 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { find, includes, get, compact, uniq } from 'lodash';
import { find, includes, get, compact, uniq, map, mapKeys } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -455,9 +455,21 @@ export const __experimentalGetCurrentThemeGlobalStylesVariations = () => async (
};

export const getBlockPatterns = () => async ( { dispatch } ) => {
const patterns = await apiFetch( {
const restPatterns = await apiFetch( {
path: '/wp/v2/block-patterns/patterns',
} );
const patterns = map( restPatterns, ( pattern ) =>
mapKeys( pattern, ( value, key ) => {
switch ( key ) {
case 'block_types':
return 'blockTypes';
case 'viewport_width':
return 'viewportWidth';
default:
return key;
}
} )
);
dispatch( { type: 'RECEIVE_BLOCK_PATTERNS', patterns } );
};

Expand Down