PluginProbe
Order Tracking – WordPress Status Tracking Plugin / 3.2.2
Order Tracking – WordPress Status Tracking Plugin v3.2.2
3.5.4 3.5.3 3.5.2 3.5.1 3.5.0 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.3.0 3.3.1 3.3.10 3.3.11 3.3.12 3.3.13 3.3.14 3.3.15 All 235 releases
order-tracking / includes / template-functions.php

template-functions.php in Order Tracking – WordPress Status Tracking Plugin 3.2.2, at includes/template-functions.php

517 lines 14.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Create a shortcode to display an order tracking form
5 * @since 3.0.0
6 */
7 function ewd_otp_tracking_form_shortcode( $atts ) {
8 global $ewd_otp_controller;
9
10 // Define shortcode attributes
11 $order_atts = array(
12 'show_orders' => 'no',
13 'order_form_title' => '',
14 'order_field_text' => '',
15 'email_field_text' => '',
16 'order_instructions' => '',
17 'submit_text' => '',
18 'notes_submit' => '',
19 );
20
21 // Create filter so addons can modify the accepted attributes
22 $order_atts = apply_filters( 'ewd_otp_tracking_form_shortcode_atts', $order_atts );
23
24 // Extract the shortcode attributes
25 $args = shortcode_atts( $order_atts, $atts );
26
27 // Render booking form
28 ewd_otp_load_view_files();
29
30 // Possibly update order status and location from the front-end
31 $update = ewd_otp_possibly_update_order();
32
33 if ( $update ) {
34
35 $args['update_message'] = $update;
36 }
37
38 $order_form = new ewdotpViewOrderForm( $args );
39
40 $order_form->set_request_parameters();
41
42 $output = $order_form->render();
43
44 return $output;
45 }
46 add_shortcode( 'tracking-form', 'ewd_otp_tracking_form_shortcode' );
47
48 /**
49 * Create a shortcode to display a customer form
50 * @since 3.0.0
51 */
52 function ewd_otp_customer_form_shortcode( $atts ) {
53 global $ewd_otp_controller;
54
55 // Define shortcode attributes
56 $customer_atts = array(
57 'order_form_title' => '',
58 'order_field_text' => '',
59 'email_field_text' => '',
60 'order_instructions' => '',
61 'submit_text' => '',
62 );
63
64 $verify_email = $ewd_otp_controller->settings->get_setting( 'email-verification' );
65
66 if ( ! empty( $_POST['ewd_otp_identifier_number'] ) and ( ! empty( $_POST['ewd_otp_form_type'] ) and $_POST['ewd_otp_form_type'] == 'customer_form' ) ) {
67
68 $customer_id = intval( $_POST['ewd_otp_identifier_number'] );
69 }
70 elseif ( get_current_user_id() ) {
71
72 $customer_id = $ewd_otp_controller->customer_manager->get_customer_id_from_wp_id( get_current_user_id() );
73
74 $verify_email = false;
75 }
76 elseif ( class_exists( 'FEUP_User' ) ) {
77
78 $feup_user = new FEUP_User();
79
80 $customer_id = $ewd_otp_controller->customer_manager->get_customer_id_from_feup_id( $feup_user->Get_User_ID() );
81
82 $verify_email = false;
83 }
84
85 if ( ! empty( $customer_id ) ) {
86
87 $customer = new ewdotpCustomer();
88
89 $customer->load_customer_from_id( $customer_id );
90
91 if ( ! $verify_email or $customer->verify_customer_email( sanitize_email( $_POST['ewd_otp_form_email'] ) ) ) {
92
93 $customer_atts['customer'] = $customer;
94 }
95 }
96
97 if ( ! empty( $_POST['ewd_otp_customer_download'] ) and $ewd_otp_controller->settings->get_setting( 'allow-customer-downloads' ) ) {
98
99 $customer_id = intval( $_POST['ewd_otp_customer_id'] );
100 $customer_email = sanitize_email( $_POST['ewd_otp_customer_email'] );
101
102 $customer = new ewdotpCustomer();
103
104 $customer->load_customer_from_id( $customer_id );
105
106 if ( $customer->verify_customer_email( $customer_email ) ) {
107
108 $ewd_otp_controller->exports->nonce_check = false;
109
110 $ewd_otp_controller->exports->customer_id = $customer_id;
111 $ewd_otp_controller->exports->after = date( 'Y-m-d H:i:s', strtotime( '-365 days' ) );
112
113 $ewd_otp_controller->exports->export_orders();
114 }
115 }
116
117 // Create filter so addons can modify the accepted attributes
118 $customer_atts = apply_filters( 'ewd_otp_customer_form_shortcode_atts', $customer_atts );
119
120 // Extract the shortcode attributes
121 $args = shortcode_atts( $customer_atts, $atts );
122
123 // Render booking form
124 ewd_otp_load_view_files();
125
126 // Possibly update order status and location from the front-end
127 $update = ewd_otp_possibly_update_order();
128
129 if ( $update ) {
130
131 $args['update_message'] = $update;
132 }
133
134 $customer_form = new ewdotpViewCustomerForm( $args );
135
136 $customer_form->set_request_parameters();
137
138 $output = $customer_form->render();
139
140 return $output;
141 }
142 add_shortcode( 'customer-form', 'ewd_otp_customer_form_shortcode' );
143
144 /**
145 * Create a shortcode to display a sales rep form
146 * @since 3.0.0
147 */
148 function ewd_otp_sales_rep_form_shortcode( $atts ) {
149 global $ewd_otp_controller;
150
151 if ( ! $ewd_otp_controller->permissions->check_permission( 'sales_reps' ) ) { return; }
152
153 // Define shortcode attributes
154 $sales_rep_atts = array(
155 'order_form_title' => '',
156 'order_field_text' => '',
157 'email_field_text' => '',
158 'order_instructions' => '',
159 'submit_text' => '',
160 );
161
162 if ( get_current_user_id() ) {
163
164 $sales_rep_id = $ewd_otp_controller->sales_rep_manager->get_sales_rep_id_from_wp_id( get_current_user_id() );
165 }
166
167 if ( ! empty( $sales_rep_id ) ) {
168
169 $sales_rep = new ewdotpSalesRep();
170
171 $sales_rep->load_sales_rep_from_id( $sales_rep_id );
172
173 $sales_rep_atts['sales_rep'] = $sales_rep;
174 }
175
176 if ( ! empty( $_POST['ewd_otp_sales_rep_download'] ) and $ewd_otp_controller->settings->get_setting( 'allow-sales-rep-downloads' ) ) {
177
178 $sales_rep_id = intval( $_POST['ewd_otp_sales_rep_id'] );
179 $sales_rep_email = sanitize_email( $_POST['ewd_otp_sales_rep_email'] );
180
181 $sales_rep = new ewdotpSalesRep();
182
183 $sales_rep->load_sales_rep_from_id( $sales_rep_id );
184
185 if ( $sales_rep->verify_sales_rep_email( $sales_rep_email ) ) {
186
187 $ewd_otp_controller->exports->nonce_check = false;
188
189 $ewd_otp_controller->exports->sales_rep_id = $sales_rep_id;
190 $ewd_otp_controller->exports->after = date( 'Y-m-d H:i:s', strtotime( '-365 days' ) );
191
192 $ewd_otp_controller->exports->export_orders();
193 }
194 }
195
196 // Create filter so addons can modify the accepted attributes
197 $sales_rep_atts = apply_filters( 'ewd_otp_sales_rep_form_shortcode_atts', $sales_rep_atts );
198
199 // Extract the shortcode attributes
200 $args = shortcode_atts( $sales_rep_atts, $atts );
201
202 // Render booking form
203 ewd_otp_load_view_files();
204
205 // Possibly update order status and location from the front-end
206 $update = ewd_otp_possibly_update_order();
207
208 if ( $update ) {
209
210 $args['update_message'] = $update;
211 }
212
213 $sales_rep_form = new ewdotpViewSalesRepForm( $args );
214
215 $sales_rep_form->set_request_parameters();
216
217 $output = $sales_rep_form->render();
218
219 return $output;
220 }
221 add_shortcode( 'sales-rep-form', 'ewd_otp_sales_rep_form_shortcode' );
222
223 /**
224 * Create a shortcode to display the customer order form
225 * @since 3.0.0
226 */
227 function ewd_otp_customer_order_form_shortcode( $atts ) {
228 global $ewd_otp_controller;
229
230 if ( ! $ewd_otp_controller->permissions->check_permission( 'customer_orders' ) ) { return; }
231
232 // Define shortcode attributes
233 $customer_order_atts = array(
234 'location' => '',
235 'customer_name_field_text' => '',
236 'customer_email_field_text' => '',
237 'customer_notes_field_text' => '',
238 'success_redirect_page' => '',
239 'submit_text' => ''
240 );
241
242 // Create filter so addons can modify the accepted attributes
243 $customer_order_atts = apply_filters( 'ewd_otp_customer_order_form_shortcode_atts', $customer_order_atts );
244
245 // Extract the shortcode attributes
246 $args = shortcode_atts( $customer_order_atts, $atts );
247
248 // Handle order submission
249 if ( isset( $_POST['ewd_otp_form_type'] ) and $_POST['ewd_otp_form_type'] == 'customer_order_form' ) {
250
251 $args['order_submitted'] = true;
252
253 $order = new ewdotpOrder();
254 $status = $order->process_client_order_submission();
255
256 if ( ! $status ) {
257
258 $args['update_message'] = '';
259
260 foreach ( $order->validation_errors as $validation_error ) {
261
262 $args['update_message'] .= '<br />' . $validation_error['message'];
263 }
264 }
265 else {
266
267 $args['update_message'] = $ewd_otp_controller->settings->get_setting( 'label-customer-order-thank-you' ) . ' ' . $order->number;
268
269 if ( ! empty( $args['success_redirect_page'] ) ) { header( 'location:' . esc_url_raw( $args['success_redirect_page'] ) ); exit(); }
270 }
271 }
272
273 // Render booking form
274 ewd_otp_load_view_files();
275
276 $customer_order_form = new ewdotpViewCustomerOrderForm( $args );
277
278 $customer_order_form->set_request_parameters();
279
280 $output = $customer_order_form->render();
281
282 return $output;
283 }
284 add_shortcode( 'customer-order', 'ewd_otp_customer_order_form_shortcode' );
285
286 function ewd_otp_load_view_files() {
287
288 $files = array(
289 EWD_OTP_PLUGIN_DIR . '/views/Base.class.php' // This will load all default classes
290 );
291
292 $files = apply_filters( 'ewd_otp_load_view_files', $files );
293
294 foreach( $files as $file ) {
295 require_once( $file );
296 }
297
298 }
299
300 function ewd_otp_possibly_update_order() {
301 global $ewd_otp_controller;
302
303 if ( empty( $_POST['ewd_otp_update_status_and_location'] ) ) { return false; }
304
305 $order_id = intval( $_POST['ewd_otp_order_id'] );
306
307 if ( empty( $order_id ) ) { return false; }
308
309 $order = new ewdotpOrder();
310
311 $order->load_order_from_id( $order_id );
312
313 $current_user_cannot = ! current_user_can( $ewd_otp_controller->settings->get_setting( 'access-role' ) );
314 $order_has_no_sales_rep = 1 > intval( $order->sales_rep );
315 $different_sales_rep = 1 > intval( $order->sales_rep ) ? true : ( get_current_user_id() !== intval( $order->get_sales_rep_wp_id() ) );
316
317 if ( $current_user_cannot and ( $order_has_no_sales_rep or $different_sales_rep) ) {
318 return false;
319 }
320
321 if ( ! empty( $_POST['ewd_otp_order_location'] ) ) { $order->location = sanitize_text_field( $_POST['ewd_otp_order_location'] ); }
322
323 if ( ! empty( $_POST['ewd_otp_order_status'] ) ) { $order->set_status( sanitize_text_field( $_POST['ewd_otp_order_status'] ) ); }
324
325 return __( 'Order information has been updated.', 'order-tracking' );
326 }
327
328 if ( ! function_exists( 'ewd_otp_decode_infinite_table_setting' ) ) {
329 function ewd_otp_decode_infinite_table_setting( $values ) {
330
331 if ( empty( $values ) ) { return array(); }
332
333 return is_array( json_decode( html_entity_decode( $values ) ) ) ? json_decode( html_entity_decode( $values ) ) : array();
334 }
335 }
336
337 // add an output buffer layer for the plugin
338 add_action( 'init', 'ewd_otp_add_ob_start' );
339 add_action( 'shutdown', 'ewd_otp_flush_ob_end' );
340
341 // If there's an IPN request, add our setup function to potentially handle it
342 if ( isset($_POST['ipn_track_id']) ) { add_action( 'init', 'ewd_otp_setup_paypal_ipn', 11 ); }
343
344 /**
345 * Sets up the PayPal IPN process
346 * @since 3.0.0
347 */
348 if ( !function_exists( 'ewd_otp_setup_paypal_ipn' ) ) {
349 function ewd_otp_setup_paypal_ipn() {
350 global $ewd_otp_controller;
351
352 if ( empty( $ewd_otp_controller->settings->get_setting( 'allow-order-payments' ) ) ) { return; }
353
354 ewd_otp_handle_paypal_ipn();
355 }
356 } // endif;
357
358 /**
359 * Handle PayPal IPN requests
360 * @since 3.0.0
361 */
362 if ( !function_exists( 'ewd_otp_handle_paypal_ipn' ) ) {
363 function ewd_otp_handle_paypal_ipn() {
364
365 // CONFIG: Enable debug mode. This means we'll log requests into 'ipn.log' in the same directory.
366 // Especially useful if you encounter network errors or other intermittent problems with IPN (validation).
367 // Set this to 0 once you go live or don't require logging.
368 $debug = get_option( 'ewd_otp_enable_payment_debugging' );
369
370 // Set to 0 once you're ready to go live
371 define("EWD_OTP_USE_SANDBOX", 0);
372 define("EWD_OTP_LOG_FILE", "ipn.log");
373 // Read POST data
374 // reading posted data directly from $_POST causes serialization
375 // issues with array data in POST. Reading raw POST data from input stream instead.
376 $raw_post_data = file_get_contents('php://input');
377 $raw_post_array = explode('&', $raw_post_data);
378 $myPost = array();
379 foreach ($raw_post_array as $keyval) {
380 $keyval = explode ('=', $keyval);
381 if (count($keyval) == 2)
382 $myPost[$keyval[0]] = urldecode($keyval[1]);
383 }
384 // read the post from PayPal system and add 'cmd'
385 $req = 'cmd=_notify-validate';
386 if(function_exists('get_magic_quotes_gpc')) {
387 $get_magic_quotes_exists = true;
388 }
389 foreach ($myPost as $key => $value) {
390 if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
391 $value = urlencode(stripslashes($value));
392 } else {
393 $value = urlencode($value);
394 }
395 $req .= "&$key=$value";
396 }
397 // Post IPN data back to PayPal to validate the IPN data is genuine
398 // Without this step anyone can fake IPN data
399 if(EWD_OTP_USE_SANDBOX == true) {
400 $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
401 } else {
402 $paypal_url = "https://www.paypal.com/cgi-bin/webscr";
403 }
404
405 $response = wp_remote_post($paypal_url, array(
406 'method' => 'POST',
407 'body' => $req,
408 'timeout' => 30
409 ));
410
411 // Inspect IPN validation result and act accordingly
412 // Split response headers and payload, a better way for strcmp
413 $tokens = explode("\r\n\r\n", trim($response['body']));
414 $res = trim(end($tokens));
415
416 if ( $debug ) {
417 update_option( 'ewd_otp_debugging', get_option( 'ewd_otp_debugging' ) . print_r( date('[Y-m-d H:i e] '). "IPN response: $res - $req ". PHP_EOL, true ) );
418 }
419
420 if (strcmp ($res, "VERIFIED") == 0) {
421
422 $paypal_receipt_number = sanitize_text_field( $_POST['txn_id'] );
423 $payment_amount = sanitize_text_field( $_POST['mc_gross'] );
424
425 $order_id = sanitize_text_field( $_POST['custom'] );
426
427 $order = new ewdotpOrder();
428 $order->load_order_from_id( $order_id );
429
430 if ( ! $order->id ) { return; }
431
432 $order->payment_completed = true;
433 $order->paypal_receipt_number = sanitize_text_field( $paypal_receipt_number );
434
435 $order->update_order();
436
437 do_action( 'ewd_otp_order_paid', $order );
438 }
439 }
440 } // endif;
441
442 /**
443 * Opens a buffer when handling PayPal IPN requests
444 * @since 3.0.0
445 */
446 if ( !function_exists( 'ewd_otp_add_ob_start' ) ) {
447 function ewd_otp_add_ob_start() {
448 ob_start();
449 }
450 } // endif;
451
452 /**
453 * Closes a buffer when handling PayPal IPN requests
454 * @since 3.0.0
455 */
456 if ( !function_exists( 'ewd_otp_flush_ob_end' ) ) {
457 function ewd_otp_flush_ob_end() {
458 if ( ob_get_length() ) { ob_end_clean(); }
459 }
460 } // endif;
461
462 if ( ! function_exists( 'ewd_hex_to_rgb' ) ) {
463 function ewd_hex_to_rgb( $hex ) {
464
465 $hex = str_replace("#", "", $hex);
466
467 // return if the string isn't a color code
468 if ( strlen( $hex ) !== 3 and strlen( $hex ) !== 6 ) { return '0,0,0'; }
469
470 if(strlen($hex) == 3) {
471 $r = hexdec( substr( $hex, 0, 1 ) . substr( $hex, 0, 1 ) );
472 $g = hexdec( substr( $hex, 1, 1 ) . substr( $hex, 1, 1 ) );
473 $b = hexdec( substr( $hex, 2, 1 ) . substr( $hex, 2, 1 ) );
474 } else {
475 $r = hexdec( substr( $hex, 0, 2 ) );
476 $g = hexdec( substr( $hex, 2, 2 ) );
477 $b = hexdec( substr( $hex, 4, 2 ) );
478 }
479
480 $rgb = $r . ", " . $g . ", " . $b;
481
482 return $rgb;
483 }
484 }
485
486 if ( ! function_exists( 'ewd_format_classes' ) ) {
487 function ewd_format_classes( $classes ) {
488
489 if ( count( $classes ) ) {
490 return ' class="' . esc_attr( join( ' ', $classes ) ) . '"';
491 }
492 }
493 }
494
495 if ( ! function_exists( 'ewd_add_frontend_ajax_url' ) ) {
496 function ewd_add_frontend_ajax_url() { ?>
497
498 <script type="text/javascript">
499 var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
500 </script>
501 <?php }
502 }
503
504 if ( ! function_exists( 'ewd_random_string' ) ) {
505 function ewd_random_string( $length = 10 ) {
506
507 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
508 $randstring = '';
509
510 for ( $i = 0; $i < $length; $i++ ) {
511
512 $randstring .= $characters[ rand( 0, strlen( $characters ) - 1 ) ];
513 }
514
515 return $randstring;
516 }
517 }