| 1 |
<?php |
| 2 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 3 |
|
| 4 |
if ( !class_exists( 'ewdotpAJAX' ) ) { |
| 5 |
/** |
| 6 |
* Class to handle AJAX interactions for Order Tracking |
| 7 |
* |
| 8 |
* @since 3.0.0 |
| 9 |
*/ |
| 10 |
class ewdotpAJAX { |
| 11 |
|
| 12 |
public function __construct() { |
| 13 |
|
| 14 |
add_action( 'wp_ajax_ewd_otp_get_order', array( $this, 'get_order' ) ); |
| 15 |
add_action( 'wp_ajax_nopriv_ewd_otp_get_order', array( $this, 'get_order' ) ); |
| 16 |
|
| 17 |
add_action( 'wp_ajax_ewd_otp_get_customer_orders', array( $this, 'get_customer_orders' ) ); |
| 18 |
add_action( 'wp_ajax_nopriv_ewd_otp_get_customer_orders', array( $this, 'get_customer_orders' ) ); |
| 19 |
|
| 20 |
add_action( 'wp_ajax_ewd_otp_get_sales_rep_orders', array( $this, 'get_sales_rep_orders' ) ); |
| 21 |
add_action( 'wp_ajax_nopriv_ewd_otp_get_sales_rep_orders', array( $this, 'get_sales_rep_orders' ) ); |
| 22 |
|
| 23 |
add_action( 'wp_ajax_ewd_otp_update_customer_note', array( $this, 'update_customer_note' ) ); |
| 24 |
add_action( 'wp_ajax_nopriv_ewd_otp_update_customer_note', array( $this, 'update_customer_note' ) ); |
| 25 |
|
| 26 |
add_action( 'wp_ajax_ewd_otp_delete_order', array( $this, 'admin_delete_order' ) ); |
| 27 |
add_action( 'wp_ajax_ewd_otp_hide_order', array( $this, 'admin_hide_order' ) ); |
| 28 |
add_action( 'wp_ajax_ewd_otp_delete_customer', array( $this, 'admin_delete_customer' ) ); |
| 29 |
add_action( 'wp_ajax_ewd_otp_delete_sales_rep', array( $this, 'admin_delete_sales_rep' ) ); |
| 30 |
|
| 31 |
add_action( 'wp_ajax_ewd_otp_send_test_email', array( $this, 'send_test_email' ) ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Returns the output for a single order, given its tracking number and (optionally) email |
| 36 |
* @since 3.0.0 |
| 37 |
*/ |
| 38 |
public function get_order() { |
| 39 |
global $ewd_otp_controller; |
| 40 |
|
| 41 |
// Authenticate request |
| 42 |
if ( ! check_ajax_referer( 'ewd-otp-js', 'nonce' ) ) { |
| 43 |
ewdotpHelper::admin_nopriv_ajax(); |
| 44 |
} |
| 45 |
|
| 46 |
$order = new ewdotpOrder(); |
| 47 |
|
| 48 |
$order->load_order_from_tracking_number( sanitize_text_field( $_POST['order_number'] ) ); |
| 49 |
|
| 50 |
if ( empty( $order->id ) ) { |
| 51 |
|
| 52 |
wp_send_json_error( |
| 53 |
array( |
| 54 |
'output' => __( 'There are no order statuses for tracking number: ', 'order-tracking' ) . sanitize_text_field( $_POST['order_number'] ) |
| 55 |
) |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
if ( $ewd_otp_controller->settings->get_setting( 'email-verification' ) and ! $order->verify_order_email( sanitize_email( $_POST['order_email'] ) ) ) { |
| 60 |
|
| 61 |
wp_send_json_error( |
| 62 |
array( |
| 63 |
'output' => __( 'The email submitted does not match the email associated with this order.', 'order-tracking' ) |
| 64 |
) |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
$order->load_order_status_history(); |
| 69 |
|
| 70 |
$customer = new ewdotpCustomer(); |
| 71 |
|
| 72 |
$customer->load_customer_from_id( $order->customer ); |
| 73 |
|
| 74 |
$sales_rep = new ewdotpSalesRep(); |
| 75 |
|
| 76 |
$sales_rep->load_sales_rep_from_id( $order->sales_rep ); |
| 77 |
|
| 78 |
$args = array( |
| 79 |
'order' => $order, |
| 80 |
'customer' => $customer, |
| 81 |
'sales_rep' => $sales_rep, |
| 82 |
'notes_submit' => sanitize_text_field( $_POST['customer_notes_label'] ) |
| 83 |
); |
| 84 |
|
| 85 |
$order_view = new ewdotpViewOrderForm( $args ); |
| 86 |
|
| 87 |
$order_view->set_order_form_options(); |
| 88 |
|
| 89 |
ob_start(); |
| 90 |
|
| 91 |
$order_view->maybe_print_order_results(); |
| 92 |
|
| 93 |
$output = ob_get_clean(); |
| 94 |
|
| 95 |
wp_send_json_success( |
| 96 |
array( |
| 97 |
'output' => $output |
| 98 |
) |
| 99 |
); |
| 100 |
|
| 101 |
die(); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Returns the customer order table for a given customer id |
| 106 |
* @since 3.0.0 |
| 107 |
*/ |
| 108 |
public function get_customer_orders() { |
| 109 |
global $ewd_otp_controller; |
| 110 |
|
| 111 |
// Authenticate request |
| 112 |
if ( ! check_ajax_referer( 'ewd-otp-js', 'nonce' ) ) { |
| 113 |
ewdotpHelper::admin_nopriv_ajax(); |
| 114 |
} |
| 115 |
|
| 116 |
$customer = new ewdotpCustomer(); |
| 117 |
|
| 118 |
$customer->load_customer_from_number( sanitize_text_field( trim( $_POST['customer_number'] ) ) ); |
| 119 |
|
| 120 |
if ( $ewd_otp_controller->settings->get_setting( 'email-verification' ) and ! $customer->verify_customer_email( sanitize_email( $_POST['customer_email'] ) ) ) { |
| 121 |
|
| 122 |
wp_send_json_error( |
| 123 |
array( |
| 124 |
'output' => __( 'The email submitted does not match the email associated with this customer.', 'order-tracking' ) |
| 125 |
) |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
$args = array( |
| 130 |
'customer' => $customer |
| 131 |
); |
| 132 |
|
| 133 |
$customer_view = new ewdotpViewCustomerForm( $args ); |
| 134 |
|
| 135 |
$customer_view->set_customer_orders(); |
| 136 |
|
| 137 |
ob_start(); |
| 138 |
|
| 139 |
$customer_view->maybe_print_customer_results(); |
| 140 |
|
| 141 |
$output = ob_get_clean(); |
| 142 |
|
| 143 |
if( !$output ) { |
| 144 |
|
| 145 |
$customer_view->error_message = __( 'No orders were found associated with the submitted customer number', 'order-tracking' ); |
| 146 |
|
| 147 |
ob_start(); |
| 148 |
|
| 149 |
$customer_view->print_error_message(); |
| 150 |
|
| 151 |
$output = ob_get_clean(); |
| 152 |
} |
| 153 |
|
| 154 |
wp_send_json_success( |
| 155 |
array( |
| 156 |
'output' => $output |
| 157 |
) |
| 158 |
); |
| 159 |
|
| 160 |
die(); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Returns the sales rep order table for a given sales rep id |
| 165 |
* @since 3.0.0 |
| 166 |
*/ |
| 167 |
public function get_sales_rep_orders() { |
| 168 |
global $ewd_otp_controller; |
| 169 |
|
| 170 |
// Authenticate request |
| 171 |
if ( ! check_ajax_referer( 'ewd-otp-js', 'nonce' ) ) { |
| 172 |
ewdotpHelper::admin_nopriv_ajax(); |
| 173 |
} |
| 174 |
|
| 175 |
$sales_rep = new ewdotpSalesRep(); |
| 176 |
|
| 177 |
$sales_rep->load_sales_rep_from_number( sanitize_text_field( trim( $_POST['sales_rep_number'] ) ) ); |
| 178 |
|
| 179 |
if ( $ewd_otp_controller->settings->get_setting( 'email-verification' ) and ! $sales_rep->verify_sales_rep_email( sanitize_email( $_POST['sales_rep_email'] ) ) ) { |
| 180 |
|
| 181 |
wp_send_json_error( |
| 182 |
array( |
| 183 |
'output' => __( 'The email submitted does not match the email associated with this sales rep.', 'order-tracking' ) |
| 184 |
) |
| 185 |
); |
| 186 |
} |
| 187 |
|
| 188 |
$args = array( |
| 189 |
'sales_rep' => $sales_rep |
| 190 |
); |
| 191 |
|
| 192 |
$sales_rep_view = new ewdotpViewSalesRepForm( $args ); |
| 193 |
|
| 194 |
$sales_rep_view->set_sales_rep_orders(); |
| 195 |
|
| 196 |
ob_start(); |
| 197 |
|
| 198 |
$sales_rep_view->maybe_print_sales_rep_results(); |
| 199 |
|
| 200 |
$output = ob_get_clean(); |
| 201 |
|
| 202 |
if( !$output ) { |
| 203 |
|
| 204 |
$sales_rep_view->error_message = __( 'No orders were found associated with the submitted sales rep number.', 'order-tracking' ); |
| 205 |
|
| 206 |
ob_start(); |
| 207 |
|
| 208 |
$sales_rep_view->print_error_message(); |
| 209 |
|
| 210 |
$output = ob_get_clean(); |
| 211 |
} |
| 212 |
|
| 213 |
wp_send_json_success( |
| 214 |
array( |
| 215 |
'output' => $output |
| 216 |
) |
| 217 |
); |
| 218 |
|
| 219 |
die(); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Updates the customer note for an order |
| 224 |
* @since 3.0.0 |
| 225 |
*/ |
| 226 |
public function update_customer_note() { |
| 227 |
global $ewd_otp_controller; |
| 228 |
|
| 229 |
// Authenticate request |
| 230 |
if ( ! check_ajax_referer( 'ewd-otp-js', 'nonce' ) ) { |
| 231 |
ewdotpHelper::admin_nopriv_ajax(); |
| 232 |
} |
| 233 |
|
| 234 |
$order = new ewdotpOrder(); |
| 235 |
|
| 236 |
$order->load_order_from_tracking_number( sanitize_text_field( $_POST['order_number'] ) ); |
| 237 |
|
| 238 |
if ( $order->id != intval( $_POST['order_id'] ) ) { return; } |
| 239 |
|
| 240 |
$order->customer_notes = sanitize_textarea_field( $_POST['customer_notes'] ); |
| 241 |
|
| 242 |
$order->update_order(); |
| 243 |
|
| 244 |
do_action( 'ewd_otp_customer_note_updated', $order ); |
| 245 |
|
| 246 |
wp_send_json_success( |
| 247 |
array( |
| 248 |
'output' => __( 'Customer note has been successfully updated.', 'order-tracking' ) |
| 249 |
) |
| 250 |
); |
| 251 |
|
| 252 |
die(); |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Deletes a single order via the admin page |
| 257 |
* @since 3.0.0 |
| 258 |
*/ |
| 259 |
public function admin_delete_order() { |
| 260 |
global $ewd_otp_controller; |
| 261 |
|
| 262 |
// Authenticate request |
| 263 |
if ( |
| 264 |
! check_ajax_referer( 'ewd-otp-admin-js', 'nonce' ) |
| 265 |
or ! current_user_can( $ewd_otp_controller->settings->get_setting( 'access-role' ) ) |
| 266 |
) { |
| 267 |
ewdotpHelper::admin_nopriv_ajax(); |
| 268 |
} |
| 269 |
|
| 270 |
if ( ! current_user_can( $ewd_otp_controller->settings->get_setting( 'access-role' ) ) ) { return; } |
| 271 |
|
| 272 |
$ewd_otp_controller->order_manager->delete_order( intval( $_POST['order_id'] ) ); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Hides an order from the admin page |
| 277 |
* @since 3.0.0 |
| 278 |
*/ |
| 279 |
public function admin_hide_order() { |
| 280 |
global $ewd_otp_controller; |
| 281 |
|
| 282 |
// Authenticate request |
| 283 |
if ( |
| 284 |
! check_ajax_referer( 'ewd-otp-admin-js', 'nonce' ) |
| 285 |
or ! current_user_can( $ewd_otp_controller->settings->get_setting( 'access-role' ) ) |
| 286 |
) { |
| 287 |
ewdotpHelper::admin_nopriv_ajax(); |
| 288 |
} |
| 289 |
|
| 290 |
if ( ! current_user_can( $ewd_otp_controller->settings->get_setting( 'access-role' ) ) ) { return; } |
| 291 |
|
| 292 |
$order = new ewdotpOrder(); |
| 293 |
|
| 294 |
$order->load_order_from_id( intval( $_POST['order_id'] ) ); |
| 295 |
|
| 296 |
$order->display = false; |
| 297 |
|
| 298 |
$order->update_order(); |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Deletes a single customer via the admin page |
| 303 |
* @since 3.0.0 |
| 304 |
*/ |
| 305 |
public function admin_delete_customer() { |
| 306 |
global $ewd_otp_controller; |
| 307 |
|
| 308 |
// Authenticate request |
| 309 |
if ( |
| 310 |
! check_ajax_referer( 'ewd-otp-admin-js', 'nonce' ) |
| 311 |
or ! current_user_can( $ewd_otp_controller->settings->get_setting( 'access-role' ) ) |
| 312 |
) { |
| 313 |
ewdotpHelper::admin_nopriv_ajax(); |
| 314 |
} |
| 315 |
|
| 316 |
if ( ! current_user_can( $ewd_otp_controller->settings->get_setting( 'access-role' ) ) ) { return; } |
| 317 |
|
| 318 |
$ewd_otp_controller->customer_manager->delete_customer( intval( $_POST['customer_id'] ) ); |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Deletes a single sales rep via the admin page |
| 323 |
* @since 3.0.0 |
| 324 |
*/ |
| 325 |
public function admin_delete_sales_rep() { |
| 326 |
global $ewd_otp_controller; |
| 327 |
|
| 328 |
// Authenticate request |
| 329 |
if ( |
| 330 |
! check_ajax_referer( 'ewd-otp-admin-js', 'nonce' ) |
| 331 |
or ! current_user_can( $ewd_otp_controller->settings->get_setting( 'access-role' ) ) |
| 332 |
) { |
| 333 |
ewdotpHelper::admin_nopriv_ajax(); |
| 334 |
} |
| 335 |
|
| 336 |
if ( ! current_user_can( $ewd_otp_controller->settings->get_setting( 'access-role' ) ) ) { return; } |
| 337 |
|
| 338 |
$ewd_otp_controller->sales_rep_manager->delete_sales_rep( intval( $_POST['sales_rep_id'] ) ); |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Send a test email for the order emails |
| 343 |
* @since 3.0.0 |
| 344 |
*/ |
| 345 |
public function send_test_email() { |
| 346 |
global $ewd_otp_controller; |
| 347 |
|
| 348 |
$email_address = sanitize_email( $_POST['email_address'] ); |
| 349 |
$email_to_send = sanitize_text_field( $_POST['email_to_send'] ); |
| 350 |
$order = new ewdotpOrder(); |
| 351 |
|
| 352 |
$mail_success = $ewd_otp_controller->notifications->send_email( $email_to_send, $email_address, $order ); |
| 353 |
|
| 354 |
if ( ! empty( $mail_success ) ) { |
| 355 |
|
| 356 |
echo '<div class="ewd-otp-test-email-response">Success: Email has been sent successfully.</div>'; |
| 357 |
} |
| 358 |
else { |
| 359 |
|
| 360 |
echo '<div class="ewd-otp-test-email-response">Error: Please check your email settings, or try using an SMTP email plugin to change email settings.</div>'; |
| 361 |
} |
| 362 |
|
| 363 |
die(); |
| 364 |
} |
| 365 |
} |
| 366 |
} |