class-integration.php
1.35 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
53
54
55
<?php
namespace WPML\PB\Gutenberg\ReusableBlocks;
use WPML\FP\Fns;
class Integration implements \WPML\PB\Gutenberg\Integration {
/** @var Translation $translation */
private $translation;
public function __construct( Translation $translation ) {
$this->translation = $translation;
}
public function add_hooks() {
add_filter( 'render_block_data', [ $this, 'convertReusableBlock' ] );
add_filter( 'render_block', Fns::withoutRecursion( Fns::identity(), [ $this, 'reRenderInnerReusableBlock' ] ), 10, 2 );
}
/**
* Converts the block in the current language
*
* @param array $block
*
* @return array
*/
public function convertReusableBlock( array $block ) {
return $this->translation->convertBlock( $block );
}
/**
* The filter hook `render_block_data` applies only for root blocks,
* nested blocks are not passing through this hook.
* That's why we need to re-render reusable nested blocks.
*
* @param string $blockContent
* @param array $block
*
* @return string
*/
public function reRenderInnerReusableBlock( $blockContent, $block ) {
$originalId = Blocks::getReusableId( $block );
if ( $originalId ) {
$convertedBlock = $this->translation->convertBlock( $block );
if ( Blocks::getReusableId( $convertedBlock ) !== $originalId ) {
return render_block( $convertedBlock );
}
}
return $blockContent;
}
}