| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class to act as a wrapper for a single order |
| 4 |
*/ |
| 5 |
|
| 6 |
if ( !defined( 'ABSPATH' ) ) |
| 7 |
exit; |
| 8 |
|
| 9 |
if ( !class_exists( 'ewdotpOrder' ) ) { |
| 10 |
class ewdotpOrder { |
| 11 |
|
| 12 |
// Object properties from database |
| 13 |
public $id = 0; |
| 14 |
|
| 15 |
public $name; |
| 16 |
public $number; |
| 17 |
public $email; |
| 18 |
public $phone_number; |
| 19 |
|
| 20 |
public $status; |
| 21 |
public $external_status; |
| 22 |
public $location; |
| 23 |
|
| 24 |
public $notes_public; |
| 25 |
public $notes_private; |
| 26 |
public $customer_notes; |
| 27 |
|
| 28 |
public $customer; |
| 29 |
public $sales_rep; |
| 30 |
public $woocommerce_id; |
| 31 |
public $zendesk_id; |
| 32 |
|
| 33 |
public $status_updated; |
| 34 |
public $status_updated_fmtd; |
| 35 |
|
| 36 |
public $display; |
| 37 |
|
| 38 |
public $payment_price; |
| 39 |
public $payment_completed; |
| 40 |
public $paypal_receipt_number; |
| 41 |
|
| 42 |
public $views; |
| 43 |
|
| 44 |
public $tracking_link_clicked; |
| 45 |
public $tracking_link_code; |
| 46 |
|
| 47 |
// Stores all of the custom field values for an order |
| 48 |
public $custom_fields = array(); |
| 49 |
|
| 50 |
// Current status of the order |
| 51 |
public $current_status; |
| 52 |
|
| 53 |
// Current location of the order |
| 54 |
public $current_location; |
| 55 |
|
| 56 |
// Stores all of the previous statuses for an order |
| 57 |
public $status_history = array(); |
| 58 |
|
| 59 |
// Store any errors recorded on order submission |
| 60 |
public $validation_errors = array(); |
| 61 |
|
| 62 |
/** |
| 63 |
* Load an order based on a specific database record |
| 64 |
* @since 3.0.0 |
| 65 |
*/ |
| 66 |
public function load_order( $db_order ) { |
| 67 |
global $ewd_otp_controller; |
| 68 |
|
| 69 |
if ( ! is_object( $db_order ) ) { return false; } |
| 70 |
|
| 71 |
$this->id = is_object( $db_order ) ? $db_order->Order_ID : 0; |
| 72 |
|
| 73 |
$this->name = is_object( $db_order ) ? $db_order->Order_Name : ''; |
| 74 |
$this->number = is_object( $db_order ) ? $db_order->Order_Number : ''; |
| 75 |
$this->email = is_object( $db_order ) ? $db_order->Order_Email : ''; |
| 76 |
$this->phone_number = is_object( $db_order ) ? $db_order->Order_Phone_Number : ''; |
| 77 |
|
| 78 |
$this->status = is_object( $db_order ) ? $db_order->Order_Status : ''; |
| 79 |
$this->external_status = is_object( $db_order ) ? $db_order->Order_External_Status : ''; |
| 80 |
$this->location = is_object( $db_order ) ? $db_order->Order_Location : ''; |
| 81 |
|
| 82 |
$this->notes_public = is_object( $db_order ) ? $db_order->Order_Notes_Public : ''; |
| 83 |
$this->notes_private = is_object( $db_order ) ? $db_order->Order_Notes_Private : ''; |
| 84 |
$this->customer_notes = is_object( $db_order ) ? $db_order->Order_Customer_Notes : ''; |
| 85 |
|
| 86 |
$this->customer = is_object( $db_order ) ? $db_order->Customer_ID : 0; |
| 87 |
$this->sales_rep = is_object( $db_order ) ? $db_order->Sales_Rep_ID : 0; |
| 88 |
$this->woocommerce_id = is_object( $db_order ) ? $db_order->WooCommerce_ID : 0; |
| 89 |
$this->zendesk_id = is_object( $db_order ) ? $db_order->Zendesk_ID : 0; |
| 90 |
|
| 91 |
$this->status_updated = is_object( $db_order ) ? $db_order->Order_Status_Updated : ''; |
| 92 |
$this->status_updated_fmtd = $this->date_formatted( $this->status_updated ); |
| 93 |
|
| 94 |
$this->display = is_object( $db_order ) ? ( $db_order->Order_Display == 'Yes' ? true : false ) : false; |
| 95 |
|
| 96 |
$this->payment_price = is_object( $db_order ) ? $db_order->Order_Payment_Price : 0; |
| 97 |
$this->payment_completed = is_object( $db_order ) ? ( $db_order->Order_Payment_Completed == 'Yes' ? true : false ) : false; |
| 98 |
$this->paypal_receipt_number = is_object( $db_order ) ? $db_order->Order_PayPal_Receipt_Number : ''; |
| 99 |
|
| 100 |
$this->views = is_object( $db_order ) ? $db_order->Order_View_Count : 0; |
| 101 |
|
| 102 |
$this->tracking_link_clicked = is_object( $db_order ) ? ( $db_order->Order_Tracking_Link_Clicked == 'Yes' ? true : false ) : false; |
| 103 |
$this->tracking_link_code = is_object( $db_order ) ? $db_order->Order_Tracking_Link_Code : ''; |
| 104 |
|
| 105 |
$custom_fields = $ewd_otp_controller->settings->get_order_custom_fields(); |
| 106 |
|
| 107 |
$this->custom_fields = array(); |
| 108 |
foreach ( $custom_fields as $custom_field ) { |
| 109 |
|
| 110 |
$this->custom_fields[ $custom_field->id ] = $ewd_otp_controller->order_manager->get_field_value( $custom_field->id, $this->id ); |
| 111 |
} |
| 112 |
|
| 113 |
$statuses = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'statuses' ) ); |
| 114 |
|
| 115 |
foreach ( $statuses as $status ) { |
| 116 |
|
| 117 |
if ( $this->external_status == $status->status ) { $this->current_status = $status; } |
| 118 |
} |
| 119 |
|
| 120 |
$locations = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'locations' ) ); |
| 121 |
|
| 122 |
foreach ( $locations as $location ) { |
| 123 |
|
| 124 |
if ( $this->location == $location->name ) { $this->current_location = $location; } |
| 125 |
} |
| 126 |
|
| 127 |
return true; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Loads an order based on its ID |
| 132 |
* @since 3.0.0 |
| 133 |
*/ |
| 134 |
public function load_order_from_id( $order_id ) { |
| 135 |
global $ewd_otp_controller; |
| 136 |
|
| 137 |
$db_order = $ewd_otp_controller->order_manager->get_order_from_id( $order_id ); |
| 138 |
|
| 139 |
if ( empty( $db_order ) ) { return false; } |
| 140 |
|
| 141 |
return $this->load_order( $db_order ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Loads an order based on its tracking number |
| 146 |
* @since 3.0.0 |
| 147 |
*/ |
| 148 |
public function load_order_from_tracking_number( $order_number ) { |
| 149 |
global $ewd_otp_controller; |
| 150 |
|
| 151 |
$db_order = $ewd_otp_controller->order_manager->get_order_from_tracking_number( $order_number ); |
| 152 |
|
| 153 |
if ( empty( $db_order ) ) { return false; } |
| 154 |
|
| 155 |
return $this->load_order( $db_order ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Returns the WordPress user ID for this order's sales rep |
| 160 |
* @since 3.0.0 |
| 161 |
*/ |
| 162 |
public function get_sales_rep_wp_id() { |
| 163 |
|
| 164 |
if ( empty( $this->sales_rep ) ) { return 0; } |
| 165 |
|
| 166 |
$sales_rep = new ewdotpSalesRep(); |
| 167 |
|
| 168 |
$sales_rep->load_sales_rep_from_id( $this->sales_rep ); |
| 169 |
|
| 170 |
return ! empty( $sales_rep->wp_id ) ? $sales_rep->wp_id : 0; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Loads an order's status history |
| 175 |
* @since 3.0.0 |
| 176 |
*/ |
| 177 |
public function load_order_status_history() { |
| 178 |
global $ewd_otp_controller; |
| 179 |
|
| 180 |
$this->status_history = array(); |
| 181 |
|
| 182 |
$db_status_history = $ewd_otp_controller->order_manager->get_order_status_history( $this->id ); |
| 183 |
|
| 184 |
foreach ( $db_status_history as $db_status ) { |
| 185 |
|
| 186 |
$status_history_object = new stdClass(); |
| 187 |
|
| 188 |
$status_history_object->id = $db_status->Order_Status_ID; |
| 189 |
$status_history_object->status = $db_status->Order_Status; |
| 190 |
$status_history_object->location = $db_status->Order_Location; |
| 191 |
$status_history_object->internal_status = $db_status->Order_Internal_Status; |
| 192 |
$status_history_object->updated = $db_status->Order_Status_Created; |
| 193 |
$status_history_object->updated_fmtd = $this->date_formatted( $status_history_object->updated ); |
| 194 |
|
| 195 |
$this->status_history[] = $status_history_object; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Verify that the submitted email matches the one belonging to the order |
| 201 |
* @since 3.0.0 |
| 202 |
*/ |
| 203 |
public function verify_order_email( $email_address ) { |
| 204 |
|
| 205 |
if ( (empty( $email_address) and empty( $this->email ) ) or strtolower( $email_address ) == strtolower( $this->email ) ) { return true; } |
| 206 |
|
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Verify that the submitted user ID matches either the customer or sales rep for this order |
| 212 |
* @since 3.0.15 |
| 213 |
*/ |
| 214 |
public function verify_order_user( $user_id ) { |
| 215 |
global $ewd_otp_controller; |
| 216 |
|
| 217 |
if ( empty( $user_id ) ) { return false; } |
| 218 |
|
| 219 |
if ( $this->customer == $ewd_otp_controller->customer_manager->get_customer_id_from_wp_id( $user_id ) ) { return true; } |
| 220 |
|
| 221 |
if ( $this->sales_rep == $ewd_otp_controller->sales_rep_manager->get_sales_rep_id_from_wp_id( $user_id ) ) { return true; } |
| 222 |
|
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Validates a submitted order, and calls insert_order if validated |
| 228 |
* @since 3.0.0 |
| 229 |
*/ |
| 230 |
public function process_client_order_submission() { |
| 231 |
global $ewd_otp_controller; |
| 232 |
|
| 233 |
$this->validate_submission(); |
| 234 |
if ( $this->is_valid_submission() === false ) { |
| 235 |
return false; |
| 236 |
} |
| 237 |
|
| 238 |
$this->insert_order(); |
| 239 |
|
| 240 |
if ( ! empty( $this->status ) ) { $this->insert_order_status(); } |
| 241 |
|
| 242 |
do_action( 'ewd_otp_insert_customer_order', $this ); |
| 243 |
|
| 244 |
return true; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Validate submission data. Expects to find data in $_POST. |
| 249 |
* @since 3.0.0 |
| 250 |
*/ |
| 251 |
public function validate_submission() { |
| 252 |
global $ewd_otp_controller; |
| 253 |
|
| 254 |
$this->validation_errors = array(); |
| 255 |
|
| 256 |
// reCAPTCHA |
| 257 |
if ( $ewd_otp_controller->settings->get_setting( 'use-captcha' ) == 'recaptcha' ) { |
| 258 |
|
| 259 |
if ( ! isset( $_POST['g-recaptcha-response'] ) ) { |
| 260 |
|
| 261 |
$this->validation_errors[] = array( |
| 262 |
'field' => 'recaptcha', |
| 263 |
'error_msg' => 'No reCAPTCHA code', |
| 264 |
'message' => __( 'Please fill out the reCAPTCHA box before submitting.', 'order-tracking' ), |
| 265 |
); |
| 266 |
|
| 267 |
} |
| 268 |
else { |
| 269 |
|
| 270 |
$secret_key = $ewd_otp_controller->settings->get_setting( 'captcha-secret-key' ); |
| 271 |
$captcha = sanitize_text_field( $_POST['g-recaptcha-response'] ); |
| 272 |
|
| 273 |
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode( $secret_key ) . '&response=' . urlencode( $captcha ); |
| 274 |
$json_response = file_get_contents( $url ); |
| 275 |
$response = json_decode( $json_response ); |
| 276 |
|
| 277 |
$reCaptcha_error = false; |
| 278 |
if ( json_last_error() != JSON_ERROR_NONE ) { |
| 279 |
|
| 280 |
$response = new stdClass(); |
| 281 |
$response->success = false; |
| 282 |
$reCaptcha_error = true; |
| 283 |
|
| 284 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 285 |
|
| 286 |
error_log( 'ORP reCAPTCHA error. Raw respose: ' . print_r( array( $json_response ), true ) ); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
if ( ! $response->success ) { |
| 291 |
|
| 292 |
$message = __( 'Please fill out the reCAPTCHA box again and re-submit.', 'ultimate-reviews' ); |
| 293 |
|
| 294 |
if ( $reCaptcha_error ) { |
| 295 |
|
| 296 |
$message .= __( 'If you encounter reCAPTCHA error multiple times, please contact us.', 'ultimate-reviews' ); |
| 297 |
} |
| 298 |
|
| 299 |
$this->validation_errors[] = array( |
| 300 |
'field' => 'recaptcha', |
| 301 |
'error_msg' => 'Invalid reCAPTCHA code', |
| 302 |
'message' => $message, |
| 303 |
); |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
// Order Basics |
| 309 |
$this->number = $ewd_otp_controller->settings->get_setting( 'customer-order-number-prefix' ) . ewd_random_string( 5 ) . $ewd_otp_controller->settings->get_setting( 'customer-order-number-suffix' ); |
| 310 |
|
| 311 |
$this->name = empty( $_POST['ewd_otp_order_name'] ) ? '' : sanitize_text_field( $_POST['ewd_otp_order_name'] ); |
| 312 |
$this->email = empty( $_POST['ewd_otp_order_email'] ) ? '' : sanitize_text_field( $_POST['ewd_otp_order_email'] ); |
| 313 |
$this->location = empty( $_POST['ewd_otp_location'] ) ? '' : sanitize_text_field( $_POST['ewd_otp_location'] ); |
| 314 |
$this->customer_notes = empty( $_POST['ewd_otp_customer_notes'] ) ? '' : sanitize_textarea_field( $_POST['ewd_otp_customer_notes'] ); |
| 315 |
|
| 316 |
$this->external_status = $this->status = $ewd_otp_controller->settings->get_setting( 'default-customer-order-form-status' ); |
| 317 |
$this->display = true; |
| 318 |
|
| 319 |
if ( empty( $this->status ) ) { |
| 320 |
|
| 321 |
$statuses = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'statuses' ) ); |
| 322 |
|
| 323 |
$status = reset( $statuses ); |
| 324 |
|
| 325 |
$this->external_status = $this->status = $status->status; |
| 326 |
} |
| 327 |
|
| 328 |
// Customer |
| 329 |
$user = wp_get_current_user(); |
| 330 |
|
| 331 |
if ( $user ) { |
| 332 |
|
| 333 |
$this->customer = $ewd_otp_controller->customer_manager->get_customer_id_from_wp_id( $user->ID ); |
| 334 |
} |
| 335 |
elseif ( function_exists( 'FEUP_User' ) ) { |
| 336 |
|
| 337 |
$feup_user = new FEUP_User(); |
| 338 |
|
| 339 |
if ( $feup_user->Is_Logged_In() ) { |
| 340 |
|
| 341 |
$this->customer = $ewd_otp_controller->customer_manager->get_customer_id_from_feup_id( $feup_user->Get_User_ID() ); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
if ( empty( $this->customer ) and ! empty( $this->email ) and $ewd_otp_controller->settings->get_setting( 'allow-assign-orders-to-customers' ) ) { |
| 346 |
|
| 347 |
$this->customer = $ewd_otp_controller->customer_manager->get_customer_id_from_email( $this->email ); |
| 348 |
} |
| 349 |
|
| 350 |
// Sales Rep |
| 351 |
if ( ! empty( $this->customer ) ) { |
| 352 |
|
| 353 |
$this->sales_rep = $ewd_otp_controller->customer_manager->get_customer_field( 'Sales_Rep_ID', $this->customer ); |
| 354 |
} |
| 355 |
|
| 356 |
if ( empty( $this->sales_rep ) ){ |
| 357 |
|
| 358 |
$this->sales_rep = empty( $_POST['ewd_otp_sales_rep'] ) ? $ewd_otp_controller->settings->get_setting( 'default-sales-rep' ) : intval( $_POST['ewd_otp_sales_rep'] ); |
| 359 |
} |
| 360 |
|
| 361 |
$custom_fields = $ewd_otp_controller->settings->get_order_custom_fields(); |
| 362 |
|
| 363 |
foreach ( $custom_fields as $custom_field ) { |
| 364 |
|
| 365 |
$input_name = 'ewd_otp_custom_field_' . $custom_field->id; |
| 366 |
|
| 367 |
if ( $custom_field->type == 'checkbox' ) { $this->custom_fields[ $custom_field->id ] = ( empty( $_POST[ $input_name ] ) or ! is_array( $_POST[ $input_name ] ) ) ? array() : sanitize_text_field( implode( ',', $_POST[ $input_name ] ) ); } |
| 368 |
elseif ( $custom_field->type == 'textarea' ) { $this->custom_fields[ $custom_field->id ] = empty( $_POST[ $input_name ] ) ? false : sanitize_textarea_field( $_POST[ $input_name ] ); } |
| 369 |
else { $this->custom_fields[ $custom_field->id ] = empty( $_POST[ $input_name ] ) ? false : sanitize_text_field( $_POST[ $input_name ] ); } |
| 370 |
} |
| 371 |
|
| 372 |
do_action( 'ewd_otp_validate_order_submission', $this ); |
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* Validates a submitted order, and calls insert_order if validated |
| 377 |
* @since 3.0.0 |
| 378 |
*/ |
| 379 |
public function process_admin_order_submission() { |
| 380 |
global $ewd_otp_controller; |
| 381 |
|
| 382 |
$this->validate_admin_submission(); |
| 383 |
|
| 384 |
if ( $this->is_valid_submission() === false ) { |
| 385 |
return false; |
| 386 |
} |
| 387 |
|
| 388 |
if ( $this->id ) { |
| 389 |
|
| 390 |
$old_status = $ewd_otp_controller->order_manager->get_order_field( 'status', $this->id ); |
| 391 |
|
| 392 |
$this->update_order(); |
| 393 |
|
| 394 |
if ( $this->status != $old_status ) { |
| 395 |
|
| 396 |
$this->insert_order_status(); |
| 397 |
} |
| 398 |
|
| 399 |
do_action( 'ewd_otp_admin_order_updated', $this, $old_status ); |
| 400 |
} |
| 401 |
else { |
| 402 |
|
| 403 |
$this->insert_order(); |
| 404 |
|
| 405 |
$this->insert_order_status(); |
| 406 |
|
| 407 |
do_action( 'ewd_otp_admin_order_inserted', $this ); |
| 408 |
} |
| 409 |
|
| 410 |
return true; |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* Validate submission data entered via the admin page |
| 415 |
* @since 3.0.0 |
| 416 |
*/ |
| 417 |
public function validate_admin_submission() { |
| 418 |
global $ewd_otp_controller; |
| 419 |
|
| 420 |
$this->validation_errors = array(); |
| 421 |
|
| 422 |
if ( ! isset( $_POST['ewd-otp-admin-nonce'] ) |
| 423 |
or ! wp_verify_nonce( $_POST['ewd-otp-admin-nonce'], 'ewd-otp-admin-nonce' ) |
| 424 |
) { |
| 425 |
$this->validation_errors[] = __( 'The request has been rejected because it does not appear to have come from this site.', 'order-tracking' ); |
| 426 |
} |
| 427 |
|
| 428 |
// Order Data |
| 429 |
$this->number = empty( $_POST['ewd_otp_number'] ) ? '' : sanitize_text_field( $_POST['ewd_otp_number'] ); |
| 430 |
$this->name = empty( $_POST['ewd_otp_name'] ) ? '' : sanitize_text_field( $_POST['ewd_otp_name'] ); |
| 431 |
$this->email = empty( $_POST['ewd_otp_email'] ) ? '' : sanitize_text_field( $_POST['ewd_otp_email'] ); |
| 432 |
|
| 433 |
$this->status = $this->external_status = empty( $_POST['ewd_otp_status'] ) ? '' : sanitize_text_field( $_POST['ewd_otp_status'] ); |
| 434 |
$this->location = empty( $_POST['ewd_otp_location'] ) ? '' : sanitize_text_field( $_POST['ewd_otp_location'] ); |
| 435 |
$this->display = ( empty( $_POST['ewd_otp_display'] ) or $_POST['ewd_otp_display'] == 'no' ) ? false : true; |
| 436 |
|
| 437 |
$statuses = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'statuses' ) ); |
| 438 |
|
| 439 |
// Overwrite the external status of the order if the new status is an internal one |
| 440 |
foreach ( $statuses as $status ) { |
| 441 |
|
| 442 |
if ( $this->status == $status->status and $status->internal == 'yes' ) { |
| 443 |
|
| 444 |
$this->external_status = $ewd_otp_controller->order_manager->get_order_field( 'Order_External_Status', $this->id ); |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
$this->notes_public = empty( $_POST['ewd_otp_public_notes'] ) ? '' : sanitize_textarea_field( $_POST['ewd_otp_public_notes'] ); |
| 449 |
$this->notes_private = empty( $_POST['ewd_otp_private_notes'] ) ? '' : sanitize_textarea_field( $_POST['ewd_otp_private_notes'] ); |
| 450 |
$this->customer_notes = empty( $_POST['ewd_otp_customer_notes'] ) ? '' : sanitize_textarea_field( $_POST['ewd_otp_customer_notes'] ); |
| 451 |
|
| 452 |
$this->customer = empty( $_POST['ewd_otp_customer'] ) ? 0 : sanitize_text_field( $_POST['ewd_otp_customer'] ); |
| 453 |
$this->sales_rep = empty( $_POST['ewd_otp_sales_rep'] ) ? 0 : sanitize_text_field( $_POST['ewd_otp_sales_rep'] ); |
| 454 |
|
| 455 |
$this->payment_price = empty( $_POST['ewd_otp_payment_price'] ) ? '' : sanitize_text_field( $_POST['ewd_otp_payment_price'] ); |
| 456 |
$this->payment_completed = ( ! empty( $_POST['ewd_otp_payment_completed'] ) and $_POST['ewd_otp_payment_completed'] == 'yes' ) ? true : false; |
| 457 |
$this->paypal_receipt_number = empty( $_POST['ewd_otp_paypal_receipt_number'] ) ? '' : sanitize_text_field( $_POST['ewd_otp_paypal_receipt_number'] ); |
| 458 |
|
| 459 |
$custom_fields = $ewd_otp_controller->settings->get_order_custom_fields(); |
| 460 |
|
| 461 |
foreach ( $custom_fields as $custom_field ) { |
| 462 |
|
| 463 |
$input_name = 'ewd-otp-custom-field-' . $custom_field->id; |
| 464 |
|
| 465 |
if ( $custom_field->type == 'checkbox' ) { $this->custom_fields[ $custom_field->id ] = ( empty( $_POST[ $input_name ] ) or ! is_array( $_POST[ $input_name ] ) ) ? '' : sanitize_text_field( implode( ',', $_POST[ $input_name ] ) ); } |
| 466 |
elseif ( $custom_field->type == 'textarea' ) { $this->custom_fields[ $custom_field->id ] = empty( $_POST[ $input_name ] ) ? false : sanitize_textarea_field( $_POST[ $input_name ] ); } |
| 467 |
elseif ( $custom_field->type == 'file' or $custom_field->type == 'image' ) { $this->custom_fields[ $custom_field->id ] = ! empty( $_FILES[ $input_name ]['name'] ) ? $this->handle_file_upload( $input_name ) : ( ! empty( $_POST[ $input_name ] ) ? sanitize_text_field( $_POST[ $input_name ] ) : '' ); } |
| 468 |
else { $this->custom_fields[ $custom_field->id ] = empty( $_POST[ $input_name ] ) ? false : sanitize_text_field( $_POST[ $input_name ] ); } |
| 469 |
} |
| 470 |
|
| 471 |
do_action( 'ewd_otp_validate_order_submission', $this ); |
| 472 |
} |
| 473 |
|
| 474 |
/** |
| 475 |
* Takes an input name, uploads the file from that input if it exists, returns the file URL |
| 476 |
* @since 3.0.0 |
| 477 |
*/ |
| 478 |
public function handle_file_upload( $input_name ) { |
| 479 |
|
| 480 |
if ( ! function_exists( 'wp_handle_upload' ) ) { |
| 481 |
|
| 482 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 483 |
} |
| 484 |
|
| 485 |
$args = array( |
| 486 |
'test_form' => false |
| 487 |
); |
| 488 |
|
| 489 |
$uploaded_file = wp_handle_upload( $_FILES[ $input_name ], $args ); |
| 490 |
|
| 491 |
if ( $uploaded_file && empty( $uploaded_file['error'] ) ) { |
| 492 |
|
| 493 |
return $uploaded_file['url']; |
| 494 |
} |
| 495 |
else { |
| 496 |
|
| 497 |
return false; |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
* Check if submission is valid |
| 503 |
* @since 3.0.0 |
| 504 |
*/ |
| 505 |
public function is_valid_submission() { |
| 506 |
|
| 507 |
if ( !count( $this->validation_errors ) ) { |
| 508 |
return true; |
| 509 |
} |
| 510 |
|
| 511 |
return false; |
| 512 |
} |
| 513 |
|
| 514 |
/** |
| 515 |
* Takes a status, correctly updates the orders status based on whether its an internal or external status |
| 516 |
* @since 3.0.0 |
| 517 |
*/ |
| 518 |
public function set_status( $new_status ) { |
| 519 |
global $ewd_otp_controller; |
| 520 |
|
| 521 |
$old_status = $ewd_otp_controller->order_manager->get_order_field( 'status', $this->id ); |
| 522 |
|
| 523 |
$statuses = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'statuses' ) ); |
| 524 |
|
| 525 |
$internal_status = false; |
| 526 |
|
| 527 |
foreach ( $statuses as $status ) { |
| 528 |
|
| 529 |
if ( $status->status == $new_status and $status->internal == 'yes' ) { $internal_status = true; } |
| 530 |
} |
| 531 |
|
| 532 |
if ( ! $internal_status ) { |
| 533 |
|
| 534 |
$this->external_status = $new_status; |
| 535 |
} |
| 536 |
|
| 537 |
$this->status = $new_status; |
| 538 |
|
| 539 |
$this->update_order(); |
| 540 |
|
| 541 |
$this->insert_order_status(); |
| 542 |
|
| 543 |
if ( ! $internal_status ) { |
| 544 |
|
| 545 |
do_action( 'ewd_otp_status_updated', $this, $old_status ); |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
/** |
| 550 |
* Takes a location and updates the orders location |
| 551 |
* @since 3.0.0 |
| 552 |
*/ |
| 553 |
public function set_location( $new_location ) { |
| 554 |
global $ewd_otp_controller; |
| 555 |
|
| 556 |
$old_location = $this->location; |
| 557 |
|
| 558 |
$this->location = $new_location; |
| 559 |
|
| 560 |
$this->update_order(); |
| 561 |
|
| 562 |
$this->insert_order_status(); |
| 563 |
|
| 564 |
do_action( 'ewd_otp_location_updated', $this, $old_location ); |
| 565 |
} |
| 566 |
|
| 567 |
/** |
| 568 |
* Takes a tracking link code and updates the tracking code value in the database for this order |
| 569 |
* @since 3.0.0 |
| 570 |
*/ |
| 571 |
public function set_tracking_link_code( $tracking_code ) { |
| 572 |
|
| 573 |
$this->tracking_link_code = $tracking_code; |
| 574 |
|
| 575 |
$this->update_order(); |
| 576 |
} |
| 577 |
|
| 578 |
/** |
| 579 |
* Takes a tracking link code and verifies that it matches the one currently saved for the order |
| 580 |
* @since 3.0.0 |
| 581 |
*/ |
| 582 |
public function verify_tracking_link( $tracking_code ) { |
| 583 |
|
| 584 |
return $this->tracking_link_code == $tracking_code ? true : false; |
| 585 |
} |
| 586 |
|
| 587 |
/** |
| 588 |
* Update the tracking link clicked flag for this order |
| 589 |
* @since 3.0.0 |
| 590 |
*/ |
| 591 |
public function set_tracking_link_clicked() { |
| 592 |
|
| 593 |
$this->tracking_link_clicked = true; |
| 594 |
|
| 595 |
$this->update_order(); |
| 596 |
} |
| 597 |
|
| 598 |
/** |
| 599 |
* Increase the view count for this order |
| 600 |
* @since 3.0.0 |
| 601 |
*/ |
| 602 |
public function increase_view_count() { |
| 603 |
global $ewd_otp_controller; |
| 604 |
|
| 605 |
$ewd_otp_controller->order_manager->increase_order_views( $this->id ); |
| 606 |
} |
| 607 |
|
| 608 |
/** |
| 609 |
* Insert a new order into the database |
| 610 |
* @since 3.0.0 |
| 611 |
*/ |
| 612 |
public function insert_order() { |
| 613 |
global $ewd_otp_controller; |
| 614 |
|
| 615 |
$id = $ewd_otp_controller->order_manager->insert_order( $this ); |
| 616 |
|
| 617 |
$this->id = $id; |
| 618 |
} |
| 619 |
|
| 620 |
public function insert_order_status() { |
| 621 |
global $ewd_otp_controller; |
| 622 |
|
| 623 |
if ( ! $this->id ) { return; } |
| 624 |
|
| 625 |
$this->status_updated = date( 'Y-m-d H:i:s' ); |
| 626 |
|
| 627 |
$ewd_otp_controller->order_manager->update_order_status( $this ); |
| 628 |
} |
| 629 |
|
| 630 |
/** |
| 631 |
* Update an order already in the database |
| 632 |
* @since 3.0.0 |
| 633 |
*/ |
| 634 |
public function update_order() { |
| 635 |
global $ewd_otp_controller; |
| 636 |
|
| 637 |
$ewd_otp_controller->order_manager->update_order( $this ); |
| 638 |
|
| 639 |
} |
| 640 |
|
| 641 |
/** |
| 642 |
* Returns the date/time formatted based on the use WP timezone settings and formats |
| 643 |
* @since 3.0.0 |
| 644 |
*/ |
| 645 |
public function date_formatted( $input ) { |
| 646 |
global $ewd_otp_controller; |
| 647 |
|
| 648 |
$output = $input; |
| 649 |
|
| 650 |
if ( $ewd_otp_controller->settings->get_setting( 'use-wp-timezone' ) ) { |
| 651 |
|
| 652 |
$wp_tz = wp_timezone(); |
| 653 |
$current_tz = new DateTime( $input, new DateTimeZone( date_default_timezone_get() ) ); |
| 654 |
$offset = $wp_tz->getOffset( $current_tz ); |
| 655 |
|
| 656 |
$output = date( |
| 657 |
get_option( 'date_format' ).' '.get_option( 'time_format' ), |
| 658 |
( $current_tz->format( 'U' ) + $offset ) |
| 659 |
); |
| 660 |
} |
| 661 |
|
| 662 |
return $output; |
| 663 |
} |
| 664 |
} |
| 665 |
} |