PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
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 2.10.2 All 137 releases
bit-form / includes / Core / Integration / Acf / AcfHandler.php

AcfHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.0, at includes/Core/Integration/Acf/AcfHandler.php

241 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 *POST CREATEION WITH ACF
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\Acf;
9
10 use BitCode\BitForm\Core\Form\FormManager;
11 use BitCode\BitForm\Core\Integration\IntegrationHandler;
12 use BitCode\BitForm\Core\Util\SmartTags;
13 use BitCode\BitForm\Core\Util\WpFileHandler;
14
15 /**
16 * Provide functionality for POST CREATEION WITH ACF
17 */
18 class AcfHandler {
19 private $_formID;
20
21 public function __construct($integrationID, $fromID) {
22 $this->_formID = $fromID;
23 global $wpdb;
24 $this->_wpdb = $wpdb;
25 }
26
27 /**
28 * Helps to register ajax function's with wp
29 *
30 * @return null
31 */
32 private function smartTagMappingValue($fieldMap) {
33 $specialTagFieldValue = [];
34 $data = SmartTags::getPostUserData(true);
35 $specialTagFields = SmartTags::smartTagFieldKeys();
36
37 foreach ($fieldMap as $value) {
38 if (isset($value->formField)) {
39 $triggerValue = $value->formField;
40 if (in_array($triggerValue, $specialTagFields)) {
41 $specialTagFieldValue[$value->formField] = SmartTags::getSmartTagValue($triggerValue, $data);
42 }
43 }
44 }
45 return $specialTagFieldValue;
46 }
47
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 }
69
70 private function getAcfFields($postType) {
71 $acfFields = [];
72 $field_groups = get_posts(['post_type' => 'acf-field-group']);
73
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 }
93
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 }
124
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 }
162
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();
168
169 $fieldData = [];
170
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 }
179
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']);
185
186 $taxanomyData = $taxonomy->taxonomyData($formFields, $fieldValues);
187
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 $updatedValues = $fieldValues + $smartTagValue;
192 $updateData = $this->postFieldMapping($fieldData, $integrationDetails->post_map, $formFields, $updatedValues, $post_id, $entryID);
193
194 $updateData['ID'] = $post_id;
195 unset($updateData['_thumbnail_id']);
196
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);
201 }
202 }
203 $this->_wpdb->insert(
204 "{$this->_wpdb->prefix}bitforms_form_entrymeta",
205 [
206 'meta_key' => $fieldData['post_type'] . '_' . $entryID,
207 'meta_value' => $post_id,
208 'bitforms_form_entry_id' => $entryID,
209 ]
210 );
211
212 $smartTagValue = $this->smartTagMappingValue($integrationDetails->acf_map);
213 $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 }
221 }
222 } 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);
226 }
227 }
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);
230 foreach ($acfFieldMapping as $data) {
231 if (isset($data['key'], $data['value'])) {
232 update_post_meta($exist_post_id[0]->meta_value, $data['name'], $data['value']);
233 }
234 }
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);
238 }
239 }
240 }
241