Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
59.46% |
22 / 37 |
|
0.00% |
0 / 3 |
CRAP | n/a |
0 / 0 |
|
| wpcom_unregister_jetpack_patterns | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
3.00 | |||
| register_patterns_on_api_request | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
4.01 | |||
| wpcom_unregister_core_block_patterns | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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 | if ( ! $request_allowed || ! apply_filters( 'a8c_enable_block_patterns_api', false ) ) { |
| 63 | return $response; |
| 64 | } |
| 65 | |
| 66 | $register_patterns_func(); |
| 67 | |
| 68 | wpcom_unregister_jetpack_patterns(); |
| 69 | |
| 70 | return $response; |
| 71 | }; |
| 72 | } |
| 73 | add_filter( |
| 74 | 'rest_dispatch_request', |
| 75 | register_patterns_on_api_request( |
| 76 | function () { |
| 77 | require_once __DIR__ . '/class-wpcom-block-patterns-from-api.php'; |
| 78 | ( new Wpcom_Block_Patterns_From_Api() )->register_patterns(); |
| 79 | } |
| 80 | ), |
| 81 | 11, |
| 82 | 2 |
| 83 | ); |
| 84 | |
| 85 | /** |
| 86 | * Hide patterns bundled in core and from the Dotorg pattern directory. |
| 87 | * The reason is to show only the Dotcom pattern library. |
| 88 | */ |
| 89 | function wpcom_unregister_core_block_patterns() { |
| 90 | remove_theme_support( 'core-block-patterns' ); |
| 91 | } |
| 92 | |
| 93 | add_action( 'after_setup_theme', 'wpcom_unregister_core_block_patterns' ); |