PluginProbe
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on / trunk
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on vtrunk
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 trunk, at importExtensions/ImportHelpers.php

315 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 $pattern = "/({([a-z A-Z 0-9 | , _ -]+)(.*?)(}))/";
137 if(preg_match_all($pattern, $csv_value, $matches, PREG_PATTERN_ORDER)){
138 $csv_element = $csv_value;
139
140 foreach($matches[2] as $value){
141
142 $get_key = array_search($value , $header_array);
143 if(isset($value_array[$get_key])){
144 $csv_value_element = $value_array[$get_key];
145 $value = '{'.$value.'}';
146 $csv_element = str_replace($value, $csv_value_element, $csv_element);
147 }
148 }
149
150 $math = 'MATH(';
151 if (strpos($csv_element, $math) !== false) {
152 $equation = str_replace('MATH(', '', $csv_element);
153 $equation = str_replace(')', '', $equation);
154 $csv_element = $this->evalmath($equation);
155 }
156
157 $wp_element= trim($key);
158 if(!empty($csv_element) && !empty($wp_element)){
159 $post_values[$wp_element] = $csv_element;
160 }
161 }
162
163 elseif(!in_array($csv_value , $header_array)){
164 $wp_element= trim($key);
165 $post_values[$wp_element] = $csv_value;
166 }
167
168 else{
169 $get_key= array_search($csv_value , $header_array);
170 if(isset($value_array[$get_key])){
171 $csv_element = $value_array[$get_key];
172 $wp_element = trim($key);
173 if(!empty($csv_element) && !empty($wp_element)){
174 $post_values[$wp_element] = $csv_element;
175 }
176 }
177 }
178 }
179 }
180 return $post_values;
181 }
182
183 public function evalmath($equation) {
184
185 $result = 0;
186
187 //input
188 $equation = preg_replace("/[^0-9+\-.*\/()%]/","",$equation);
189
190 // convert percentages to decimal
191 $equation = preg_replace("/([+-])([0-9]{1})(%)/","*(1\$1.0\$2)",$equation);
192 $equation = preg_replace("/([+-])([0-9]+)(%)/","*(1\$1.\$2)",$equation);
193
194 if ( $equation != "" )
195 {
196 $result = @eval("return " . $equation . ";" );
197 }
198
199 if ($result === null)
200 {
201 //throw new Exception("Unable to calculate equation");
202 $result = "Unable to calculate equation";
203 }
204 if($result === FALSE){
205 $result = 'false';
206 }
207
208 return $result;
209 }
210
211 public function update_log($message , $status , $verify , $post_id , $hash_key){
212
213 global $wpdb;
214 $importlog_table_name = $wpdb->prefix ."import_log_detail";
215
216 $wpdb->insert($importlog_table_name, array(
217 'hash_key' => $hash_key,
218 'message' => "{$message}",
219 'status' => "{$status}",
220 'verify' => "{$verify}",
221 'post_id' => $post_id,
222
223 ),
224 array('%s', '%s', '%s', '%s', '%d')
225 );
226 }
227
228 public function update_error_log($message , $hash_key , $post_id){
229 global $wpdb;
230 $importlog_table_name = $wpdb->prefix ."import_log_detail";
231
232 $wpdb->insert($importlog_table_name, array(
233 'hash_key' => $hash_key,
234 'message' => "{$message}",
235 'post_id' => $post_id
236
237 ),
238 array('%s', '%s', '%d')
239 );
240 }
241
242 public function update_category_log($category , $post_id){
243 global $wpdb;
244 $wpdb->update($wpdb->prefix.'import_log_detail', array(
245 'categories' => "{$category}"
246 ),
247 array('post_id' => $post_id)
248 );
249 }
250
251 public function update_tag_log($tag , $post_id){
252 global $wpdb;
253 $wpdb->update($wpdb->prefix.'import_log_detail', array(
254 'tags' => "{$tag}"
255 ),
256 array('post_id' => $post_id)
257 );
258 }
259 public function update_status_log($status , $verify , $post_id){
260 global $wpdb;
261 $wpdb->update($wpdb->prefix.'import_log_detail', array(
262 'status' => "{$status}",
263 'verify' => "{$verify}"
264 ),
265 array('post_id' => $post_id)
266 );
267 }
268
269 public function formatSizeUnits($bytes)
270 {
271 if ($bytes >= 1073741824)
272 {
273 $bytes = number_format($bytes / 1073741824, 2) . ' GB';
274 }
275 elseif ($bytes >= 1048576)
276 {
277 $bytes = number_format($bytes / 1048576, 2) . ' MB';
278 }
279 elseif ($bytes >= 1024)
280 {
281 $bytes = number_format($bytes / 1024, 2) . ' KB';
282 }
283 elseif ($bytes > 1)
284 {
285 $bytes = $bytes . ' bytes';
286 }
287 elseif ($bytes == 1)
288 {
289 $bytes = $bytes . ' byte';
290 }
291 else
292 {
293 $bytes = '0 bytes';
294 }
295
296 return $bytes;
297 }
298
299 public function update_count($hash_key){
300 $response = [];
301 global $wpdb;
302 $log_table_name = $wpdb->prefix ."import_detail_log";
303 $get_data = $wpdb->get_results($wpdb->prepare("SELECT skipped , created , updated FROM $log_table_name WHERE hash_key = %s", $hash_key));
304 $skipped = $get_data[0]->skipped;
305 $response['skipped'] = $skipped + 1;
306 $created = $get_data[0]->created;
307 $response['created'] = $created + 1;
308 $updated = $get_data[0]->updated;
309 $response['updated'] = $updated + 1;
310
311 return $response;
312 }
313
314 }
315