PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / trunk
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder vtrunk
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
ablocks / includes / classes / block-renderer-attributes.php

block-renderer-attributes.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder trunk, at includes/classes/block-renderer-attributes.php

67 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ABlocks\Classes;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 /**
9 * Keeps aBlocks' server-side previews rendering when another plugin adds an
10 * attribute to every block in the editor alone.
11 *
12 * A plugin can register an extra attribute on every block type from JavaScript,
13 * through the `blocks.registerBlockType` filter, without registering it in PHP.
14 * The editor then sends that attribute with every ServerSideRender request, and
15 * WP_REST_Block_Renderer_Controller validates `attributes` against the
16 * PHP-registered schema with `additionalProperties => false`. One key the
17 * server has never heard of rejects the whole render with a 400
18 * `rest_invalid_param`, and every dynamic aBlocks block shows "Error loading
19 * block" for as long as that plugin is active.
20 *
21 * aBlocks cannot register attributes it does not know about, so before the
22 * request is validated it drops, from its own blocks' render requests, every
23 * attribute the server has not registered. The server would not use those keys
24 * anyway. Attributes a plugin does mirror on the server (see FlexItem) stay.
25 */
26 class BlockRendererAttributes {
27
28 const ROUTE = '/wp/v2/block-renderer/';
29
30 public static function init() {
31 // `rest_pre_dispatch` runs before WP_REST_Server validates the request's
32 // parameters, which is where the unknown key would fail it.
33 add_filter( 'rest_pre_dispatch', [ __CLASS__, 'drop_unregistered_attributes' ], 10, 3 );
34 }
35
36 /**
37 * @param mixed $result A response to short-circuit with; passed through untouched.
38 * @param \WP_REST_Server $server Server instance.
39 * @param \WP_REST_Request $request The request about to be dispatched.
40 * @return mixed
41 */
42 public static function drop_unregistered_attributes( $result, $server, $request ) {
43 $route = $request->get_route();
44
45 if ( 0 !== strpos( $route, self::ROUTE . 'ablocks/' ) ) {
46 return $result;
47 }
48
49 $attributes = $request->get_param( 'attributes' );
50 if ( ! is_array( $attributes ) ) {
51 return $result;
52 }
53
54 $block_type = \WP_Block_Type_Registry::get_instance()->get_registered( substr( $route, strlen( self::ROUTE ) ) );
55 if ( ! $block_type ) {
56 return $result;
57 }
58
59 $registered = array_intersect_key( $attributes, $block_type->get_attributes() );
60 if ( count( $registered ) !== count( $attributes ) ) {
61 $request->set_param( 'attributes', $registered );
62 }
63
64 return $result;
65 }
66 }
67