Easy Digital Downloads
  • Package
  • Function
  • Tree

Packages

  • EDD
    • Admin
      • Actions
      • Add-ons
      • Dashboard
      • Discounts
      • Downloads
      • Export
      • Notices
      • Pages
      • Payments
      • Reports
      • Settings
      • System
      • Upgrades
      • Upload
      • Welcome
    • Cart
    • Checkout
    • Classes
      • API
      • Fees
      • HTML
      • Roles
      • Session
    • Emails
    • Functions
      • AJAX
      • Compatibility
      • Errors
      • Formatting
      • Install
      • Login
      • Taxes
      • Templates
    • Gateways
    • Logging
    • Payments
    • Shortcodes
    • Widgets

Functions

  • _edd_deprecated_function
  • edd_admin_downloads_icon
  • edd_allowed_mime_types
  • edd_block_attachments
  • edd_cart_discounts_html
  • edd_cart_has_discounts
  • edd_change_default_title
  • edd_cleanup_file_symlinks
  • edd_count_file_downloads_of_user
  • edd_count_purchases_of_customer
  • edd_decrease_earnings
  • edd_decrease_purchase_count
  • edd_deliver_download
  • edd_discount_exists
  • edd_discount_is_min_met
  • edd_discount_is_single_use
  • edd_discount_product_reqs_met
  • edd_display_cart_discount
  • edd_format_discount_rate
  • edd_get_actions
  • edd_get_average_monthly_download_earnings
  • edd_get_average_monthly_download_sales
  • edd_get_bundled_products
  • edd_get_cart_discounted_amount
  • edd_get_cart_discounts
  • edd_get_cart_discounts_html
  • edd_get_country_list
  • edd_get_currencies
  • edd_get_current_page_url
  • edd_get_default_labels
  • edd_get_discount
  • edd_get_discount_amount
  • edd_get_discount_by_code
  • edd_get_discount_code
  • edd_get_discount_expiration
  • edd_get_discount_id_by_code
  • edd_get_discount_max_uses
  • edd_get_discount_min_price
  • edd_get_discount_product_condition
  • edd_get_discount_product_reqs
  • edd_get_discount_start_date
  • edd_get_discount_type
  • edd_get_discount_uses
  • edd_get_discounted_amount
  • edd_get_discounts
  • edd_get_download
  • edd_get_download_earnings_stats
  • edd_get_download_file_url
  • edd_get_download_files
  • edd_get_download_final_price
  • edd_get_download_price
  • edd_get_download_sales_stats
  • edd_get_download_type
  • edd_get_file_ctype
  • edd_get_file_download_limit
  • edd_get_file_download_limit_override
  • edd_get_file_extension
  • edd_get_file_price_condition
  • edd_get_highest_price_option
  • edd_get_ip
  • edd_get_label_plural
  • edd_get_label_singular
  • edd_get_lowest_price_option
  • edd_get_php_arg_separator_output
  • edd_get_price_option_name
  • edd_get_product_notes
  • edd_get_provinces_list
  • edd_get_purchase_cc_info
  • edd_get_purchase_form_user
  • edd_get_states_list
  • edd_get_success_page_url
  • edd_get_symlink_dir
  • edd_get_symlink_url
  • edd_get_users_purchases
  • edd_get_variable_prices
  • edd_has_active_discounts
  • edd_has_purchases
  • edd_has_user_purchased
  • edd_has_variable_prices
  • edd_increase_discount_usage
  • edd_increase_earnings
  • edd_increase_purchase_count
  • edd_is_cc_verify_enabled
  • edd_is_discount_active
  • edd_is_discount_expired
  • edd_is_discount_maxed_out
  • edd_is_discount_not_global
  • edd_is_discount_started
  • edd_is_discount_used
  • edd_is_discount_valid
  • edd_is_file_at_download_limit
  • edd_is_func_disabled
  • edd_is_odd
  • edd_is_test_mode
  • edd_let_to_num
  • edd_load_admin_scripts
  • edd_load_scripts
  • edd_logged_in_only
  • edd_month_num_to_name
  • edd_no_guest_checkout
  • edd_no_redownload
  • edd_post_actions
  • edd_presstrends
  • edd_price
  • edd_price_range
  • edd_process_download
  • edd_process_purchase_form
  • edd_purchase_form_required_fields
  • edd_purchase_form_validate_agree_to_terms
  • edd_purchase_form_validate_cc
  • edd_purchase_form_validate_cc_zip
  • edd_purchase_form_validate_discounts
  • edd_purchase_form_validate_fields
  • edd_purchase_form_validate_gateway
  • edd_purchase_form_validate_guest_user
  • edd_purchase_form_validate_logged_in_user
  • edd_purchase_form_validate_new_user
  • edd_purchase_form_validate_user_login
  • edd_purchase_total_of_user
  • edd_query_vars
  • edd_readfile_chunked
  • edd_record_download_in_log
  • edd_record_sale_in_log
  • edd_register_and_login_new_user
  • edd_register_post_type_statuses
  • edd_register_styles
  • edd_remove_cart_discount
  • edd_remove_discount
  • edd_remove_download_logs_on_delete
  • edd_send_back_to_checkout
  • edd_send_to_success_page
  • edd_set_cart_discount
  • edd_set_file_download_limit_override
  • edd_setup_download_taxonomies
  • edd_setup_edd_post_types
  • edd_single_price_option_mode
  • edd_store_discount
  • edd_straight_to_checkout
  • edd_string_is_image_url
  • edd_unset_all_cart_discounts
  • edd_unset_cart_discount
  • edd_update_discount_status
  • edd_updated_messages
  • edd_validate_username
  • edd_verify_download_link
  • edd_version_in_header
  1 <?php
  2 /**
  3  * Process Purchase
  4  *
  5  * @package     EDD
  6  * @subpackage  Functions
  7  * @copyright   Copyright (c) 2013, Pippin Williamson
  8  * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
  9  * @since       1.0
 10  */
 11 
 12 // Exit if accessed directly
 13 if ( ! defined( 'ABSPATH' ) ) exit;
 14 
 15 /**
 16  * Process Purchase Form
 17  *
 18  * Handles the purchase form process.
 19  *
 20  * @access      private
 21  * @since       1.0
 22  * @version     1.0.8.1
 23  * @return      void
 24  */
 25 function edd_process_purchase_form() {
 26     // Make sure the cart isn't empty
 27     if ( ! edd_get_cart_contents() ) {
 28         edd_set_error( 'empty_cart', __( 'Your cart is empty.', 'edd') );
 29     } else {
 30         // Validate the form $_POST data
 31         $valid_data = edd_purchase_form_validate_fields();
 32 
 33         // Allow themes and plugins to hoook to errors
 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     // Setup user information
 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     // Setup purchase information
 65     $purchase_data = array(
 66         'downloads'    => edd_get_cart_contents(),
 67         'fees'         => edd_get_cart_fees(),              // Any arbitrary fees that have been added to the cart
 68         'subtotal'     => edd_get_cart_subtotal(),          // Amount before taxes and discounts
 69         'discount'     => edd_get_cart_discounted_amount(), // Discounted amount
 70         'tax'          => edd_get_cart_tax(),               // Taxed amount
 71         'price'        => edd_get_cart_total(),             // Amount after taxes
 72         'purchase_key' => strtolower( md5( uniqid() ) ),    // Random key
 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     // Add the user data for hooks
 83     $valid_data['user'] = $user;
 84 
 85     // Allow themes and plugins to hook before the gateway
 86     do_action( 'edd_checkout_before_gateway', $_POST, $user_info, $valid_data );
 87 
 88     // Allow the purchase data to be modified before it is sent to the gateway
 89     $purchase_data = apply_filters(
 90         'edd_purchase_data_before_gateway',
 91         $purchase_data,
 92         $valid_data
 93     );
 94 
 95     // If the total amount in the cart is 0, send to the manaul gateway. This emulates a free download purchase
 96     if ( !$purchase_data['price'] ) {
 97         // Revert to manual
 98         $valid_data['gateway'] = 'manual';
 99     }
100 
101     // Used for showing download links to non logged-in users after purchase, and for other plugins needing purchase data.
102     edd_set_purchase_session( $purchase_data );
103 
104     // Send info to the gateway for payment processing
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  * Purchase Form Validate Fields
114  *
115  * @access      private
116  * @since       1.0.8.1
117  * @return      bool|array
118  */
119 function edd_purchase_form_validate_fields() {
120     global $edd_options;
121 
122     // Check if there is $_POST
123     if ( empty( $_POST ) ) return false;
124 
125     // Start an array to collect valid data
126     $valid_data = array(
127         'gateway'          => edd_purchase_form_validate_gateway(), // Gateway fallback
128         'discount'         => edd_purchase_form_validate_discounts(),    // Set default discount
129         'need_new_user'    => false,     // New user flag
130         'need_user_login'  => false,     // Login user flag
131         'logged_user_data' => array(),   // Logged user collected data
132         'new_user_data'    => array(),   // New user collected data
133         'login_user_data'  => array(),   // Login user collected data
134         'guest_user_data'  => array(),   // Guest user collected data
135         'cc_info'          => edd_purchase_form_validate_cc()    // Credit card info
136     );
137 
138     // Validate agree to terms
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         // Collect logged in user data
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        // Set new user registrarion as required
147       $valid_data['need_new_user'] = true;
148 
149        // Validate new user data
150       $valid_data['new_user_data'] = edd_purchase_form_validate_new_user();
151     // Check if login validation is needed
152     } else if ( isset( $_POST['edd-purchase-var'] ) && $_POST['edd-purchase-var'] == 'needs-to-login' ) {
153         // Set user login as required
154         $valid_data['need_user_login'] = true;
155 
156         // Validate users login info
157         $valid_data['login_user_data'] = edd_purchase_form_validate_user_login();
158     } else {
159         // Not registering or logging in, so setup guest user data
160         $valid_data['guest_user_data'] = edd_purchase_form_validate_guest_user();
161     }
162 
163     // Return collected data
164     return $valid_data;
165 }
166 
167 /**
168  * Purchase Form Validate Gateway
169  *
170  * @access      private
171  * @since       1.0
172  * @return      string
173  */
174 function edd_purchase_form_validate_gateway() {
175     // Check if a gateway value is present
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     // Return empty
191     return '';
192 }
193 
194 /**
195  * Purchase Form Validate Discounts
196  *
197  * @access      private
198  * @since       1.0.8.1
199  * @return      string
200  */
201 function edd_purchase_form_validate_discounts() {
202     // Retrieve the discount stored in cookies
203     $discounts = edd_get_cart_discounts();
204 
205     // Check for valid discount is present
206     if ( ! empty( $_POST['edd-discount'] ) || $discounts !== false  ) {
207         if( empty( $discounts ) ) {
208             $discount = sanitize_text_field( $_POST['edd-discount'] );
209         } else {
210             // Use the discount stored in the cookies
211             $discount = $discounts[0];
212 
213             // Note: At some point this will support multiple discounts
214         }
215 
216         $user = isset( $_POST['edd_user_login'] ) ? sanitize_text_field( $_POST['edd_user_login'] ) : sanitize_email( $_POST['edd_email'] );
217 
218         // Check if validates
219         if (  edd_is_discount_valid( $discount, $user ) ) {
220             // Return clean discount
221             return $discount;
222         } else {
223             // Set invalid discount error
224             edd_set_error( 'invalid_discount', __( 'The discount you entered is invalid', 'edd' ) );
225         }
226     }
227     // Return default value
228     return 'none';
229 }
230 
231 /**
232  * Purchase Form Validate Agree To Terms
233  *
234  * @access      private
235  * @since       1.0.8.1
236  * @return      void
237 */
238 function edd_purchase_form_validate_agree_to_terms() {
239     // Validate agree to terms
240     if ( ! isset( $_POST['edd_agree_to_terms'] ) || $_POST['edd_agree_to_terms'] != 1 ) {
241         // User did not agree
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  * Purchase Form Required Fields
248  *
249  * @access      private
250  * @since       1.5
251  * @return      array
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  * Purchase Form Validate Logged In User
265  *
266  * @access      private
267  * @since       1.0
268  * @return      array
269 */
270 function edd_purchase_form_validate_logged_in_user() {
271     global $user_ID;
272 
273     // Start empty array to collect valid user data
274     $valid_user_data = array(
275         // Assume there will be errors
276         'user_id' => -1
277     );
278 
279     // Verify there is a user_ID
280     if ( $user_ID > 0 ) {
281         // Get the logged in user data
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         // Loop through required fields and show error messages
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         // Verify data
296         if ( $user_data ) {
297             // Collected logged in user data
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             // Set invalid user error
306             edd_set_error( 'invalid_user', __( 'The user information is invalid.', 'edd' ) );
307         }
308     }
309 
310     // Return user data
311     return $valid_user_data;
312 }
313 
314 /**
315  * Purchase Form Validate New User
316  *
317  * @access      private
318  * @since       1.0.8.1
319  * @return      array
320 */
321 function edd_purchase_form_validate_new_user() {
322     $registering_new_user = false;
323 
324     // Start an empty array to collect valid user data
325     $valid_user_data = array(
326         // Assume there will be errors
327         'user_id' => -1,
328         // Get first name
329         'user_first' => isset( $_POST["edd_first"] ) ? strip_tags( trim( $_POST["edd_first"] ) ) : '',
330         // Get last name
331         'user_last' => isset( $_POST["edd_last"] ) ? strip_tags( trim( $_POST["edd_last"] ) ) : '',
332     );
333 
334     // Check the new user's credentials against existing ones
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     // Check if we have an username to register
342     if ( $user_login && strlen( $user_login ) > 0 ) {
343         $registering_new_user = true;
344 
345         // We have an user name, check if it already exists
346         if ( username_exists( $user_login ) ) {
347             // Username already registered
348             edd_set_error( 'username_unavailable', __( 'Username already taken', 'edd' ) );
349         // Check if it's valid
350         } else if ( ! edd_validate_username( $user_login ) ) {
351            // Invalid username
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             // All the checks have run and it's good to go
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     // Check if we have an email to verify
367     if ( $user_email && strlen( $user_email ) > 0 ) {
368         // Validate email
369         if ( ! is_email( $user_email ) ) {
370            edd_set_error( 'email_invalid', __('Invalid email', 'edd') );
371         // Check if email exists
372         } else if ( email_exists( $user_email ) && $registering_new_user ) {
373             edd_set_error( 'email_used', __('Email already used', 'edd') );
374         } else {
375            // All the checks have run and it's good to go
376             $valid_user_data['user_email'] = $user_email;
377         }
378     } else {
379         // No email
380         edd_set_error( 'email_empty', __('Enter an email', 'edd') );
381     }
382 
383     // Check password
384     if ( $user_pass && $pass_confirm ) {
385         // Verify confirmation matches
386         if ( $user_pass != $pass_confirm ) {
387             // Passwords do not match
388             edd_set_error( 'password_mismatch', __( 'Passwords don\'t match', 'edd' ) );
389         } else {
390             // All is good to go
391             $valid_user_data['user_pass'] = $user_pass;
392         }
393     } else {
394         // Password or confrimation missing
395         if ( ! $user_pass && $registering_new_user ) {
396             // The password is invalid
397             edd_set_error( 'password_empty', __( 'Enter a password', 'edd' ) );
398         } else if ( ! $pass_confirm && $registering_new_user ) {
399             // Confirmation password is invalid
400             edd_set_error( 'confirmation_empty', __( 'Enter the password confirmation', 'edd' ) );
401         }
402     }
403 
404     return $valid_user_data;
405 }
406 
407 /**
408  * Purchase Form Validate User Login
409  *
410  * @access      private
411  * @since       1.0.8.1
412  * @return      array
413 */
414 function edd_purchase_form_validate_user_login() {
415     // Start an array to collect valid user data
416     $valid_user_data = array(
417         // Assume there will be errors
418         'user_id' => -1
419     );
420 
421     // Username
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     // Get the user by login
428     $user_data = get_user_by( 'login', strip_tags( $_POST['edd_user_login'] ) );
429 
430     // Check if user exists
431     if ( $user_data ) {
432         // Get password
433         $user_pass = isset( $_POST["edd_user_pass"] ) ? $_POST["edd_user_pass"] : false;
434 
435         // Check user_pass
436         if ( $user_pass ) {
437             // Check if password is valid
438             if ( ! wp_check_password( $user_pass, $user_data->user_pass, $user_data->ID ) ) {
439                 // Incorrect password
440                 edd_set_error( 'password_incorrect', __( 'The password you entered is incorrect', 'edd' ) );
441             // All is correct
442             } else {
443                 // Repopulate the valid user data array
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             // Empty password
455             edd_set_error( 'password_empty', __( 'Enter a password', 'edd' ) );
456         }
457     } else {
458         //  no username
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  * Purchase Form Validate Guest User
467  *
468  * @access      private
469  * @since       1.0.8.1
470  * @return      array
471 */
472 function edd_purchase_form_validate_guest_user() {
473     // Start an array to collect valid user data
474     $valid_user_data = array(
475         // Set a default id for guests
476         'user_id' => 0,
477     );
478 
479     // Show error message if user must be logged in
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     // Get the guest email
485     $guest_email = isset( $_POST['edd_email'] ) ? $_POST['edd_email'] : false;
486 
487     // Check email
488     if ( $guest_email && strlen( $guest_email ) > 0 ) {
489         // Validate email
490         if ( ! is_email( $guest_email ) ) {
491             // Invalid email
492             edd_set_error( 'email_invalid', __( 'Invalid email', 'edd' ) );
493         } else {
494             // All is good to go
495             $valid_user_data['user_email'] = $guest_email;
496         }
497     } else {
498         // No email
499         edd_set_error( 'email_empty', __( 'Enter an email', 'edd' ) );
500     }
501 
502     // Loop through required fields and show error messages
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  * Register And Login New User
514  *
515  * @param array $user_data
516  *
517  * @access      private
518  * @since       1.0.8.1
519  * @return      integer
520 */
521 function edd_register_and_login_new_user( $user_data = array() ) {
522     // Verify the array
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     // Insert new user
537     $user_id = wp_insert_user( apply_filters( 'edd_insert_user_args', $user_args ) );
538 
539     // Validate inserted user
540     if ( is_wp_error( $user_id ) )
541         return -1;
542 
543     // Allow themes and plugins to hook
544     do_action( 'edd_insert_user', $user_id );
545 
546     // Login new user
547     edd_log_user_in( $user_id, $user_data['user_login'], $user_data['user_pass'] );
548 
549     // Return user id
550     return $user_id;
551 }
552 
553 /**
554  * Get Purchase Form User
555  *
556  * @param array $valid_data
557  *
558  * @access      private
559  * @since       1.0.8.1
560  * @return      array
561 */
562 function edd_get_purchase_form_user( $valid_data = array() ) {
563     // Initialize user
564     $user    = false;
565     $is_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX;
566 
567     if ( $is_ajax ) {
568         // Do not create or login the user during the ajax submission (check for errors only)
569         return true;
570     } else if ( is_user_logged_in() ) {
571         // Set the valid user as the logged in collected data
572         $user = $valid_data['logged_in_user'];
573     } else if ( $valid_data['need_new_user'] === true || $valid_data['need_user_login'] === true  ) {
574         // New user registration
575         if ( $valid_data['need_new_user'] === true ) {
576             // Set user
577             $user = $valid_data['new_user_data'];
578             // Register and login new user
579             $user['user_id'] = edd_register_and_login_new_user( $user );
580         // User login
581         } else if ( $valid_data['need_user_login'] === true  && ! $is_ajax ) {
582             // Set user
583             $user = $valid_data['login_user_data'];
584             // Login user
585             edd_log_user_in( $user['user_id'], $user['user_login'], $user['user_pass'] );
586         }
587     }
588 
589     // Check guest checkout
590     if ( false === $user && false === edd_no_guest_checkout() ) {
591         // Set user
592         $user = $valid_data['guest_user_data'];
593     }
594 
595     // Verify we have an user
596     if ( false === $user || empty( $user ) ) {
597         // Return false
598         return false;
599     }
600 
601     // Get user first name
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     // Get user last name
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     // Return valid user
612     return $user;
613 }
614 
615 /**
616  * Validates the credit card info
617  *
618  * @access      private
619  * @since       1.4.4
620  * @return      array
621  */
622 function edd_purchase_form_validate_cc() {
623     $card_data = edd_get_purchase_cc_info();
624 
625     // Validate the card zip
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     // This should validate card numbers at some point too
632     return $card_data;
633 }
634 
635 /**
636  * Get Credit Card Info
637  *
638  * @access      private
639  * @since       1.4.4
640  * @return      array
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     // Return cc info
668     return $cc_info;
669 }
670 
671 /**
672  * Validate zip code based on country code
673  *
674  * @access      private
675  * @since       1.4.4
676  * @return      bool
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]$", //india
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  * Send To Success Page
850  *
851  * Sends the user to the succes page.
852  *
853  * @param string $query_string
854  * @access      public
855  * @since       1.0
856  * @return      void
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  * Send back to checkout.
872  *
873  * Used to redirect a user back to the purchase
874  * page if there are errors present.
875  *
876  * @param array $args
877  * @access public
878  * @since  1.0
879  * @return Void
880  */
881 function edd_send_back_to_checkout( $args = array() ) {
882     $redirect = edd_get_checkout_uri();
883 
884     if ( ! empty( $args ) ) {
885         // Check for backward compatibility
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  * Get Success Page URL
900  *
901  * Gets the success page URL.
902  *
903  * @param string $query_string
904  * @access      public
905  * @since       1.0
906  * @return      string
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 }
Easy Digital Downloads API documentation generated by ApiGen 2.8.0