PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.0.0.1
JetFormBuilder — Dynamic Blocks Form Builder v3.0.0.1
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 / 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
171 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 find_block( $callable, $blocks ): array {
43 if ( ! is_callable( $callable ) ) {
44 return array();
45 }
46 foreach ( $blocks as $block ) {
47 if ( ! isset( $block['blockName'] ) || ! isset( $block['attrs'] ) ) {
48 continue;
49 }
50 if ( call_user_func( $callable, $block ) ) {
51 return $block;
52 }
53
54 if ( 0 < count( $block['innerBlocks'] ) ) {
55 $find = self::find_block( $callable, $block['innerBlocks'] );
56
57 if ( $find ) {
58 return $find;
59 }
60 }
61 }
62
63 return array();
64 }
65
66 /**
67 * @param $blocks
68 * @param string $namespace
69 *
70 * @return array
71 */
72 public static function filter_blocks_by_namespace( $blocks, $namespace = Form_Manager::NAMESPACE_FIELDS ): array {
73 $fields = array();
74
75 self::filter_blocks(
76 function ( $block ) use ( $namespace ) {
77 return ( false !== stripos( $block['blockName'], $namespace ) );
78 },
79 $fields,
80 $blocks
81 );
82
83 return $fields;
84 }
85
86 public static function filter_blocks( $callable, array &$storage, array $source ) {
87 foreach ( $source as $index => $block ) {
88 if ( ! isset( $block['blockName'] ) ) {
89 continue;
90 }
91 if ( call_user_func( $callable, $block ) ) {
92 $storage[] = $block;
93 }
94
95 if ( ! empty( $block['innerBlocks'] ) ) {
96 self::filter_blocks( $callable, $storage, $block['innerBlocks'] );
97 }
98 }
99 }
100
101 public static function get_blocks_by_post( $post_id ): array {
102 $post = get_post( $post_id );
103
104 if ( ! is_a( $post, \WP_Post::class ) ) {
105 return array();
106 }
107
108 return array_map( function ( $block ) {
109 if ( 'core/block' !== $block['blockName'] ) {
110 return $block;
111 }
112 $reusable_id = $block['attrs']['ref'] ?? 0;
113 $block['innerBlocks'] = self::get_blocks_by_post( $reusable_id );
114
115 return $block;
116 }, parse_blocks( $post->post_content ) );
117 }
118
119 public static function delete_namespace( $block ): string {
120 if ( is_array( $block ) ) {
121 $block = $block['blockName'] ?? '';
122 }
123
124 if ( stripos( $block, '/' ) === false ) {
125 return $block;
126 }
127
128 return explode( '/', $block )[1] ?? '';
129 }
130
131 public static function is_field( $block_name ): bool {
132 return ( stripos( $block_name, Form_Manager::NAMESPACE_FIELDS ) !== false );
133 }
134
135 public static function render_with_context( $block, $context ) {
136 return ( new \WP_Block( $block, $context ) )->render();
137 }
138
139 public static function pref( string $block_name ): string {
140 return Form_Manager::NAMESPACE_FIELDS . self::delete_namespace( $block_name );
141 }
142
143 public static function get_attrs_from_block( array $block, array $attrs_list ): array {
144 $source = $block['attrs'] ?? $block;
145 $attrs = array();
146
147 foreach ( $attrs_list as $attr_name ) {
148 if ( ! isset( $source[ $attr_name ] ) ) {
149 continue;
150 }
151 $attrs[ $attr_name ] = $source[ $attr_name ];
152 }
153
154 return $attrs;
155 }
156
157 public static function get_block_names( $names ) {
158 if ( ! is_array( $names ) ) {
159 $block = jet_form_builder()->blocks->get_field_by_name( $names );
160
161 return $block->get_name();
162 }
163
164 return array_map(
165 array( static::class, 'get_block_names' ),
166 $names
167 );
168 }
169
170 }
171