Skip to content

Commit

Permalink
Block patterns REST endpoint: use snake case for field names (#40254)
Browse files Browse the repository at this point in the history
* Block patterns REST endpoint: use camel case for field names

* fix formatting

Co-authored-by: ntsekouras <ntsekouras@outlook.com>
  • Loading branch information
2 people authored and adamziel committed Apr 12, 2022
1 parent 581c02e commit 709914d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
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

0 comments on commit 709914d

Please sign in to comment.