Skip to content

Commit

Permalink
Migrate Block Patterns Registration to jetpack-mu-wpcom (#34162)
Browse files Browse the repository at this point in the history
Co-authored-by: Miguel <miguel.san.segundo@automattic.com>

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/6977731794
  • Loading branch information
okmttdhr authored and matticbot committed Nov 24, 2023
1 parent 2a65381 commit a7dbf17
Show file tree
Hide file tree
Showing 11 changed files with 586 additions and 10 deletions.
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/automattic/jetpack-mu-wpcom/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This is an alpha version! The changes listed here are not final.

### Added
- Add dynamic titles to task lists
- Migrate Block Patterns

### Changed
- Code Modernization: Replace usage of strpos() with str_contains()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public static function load_features() {
require_once __DIR__ . '/features/100-year-plan/locked-mode.php';

require_once __DIR__ . '/features/media/heif-support.php';

require_once __DIR__ . '/features/block-patterns/block-patterns.php';
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Block Patterns

## Adding a new pattern

Please add any new block pattern to the `patterns` folder within this module.
Every file in that folder gets automatically included registered as a block pattern.

There are two ways to add a new pattern, depending on whether it contains text or images that need to be localized:

1. A json file for static block patterns.
1. A php file for dynamic block patterns that need localization.

Please refer the Image and Description pattern as an example for a dynamic pattern that needs translation.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Load block patterns from the API.
*
* @package automattic/jetpack-mu-wpcom
*/

/**
* Re-register some core patterns to push them down in the inserter list.
* The reason is that Dotcom curate the pattern list based on their look.
*/
function wpcom_reorder_curated_core_patterns() {
$pattern_names = array( 'core/social-links-shared-background-color' );
foreach ( $pattern_names as $pattern_name ) {
$pattern = \WP_Block_Patterns_Registry::get_instance()->get_registered( $pattern_name );
if ( $pattern ) {
unregister_block_pattern( $pattern_name );
register_block_pattern(
$pattern_name,
$pattern
);
}
}
}

/**
* Return a function that loads and register block patterns from the API. This
* function can be registered to the `rest_dispatch_request` filter.
*
* @param Function $register_patterns_func A function that when called will
* register the relevant block patterns in the registry.
*/
function register_patterns_on_api_request( $register_patterns_func ) {
/**
* Load editing toolkit block patterns from the API.
*
* It will only register the patterns for certain allowed requests and
* return early otherwise.
*
* @param mixed $response
* @param WP_REST_Request $request
*/
return function ( $response, $request ) use ( $register_patterns_func ) {
/**
* Do nothing if it is loaded in the ETK.
*/
if ( class_exists( 'A8C\FSE\Block_Patterns_From_API' ) ) {
return $response;
}

$route = $request->get_route();
// Matches either /wp/v2/sites/123/block-patterns/patterns or /wp/v2/block-patterns/patterns
// to handle the API format of both WordPress.com and WordPress core.
$request_allowed = preg_match( '/^\/wp\/v2\/(sites\/[0-9]+\/)?block\-patterns\/(patterns|categories)$/', $route );

if ( ! $request_allowed || ! apply_filters( 'a8c_enable_block_patterns_api', false ) ) {
return $response;
}

$register_patterns_func();

wpcom_reorder_curated_core_patterns();

return $response;
};
}
add_filter(
'rest_dispatch_request',
register_patterns_on_api_request(
function () {
require_once __DIR__ . '/class-wpcom-block-patterns-from-api.php';
( new Wpcom_Block_Patterns_From_Api() )->register_patterns();
}
),
11,
2
);
Loading

0 comments on commit a7dbf17

Please sign in to comment.