class-blocks.php
1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace WPML\PB\Gutenberg\ReusableBlocks;
use WPML\FP\Obj;
class Blocks {
/**
* @param array $block
*
* @return bool
*/
public static function isReusable( array $block ) {
return 'core/block' === $block['blockName']
&& isset( $block['attrs']['ref'] )
&& is_numeric( $block['attrs']['ref'] );
}
/**
* @param array $block
*
* @return int
*/
public static function getReusableId( array $block ) {
return (int) Obj::path( [ 'attrs', 'ref' ], $block );
}
/**
* We get block IDs recursively to find possible
* nested reusable blocks.
*
* @param int $post_id
*
* @return array
*/
public function getChildrenIdsFromPost( $post_id ) {
$post = get_post( $post_id );
if ( $post ) {
$blocks = \wpml_collect( \WPML_Gutenberg_Integration::parse_blocks( $post->post_content ) );
return $blocks
->filter( [ self::class, 'isReusable' ] )
->map( function( $block ) {
$block_id = self::getReusableId( $block );
return array_merge( [ $block_id ], $this->getChildrenIdsFromPost( $block_id ) );
})->flatten()->toArray();
}
return [];
}
}