PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.3.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.3.1
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/Acf/AcfHandler.php +64 -170 2.10.03.3.1 View file →
@@ -8,20 +8,22 @@
8 8 namespace BitCode\BitForm\Core\Integration\Acf;
9 9
10 10 use BitCode\BitForm\Core\Form\FormManager;
11 11 use BitCode\BitForm\Core\Integration\IntegrationHandler;
12 +use BitCode\BitForm\Core\Util\PostHelpers;
12 13 use BitCode\BitForm\Core\Util\SmartTags;
13 14 use BitCode\BitForm\Core\Util\WpFileHandler;
14 15
15 -/**
16 - * Provide functionality for POST CREATEION WITH ACF
17 - */
18 16 class AcfHandler
19 17 {
18 + use PostHelpers;
19 +
20 20 private $_formID;
21 21
22 22 private $_wpdb;
23 23
24 + private const UPLOAD_FIELD = ['file-up', 'advanced-file-up'];
25 +
24 26 public function __construct($integrationID, $fromID)
25 27 {
26 28 $this->_formID = $fromID;
27 29 global $wpdb;
@@ -27,13 +29,8 @@
27 29 global $wpdb;
28 30 $this->_wpdb = $wpdb;
29 31 }
30 32
31 - /**
32 - * Helps to register ajax function's with wp
33 - *
34 - * @return null
35 - */
36 33 private function smartTagMappingValue($fieldMap)
37 34 {
38 35 $specialTagFieldValue = [];
39 36 $data = SmartTags::getPostUserData(true);
@@ -41,8 +38,9 @@
41 38
42 39 foreach ($fieldMap as $value) {
43 40 if (isset($value->formField)) {
44 41 $triggerValue = $value->formField;
42 +
45 43 if (in_array($triggerValue, $specialTagFields)) {
46 44 $specialTagFieldValue[$value->formField] = SmartTags::getSmartTagValue($triggerValue, $data);
47 45 }
48 46 }
@@ -49,202 +47,98 @@
49 47 }
50 48 return $specialTagFieldValue;
51 49 }
52 50
53 - private function postFieldMapping($fieldData, $post_map, $formFields, $fieldValues, $postID, $entryID)
51 + public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID)
54 52 {
55 - $uploadFeatureImg = new WpFileHandler($this->_formID);
56 - foreach ($post_map as $fieldPair) {
57 - foreach ($formFields as $field) {
58 - if (!empty($fieldPair->postField) && !empty($fieldPair->formField)) {
59 - if ('_thumbnail_id' !== $fieldPair->postField) {
60 - if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) {
61 - $fieldData[$fieldPair->postField] = $fieldPair->customValue;
62 - } else {
63 - $fieldData[$fieldPair->postField] = $fieldValues[$fieldPair->formField];
64 - }
65 - } elseif ($fieldPair->formField === $field['key'] && 'file-up' === $field['type'] && '_thumbnail_id' === $fieldPair->postField) {
66 - if (!empty($fieldValues[$field['key']])) {
67 - $uploadFeatureImg->uploadFeatureImg($fieldValues[$field['key']], $entryID, $postID);
68 - }
69 - }
70 - }
71 - }
72 - }
73 - return $fieldData;
74 - }
53 + $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details;
75 54
76 - private function getAcfFields($postType)
77 - {
78 - $acfFields = [];
79 - $field_groups = get_posts(['post_type' => 'acf-field-group']);
55 + $taxonomy = new WpFileHandler($integrationData->form_id);
80 56
81 - if ($field_groups) {
82 - $groups = acf_get_field_groups(['post_type' => $postType]);
83 - foreach ($groups as $group) {
84 - foreach (acf_get_fields($group['key']) as $acf) {
85 - array_push(
86 - $acfFields,
87 - [
88 - 'key' => $acf['key'],
89 - 'name' => $acf['name'],
90 - 'required' => $acf['required'],
91 - 'type' => $acf['type'],
92 - 'multiple' => isset($acf['multiple']) ? $acf['multiple'] : '',
93 - ]
94 - );
95 - }
96 - }
97 - }
98 - return $acfFields;
99 - }
57 + $formManger = FormManager::getInstance($integrationData->form_id);
100 58
101 - private function acfFieldMapping($acfMapField, $fieldValues, $acfFields)
102 - {
103 - $acfFieldData = [];
104 - $string_types = ['text', 'textarea', 'password', 'wysiwyg', 'number', 'radio', 'color_picker', 'oembed', 'email', 'url', 'date_picker', 'true_false', 'date_time_picker', 'time_picker', 'message'];
105 - $selectedTypes = ['checkbox', 'select', 'user', 'post_object'];
106 - foreach ($acfMapField as $key => $fieldPair) {
107 - foreach ($acfFields as $acf) {
108 - if (property_exists($fieldPair, 'acfField')) {
109 - if ($acf['key'] === $fieldPair->acfField && in_array($acf['type'], $string_types)) {
110 - $acfFieldData[$key]['key'] = $fieldPair->acfField;
111 - $acfFieldData[$key]['name'] = $acf['name'];
112 - if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) {
113 - $acfFieldData[$key]['value'] = $fieldPair->customValue;
114 - } elseif ('custom' !== $fieldPair->formField && !empty($fieldValues[$fieldPair->formField])) {
115 - $acfFieldData[$key]['value'] = $fieldValues[$fieldPair->formField];
116 - }
117 - } elseif ($acf['key'] === $fieldPair->acfField && in_array($acf['type'], $selectedTypes) && !empty($fieldValues[$fieldPair->formField])) {
118 - $acfFieldData[$key]['key'] = $fieldPair->acfField;
119 - if (1 === $acf['multiple']) {
120 - $acfFieldData[$key]['value'] = explode(',', $fieldValues[$fieldPair->formField]);
121 - $acfFieldData[$key]['name'] = $acf['name'];
122 - } else {
123 - $acfFieldData[$key]['value'] = $fieldValues[$fieldPair->formField];
124 - $acfFieldData[$key]['name'] = $acf['name'];
125 - }
126 - }
127 - }
128 - }
129 - }
130 - return $acfFieldData;
131 - }
59 + $formFields = $formManger->getFields();
132 60
133 - private function acfFileMapping($acfMapField, $fieldValues, $acfFields, $entryID, $post_id)
134 - {
135 - $fileTypes = ['file', 'image'];
136 - $fileUploadHandle = new WpFileHandler($this->_formID);
137 - foreach ($acfMapField as $fieldPair) {
138 - foreach ($acfFields as $acf) {
139 - if (property_exists($fieldPair, 'acfFileUpload')) {
140 - if ($acf['key'] === $fieldPair->acfFileUpload && in_array($acf['type'], $fileTypes)) {
141 - if (!empty($fieldValues[$fieldPair->formField])) {
142 - $attachMentId = $fileUploadHandle->singleFileMoveWpMedia($entryID, $fieldValues[$fieldPair->formField], $post_id);
143 - if (!empty($attachMentId)) {
144 - $exists = metadata_exists('post', $post_id, '_' . $acf['name']);
145 - if (false === $exists) {
146 - update_post_meta($post_id, '_' . $acf['name'], $acf['key']);
147 - update_post_meta($post_id, $acf['name'], wp_json_encode($attachMentId));
148 - } else {
149 - update_post_meta($post_id, $acf['name'], wp_json_encode($attachMentId));
150 - }
151 - }
152 - }
153 - } elseif ($acf['key'] === $fieldPair->acfFileUpload && 'gallery' === $acf['type']) {
154 - if (!empty($fieldValues[$fieldPair->formField])) {
155 - $attachMentId = $fileUploadHandle->multiFileMoveWpMedia($entryID, $fieldValues[$fieldPair->formField], $post_id);
156 - $exists = metadata_exists('post', $post_id, '_' . $acf['name']);
157 - if (!empty($attachMentId)) {
158 - if (false === $exists) {
159 - update_post_meta($post_id, '_' . $acf['name'], $acf['key']);
160 - update_post_meta($post_id, $acf['name'], $attachMentId);
161 - } else {
162 - update_post_meta($post_id, $acf['name'], $attachMentId);
163 - }
164 - }
165 - }
166 - }
167 - }
168 - }
169 - }
170 - }
61 + $postData = $this->setPostData($integrationDetails);
171 62
172 - public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID)
173 - {
174 - $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details;
175 - $taxonomy = new WpFileHandler($integrationData->form_id);
176 - $formManger = new FormManager($integrationData->form_id);
177 - $formFields = $formManger->getFields();
63 + $existId = $postData['post_type'] . '_' . $entryID;
178 64
179 - $fieldData = [];
65 + $existPostId = $this->_wpdb->get_results(
66 + $this->_wpdb->prepare(
67 + "SELECT * FROM `{$this->_wpdb->prefix}bitforms_form_entrymeta` WHERE `meta_key`=%s",
68 + $existId
69 + )
70 + );
180 71
181 - $fieldData['comment_status'] = isset($integrationDetails->comment_status) ? $integrationDetails->comment_status : '';
182 - $fieldData['post_status'] = isset($integrationDetails->post_status) ? $integrationDetails->post_status : '';
183 - $fieldData['post_type'] = isset($integrationDetails->post_type) ? $integrationDetails->post_type : '';
184 - if (isset($integrationDetails->post_author) && 'logged_in_user' !== $integrationDetails->post_author) {
185 - $fieldData['post_author'] = $integrationDetails->post_author;
186 - } else {
187 - $fieldData['post_author'] = get_current_user_id();
188 - }
72 + $taxonomies = $taxonomy->taxonomyData($formFields, $fieldValues);
189 73
190 - $exist_id = $fieldData['post_type'] . '_' . $entryID;
191 - $sql = "SELECT * FROM `{$this->_wpdb->prefix}bitforms_form_entrymeta` WHERE `meta_key`='$exist_id' ";
192 - $exist_post_id = $this->_wpdb->get_results($sql);
193 - $post_id = '';
194 - $acfFields = $this->getAcfFields($fieldData['post_type']);
74 + if ([] === $existPostId) {
75 + $postId = wp_insert_post(['post_title' => '(no title)', 'post_content' => '']);
195 76
196 - $taxanomyData = $taxonomy->taxonomyData($formFields, $fieldValues);
77 + $smartTagValue = $this->smartTagMappingValue($integrationDetails->post_map);
197 78
198 - if ([] === $exist_post_id) {
199 - $post_id = wp_insert_post(['post_title' => '(no title)', 'post_content' => '']);
200 - $smartTagValue = $this->smartTagMappingValue($integrationDetails->post_map);
201 79 $updatedValues = $fieldValues + $smartTagValue;
202 - $updateData = $this->postFieldMapping($fieldData, $integrationDetails->post_map, $formFields, $updatedValues, $post_id, $entryID);
203 80
204 - $updateData['ID'] = $post_id;
205 - unset($updateData['_thumbnail_id']);
81 + $postMappedData = $this->postFieldMapping($integrationDetails->post_map, $updatedValues, $postId, $entryID);
206 82
207 - wp_update_post($updateData, true);
208 - if (!empty($taxanomyData)) {
209 - foreach ($taxanomyData as $taxanomy) {
210 - wp_set_post_terms($post_id, $taxanomy['value'], $taxanomy['term'], false);
83 + $updatedData = $postData + $postMappedData;
84 +
85 + $updatedData['ID'] = $postId;
86 +
87 + unset($updatedData['_thumbnail_id']);
88 +
89 + wp_update_post($updatedData, true);
90 +
91 + if (!empty($taxonomies)) {
92 + foreach ($taxonomies as $taxonomy) {
93 + wp_set_post_terms($postId, $taxonomy['value'], $taxonomy['term'], false);
211 94 }
212 95 }
96 +
97 + // Form entry meta insert; meta_key/meta_value required to map dynamic field data to ACF integration record.
213 98 $this->_wpdb->insert(
214 99 "{$this->_wpdb->prefix}bitforms_form_entrymeta",
215 100 [
216 - 'meta_key' => $fieldData['post_type'] . '_' . $entryID,
217 - 'meta_value' => $post_id,
101 + 'meta_key' => $postData['post_type'] . '_' . $entryID,
102 + 'meta_value' => $postId,
218 103 'bitforms_form_entry_id' => $entryID,
219 104 ]
220 105 );
221 106
222 107 $smartTagValue = $this->smartTagMappingValue($integrationDetails->acf_map);
108 +
223 109 $updatedAcfValues = $fieldValues + $smartTagValue;
224 - $acfFieldMapping = $this->acfFieldMapping($integrationDetails->acf_map, $updatedAcfValues, $acfFields);
225 - $this->acfFileMapping($integrationDetails->acf_file_map, $fieldValues, $acfFields, $entryID, $post_id);
226 - foreach ($acfFieldMapping as $data) {
227 - if (isset($data['key'], $data['value'])) {
228 - add_post_meta($post_id, '_' . $data['name'], $data['key']);
229 - add_post_meta($post_id, $data['name'], $data['value']);
230 - }
110 +
111 + $acfFieldMappedData = AcfHelper::acfFieldMapping($integrationDetails->acf_map, $updatedAcfValues);
112 +
113 + AcfHelper::acfFileMapping($integrationDetails->acf_file_map, $fieldValues, $entryID, $postId, $this->_formID);
114 +
115 + foreach ($acfFieldMappedData as $key => $metaValue) {
116 + update_field($key, $metaValue, $postId);
231 117 }
232 118 } else {
233 - if (!empty($taxanomyData)) {
234 - foreach ($taxanomyData as $taxanomy) {
235 - wp_set_post_terms($exist_post_id[0]->meta_value, $taxanomy['value'], $taxanomy['term'], false);
119 + if (!empty($taxonomies)) {
120 + foreach ($taxonomies as $taxonomy) {
121 + wp_set_post_terms($existPostId[0]->meta_value, $taxonomy['value'], $taxonomy['term'], false);
236 122 }
237 123 }
238 - $acfFieldMapping = $this->acfFieldMapping($integrationDetails->acf_map, $fieldValues, $acfFields);
239 - $this->acfFileMapping($integrationDetails->acf_file_map, $fieldValues, $acfFields, $entryID, $exist_post_id[0]->meta_value);
124 +
125 + $acfFieldMapping = AcfHelper::acfFieldMapping($integrationDetails->acf_map, $fieldValues);
126 +
127 + AcfHelper::acfFileMapping($integrationDetails->acf_file_map, $fieldValues, $entryID, $existPostId[0]->meta_value, $this->_formID);
128 +
240 129 foreach ($acfFieldMapping as $data) {
241 130 if (isset($data['key'], $data['value'])) {
242 - update_post_meta($exist_post_id[0]->meta_value, $data['name'], $data['value']);
131 + update_post_meta($existPostId[0]->meta_value, $data['name'], $data['value']);
243 132 }
244 133 }
245 - $updateData = $this->postFieldMapping($fieldData, $integrationDetails->post_map, $formFields, $fieldValues, $exist_post_id[0]->meta_value, $entryID);
246 - $updateData['ID'] = $exist_post_id[0]->meta_value;
247 - wp_update_post($updateData, true);
134 +
135 + $postMappedData = $this->postFieldMapping($integrationDetails->post_map, $fieldValues, $existPostId[0]->meta_value, $entryID);
136 +
137 + $updatedData = $postMappedData + $postData;
138 +
139 + $updatedData['ID'] = $existPostId[0]->meta_value;
140 +
141 + wp_update_post($updatedData, true);
248 142 }
249 143 }
250 144 }