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