PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.5.4
JetFormBuilder — Dynamic Blocks Form Builder v3.5.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 year ago
process-meta-boxes-trait.php
159 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 = $modifier->fields_map;
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 }
80
81 return $result;
82 }
83
84 public function convert_meta_fields_structure( array $meta_fields, array $fields_map ): array {
85 $result = array();
86
87 foreach ( $meta_fields as $key => $meta_field ) {
88 if ( isset( $meta_field['name'] ) && in_array( $meta_field['name'], $fields_map ) ) {
89 $result[ $meta_field['name'] ] = $meta_field;
90
91 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'] ) ) ) {
92 $result[ $meta_field['name'] ]['is_timestamp'] = 2;
93 }
94
95 if ( isset( $meta_field['fields'] ) && is_array( $meta_field['fields'] ) ) {
96 $result[ $meta_field['name'] ]['fields'] = $this->convert_meta_fields_structure( $meta_field['fields'], $fields_map );
97 }
98 }
99 }
100
101 return $result;
102 }
103
104 public function normalize_checkboxes( array $meta_box_fields, array $values ): array {
105 foreach ( $meta_box_fields as $field_key => $field_data ) {
106 if ( isset( $values[ $field_key ] ) && is_array( $values[ $field_key ] ) && ( 'repeater' === $field_data['type'] ?? '' ) ) {
107 $repeater_values = $values[ $field_key ];
108 $repeater_fields = $field_data['fields'] ?? array();
109
110 foreach ( $repeater_fields as $sub_field_key => $sub_field_data ) {
111 if ( ( 'checkbox' === $sub_field_data['type'] ?? '' ) ) {
112 foreach ( $repeater_values as $item_key => $item_values ) {
113 if ( isset( $item_values[ $sub_field_key ] ) && is_array( $item_values[ $sub_field_key ] ) ) {
114 $options = $sub_field_data['options'] ?? array();
115 $normalized = array();
116
117 foreach ( $item_values[ $sub_field_key ] as $checkbox_key => $checkbox_value ) {
118 if ( ! empty( $checkbox_value ) ) {
119 $normalized[ $checkbox_value ] = 'true';
120 }
121 }
122
123 foreach ( $options as $option_key => $option_value ) {
124 if ( ! isset( $normalized[ $option_key ] ) ) {
125 $normalized[ $option_key ] = 'false';
126 }
127 }
128
129 $values[ $field_key ][ $item_key ][ $sub_field_key ] = $normalized;
130 }
131 }
132 }
133 }
134 }
135
136 if ( isset( $values[ $field_key ] ) && is_array( $values[ $field_key ] ) && ( 'checkbox' === $field_data['type'] ?? '' ) ) {
137 $options = $field_data['options'] ?? array();
138 $normalized = array();
139
140 foreach ( $values[ $field_key ] as $checkbox_key => $checkbox_value ) {
141 if ( ! empty( $checkbox_value ) ) {
142 $normalized[ $checkbox_value ] = 'true';
143 }
144 }
145
146 foreach ( $options as $option_key => $option_value ) {
147 if ( ! isset( $normalized[ $option_key ] ) ) {
148 $normalized[ $option_key ] = 'false';
149 }
150 }
151
152 $values[ $field_key ] = $normalized;
153 }
154 }
155
156 return $values;
157 }
158 }
159