PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.2.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.2.2
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 3.2.2, at includes/Core/Integration/Acf/AcfHandler.php

145 lines 4.4 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\PostHelpers;
13 use BitCode\BitForm\Core\Util\SmartTags;
14 use BitCode\BitForm\Core\Util\WpFileHandler;
15
16 class AcfHandler
17 {
18 use PostHelpers;
19
20 private $_formID;
21
22 private $_wpdb;
23
24 private const UPLOAD_FIELD = ['file-up', 'advanced-file-up'];
25
26 public function __construct($integrationID, $fromID)
27 {
28 $this->_formID = $fromID;
29 global $wpdb;
30 $this->_wpdb = $wpdb;
31 }
32
33 private function smartTagMappingValue($fieldMap)
34 {
35 $specialTagFieldValue = [];
36 $data = SmartTags::getPostUserData(true);
37 $specialTagFields = SmartTags::smartTagFieldKeys();
38
39 foreach ($fieldMap as $value) {
40 if (isset($value->formField)) {
41 $triggerValue = $value->formField;
42
43 if (in_array($triggerValue, $specialTagFields)) {
44 $specialTagFieldValue[$value->formField] = SmartTags::getSmartTagValue($triggerValue, $data);
45 }
46 }
47 }
48 return $specialTagFieldValue;
49 }
50
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;
54
55 $taxonomy = new WpFileHandler($integrationData->form_id);
56
57 $formManger = FormManager::getInstance($integrationData->form_id);
58
59 $formFields = $formManger->getFields();
60
61 $postData = $this->setPostData($integrationDetails);
62
63 $existId = $postData['post_type'] . '_' . $entryID;
64
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 );
71
72 $taxonomies = $taxonomy->taxonomyData($formFields, $fieldValues);
73
74 if ([] === $existPostId) {
75 $postId = wp_insert_post(['post_title' => '(no title)', 'post_content' => '']);
76
77 $smartTagValue = $this->smartTagMappingValue($integrationDetails->post_map);
78
79 $updatedValues = $fieldValues + $smartTagValue;
80
81 $postMappedData = $this->postFieldMapping($integrationDetails->post_map, $updatedValues, $postId, $entryID);
82
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);
94 }
95 }
96
97 // Form entry meta insert; meta_key/meta_value required to map dynamic field data to ACF integration record.
98 $this->_wpdb->insert(
99 "{$this->_wpdb->prefix}bitforms_form_entrymeta",
100 [
101 'meta_key' => $postData['post_type'] . '_' . $entryID,
102 'meta_value' => $postId,
103 'bitforms_form_entry_id' => $entryID,
104 ]
105 );
106
107 $smartTagValue = $this->smartTagMappingValue($integrationDetails->acf_map);
108
109 $updatedAcfValues = $fieldValues + $smartTagValue;
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);
117 }
118 } else {
119 if (!empty($taxonomies)) {
120 foreach ($taxonomies as $taxonomy) {
121 wp_set_post_terms($existPostId[0]->meta_value, $taxonomy['value'], $taxonomy['term'], false);
122 }
123 }
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
129 foreach ($acfFieldMapping as $data) {
130 if (isset($data['key'], $data['value'])) {
131 update_post_meta($existPostId[0]->meta_value, $data['name'], $data['value']);
132 }
133 }
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);
142 }
143 }
144 }
145