Skip to content

Commit

Permalink
Merge pull request #1 from wpengine/fix/fetch-error-when-no-types
Browse files Browse the repository at this point in the history
Fix API fetch error when no "Category Types" exist
  • Loading branch information
ndiego committed Jan 12, 2022
2 parents d9557a3 + 75ca066 commit dbb7ce1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
31 changes: 15 additions & 16 deletions includes/class-wp-block-pattern-category-type-registry.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php
/**
* Blocks API: WP_Block_Pattern_Category_Type_Registry class
* WP_Block_Pattern_Category_Type_Registry class
*
* @package WordPress
* @subpackage Blocks
* @since 6.0.0
* @package Block Pattern Explorer
* @since 0.2.0
*/

/**
Expand All @@ -14,23 +13,23 @@ final class WP_Block_Pattern_Category_Type_Registry {
/**
* Registered block pattern category types array.
*
* @since 6.0.0
* @since 0.2.0
* @var array
*/
private $registered_category_types = array();

/**
* Container for the main instance of the class.
*
* @since 6.0.0
* @since 0.2.0
* @var WP_Block_Pattern_Category_Type_Registry|null
*/
private static $instance = null;

/**
* Registers a pattern category type.
*
* @since 6.0.0
* @since 0.2.0
*
* @param string $category_type_name Pattern category type name including namespace.
* @param array $category_type_properties Array containing the properties of the category type: label.
Expand All @@ -41,7 +40,7 @@ public function register( $category_type_name, $category_type_properties ) {
_doing_it_wrong(
__METHOD__,
__( 'Block pattern category type name must be a string.' ),
'6.0.0'
'0.2.0'
);
return false;
}
Expand All @@ -57,7 +56,7 @@ public function register( $category_type_name, $category_type_properties ) {
/**
* Unregisters a pattern category type.
*
* @since 6.0.0
* @since 0.2.0
*
* @param string $category_type_name Pattern category type name including namespace.
* @return bool True if the pattern category type was unregistered with success and false otherwise.
Expand All @@ -68,7 +67,7 @@ public function unregister( $category_type_name ) {
__METHOD__,
/* translators: %s: Block pattern categpry type name. */
sprintf( __( 'Block pattern category type "%s" not found.' ), $category_type_name ),
'6.0.0'
'0.2.0'
);
return false;
}
Expand All @@ -81,7 +80,7 @@ public function unregister( $category_type_name ) {
/**
* Retrieves an array containing the properties of a registered pattern category type.
*
* @since 6.0.0
* @since 0.2.0
*
* @param string $category_type_name Pattern category type name including namespace.
* @return array Registered pattern category type properties.
Expand All @@ -97,7 +96,7 @@ public function get_registered( $category_type_name ) {
/**
* Retrieves all registered pattern category types.
*
* @since 6.0.0
* @since 0.2.0
*
* @return array Array of arrays containing the registered pattern category types.
*/
Expand All @@ -108,7 +107,7 @@ public function get_all_registered() {
/**
* Checks if a pattern category type is registered.
*
* @since 6.0.0
* @since 0.2.0
*
* @param string $category_type_name Pattern category name including namespace.
* @return bool True if the pattern category type is registered, false otherwise.
Expand All @@ -122,7 +121,7 @@ public function is_registered( $category_type_name ) {
*
* The instance will be created if it does not exist yet.
*
* @since 6.0.0
* @since 0.2.0
*
* @return WP_Block_Pattern_Category_Type_Registry The main instance.
*/
Expand All @@ -138,7 +137,7 @@ public static function get_instance() {
/**
* Registers a new pattern category type.
*
* @since 6.0.0
* @since 0.2.0
*
* @param string $category_type_name Pattern category type name including namespace.
* @param array $category_type_properties Array containing the properties of the category type.
Expand All @@ -151,7 +150,7 @@ function register_block_pattern_category_type( $category_type_name, $category_ty
/**
* Unregisters a pattern category type.
*
* @since 6.0.0
* @since 0.2.0
*
* @param string $category_type_name Pattern category type name including namespace.
* @return bool True if the pattern category type was unregistered with success and false otherwise.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function get_pattern_category_types() {

$pattern_category_types = WP_Block_Pattern_Category_Type_Registry::get_instance()->get_all_registered();

if ( $pattern_category_types ) {
if ( is_array( $pattern_category_types ) ) {
// @TODO Possibly add a prepare_settings_for_response function here
// in the future.
return new WP_REST_Response( array( 'patternCategoryTypes' => $pattern_category_types ), 200 );
Expand Down

0 comments on commit dbb7ce1

Please sign in to comment.