PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.15.3
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.15.3
3.3.1 V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 All 138 releases
← All changes | includes/Core/Integration/MetaBox/MetaBoxHandler.php +48 -173 2.10.02.15.3 View file →
@@ -6,23 +6,24 @@
6 6 */
7 7
8 8 namespace BitCode\BitForm\Core\Integration\MetaBox;
9 9
10 -// use BitCode\BitFormPro\Core\Util\WpFileHandler;
11 10 use BitCode\BitForm\Core\Form\FormManager;
12 11 use BitCode\BitForm\Core\Integration\IntegrationHandler;
12 +use BitCode\BitForm\Core\Util\PostHelpers;
13 13 use BitCode\BitForm\Core\Util\SmartTags;
14 14 use BitCode\BitForm\Core\Util\WpFileHandler;
15 15
16 -/**
17 - * Provide functionality for POST CREATEION WITH MetaBox
18 - */
19 16 class MetaBoxHandler
20 17 {
18 + use PostHelpers;
19 +
21 20 private $_formID;
22 21
23 22 private $_wpdb;
24 23
24 + private const UPLOAD_FIELD = ['file-up', 'advanced-file-up'];
25 +
25 26 public function __construct($integrationID, $fromID)
26 27 {
27 28 $this->_formID = $fromID;
28 29
@@ -29,13 +30,8 @@
29 30 global $wpdb;
30 31 $this->_wpdb = $wpdb;
31 32 }
32 33
33 - /**
34 - * Helps to register ajax function's with wp
35 - *
36 - * @return null
37 - */
38 34 private function smartTagMappingValue($fieldMap)
39 35 {
40 36 $specialTagFieldValue = [];
41 37 $data = SmartTags::getPostUserData(true);
@@ -51,171 +47,46 @@
51 47 }
52 48 return $specialTagFieldValue;
53 49 }
54 50
55 - private function postFieldMapping($fieldData, $post_map, $formFields, $fieldValues, $postID, $entryID)
51 + public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
56 52 {
57 - $uploadFeatureImg = new WpFileHandler($this->_formID);
58 - foreach ($post_map as $fieldPair) {
59 - foreach ($formFields as $field) {
60 - if (!empty($fieldPair->postField) && !empty($fieldPair->formField)) {
61 - if ('_thumbnail_id' !== $fieldPair->postField) {
62 - if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) {
63 - $fieldData[$fieldPair->postField] = $fieldPair->customValue;
64 - } else {
65 - $fieldData[$fieldPair->postField] = $fieldValues[$fieldPair->formField];
66 - }
67 - } elseif ($fieldPair->formField === $field['key'] && 'file-up' === $field['type'] && '_thumbnail_id' === $fieldPair->postField) {
68 - if (!empty($fieldValues[$field['key']])) {
69 - $uploadFeatureImg->uploadFeatureImg($fieldValues[$field['key']], $entryID, $postID);
70 - }
71 - }
72 - }
73 - }
74 - }
75 - return $fieldData;
76 - }
53 + $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details;
77 54
78 - private function getMetaboxFields($postType)
79 - {
80 - $metaboxFields = [];
81 - $filterTypes = [
82 - 'file_input',
83 - 'group',
84 - 'tab',
85 - 'osm',
86 - 'heading',
87 - 'key_value',
88 - 'map',
89 - 'custom_html',
90 - 'background',
91 - 'fieldset_text',
92 - ];
55 + $formManger = new FormManager($integrationData->form_id);
93 56
94 - if (function_exists('rwmb_meta')) {
95 - foreach (rwmb_get_object_fields($postType) as $index => $field) {
96 - if (!in_array($field['type'], $filterTypes)) {
97 - $metaboxFields[$index]['name'] = $field['name'];
98 - $metaboxFields[$index]['key'] = $field['id'];
99 - $metaboxFields[$index]['required'] = $field['required'];
100 - $metaboxFields[$index]['type'] = $field['type'];
101 - $metaboxFields[$index]['multiple'] = $field['multiple'];
102 - }
103 - }
104 - }
57 + $formFields = $formManger->getFields();
105 58
106 - return $metaboxFields;
107 - }
59 + $postData = $this->setPostData($integrationDetails);
108 60
109 - private function metaboxFieldMapping($metaboxMapField, $fieldValues, $metaboxFields, $entryID, $postId, $action)
110 - {
111 - $metaboxFieldData = [];
112 - $stringTypes = ['text', 'checkbox', 'switch', 'textarea', 'password', 'wysiwyg', 'number', 'radio', 'color', 'oembed', 'email', 'url', 'date', 'true_false', 'date_time_picker', 'time_picker', 'message', 'tel', 'datetime-local', 'time', 'datetime'];
113 - $selectedTypes = ['select', 'checkbox_list', 'select_advanced', 'user', 'post'];
114 - foreach ($metaboxMapField as $key => $fieldPair) {
115 - foreach ($metaboxFields as $metabox) {
116 - if (property_exists($fieldPair, 'metaboxField')) {
117 - if ($metabox['key'] === $fieldPair->metaboxField && in_array($metabox['type'], $stringTypes)) {
118 - $metaboxFieldData[$key]['name'] = $metabox['key'];
119 - if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) {
120 - $metaboxFieldData[$key]['value'] = $fieldPair->customValue;
121 - } elseif ('custom' !== $fieldPair->formField && !empty($fieldValues[$fieldPair->formField])) {
122 - $metaboxFieldData[$key]['value'] = $fieldValues[$fieldPair->formField];
123 - }
124 - } elseif ($metabox['key'] === $fieldPair->metaboxField && in_array($metabox['type'], $selectedTypes) && !empty($fieldValues[$fieldPair->formField])) {
125 - $fieldData = $fieldValues[$fieldPair->formField];
126 - $values = !is_array($fieldData) ? explode(',', $fieldData) : $fieldData;
61 + $id = $postData['post_type'] . '_' . $entryID;
127 62
128 - $getMetaValues = get_post_meta($postId, $metabox['key'], false);
129 - if (true === $metabox['multiple'] && is_array($values)) {
130 - if ('update' === $action && count($getMetaValues) > 0) {
131 - foreach ($getMetaValues as $value) {
132 - delete_post_meta($postId, $metabox['key']);
133 - }
134 - }
135 - foreach ($values as $value) {
136 - add_post_meta($postId, $metabox['key'], $value);
137 - }
138 - } else {
139 - $metaboxFieldData[$key]['name'] = $metabox['key'];
140 - $metaboxFieldData[$key]['value'] = $fieldValues[$fieldPair->formField];
141 - }
142 - }
143 - }
144 - }
145 - }
146 - return $metaboxFieldData;
147 - }
63 + $existPostId = $this->_wpdb->get_results("SELECT * FROM `{$this->_wpdb->prefix}bitforms_form_entrymeta` WHERE `meta_key`='$id' ");
148 64
149 - public function fileUploadMapping($metaboxMapField, $fieldValues, $metaboxFields, $entryID, $postId)
150 - {
151 - $fileUploadHandle = new WpFileHandler($this->_formID);
65 + $metaBoxFields = rwmb_get_object_fields($postData['post_type']);
152 66
153 - foreach ($metaboxMapField as $key => $fieldPair) {
154 - foreach ($metaboxFields as $metabox) {
155 - if (property_exists($fieldPair, 'metaboxFileUpload')) {
156 - if ($metabox['key'] === $fieldPair->metaboxFileUpload && !empty($fieldValues[$fieldPair->formField])) {
157 - if (false === $metabox['multiple']) {
158 - $attachMentId = $fileUploadHandle->singleFileMoveWpMedia($entryID, $fieldValues[$fieldPair->formField], $postId);
67 + $taxonomies = (new WpFileHandler($integrationData->form_id))->getTaxonomies($formFields, $fieldValues);
159 68
160 - if (!empty($attachMentId)) {
161 - add_post_meta($postId, $metabox['key'], $attachMentId);
162 - }
163 - } elseif (true === $metabox['multiple']) {
164 - $attachMentId = $fileUploadHandle->multiFileMoveWpMedia($entryID, $fieldValues[$fieldPair->formField], $postId);
165 - //$exists = metadata_exists('post', $postId, $metabox['key']);
166 - if (!empty($attachMentId) && is_array($attachMentId)) {
167 - foreach ($attachMentId as $attachemnt) {
168 - add_post_meta($postId, $metabox['key'], $attachemnt);
169 - }
170 - }
171 - }
172 - }
173 - }
174 - }
175 - }
176 - }
69 + if ([] === $existPostId) {
70 + $postId = wp_insert_post(['post_title' => '(no title)', 'post_content' => '']);
177 71
178 - public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
179 - {
180 - $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details;
181 - $taxonomy = new WpFileHandler($integrationData->form_id);
182 - $formManger = new FormManager($integrationData->form_id);
183 - $formFields = $formManger->getFields();
72 + $smartTagValue = $this->smartTagMappingValue($integrationDetails->post_map);
184 73
185 - $fieldData = [];
74 + $updatedValues = $fieldValues + $smartTagValue;
186 75
187 - $fieldData['comment_status'] = isset($integrationDetails->comment_status) ? $integrationDetails->comment_status : '';
188 - $fieldData['post_status'] = isset($integrationDetails->post_status) ? $integrationDetails->post_status : '';
189 - $fieldData['post_type'] = isset($integrationDetails->post_type) ? $integrationDetails->post_type : '';
190 - if (isset($integrationDetails->post_author) && 'logged_in_user' !== $integrationDetails->post_author) {
191 - $fieldData['post_author'] = $integrationDetails->post_author;
192 - } else {
193 - $fieldData['post_author'] = get_current_user_id();
194 - }
76 + $postMappedData = $this->postFieldMapping($integrationDetails->post_map, $updatedValues, $postId, $entryID);
195 77
196 - $exist_id = $fieldData['post_type'] . '_' . $entryID;
197 - $sql = "SELECT * FROM `{$this->_wpdb->prefix}bitforms_form_entrymeta` WHERE `meta_key`='$exist_id' ";
198 - $existPostId = $this->_wpdb->get_results($sql);
199 - $postId = '';
200 - $metaboxFields = $this->getMetaboxFields($fieldData['post_type']);
78 + $updatedData = $postData + $postMappedData;
201 79
202 - $taxanomyData = $taxonomy->taxonomyData($formFields, $fieldValues);
80 + $updatedData['ID'] = $postId;
203 81
204 - if ([] === $existPostId) {
205 - $postId = wp_insert_post(['post_title' => '(no title)', 'post_content' => '']);
82 + unset($updatedData['_thumbnail_id']);
206 83
207 - $smartTagValue = $this->smartTagMappingValue($integrationDetails->post_map);
208 - $updatedValues = $fieldValues + $smartTagValue;
209 - $updateData = $this->postFieldMapping($fieldData, $integrationDetails->post_map, $formFields, $updatedValues, $postId, $entryID);
84 + wp_update_post($updatedData, true);
210 85
211 - $updateData['ID'] = $postId;
212 - unset($updateData['_thumbnail_id']);
213 -
214 - wp_update_post($updateData, true);
215 - if (!empty($taxanomyData)) {
216 - foreach ($taxanomyData as $taxanomy) {
217 - wp_set_post_terms($postId, $taxanomy['value'], $taxanomy['term'], false);
86 + if (!empty($taxonomies)) {
87 + foreach ($taxonomies as $taxonomy) {
88 + wp_set_post_terms($postId, $taxonomy['value'], $taxonomy['term'], false);
218 89 }
219 90 }
220 91
221 92 $this->_wpdb->insert(
@@ -220,9 +91,9 @@
220 91
221 92 $this->_wpdb->insert(
222 93 "{$this->_wpdb->prefix}bitforms_form_entrymeta",
223 94 [
224 - 'meta_key' => $fieldData['post_type'] . '_' . $entryID,
95 + 'meta_key' => $postData['post_type'] . '_' . $entryID,
225 96 'meta_value' => $postId,
226 97 'bitforms_form_entry_id' => $entryID,
227 98 ]
228 99 );
@@ -227,31 +98,35 @@
227 98 ]
228 99 );
229 100
230 101 $smartTagValue = $this->smartTagMappingValue($integrationDetails->metabox_map);
102 +
231 103 $updatedMbValues = $fieldValues + $smartTagValue;
232 - $metaboxFieldMapping = $this->metaboxFieldMapping($integrationDetails->metabox_map, $updatedMbValues, $metaboxFields, $entryID, $postId, 'create');
233 - $this->fileUploadMapping($integrationDetails->metabox_file_map, $fieldValues, $metaboxFields, $entryID, $postId, 'create');
234 - foreach ($metaboxFieldMapping as $data) {
235 - if (isset($data['name'], $data['value'])) {
236 - add_post_meta($postId, $data['name'], $data['value']);
237 - }
104 +
105 + $metaBoxMappedData = MetaBoxHelper::metaBoxFieldMapping($integrationDetails->metabox_map, $updatedMbValues, $metaBoxFields, $postId, 'create');
106 +
107 + MetaBoxHelper::fileUploadMapping($integrationDetails->metabox_file_map, $fieldValues, $metaBoxFields, $this->_formID, $entryID, $postId);
108 +
109 + foreach ($metaBoxMappedData as $key => $value) {
110 + add_post_meta($postId, $key, $value);
238 111 }
239 112 } else {
240 - if (!empty($taxanomyData)) {
241 - foreach ($taxanomyData as $taxanomy) {
242 - wp_set_post_terms($existPostId[0]->meta_value, $taxanomy['value'], $taxanomy['term'], false);
113 + if (!empty($taxonomies)) {
114 + foreach ($taxonomies as $taxonomy) {
115 + wp_set_post_terms($existPostId[0]->meta_value, $taxonomy['value'], $taxonomy['term'], false);
243 116 }
244 117 }
245 - $metaboxFieldMapping = $this->metaboxFieldMapping($integrationDetails->metabox_map, $fieldValues, $metaboxFields, $entryID, $existPostId[0]->meta_value, 'update');
246 118
247 - foreach ($metaboxFieldMapping as $data) {
248 - if (isset($data['name'], $data['value'])) {
249 - update_post_meta($existPostId[0]->meta_value, $data['name'], $data['value']);
250 - }
119 + $metaBoxMappedData = MetaBoxHelper::metaBoxFieldMapping($integrationDetails->metabox_map, $fieldValues, $metaBoxFields, $existPostId[0]->meta_value, 'update');
120 +
121 + foreach ($metaBoxMappedData as $key => $value) {
122 + update_post_meta($existPostId[0]->meta_value, $key, $value);
251 123 }
252 - $updateData = $this->postFieldMapping($fieldData, $integrationDetails->post_map, $formFields, $fieldValues, $existPostId[0]->meta_value, $entryID);
253 - $updateData['ID'] = $existPostId[0]->meta_value;
254 - wp_update_post($updateData, true);
124 +
125 + $updatedData = $this->postFieldMapping($postData, $integrationDetails->post_map, $fieldValues, $existPostId[0]->meta_value, $entryID);
126 +
127 + $updatedData['ID'] = $existPostId[0]->meta_value;
128 +
129 + wp_update_post($updatedData, true);
255 130 }
256 131 }
257 132 }