| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* WooCommerce Record Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\WooCommerce; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Database\FormEntryLogModel; |
| 11 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 12 |
use WC_Product_Download; |
| 13 |
use WP_Error; |
| 14 |
|
| 15 |
/** |
| 16 |
* Provide functionality for Record insert,upsert |
| 17 |
*/ |
| 18 |
class RecordApiHelper |
| 19 |
{ |
| 20 |
private $_integrationID; |
| 21 |
|
| 22 |
private $_logID; |
| 23 |
|
| 24 |
private $_logResponse; |
| 25 |
|
| 26 |
public function __construct($integId, $logID) |
| 27 |
{ |
| 28 |
$this->_integrationID = $integId; |
| 29 |
$this->_logID = $logID; |
| 30 |
$this->_logResponse = new UtilApiResponse(); |
| 31 |
} |
| 32 |
|
| 33 |
public function executeRecordApi($formID, $entryID, $module, $fieldValues, $fieldMap, $uploadFieldMap, $required) |
| 34 |
{ |
| 35 |
$fieldData = []; |
| 36 |
foreach ($fieldMap as $fieldPair) { |
| 37 |
if (!empty($fieldPair->wcField) && !empty($fieldPair->formField)) { |
| 38 |
if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) { |
| 39 |
$fieldData[$fieldPair->wcField] = $fieldPair->customValue; |
| 40 |
} else { |
| 41 |
$fieldData[$fieldPair->wcField] = $fieldValues[$fieldPair->formField]; |
| 42 |
} |
| 43 |
|
| 44 |
if (in_array($fieldPair->wcField, $required) && empty($fieldValues[$fieldPair->formField])) { |
| 45 |
$error = new WP_Error('REQ_FIELD_EMPTY', wp_sprintf(__('%s is required for woocommerce %s', 'bit-form'), $fieldPair->wcField, $module)); |
| 46 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => $module, 'type_name' => 'create'], 'validation', $error); |
| 47 |
return $error; |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
$model = new FormEntryLogModel(); |
| 53 |
$result = $model->entryLogCheck($entryID, $this->_integrationID); |
| 54 |
|
| 55 |
$entry_type = 'create'; |
| 56 |
|
| 57 |
if (count($result) && !isset($result->errors['result_empty'])) { |
| 58 |
$entry_type = 'edit'; |
| 59 |
$api_type = json_decode($result[0]->api_type); |
| 60 |
|
| 61 |
if ($api_type->type === $module) { |
| 62 |
$id = $result[0]->response_obj; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
if ('product' === $module) { |
| 67 |
if (!empty($fieldData['tags_input'])) { |
| 68 |
$tags = explode(',', $fieldData['tags_input']); |
| 69 |
unset($fieldData['tags_input']); |
| 70 |
} |
| 71 |
|
| 72 |
if (!empty($fieldData['post_category'])) { |
| 73 |
$categories = explode(',', $fieldData['post_category']); |
| 74 |
unset($fieldData['post_category']); |
| 75 |
} |
| 76 |
|
| 77 |
if (!empty($fieldData['_regular_price'])) { |
| 78 |
$price = $fieldData['_regular_price']; |
| 79 |
} |
| 80 |
|
| 81 |
if (!empty($fieldData['_sale_price'])) { |
| 82 |
$price = $fieldData['_sale_price']; |
| 83 |
} |
| 84 |
|
| 85 |
if (!empty($fieldData['product_type'])) { |
| 86 |
$product_type = $fieldData['product_type']; |
| 87 |
if ('external' === $product_type && !empty($fieldData['_product_url'])) { |
| 88 |
$product_type = 'external'; |
| 89 |
} else { |
| 90 |
$product_type = 'simple'; |
| 91 |
unset($fieldData['_product_url'], $fieldData['_button_text']); |
| 92 |
} |
| 93 |
unset($fieldData['product_type']); |
| 94 |
} |
| 95 |
|
| 96 |
$post_fields = [ |
| 97 |
'post_content', 'post_title', 'post_status', 'post_type', 'comment_status', 'post_password', 'menu_order', 'post_excerpt', 'post_date', 'post_date_gmt' |
| 98 |
]; |
| 99 |
|
| 100 |
$post_inputs = array_intersect_key($fieldData, array_flip($post_fields)); |
| 101 |
$meta_inputs = array_diff_key($fieldData, array_flip($post_fields)); |
| 102 |
|
| 103 |
$fieldData = $post_inputs; |
| 104 |
$fieldData['post_type'] = $module; |
| 105 |
$fieldData['meta_input'] = $meta_inputs; |
| 106 |
|
| 107 |
if (!empty($fieldData['post_date']) || !empty($fieldData['post_date_gmt'])) { |
| 108 |
$fieldData['post_status'] = 'future'; |
| 109 |
} |
| 110 |
|
| 111 |
if (isset($id)) { |
| 112 |
$fieldData['ID'] = $id; |
| 113 |
} |
| 114 |
|
| 115 |
$product_id = wp_insert_post($fieldData); |
| 116 |
|
| 117 |
if (isset($product_type)) { |
| 118 |
wp_set_object_terms($product_id, $product_type, 'product_type'); |
| 119 |
} |
| 120 |
|
| 121 |
if (isset($price)) { |
| 122 |
update_post_meta($product_id, '_price', $price); |
| 123 |
} |
| 124 |
|
| 125 |
if (isset($categories)) { |
| 126 |
wp_set_object_terms($product_id, $categories, 'product_cat'); |
| 127 |
} |
| 128 |
|
| 129 |
if (isset($tags)) { |
| 130 |
wp_set_object_terms($product_id, $tags, 'product_tag'); |
| 131 |
} |
| 132 |
|
| 133 |
if (is_wp_error($product_id) || !$product_id) { |
| 134 |
$response = is_wp_error($product_id) ? $product_id->get_error_message() : 'error'; |
| 135 |
return $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'product', 'type_name' => $entry_type], 'error', $response); |
| 136 |
} else { |
| 137 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'product', 'type_name' => $entry_type], 'success', $product_id); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
if ('customer' === $module) { |
| 142 |
$user_fields = ['user_pass', 'user_login', 'user_nicename', 'user_url', 'user_email', 'display_name', 'nickname', 'first_name', 'last_name', 'description', 'locale']; |
| 143 |
|
| 144 |
$user_inputs = array_intersect_key($fieldData, array_flip($user_fields)); |
| 145 |
$meta_inputs = array_diff_key($fieldData, array_flip($user_fields)); |
| 146 |
|
| 147 |
$fieldData = $user_inputs; |
| 148 |
$fieldData['role'] = $module; |
| 149 |
|
| 150 |
if (isset($id)) { |
| 151 |
$fieldData['ID'] = $id; |
| 152 |
} |
| 153 |
|
| 154 |
$user_id = wp_insert_user($fieldData); |
| 155 |
|
| 156 |
if (is_wp_error($user_id) || !$user_id) { |
| 157 |
$response = is_wp_error($user_id) ? $user_id->get_error_message() : 'error'; |
| 158 |
return $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'customer', 'type_name' => $entry_type], 'error', $response); |
| 159 |
} else { |
| 160 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'customer', 'type_name' => $entry_type], 'success', $user_id); |
| 161 |
} |
| 162 |
|
| 163 |
foreach ($meta_inputs as $metaKey => $metaValue) { |
| 164 |
update_user_meta($user_id, $metaKey, $metaValue); |
| 165 |
} |
| 166 |
|
| 167 |
do_action('woocommerce_update_customer', $user_id); |
| 168 |
} |
| 169 |
|
| 170 |
$flag = null; |
| 171 |
if (isset($product_id)) { |
| 172 |
$basepath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR; |
| 173 |
|
| 174 |
foreach ($uploadFieldMap as $uploadField) { |
| 175 |
if (!empty($uploadField->formField) && !empty($uploadField->wcField)) { |
| 176 |
if ('product_image' === $uploadField->wcField) { |
| 177 |
$flag = 0; |
| 178 |
} |
| 179 |
if ('product_gallery' === $uploadField->wcField) { |
| 180 |
$flag = 1; |
| 181 |
} |
| 182 |
if ('downloadable_files' === $uploadField->wcField) { |
| 183 |
$flag = 2; |
| 184 |
} |
| 185 |
|
| 186 |
$attach_ids = ''; |
| 187 |
|
| 188 |
if (!empty($fieldValues[$uploadField->formField])) { |
| 189 |
$uplaodFiles = $fieldValues[$uploadField->formField]; |
| 190 |
if ('string' === gettype($fieldValues[$uploadField->formField])) { |
| 191 |
$uplaodFiles = json_decode($fieldValues[$uploadField->formField]); |
| 192 |
} |
| 193 |
if (is_array($uplaodFiles)) { |
| 194 |
foreach ($uplaodFiles as $singleFile) { |
| 195 |
$url = $basepath . $singleFile; |
| 196 |
$attach_id = $this->attach_product_attachments($product_id, $flag, $url, $singleFile); |
| 197 |
if (1 === $flag && $attach_id) { |
| 198 |
$attach_ids .= ',' . $attach_id; |
| 199 |
} |
| 200 |
} |
| 201 |
} else { |
| 202 |
$filename = $uplaodFiles; |
| 203 |
$url = $basepath . $filename; |
| 204 |
$this->attach_product_attachments($product_id, $flag, $url, $filename); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
if (1 === $flag) { |
| 209 |
update_post_meta($product_id, '_product_image_gallery', $attach_ids); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
public function upload_attachment($product_id, $url) |
| 217 |
{ |
| 218 |
require_once ABSPATH . 'wp-admin/includes/image.php'; |
| 219 |
|
| 220 |
$image_url = $url; |
| 221 |
$url_array = explode('/', $url); |
| 222 |
$image_name = $url_array[count($url_array) - 1]; |
| 223 |
$image_data = file_get_contents($image_url); |
| 224 |
|
| 225 |
$upload_dir = wp_upload_dir(); |
| 226 |
$unique_file_name = wp_unique_filename($upload_dir['path'], $image_name); |
| 227 |
$filename = basename($unique_file_name); |
| 228 |
|
| 229 |
if (wp_mkdir_p($upload_dir['path'])) { |
| 230 |
$file = $upload_dir['path'] . '/' . $filename; |
| 231 |
} else { |
| 232 |
$file = $upload_dir['basedir'] . '/' . $filename; |
| 233 |
} |
| 234 |
file_put_contents($file, $image_data); |
| 235 |
|
| 236 |
$wp_filetype = wp_check_filetype($filename, null); |
| 237 |
|
| 238 |
$attachment = [ |
| 239 |
'post_mime_type' => $wp_filetype['type'], |
| 240 |
'post_title' => sanitize_file_name($filename), |
| 241 |
'post_content' => '', |
| 242 |
'post_status' => 'inherit' |
| 243 |
]; |
| 244 |
|
| 245 |
$attach_id = wp_insert_attachment($attachment, $file, $product_id); |
| 246 |
|
| 247 |
$attach_data = wp_generate_attachment_metadata($attach_id, $file); |
| 248 |
|
| 249 |
wp_update_attachment_metadata($attach_id, $attach_data); |
| 250 |
|
| 251 |
return $attach_id; |
| 252 |
} |
| 253 |
|
| 254 |
public function attach_product_attachments($product_id, $flag, $url, $filename) |
| 255 |
{ |
| 256 |
$attach_id = $this->upload_attachment($product_id, $url); |
| 257 |
if (0 === $flag) { |
| 258 |
set_post_thumbnail($product_id, $attach_id); |
| 259 |
} |
| 260 |
|
| 261 |
if (1 === $flag) { |
| 262 |
return $attach_id; |
| 263 |
} |
| 264 |
|
| 265 |
if (2 === $flag) { |
| 266 |
$this->attach_downloadable_attachments($product_id, $url, $filename); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
public function attach_downloadable_attachments($product_id, $url, $filename) |
| 271 |
{ |
| 272 |
if ('yes' !== get_post_meta($product_id, '_downloadable', true)) { |
| 273 |
return false; |
| 274 |
} |
| 275 |
|
| 276 |
require_once dirname(WC_PLUGIN_FILE) . '/includes/wc-product-functions.php'; |
| 277 |
|
| 278 |
$attach_id = $this->upload_attachment($product_id, $url); |
| 279 |
$download_id = md5($url); |
| 280 |
$file_url = wp_get_attachment_url($attach_id); |
| 281 |
|
| 282 |
$pd_object = new WC_Product_Download(); |
| 283 |
$pd_object->set_id($download_id); |
| 284 |
$pd_object->set_name($filename); |
| 285 |
$pd_object->set_file($file_url); |
| 286 |
|
| 287 |
$product = wc_get_product($product_id); |
| 288 |
|
| 289 |
$downloads = $product->get_downloads(); |
| 290 |
$downloads[$download_id] = $pd_object; |
| 291 |
|
| 292 |
$product->set_downloads($downloads); |
| 293 |
$product->save(); |
| 294 |
} |
| 295 |
} |
| 296 |
|