| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) { exit; } |
| 3 |
if (!class_exists('wpSmartImportCommon')){ |
| 4 |
class wpSmartImportCommon { |
| 5 |
|
| 6 |
public function wpsi_get_option($option_name = '' , $default = false) { |
| 7 |
$option_name = empty($option_name) ? wpSmartImport::getVar('wpsi_session') : $option_name ; |
| 8 |
return get_option($option_name,$default); |
| 9 |
} |
| 10 |
|
| 11 |
public function update_session($array) { |
| 12 |
if (!is_array($array) || empty($array)) { |
| 13 |
return false; |
| 14 |
} |
| 15 |
$option = $this->wpsi_get_option(); |
| 16 |
if (is_string($option) || empty($option)) { |
| 17 |
$option = array(); |
| 18 |
} |
| 19 |
foreach ($array as $key => $value) { |
| 20 |
(array)$option[$key] = $value; |
| 21 |
} |
| 22 |
update_option(wpSmartImport::getVar('wpsi_session'), $option); |
| 23 |
return true; |
| 24 |
} |
| 25 |
|
| 26 |
public function unset_session() { |
| 27 |
update_option(wpSmartImport::getVar('wpsi_session'), ""); |
| 28 |
} |
| 29 |
|
| 30 |
static public function woocommerce_exist() { |
| 31 |
$active_plugins = apply_filters('active_plugins', get_option('active_plugins')); |
| 32 |
if (in_array("woocommerce/woocommerce.php", $active_plugins)) { |
| 33 |
return true; |
| 34 |
} |
| 35 |
return false; |
| 36 |
} |
| 37 |
|
| 38 |
static public function verify_ajax($_nonce = '') { |
| 39 |
if (!wp_doing_ajax() || !wp_verify_nonce(sanitize_text_field($_nonce), 'wpsi_nonce') || !current_user_can('manage_options')) { |
| 40 |
echo 'Invalid HTTP Request'; |
| 41 |
wp_die( esc_html__( 'You do not have sufficient permissions to perform this action.', 'wp-smart-import' ) ); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
public function rec_search_file_path($path, $fdata) { |
| 46 |
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path["basedir"])); |
| 47 |
$basename = basename($path["basedir"]); |
| 48 |
$files = array(); |
| 49 |
foreach ($rii as $file): |
| 50 |
if (!$file->isDir()) { |
| 51 |
if (array_key_exists('extension', $fdata)) { |
| 52 |
$pattern = $fdata['basename']; |
| 53 |
} else { |
| 54 |
$pattern = $fdata['basename'].'.*'; |
| 55 |
} |
| 56 |
$result = fnmatch($pattern, $file->getFilename()); |
| 57 |
if ($result) { |
| 58 |
$exp = explode($basename, $file->getPathname()); |
| 59 |
$files[] = $path["baseurl"].$exp[1]; |
| 60 |
} |
| 61 |
} |
| 62 |
endforeach; |
| 63 |
return !empty($files) ? $files[0] :''; |
| 64 |
} |
| 65 |
static public function current_xmlfile_path() { |
| 66 |
$upload_dir = wp_upload_dir(); |
| 67 |
global $session; |
| 68 |
$file_path = $session['file_path']; |
| 69 |
return $upload_dir['basedir']. DIRECTORY_SEPARATOR . wpSmartImport::getVar('folder_name') . DIRECTORY_SEPARATOR .$file_path; |
| 70 |
} |
| 71 |
public function wpsi_currentxmlData($str, $arg = '') { |
| 72 |
$upload_dir = wp_upload_dir(); |
| 73 |
global $session; |
| 74 |
$file_path = wpSmartImport::getVar('file_path', 'session'); |
| 75 |
$file_name = pathinfo($file_path); |
| 76 |
if (isset($file_path)) { |
| 77 |
if ($str == "folder") { |
| 78 |
return $file_name['dirname']; |
| 79 |
} elseif ($str == "file") { |
| 80 |
return $upload_dir['basedir']. "/". wpSmartImport::getVar('folder_name') . "/".$file_path; |
| 81 |
} elseif ($str == "extension") { |
| 82 |
return $file_name['extension']; |
| 83 |
} elseif ($str == "file_name") { |
| 84 |
return $file_name['basename']; |
| 85 |
} elseif ($str == "node") { |
| 86 |
$node = $session['node']; |
| 87 |
if (isset($node)) { |
| 88 |
return $node; |
| 89 |
} |
| 90 |
} elseif ( $str == "FilePath") { |
| 91 |
if ($arg == "url") { |
| 92 |
return $upload_dir['baseurl']."/". wpSmartImport::getVar('folder_name')."/".$file_name['dirname'].'/'; |
| 93 |
} else { |
| 94 |
return $upload_dir['basedir']."/". wpSmartImport::getVar('folder_name') . "/".$file_name['dirname'].'/'; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
return ''; |
| 99 |
} |
| 100 |
|
| 101 |
static public function parse_post_data($pdata, $batchs) { |
| 102 |
$pdata_temp = array(); |
| 103 |
foreach ($batchs as $bn) { |
| 104 |
$array = $pdata; |
| 105 |
array_walk_recursive($array, function (&$value, $key) use ($bn) { |
| 106 |
$value = self::parse_string($value, $bn); |
| 107 |
}); |
| 108 |
$pdata_temp[] = $array; |
| 109 |
} |
| 110 |
return $pdata_temp; |
| 111 |
} |
| 112 |
|
| 113 |
static public function parse_post_array($array, $node_num = 0) { |
| 114 |
array_walk_recursive($array, function (&$value, $key) use ($node_num){ |
| 115 |
$value = self::parse_string($value, $node_num); |
| 116 |
}); |
| 117 |
return $array; |
| 118 |
} |
| 119 |
|
| 120 |
static public function parse_string($string, $node_num = 0, $allow_default = false, $default = '') |
| 121 |
{ |
| 122 |
global $session; |
| 123 |
preg_match_all('/{(.*?)}/', $string, $matches); |
| 124 |
$node = $session['node']; |
| 125 |
$res = wpsiAjaxController::checkfile(); |
| 126 |
$parse_string = ''; |
| 127 |
if ($res && isset($res['status']) && $res['status'] === 'success'){ |
| 128 |
$dom = $res['dom_obj']; // DomDocument Object |
| 129 |
libxml_clear_errors(); |
| 130 |
$xpath = new DOMXPath($dom); |
| 131 |
$element = $dom->getElementsByTagName($node)->item($node_num); |
| 132 |
// our query is relative to the tbody node |
| 133 |
if (!empty($matches[1])) { |
| 134 |
foreach ($matches[1] as $midx => $mval) { |
| 135 |
$query = $mval; |
| 136 |
$entries = $xpath->query($query,$element); |
| 137 |
if($entries->length > 0 ) { |
| 138 |
foreach ($entries as $entry) { |
| 139 |
$matches[1][$midx] = trim($entry->nodeValue); |
| 140 |
|
| 141 |
} |
| 142 |
} else if($allow_default){ |
| 143 |
$matches[1][$midx] = trim($entry->nodeValue); |
| 144 |
} |
| 145 |
} |
| 146 |
$temp_str = $string; |
| 147 |
foreach ($matches[0] as $midx => $mvalue) { |
| 148 |
$temp_str = str_replace($mvalue, $matches[1][$midx], $temp_str); |
| 149 |
} |
| 150 |
$parse_string = $temp_str; |
| 151 |
|
| 152 |
} else { |
| 153 |
$parse_string = $string; |
| 154 |
} |
| 155 |
} |
| 156 |
$parse_string = apply_filters( 'wpsi_parse_field_value', $parse_string, $string, $node_num ); |
| 157 |
return $parse_string; |
| 158 |
} |
| 159 |
|
| 160 |
// generate_featured_image Wordpress |
| 161 |
static public function generate_featured_image($image_url, $post_id = 0) { |
| 162 |
$qpos = strrpos($image_url, "?"); |
| 163 |
$image_url_not_querystr = ($qpos !== false) ? substr($image_url, 0, $qpos) : $image_url; |
| 164 |
$filename = basename($image_url_not_querystr); |
| 165 |
$ext = pathinfo($image_url_not_querystr, PATHINFO_EXTENSION); |
| 166 |
if (empty($ext)) { |
| 167 |
$filename .= '.jpeg'; |
| 168 |
} |
| 169 |
|
| 170 |
// Check if this file has already been uploaded as an attachment |
| 171 |
global $wpdb; |
| 172 |
$sanitized_filename = sanitize_file_name($filename); |
| 173 |
$filename_no_ext = pathinfo($sanitized_filename, PATHINFO_FILENAME); |
| 174 |
$ext = pathinfo($sanitized_filename, PATHINFO_EXTENSION); |
| 175 |
|
| 176 |
$attachment_id = $wpdb->get_var($wpdb->prepare( |
| 177 |
"SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND (guid LIKE %s OR post_title = %s OR post_title = %s) LIMIT 1", |
| 178 |
'%' . $wpdb->esc_like($filename_no_ext) . '%' . $ext, |
| 179 |
$filename, |
| 180 |
$sanitized_filename |
| 181 |
)); |
| 182 |
if ($attachment_id) { |
| 183 |
return $attachment_id; |
| 184 |
} |
| 185 |
|
| 186 |
$upload_dir = wp_upload_dir(); |
| 187 |
$request = wp_remote_get($image_url, array('timeout' => intval(get_option('wpsi_image_timeout', 15)))); |
| 188 |
$response = @wp_remote_retrieve_body($request); |
| 189 |
|
| 190 |
$md5_hash = ''; |
| 191 |
if (!empty($response)) { |
| 192 |
$md5_hash = md5($response); |
| 193 |
$existing_attach_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wpsi_image_hash' AND meta_value = %s LIMIT 1", $md5_hash)); |
| 194 |
if ($existing_attach_id) { |
| 195 |
return $existing_attach_id; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
$filename = wp_unique_filename($upload_dir['path'], sanitize_file_name($filename)); |
| 200 |
$upload_file = wp_upload_bits($filename, null, $response); |
| 201 |
if (wp_mkdir_p($upload_dir['path'])) |
| 202 |
$file = $upload_dir['path'].'/'.$filename; |
| 203 |
else |
| 204 |
$file = $upload_dir['basedir'].'/'.$filename; |
| 205 |
|
| 206 |
$wp_filetype = wp_check_filetype($filename, null); |
| 207 |
$attachment = array( |
| 208 |
'post_mime_type' => $wp_filetype['type'], |
| 209 |
'post_title' => $filename, |
| 210 |
'post_content' => '', |
| 211 |
'post_status' => 'inherit' |
| 212 |
); |
| 213 |
$attach_id = wp_insert_attachment($attachment, $file, $post_id); |
| 214 |
if (!empty($md5_hash) && $attach_id) { |
| 215 |
add_post_meta($attach_id, '_wpsi_image_hash', $md5_hash); |
| 216 |
} |
| 217 |
require_once(ABSPATH . 'wp-admin/includes/image.php'); |
| 218 |
$attach_data = wp_generate_attachment_metadata($attach_id, $file); |
| 219 |
wp_update_attachment_metadata($attach_id, $attach_data); |
| 220 |
return $attach_id; |
| 221 |
} |
| 222 |
|
| 223 |
public function sort_and_get_images($files, $mode) { |
| 224 |
if (is_array($files)) { |
| 225 |
foreach ($files as $file_string) { |
| 226 |
$file_string = explode(',',$file_string); |
| 227 |
$file_string = array_filter( $file_string ); |
| 228 |
foreach ($file_string as $key => $fname) { |
| 229 |
$images[] = trim($fname); |
| 230 |
} |
| 231 |
} |
| 232 |
} else { |
| 233 |
$files = explode(',',$files); |
| 234 |
$files = array_filter( $files ); |
| 235 |
if(is_array($files)) { |
| 236 |
foreach ($files as $idx => $fname) { |
| 237 |
$images[] = trim($fname); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
if( empty( $images ) ){ |
| 242 |
$images = array(); |
| 243 |
} |
| 244 |
$images = array_filter(array_unique($images)); |
| 245 |
if (is_array($images)) { |
| 246 |
$upload_dir = wp_upload_dir(); |
| 247 |
foreach ($images as $fname) { |
| 248 |
$file_names[] = $fname; |
| 249 |
if ($mode == 'media') { |
| 250 |
$path = explode("?", $fname); |
| 251 |
$file = pathinfo(trim($path[0])); |
| 252 |
$fpath = $this->rec_search_file_path($upload_dir, $file); |
| 253 |
$srcs[] = $fpath; |
| 254 |
} else { |
| 255 |
$srcs[] = $fname; |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
return array('file_names' => $file_names, 'srcs' => $srcs); |
| 260 |
} |
| 261 |
|
| 262 |
public function get_random_dateTime($startDate, $endDate) { |
| 263 |
$startDate = strtolower(trim($startDate)); |
| 264 |
$endDat = strtolower(trim($endDate)); |
| 265 |
$randomTime = mt_rand(strtotime($startDate), strtotime($endDate)); // phpcs:ignore WordPress.WP.AlternativeFunctions.rand_mt_rand |
| 266 |
return wp_date('Y-m-d H:i:s', $randomTime); |
| 267 |
} |
| 268 |
} |
| 269 |
} |