| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Pod Integration |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\Pods; |
| 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 POD integration |
| 17 |
*/ |
| 18 |
class PodsHandler |
| 19 |
{ |
| 20 |
private $_formID; |
| 21 |
private $_wpdb; |
| 22 |
|
| 23 |
public const BIT_FORMS_FILE_TYPE = ['file-up', 'advanced-file-up', 'signature']; |
| 24 |
|
| 25 |
public function __construct($integrationID, $fromID) |
| 26 |
{ |
| 27 |
$this->_formID = $fromID; |
| 28 |
|
| 29 |
global $wpdb; |
| 30 |
$this->_wpdb = $wpdb; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Helps to register ajax function's with wp |
| 35 |
* |
| 36 |
* @return null |
| 37 |
*/ |
| 38 |
private function smartTagMappingValue($fieldMap) |
| 39 |
{ |
| 40 |
$specialTagFieldValue = []; |
| 41 |
$data = SmartTags::getPostUserData(true); |
| 42 |
$specialTagFields = SmartTags::smartTagFieldKeys(); |
| 43 |
|
| 44 |
foreach ($fieldMap as $value) { |
| 45 |
if (isset($value->formField)) { |
| 46 |
$triggerValue = $value->formField; |
| 47 |
if (in_array($triggerValue, $specialTagFields)) { |
| 48 |
$specialTagFieldValue[$value->formField] = SmartTags::getSmartTagValue($triggerValue, $data); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
return $specialTagFieldValue; |
| 53 |
} |
| 54 |
|
| 55 |
private function podMappingField($integrationDetails, $podField, $fieldValues, $entryID, $postID) |
| 56 |
{ |
| 57 |
$string_types = [ |
| 58 |
'text', |
| 59 |
'textarea', |
| 60 |
'radio', |
| 61 |
'number', |
| 62 |
'oembed', |
| 63 |
'password', |
| 64 |
'email', |
| 65 |
'pick', |
| 66 |
'paragraph', |
| 67 |
'wysiwyg', |
| 68 |
'code', |
| 69 |
'check', |
| 70 |
'color', |
| 71 |
'website', |
| 72 |
'date', |
| 73 |
'time', |
| 74 |
'datetime', |
| 75 |
'datetime-local', |
| 76 |
'url', |
| 77 |
'currency', |
| 78 |
]; |
| 79 |
|
| 80 |
$fieldValues = IntegrationHandler::formattedRepeaterValue($fieldValues); |
| 81 |
|
| 82 |
$fileUploadHandle = new WpFileHandler($this->_formID); |
| 83 |
$podData = []; |
| 84 |
foreach ($integrationDetails->pod_map as $fieldPair) { |
| 85 |
if (!empty($fieldPair->podFormField) && !empty($fieldPair->formField)) { |
| 86 |
$pod = $podField[$fieldPair->podFormField]; |
| 87 |
if (in_array($pod['type'], $string_types) && !empty($fieldValues[$fieldPair->formField])) { |
| 88 |
if (1 === $pod['is_repeatable'] && is_array($fieldValues[$fieldPair->formField])) { |
| 89 |
// $repeaterValues = call_user_func_array('array_merge', array_map('array_values', $fieldValues[$fieldPair->formField])); |
| 90 |
$podData[$fieldPair->podFormField] = $fieldValues[$fieldPair->formField]; |
| 91 |
} else { |
| 92 |
$podData[$fieldPair->podFormField] = $fieldValues[$fieldPair->formField]; |
| 93 |
} |
| 94 |
} |
| 95 |
// elseif (isset($field['mul'], $field) && in_array($pod['type'], $string_types) && 'file-up' !== $field['type']) { |
| 96 |
// if (true === $field['mul'] && 'multi' === $pod['pick_format_type'] && !empty($fieldValues[$fieldPair->formField])) { |
| 97 |
// $podData[$fieldPair->podFormField] = explode(',', $fieldValues[$fieldPair->formField]); |
| 98 |
// } elseif (false === $field['mul'] && 'multi' === $pod['pick_format_type'] && !empty($fieldValues[$fieldPair->formField])) { |
| 99 |
// $podData[$fieldPair->podFormField] = $fieldValues[$fieldPair->formField]; |
| 100 |
// } |
| 101 |
// } |
| 102 |
elseif ('file' === $pod['type']) { |
| 103 |
if (!empty($fieldValues[$fieldPair->formField])) { |
| 104 |
if ('multi' === $pod['file_format_type']) { |
| 105 |
$attachmentId = $fileUploadHandle->multiFileMoveWpMedia($entryID, $fieldValues[$fieldPair->formField], $postID); |
| 106 |
if (!empty($attachmentId)) { |
| 107 |
update_post_meta($postID, $fieldPair->podFormField, $attachmentId); |
| 108 |
update_post_meta($postID, '_pods_' . $fieldPair->podFormField, wp_json_encode($attachmentId)); |
| 109 |
} |
| 110 |
} elseif ('single' === $pod['file_format_type']) { |
| 111 |
$attachmentData = []; |
| 112 |
if (!is_array($fieldValues[$fieldPair->formField])) { |
| 113 |
$fileArr = explode('/', $fieldValues[$fieldPair->formField]); |
| 114 |
$attachmentData[] = $fileArr[count($fileArr) - 1]; |
| 115 |
} else { |
| 116 |
$attachmentData = $fieldValues[$fieldPair->formField]; |
| 117 |
} |
| 118 |
$attachmentId = $fileUploadHandle->singleFileMoveWpMedia($entryID, $attachmentData, $postID); |
| 119 |
if (!empty($attachmentId)) { |
| 120 |
update_post_meta($postID, $fieldPair->podFormField, $attachmentId); |
| 121 |
update_post_meta($postID, '_pods_' . $fieldPair->podFormField, wp_json_encode($attachmentId)); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
return $podData; |
| 129 |
} |
| 130 |
|
| 131 |
// formatted field values for repeater field |
| 132 |
|
| 133 |
private function postFieldMapping($fieldData, $post_map, $formFields, $fieldValues, $postID, $entryID) |
| 134 |
{ |
| 135 |
$uploadFeatureImg = new WpFileHandler($this->_formID); |
| 136 |
foreach ($post_map as $fieldPair) { |
| 137 |
foreach ($formFields as $field) { |
| 138 |
if (!empty($fieldPair->postFormField) && !empty($fieldPair->formField)) { |
| 139 |
if ($fieldPair->formField === $field['key'] && '_thumbnail_id' !== $fieldPair->postFormField) { |
| 140 |
$fieldData[$fieldPair->postFormField] = $fieldValues[$fieldPair->formField]; |
| 141 |
} elseif ($fieldPair->formField === $field['key'] && in_array($field['type'], self::BIT_FORMS_FILE_TYPE) && '_thumbnail_id' === $fieldPair->postFormField && !empty($fieldValues[$field['key']])) { |
| 142 |
if (!empty($fieldValues[$field['key']])) { |
| 143 |
$uploadFeatureImg->uploadFeatureImg($fieldValues[$field['key']], $entryID, $postID); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
return $fieldData; |
| 150 |
} |
| 151 |
|
| 152 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 153 |
{ |
| 154 |
$integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 155 |
$fieldData = []; |
| 156 |
$taxonomy = new WpFileHandler($integrationData->form_id); |
| 157 |
|
| 158 |
$formManger = FormManager::getInstance($integrationData->form_id); |
| 159 |
$formFields = $formManger->getFields(); |
| 160 |
$allFields = pods($integrationDetails->post_type); |
| 161 |
$podField = []; |
| 162 |
|
| 163 |
foreach ($allFields->fields as $key => $field) { |
| 164 |
$podField[$key]['type'] = $field['type']; |
| 165 |
// $podField[$key]['pick_format_type'] = $field['options']['pick_format_type']; |
| 166 |
// $podField[$key]['file_format_type'] = $field['options']['file_format_type']; |
| 167 |
$podField[$key]['pick_format_type'] = $field['pick_format_type']; |
| 168 |
$podField[$key]['file_format_type'] = $field['file_format_type']; |
| 169 |
$podField[$key]['is_repeatable'] = (int) $field['repeatable']; |
| 170 |
} |
| 171 |
|
| 172 |
$fieldData['comment_status'] = isset($integrationDetails->comment_status) ? $integrationDetails->comment_status : ''; |
| 173 |
$fieldData['post_status'] = isset($integrationDetails->post_status) ? $integrationDetails->post_status : ''; |
| 174 |
$fieldData['post_type'] = isset($integrationDetails->post_type) ? $integrationDetails->post_type : ''; |
| 175 |
|
| 176 |
if (isset($integrationDetails->post_author) && 'logged_in_user' !== $integrationDetails->post_author) { |
| 177 |
$fieldData['post_author'] = $integrationDetails->post_author; |
| 178 |
} else { |
| 179 |
$fieldData['post_author'] = get_current_user_id(); |
| 180 |
} |
| 181 |
|
| 182 |
$exist_id = $fieldData['post_type'] . '_' . $entryID; |
| 183 |
$exist_post_id = $this->_wpdb->get_results( |
| 184 |
$this->_wpdb->prepare( |
| 185 |
"SELECT * FROM `{$this->_wpdb->prefix}bitforms_form_entrymeta` WHERE `meta_key`=%s", |
| 186 |
$exist_id |
| 187 |
) |
| 188 |
); |
| 189 |
$taxanomyData = $taxonomy->taxonomyData($formFields, $fieldValues); |
| 190 |
|
| 191 |
if ([] === $exist_post_id) { |
| 192 |
$post_id = wp_insert_post(['post_title' => 'null', 'post_content' => 'null']); |
| 193 |
$smartTagValue = $this->smartTagMappingValue($integrationDetails->post_map); |
| 194 |
$updatedValues = $fieldValues + $smartTagValue; |
| 195 |
$updateData = $this->postFieldMapping($fieldData, $integrationDetails->post_map, $formFields, $updatedValues, $post_id, $entryID); |
| 196 |
$updateData['ID'] = $post_id; |
| 197 |
unset($updateData['_thumbnail_id']); |
| 198 |
wp_update_post($updateData, true); |
| 199 |
|
| 200 |
if (!empty($taxanomyData)) { |
| 201 |
foreach ($taxanomyData as $taxanomy) { |
| 202 |
wp_set_post_terms($post_id, $taxanomy['value'], $taxanomy['term'], false); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
// Form entry meta insert; meta_key/meta_value required to map dynamic field data to Pods integration record. |
| 207 |
$this->_wpdb->insert( |
| 208 |
"{$this->_wpdb->prefix}bitforms_form_entrymeta", |
| 209 |
[ |
| 210 |
'meta_key' => $fieldData['post_type'] . '_' . $entryID, |
| 211 |
'meta_value' => $post_id, |
| 212 |
'bitforms_form_entry_id' => $entryID, |
| 213 |
] |
| 214 |
); |
| 215 |
|
| 216 |
$smartTagValue = $this->smartTagMappingValue($integrationDetails->pod_map); |
| 217 |
$updatedPodValues = $fieldValues + $smartTagValue; |
| 218 |
$podData = $this->podMappingField($integrationDetails, $podField, $updatedPodValues, $entryID, $post_id); |
| 219 |
foreach ($podData as $key => $data) { |
| 220 |
add_post_meta($post_id, $key, $data); |
| 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 |
|
| 229 |
$podData = $this->podMappingField($integrationDetails, $podField, $fieldValues, $entryID, $exist_post_id[0]->meta_value); |
| 230 |
|
| 231 |
foreach ($podData as $key => $data) { |
| 232 |
if (is_array($data)) { |
| 233 |
$count = count($data); |
| 234 |
for ($i = 0; $i < $count; $i++) { |
| 235 |
delete_post_meta($exist_post_id[0]->meta_value, $key); |
| 236 |
} |
| 237 |
add_post_meta($exist_post_id[0]->meta_value, $key, $data); |
| 238 |
} else { |
| 239 |
update_post_meta($exist_post_id[0]->meta_value, $key, $data); |
| 240 |
} |
| 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 |
return $exist_post_id; |
| 248 |
} |
| 249 |
} |
| 250 |
|