| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 3 |
|
| 4 |
if ( ! class_exists( 'ewdotpWooCommerce' ) ) { |
| 5 |
/** |
| 6 |
* Class to handle interactions with the WooCommerce platform |
| 7 |
* |
| 8 |
* @since 3.0.0 |
| 9 |
*/ |
| 10 |
class ewdotpWooCommerce { |
| 11 |
|
| 12 |
public function __construct() { |
| 13 |
|
| 14 |
add_action( 'init', array( $this, 'add_hooks' ) ); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Adds in the necessary hooks to handle WooCommerce integration |
| 19 |
* @since 3.0.0 |
| 20 |
*/ |
| 21 |
public function add_hooks() { |
| 22 |
global $ewd_otp_controller; |
| 23 |
|
| 24 |
if ( empty( $ewd_otp_controller->settings->get_setting( 'woocommerce-integration' ) ) ) { return; } |
| 25 |
|
| 26 |
add_action( 'woocommerce_checkout_order_processed', array( $this, 'add_order' ) ); |
| 27 |
add_action( 'woocommerce_order_status_changed', array( $this, 'update_order' ) ); |
| 28 |
|
| 29 |
if ( $ewd_otp_controller->settings->get_setting( 'woocommerce-replace-statuses' ) ) { |
| 30 |
|
| 31 |
add_action( 'wc_order_statuses', array( $this, 'filter_statuses' ) ); |
| 32 |
|
| 33 |
add_filter( 'bulk_actions-edit-shop_order', array( $this, 'add_custom_status_bulk_actions' ), 99 ); |
| 34 |
|
| 35 |
add_filter( 'woocommerce_payment_complete_order_status', array( $this, 'get_equivalent_status' ) ); |
| 36 |
add_filter( 'woocommerce_valid_order_statuses_for_order_again', array( $this, 'get_equivalent_status' ) ); |
| 37 |
add_filter( 'woocommerce_valid_order_statuses_for_cancel', array( $this, 'get_equivalent_status' ) ); |
| 38 |
add_filter( 'woocommerce_bacs_process_payment_order_status', array( $this, 'get_equivalent_status' ) ); |
| 39 |
add_filter( 'woocommerce_default_order_status', array( $this, 'get_equivalent_status' ) ); |
| 40 |
add_filter( 'woocommerce_valid_order_statuses_for_payment', array( $this, 'get_equivalent_status' ) ); |
| 41 |
add_filter( 'woocommerce_valid_order_statuses_for_payment_complete', array( $this, 'get_equivalent_status' ) ); |
| 42 |
add_filter( 'woocommerce_valid_order_statuses_for_cancel', array( $this, 'get_equivalent_status' ) ); |
| 43 |
add_filter( 'woocommerce_reports_order_statuses', array( $this, 'get_equivalent_status' ) ); |
| 44 |
|
| 45 |
add_filter( 'woocommerce_reports_get_order_report_data_args', array( $this, 'report_parent_statuses' ) ); |
| 46 |
} |
| 47 |
|
| 48 |
if ( $ewd_otp_controller->settings->get_setting( 'woocommerce-show-on-order-page' ) ) { |
| 49 |
|
| 50 |
add_action( 'woocommerce_order_details_after_order_table', array( $this, 'add_tracking_to_order_page' ) ); |
| 51 |
} |
| 52 |
|
| 53 |
if ( $ewd_otp_controller->settings->get_setting( 'woocommerce-locations-enabled' ) ) { |
| 54 |
|
| 55 |
add_action( 'woocommerce_admin_order_data_after_order_details', array( $this, 'add_order_location' ) ); |
| 56 |
add_action( 'save_post_shop_order', array( $this, 'save_wc_location' ), 1 ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Automatically create an OTP order after WC checkout, if enabled |
| 62 |
* @since 3.0.0 |
| 63 |
*/ |
| 64 |
public function add_order( $post_id ) { |
| 65 |
global $ewd_otp_controller; |
| 66 |
|
| 67 |
if ( get_post_type( $post_id ) != 'shop_order' ) { return; } |
| 68 |
|
| 69 |
$woocommerce_order = new WC_Order( $post_id ); |
| 70 |
|
| 71 |
$order = new ewdotpOrder(); |
| 72 |
|
| 73 |
$order->name = __( 'WooCommerce Order #', 'order-tracking' ) . $post_id; |
| 74 |
$order->number = $ewd_otp_controller->settings->get_setting( 'woocommerce-prefix' ) . $post_id . ( ! $ewd_otp_controller->settings->get_setting( 'woocommerce-disable-random-suffix' ) ? ewd_random_string( 4 ) : '' ); |
| 75 |
$order->email = get_post_meta( $post_id, '_billing_email', true ); |
| 76 |
|
| 77 |
$order->display = true; |
| 78 |
|
| 79 |
$order->woocommerce_id = $post_id; |
| 80 |
$order->payment_completed = true; |
| 81 |
|
| 82 |
$order->status = $order->external_status = $this->get_wc_status( get_post_status( $post_id ) ); |
| 83 |
|
| 84 |
if ( $woocommerce_order->get_customer_id() ) { $order->customer = $ewd_otp_controller->customer_manager->get_customer_id_from_wp_id( $woocommerce_order->get_customer_id() ); } |
| 85 |
else { $order->customer = $ewd_otp_controller->customer_manager->get_customer_id_from_name( get_post_meta($post_id, '_billing_first_name', true ) . ' ' . get_post_meta($post_id, '_billing_last_name', true ) ); } |
| 86 |
|
| 87 |
$custom_fields = get_option( 'ewd-otp-custom-fields' ); |
| 88 |
|
| 89 |
foreach ( $custom_fields as $custom_field ) { |
| 90 |
|
| 91 |
if ( $custom_field->equivalent == 'none' ) { continue; } |
| 92 |
|
| 93 |
$order->custom_fields[ $custom_field->id ] = get_post_meta( $post_id, $custom_field->equivalent, true ); |
| 94 |
} |
| 95 |
|
| 96 |
$order->insert_order(); |
| 97 |
|
| 98 |
$order->insert_order_status(); |
| 99 |
|
| 100 |
do_action( 'ewd_otp_admin_order_inserted', $this ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Update an WC order's OTP equivalent, when the WC order gets a new status |
| 105 |
* @since 3.0.0 |
| 106 |
*/ |
| 107 |
public function update_order( $post_id, $old_status = '', $new_status = '' ) { |
| 108 |
global $ewd_otp_controller; |
| 109 |
|
| 110 |
if ( get_post_type( $post_id ) != 'shop_order' ) { return; } |
| 111 |
|
| 112 |
$woocommerce_order = $ewd_otp_controller->order_manager->get_order_from_woocommerce_id( $post_id ); |
| 113 |
|
| 114 |
if ( ! $woocommerce_order ) { return; } |
| 115 |
|
| 116 |
$order = new ewdotpOrder(); |
| 117 |
$order->load_order( $woocommerce_order ); |
| 118 |
|
| 119 |
$order->set_status( $this->get_wc_status( get_post_status( $post_id ) ) ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Replace the WC statuses with the OTP statuses for WC products |
| 124 |
* @since 3.0.0 |
| 125 |
*/ |
| 126 |
public function filter_statuses( $wc_statuses ) { |
| 127 |
global $ewd_otp_controller; |
| 128 |
global $wp_post_statuses; |
| 129 |
|
| 130 |
$statuses = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'statuses' ) ); |
| 131 |
|
| 132 |
$wc_statuses = array(); |
| 133 |
foreach ( $statuses as $status ) { |
| 134 |
|
| 135 |
$sanitized_status = sanitize_title( $status->status, '', 'ewd_otp' ); |
| 136 |
|
| 137 |
$wc_statuses[ 'wc-' . $sanitized_status ] = $status->status; |
| 138 |
|
| 139 |
if ( ! empty( $wp_post_statuses[ 'wc-' . $sanitized_status ] ) ) { continue; } |
| 140 |
|
| 141 |
$args = array( |
| 142 |
'name' => 'wc-' . $sanitized_status, |
| 143 |
'label' => $status->status, |
| 144 |
'label_count' => false, |
| 145 |
'exclude_from_search' => null, |
| 146 |
'_builtin' => false, |
| 147 |
'internal' => null, |
| 148 |
'protected' => null, |
| 149 |
'private' => null, |
| 150 |
'publicly_queryable' => null, |
| 151 |
'show_in_admin_status_list' => null, |
| 152 |
'show_in_admin_all_list' => true, |
| 153 |
'post_type' => array( 'shop_order' ) |
| 154 |
); |
| 155 |
|
| 156 |
$wp_post_statuses[ 'wc-' . $sanitized_status ] = (object) $args; |
| 157 |
} |
| 158 |
|
| 159 |
return $wc_statuses; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Allow WC orders to be set one of the different OTP statuses |
| 164 |
* @since 3.0.0 |
| 165 |
*/ |
| 166 |
public function add_custom_status_bulk_actions( $actions ) { |
| 167 |
global $ewd_otp_controller; |
| 168 |
|
| 169 |
$statuses = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'statuses' ) ); |
| 170 |
|
| 171 |
if ( isset( $actions['mark_processing'] ) ) { unset( $actions['mark_processing'] ); } |
| 172 |
if ( isset( $actions['mark_on-hold'] ) ) { unset( $actions['mark_on-hold'] ); } |
| 173 |
if ( isset( $actions['mark_completed'] ) ) { unset( $actions['mark_completed'] ); } |
| 174 |
|
| 175 |
foreach ( $statuses as $status ) { |
| 176 |
|
| 177 |
$sanitized_status = sanitize_title( $status->status, '', 'ewd_otp' ); |
| 178 |
|
| 179 |
$actions[ 'mark_' . $sanitized_status ] = __('Change status to ', 'order-tracking') . $status->status; |
| 180 |
} |
| 181 |
|
| 182 |
return $actions; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Get the OTP equivalent for one or multiple WC statuses |
| 187 |
* @since 3.0.0 |
| 188 |
*/ |
| 189 |
public function get_equivalent_status( $statuses ) { |
| 190 |
global $ewd_otp_controller; |
| 191 |
|
| 192 |
$statuses_array = is_array( $statuses ) ? $statuses : (array) $statuses; |
| 193 |
|
| 194 |
$equivalent_statuses = array( |
| 195 |
'completed' => sanitize_title( $ewd_otp_controller->settings->get_setting( 'woocommerce-paid-status' ), '', 'ewd_otp' ), |
| 196 |
'pending' => sanitize_title( $ewd_otp_controller->settings->get_setting( 'woocommerce-unpaid-status' ), '', 'ewd_otp' ), |
| 197 |
'processing' => sanitize_title( $ewd_otp_controller->settings->get_setting( 'woocommerce-processing-status' ), '', 'ewd_otp' ), |
| 198 |
'cancelled' => sanitize_title( $ewd_otp_controller->settings->get_setting( 'woocommerce-cancelled-status' ), '', 'ewd_otp' ), |
| 199 |
'on-hold' => sanitize_title( $ewd_otp_controller->settings->get_setting( 'woocommerce-onhold-status' ), '', 'ewd_otp' ), |
| 200 |
'failed' => sanitize_title( $ewd_otp_controller->settings->get_setting( 'woocommerce-failed-status' ), '', 'ewd_otp' ), |
| 201 |
'refunded' => sanitize_title( $ewd_otp_controller->settings->get_setting( 'woocommerce-refunded-status' ), '', 'ewd_otp' ) |
| 202 |
); |
| 203 |
|
| 204 |
$return_statuses = array(); |
| 205 |
foreach ( $statuses_array as $status ) { |
| 206 |
|
| 207 |
$return_statuses[] = $equivalent_statuses[ $status ]; |
| 208 |
} |
| 209 |
|
| 210 |
return is_array( $statuses ) ? $return_statuses : reset( $return_statuses ); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Return the OTP equivalent status for the parent_order_status query_param |
| 215 |
* @since 3.0.0 |
| 216 |
*/ |
| 217 |
public function report_parent_statuses( $query_params ) { |
| 218 |
|
| 219 |
if ( isset( $query_params['parent_order_status'] ) ) { |
| 220 |
|
| 221 |
$equivalent_status = $this->get_equivalent_status( $query_params['parent_order_status'] ); |
| 222 |
$query_params['parent_order_status'] = $equivalent_status; |
| 223 |
} |
| 224 |
|
| 225 |
return $query_params; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Adds the tracking information for an order to that order's page |
| 230 |
* @since 3.0.0 |
| 231 |
*/ |
| 232 |
public function add_tracking_to_order_page( $wc_order ) { |
| 233 |
global $ewd_otp_controller; |
| 234 |
|
| 235 |
$db_order = $ewd_otp_controller->order_manager->get_order_from_woocommerce_id( $wc_order->get_order_number() ); |
| 236 |
|
| 237 |
if ( empty( $db_order ) ) { return; } |
| 238 |
|
| 239 |
$order = new ewdotpOrder(); |
| 240 |
$order->load_order( $db_order ); |
| 241 |
$order->load_order_status_history(); |
| 242 |
|
| 243 |
?> |
| 244 |
|
| 245 |
<h2> |
| 246 |
<?php _e( 'Tracking Information', 'order-tracking' ); ?> |
| 247 |
</h2> |
| 248 |
|
| 249 |
<table class='shop_table shop_table_responsive'> |
| 250 |
|
| 251 |
<thead> |
| 252 |
|
| 253 |
<tr> |
| 254 |
|
| 255 |
<th><?php _e( 'Order Status', 'order-tracking' ); ?></th> |
| 256 |
<th><?php _e( 'Order Location', 'order-tracking' ); ?></th> |
| 257 |
<th><?php _e( 'Updated', 'order-tracking' ); ?></th> |
| 258 |
|
| 259 |
</tr> |
| 260 |
|
| 261 |
</thead> |
| 262 |
|
| 263 |
<tbody> |
| 264 |
|
| 265 |
<?php foreach ( $order->status_history as $status ) { ?> |
| 266 |
|
| 267 |
<tr> |
| 268 |
|
| 269 |
<td><?php echo esc_html( $status->status ); ?></td> |
| 270 |
<td><?php echo esc_html( $status->location ); ?></td> |
| 271 |
<td><?php echo esc_html( $status->updated ); ?></td> |
| 272 |
|
| 273 |
</tr> |
| 274 |
|
| 275 |
<?php } ?> |
| 276 |
|
| 277 |
</tbody> |
| 278 |
|
| 279 |
</table> |
| 280 |
|
| 281 |
<?php |
| 282 |
|
| 283 |
if ( empty( $ewd_otp_controller->settings->get_setting( 'tracking-page-url' ) ) ) { return; } |
| 284 |
|
| 285 |
$args = array( |
| 286 |
'tracking_number' => $order->number, |
| 287 |
'order_email' => $order->email |
| 288 |
); |
| 289 |
|
| 290 |
echo '<p><a href="' . esc_url( add_query_arg( $args, $ewd_otp_controller->settings->get_setting( 'tracking-page-url' ) ) ) . '">' . __('View Detailed Tracking Information', 'order-tracking') . '</a></p>'; |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Adds the order's current location to an order's admin page |
| 295 |
* @since 3.0.0 |
| 296 |
*/ |
| 297 |
public function add_order_location( $wc_order ) { |
| 298 |
global $ewd_otp_controller; |
| 299 |
|
| 300 |
$db_order = $ewd_otp_controller->order_manager->get_order_from_woocommerce_id( $wc_order->get_order_number() ); |
| 301 |
|
| 302 |
if ( empty( $db_order ) ) { return; } |
| 303 |
|
| 304 |
$order = new ewdotpOrder(); |
| 305 |
$order->load_order( $db_order ); |
| 306 |
|
| 307 |
$locations = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'locations' ) ); |
| 308 |
|
| 309 |
?> |
| 310 |
|
| 311 |
<p class="form-field form-field-wide wc-order-status"> |
| 312 |
|
| 313 |
<label for="order_location"><?php _e( 'Location:', 'order-tracking' ); ?></label> |
| 314 |
|
| 315 |
<select id="order_location" name="order_location" class="wc-enhanced-select"> |
| 316 |
|
| 317 |
<?php foreach ( $locations as $location ) { ?> |
| 318 |
|
| 319 |
<option value="<?php echo esc_attr( $location->name ); ?>" <?php echo ( $order->location == $location->name ? 'selected' : '' ); ?>> |
| 320 |
<?php echo esc_html( $location->name ); ?> |
| 321 |
</option> |
| 322 |
|
| 323 |
<?php } ?> |
| 324 |
|
| 325 |
</select> |
| 326 |
|
| 327 |
</p> |
| 328 |
|
| 329 |
<?php |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Update an order after receiving a notification form Zendesk |
| 334 |
* @since 3.0.0 |
| 335 |
*/ |
| 336 |
public function save_wc_location( $post_id ) { |
| 337 |
global $ewd_otp_controller; |
| 338 |
|
| 339 |
// If this is an autosave, our form has not been submitted, so we don't want to do anything. |
| 340 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 341 |
return; |
| 342 |
} |
| 343 |
|
| 344 |
// Check the user's permissions. |
| 345 |
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) { |
| 346 |
|
| 347 |
if ( ! current_user_can( 'edit_page', $post_id ) ) { |
| 348 |
return; |
| 349 |
} |
| 350 |
|
| 351 |
} else { |
| 352 |
|
| 353 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 354 |
return; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
/* OK, it's safe for us to save the data now. If there's no order location, don't save any other information.*/ |
| 359 |
if ( ! isset( $_POST['order_location'] ) ) { |
| 360 |
return; |
| 361 |
} |
| 362 |
|
| 363 |
$db_order = $ewd_otp_controller->order_manager->get_order_from_woocommerce_id( $post_id ); |
| 364 |
|
| 365 |
if ( empty( $db_order ) ) { return; } |
| 366 |
|
| 367 |
$order = new ewdotpOrder(); |
| 368 |
$order->load_order( $db_order ); |
| 369 |
|
| 370 |
if ( $_POST['order_location'] == $order->location ) { return; } |
| 371 |
|
| 372 |
$order->location = sanitize_text_field( $_POST['order_location'] ); |
| 373 |
|
| 374 |
$order->update_order(); |
| 375 |
|
| 376 |
$order->insert_order_status(); |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Get the OTP equivalent status of a WC status |
| 381 |
* @since 3.0.0 |
| 382 |
*/ |
| 383 |
public function get_wc_status( $wc_status ) { |
| 384 |
global $ewd_otp_controller; |
| 385 |
|
| 386 |
$statuses = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'statuses' ) ); |
| 387 |
|
| 388 |
switch ( $wc_status ) { |
| 389 |
|
| 390 |
case 'wc-pending': |
| 391 |
$otp_status = 'Pending Payment'; |
| 392 |
break; |
| 393 |
|
| 394 |
case 'wc-processing': |
| 395 |
$otp_status = 'Processing'; |
| 396 |
break; |
| 397 |
|
| 398 |
case 'wc-on-hold': |
| 399 |
$otp_status = 'On Hold'; |
| 400 |
break; |
| 401 |
|
| 402 |
case 'wc-completed': |
| 403 |
$otp_status = 'Completed'; |
| 404 |
break; |
| 405 |
|
| 406 |
case 'wc-cancelled': |
| 407 |
$otp_status = 'Cancelled'; |
| 408 |
break; |
| 409 |
|
| 410 |
case 'wc-refunded': |
| 411 |
$otp_status = 'Refunded'; |
| 412 |
break; |
| 413 |
|
| 414 |
case 'wc-failed': |
| 415 |
$otp_status = 'Failed'; |
| 416 |
break; |
| 417 |
|
| 418 |
default: |
| 419 |
|
| 420 |
$otp_status = ''; |
| 421 |
|
| 422 |
foreach ( $statuses as $status ) { |
| 423 |
|
| 424 |
if ( 'wc-' . sanitize_title( $status->status, '', 'ewd_otp' ) == $wc_status ) { $otp_status = $status->status; } |
| 425 |
} |
| 426 |
|
| 427 |
break; |
| 428 |
} |
| 429 |
|
| 430 |
return $otp_status; |
| 431 |
} |
| 432 |
|
| 433 |
/** |
| 434 |
* Return post status to the originals for WC orders when the plugin is deactivated |
| 435 |
* @since 3.0.0 |
| 436 |
*/ |
| 437 |
public function revert_statuses() { |
| 438 |
global $wpdb; |
| 439 |
global $ewd_otp_controller; |
| 440 |
|
| 441 |
$statuses = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'statuses' ) ); |
| 442 |
|
| 443 |
foreach ( $statuses as $status ) { |
| 444 |
|
| 445 |
$sanitized_status = sanitize_title( $status->status, '', 'ewd_otp' ); |
| 446 |
|
| 447 |
if ( $status->status == $ewd_otp_controller->settings->get_setting( 'woocommerce-paid-status' ) ) { $wc_status = 'wc-completed'; } |
| 448 |
elseif ( $status->status == $ewd_otp_controller->settings->get_setting( 'woocommerce-unpaid-status' ) ) { $wc_status = 'wc-pending'; } |
| 449 |
elseif ( $status->status == $ewd_otp_controller->settings->get_setting( 'woocommerce-processing-status' )) { $wc_status = 'wc-processing'; } |
| 450 |
elseif ( $status->status == $ewd_otp_controller->settings->get_setting( 'woocommerce-cancelled-status' )) { $wc_status = 'wc-cancelled'; } |
| 451 |
elseif ( $status->status == $ewd_otp_controller->settings->get_setting( 'woocommerce-onhold-status' )) { $wc_status = 'wc-on-hold'; } |
| 452 |
elseif ( $status->status == $ewd_otp_controller->settings->get_setting( 'woocommerce-failed-status' )) { $wc_status = 'wc-failed'; } |
| 453 |
elseif ( $status->status == $ewd_otp_controller->settings->get_setting( 'woocommerce-refunded-status' )) { $wc_status = 'wc-refunded'; } |
| 454 |
else { $wc_status = 'wc-processing'; } |
| 455 |
|
| 456 |
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_status=%s WHERE post_status=%s AND post_type='shop_order'", $wc_status, 'wc-' . $sanitized_status ) ); |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
} |