class-wpml-widgets-support-frontend.php
2.32 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\FP\Str;
use WPML\LIB\WP\Hooks;
/**
* This code is inspired by WPML Widgets (https://wordpress.org/plugins/wpml-widgets/),
* created by Jeroen Sormani
*
* @author OnTheGo Systems
*/
class WPML_Widgets_Support_Frontend implements IWPML_Action {
/**
* @see \WPML\PB\Gutenberg\Widgets\Block\DisplayTranslation::PRIORITY_BEFORE_REMOVE_BLOCK_MARKUP
*/
const PRIORITY_AFTER_TRANSLATION_APPLIED = 0;
/** @var array $displayFor */
private $displayFor;
/**
* WPML_Widgets constructor.
*
* @param string $current_language
*/
public function __construct( $current_language ) {
$this->displayFor = [ null, $current_language, 'all' ];
}
public function add_hooks() {
add_filter( 'widget_block_content', [ $this, 'filterByLanguage' ], self::PRIORITY_AFTER_TRANSLATION_APPLIED );
add_filter( 'widget_display_callback', [ $this, 'display' ], - PHP_INT_MAX );
}
/**
* @param string $content
*
* @return string
*/
public function filterByLanguage( $content ) {
$render = function () use ( $content ) {
return wpml_collect( parse_blocks( $content ) )
->map( Fns::unary( 'render_block' ) )
->reduce( Str::concat(), '' );
};
return Hooks::callWithFilter( $render, 'pre_render_block', [ $this, 'shouldRender' ], 10, 2 );
}
/**
* Determine if a block should be rendered depending on its language
* Returning an empty string will stop the block from being rendered.
*
* @param string|null $pre_render The pre-rendered content. Default null.
* @param array $block The block being rendered.
*
* @return string|null
*/
public function shouldRender( $pre_render, $block ) {
return Lst::includes( Obj::path( [ 'attrs', 'wpml_language' ], $block ), $this->displayFor ) ? $pre_render : '';
}
/**
* Get display status of the widget.
*
* @param array|bool $instance
*
* @return array|bool
*/
public function display( $instance ) {
if (
! $instance ||
( is_array( $instance ) && $this->it_must_display( $instance ) )
) {
return $instance;
}
return false;
}
/**
* Returns display status of the widget as boolean.
*
* @param array $instance
*
* @return bool
*/
private function it_must_display( $instance ) {
return Lst::includes( Obj::propOr( null, 'wpml_language', $instance ), $this->displayFor );
}
}