← All changes
|
includes/Core/Integration/MetaBox/MetaBoxHandler.php
+173
-54
3.1.2
→
2.9.1
View file →
| @@ -6,24 +6,23 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace BitCode\BitForm\Core\Integration\MetaBox; |
| 9 | 9 | |
| 10 | +// use BitCode\BitFormPro\Core\Util\WpFileHandler; | |
| 10 | 11 | use BitCode\BitForm\Core\Form\FormManager; |
| 11 | 12 | use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 12 | -use BitCode\BitForm\Core\Util\PostHelpers; | |
| 13 | 13 | use BitCode\BitForm\Core\Util\SmartTags; |
| 14 | 14 | use BitCode\BitForm\Core\Util\WpFileHandler; |
| 15 | 15 | |
| 16 | +/** | |
| 17 | + * Provide functionality for POST CREATEION WITH MetaBox | |
| 18 | + */ | |
| 16 | 19 | class MetaBoxHandler |
| 17 | 20 | { |
| 18 | - use PostHelpers; | |
| 19 | - | |
| 20 | 21 | private $_formID; |
| 21 | 22 | |
| 22 | 23 | private $_wpdb; |
| 23 | 24 | |
| 24 | - private const UPLOAD_FIELD = ['file-up', 'advanced-file-up']; | |
| 25 | - | |
| 26 | 25 | public function __construct($integrationID, $fromID) |
| 27 | 26 | { |
| 28 | 27 | $this->_formID = $fromID; |
| 29 | 28 | |
| @@ -30,8 +29,13 @@ | ||
| 30 | 29 | global $wpdb; |
| 31 | 30 | $this->_wpdb = $wpdb; |
| 32 | 31 | } |
| 33 | 32 | |
| 33 | + /** | |
| 34 | + * Helps to register ajax function's with wp | |
| 35 | + * | |
| 36 | + * @return null | |
| 37 | + */ | |
| 34 | 38 | private function smartTagMappingValue($fieldMap) |
| 35 | 39 | { |
| 36 | 40 | $specialTagFieldValue = []; |
| 37 | 41 | $data = SmartTags::getPostUserData(true); |
| @@ -47,59 +51,178 @@ | ||
| 47 | 51 | } |
| 48 | 52 | return $specialTagFieldValue; |
| 49 | 53 | } |
| 50 | 54 | |
| 51 | - public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) | |
| 55 | + private function postFieldMapping($fieldData, $post_map, $formFields, $fieldValues, $postID, $entryID) | |
| 52 | 56 | { |
| 53 | - $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; | |
| 57 | + $uploadFeatureImg = new WpFileHandler($this->_formID); | |
| 58 | + foreach ($post_map as $fieldPair) { | |
| 59 | + foreach ($formFields as $field) { | |
| 60 | + if (!empty($fieldPair->postField) && !empty($fieldPair->formField)) { | |
| 61 | + if ('_thumbnail_id' !== $fieldPair->postField) { | |
| 62 | + if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) { | |
| 63 | + $fieldData[$fieldPair->postField] = $fieldPair->customValue; | |
| 64 | + } else { | |
| 65 | + $fieldData[$fieldPair->postField] = $fieldValues[$fieldPair->formField]; | |
| 66 | + } | |
| 67 | + } elseif ($fieldPair->formField === $field['key'] && 'file-up' === $field['type'] && '_thumbnail_id' === $fieldPair->postField) { | |
| 68 | + if (!empty($fieldValues[$field['key']])) { | |
| 69 | + $uploadFeatureImg->uploadFeatureImg($fieldValues[$field['key']], $entryID, $postID); | |
| 70 | + } | |
| 71 | + } | |
| 72 | + } | |
| 73 | + } | |
| 74 | + } | |
| 75 | + return $fieldData; | |
| 76 | + } | |
| 54 | 77 | |
| 55 | - $formManger = FormManager::getInstance($integrationData->form_id); | |
| 78 | + private function getMetaboxFields($postType) | |
| 79 | + { | |
| 80 | + $metaboxFields = []; | |
| 81 | + $filterTypes = [ | |
| 82 | + 'file_input', | |
| 83 | + 'group', | |
| 84 | + 'tab', | |
| 85 | + 'osm', | |
| 86 | + 'heading', | |
| 87 | + 'key_value', | |
| 88 | + 'map', | |
| 89 | + 'custom_html', | |
| 90 | + 'background', | |
| 91 | + 'fieldset_text', | |
| 92 | + ]; | |
| 56 | 93 | |
| 57 | - $formFields = $formManger->getFields(); | |
| 94 | + if (function_exists('rwmb_meta')) { | |
| 95 | + foreach (rwmb_get_object_fields($postType) as $index => $field) { | |
| 96 | + if (!in_array($field['type'], $filterTypes)) { | |
| 97 | + $metaboxFields[$index]['name'] = $field['name']; | |
| 98 | + $metaboxFields[$index]['key'] = $field['id']; | |
| 99 | + $metaboxFields[$index]['required'] = $field['required']; | |
| 100 | + $metaboxFields[$index]['type'] = $field['type']; | |
| 101 | + $metaboxFields[$index]['multiple'] = $field['multiple']; | |
| 102 | + } | |
| 103 | + } | |
| 104 | + } | |
| 58 | 105 | |
| 59 | - $postData = $this->setPostData($integrationDetails); | |
| 106 | + return $metaboxFields; | |
| 107 | + } | |
| 60 | 108 | |
| 61 | - $id = $postData['post_type'] . '_' . $entryID; | |
| 109 | + private function metaboxFieldMapping($metaboxMapField, $fieldValues, $metaboxFields, $entryID, $postId, $action) | |
| 110 | + { | |
| 111 | + $metaboxFieldData = []; | |
| 112 | + $stringTypes = ['text', 'checkbox', 'switch', 'textarea', 'password', 'wysiwyg', 'number', 'radio', 'color', 'oembed', 'email', 'url', 'date', 'true_false', 'date_time_picker', 'time_picker', 'message', 'tel', 'datetime-local', 'time', 'datetime']; | |
| 113 | + $selectedTypes = ['select', 'checkbox_list', 'select_advanced', 'user', 'post']; | |
| 114 | + foreach ($metaboxMapField as $key => $fieldPair) { | |
| 115 | + foreach ($metaboxFields as $metabox) { | |
| 116 | + if (property_exists($fieldPair, 'metaboxField')) { | |
| 117 | + if ($metabox['key'] === $fieldPair->metaboxField && in_array($metabox['type'], $stringTypes)) { | |
| 118 | + $metaboxFieldData[$key]['name'] = $metabox['key']; | |
| 119 | + if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) { | |
| 120 | + $metaboxFieldData[$key]['value'] = $fieldPair->customValue; | |
| 121 | + } elseif ('custom' !== $fieldPair->formField && !empty($fieldValues[$fieldPair->formField])) { | |
| 122 | + $metaboxFieldData[$key]['value'] = $fieldValues[$fieldPair->formField]; | |
| 123 | + } | |
| 124 | + } elseif ($metabox['key'] === $fieldPair->metaboxField && in_array($metabox['type'], $selectedTypes) && !empty($fieldValues[$fieldPair->formField])) { | |
| 125 | + $fieldData = $fieldValues[$fieldPair->formField]; | |
| 126 | + $values = !is_array($fieldData) ? explode(',', $fieldData) : $fieldData; | |
| 62 | 127 | |
| 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 | - ); | |
| 128 | + $getMetaValues = get_post_meta($postId, $metabox['key'], false); | |
| 129 | + if (true === $metabox['multiple'] && is_array($values)) { | |
| 130 | + if ('update' === $action && count($getMetaValues) > 0) { | |
| 131 | + foreach ($getMetaValues as $value) { | |
| 132 | + delete_post_meta($postId, $metabox['key']); | |
| 133 | + } | |
| 134 | + } | |
| 135 | + foreach ($values as $value) { | |
| 136 | + add_post_meta($postId, $metabox['key'], $value); | |
| 137 | + } | |
| 138 | + } else { | |
| 139 | + $metaboxFieldData[$key]['name'] = $metabox['key']; | |
| 140 | + $metaboxFieldData[$key]['value'] = $fieldValues[$fieldPair->formField]; | |
| 141 | + } | |
| 142 | + } | |
| 143 | + } | |
| 144 | + } | |
| 145 | + } | |
| 146 | + return $metaboxFieldData; | |
| 147 | + } | |
| 69 | 148 | |
| 70 | - $metaBoxFields = rwmb_get_object_fields($postData['post_type']); | |
| 149 | + public function fileUploadMapping($metaboxMapField, $fieldValues, $metaboxFields, $entryID, $postId) | |
| 150 | + { | |
| 151 | + $fileUploadHandle = new WpFileHandler($this->_formID); | |
| 71 | 152 | |
| 72 | - $taxonomies = (new WpFileHandler($integrationData->form_id))->taxonomyData($formFields, $fieldValues); | |
| 153 | + foreach ($metaboxMapField as $key => $fieldPair) { | |
| 154 | + foreach ($metaboxFields as $metabox) { | |
| 155 | + if (property_exists($fieldPair, 'metaboxFileUpload')) { | |
| 156 | + if ($metabox['key'] === $fieldPair->metaboxFileUpload && !empty($fieldValues[$fieldPair->formField])) { | |
| 157 | + if (false === $metabox['multiple']) { | |
| 158 | + $attachMentId = $fileUploadHandle->singleFileMoveWpMedia($entryID, $fieldValues[$fieldPair->formField], $postId); | |
| 73 | 159 | |
| 74 | - if ([] === $existPostId) { | |
| 75 | - $postId = wp_insert_post(['post_title' => '(no title)', 'post_content' => '']); | |
| 160 | + if (!empty($attachMentId)) { | |
| 161 | + add_post_meta($postId, $metabox['key'], $attachMentId); | |
| 162 | + } | |
| 163 | + } elseif (true === $metabox['multiple']) { | |
| 164 | + $attachMentId = $fileUploadHandle->multiFileMoveWpMedia($entryID, $fieldValues[$fieldPair->formField], $postId); | |
| 165 | + //$exists = metadata_exists('post', $postId, $metabox['key']); | |
| 166 | + if (!empty($attachMentId) && is_array($attachMentId)) { | |
| 167 | + foreach ($attachMentId as $attachemnt) { | |
| 168 | + add_post_meta($postId, $metabox['key'], $attachemnt); | |
| 169 | + } | |
| 170 | + } | |
| 171 | + } | |
| 172 | + } | |
| 173 | + } | |
| 174 | + } | |
| 175 | + } | |
| 176 | + } | |
| 76 | 177 | |
| 77 | - $smartTagValue = $this->smartTagMappingValue($integrationDetails->post_map); | |
| 178 | + public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) | |
| 179 | + { | |
| 180 | + $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; | |
| 181 | + $taxonomy = new WpFileHandler($integrationData->form_id); | |
| 182 | + $formManger = new FormManager($integrationData->form_id); | |
| 183 | + $formFields = $formManger->getFields(); | |
| 78 | 184 | |
| 79 | - $updatedValues = $fieldValues + $smartTagValue; | |
| 185 | + $fieldData = []; | |
| 80 | 186 | |
| 81 | - $postMappedData = $this->postFieldMapping($integrationDetails->post_map, $updatedValues, $postId, $entryID); | |
| 187 | + $fieldData['comment_status'] = isset($integrationDetails->comment_status) ? $integrationDetails->comment_status : ''; | |
| 188 | + $fieldData['post_status'] = isset($integrationDetails->post_status) ? $integrationDetails->post_status : ''; | |
| 189 | + $fieldData['post_type'] = isset($integrationDetails->post_type) ? $integrationDetails->post_type : ''; | |
| 190 | + if (isset($integrationDetails->post_author) && 'logged_in_user' !== $integrationDetails->post_author) { | |
| 191 | + $fieldData['post_author'] = $integrationDetails->post_author; | |
| 192 | + } else { | |
| 193 | + $fieldData['post_author'] = get_current_user_id(); | |
| 194 | + } | |
| 82 | 195 | |
| 83 | - $updatedData = $postData + $postMappedData; | |
| 196 | + $exist_id = $fieldData['post_type'] . '_' . $entryID; | |
| 197 | + $sql = "SELECT * FROM `{$this->_wpdb->prefix}bitforms_form_entrymeta` WHERE `meta_key`='$exist_id' "; | |
| 198 | + $existPostId = $this->_wpdb->get_results($sql); | |
| 199 | + $postId = ''; | |
| 200 | + $metaboxFields = $this->getMetaboxFields($fieldData['post_type']); | |
| 84 | 201 | |
| 85 | - $updatedData['ID'] = $postId; | |
| 202 | + $taxanomyData = $taxonomy->taxonomyData($formFields, $fieldValues); | |
| 86 | 203 | |
| 87 | - unset($updatedData['_thumbnail_id']); | |
| 204 | + if ([] === $existPostId) { | |
| 205 | + $postId = wp_insert_post(['post_title' => '(no title)', 'post_content' => '']); | |
| 88 | 206 | |
| 89 | - wp_update_post($updatedData, true); | |
| 207 | + $smartTagValue = $this->smartTagMappingValue($integrationDetails->post_map); | |
| 208 | + $updatedValues = $fieldValues + $smartTagValue; | |
| 209 | + $updateData = $this->postFieldMapping($fieldData, $integrationDetails->post_map, $formFields, $updatedValues, $postId, $entryID); | |
| 90 | 210 | |
| 91 | - if (!empty($taxonomies)) { | |
| 92 | - foreach ($taxonomies as $taxonomy) { | |
| 93 | - wp_set_post_terms($postId, $taxonomy['value'], $taxonomy['term'], false); | |
| 211 | + $updateData['ID'] = $postId; | |
| 212 | + unset($updateData['_thumbnail_id']); | |
| 213 | + | |
| 214 | + wp_update_post($updateData, true); | |
| 215 | + if (!empty($taxanomyData)) { | |
| 216 | + foreach ($taxanomyData as $taxanomy) { | |
| 217 | + wp_set_post_terms($postId, $taxanomy['value'], $taxanomy['term'], false); | |
| 94 | 218 | } |
| 95 | 219 | } |
| 96 | 220 | |
| 97 | - // Form entry meta insert; meta_key/meta_value required to map dynamic field data to MetaBox integration record. | |
| 98 | 221 | $this->_wpdb->insert( |
| 99 | 222 | "{$this->_wpdb->prefix}bitforms_form_entrymeta", |
| 100 | 223 | [ |
| 101 | - 'meta_key' => $postData['post_type'] . '_' . $entryID, | |
| 224 | + 'meta_key' => $fieldData['post_type'] . '_' . $entryID, | |
| 102 | 225 | 'meta_value' => $postId, |
| 103 | 226 | 'bitforms_form_entry_id' => $entryID, |
| 104 | 227 | ] |
| 105 | 228 | ); |
| @@ -104,35 +227,31 @@ | ||
| 104 | 227 | ] |
| 105 | 228 | ); |
| 106 | 229 | |
| 107 | 230 | $smartTagValue = $this->smartTagMappingValue($integrationDetails->metabox_map); |
| 108 | - | |
| 109 | 231 | $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); | |
| 232 | + $metaboxFieldMapping = $this->metaboxFieldMapping($integrationDetails->metabox_map, $updatedMbValues, $metaboxFields, $entryID, $postId, 'create'); | |
| 233 | + $this->fileUploadMapping($integrationDetails->metabox_file_map, $fieldValues, $metaboxFields, $entryID, $postId, 'create'); | |
| 234 | + foreach ($metaboxFieldMapping as $data) { | |
| 235 | + if (isset($data['name'], $data['value'])) { | |
| 236 | + add_post_meta($postId, $data['name'], $data['value']); | |
| 237 | + } | |
| 117 | 238 | } |
| 118 | 239 | } else { |
| 119 | - if (!empty($taxonomies)) { | |
| 120 | - foreach ($taxonomies as $taxonomy) { | |
| 121 | - wp_set_post_terms($existPostId[0]->meta_value, $taxonomy['value'], $taxonomy['term'], false); | |
| 240 | + if (!empty($taxanomyData)) { | |
| 241 | + foreach ($taxanomyData as $taxanomy) { | |
| 242 | + wp_set_post_terms($existPostId[0]->meta_value, $taxanomy['value'], $taxanomy['term'], false); | |
| 122 | 243 | } |
| 123 | 244 | } |
| 245 | + $metaboxFieldMapping = $this->metaboxFieldMapping($integrationDetails->metabox_map, $fieldValues, $metaboxFields, $entryID, $existPostId[0]->meta_value, 'update'); | |
| 124 | 246 | |
| 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); | |
| 247 | + foreach ($metaboxFieldMapping as $data) { | |
| 248 | + if (isset($data['name'], $data['value'])) { | |
| 249 | + update_post_meta($existPostId[0]->meta_value, $data['name'], $data['value']); | |
| 250 | + } | |
| 129 | 251 | } |
| 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); | |
| 252 | + $updateData = $this->postFieldMapping($fieldData, $integrationDetails->post_map, $formFields, $fieldValues, $existPostId[0]->meta_value, $entryID); | |
| 253 | + $updateData['ID'] = $existPostId[0]->meta_value; | |
| 254 | + wp_update_post($updateData, true); | |
| 136 | 255 | } |
| 137 | 256 | } |
| 138 | 257 | } |