Skip to content

Commit

Permalink
[Patterns]: Featued patterns from pattern directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Sep 24, 2021
1 parent b346d7d commit 1bb837b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
57 changes: 55 additions & 2 deletions lib/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ function remove_core_patterns() {
*
* @since 5.8.0
*/
function load_remote_patterns() {
function load_remote_core_patterns() {
// This is the core function that provides the same feature.
if ( function_exists( '_load_remote_block_patterns' ) ) {
return;
Expand Down Expand Up @@ -236,6 +236,58 @@ function load_remote_patterns() {
}
}

/**
* Register featured patterns from wordpress.org/patterns.
*
* @since 5.9.0
*/
function load_remote_featured_patterns() {
/**
* Filter to disable remote block patterns.
*
* @since 5.8.0
*
* @param bool $should_load_remote
*/
$should_load_remote = apply_filters( 'should_load_remote_block_patterns', true );

if ( ! $should_load_remote ) {
return;
}

// TODO: check about the `featured` name.. Should this be reserved?
// Also check core logic: https://developer.wordpress.org/reference/classes/wp_block_pattern_categories_registry/
// Do we just allow collisions/overide with categories when they have the same name? Needs checking..
if ( ! WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( 'Featured' ) ) {
register_block_pattern_category( 'featured', array( 'label' => __( 'Featured', 'gutenberg' ) ) );
}

$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
// TODO: change this with new category id when created in patterns directory..
$request->set_param( 'category', 4 ); // temp gallery category id.

// TODO: check to update the controller here: https://github.com/WordPress/gutenberg/blob/trunk/lib/class-wp-rest-pattern-directory-controller.php#L321
// because it unsets the `per_page` param.
// $request->set_param( 'per_page', 2 );.

$response = rest_do_request( $request );
if ( $response->is_error() ) {
return;
}
$patterns = $response->get_data();
// check above comment about `per_page`.
$patterns = array_slice( $patterns, 0, 2 );
foreach ( $patterns as $pattern ) {
$pattern_name = sanitize_title( $pattern['title'] );
if ( ! WP_Block_Patterns_Registry::get_instance()->is_registered( $pattern_name ) ) {
// TODO: should we override this one as is done here? If not we should also add the `featured`
// category and also check to registere remaing categories if not already registered.
$pattern['categories'] = array( 'featured' );
register_block_pattern( $pattern_name, (array) $pattern );
};
}
}


add_action(
'init',
Expand All @@ -257,7 +309,8 @@ function( $current_screen ) {

$is_site_editor = ( function_exists( 'gutenberg_is_edit_site_page' ) && gutenberg_is_edit_site_page( $current_screen->id ) );
if ( $current_screen->is_block_editor || $is_site_editor ) {
load_remote_patterns();
load_remote_core_patterns();
load_remote_featured_patterns();
}
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ function BlockPatternsCategory( {
// Remove any empty categories
const populatedCategories = useMemo(
() =>
allCategories.filter( ( category ) =>
allPatterns.some( ( pattern ) =>
pattern.categories?.includes( category.name )
allCategories
.filter( ( category ) =>
allPatterns.some( ( pattern ) =>
pattern.categories?.includes( category.name )
)
)
),
// TODO: check if we can sort differently. Currently is the registration order.
.sort( ( { name: currentName }, { name: nextName } ) => {
if ( ! [ currentName, nextName ].includes( 'featured' ) ) {
return 0;
}
return currentName === 'featured' ? -1 : 1;
} ),
[ allPatterns, allCategories ]
);

Expand Down

0 comments on commit 1bb837b

Please sign in to comment.