PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 2.0.1
JetFormBuilder — Dynamic Blocks Form Builder v2.0.1
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
button-types 4 years ago exceptions 4 years ago modules 4 years ago render 4 years ago types 4 years ago action-buttons-manager.php 4 years ago block-helper.php 4 years ago blocks-repository-base.php 4 years ago default-blocks-repository.php 4 years ago form-builder-blocks-repository.php 4 years ago manager.php 4 years ago
block-helper.php
143 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Blocks;
5
6 use Jet_Form_Builder\Form_Manager;
7
8 /**
9 * Class Helper
10 *
11 * @package Jet_Form_Builder\Blocks
12 */
13 class Block_Helper {
14
15 /**
16 * @param $value
17 * @param $blocks
18 *
19 * @return array
20 */
21 public static function find_block_by_name( $value, $blocks ): array {
22 return self::find_block_by_attr( $value, 'name', $blocks );
23 }
24
25 /**
26 * @param $value
27 * @param $attr_name
28 * @param $blocks
29 *
30 * @return array
31 */
32 public static function find_block_by_attr( $value, $attr_name, $blocks ): array {
33 return self::find_block(
34 function ( $block ) use ( $value, $attr_name ) {
35 return ( ( $block['attrs'][ $attr_name ] ?? false ) === $value );
36 },
37 $blocks
38 );
39 }
40
41 public static function find_block( $callable, $blocks ): array {
42 if ( ! is_callable( $callable ) ) {
43 return array();
44 }
45 foreach ( $blocks as $block ) {
46 if ( ! isset( $block['blockName'] ) || ! isset( $block['attrs'] ) ) {
47 continue;
48 }
49 if ( call_user_func( $callable, $block ) ) {
50 return $block;
51 }
52
53 if ( 0 < count( $block['innerBlocks'] ) ) {
54 $find = self::find_block( $callable, $block['innerBlocks'] );
55
56 if ( $find ) {
57 return $find;
58 }
59 }
60 }
61
62 return array();
63 }
64
65 /**
66 * @param $blocks
67 * @param string $namespace
68 *
69 * @return array
70 */
71 public static function filter_blocks_by_namespace( $blocks, $namespace = Form_Manager::NAMESPACE_FIELDS ): array {
72 $fields = array();
73
74 self::filter_blocks(
75 function ( $block ) use ( $namespace ) {
76 return ( false !== stripos( $block['blockName'], $namespace ) );
77 },
78 $fields,
79 $blocks
80 );
81
82 return $fields;
83 }
84
85 public static function filter_blocks( $callable, array &$storage, array $source ) {
86 foreach ( $source as $index => $block ) {
87 if ( ! isset( $block['blockName'] ) ) {
88 continue;
89 }
90 if ( call_user_func( $callable, $block ) ) {
91 $storage[] = $block;
92 }
93
94 if ( ! empty( $block['innerBlocks'] ) ) {
95 self::filter_blocks( $callable, $storage, $block['innerBlocks'] );
96 }
97 }
98 }
99
100 public static function get_blocks_by_post( $post_id ): array {
101 return parse_blocks( get_post( $post_id )->post_content );
102 }
103
104 public static function delete_namespace( $block ): string {
105 if ( is_array( $block ) ) {
106 $block = $block['blockName'] ?? '';
107 }
108
109 if ( stripos( $block, '/' ) === false ) {
110 return $block;
111 }
112
113 return explode( '/', $block )[1] ?? '';
114 }
115
116 public static function is_field( $block_name ): bool {
117 return ( stripos( $block_name, Form_Manager::NAMESPACE_FIELDS ) !== false );
118 }
119
120 public static function render_with_context( $block, $context ) {
121 return ( new \WP_Block( $block, $context ) )->render();
122 }
123
124 public static function pref( string $block_name ): string {
125 return Form_Manager::NAMESPACE_FIELDS . self::delete_namespace( $block_name );
126 }
127
128 public static function get_attrs_from_block( array $block, array $attrs_list ): array {
129 $source = $block['attrs'] ?? $block;
130 $attrs = array();
131
132 foreach ( $attrs_list as $attr_name ) {
133 if ( ! isset( $source[ $attr_name ] ) ) {
134 continue;
135 }
136 $attrs[ $attr_name ] = $source[ $attr_name ];
137 }
138
139 return $attrs;
140 }
141
142 }
143