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 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 47 48 49 50 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 61 62 63 64 65 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
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 94 95 96 97 98 99 100 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
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 122 123 124 125 126
127 function edd_get_variable_prices( $download_id ) {
128 return get_post_meta( $download_id, 'edd_variable_prices', true );
129 }
130
131 132 133 134 135 136 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 148 149 150 151 152 153 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 169 170 171 172 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 203 204 205 206 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 238 239 240 241 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"> – </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 255 256 257 258 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 271 272 273 274 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 283 284 285 286 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 295 296 297 298 299
300 function edd_get_download_earnings_stats( $download_id ) {
301
302
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 314 315 316 317 318
319 function edd_get_download_sales_stats( $download_id ) {
320
321
322 if ( '' == get_post_meta( $download_id, '_edd_download_sales', true ) ) {
323 add_post_meta( $download_id, '_edd_download_sales', 0 );
324 }
325
326 $sales = get_post_meta( $download_id, '_edd_download_sales', true );
327
328 return $sales;
329 }
330
331 332 333 334 335 336 337 338 339 340 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 359 360 361 362 363 364 365 366 367 368 369 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 392 393 394 395 396 397 398 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
407 $edd_logs->delete_logs( $download_id );
408 }
409 add_action( 'delete_post', 'edd_remove_download_logs_on_delete' );
410
411 412 413 414 415 416 417 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 430 431 432 433 434 435
436 function edd_decrease_purchase_count( $download_id ) {
437 $sales = edd_get_download_sales_stats( $download_id );
438 if ( $sales > 0 )
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 449 450 451 452 453 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 467 468 469 470 471 472
473 function edd_decrease_earnings( $download_id, $amount ) {
474 $earnings = edd_get_download_earnings_stats( $download_id );
475
476 if ( $earnings > 0 )
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 487 488 489 490 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 ) );
499 $months = floor( ( $diff - $years * 365*60*60*24 ) / ( 30*60*60*24 ) );
500
501 if ( $months > 0 )
502 return ( $earnings / $months );
503
504 return $earnings;
505 }
506
507 508 509 510 511 512 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 ) );
521 $months = floor( ( $diff - $years * 365*60*60*24 ) / ( 30*60*60*24 ) );
522
523 if ( $months > 0 )
524 return ( $sales / $months );
525
526 return $sales;
527 }
528
529 530 531 532 533 534 535 536 537 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 562 563 564 565 566 567 568 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 579 580 581 582 583 584 585 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 597 598 599 600 601 602 603 604 605 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 621 622 623 624 625 626 627 628 629 630 631
632 function edd_is_file_at_download_limit( $download_id = 0, $payment_id = 0, $file_id = 0 ) {
633
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
657
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 671 672 673 674 675 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 690 691 692 693 694 695 696 697 698 699 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;
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 729 730 731 732 733 734 735 736 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
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
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
785 if ( time() < $expire ) {
786 return $payment->ID;
787 }
788 return false;
789 }
790
791 }
792
793 }
794
795 }
796
797 return false;
798 }
799
800 801 802 803 804 805 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 }