1 <?php
2 3 4 5 6 7 8 9 10
11
12
13 if ( ! defined( 'ABSPATH' ) ) exit;
14
15 16 17 18 19 20 21 22 23 24
25 function edd_process_purchase_form() {
26
27 if ( ! edd_get_cart_contents() ) {
28 edd_set_error( 'empty_cart', __( 'Your cart is empty.', 'edd') );
29 } else {
30
31 $valid_data = edd_purchase_form_validate_fields();
32
33
34 do_action( 'edd_checkout_error_checks', $valid_data, $_POST );
35 }
36
37 $is_ajax = isset( $_POST['edd_ajax'] );
38
39 $user = edd_get_purchase_form_user( $valid_data );
40
41 if ( edd_get_errors() || ! $user ) {
42 if ( $is_ajax ) {
43 do_action( 'edd_ajax_checkout_errors' );
44 exit;
45 } else {
46 return false;
47 }
48 }
49
50 if ( $is_ajax ) {
51 echo 'success';
52 exit;
53 }
54
55
56 $user_info = array(
57 'id' => $user['user_id'],
58 'email' => $user['user_email'],
59 'first_name' => $user['user_first'],
60 'last_name' => $user['user_last'],
61 'discount' => $valid_data['discount']
62 );
63
64
65 $purchase_data = array(
66 'downloads' => edd_get_cart_contents(),
67 'fees' => edd_get_cart_fees(),
68 'subtotal' => edd_get_cart_subtotal(),
69 'discount' => edd_get_cart_discounted_amount(),
70 'tax' => edd_get_cart_tax(),
71 'price' => edd_get_cart_total(),
72 'purchase_key' => strtolower( md5( uniqid() ) ),
73 'user_email' => $user['user_email'],
74 'date' => date( 'Y-m-d H:i:s' ),
75 'user_info' => $user_info,
76 'post_data' => $_POST,
77 'cart_details' => edd_get_cart_content_details(),
78 'gateway' => $valid_data['gateway'],
79 'card_info' => $valid_data['cc_info']
80 );
81
82
83 $valid_data['user'] = $user;
84
85
86 do_action( 'edd_checkout_before_gateway', $_POST, $user_info, $valid_data );
87
88
89 $purchase_data = apply_filters(
90 'edd_purchase_data_before_gateway',
91 $purchase_data,
92 $valid_data
93 );
94
95
96 if ( !$purchase_data['price'] ) {
97
98 $valid_data['gateway'] = 'manual';
99 }
100
101
102 edd_set_purchase_session( $purchase_data );
103
104
105 edd_send_to_gateway( $valid_data['gateway'], $purchase_data );
106 exit;
107 }
108 add_action( 'edd_purchase', 'edd_process_purchase_form' );
109 add_action( 'wp_ajax_edd_process_checkout', 'edd_process_purchase_form' );
110 add_action( 'wp_ajax_nopriv_edd_process_checkout', 'edd_process_purchase_form' );
111
112 113 114 115 116 117 118
119 function edd_purchase_form_validate_fields() {
120 global $edd_options;
121
122
123 if ( empty( $_POST ) ) return false;
124
125
126 $valid_data = array(
127 'gateway' => edd_purchase_form_validate_gateway(),
128 'discount' => edd_purchase_form_validate_discounts(),
129 'need_new_user' => false,
130 'need_user_login' => false,
131 'logged_user_data' => array(),
132 'new_user_data' => array(),
133 'login_user_data' => array(),
134 'guest_user_data' => array(),
135 'cc_info' => edd_purchase_form_validate_cc()
136 );
137
138
139 if ( isset( $edd_options['show_agree_to_terms'] ) )
140 edd_purchase_form_validate_agree_to_terms();
141
142 if ( is_user_logged_in() ) {
143
144 $valid_data['logged_in_user'] = edd_purchase_form_validate_logged_in_user();
145 } else if ( isset( $_POST['edd-purchase-var'] ) && $_POST['edd-purchase-var'] == 'needs-to-register' ) {
146
147 $valid_data['need_new_user'] = true;
148
149
150 $valid_data['new_user_data'] = edd_purchase_form_validate_new_user();
151
152 } else if ( isset( $_POST['edd-purchase-var'] ) && $_POST['edd-purchase-var'] == 'needs-to-login' ) {
153
154 $valid_data['need_user_login'] = true;
155
156
157 $valid_data['login_user_data'] = edd_purchase_form_validate_user_login();
158 } else {
159
160 $valid_data['guest_user_data'] = edd_purchase_form_validate_guest_user();
161 }
162
163
164 return $valid_data;
165 }
166
167 168 169 170 171 172 173
174 function edd_purchase_form_validate_gateway() {
175
176 if ( ! empty( $_POST['edd-gateway'] ) ) {
177 $gateway = sanitize_text_field( $_POST['edd-gateway'] );
178
179 if ( edd_is_gateway_active( $gateway ) )
180 return $gateway;
181
182 if ( ! edd_get_cart_amount() )
183 return 'manual';
184
185 edd_set_error( 'invalid_gateway', __( 'The selected gateway is not active', 'edd' ) );
186 } else {
187 edd_set_error( 'empty_gateway', __( 'No gateway has been selected', 'edd' ) );
188 }
189
190
191 return '';
192 }
193
194 195 196 197 198 199 200
201 function edd_purchase_form_validate_discounts() {
202
203 $discounts = edd_get_cart_discounts();
204
205
206 if ( ! empty( $_POST['edd-discount'] ) || $discounts !== false ) {
207 if( empty( $discounts ) ) {
208 $discount = sanitize_text_field( $_POST['edd-discount'] );
209 } else {
210
211 $discount = $discounts[0];
212
213
214 }
215
216 $user = isset( $_POST['edd_user_login'] ) ? sanitize_text_field( $_POST['edd_user_login'] ) : sanitize_email( $_POST['edd_email'] );
217
218
219 if ( edd_is_discount_valid( $discount, $user ) ) {
220
221 return $discount;
222 } else {
223
224 edd_set_error( 'invalid_discount', __( 'The discount you entered is invalid', 'edd' ) );
225 }
226 }
227
228 return 'none';
229 }
230
231 232 233 234 235 236 237
238 function edd_purchase_form_validate_agree_to_terms() {
239
240 if ( ! isset( $_POST['edd_agree_to_terms'] ) || $_POST['edd_agree_to_terms'] != 1 ) {
241
242 edd_set_error( 'agree_to_terms', apply_filters( 'edd_agree_to_terms_text', __( 'You must agree to the terms of use', 'edd' ) ) );
243 }
244 }
245
246 247 248 249 250 251 252
253 function edd_purchase_form_required_fields() {
254 $required_fields = array(
255 'edd_first' => array(
256 'error_id' => 'invalid_first_name',
257 'error_message' => __( 'Please enter your first name.', 'edd' )
258 )
259 );
260 return apply_filters( 'edd_purchase_form_required_fields', $required_fields );
261 }
262
263 264 265 266 267 268 269
270 function edd_purchase_form_validate_logged_in_user() {
271 global $user_ID;
272
273
274 $valid_user_data = array(
275
276 'user_id' => -1
277 );
278
279
280 if ( $user_ID > 0 ) {
281
282 $user_data = get_userdata( $user_ID );
283
284 if ( ! is_email( $_POST['edd_email'] ) ) {
285 edd_set_error( 'invalid_email', __( 'Please enter a valid email address.', 'edd' ) );
286 }
287
288
289 foreach ( edd_purchase_form_required_fields() as $field_name => $value ) {
290 if ( in_array( $value, edd_purchase_form_required_fields() ) && empty( $_POST[ $field_name ] ) ) {
291 edd_set_error( $value['error_id'], $value['error_message'] );
292 }
293 }
294
295
296 if ( $user_data ) {
297
298 $valid_user_data = array(
299 'user_id' => $user_ID,
300 'user_email' => sanitize_email( $_POST['edd_email'] ),
301 'user_first' => sanitize_text_field( $_POST['edd_first'] ),
302 'user_last' => sanitize_text_field( $_POST['edd_last'] ),
303 );
304 } else {
305
306 edd_set_error( 'invalid_user', __( 'The user information is invalid.', 'edd' ) );
307 }
308 }
309
310
311 return $valid_user_data;
312 }
313
314 315 316 317 318 319 320
321 function edd_purchase_form_validate_new_user() {
322 $registering_new_user = false;
323
324
325 $valid_user_data = array(
326
327 'user_id' => -1,
328
329 'user_first' => isset( $_POST["edd_first"] ) ? strip_tags( trim( $_POST["edd_first"] ) ) : '',
330
331 'user_last' => isset( $_POST["edd_last"] ) ? strip_tags( trim( $_POST["edd_last"] ) ) : '',
332 );
333
334
335 $user_login = isset( $_POST["edd_user_login"] ) ? trim( $_POST["edd_user_login"] ) : false;
336 $user_email = isset( $_POST['edd_email'] ) ? trim( $_POST['edd_email'] ) : false;
337 $user_pass = isset( $_POST["edd_user_pass"] ) ? trim( $_POST["edd_user_pass"] ) : false;
338 $pass_confirm = isset( $_POST["edd_user_pass_confirm"] ) ? trim( $_POST["edd_user_pass_confirm"] ) : false;
339
340
341
342 if ( $user_login && strlen( $user_login ) > 0 ) {
343 $registering_new_user = true;
344
345
346 if ( username_exists( $user_login ) ) {
347
348 edd_set_error( 'username_unavailable', __( 'Username already taken', 'edd' ) );
349
350 } else if ( ! edd_validate_username( $user_login ) ) {
351
352 if ( is_multisite() )
353 edd_set_error( 'username_invalid', __( 'Invalid username. Only lowercase letters (a-z) and numbers are allowed', 'edd' ) );
354 else
355 edd_set_error( 'username_invalid', __( 'Invalid username', 'edd' ) );
356 } else {
357
358 $valid_user_data['user_login'] = $user_login;
359 }
360 } else {
361 if ( edd_no_guest_checkout() ) {
362 edd_set_error( 'registration_required', __( 'You must register or login to complete your purchase', 'edd' ) );
363 }
364 }
365
366
367 if ( $user_email && strlen( $user_email ) > 0 ) {
368
369 if ( ! is_email( $user_email ) ) {
370 edd_set_error( 'email_invalid', __('Invalid email', 'edd') );
371
372 } else if ( email_exists( $user_email ) && $registering_new_user ) {
373 edd_set_error( 'email_used', __('Email already used', 'edd') );
374 } else {
375
376 $valid_user_data['user_email'] = $user_email;
377 }
378 } else {
379
380 edd_set_error( 'email_empty', __('Enter an email', 'edd') );
381 }
382
383
384 if ( $user_pass && $pass_confirm ) {
385
386 if ( $user_pass != $pass_confirm ) {
387
388 edd_set_error( 'password_mismatch', __( 'Passwords don\'t match', 'edd' ) );
389 } else {
390
391 $valid_user_data['user_pass'] = $user_pass;
392 }
393 } else {
394
395 if ( ! $user_pass && $registering_new_user ) {
396
397 edd_set_error( 'password_empty', __( 'Enter a password', 'edd' ) );
398 } else if ( ! $pass_confirm && $registering_new_user ) {
399
400 edd_set_error( 'confirmation_empty', __( 'Enter the password confirmation', 'edd' ) );
401 }
402 }
403
404 return $valid_user_data;
405 }
406
407 408 409 410 411 412 413
414 function edd_purchase_form_validate_user_login() {
415
416 $valid_user_data = array(
417
418 'user_id' => -1
419 );
420
421
422 if ( ! isset( $_POST['edd_user_login'] ) || $_POST['edd_user_login'] == '' ) {
423 edd_set_error( 'must_log_in', __( 'You must login or register to complete your purchase', 'edd' ) );
424 return $valid_user_data;
425 }
426
427
428 $user_data = get_user_by( 'login', strip_tags( $_POST['edd_user_login'] ) );
429
430
431 if ( $user_data ) {
432
433 $user_pass = isset( $_POST["edd_user_pass"] ) ? $_POST["edd_user_pass"] : false;
434
435
436 if ( $user_pass ) {
437
438 if ( ! wp_check_password( $user_pass, $user_data->user_pass, $user_data->ID ) ) {
439
440 edd_set_error( 'password_incorrect', __( 'The password you entered is incorrect', 'edd' ) );
441
442 } else {
443
444 $valid_user_data = array(
445 'user_id' => $user_data->ID,
446 'user_login' => $user_data->user_login,
447 'user_email' => $user_data->user_email,
448 'user_first' => $user_data->first_name,
449 'user_last' => $user_data->last_name,
450 'user_pass' => $user_pass,
451 );
452 }
453 } else {
454
455 edd_set_error( 'password_empty', __( 'Enter a password', 'edd' ) );
456 }
457 } else {
458
459 edd_set_error( 'username_incorrect', __( 'The username you entered does not exist', 'edd' ) );
460 }
461
462 return $valid_user_data;
463 }
464
465 466 467 468 469 470 471
472 function edd_purchase_form_validate_guest_user() {
473
474 $valid_user_data = array(
475
476 'user_id' => 0,
477 );
478
479
480 if ( edd_logged_in_only() ) {
481 edd_set_error( 'logged_in_only', __( 'You must be logged into an account to purchase.', 'edd' ) );
482 }
483
484
485 $guest_email = isset( $_POST['edd_email'] ) ? $_POST['edd_email'] : false;
486
487
488 if ( $guest_email && strlen( $guest_email ) > 0 ) {
489
490 if ( ! is_email( $guest_email ) ) {
491
492 edd_set_error( 'email_invalid', __( 'Invalid email', 'edd' ) );
493 } else {
494
495 $valid_user_data['user_email'] = $guest_email;
496 }
497 } else {
498
499 edd_set_error( 'email_empty', __( 'Enter an email', 'edd' ) );
500 }
501
502
503 foreach ( edd_purchase_form_required_fields() as $field_name => $value ) {
504 if ( in_array( $value, edd_purchase_form_required_fields() ) && empty( $_POST[ $field_name ] ) ) {
505 edd_set_error( $value['error_id'], $value['error_message'] );
506 }
507 }
508
509 return $valid_user_data;
510 }
511
512 513 514 515 516 517 518 519 520
521 function edd_register_and_login_new_user( $user_data = array() ) {
522
523 if ( empty( $user_data ) )
524 return -1;
525
526 $user_args = array(
527 'user_login' => isset( $user_data['user_login'] ) ? $user_data['user_login'] : null,
528 'user_pass' => isset( $user_data['user_pass'] ) ? $user_data['user_pass'] : null,
529 'user_email' => $user_data['user_email'],
530 'first_name' => $user_data['user_first'],
531 'last_name' => $user_data['user_last'],
532 'user_registered' => date('Y-m-d H:i:s'),
533 'role' => get_option( 'default_role' )
534 );
535
536
537 $user_id = wp_insert_user( apply_filters( 'edd_insert_user_args', $user_args ) );
538
539
540 if ( is_wp_error( $user_id ) )
541 return -1;
542
543
544 do_action( 'edd_insert_user', $user_id );
545
546
547 edd_log_user_in( $user_id, $user_data['user_login'], $user_data['user_pass'] );
548
549
550 return $user_id;
551 }
552
553 554 555 556 557 558 559 560 561
562 function edd_get_purchase_form_user( $valid_data = array() ) {
563
564 $user = false;
565 $is_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX;
566
567 if ( $is_ajax ) {
568
569 return true;
570 } else if ( is_user_logged_in() ) {
571
572 $user = $valid_data['logged_in_user'];
573 } else if ( $valid_data['need_new_user'] === true || $valid_data['need_user_login'] === true ) {
574
575 if ( $valid_data['need_new_user'] === true ) {
576
577 $user = $valid_data['new_user_data'];
578
579 $user['user_id'] = edd_register_and_login_new_user( $user );
580
581 } else if ( $valid_data['need_user_login'] === true && ! $is_ajax ) {
582
583 $user = $valid_data['login_user_data'];
584
585 edd_log_user_in( $user['user_id'], $user['user_login'], $user['user_pass'] );
586 }
587 }
588
589
590 if ( false === $user && false === edd_no_guest_checkout() ) {
591
592 $user = $valid_data['guest_user_data'];
593 }
594
595
596 if ( false === $user || empty( $user ) ) {
597
598 return false;
599 }
600
601
602 if ( ! isset( $user['user_first'] ) || strlen( trim( $user['user_first'] ) ) < 1 ) {
603 $user['user_first'] = isset( $_POST["edd_first"] ) ? strip_tags( trim( $_POST["edd_first"] ) ) : '';
604 }
605
606
607 if ( ! isset( $user['user_last'] ) || strlen( trim( $user['user_last'] ) ) < 1 ) {
608 $user['user_last'] = isset( $_POST["edd_last"] ) ? strip_tags( trim( $_POST["edd_last"] ) ) : '';
609 }
610
611
612 return $user;
613 }
614
615 616 617 618 619 620 621
622 function edd_purchase_form_validate_cc() {
623 $card_data = edd_get_purchase_cc_info();
624
625
626 if ( ! empty( $card_data['card_zip'] ) ) {
627 if ( ! edd_purchase_form_validate_cc_zip( $card_data['card_zip'], $card_data['card_country'] ) )
628 edd_set_error( 'invalid_cc_zip', __( 'The zip code you entered for your credit card is invalid', 'edd' ) );
629 }
630
631
632 return $card_data;
633 }
634
635 636 637 638 639 640 641
642 function edd_get_purchase_cc_info() {
643 $cc_info = array();
644 $cc_info['card_name'] = isset( $_POST['card_name'] ) ? sanitize_text_field( $_POST['card_name'] ) : '';
645 $cc_info['card_number'] = isset( $_POST['card_number'] ) ? sanitize_text_field( $_POST['card_number'] ) : '';
646 $cc_info['card_cvc'] = isset( $_POST['card_cvc'] ) ? sanitize_text_field( $_POST['card_cvc'] ) : '';
647 $cc_info['card_exp_month'] = isset( $_POST['card_exp_month'] ) ? sanitize_text_field( $_POST['card_exp_month'] ) : '';
648 $cc_info['card_exp_year'] = isset( $_POST['card_exp_year'] ) ? sanitize_text_field( $_POST['card_exp_year'] ) : '';
649 $cc_info['card_address'] = isset( $_POST['card_address'] ) ? sanitize_text_field( $_POST['card_address'] ) : '';
650 $cc_info['card_address_2'] = isset( $_POST['card_address_2'] ) ? sanitize_text_field( $_POST['card_address_2'] ) : '';
651 $cc_info['card_city'] = isset( $_POST['card_city'] ) ? sanitize_text_field( $_POST['card_city'] ) : '';
652 $cc_info['card_country'] = isset( $_POST['billing_country'] )? sanitize_text_field( $_POST['billing_country'] ) : '';
653 $cc_info['card_zip'] = isset( $_POST['card_zip'] ) ? sanitize_text_field( $_POST['card_zip'] ) : '';
654
655 switch ( $cc_info['card_country'] ) :
656 case 'US' :
657 $cc_info['card_state'] = isset( $_POST['card_state_us'] ) ? sanitize_text_field( $_POST['card_state_us'] ) : '';
658 break;
659 case 'CA' :
660 $cc_info['card_state'] = isset( $_POST['card_state_ca'] ) ? sanitize_text_field( $_POST['card_state_ca'] ) : '';
661 break;
662 default :
663 $cc_info['card_state'] = isset( $_POST['card_state_other'] )? sanitize_text_field( $_POST['card_state_other'] ) : '';
664 break;
665 endswitch;
666
667
668 return $cc_info;
669 }
670
671 672 673 674 675 676 677
678 function edd_purchase_form_validate_cc_zip( $zip = 0, $country_code = '' ) {
679 $ret = false;
680
681 if ( empty( $zip ) || empty( $country_code ) )
682 return $ret;
683
684 $zip_regex = array(
685 "AD" => "AD\d{3}",
686 "AM" => "(37)?\d{4}",
687 "AR" => "^([A-HJ-TP-Z]{1}\d{4}[A-Z]{3}|[a-z]{1}\d{4}[a-hj-tp-z]{3})$",
688 "AS" => "96799",
689 "AT" => "\d{4}",
690 "AU" => "^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$",
691 "AX" => "22\d{3}",
692 "AZ" => "\d{4}",
693 "BA" => "\d{5}",
694 "BB" => "(BB\d{5})?",
695 "BD" => "\d{4}",
696 "BE" => "^[1-9]{1}[0-9]{3}$",
697 "BG" => "\d{4}",
698 "BH" => "((1[0-2]|[2-9])\d{2})?",
699 "BM" => "[A-Z]{2}[ ]?[A-Z0-9]{2}",
700 "BN" => "[A-Z]{2}[ ]?\d{4}",
701 "BR" => "\d{5}[\-]?\d{3}",
702 "BY" => "\d{6}",
703 "CA" => "^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\ {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$",
704 "CC" => "6799",
705 "CH" => "^[1-9][0-9][0-9][0-9]$",
706 "CK" => "\d{4}",
707 "CL" => "\d{7}",
708 "CN" => "\d{6}",
709 "CR" => "\d{4,5}|\d{3}-\d{4}",
710 "CS" => "\d{5}",
711 "CV" => "\d{4}",
712 "CX" => "6798",
713 "CY" => "\d{4}",
714 "CZ" => "\d{3}[ ]?\d{2}",
715 "DE" => "\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3}))\b",
716 "DK" => "^([D-d][K-k])?( |-)?[1-9]{1}[0-9]{3}$",
717 "DO" => "\d{5}",
718 "DZ" => "\d{5}",
719 "EC" => "([A-Z]\d{4}[A-Z]|(?:[A-Z]{2})?\d{6})?",
720 "EE" => "\d{5}",
721 "EG" => "\d{5}",
722 "ES" => "^([1-9]{2}|[0-9][1-9]|[1-9][0-9])[0-9]{3}$",
723 "ET" => "\d{4}",
724 "FI" => "\d{5}",
725 "FK" => "FIQQ 1ZZ",
726 "FM" => "(9694[1-4])([ \-]\d{4})?",
727 "FO" => "\d{3}",
728 "FR" => "^(F-)?((2[A|B])|[0-9]{2})[0-9]{3}$",
729 "GE" => "\d{4}",
730 "GF" => "9[78]3\d{2}",
731 "GL" => "39\d{2}",
732 "GN" => "\d{3}",
733 "GP" => "9[78][01]\d{2}",
734 "GR" => "\d{3}[ ]?\d{2}",
735 "GS" => "SIQQ 1ZZ",
736 "GT" => "\d{5}",
737 "GU" => "969[123]\d([ \-]\d{4})?",
738 "GW" => "\d{4}",
739 "HM" => "\d{4}",
740 "HN" => "(?:\d{5})?",
741 "HR" => "\d{5}",
742 "HT" => "\d{4}",
743 "HU" => "\d{4}",
744 "ID" => "\d{5}",
745 "IE" => "((D|DUBLIN)?([1-9]|6[wW]|1[0-8]|2[024]))?",
746 "IL" => "\d{5}",
747 "IN"=> "^[1-9][0-9][0-9][0-9][0-9][0-9]$",
748 "IO" => "BBND 1ZZ",
749 "IQ" => "\d{5}",
750 "IS" => "\d{3}",
751 "IT" => "^(V-|I-)?[0-9]{5}$",
752 "JO" => "\d{5}",
753 "JP" => "\d{3}-\d{4}",
754 "KE" => "\d{5}",
755 "KG" => "\d{6}",
756 "KH" => "\d{5}",
757 "KR" => "\d{3}[\-]\d{3}",
758 "KW" => "\d{5}",
759 "KZ" => "\d{6}",
760 "LA" => "\d{5}",
761 "LB" => "(\d{4}([ ]?\d{4})?)?",
762 "LI" => "(948[5-9])|(949[0-7])",
763 "LK" => "\d{5}",
764 "LR" => "\d{4}",
765 "LS" => "\d{3}",
766 "LT" => "\d{5}",
767 "LU" => "\d{4}",
768 "LV" => "\d{4}",
769 "MA" => "\d{5}",
770 "MC" => "980\d{2}",
771 "MD" => "\d{4}",
772 "ME" => "8\d{4}",
773 "MG" => "\d{3}",
774 "MH" => "969[67]\d([ \-]\d{4})?",
775 "MK" => "\d{4}",
776 "MN" => "\d{6}",
777 "MP" => "9695[012]([ \-]\d{4})?",
778 "MQ" => "9[78]2\d{2}",
779 "MT" => "[A-Z]{3}[ ]?\d{2,4}",
780 "MU" => "(\d{3}[A-Z]{2}\d{3})?",
781 "MV" => "\d{5}",
782 "MX" => "\d{5}",
783 "MY" => "\d{5}",
784 "NC" => "988\d{2}",
785 "NE" => "\d{4}",
786 "NF" => "2899",
787 "NG" => "(\d{6})?",
788 "NI" => "((\d{4}-)?\d{3}-\d{3}(-\d{1})?)?",
789 "NL" => "^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$",
790 "NO" => "\d{4}",
791 "NP" => "\d{5}",
792 "NZ" => "\d{4}",
793 "OM" => "(PC )?\d{3}",
794 "PF" => "987\d{2}",
795 "PG" => "\d{3}",
796 "PH" => "\d{4}",
797 "PK" => "\d{5}",
798 "PL" => "\d{2}-\d{3}",
799 "PM" => "9[78]5\d{2}",
800 "PN" => "PCRN 1ZZ",
801 "PR" => "00[679]\d{2}([ \-]\d{4})?",
802 "PT" => "\d{4}([\-]\d{3})?",
803 "PW" => "96940",
804 "PY" => "\d{4}",
805 "RE" => "9[78]4\d{2}",
806 "RO" => "\d{6}",
807 "RS" => "\d{6}",
808 "RU" => "\d{6}",
809 "SA" => "\d{5}",
810 "SE" => "^(s-|S-){0,1}[0-9]{3}\s?[0-9]{2}$",
811 "SG" => "\d{6}",
812 "SH" => "(ASCN|STHL) 1ZZ",
813 "SI" => "\d{4}",
814 "SJ" => "\d{4}",
815 "SK" => "\d{3}[ ]?\d{2}",
816 "SM" => "4789\d",
817 "SN" => "\d{5}",
818 "SO" => "\d{5}",
819 "SZ" => "[HLMS]\d{3}",
820 "TC" => "TKCA 1ZZ",
821 "TH" => "\d{5}",
822 "TJ" => "\d{6}",
823 "TM" => "\d{6}",
824 "TN" => "\d{4}",
825 "TR" => "\d{5}",
826 "TW" => "\d{3}(\d{2})?",
827 "UA" => "\d{5}",
828 "UK" => "^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$",
829 "US" => "^\d{5}([\-]?\d{4})?$",
830 "UY" => "\d{5}",
831 "UZ" => "\d{6}",
832 "VA" => "00120",
833 "VE" => "\d{4}",
834 "VI" => "008(([0-4]\d)|(5[01]))([ \-]\d{4})?",
835 "WF" => "986\d{2}",
836 "YT" => "976\d{2}",
837 "YU" => "\d{5}",
838 "ZA" => "\d{4}",
839 "ZM" => "\d{5}"
840 );
841
842 if ( preg_match( "/" . $zip_regex[ $country_code ] . "/i", $zip ) )
843 $ret = true;
844
845 return apply_filters( 'edd_is_zip_valid', $ret, $zip, $country_code );
846 }
847
848 849 850 851 852 853 854 855 856 857
858 function edd_send_to_success_page( $query_string = null ) {
859 global $edd_options;
860
861 $redirect = get_permalink($edd_options['success_page']);
862
863 if ( $query_string )
864 $redirect .= $query_string;
865
866 wp_redirect( apply_filters('edd_success_page_redirect', $redirect, $_POST['edd-gateway'], $query_string) );
867 exit;
868 }
869
870 871 872 873 874 875 876 877 878 879 880
881 function edd_send_back_to_checkout( $args = array() ) {
882 $redirect = edd_get_checkout_uri();
883
884 if ( ! empty( $args ) ) {
885
886 if ( is_string( $args ) )
887 $args = str_replace( '?', '', $args );
888
889 $args = wp_parse_args( $args );
890
891 $redirect = add_query_arg( $args, $redirect );
892 }
893
894 wp_redirect( apply_filters( 'edd_send_back_to_checkout', $redirect, $args ) );
895 exit;
896 }
897
898 899 900 901 902 903 904 905 906 907
908 function edd_get_success_page_url( $query_string = null ) {
909 global $edd_options;
910
911 $success_page = get_permalink($edd_options['success_page']);
912 if ( $query_string )
913 $success_page .= $query_string;
914
915 return apply_filters( 'edd_success_page_url', $success_page );
916 }