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

Add block pattern categories' descriptions to the REST API and update the descriptions #45244

Merged
merged 3 commits into from
Oct 28, 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
8 changes: 0 additions & 8 deletions lib/compat/wordpress-6.0/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ function gutenberg_register_edit_site_export_endpoint() {
}
add_action( 'rest_api_init', 'gutenberg_register_edit_site_export_endpoint' );

/**
* Registers the block pattern categories REST API routes.
*/
function gutenberg_register_rest_block_pattern_categories() {
$block_patterns = new WP_REST_Block_Pattern_Categories_Controller();
$block_patterns->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_rest_block_pattern_categories' );

/**
* Register a core site settings.
Expand Down
62 changes: 62 additions & 0 deletions lib/compat/wordpress-6.2/block-patterns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Overrides Core's wp-includes/block-patterns.php to add category descriptions for WP 6.2.
*
* @package gutenberg
*/

/**
* Registers the block pattern categories REST API routes.
*/
function gutenberg_register_core_block_patterns_and_categories() {
register_block_pattern_category(
'buttons',
array(
'label' => _x( 'Buttons', 'Block pattern category', 'gutenberg' ),
)
);
register_block_pattern_category(
'columns',
array(
'label' => _x( 'Columns', 'Block pattern category', 'gutenberg' ),
)
);
register_block_pattern_category(
'footer',
array(
'label' => _x( 'Footers', 'Block pattern category', 'gutenberg' ),
)
);
register_block_pattern_category(
'gallery',
array(
'label' => _x( 'Gallery', 'Block pattern category', 'gutenberg' ),
)
);
register_block_pattern_category(
'header',
array(
'label' => _x( 'Headers', 'Block pattern category', 'gutenberg' ),
)
);
register_block_pattern_category(
'text',
array(
'label' => _x( 'Text', 'Block pattern category', 'gutenberg' ),
)
);
register_block_pattern_category(
'query',
array(
'label' => _x( 'Posts', 'Block pattern category', 'gutenberg' ),
'description' => __( 'Display post summaries in lists, grids, and other layouts.', 'gutenberg' ),
)
);
register_block_pattern_category(
'featured',
array(
'label' => _x( 'Featured', 'Block pattern category', 'gutenberg' ),
)
);
}
add_action( 'init', 'gutenberg_register_core_block_patterns_and_categories' );
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* REST API: Gutenberg_REST_Block_Pattern_Categories_Controller class
*
* Adds descriptions to the block pattern categories.
*
* @package WordPress
* @subpackage REST_API
*/

/**
* Core class used to access block pattern categories via the REST API.
*
* @see WP_REST_Controller
*/
class Gutenberg_REST_Block_Pattern_Categories_Controller extends WP_REST_Block_Pattern_Categories_Controller {
/**
* Prepare a raw block pattern category before it gets output in a REST API response.
*
* @since 6.0.0
*
* @param object $item Raw category as registered, before any changes.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
$fields = $this->get_fields_for_response( $request );
$keys = array( 'name', 'label', 'description' );
$data = array();
foreach ( $keys as $key ) {
if ( rest_is_field_included( $key, $fields ) ) {
$data[ $key ] = $item[ $key ];
}
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
return rest_ensure_response( $data );
}

/**
* Retrieves the block pattern category schema, conforming to JSON Schema.
*
* @since 6.0.0
*
* @return array Item schema data.
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'block-pattern-category',
'type' => 'object',
'properties' => array(
'name' => array(
'description' => __( 'The category name.', 'gutenberg' ),
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
'label' => array(
'description' => __( 'The category label, in human readable format.', 'gutenberg' ),
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
'description' => array(
'description' => __( 'The category description, in human readable format.', 'gutenberg' ),
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
),
);

return $this->add_additional_fields_schema( $schema );
}
}
15 changes: 15 additions & 0 deletions lib/compat/wordpress-6.2/rest-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Overrides Core's wp-includes/rest-api.php and registers the new endpoint for WP 6.2.
*
* @package gutenberg
*/

/**
* Registers the block pattern categories REST API routes.
*/
function gutenberg_register_rest_block_pattern_categories() {
$block_patterns = new Gutenberg_REST_Block_Pattern_Categories_Controller();
$block_patterns->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_rest_block_pattern_categories' );
5 changes: 5 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ function gutenberg_is_experiment_enabled( $name ) {
require_once __DIR__ . '/compat/wordpress-6.1/class-gutenberg-rest-templates-controller.php';
require_once __DIR__ . '/compat/wordpress-6.1/rest-api.php';

// WordPress 6.2 compat.
require_once __DIR__ . '/compat/wordpress-6.2/class-gutenberg-rest-block-pattern-categories-controller.php';
require_once __DIR__ . '/compat/wordpress-6.2/rest-api.php';
require_once __DIR__ . '/compat/wordpress-6.2/block-patterns.php';

// Experimental.
if ( ! class_exists( 'WP_Rest_Customizer_Nonces' ) ) {
require_once __DIR__ . '/experimental/class-wp-rest-customizer-nonces.php';
Expand Down