Skip to content

Commit

Permalink
Merge branch 'trunk' into update/jetpack-ai-handle-contact-us-upgrade…
Browse files Browse the repository at this point in the history
…-on-simple-sites
  • Loading branch information
lhkowalski committed Nov 24, 2023
2 parents 0de8f82 + 42aec0a commit 6eb41bd
Show file tree
Hide file tree
Showing 37 changed files with 1,269 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Migrate Block Patterns
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 6eb41bd

Please sign in to comment.