PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.5
JetFormBuilder — Dynamic Blocks Form Builder v3.6.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 / actions-v2 / insert-post / traits / process-meta-boxes-trait.php
jetformbuilder / modules / actions-v2 / insert-post / traits Last commit date
process-meta-boxes-trait.php 1 week ago
process-meta-boxes-trait.php
183 lines
1 <?php
2
3 namespace JFB_Modules\Actions_V2\Insert_Post\Traits;
4
5 use Jet_Engine\Glossaries\Meta_Fields;
6
7 // If this file is called directly, abort.
8 if ( ! defined( 'WPINC' ) ) {
9 die;
10 }
11
12 /**
13 * Trait Process_Meta_Boxes_Trait
14 *
15 * @package JFB_Modules\Actions_V2\Insert_Post\Traits
16 */
17 trait Process_Meta_Boxes_Trait {
18 /**
19 * Process meta boxes for the post.
20 *
21 * @param int $post_id Post ID.
22 * @param mixed $modifier Modifier object.
23 *
24 * @return void
25 */
26 protected function process_meta_boxes( int $post_id, $modifier ) {
27 if ( ! class_exists( 'Cherry_X_Post_Meta' ) || ! function_exists( 'jet_engine' ) ) {
28 return;
29 }
30
31 if ( ! $modifier || empty( $modifier->fields_map ) ) {
32 return;
33 }
34
35 if ( ! $post_id ) {
36 return;
37 }
38
39 $post = get_post( $post_id );
40 $post_type = get_post_type( $post_id );
41 $meta_box_fields = $this->get_meta_box_fields( $post_id, $modifier );
42
43 if ( ! empty( $meta_box_fields ) ) {
44 $args = array(
45 'page' => array( $post_type ),
46 'fields' => $meta_box_fields,
47 );
48
49 $meta = new \Cherry_X_Post_Meta( $args );
50
51 $meta->save_meta_option( $post_id );
52
53 if ( class_exists( 'Jet_Smart_Filters_Indexer_Manager' ) ) {
54 $indexer = new \Jet_Smart_Filters_Indexer_Manager();
55 $indexer->post_updated( $post_id, $post );
56 }
57 }
58 }
59
60 public function get_meta_box_fields( int $post_id, $modifier ): array {
61 if ( ! $post_id ) {
62 return array();
63 }
64
65 if ( ! class_exists( 'Cherry_X_Post_Meta' ) || ! function_exists( 'jet_engine' ) ) {
66 return array();
67 }
68
69 $post_type = get_post_type( $post_id );
70 $fields_map = $this->get_current_fields_map( $modifier );
71 $all_meta_fields = jet_engine()->meta_boxes->get_registered_fields();
72 $found_fields = $all_meta_fields[ $post_type ] ?? array();
73 $result = array();
74 $post_meta = new \Jet_Engine_CPT_Meta( $post_type, $found_fields );
75 $post_meta_fields = $post_meta->prepare_meta_fields( $found_fields );
76
77 if ( ! empty( $found_fields ) ) {
78 $result = $this->convert_meta_fields_structure( $post_meta_fields, $fields_map );
79 $result = $this->filter_meta_box_fields_by_values( $result );
80 }
81
82 return $result;
83 }
84
85 protected function get_current_fields_map( $modifier ): array {
86 $field_names = array_keys( $modifier->get_request() );
87
88 foreach ( jet_fb_context()->iterate_parsers_list( false ) as $parser ) {
89 $field_names[] = $parser->get_name();
90 }
91
92 return $this->filter_fields_map_by_field_names( $modifier->fields_map, $field_names );
93 }
94
95 protected function filter_fields_map_by_field_names( array $fields_map, array $field_names ): array {
96 return array_intersect_key( $fields_map, array_fill_keys( $field_names, true ) );
97 }
98
99 protected function filter_meta_box_fields_by_values( array $meta_box_fields ): array {
100 if ( ! is_array( $this->value ) ) {
101 return array();
102 }
103
104 // Prevent JetEngine from clearing mapped fields that are absent from the current form.
105 return array_intersect_key( $meta_box_fields, $this->value );
106 }
107
108 public function convert_meta_fields_structure( array $meta_fields, array $fields_map ): array {
109 $result = array();
110
111 foreach ( $meta_fields as $key => $meta_field ) {
112 if ( isset( $meta_field['name'] ) && in_array( $meta_field['name'], $fields_map ) ) {
113 $result[ $meta_field['name'] ] = $meta_field;
114
115 if ( isset( $meta_field['type'] ) && 'text' === $meta_field['type'] && ( isset( $meta_field['input_type'] ) && ( 'datetime-local' === $meta_field['input_type'] || 'date' === $meta_field['input_type'] || 'time' === $meta_field['input_type'] ) ) ) {
116 $result[ $meta_field['name'] ]['is_timestamp'] = 2;
117 }
118
119 if ( isset( $meta_field['fields'] ) && is_array( $meta_field['fields'] ) ) {
120 $result[ $meta_field['name'] ]['fields'] = $this->convert_meta_fields_structure( $meta_field['fields'], $fields_map );
121 }
122 }
123 }
124
125 return $result;
126 }
127
128 public function normalize_checkboxes( array $meta_box_fields, array $values ): array {
129 foreach ( $meta_box_fields as $field_key => $field_data ) {
130 if ( isset( $values[ $field_key ] ) && is_array( $values[ $field_key ] ) && ( 'repeater' === $field_data['type'] ?? '' ) ) {
131 $repeater_values = $values[ $field_key ];
132 $repeater_fields = $field_data['fields'] ?? array();
133
134 foreach ( $repeater_fields as $sub_field_key => $sub_field_data ) {
135 if ( ( 'checkbox' === $sub_field_data['type'] ?? '' ) ) {
136 foreach ( $repeater_values as $item_key => $item_values ) {
137 if ( isset( $item_values[ $sub_field_key ] ) && is_array( $item_values[ $sub_field_key ] ) ) {
138 $options = $sub_field_data['options'] ?? array();
139 $normalized = array();
140
141 foreach ( $item_values[ $sub_field_key ] as $checkbox_key => $checkbox_value ) {
142 if ( ! empty( $checkbox_value ) ) {
143 $normalized[ $checkbox_value ] = 'true';
144 }
145 }
146
147 foreach ( $options as $option_key => $option_value ) {
148 if ( ! isset( $normalized[ $option_key ] ) ) {
149 $normalized[ $option_key ] = 'false';
150 }
151 }
152
153 $values[ $field_key ][ $item_key ][ $sub_field_key ] = $normalized;
154 }
155 }
156 }
157 }
158 }
159
160 if ( isset( $values[ $field_key ] ) && ( 'checkbox' === ( $field_data['type'] ?? '' ) ) ) {
161
162 $options = $field_data['options'] ?? array();
163 $normalized = array();
164 // Changed: normalize JetEngine checkbox value even if it comes as a scalar.
165 $checkbox_values = (array) $values[ $field_key ];
166 foreach ( $checkbox_values as $checkbox_key => $checkbox_value ) {
167 if ( ! empty( $checkbox_value ) ) {
168 $normalized[ $checkbox_value ] = 'true';
169 }
170 }
171 foreach ( $options as $option_key => $option_value ) {
172 if ( ! isset( $normalized[ $option_key ] ) ) {
173 $normalized[ $option_key ] = 'false';
174 }
175 }
176 $values[ $field_key ] = $normalized;
177 }
178 }
179
180 return $values;
181 }
182 }
183