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  * Download Functions
  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  * Retrieves a download post object by ID or slug.
 17  *
 18  * @since 1.0
 19  * @param int $download Download ID
 20  * @return object $download Entire download data
 21  */
 22 function edd_get_download( $download ) {
 23     if ( is_numeric( $download ) ) {
 24         $download = get_post( $download );
 25         if ( $download->post_type != 'download' )
 26             return null;
 27         return $download;
 28     }
 29 
 30     $args = array(
 31         'post_type'   => 'download',
 32         'name'        => $download,
 33         'numberposts' => 1
 34     );
 35 
 36     $download = get_posts($args);
 37 
 38     if ( $download ) {
 39         return $download[0];
 40     }
 41 
 42     return null;
 43 }
 44 
 45 /**
 46  * Returns the price of a download, but only for non-variable priced downloads.
 47  *
 48  * @since 1.0
 49  * @param int $download_id ID number of the download to retrieve a price for
 50  * @return mixed string|int Price of the download
 51  */
 52 function edd_get_download_price( $download_id ) {
 53     $price = get_post_meta( $download_id, 'edd_price', true );
 54     if ( $price )
 55         return edd_sanitize_amount( $price );
 56     return  0;
 57 }
 58 
 59 /**
 60  * Displays a formatted price for a download
 61  *
 62  * @since 1.0
 63  * @param int $download_id ID of the download price to show
 64  * @param bool $echo Whether to echo or return the results
 65  * @return void
 66  */
 67 function edd_price( $download_id, $echo = true ) {
 68     if ( edd_has_variable_prices( $download_id ) ) {
 69         $prices = edd_get_variable_prices( $download_id );
 70         // Return the lowest price
 71         $price_float = 0;
 72         foreach ($prices as $key => $value)
 73             if ( ( ( (float)$prices[ $key ]['amount'] ) < $price_float ) or ( $price_float == 0 ) )
 74                 $price_float = (float)$prices[ $key ]['amount'];
 75             $price = edd_sanitize_amount( $price_float );
 76     } else {
 77         $price = edd_get_download_price( $download_id );
 78     }
 79 
 80     $price = apply_filters( 'edd_download_price', $price, $download_id );
 81 
 82     $price = '<span class="edd_price" id="edd_price_' . $download_id . '">' . $price . '</span>';
 83 
 84     if ( $echo )
 85         echo $price;
 86     else
 87         return $price;
 88 }
 89 add_filter( 'edd_download_price', 'edd_format_amount', 10 );
 90 add_filter( 'edd_download_price', 'edd_currency_filter', 20 );
 91 
 92 /**
 93  * Retrieves the final price of a downloadable product after purchase
 94  * this price includes any necessary discounts that were applied
 95  *
 96  * @since 1.0
 97  * @param int $download_id ID of the download
 98  * @param array $user_purchase_info - an array of all information for the payment
 99  * @param string $amount_override a custom amount that over rides the 'edd_price' meta, used for variable prices
100  * @return string - the price of the download
101  */
102 function edd_get_download_final_price( $download_id, $user_purchase_info, $amount_override = null ) {
103     if ( is_null( $amount_override ) ) {
104         $original_price = get_post_meta( $download_id, 'edd_price', true );
105     } else {
106         $original_price = $amount_override;
107     }
108     if ( isset( $user_purchase_info['discount'] ) && $user_purchase_info['discount'] != 'none' ) {
109         // if the discount was a %, we modify the amount. Flat rate discounts are ignored
110         if ( edd_get_discount_type( edd_get_discount_id_by_code( $user_purchase_info['discount'] ) ) != 'flat' )
111             $price = edd_get_discounted_amount( $user_purchase_info['discount'], $original_price );
112         else
113             $price = $original_price;
114     } else {
115         $price = $original_price;
116     }
117     return apply_filters( 'edd_final_price', $price, $download_id, $user_purchase_info );
118 }
119 
120 /**
121  * Retrieves the variable prices for a download
122  *
123  * @since 1.2
124  * @param int $download_id ID of the download
125  * @return array Variable prices
126  */
127 function edd_get_variable_prices( $download_id ) {
128     return get_post_meta( $download_id, 'edd_variable_prices', true );
129 }
130 
131 /**
132  * Checks to see if a download has variable prices enabled.
133  *
134  * @since 1.0.7
135  * @param int $download_id ID number of the download to checl
136  * @return bool true if has variable prices, false otherwise
137  */
138 function edd_has_variable_prices( $download_id ) {
139     if ( get_post_meta( $download_id, '_variable_pricing', true ) ) {
140         return true;
141     }
142 
143     return false;
144 }
145 
146 /**
147  * Retrieves the name of a variable price option
148  *
149  * @since 1.0.9
150  * @param int $download_id ID of the download
151  * @param int $price_id ID of the price option
152  * @param int @payment_id ID of the payment
153  * @return string $price_name Name of the price option
154  */
155 function edd_get_price_option_name( $download_id, $price_id, $payment_id = 0 ) {
156     $prices = edd_get_variable_prices( $download_id );
157     $price_name = '';
158 
159     if ( $prices && is_array( $prices ) ) {
160         if ( isset( $prices[ $price_id ] ) )
161             $price_name = $prices[ $price_id ]['name'];
162     }
163 
164     return apply_filters( 'edd_get_price_option_name', $price_name, $download_id, $payment_id );
165 }
166 
167 /**
168  * Retrieves cheapest price option of a variable priced download
169  *
170  * @since 1.4.4
171  * @param int $download_id ID of the download
172  * @return float Amount of the lowest price
173  */
174 function edd_get_lowest_price_option( $download_id = 0 ) {
175     if ( empty( $download_id ) )
176         $download_id = get_the_ID();
177 
178     if ( ! edd_has_variable_prices( $download_id ) )
179         return edd_get_download_price( $download_id );
180 
181     $prices = edd_get_variable_prices( $download_id );
182 
183     $low = 0.00;
184 
185     if ( ! empty( $prices ) ) {
186         $min = 0;
187 
188         foreach ( $prices as $key => $price ) {
189             if ( empty( $price['amount'] ) )
190                 continue;
191             if ( $prices[ $min ]['amount'] > $price['amount'] )
192                 $min = $key;
193         }
194 
195         $low = $prices[ $min ]['amount'];
196     }
197 
198     return $low;
199 }
200 
201 /**
202  * Retrieves most expensive price option of a variable priced download
203  *
204  * @since 1.4.4
205  * @param int $download_id ID of the download
206  * @return float Amount of the highest price
207  */
208 function edd_get_highest_price_option( $download_id = 0 ) {
209     if ( empty( $download_id ) )
210         $download_id = get_the_ID();
211 
212     if ( ! edd_has_variable_prices( $download_id ) )
213         return edd_get_download_price( $download_id );
214 
215     $prices = edd_get_variable_prices( $download_id );
216 
217     $high = 0.00;
218 
219     if ( ! empty( $prices ) ) {
220         $max = 0;
221 
222         foreach ( $prices as $key => $price ) {
223             if ( empty( $price['amount'] ) )
224                 continue;
225 
226             if ( $prices[ $max ]['amount'] < $price['amount'] )
227                 $max = $key;
228         }
229 
230         $high = $prices[ $max ]['amount'];
231     }
232 
233     return $high;
234 }
235 
236 /**
237  * Retrieves a price from from low to high of a variable priced download
238  *
239  * @since 1.4.4
240  * @param int $download_id ID of the download
241  * @return string $range A fully formatted price range
242  */
243 function edd_price_range( $download_id = 0 ) {
244     $low   = edd_get_lowest_price_option( $download_id );
245     $high  = edd_get_highest_price_option( $download_id );
246     $range = '<span class="edd_price_range_low">' . edd_currency_filter( $low ) . '</span>';
247     $range .= '<span class="edd_price_range_sep">&nbsp;&ndash;&nbsp;</span>';
248     $range .= '<span class="edd_price_range_high">' . edd_currency_filter( $high ) . '</span>';
249 
250     return apply_filters( 'edd_price_range', $range, $download_id, $low, $high );
251 }
252 
253 /**
254  * Checks to see if multiple price options can be purchased at once
255  *
256  * @since 1.4.2
257  * @param int $download_id Download ID
258  * @return bool
259  */
260 function edd_single_price_option_mode( $download_id = 0 ) {
261     if ( empty( $download_id ) )
262         $download_id = get_the_ID();
263 
264     $ret = get_post_meta( $download_id, '_edd_price_options_mode', true );
265 
266     return (bool) apply_filters( 'edd_single_price_option_mode', $ret, $download_id );
267 }
268 
269 /**
270  * Gets the Download type, either default or "bundled"
271  *
272  * @since 1.5
273  * @param int $download_id Download ID
274  * @return string $type Download type
275  */
276 function edd_get_download_type( $download_id ) {
277     $type = get_post_meta( $download_id, '_edd_product_type', true );
278     return apply_filters( 'edd_get_download_type', $type, $download_id );
279 }
280 
281 /**
282  * Retrieves the product IDs of bundled products
283  *
284  * @since 1.5
285  * @param int $download_id Download ID
286  * @return array $products Products in the bundle
287  */
288 function edd_get_bundled_products( $download_id = 0 ) {
289     $products = get_post_meta( $download_id, '_edd_bundled_products', true );
290     return apply_filters( 'edd_get_bundled_products', $products, $download_id );
291 }
292 
293 /**
294  * Returns the total earnings for a download.
295  *
296  * @since 1.0
297  * @param int $download_id Download ID
298  * @return int $earnings Earnings for a certain download
299  */
300 function edd_get_download_earnings_stats( $download_id ) {
301     // If the current Download CPT has no earnings value associated with it, we need to initialize it.
302     // This is what enables us to sort it.
303     if ( '' == get_post_meta( $download_id, '_edd_download_earnings', true ) ) {
304         add_post_meta( $download_id, '_edd_download_earnings', 0 );
305     }
306 
307     $earnings = get_post_meta( $download_id, '_edd_download_earnings', true );
308 
309     return $earnings;
310 }
311 
312 /**
313  * Return the sales number for a download.
314  *
315  * @since 1.0
316  * @param int $download_id Download ID
317  * @return int $sales Amount of sales for a certain download
318  */
319 function edd_get_download_sales_stats( $download_id ) {
320     // If the current Download CPT has no sales value associated with it, we need to initialize it.
321     // This is what enables us to sort it.
322     if ( '' == get_post_meta( $download_id, '_edd_download_sales', true ) ) {
323         add_post_meta( $download_id, '_edd_download_sales', 0 );
324     } // End if
325 
326     $sales = get_post_meta( $download_id, '_edd_download_sales', true );
327 
328     return $sales;
329 }
330 
331 /**
332  * Record Sale In Log
333  *
334  * Stores log information for a download sale.
335  *
336  * @since 1.0
337  * @global $edd_logs
338  * @param int $download_id Download ID
339  * @param int $payment_id Payment ID
340  * @return void
341 */
342 function edd_record_sale_in_log( $download_id, $payment_id ) {
343     global $edd_logs;
344 
345     $log_data = array(
346         'post_parent'   => $download_id,
347         'log_type'      => 'sale'
348     );
349 
350     $log_meta = array(
351         'payment_id'    => $payment_id
352     );
353 
354     $log_id = $edd_logs->insert_log( $log_data, $log_meta );
355 }
356 
357 /**
358  * Record Download In Log
359  *
360  * Stores a log entry for a file download.
361  *
362  * @since 1.0
363  * @global $edd_logs
364  * @param int $download_id Download ID
365  * @param int $file_id ID of the file dowloded
366  * @param array $user_info User information
367  * @param string $ip IP Address
368  * @param int $payment_id Payment ID
369  * @return void
370  */
371 function edd_record_download_in_log( $download_id, $file_id, $user_info, $ip, $payment_id ) {
372     global $edd_logs;
373 
374     $log_data = array(
375         'post_parent'   => $download_id,
376         'log_type'      => 'file_download'
377     );
378 
379     $log_meta = array(
380         'user_info' => $user_info,
381         'user_id'   => (int) $user_info['id'],
382         'file_id'   => (int) $file_id,
383         'ip'        => $ip,
384         'payment_id'=> $payment_id
385     );
386 
387     $log_id = $edd_logs->insert_log( $log_data, $log_meta );
388 }
389 
390 /**
391  * Delete log entries when deleting download product
392  *
393  * Removes all related log entries when a download is completely deleted.
394  * (Does not run when a download is trashed)
395  *
396  * @since 1.3.4
397  * @param int $download_id Download ID
398  * @return void
399  */
400 function edd_remove_download_logs_on_delete( $download_id = 0 ) {
401     if ( 'download' != get_post_type( $download_id ) )
402         return;
403 
404     global $edd_logs;
405 
406     // Remove all log entries related to this download
407     $edd_logs->delete_logs( $download_id );
408 }
409 add_action( 'delete_post', 'edd_remove_download_logs_on_delete' );
410 
411 /**
412  *
413  * Increases the sale count of a download.
414  *
415  * @since 1.0
416  * @param int $download_id Download ID
417  * @return void
418  */
419 function edd_increase_purchase_count( $download_id ) {
420     $sales = edd_get_download_sales_stats( $download_id );
421     $sales = $sales + 1;
422     if ( update_post_meta( $download_id, '_edd_download_sales', $sales ) )
423         return $sales;
424 
425     return false;
426 }
427 
428 /**
429  * Decreases the sale count of a download. Primarily for when a purchase is
430  * refunded.
431  *
432  * @since 1.0.8.1
433  * @param int $download_id Download ID
434  * @return void
435  */
436 function edd_decrease_purchase_count( $download_id ) {
437     $sales = edd_get_download_sales_stats( $download_id );
438     if ( $sales > 0 ) // Only decrease if not already zero
439         $sales = $sales - 1;
440 
441     if ( update_post_meta( $download_id, '_edd_download_sales', $sales ) )
442         return $sales;
443 
444     return false;
445 }
446 
447 /**
448  * Increases the total earnings of a download.
449  *
450  * @since 1.0
451  * @param int $download_id Download ID
452  * @param int $amount Earnings
453  * @return void
454  */
455 function edd_increase_earnings( $download_id, $amount ) {
456     $earnings = edd_get_download_earnings_stats( $download_id );
457     $earnings = $earnings + $amount;
458 
459     if ( update_post_meta( $download_id, '_edd_download_earnings', $earnings ) )
460         return $earnings;
461 
462     return false;
463 }
464 
465 /**
466  * Decreases the total earnings of a download. Primarily for when a purchase is refunded.
467  *
468  * @since 1.0.8.1
469  * @param int $download_id Download ID
470  * @param int $amount Earnings
471  * @return void
472  */
473 function edd_decrease_earnings( $download_id, $amount ) {
474     $earnings = edd_get_download_earnings_stats( $download_id );
475 
476     if ( $earnings > 0 ) // Only decrease if greater than zero
477         $earnings = $earnings - $amount;
478 
479     if ( update_post_meta( $download_id, '_edd_download_earnings', $earnings ) )
480         return $earnings;
481 
482     return false;
483 }
484 
485 /**
486  * Retreives the average monthly earnings for a specific download
487  *
488  * @since 1.3
489  * @param int $download_id Download ID
490  * @return float $earnings Average monthly earnings
491  */
492 function edd_get_average_monthly_download_earnings( $download_id ) {
493     $earnings     = edd_get_download_earnings_stats( $download_id );
494     $release_date = get_post_field( 'post_date', $download_id );
495 
496     $diff   = abs( time() - strtotime( $release_date ) );
497 
498     $years  = floor( $diff / ( 365*60*60*24 ) );                            // Number of years since publication
499     $months = floor( ( $diff - $years * 365*60*60*24 ) / ( 30*60*60*24 ) ); // Number of months since publication
500 
501     if ( $months > 0 )
502         return ( $earnings / $months );
503 
504     return $earnings;
505 }
506 
507 /**
508  * Retreives the average monthly sales for a specific download
509  *
510  * @since 1.3
511  * @param int $download_id Download ID
512  * @return float $sales Average monthly sales
513  */
514 function edd_get_average_monthly_download_sales( $download_id ) {
515     $sales          = edd_get_download_sales_stats( $download_id );
516     $release_date   = get_post_field( 'post_date', $download_id );
517 
518     $diff   = abs( time() - strtotime( $release_date ) );
519 
520     $years  = floor( $diff / ( 365*60*60*24 ) );                            // Number of years since publication
521     $months = floor( ( $diff - $years * 365*60*60*24 ) / ( 30*60*60*24 ) ); // Number of months since publication
522 
523     if ( $months > 0 )
524         return ( $sales / $months );
525 
526     return $sales;
527 }
528 
529 /**
530  * Gets all download files for a product
531  *
532  * Can retrieve files specific to price ID
533  *
534  * @since 1.0
535  * @param int $download_id Download ID
536  * @param int $variable_price_id Variable pricing option ID
537  * @return array $files Download files
538  */
539 function edd_get_download_files( $download_id, $variable_price_id = null ) {
540     $files = array();
541     $download_files = get_post_meta( $download_id, 'edd_download_files', true );
542 
543     if ( $download_files ) {
544         if ( ! is_null( $variable_price_id ) ) {
545             foreach ( $download_files as $key => $file_info ) {
546                 if ( isset( $file_info['condition'] ) ) {
547                     if ( $file_info['condition'] == $variable_price_id || $file_info['condition'] == 'all' ) {
548                         $files[ $key ] = $file_info;
549                     }
550                 }
551             }
552         } else {
553             $files = $download_files;
554         }
555     }
556 
557     return $files;
558 }
559 
560 /**
561  * Gets the file download file limit for a particular download
562  *
563  * This limit refers to the maximum number of times files connected to a product
564  * can be downloaded.
565  *
566  * @since 1.3.1
567  * @param int $download_id Download ID
568  * @return int $limit File download limit
569  */
570 function edd_get_file_download_limit( $download_id = 0 ) {
571     $limit = get_post_meta( $download_id, '_edd_download_limit', true );
572     if ( $limit )
573         return absint( $limit );
574     return 0;
575 }
576 
577 /**
578  * Gets the file download file limit override for a particular download
579  *
580  * The override allows the main file download limit to be bypassed
581  *
582  * @since 1.3.2
583  * @param int $download_id Download ID
584  * @param int $payment_id Payment ID
585  * @return int $limit_override The new limit
586 */
587 function edd_get_file_download_limit_override( $download_id = 0, $payment_id = 0 ) {
588     $limit_override = get_post_meta( $download_id, '_edd_download_limit_override_' . $payment_id, true );
589     if ( $limit_override ) {
590         return absint( $limit_override );
591     }
592     return 0;
593 }
594 
595 /**
596  * Sets the file download file limit override for a particular download
597  *
598  * The override allows the main file download limit to be bypassed
599  * If no override is set yet, the override is set to the main limmit + 1
600  * If the override is already set, then it is simply incremented by 1
601  *
602  * @since 1.3.2
603  * @param int $download_id Download ID
604  * @param int $payment_id Payment ID
605  * @return void
606  */
607 function edd_set_file_download_limit_override( $download_id = 0, $payment_id = 0 ) {
608     $override   = edd_get_file_download_limit_override( $download_id );
609     $limit      = edd_get_file_download_limit( $download_id );
610 
611     if ( ! empty( $override ) ) {
612         $override = $override += 1;
613     } else {
614         $override = $limit += 1;
615     }
616     update_post_meta( $download_id, '_edd_download_limit_override_' . $payment_id, $override );
617 }
618 
619 /**
620  * Checks if a file is at its download limit
621  *
622  * This limit refers to the maximum number of times files connected to a product
623  * can be downloaded.
624  *
625  * @since 1.3.1
626  * @uses EDD_Logging::get_log_count()
627  * @param int $download_id Download ID
628  * @param int $payment_id Payment ID
629  * @param int $file_id File ID
630  * @return bool True if at limit, false otherwise
631  */
632 function edd_is_file_at_download_limit( $download_id = 0, $payment_id = 0, $file_id = 0 ) {
633     // Checks to see if at limit
634     $logs = new EDD_Logging();
635 
636     $meta_query = array(
637         'relation'  => 'AND',
638         array(
639             'key'   => '_edd_log_file_id',
640             'value' => (int) $file_id
641         ),
642         array(
643             'key'   => '_edd_log_payment_id',
644             'value' => (int) $payment_id
645         )
646     );
647 
648     $ret = false;
649     $download_count = $logs->get_log_count( $download_id, 'file_download', $meta_query );
650     $download_limit = edd_get_file_download_limit( $download_id );
651 
652     if ( ! empty( $download_limit ) ) {
653         if ( $download_count >= $download_limit ) {
654             $ret = true;
655 
656             // Check to make sure the limit isn't overwritten
657             // A limit is overwritten when purchase receipt is resent
658             $limit_override = edd_get_file_download_limit_override( $download_id, $payment_id );
659 
660             if ( ! empty( $limit_override ) && $download_count < $limit_override ) {
661                 $ret = false;
662             }
663         }
664     }
665 
666     return (bool) apply_filters( 'edd_is_file_at_download_limit', $ret, $download_id, $payment_id, $file_id );
667 }
668 
669 /**
670  * Gets the Price ID that can download a file
671  *
672  * @since 1.0.9
673  * @param int $download_id Download ID
674  * @param string $file_key File Key
675  * @return string - the price ID if restricted, "all" otherwise
676  */
677 function edd_get_file_price_condition( $download_id, $file_key ) {
678     $files = edd_get_download_files( $download_id );
679 
680     if ( ! $files )
681         return false;
682 
683     $condition = isset( $files[ $file_key ]['condition']) ? $files[ $file_key ]['condition'] : 'all';
684 
685     return $condition;
686 }
687 
688 /**
689  * Get Download File Url
690  *
691  * Constructs the file download url for a specific file.
692  *
693  * @since 1.0
694  * @param string $key
695  * @param string $email Customer email addresss
696  * @param int $filekey File key
697  * @param int $download_id Download ID
698  * @param int $price_id
699  * @return string $download_url Constructed download URL
700 */
701 function edd_get_download_file_url( $key, $email, $filekey, $download_id, $price_id = false ) {
702     global $edd_options;
703 
704     $hours = isset( $edd_options['download_link_expiration'] )
705             && is_numeric( $edd_options['download_link_expiration'] )
706             ? absint($edd_options['download_link_expiration']) : 24;
707 
708     if ( ! ( $date = strtotime( '+' . $hours . 'hours' ) ) )
709         $date = 2147472000; // Highest possible date, January 19, 2038
710 
711     $params = array(
712         'download_key'  => $key,
713         'email'         => rawurlencode( $email ),
714         'file'          => $filekey,
715         'price_id'      => (int) $price_id,
716         'download'      => $download_id,
717         'expire'        => rawurlencode( base64_encode( $date ) )
718     );
719 
720     $params = apply_filters( 'edd_download_file_url_args', $params );
721 
722     $download_url = add_query_arg( $params, home_url() );
723 
724     return $download_url;
725 }
726 
727 /**
728  * Verifies a download purchase using a purchase key and email.
729  *
730  * @since 1.0
731  * @param int $download_id Download ID
732  * @param string $key
733  * @param string $email
734  * @param string $expire
735  * @param string $file_key
736  * @return bool True if payment and link was verified, false otherwise
737  */
738 function edd_verify_download_link( $download_id, $key, $email, $expire, $file_key ) {
739     $meta_query = array(
740         'relation'  => 'AND',
741         array(
742             'key'   => '_edd_payment_purchase_key',
743             'value' => $key
744         ),
745         array(
746             'key'   => '_edd_payment_user_email',
747             'value' => $email
748         )
749     );
750 
751     $payments = get_posts( array( 'meta_query' => $meta_query, 'post_type' => 'edd_payment' ) );
752 
753     if ( $payments ) {
754         foreach ( $payments as $payment ) {
755             $payment_meta   = edd_get_payment_meta( $payment->ID );
756             $downloads      = maybe_unserialize( $payment_meta['downloads'] );
757             $cart_details   = unserialize( $payment_meta['cart_details'] );
758 
759             if ( $payment->post_status != 'publish' && $payment->post_status != 'complete' )
760                 return false;
761 
762             if ( $downloads ) {
763                 foreach ( $downloads as $download_key => $download ) {
764 
765                     $id = isset( $payment_meta['cart_details'] ) ? $download['id'] : $download;
766 
767                     if ( $id != $download_id )
768                         continue;
769 
770                     $price_options = isset( $cart_details[ $download_key ]['item_number']['options'] ) ? $cart_details[ $download_key ]['item_number']['options'] : false;
771 
772                     $file_condition = edd_get_file_price_condition( $id, $file_key );
773 
774                     // If this download has variable prices, we have to confirm that this file was included in their purchase
775                     if ( ! empty( $price_options ) && $file_condition != 'all' && edd_has_variable_prices( $id ) ) {
776                         if ( $file_condition == $price_options['price_id'] )
777                             return $payment->ID;
778                     }
779 
780                     // Check to see if the file download limit has been reached
781                     if ( edd_is_file_at_download_limit( $id, $payment->ID, $file_key ) )
782                         wp_die( apply_filters( 'edd_download_limit_reached_text', __( 'Sorry but you have hit your download limit for this file.', 'edd' ) ), __( 'Error', 'edd' ) );
783 
784                     // Make sure the link hasn't expired
785                     if ( time() < $expire ) {
786                         return $payment->ID; // Payment has been verified and link is still valid
787                     }
788                     return false; // Payment verified, but link is no longer valid
789                 }
790 
791             }
792 
793         }
794 
795     }
796     // Payment not verified
797     return false;
798 }
799 
800 /**
801  * Get product notes
802  *
803  * @since 1.2.1
804  * @param int $download_id Download ID
805  * @return string $notes Product notes
806  */
807 function edd_get_product_notes( $download_id ) {
808     $notes = get_post_meta( $download_id, 'edd_product_notes', true );
809 
810     if ( $notes )
811         return (string) apply_filters( 'edd_product_notes', $notes, $download_id );
812 
813     return '';
814 }
Easy Digital Downloads API documentation generated by ApiGen 2.8.0