PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.1.5
JetFormBuilder — Dynamic Blocks Form Builder v3.1.5
3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / modules / block-sanitizer / module.php
jetformbuilder / modules / block-sanitizer Last commit date
interfaces 2 years ago sanitizers 2 years ago traits 2 years ago module.php 2 years ago
module.php
194 lines
1 <?php
2
3
4 namespace JFB_Modules\Block_Sanitizer;
5
6 use JFB_Components\Module\Base_Module_After_Install_It;
7 use JFB_Components\Module\Base_Module_It;
8 use JFB_Modules\Block_Sanitizer\Interfaces\Block_Context_Sanitizer_Interface;
9 use JFB_Modules\Block_Sanitizer\Interfaces\Block_Data_Sanitizer_Interface;
10 use JFB_Modules\Block_Sanitizer\Interfaces\Is_Supported_Sanitizer_Interface;
11
12 // If this file is called directly, abort.
13 if ( ! defined( 'WPINC' ) ) {
14 die;
15 }
16
17 /**
18 * The creation of this class is determined by the block rendering logic.
19 * Because it happens from the depths to the surface.
20 *
21 * The task was to throw the prepared value in the attribute into the child blocks.
22 * But the `render_callback` of the main block is run after the children,
23 * so changing the attributes for further transmission to the context makes no sense.
24 *
25 * The solution is to change the attributes on the `render_block_data' hook,
26 * because it is run in the correct order of block rendering - from top to bottom.
27 *
28 * @since 3.1.0
29 *
30 * Class Module
31 * @package Jet_Form_Builder\Blocks\Block_Sanitizer
32 */
33 final class Module implements Base_Module_It, Base_Module_After_Install_It {
34
35 /**
36 * @var Is_Supported_Sanitizer_Interface[]
37 */
38 private $sanitizers = array();
39
40 public function condition(): bool {
41 return false;
42 }
43
44 public function rep_item_id() {
45 return 'block-sanitizer';
46 }
47
48 public function init_hooks() {
49 /**
50 * Modify the blocks that inside the form
51 */
52 add_filter(
53 'jet-form-builder/before-start-form',
54 array( $this, 'init_form_blocks_hooks' )
55 );
56 add_filter(
57 'jet-form-builder/after-end-form',
58 array( $this, 'remove_form_blocks_hooks' )
59 );
60 }
61
62 public function remove_hooks() {
63 remove_filter(
64 'jet-form-builder/before-start-form',
65 array( $this, 'init_form_blocks_hooks' )
66 );
67 remove_filter(
68 'jet-form-builder/after-end-form',
69 array( $this, 'remove_form_blocks_hooks' )
70 );
71 }
72
73 public function init_form_blocks_hooks( $markup ) {
74 add_filter(
75 'render_block_data',
76 array( $this, 'blocks_data_handler' ),
77 10,
78 3
79 );
80 add_filter(
81 'render_block_context',
82 array( $this, 'blocks_context_handler' ),
83 10,
84 3
85 );
86
87 return $markup;
88 }
89
90 public function remove_form_blocks_hooks( $markup ) {
91 remove_filter(
92 'render_block_data',
93 array( $this, 'blocks_data_handler' )
94 );
95 remove_filter(
96 'render_block_context',
97 array( $this, 'blocks_context_handler' )
98 );
99
100 return $markup;
101 }
102
103 public function on_install() {
104 $this->register( new Sanitizers\Repeater_Sanitizer() );
105 }
106
107 public function on_uninstall() {
108 $this->unregister( new Sanitizers\Repeater_Sanitizer() );
109 }
110
111 public function blocks_data_handler( array $block_data, array $initial_block, $parent_block ): array {
112 if ( ! is_null( $parent_block ) ) {
113 return $block_data;
114 }
115
116 return $this->apply_blocks_data( $block_data, $initial_block, $parent_block );
117 }
118
119 public function blocks_context_handler( array $block_context, array $initial_block, $parent_block ): array {
120 if ( ! is_null( $parent_block ) ) {
121 return $block_context;
122 }
123
124 return $this->apply_blocks_context( $block_context, $initial_block, $parent_block );
125 }
126
127 /**
128 * @param array $block_data
129 * @param array $initial_block
130 * @param null|\WP_Block $parent_block
131 *
132 * @return array
133 * @noinspection PhpMissingParamTypeInspection
134 */
135 public function apply_blocks_data( array $block_data, array $initial_block, $parent_block ): array {
136 foreach ( $this->iterate_sanitizers( $block_data ) as $item ) {
137 if ( ! ( $item instanceof Block_Data_Sanitizer_Interface ) ) {
138 continue;
139 }
140 $block_data = $item->apply_block_data( $block_data, $initial_block, $parent_block );
141 }
142
143 return $block_data;
144 }
145
146 /**
147 * @param array $block_context
148 * @param array $initial_block
149 * @param null|\WP_Block $parent_block
150 *
151 * @return array
152 * @noinspection PhpMissingParamTypeInspection
153 */
154 public function apply_blocks_context( array $block_context, array $initial_block, $parent_block ): array {
155 foreach ( $this->iterate_sanitizers( $initial_block ) as $item ) {
156 if ( ! ( $item instanceof Block_Context_Sanitizer_Interface ) ) {
157 continue;
158 }
159 $block_context = $item->apply_block_context( $block_context, $initial_block, $parent_block );
160 }
161
162 return $block_context;
163 }
164
165 public function register( Is_Supported_Sanitizer_Interface $item ): Module {
166 $this->sanitizers[] = $item;
167
168 return $this;
169 }
170
171 public function unregister( Is_Supported_Sanitizer_Interface $item ): Module {
172 foreach ( $this->sanitizers as $index => $sanitizer ) {
173 if ( get_class( $sanitizer ) !== get_class( $item ) ) {
174 continue;
175 }
176 unset( $this->sanitizers[ $index ] );
177 }
178
179 $this->sanitizers = array_values( $this->sanitizers );
180
181 return $this;
182 }
183
184 protected function iterate_sanitizers( array $parsed_block ): \Generator {
185 foreach ( $this->sanitizers as $item ) {
186 if ( ! $item->is_supported( $parsed_block ) ) {
187 continue;
188 }
189
190 yield $item;
191 }
192 }
193 }
194