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