diff --git a/lib/block-patterns.php b/lib/block-patterns.php index f3586d3d7dcd9b..491c6ccb2564c9 100644 --- a/lib/block-patterns.php +++ b/lib/block-patterns.php @@ -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; @@ -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', @@ -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(); } } ); diff --git a/packages/block-editor/src/components/inserter/block-patterns-tab.js b/packages/block-editor/src/components/inserter/block-patterns-tab.js index 389aec536c6165..83715112625e4e 100644 --- a/packages/block-editor/src/components/inserter/block-patterns-tab.js +++ b/packages/block-editor/src/components/inserter/block-patterns-tab.js @@ -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 ] );