| 1 |
<?php |
| 2 |
/****************************************************************************************** |
| 3 |
* Copyright (C) Smackcoders. - All Rights Reserved under Smackcoders Proprietary License |
| 4 |
* Unauthorized copying of this file, via any medium is strictly prohibited |
| 5 |
* Proprietary and confidential |
| 6 |
* You can contact Smackcoders at email address info@smackcoders.com. |
| 7 |
*******************************************************************************************/ |
| 8 |
|
| 9 |
namespace Smackcoders\SMUSERS; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) |
| 12 |
exit; // Exit if accessed directly |
| 13 |
|
| 14 |
class ImportHelpers { |
| 15 |
private static $helpers_instance = null; |
| 16 |
|
| 17 |
public static function getInstance() { |
| 18 |
|
| 19 |
if (ImportHelpers::$helpers_instance == null) { |
| 20 |
ImportHelpers::$helpers_instance = new ImportHelpers; |
| 21 |
return ImportHelpers::$helpers_instance; |
| 22 |
} |
| 23 |
return ImportHelpers::$helpers_instance; |
| 24 |
} |
| 25 |
|
| 26 |
public function get_requested_term_details ($post_id, $term) { |
| 27 |
$termLen = strlen($term); |
| 28 |
$checktermid = intval($term); |
| 29 |
$verifiedTermLen = strlen($checktermid); |
| 30 |
if($termLen == $verifiedTermLen) { |
| 31 |
return $term; |
| 32 |
} else { |
| 33 |
$reg_term_id = wp_set_object_terms($post_id, $term, 'category'); |
| 34 |
if(isset($reg_term_id[0])){ |
| 35 |
$term_id = $reg_term_id[0]; |
| 36 |
} |
| 37 |
return $term_id; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
public function get_from_user_details($request_user) { |
| 42 |
global $wpdb; |
| 43 |
$authorLen = strlen($request_user); |
| 44 |
$checkpostuserid = intval($request_user); |
| 45 |
$postAuthorLen = strlen($checkpostuserid); |
| 46 |
|
| 47 |
if ($authorLen == $postAuthorLen) { |
| 48 |
$postauthor = $wpdb->get_results($wpdb->prepare("select ID,user_login from {$wpdb->prefix}users where ID = %s", $request_user)); |
| 49 |
if (empty($postauthor) || !$postauthor[0]->ID) { // If user name are numeric Ex: 1300001 |
| 50 |
$postauthor = $wpdb->get_results($wpdb->prepare("select ID,user_login from {$wpdb->prefix}users where user_login = \"{%s}\"",$request_user)); |
| 51 |
} |
| 52 |
} else { |
| 53 |
$postauthor = $wpdb->get_results($wpdb->prepare("select ID,user_login from {$wpdb->prefix}users where user_login = %s", $request_user)); |
| 54 |
} |
| 55 |
if (empty($postauthor) || !$postauthor[0]->ID) { |
| 56 |
$request_user = 1; |
| 57 |
$admindet = $wpdb->get_results($wpdb->prepare("select ID,user_login from {$wpdb->prefix}users where ID = %d", 1)); |
| 58 |
$message = " , <b>Author :- </b> not found (assigned to <b>" . $admindet[0]->user_login . "</b>)"; |
| 59 |
} else { |
| 60 |
$request_user = $postauthor[0]->ID; |
| 61 |
$admindet = $wpdb->get_results($wpdb->prepare("select ID,user_login from {$wpdb->prefix}users where ID = %s", $request_user)); |
| 62 |
$message = " , <b>Author :- </b>" . $admindet[0]->user_login; |
| 63 |
} |
| 64 |
$userDetails['user_id'] = $request_user; |
| 65 |
$userDetails['user_login'] = $admindet[0]->user_login; |
| 66 |
$userDetails['message'] = $message; |
| 67 |
return $userDetails; |
| 68 |
} |
| 69 |
|
| 70 |
public function assign_post_status($data_array) { |
| 71 |
if (isset($data_array['is_post_status']) && $data_array['is_post_status'] != 'on') { |
| 72 |
$data_array ['post_status'] = $data_array['is_post_status']; |
| 73 |
unset($data_array['is_post_status']); |
| 74 |
} |
| 75 |
if (isset($data_array ['post_type']) && $data_array ['post_type'] == 'page') { |
| 76 |
$data_array ['post_status'] = 'publish'; |
| 77 |
} else { |
| 78 |
if(isset($data_array['post_status']) || isset($data_array['coupon_status'])) { |
| 79 |
if(isset($data_array['post_status'])) { |
| 80 |
$data_array['post_status'] = strtolower( $data_array['post_status'] ); |
| 81 |
} else { |
| 82 |
$data_array['post_status'] = strtolower( $data_array['coupon_status'] ); |
| 83 |
} |
| 84 |
$data_array['post_status'] = trim($data_array['post_status']); |
| 85 |
if ($data_array['post_status'] != 'publish' && $data_array['post_status'] != 'private' && $data_array['post_status'] != 'draft' && $data_array['post_status'] != 'pending' && $data_array['post_status'] != 'sticky') { |
| 86 |
$stripPSF = strpos($data_array['post_status'], '{'); |
| 87 |
if ($stripPSF === 0) { |
| 88 |
$poststatus = substr($data_array['post_status'], 1); |
| 89 |
$stripPSL = substr($poststatus, -1); |
| 90 |
if ($stripPSL == '}') { |
| 91 |
$postpwd = substr($poststatus, 0, -1); |
| 92 |
$data_array['post_status'] = 'publish'; |
| 93 |
$data_array ['post_password'] = $postpwd; |
| 94 |
} else { |
| 95 |
$data_array['post_status'] = 'publish'; |
| 96 |
$data_array ['post_password'] = $poststatus; |
| 97 |
} |
| 98 |
} else { |
| 99 |
$data_array['post_status'] = 'publish'; |
| 100 |
} |
| 101 |
} |
| 102 |
if ($data_array['post_status'] == 'sticky') { |
| 103 |
$data_array['post_status'] = 'publish'; |
| 104 |
$sticky = true; |
| 105 |
} |
| 106 |
else { |
| 107 |
} |
| 108 |
} else { |
| 109 |
$data_array['post_status'] = 'publish'; |
| 110 |
} |
| 111 |
} |
| 112 |
return $data_array; |
| 113 |
} |
| 114 |
|
| 115 |
public function import_post_types($import_type, $importAs = null) { |
| 116 |
$import_type = trim($import_type); |
| 117 |
|
| 118 |
$module = array('Posts' => 'post', 'Pages' => 'page', 'Users' => 'user', 'Comments' => 'comments', 'Taxonomies' => $importAs, 'CustomerReviews' =>'wpcr3_review', 'Categories' => 'categories', 'Tags' => 'tags', 'eShop' => 'post', 'WooCommerce' => 'product', 'WPeCommerce' => 'wpsc-product','WPeCommerceCoupons' => 'wpsc-product', 'MarketPress' => 'product', 'MarketPressVariations' => 'mp_product_variation','WooCommerceVariations' => 'product', 'WooCommerceOrders' => 'product', 'WooCommerceCoupons' => 'product', 'WooCommerceRefunds' => 'product', 'CustomPosts' => $importAs); |
| 119 |
foreach (get_taxonomies() as $key => $taxonomy) { |
| 120 |
$module[$taxonomy] = $taxonomy; |
| 121 |
} |
| 122 |
if(array_key_exists($import_type, $module)) { |
| 123 |
return $module[$import_type]; |
| 124 |
} |
| 125 |
else { |
| 126 |
return $import_type; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
public function get_header_values($map , $header_array , $value_array){ |
| 131 |
|
| 132 |
$post_values = []; |
| 133 |
foreach($map as $key => $value){ |
| 134 |
$csv_value= trim($map[$key]); |
| 135 |
|
| 136 |
if(!empty($csv_value)){ |
| 137 |
//if(preg_match_all('/{(\w+)}/', $csv_value, $matches)){ |
| 138 |
$pattern = "/({([a-z A-Z 0-9 | , _ -]+)(.*?)(}))/"; |
| 139 |
if(preg_match_all($pattern, $csv_value, $matches, PREG_PATTERN_ORDER)){ |
| 140 |
$csv_element = $csv_value; |
| 141 |
|
| 142 |
foreach($matches[2] as $value){ |
| 143 |
|
| 144 |
$get_key = array_search($value , $header_array); |
| 145 |
if(isset($value_array[$get_key])){ |
| 146 |
$csv_value_element = $value_array[$get_key]; |
| 147 |
$value = '{'.$value.'}'; |
| 148 |
$csv_element = str_replace($value, $csv_value_element, $csv_element); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
$math = 'MATH('; |
| 153 |
if (strpos($csv_element, $math) !== false) { |
| 154 |
$equation = str_replace('MATH(', '', $csv_element); |
| 155 |
$equation = str_replace(')', '', $equation); |
| 156 |
$csv_element = $this->evalmath($equation); |
| 157 |
} |
| 158 |
|
| 159 |
$wp_element= trim($key); |
| 160 |
if(!empty($csv_element) && !empty($wp_element)){ |
| 161 |
$post_values[$wp_element] = $csv_element; |
| 162 |
} |
| 163 |
//} |
| 164 |
} |
| 165 |
|
| 166 |
elseif(!in_array($csv_value , $header_array)){ |
| 167 |
$wp_element= trim($key); |
| 168 |
$post_values[$wp_element] = $csv_value; |
| 169 |
} |
| 170 |
|
| 171 |
else{ |
| 172 |
$get_key= array_search($csv_value , $header_array); |
| 173 |
if(isset($value_array[$get_key])){ |
| 174 |
$csv_element = $value_array[$get_key]; |
| 175 |
$wp_element = trim($key); |
| 176 |
if(!empty($csv_element) && !empty($wp_element)){ |
| 177 |
$post_values[$wp_element] = $csv_element; |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
return $post_values; |
| 184 |
} |
| 185 |
|
| 186 |
public function evalmath($equation) { |
| 187 |
|
| 188 |
$result = 0; |
| 189 |
|
| 190 |
// sanitize input |
| 191 |
$equation = preg_replace("/[^0-9+\-.*\/()%]/","",$equation); |
| 192 |
|
| 193 |
// convert percentages to decimal |
| 194 |
$equation = preg_replace("/([+-])([0-9]{1})(%)/","*(1\$1.0\$2)",$equation); |
| 195 |
$equation = preg_replace("/([+-])([0-9]+)(%)/","*(1\$1.\$2)",$equation); |
| 196 |
//$equation = preg_replace("/([0-9]+)(%)/",".\$1",$equation); |
| 197 |
|
| 198 |
if ( $equation != "" ) |
| 199 |
{ |
| 200 |
$result = @eval("return " . $equation . ";" ); |
| 201 |
} |
| 202 |
|
| 203 |
if ($result === null) |
| 204 |
{ |
| 205 |
//throw new Exception("Unable to calculate equation"); |
| 206 |
$result = "Unable to calculate equation"; |
| 207 |
} |
| 208 |
if($result === FALSE){ |
| 209 |
$result = 'false'; |
| 210 |
} |
| 211 |
|
| 212 |
return $result; |
| 213 |
} |
| 214 |
|
| 215 |
public function update_log($message , $status , $verify , $post_id , $hash_key){ |
| 216 |
|
| 217 |
global $wpdb; |
| 218 |
$importlog_table_name = $wpdb->prefix ."import_log_detail"; |
| 219 |
|
| 220 |
$wpdb->insert($importlog_table_name, array( |
| 221 |
'hash_key' => $hash_key, |
| 222 |
'message' => "{$message}", |
| 223 |
'status' => "{$status}", |
| 224 |
'verify' => "{$verify}", |
| 225 |
'post_id' => $post_id, |
| 226 |
|
| 227 |
), |
| 228 |
array('%s', '%s', '%s', '%s', '%d') |
| 229 |
); |
| 230 |
} |
| 231 |
|
| 232 |
public function update_error_log($message , $hash_key , $post_id){ |
| 233 |
global $wpdb; |
| 234 |
$importlog_table_name = $wpdb->prefix ."import_log_detail"; |
| 235 |
|
| 236 |
$wpdb->insert($importlog_table_name, array( |
| 237 |
'hash_key' => $hash_key, |
| 238 |
'message' => "{$message}", |
| 239 |
'post_id' => $post_id |
| 240 |
|
| 241 |
), |
| 242 |
array('%s', '%s', '%d') |
| 243 |
); |
| 244 |
} |
| 245 |
|
| 246 |
public function update_category_log($category , $post_id){ |
| 247 |
global $wpdb; |
| 248 |
$wpdb->update($wpdb->prefix.'import_log_detail', array( |
| 249 |
'categories' => "{$category}" |
| 250 |
), |
| 251 |
array('post_id' => $post_id) |
| 252 |
); |
| 253 |
} |
| 254 |
|
| 255 |
public function update_tag_log($tag , $post_id){ |
| 256 |
global $wpdb; |
| 257 |
$wpdb->update($wpdb->prefix.'import_log_detail', array( |
| 258 |
'tags' => "{$tag}" |
| 259 |
), |
| 260 |
array('post_id' => $post_id) |
| 261 |
); |
| 262 |
} |
| 263 |
public function update_status_log($status , $verify , $post_id){ |
| 264 |
global $wpdb; |
| 265 |
$wpdb->update($wpdb->prefix.'import_log_detail', array( |
| 266 |
'status' => "{$status}", |
| 267 |
'verify' => "{$verify}" |
| 268 |
), |
| 269 |
array('post_id' => $post_id) |
| 270 |
); |
| 271 |
} |
| 272 |
|
| 273 |
public function formatSizeUnits($bytes) |
| 274 |
{ |
| 275 |
if ($bytes >= 1073741824) |
| 276 |
{ |
| 277 |
$bytes = number_format($bytes / 1073741824, 2) . ' GB'; |
| 278 |
} |
| 279 |
elseif ($bytes >= 1048576) |
| 280 |
{ |
| 281 |
$bytes = number_format($bytes / 1048576, 2) . ' MB'; |
| 282 |
} |
| 283 |
elseif ($bytes >= 1024) |
| 284 |
{ |
| 285 |
$bytes = number_format($bytes / 1024, 2) . ' KB'; |
| 286 |
} |
| 287 |
elseif ($bytes > 1) |
| 288 |
{ |
| 289 |
$bytes = $bytes . ' bytes'; |
| 290 |
} |
| 291 |
elseif ($bytes == 1) |
| 292 |
{ |
| 293 |
$bytes = $bytes . ' byte'; |
| 294 |
} |
| 295 |
else |
| 296 |
{ |
| 297 |
$bytes = '0 bytes'; |
| 298 |
} |
| 299 |
|
| 300 |
return $bytes; |
| 301 |
} |
| 302 |
|
| 303 |
public function update_count($hash_key){ |
| 304 |
$response = []; |
| 305 |
global $wpdb; |
| 306 |
$log_table_name = $wpdb->prefix ."import_detail_log"; |
| 307 |
$get_data = $wpdb->get_results("SELECT skipped , created , updated FROM $log_table_name WHERE hash_key = '$hash_key' "); |
| 308 |
$skipped = $get_data[0]->skipped; |
| 309 |
$response['skipped'] = $skipped + 1; |
| 310 |
$created = $get_data[0]->created; |
| 311 |
$response['created'] = $created + 1; |
| 312 |
$updated = $get_data[0]->updated; |
| 313 |
$response['updated'] = $updated + 1; |
| 314 |
|
| 315 |
return $response; |
| 316 |
} |
| 317 |
|
| 318 |
} |
| 319 |
|