-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate Block Patterns Registration to jetpack-mu-wpcom (#34162)
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
Showing
11 changed files
with
586 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
vendor/automattic/jetpack-mu-wpcom/src/features/block-patterns/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
77 changes: 77 additions & 0 deletions
77
vendor/automattic/jetpack-mu-wpcom/src/features/block-patterns/block-patterns.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
Oops, something went wrong.