PluginProbe
bBlocks – Essential Gutenberg Blocks & Patterns Collection / trunk
bBlocks – Essential Gutenberg Blocks & Patterns Collection vtrunk
2.1.6 2.1.5 2.1.4 2.1.3 2.1.2 2.1.1 2.1.0 2.0.43 2.0.42 2.0.41 2.0.40 2.0.39 2.0.38 trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 106 releases
b-blocks / includes / blocks / form / Helper.php

Helper.php in bBlocks – Essential Gutenberg Blocks & Patterns Collection trunk, at includes/blocks/form/Helper.php

141 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class BBlocksFormHelper {
4
5 public static function get_form_data( $post_id, $block_id, $block_name ) {
6 if(!is_numeric($post_id)){
7 return [];
8 }
9 $post_id = intval($post_id);
10 $post_content = get_post_field( 'post_content', $post_id );
11 if ( ! is_null( $post_content ) ) {
12 $blocks = parse_blocks( $post_content );
13 if ( is_array( $blocks ) ) {
14 return self::get_attributes( $block_id, $block_name, $blocks );
15 }
16 return [];
17 }
18 return [];
19 }
20
21 public static function get_attributes( $block_id, $block_name, $blocks ) {
22 foreach ( $blocks as $block ) {
23
24 if ( ( $block['attrs']['formId'] ?? '' ) === $block_id && ( $block['blockName'] ?? '' ) === $block_name ) {
25 return [
26 'attributes' => isset($block['attrs']) ? $block['attrs'] : [],
27 'innerBlocks' => isset($block['innerBlocks']) ? self::get_inner_blocks( $block['innerBlocks'] ) : [],
28 ];
29 }
30
31 if ( !isset( $block['innerBlocks'] ) ) {
32 return [];
33 }
34
35 if (is_array( $block['innerBlocks'] ) && count( $block['innerBlocks'] ) > 0) {
36 $data = self::get_attributes(
37 $block_id,
38 $block_name,
39 $block['innerBlocks']
40 );
41 if ( ! empty( $data ) ) {
42 return $data;
43 }
44 }
45
46 if (isset( $block['blockName'] ) && $block['blockName'] === 'core/template-part' && ! empty( $block['attrs']['slug'] ) ) {
47 $part_slug = $block['attrs']['theme'] . '//' . $block['attrs']['slug'];
48 $data = self::get_form_data( $part_slug, $block_id, $block_name );
49 if ( ! empty( $data ) ) {
50 return $data;
51 }
52 }
53 }
54 return [];
55 }
56
57 public static function get_inner_blocks( $inner_blocks ) {
58 return array_map(function ( $inner_block ) {
59 $block_data = [
60 'blockName' => $inner_block['blockName'],
61 'attributes' => $inner_block['attrs'],
62 ];
63 if ( isset($inner_block['innerBlocks']) && count($inner_block['innerBlocks']) > 0 ) {
64 $block_data['innerBlocks'] = self::get_inner_blocks( $inner_block['innerBlocks'] );
65 }
66 return $block_data;
67 }, $inner_blocks);
68 }
69
70 public static function sort_fields( $blocks, $custom_fields ) {
71
72 $fields = [];
73 foreach ( $blocks as $block ) {
74 $name = $block['attributes']['inputField']['name'] ?? null;
75 $inputType = $block['attributes']['inputField']['type'] ?? null;
76
77 if ( $name && isset( $custom_fields[ $name ] ) ) {
78 $fields[] = [
79 'name' => $name,
80 'inputType' => $inputType,
81 'value' => $custom_fields[ $name ]
82 ];
83 }
84 }
85
86 return $fields;
87 }
88
89 public static function generate_schema( $fields ) {
90 $schema = [];
91 foreach ( $fields as $field ) {
92 if ( isset( $field['name'] ) ) {
93 $name = $field['name'];
94 $input_type = $field['inputType'] ?? 'text';
95
96 switch ( $input_type ) {
97 case 'text':
98 $schema[ $name ] = 'string';
99 break;
100 case 'email':
101 $schema[ $name ] = 'email';
102 break;
103 case 'password':
104 $schema[ $name ] = 'string';
105 break;
106 case 'number':
107 $schema[ $name ] = 'number';
108 break;
109 case 'url':
110 $schema[ $name ] = 'url';
111 break;
112 case 'boolean':
113 $schema[ $name ] = 'boolean';
114 break;
115 case 'textarea':
116 $schema[ $name ] = 'textarea';
117 break;
118 default:
119 $schema[ $name ] = 'string';
120 break;
121 }
122 }
123 }
124
125 return $schema;
126 }
127
128 public static function get_password_requirements( $blocks ) {
129 $requirements = [];
130
131 foreach ( $blocks as $block ) {
132 if ( $block['attributes']['inputField']['type'] ?? '' === 'password' ) {
133 $requirements = $block['attributes']['password']['options'] ?? [];
134 break;
135 }
136 }
137
138 return $requirements;
139 }
140
141 }