PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.0.8
JetFormBuilder — Dynamic Blocks Form Builder v3.0.8
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 / includes / blocks / block-helper.php
jetformbuilder / includes / blocks Last commit date
advanced-rules 3 years ago button-types 3 years ago conditional-block 3 years ago exceptions 3 years ago modules 3 years ago render 3 years ago ssr-validation 3 years ago types 3 years ago validation-messages 3 years ago action-buttons-manager.php 3 years ago block-helper.php 3 years ago blocks-repository-base.php 3 years ago default-blocks-repository.php 3 years ago dynamic-value.php 3 years ago form-builder-blocks-repository.php 3 years ago manager.php 3 years ago switch-page-on-change.php 3 years ago validation.php 3 years ago
block-helper.php
188 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Blocks;
5
6 use Jet_Form_Builder\Blocks\Types\Base;
7 use Jet_Form_Builder\Form_Manager;
8
9 /**
10 * Class Helper
11 *
12 * @package Jet_Form_Builder\Blocks
13 */
14 class Block_Helper {
15
16 /**
17 * @param $value
18 * @param $blocks
19 *
20 * @return array
21 */
22 public static function find_block_by_name( $value, $blocks ): array {
23 return self::find_block_by_attr( $value, 'name', $blocks );
24 }
25
26 /**
27 * @param $value
28 * @param $attr_name
29 * @param $blocks
30 *
31 * @return array
32 */
33 public static function find_block_by_attr( $value, $attr_name, $blocks ): array {
34 return self::find_block(
35 function ( $block ) use ( $value, $attr_name ) {
36 return ( ( $block['attrs'][ $attr_name ] ?? false ) === $value );
37 },
38 $blocks
39 );
40 }
41
42 public static function get_form_field_names( $blocks ): array {
43 $names = array();
44
45 self::find_block(
46 function ( $block ) use ( &$names ) {
47 if ( ! empty( $block['attrs']['name'] ) ) {
48 $names[ $block['attrs']['name'] ] = 1;
49 }
50
51 return false;
52 },
53 $blocks
54 );
55
56 return $names;
57 }
58
59 public static function find_block( $callable, $blocks ): array {
60 if ( ! is_callable( $callable ) ) {
61 return array();
62 }
63 foreach ( $blocks as $block ) {
64 if ( ! isset( $block['blockName'] ) || ! isset( $block['attrs'] ) ) {
65 continue;
66 }
67 if ( call_user_func( $callable, $block ) ) {
68 return $block;
69 }
70
71 if ( 0 < count( $block['innerBlocks'] ) ) {
72 $find = self::find_block( $callable, $block['innerBlocks'] );
73
74 if ( $find ) {
75 return $find;
76 }
77 }
78 }
79
80 return array();
81 }
82
83 /**
84 * @param $blocks
85 * @param string $namespace
86 *
87 * @return array
88 */
89 public static function filter_blocks_by_namespace( $blocks, $namespace = Form_Manager::NAMESPACE_FIELDS ): array {
90 $fields = array();
91
92 self::filter_blocks(
93 function ( $block ) use ( $namespace ) {
94 return ( false !== stripos( $block['blockName'], $namespace ) );
95 },
96 $fields,
97 $blocks
98 );
99
100 return $fields;
101 }
102
103 public static function filter_blocks( $callable, array &$storage, array $source ) {
104 foreach ( $source as $index => $block ) {
105 if ( ! isset( $block['blockName'] ) ) {
106 continue;
107 }
108 if ( call_user_func( $callable, $block ) ) {
109 $storage[] = $block;
110 }
111
112 if ( ! empty( $block['innerBlocks'] ) ) {
113 self::filter_blocks( $callable, $storage, $block['innerBlocks'] );
114 }
115 }
116 }
117
118 public static function get_blocks_by_post( $post_id ): array {
119 $post = get_post( $post_id );
120
121 if ( ! is_a( $post, \WP_Post::class ) ) {
122 return array();
123 }
124
125 return array_map( function ( $block ) {
126 if ( 'core/block' !== $block['blockName'] ) {
127 return $block;
128 }
129 $reusable_id = $block['attrs']['ref'] ?? 0;
130 $block['innerBlocks'] = self::get_blocks_by_post( $reusable_id );
131
132 return $block;
133 }, parse_blocks( $post->post_content ) );
134 }
135
136 public static function delete_namespace( $block ): string {
137 if ( is_array( $block ) ) {
138 $block = $block['blockName'] ?? '';
139 }
140
141 if ( stripos( $block, '/' ) === false ) {
142 return $block;
143 }
144
145 return explode( '/', $block )[1] ?? '';
146 }
147
148 public static function is_field( $block_name ): bool {
149 return ( stripos( $block_name, Form_Manager::NAMESPACE_FIELDS ) !== false );
150 }
151
152 public static function render_with_context( $block, $context ) {
153 return ( new \WP_Block( $block, $context ) )->render();
154 }
155
156 public static function pref( string $block_name ): string {
157 return Form_Manager::NAMESPACE_FIELDS . self::delete_namespace( $block_name );
158 }
159
160 public static function get_attrs_from_block( array $block, array $attrs_list ): array {
161 $source = $block['attrs'] ?? $block;
162 $attrs = array();
163
164 foreach ( $attrs_list as $attr_name ) {
165 if ( ! isset( $source[ $attr_name ] ) ) {
166 continue;
167 }
168 $attrs[ $attr_name ] = $source[ $attr_name ];
169 }
170
171 return $attrs;
172 }
173
174 public static function get_block_names( $names ) {
175 if ( ! is_array( $names ) ) {
176 $block = jet_form_builder()->blocks->get_field_by_name( $names );
177
178 return $block->get_name();
179 }
180
181 return array_map(
182 array( static::class, 'get_block_names' ),
183 $names
184 );
185 }
186
187 }
188