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