Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
71.67% |
43 / 60 |
|
0.00% |
0 / 4 |
CRAP | n/a |
0 / 0 |
|
| wpcom_unregister_jetpack_patterns | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
3.00 | |||
| register_patterns_on_api_request | |
93.75% |
15 / 16 |
|
0.00% |
0 / 1 |
7.01 | |||
| wpcom_unregister_core_block_patterns | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| wpcom_register_core_query_block_patterns | |
94.12% |
16 / 17 |
|
0.00% |
0 / 1 |
6.01 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Load block patterns from the API. |
| 4 | * |
| 5 | * @package automattic/jetpack-mu-wpcom |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Hide Jetpack form patterns. |
| 10 | * The reason is to show only the Dotcom pattern library. |
| 11 | */ |
| 12 | function wpcom_unregister_jetpack_patterns() { |
| 13 | // @TODO: It would be better to add 'source: jetpack' when registering them in https://github.com/Automattic/jetpack/blob/3c4c16b8e9b34c8b6e798839ddf029a5c9a59e77/projects/plugins/jetpack/modules/contact-form.php#L152 |
| 14 | // and then here just filter them by source rather than by name. |
| 15 | $pattern_names = array( |
| 16 | 'contact-form', |
| 17 | 'newsletter-form', |
| 18 | 'rsvp-form', |
| 19 | 'registration-form', |
| 20 | 'appointment-form', |
| 21 | 'feedback-form', |
| 22 | 'salesforce-lead-form', |
| 23 | ); |
| 24 | foreach ( $pattern_names as $pattern_name ) { |
| 25 | $pattern = \WP_Block_Patterns_Registry::get_instance()->get_registered( $pattern_name ); |
| 26 | if ( $pattern ) { |
| 27 | unregister_block_pattern( $pattern_name ); |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Return a function that loads and register block patterns from the API. This |
| 34 | * function can be registered to the `rest_dispatch_request` filter. |
| 35 | * |
| 36 | * @param callable $register_patterns_func A function that when called will |
| 37 | * register the relevant block patterns in the registry. |
| 38 | */ |
| 39 | function register_patterns_on_api_request( $register_patterns_func ) { |
| 40 | /** |
| 41 | * Load editing toolkit block patterns from the API. |
| 42 | * |
| 43 | * It will only register the patterns for certain allowed requests and |
| 44 | * return early otherwise. |
| 45 | * |
| 46 | * @param mixed $response |
| 47 | * @param WP_REST_Request $request |
| 48 | */ |
| 49 | return function ( $response, $request ) use ( $register_patterns_func ) { |
| 50 | /** |
| 51 | * Do nothing if it is loaded in the ETK. |
| 52 | */ |
| 53 | if ( class_exists( 'A8C\FSE\Block_Patterns_From_API' ) ) { |
| 54 | return $response; |
| 55 | } |
| 56 | |
| 57 | $route = $request->get_route(); |
| 58 | // Matches either /wp/v2/sites/123/block-patterns/patterns or /wp/v2/block-patterns/patterns |
| 59 | // to handle the API format of both WordPress.com and WordPress core. |
| 60 | $request_allowed = preg_match( '/^\/wp\/v2\/(sites\/[0-9]+\/)?block\-patterns\/(patterns|categories)$/', $route ); |
| 61 | |
| 62 | // The Site Editor patterns sidebar gets its category list from view-config, |
| 63 | // so register API-backed pattern categories for that request too. |
| 64 | $request_allowed = $request_allowed || ( |
| 65 | preg_match( '/^\/wp\/v2\/(sites\/[0-9]+\/)?view\-config$/', $route ) && |
| 66 | 'postType' === $request->get_param( 'kind' ) && |
| 67 | 'wp_block' === $request->get_param( 'name' ) |
| 68 | ); |
| 69 | |
| 70 | if ( ! $request_allowed || ! apply_filters( 'a8c_enable_block_patterns_api', false ) ) { |
| 71 | return $response; |
| 72 | } |
| 73 | |
| 74 | $register_patterns_func(); |
| 75 | |
| 76 | wpcom_unregister_jetpack_patterns(); |
| 77 | |
| 78 | return $response; |
| 79 | }; |
| 80 | } |
| 81 | add_filter( |
| 82 | 'rest_dispatch_request', |
| 83 | register_patterns_on_api_request( |
| 84 | function () { |
| 85 | require_once __DIR__ . '/class-wpcom-block-patterns-from-api.php'; |
| 86 | ( new Wpcom_Block_Patterns_From_Api() )->register_patterns(); |
| 87 | } |
| 88 | ), |
| 89 | 11, |
| 90 | 2 |
| 91 | ); |
| 92 | |
| 93 | /** |
| 94 | * Hide patterns bundled in core and from the Dotorg pattern directory. |
| 95 | * The reason is to show only the Dotcom pattern library. |
| 96 | */ |
| 97 | function wpcom_unregister_core_block_patterns() { |
| 98 | remove_theme_support( 'core-block-patterns' ); |
| 99 | } |
| 100 | |
| 101 | add_action( 'after_setup_theme', 'wpcom_unregister_core_block_patterns' ); |
| 102 | |
| 103 | /** |
| 104 | * Re-register a subset of the Query Loop patterns bundled in core, which are |
| 105 | * excluded by wpcom_unregister_core_block_patterns() along with all other core |
| 106 | * patterns. |
| 107 | * |
| 108 | * Only the bundled patterns with a `core/query` root block are restored. They |
| 109 | * feed the Query Loop block's pattern picker, and also appear in the inserter |
| 110 | * under the "Posts" category. Core's group-wrapped query patterns (Offset, |
| 111 | * Large title) are deliberately left out. The list is hardcoded like core's |
| 112 | * own rather than discovered from disk, because core retires patterns by |
| 113 | * delisting them while keeping the file around (e.g. the deprecated |
| 114 | * social-links-shared-background-color pattern). |
| 115 | */ |
| 116 | function wpcom_register_core_query_block_patterns() { |
| 117 | $query_patterns = array( |
| 118 | 'query-standard-posts', |
| 119 | 'query-medium-posts', |
| 120 | 'query-small-posts', |
| 121 | 'query-grid-posts', |
| 122 | ); |
| 123 | $registry = \WP_Block_Patterns_Registry::get_instance(); |
| 124 | foreach ( $query_patterns as $slug ) { |
| 125 | $name = 'core/' . $slug; |
| 126 | $file = ABSPATH . WPINC . '/block-patterns/' . $slug . '.php'; |
| 127 | if ( $registry->is_registered( $name ) || ! file_exists( $file ) ) { |
| 128 | continue; |
| 129 | } |
| 130 | $pattern = require $file; |
| 131 | // Cheap safety net for the `core/query` root block invariant: a |
| 132 | // group-wrapped pattern would leak into the inserter, since blockTypes |
| 133 | // doesn't limit visibility there (the Query Loop picker filters |
| 134 | // non-query-root patterns client-side on its own). |
| 135 | if ( ! is_array( $pattern ) || ! str_starts_with( trim( $pattern['content'] ?? '' ), '<!-- wp:query ' ) ) { |
| 136 | continue; |
| 137 | } |
| 138 | $pattern['source'] = 'core'; |
| 139 | register_block_pattern( $name, $pattern ); |
| 140 | } |
| 141 | } |
| 142 | add_action( 'init', 'wpcom_register_core_query_block_patterns', 11 ); |