| 1 |
<?php |
| 2 |
namespace ABlocks\Classes; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Server-side half of `src/blocks/atomic-shared/flex-item-filter.js`. |
| 10 |
* |
| 11 |
* That filter registers one extra attribute — `ablocksFlexItem` — on every |
| 12 |
* non-atomic block, so anything dropped into an atomic Flex or Grid container |
| 13 |
* gets a "Flex item" panel. Registered in JS alone it leaves the two block |
| 14 |
* registries disagreeing about what a block's attributes are, and the editor |
| 15 |
* asks the server to render with the JS set: WP_REST_Block_Renderer_Controller |
| 16 |
* validates `attributes` against the PHP-registered schema with |
| 17 |
* `additionalProperties => false`, so one attribute the server has never heard |
| 18 |
* of fails validation and the render returns 400 `rest_invalid_param`. That |
| 19 |
* takes down every block rendered through ServerSideRender — aBlocks' own |
| 20 |
* dynamic blocks and third-party ones alike. |
| 21 |
* |
| 22 |
* Declaring it here rather than in BlockGlobal is deliberate: BlockGlobal feeds |
| 23 |
* the attributes of aBlocks' own blocks (through each block's attributes.php and |
| 24 |
* the block.json built from it), while the editor-side filter reaches every |
| 25 |
* block on the site. `register_block_type_args` is the one seam with the same |
| 26 |
* reach, and it needs no rebuild to take effect on blocks aBlocks does not own. |
| 27 |
*/ |
| 28 |
class FlexItem { |
| 29 |
|
| 30 |
const ATTRIBUTE = 'ablocksFlexItem'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Atomic blocks keep their flex-item properties in the styles model and |
| 34 |
* render them through their own scoped CSS, so the editor filter skips |
| 35 |
* them. Keep this list in step with SKIP in flex-item-filter.js. |
| 36 |
*/ |
| 37 |
const SKIP = [ |
| 38 |
'ablocks/atomic-div', |
| 39 |
'ablocks/atomic-flex', |
| 40 |
'ablocks/atomic-grid', |
| 41 |
'ablocks/atomic-image', |
| 42 |
'ablocks/atomic-svg', |
| 43 |
'ablocks/atomic-text', |
| 44 |
]; |
| 45 |
|
| 46 |
public static function init() { |
| 47 |
// Registered from `plugins_loaded`, before anything registers a block |
| 48 |
// type on `init` — core's own blocks included. |
| 49 |
add_filter( 'register_block_type_args', [ __CLASS__, 'add_attribute' ], 10, 2 ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* @param array $args Block type registration arguments. |
| 54 |
* @param string $block_name Block name, e.g. `ablocks/dynamic-text`. |
| 55 |
* @return array |
| 56 |
*/ |
| 57 |
public static function add_attribute( $args, $block_name ) { |
| 58 |
if ( in_array( $block_name, self::SKIP, true ) ) { |
| 59 |
return $args; |
| 60 |
} |
| 61 |
|
| 62 |
if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) { |
| 63 |
$args['attributes'] = []; |
| 64 |
} |
| 65 |
|
| 66 |
// A block that declares it itself keeps its own definition. |
| 67 |
if ( ! isset( $args['attributes'][ self::ATTRIBUTE ] ) ) { |
| 68 |
$args['attributes'][ self::ATTRIBUTE ] = [ |
| 69 |
'type' => 'object', |
| 70 |
'default' => [], |
| 71 |
]; |
| 72 |
} |
| 73 |
|
| 74 |
return $args; |
| 75 |
} |
| 76 |
} |
| 77 |
|