| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
*POST CREATEION WITH Metabox |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\MetaBox; |
| 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 MetaBoxHandler |
| 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 |
|
| 30 |
global $wpdb; |
| 31 |
$this->_wpdb = $wpdb; |
| 32 |
} |
| 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 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 52 |
{ |
| 53 |
$integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 54 |
|
| 55 |
$formManger = FormManager::getInstance($integrationData->form_id); |
| 56 |
|
| 57 |
$formFields = $formManger->getFields(); |
| 58 |
|
| 59 |
$postData = $this->setPostData($integrationDetails); |
| 60 |
|
| 61 |
$id = $postData['post_type'] . '_' . $entryID; |
| 62 |
|
| 63 |
$existPostId = $this->_wpdb->get_results( |
| 64 |
$this->_wpdb->prepare( |
| 65 |
"SELECT * FROM `{$this->_wpdb->prefix}bitforms_form_entrymeta` WHERE `meta_key`=%s", |
| 66 |
$id |
| 67 |
) |
| 68 |
); |
| 69 |
|
| 70 |
$metaBoxFields = rwmb_get_object_fields($postData['post_type']); |
| 71 |
|
| 72 |
$taxonomies = (new WpFileHandler($integrationData->form_id))->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 MetaBox 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->metabox_map); |
| 108 |
|
| 109 |
$updatedMbValues = $fieldValues + $smartTagValue; |
| 110 |
|
| 111 |
$metaBoxMappedData = MetaBoxHelper::metaBoxFieldMapping($integrationDetails->metabox_map, $updatedMbValues, $metaBoxFields, $postId, 'create'); |
| 112 |
|
| 113 |
MetaBoxHelper::fileUploadMapping($integrationDetails->metabox_file_map, $fieldValues, $metaBoxFields, $this->_formID, $entryID, $postId); |
| 114 |
|
| 115 |
foreach ($metaBoxMappedData as $key => $value) { |
| 116 |
add_post_meta($postId, $key, $value); |
| 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 |
$metaBoxMappedData = MetaBoxHelper::metaBoxFieldMapping($integrationDetails->metabox_map, $fieldValues, $metaBoxFields, $existPostId[0]->meta_value, 'update'); |
| 126 |
|
| 127 |
foreach ($metaBoxMappedData as $key => $value) { |
| 128 |
update_post_meta($existPostId[0]->meta_value, $key, $value); |
| 129 |
} |
| 130 |
|
| 131 |
$updatedData = $this->postFieldMapping($postData, $integrationDetails->post_map, $fieldValues, $existPostId[0]->meta_value, $entryID); |
| 132 |
|
| 133 |
$updatedData['ID'] = $existPostId[0]->meta_value; |
| 134 |
|
| 135 |
wp_update_post($updatedData, true); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|