PluginProbe
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on / 1.4.3
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on v1.4.3
2.0 1.7.1 1.2.9 1.3 1.4 1.4.1 1.4.2 1.4.3 1.5 1.6 1.7 trunk 1.0 1.1 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8
import-users / importExtensions / ImportHelpers.php

ImportHelpers.php in Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on 1.4.3, at importExtensions/ImportHelpers.php

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