Skip to content

Commit

Permalink
Add back-compat function for older Gutenberg and WP versions
Browse files Browse the repository at this point in the history
  • Loading branch information
noahtallen committed Apr 15, 2022
1 parent 292ae7c commit 835aa48
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function multiple_route_pattern_registration( $patterns_from_api_mock, $t
$request_mock = $this->createMock( \WP_REST_Request::class );
$request_mock->method( 'get_route' )->willReturn( $route );

load_block_patterns_from_api(
register_patterns_on_api_request(
function () use ( $patterns_from_api_mock ) {
$patterns_from_api_mock->register_patterns();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ function load_wpcom_block_editor_nux() {
* @param Function $register_patterns_func A function that when called will
* register the relevant block patterns in the registry.
*/
function load_block_patterns_from_api( $register_patterns_func ) {
function register_patterns_on_api_request( $register_patterns_func ) {
/**
* Load editing toolkit block patterns from the API.
*
Expand All @@ -267,7 +267,7 @@ function load_block_patterns_from_api( $register_patterns_func ) {
}
add_filter(
'rest_dispatch_request',
load_block_patterns_from_api(
register_patterns_on_api_request(
function () {
( new Block_Patterns_From_API() )->register_patterns();
}
Expand All @@ -276,6 +276,34 @@ function () {
2
);

/**
* This function exists for back-compatibility. Patterns in Gutenberg v13.0.0 and
* newer are loaded over the API instead of injected into the document. While we
* register patterns in the API above, we still need to maintain compatibility
* with older Gutenberg versions and WP 5.9 without Gutenberg.
*
* This can be removed once the rest API approach is in core WordPress and available
* on Atomic and Simple sites. (Note that Atomic sites may disable the Gutenberg
* plugin, so we cannot guarantee its availability.)
*
* @param object $current_screen An object describing the wp-admin screen properties.
*/
function register_patterns_on_screen_load( $current_screen ) {
// No need to use this when the REST API approach is available.
if ( class_exists( 'WP_REST_Block_Pattern_Categories_Controller' ) ) {
return;
}

// is_block_editor should include site editor support.
if ( ! apply_filters( 'a8c_enable_block_patterns_api', false ) || ! $current_screen->is_block_editor ) {
return;
}

require_once __DIR__ . '/block-patterns/class-block-patterns-from-api.php';
( new Block_Patterns_From_API() )->register_patterns();
}
add_action( 'current_screen', __NAMESPACE__ . '\register_patterns_on_screen_load' );

/**
* Load Block Inserter Modifications module.
*/
Expand Down

0 comments on commit 835aa48

Please sign in to comment.