| 1 |
<?php |
| 2 |
if ( ! function_exists( 'cache_users' ) ) : |
| 3 |
/** |
| 4 |
* Prime the user cache for a list of user IDs. |
| 5 |
* |
| 6 |
* @param array $user_ids Array of user IDs. |
| 7 |
* @return void |
| 8 |
*/ |
| 9 |
function cache_users( $user_ids ) { |
| 10 |
global $wpdb; |
| 11 |
|
| 12 |
$user_ids = array_filter( array_map( 'absint', (array) $user_ids ) ); |
| 13 |
|
| 14 |
if ( empty( $user_ids ) ) { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
// Prime user meta cache. |
| 19 |
update_meta_cache( 'user', $user_ids ); |
| 20 |
|
| 21 |
// Determine which users are not already cached. |
| 22 |
$uncached = array(); |
| 23 |
|
| 24 |
foreach ( $user_ids as $user_id ) { |
| 25 |
if ( false === wp_cache_get( $user_id, 'users' ) ) { |
| 26 |
$uncached[] = $user_id; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
if ( empty( $uncached ) ) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
$placeholders = implode( ',', array_fill( 0, count( $uncached ), '%d' ) ); |
| 35 |
|
| 36 |
$query = $wpdb->prepare( |
| 37 |
"SELECT * FROM {$wpdb->users} WHERE ID IN ($placeholders)", |
| 38 |
$uncached |
| 39 |
); |
| 40 |
|
| 41 |
$users = $wpdb->get_results( $query ); |
| 42 |
|
| 43 |
foreach ( $users as $user ) { |
| 44 |
wp_cache_add( $user->ID, $user, 'users' ); |
| 45 |
wp_cache_add( $user->user_login, $user->ID, 'userlogins' ); |
| 46 |
wp_cache_add( $user->user_nicename, $user->ID, 'userslugs' ); |
| 47 |
wp_cache_add( $user->user_email, $user->ID, 'useremail' ); |
| 48 |
} |
| 49 |
} |
| 50 |
endif; |
| 51 |
|
| 52 |
if (!defined('ABSPATH')) exit; |
| 53 |
|
| 54 |
class WPCargo |
| 55 |
{ |
| 56 |
public $status; |
| 57 |
public $settings; |
| 58 |
public $logo; |
| 59 |
public $mail_status; |
| 60 |
public $admin_mail_status; |
| 61 |
public $mail_cc; |
| 62 |
public $mail_bcc; |
| 63 |
public $client_mail_subject; |
| 64 |
public $admin_mail_subject; |
| 65 |
public $client_mail_to; |
| 66 |
public $admin_mail_to; |
| 67 |
public $client_mail_settings; |
| 68 |
public $client_mail_active; |
| 69 |
public $admin_mail_active; |
| 70 |
public $client_mail_body; |
| 71 |
public $admin_mail_body; |
| 72 |
public $client_mail_footer; |
| 73 |
public $admin_mail_footer; |
| 74 |
//public $agents; |
| 75 |
public $prefix; |
| 76 |
public $suffix; |
| 77 |
public $users; |
| 78 |
public $time_format; |
| 79 |
public $date_format; |
| 80 |
public $datetime_format; |
| 81 |
public $autogenerate_title; |
| 82 |
public $tax; |
| 83 |
public $number_digit; |
| 84 |
public $barcode_type; |
| 85 |
public $barcode_size; |
| 86 |
|
| 87 |
function __construct() |
| 88 |
{ |
| 89 |
$this->status = $this->status(); |
| 90 |
$this->settings = $this->settings(); |
| 91 |
$this->logo = $this->logo(); |
| 92 |
$this->mail_status = $this->mail_status(); |
| 93 |
$this->admin_mail_status = $this->admin_mail_status(); |
| 94 |
$this->mail_cc = $this->mail_cc(); |
| 95 |
$this->mail_bcc = $this->mail_bcc(); |
| 96 |
$this->client_mail_subject = $this->client_mail_subject(); |
| 97 |
$this->admin_mail_subject = $this->admin_mail_subject(); |
| 98 |
$this->client_mail_to = $this->client_mail_to(); |
| 99 |
$this->admin_mail_to = $this->admin_mail_to(); |
| 100 |
$this->client_mail_settings = $this->client_mail_settings(); |
| 101 |
$this->client_mail_active = $this->client_mail_active(); |
| 102 |
$this->admin_mail_active = $this->admin_mail_active(); |
| 103 |
$this->client_mail_body = $this->client_mail_body(); |
| 104 |
$this->admin_mail_body = $this->admin_mail_body(); |
| 105 |
$this->client_mail_footer = $this->client_mail_footer(); |
| 106 |
$this->admin_mail_footer = $this->admin_mail_footer(); |
| 107 |
//$this->agents = $this->agents(); |
| 108 |
$this->prefix = $this->prefix(); |
| 109 |
$this->suffix = $this->suffix(); |
| 110 |
// $this->users = $this->all_wpcargo_users(); |
| 111 |
$this->time_format = $this->time_format(); |
| 112 |
$this->date_format = $this->date_format(); |
| 113 |
$this->datetime_format = $this->datetime_format(); |
| 114 |
$this->tax = $this->tax(); |
| 115 |
$this->number_digit = $this->number_digit(); |
| 116 |
$this->barcode_type = $this->barcode_type(); |
| 117 |
$this->barcode_size = $this->barcode_size(); |
| 118 |
$this->autogenerate_title(); |
| 119 |
} |
| 120 |
|
| 121 |
/* |
| 122 |
** Public Functions |
| 123 |
*/ |
| 124 |
public function history($shipment_id) |
| 125 |
{ |
| 126 |
$history = maybe_unserialize(get_post_meta($shipment_id, 'wpcargo_shipments_update', true)); |
| 127 |
if (!is_array($history)) { |
| 128 |
return array(); |
| 129 |
} |
| 130 |
return $history; |
| 131 |
} |
| 132 |
public function barcode_type() |
| 133 |
{ |
| 134 |
return apply_filters('wpcargo_barcode_type', 'code128'); |
| 135 |
} |
| 136 |
public function barcode_size() |
| 137 |
{ |
| 138 |
return apply_filters('wpcargo_barcode_size', 60); |
| 139 |
} |
| 140 |
public function barcode($shipment_id, $html = false, $width = 180, $height = 50) |
| 141 |
{ |
| 142 |
$barcode = $this->barcode_url($shipment_id); |
| 143 |
if ($html) { |
| 144 |
$barcode = '<img class="wpcargo_shipment_barcode1" src="' . $barcode . '" alt="' . get_the_title($shipment_id) . '" />'; |
| 145 |
} |
| 146 |
return $barcode; |
| 147 |
} |
| 148 |
public function barcode_url($shipment_id, $barcode_size = '', $barcode_type = '', $orientation = '') |
| 149 |
{ |
| 150 |
$shipment_number = get_the_title($shipment_id); |
| 151 |
$is_qrcode = apply_filters('wpcargo_qrcode_enable', false); |
| 152 |
$base64_data = $is_qrcode ? wpcargo_generate_qrcode($shipment_number) : wpcargo_generate_barcodecode($shipment_number); |
| 153 |
return apply_filters('wpcargo_barcode_url', $base64_data, $shipment_id); |
| 154 |
} |
| 155 |
/* |
| 156 |
** Protected Functions |
| 157 |
*/ |
| 158 |
function status() |
| 159 |
{ |
| 160 |
$status = array(); |
| 161 |
if (did_action('init')) { |
| 162 |
$status = wpcargo_default_status(); |
| 163 |
} |
| 164 |
$wpcargo_option_settings = $this->settings(); |
| 165 |
if ($wpcargo_option_settings) { |
| 166 |
if (array_key_exists('settings_shipment_status', $wpcargo_option_settings)) { |
| 167 |
$get_all_status = trim($wpcargo_option_settings['settings_shipment_status']); |
| 168 |
if ($get_all_status) { |
| 169 |
$status = array_map('trim', explode(",", $get_all_status)); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
return apply_filters('wpcargo_status_option', $status); |
| 174 |
} |
| 175 |
protected function settings() |
| 176 |
{ |
| 177 |
return (get_option('wpcargo_option_settings')) ? get_option('wpcargo_option_settings') : array(); |
| 178 |
} |
| 179 |
protected function logo() |
| 180 |
{ |
| 181 |
$wpcargo_option_settings = $this->settings(); |
| 182 |
$logo = ''; |
| 183 |
if ($wpcargo_option_settings) { |
| 184 |
if (array_key_exists('settings_shipment_ship_logo', $wpcargo_option_settings)) { |
| 185 |
$logo = $wpcargo_option_settings['settings_shipment_ship_logo']; |
| 186 |
} |
| 187 |
} |
| 188 |
return $logo; |
| 189 |
} |
| 190 |
protected function mail_status() |
| 191 |
{ |
| 192 |
$status = array(); |
| 193 |
$mail_status = get_option('wpcargo_mail_status'); |
| 194 |
if ($mail_status) { |
| 195 |
$status = $mail_status; |
| 196 |
} |
| 197 |
return $status; |
| 198 |
} |
| 199 |
protected function admin_mail_status() |
| 200 |
{ |
| 201 |
$status = array(); |
| 202 |
$mail_status = get_option('wpcargo_admin_mail_status'); |
| 203 |
if ($mail_status) { |
| 204 |
$status = $mail_status; |
| 205 |
} |
| 206 |
return $status; |
| 207 |
} |
| 208 |
protected function mail_cc() |
| 209 |
{ |
| 210 |
return get_option('wpcargo_email_cc'); |
| 211 |
} |
| 212 |
protected function mail_bcc() |
| 213 |
{ |
| 214 |
return get_option('wpcargo_email_bcc'); |
| 215 |
} |
| 216 |
protected function client_mail_settings() |
| 217 |
{ |
| 218 |
return get_option('wpcargo_mail_settings'); |
| 219 |
} |
| 220 |
protected function client_mail_active() |
| 221 |
{ |
| 222 |
$mail_active = false; |
| 223 |
$wpcargo_mail_settings = $this->client_mail_settings(); |
| 224 |
if (!empty($wpcargo_mail_settings) && array_key_exists('wpcargo_active_mail', $wpcargo_mail_settings)) { |
| 225 |
$mail_active = true; |
| 226 |
} |
| 227 |
return $mail_active; |
| 228 |
} |
| 229 |
protected function admin_mail_active() |
| 230 |
{ |
| 231 |
return get_option('wpcargo_admin_mail_active'); |
| 232 |
} |
| 233 |
protected function client_mail_subject() |
| 234 |
{ |
| 235 |
$subject = ''; |
| 236 |
$settings = $this->client_mail_settings(); |
| 237 |
if (!empty($settings) && array_key_exists('wpcargo_mail_subject', $settings)) { |
| 238 |
$subject = $settings['wpcargo_mail_subject']; |
| 239 |
} |
| 240 |
return $subject; |
| 241 |
} |
| 242 |
protected function admin_mail_subject() |
| 243 |
{ |
| 244 |
$subject = 'Shipment Notification'; |
| 245 |
if (did_action('init')) { |
| 246 |
$subject = esc_html__('Shipment Notification', 'wpcargo'); |
| 247 |
} |
| 248 |
|
| 249 |
if (!empty(trim(get_option('wpcargo_admin_mail_subject')))) { |
| 250 |
$subject = get_option('wpcargo_admin_mail_subject'); |
| 251 |
} |
| 252 |
return $subject; |
| 253 |
} |
| 254 |
protected function client_mail_to() |
| 255 |
{ |
| 256 |
$mail_to = ''; |
| 257 |
$settings = $this->client_mail_settings(); |
| 258 |
if (!empty($settings) && array_key_exists('wpcargo_mail_to', $settings)) { |
| 259 |
$mail_to = $settings['wpcargo_mail_to']; |
| 260 |
} |
| 261 |
return $mail_to; |
| 262 |
} |
| 263 |
protected function admin_mail_to() |
| 264 |
{ |
| 265 |
$mail_to = ''; |
| 266 |
if (!empty(trim(get_option('wpcargo_admin_mail_to')))) { |
| 267 |
$mail_to = get_option('wpcargo_admin_mail_to'); |
| 268 |
} |
| 269 |
return $mail_to; |
| 270 |
} |
| 271 |
protected function client_mail_body() |
| 272 |
{ |
| 273 |
$mail_body = wpcargo_default_client_email_body(); |
| 274 |
$wpcargo_mail_settings = $this->client_mail_settings(); |
| 275 |
if (!empty($wpcargo_mail_settings) && array_key_exists('wpcargo_mail_message', $wpcargo_mail_settings) && !empty(trim($wpcargo_mail_settings['wpcargo_mail_message']))) { |
| 276 |
$mail_body = $wpcargo_mail_settings['wpcargo_mail_message']; |
| 277 |
} |
| 278 |
return $mail_body; |
| 279 |
} |
| 280 |
protected function admin_mail_body() |
| 281 |
{ |
| 282 |
$mail_body = wpcargo_default_admin_email_body(); |
| 283 |
if (!empty(trim(get_option('wpcargo_admin_mail_body')))) { |
| 284 |
$mail_body = get_option('wpcargo_admin_mail_body'); |
| 285 |
} |
| 286 |
return $mail_body; |
| 287 |
} |
| 288 |
protected function client_mail_footer() |
| 289 |
{ |
| 290 |
$mail_footer = wpcargo_default_email_footer(); |
| 291 |
$wpcargo_mail_settings = $this->client_mail_settings(); |
| 292 |
if (!empty($wpcargo_mail_settings) && array_key_exists('wpcargo_mail_footer', $wpcargo_mail_settings) && !empty(trim($wpcargo_mail_settings['wpcargo_mail_footer']))) { |
| 293 |
$mail_footer = $wpcargo_mail_settings['wpcargo_mail_footer']; |
| 294 |
} |
| 295 |
return $mail_footer; |
| 296 |
} |
| 297 |
protected function admin_mail_footer() |
| 298 |
{ |
| 299 |
$mail_footer = wpcargo_default_email_footer(); |
| 300 |
if (!empty(trim(get_option('wpcargo_admin_mail_footer')))) { |
| 301 |
$mail_footer = get_option('wpcargo_admin_mail_footer'); |
| 302 |
} |
| 303 |
return $mail_footer; |
| 304 |
} |
| 305 |
public function user_time($userID) |
| 306 |
{ |
| 307 |
if (!get_option('wpcargo_user_timezone')) { |
| 308 |
return current_time($this->time_format()); |
| 309 |
} |
| 310 |
|
| 311 |
$timezone = get_user_meta($userID, 'wpc_user_timezone', true); |
| 312 |
|
| 313 |
// fallback to WP timezone if none set or invalid |
| 314 |
if (empty($timezone)) { |
| 315 |
return current_time($this->time_format()); |
| 316 |
} |
| 317 |
|
| 318 |
try { |
| 319 |
$tz = new DateTimeZone($timezone); |
| 320 |
} catch (Exception $e) { |
| 321 |
$tz = wp_timezone(); |
| 322 |
} |
| 323 |
|
| 324 |
$dt = new DateTimeImmutable('now', $tz); |
| 325 |
|
| 326 |
return wp_date($this->time_format(), $dt->getTimestamp(), $tz); |
| 327 |
} |
| 328 |
public function user_date($userID) |
| 329 |
{ |
| 330 |
if (!get_option('wpcargo_user_timezone')) { |
| 331 |
return current_time($this->date_format()); |
| 332 |
} |
| 333 |
|
| 334 |
$timezone = get_user_meta($userID, 'wpc_user_timezone', true); |
| 335 |
|
| 336 |
// fallback to WP timezone if not set |
| 337 |
if (empty($timezone)) { |
| 338 |
return current_time($this->date_format()); |
| 339 |
} |
| 340 |
|
| 341 |
// If UTC-like value, just use WP default behavior |
| 342 |
if (stripos($timezone, 'UTC') !== false) { |
| 343 |
return current_time($this->date_format()); |
| 344 |
} |
| 345 |
|
| 346 |
try { |
| 347 |
$tz = new DateTimeZone($timezone); |
| 348 |
} catch (Exception $e) { |
| 349 |
$tz = wp_timezone(); |
| 350 |
} |
| 351 |
|
| 352 |
return wp_date( |
| 353 |
$this->date_format(), |
| 354 |
current_time('timestamp'), |
| 355 |
$tz |
| 356 |
); |
| 357 |
} |
| 358 |
public function agents() |
| 359 |
{ |
| 360 |
global $wpdb; |
| 361 |
$results = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}users AS tbluser LEFT JOIN {$wpdb->prefix}usermeta AS tbluserdata ON tbluser.ID = tbluserdata.user_id WHERE tbluserdata.meta_key LIKE %s", 'wp_capabilities'), OBJECT); |
| 362 |
|
| 363 |
$agent_name = array(); |
| 364 |
$users = array(); |
| 365 |
foreach ($results as $user) { |
| 366 |
array_push($agent_name, array( |
| 367 |
'id' => $user->ID, |
| 368 |
'name' => $user->display_name, |
| 369 |
'role' => array_keys(maybe_unserialize($user->meta_value)) |
| 370 |
)); |
| 371 |
} |
| 372 |
|
| 373 |
foreach ($agent_name as $aget_user => $agent) { |
| 374 |
if (in_array('cargo_agent', $agent['role'])) { |
| 375 |
$users[$agent['id']] = $agent['name']; |
| 376 |
} |
| 377 |
} |
| 378 |
return $users; |
| 379 |
} |
| 380 |
|
| 381 |
function get_shipment_agent($shipmentID) |
| 382 |
{ |
| 383 |
$agent = (int)get_post_meta($shipmentID, 'agent_fields', true); |
| 384 |
if (!is_numeric($agent)) { |
| 385 |
$agent = $this->agent_id('display_name', $agent); |
| 386 |
} |
| 387 |
return $agent; |
| 388 |
} |
| 389 |
function agent_display_name($userID) |
| 390 |
{ |
| 391 |
global $wpdb; |
| 392 |
$table_prefix = $wpdb->prefix; |
| 393 |
$display_name = $userID; |
| 394 |
if (is_numeric($userID)) { |
| 395 |
$display_name = $wpdb->get_var($wpdb->prepare('SELECT `display_name` FROM `' . $table_prefix . 'users` WHERE `ID` = %d', $userID)); |
| 396 |
} |
| 397 |
return $display_name; |
| 398 |
} |
| 399 |
function user_fullname($userID) |
| 400 |
{ |
| 401 |
$user_fullname = ''; |
| 402 |
$user = get_userdata((int)$userID); |
| 403 |
if (!empty($user)) { |
| 404 |
$user_fullname = $user->display_name; |
| 405 |
if (!empty($user->first_name) && !empty($user->last_name)) { |
| 406 |
$user_fullname = $user->first_name . ' ' . $user->last_name; |
| 407 |
} |
| 408 |
} |
| 409 |
return esc_html($user_fullname); |
| 410 |
} |
| 411 |
function agent_id( $value, string $field = 'display_name' ) { |
| 412 |
global $wpdb; |
| 413 |
|
| 414 |
$allowed_fields = array( |
| 415 |
'ID', |
| 416 |
'user_login', |
| 417 |
'user_nicename', |
| 418 |
'user_email', |
| 419 |
'user_url', |
| 420 |
'display_name', |
| 421 |
); |
| 422 |
|
| 423 |
if ( ! in_array( $field, $allowed_fields, true ) ) { |
| 424 |
$field = 'display_name'; |
| 425 |
} |
| 426 |
|
| 427 |
$query = $wpdb->prepare( |
| 428 |
"SELECT ID FROM {$wpdb->users} WHERE {$field} LIKE %s", |
| 429 |
$value |
| 430 |
); |
| 431 |
|
| 432 |
return $wpdb->get_var( $query ); |
| 433 |
} |
| 434 |
function time_format() |
| 435 |
{ |
| 436 |
$time_format = apply_filters('wpcargo_time_format', 'H:i a'); |
| 437 |
return $time_format; |
| 438 |
} |
| 439 |
function date_format() |
| 440 |
{ |
| 441 |
$date_format = apply_filters('wpcargo_date_format', 'Y-m-d'); |
| 442 |
return $date_format; |
| 443 |
} |
| 444 |
function datetime_format() |
| 445 |
{ |
| 446 |
$datetime_format = apply_filters('wpcargo_datetime_format', 'Y-m-d H:i a'); |
| 447 |
return $datetime_format; |
| 448 |
} |
| 449 |
protected function all_wpcargo_users() |
| 450 |
{ |
| 451 |
global $wpdb; |
| 452 |
$wpcargo_args = apply_filters('all_wpcargo_users', array( |
| 453 |
'role__in' => wpcargo_user_roles_list() |
| 454 |
)); |
| 455 |
|
| 456 |
$all_wpcargo_users = get_users($wpcargo_args); |
| 457 |
return $all_wpcargo_users; |
| 458 |
} |
| 459 |
public function prefix() |
| 460 |
{ |
| 461 |
$options = $this->settings(); |
| 462 |
$prefix = ''; |
| 463 |
if (array_key_exists('wpcargo_title_prefix', $options)) { |
| 464 |
$prefix = trim($options['wpcargo_title_prefix']); |
| 465 |
} |
| 466 |
return apply_filters('wpcargo_prefix', $prefix); |
| 467 |
} |
| 468 |
public function suffix() |
| 469 |
{ |
| 470 |
$suffix = get_option('wpcargo_title_suffix'); |
| 471 |
return apply_filters('wpcargo_suffix', $suffix); |
| 472 |
} |
| 473 |
protected function tax() |
| 474 |
{ |
| 475 |
$options = $this->settings(); |
| 476 |
$tax = 0; |
| 477 |
if (array_key_exists('wpcargo_tax', $options)) { |
| 478 |
$tax = floatval($options['wpcargo_tax']); |
| 479 |
} |
| 480 |
return $tax; |
| 481 |
} |
| 482 |
protected function autogenerate_title() |
| 483 |
{ |
| 484 |
$options = $this->settings(); |
| 485 |
$autogenerate = false; |
| 486 |
if (array_key_exists('wpcargo_title_prefix_action', $options)) { |
| 487 |
$autogenerate = true; |
| 488 |
} |
| 489 |
$this->autogenerate_title = $autogenerate; |
| 490 |
} |
| 491 |
protected function number_digit() |
| 492 |
{ |
| 493 |
return (get_option('wpcargo_title_numdigit')) ? get_option('wpcargo_title_numdigit') : 12; |
| 494 |
} |
| 495 |
public function create_shipment_number() |
| 496 |
{ |
| 497 |
global $wpdb; |
| 498 |
$numdigit = $this->number_digit; |
| 499 |
$numstr = ''; |
| 500 |
for ($i = 1; $i < $numdigit; $i++) { |
| 501 |
$numstr .= 9; |
| 502 |
} |
| 503 |
$rand_number = str_pad(wp_rand(0, $numstr), $numdigit, "0", STR_PAD_LEFT); |
| 504 |
$prefix_extra = apply_filters('wpcargo_prefix_extra', $this->prefix); |
| 505 |
$suffix_extra = apply_filters('wpcargo_suffix_extra', $this->suffix); |
| 506 |
$shipment_title = $prefix_extra . $rand_number . $suffix_extra; |
| 507 |
|
| 508 |
$shipment_title = apply_filters('wpcargo_generated_shipment_number', $shipment_title, $rand_number); |
| 509 |
if (! function_exists('post_exists')) { |
| 510 |
require_once(ABSPATH . 'wp-admin/includes/post.php'); |
| 511 |
} |
| 512 |
if (get_option('wpcargo_restrict_duplicate')) { |
| 513 |
if (post_exists($shipment_title)) { |
| 514 |
return $this->create_shipment_number(); |
| 515 |
} |
| 516 |
} |
| 517 |
return esc_html($shipment_title); |
| 518 |
} |
| 519 |
public function is_title_exist($title = '') |
| 520 |
{ |
| 521 |
global $wpdb; |
| 522 |
$sql = $wpdb->prepare("SELECT COUNT(*) FROM `{$wpdb->prefix}posts` WHERE `post_type` = 'wpcargo_shipment' AND `post_status` IN ('publish', 'pending', 'draft') AND `post_title` = %s", $title); |
| 523 |
$sql = apply_filters('wpcargo_is_title_exist_sql', $sql, $title); |
| 524 |
$result = $wpdb->get_var($sql); |
| 525 |
return $result; |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
$wpcargo = new WPCargo(); |
| 530 |
|