| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package GamiPress\Easy_Digital_Downloads\Functions |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
// Exit if accessed directly |
| 9 |
if( !defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Award user with points on purchase |
| 13 |
* |
| 14 |
* @param integer $payment_id |
| 15 |
*/ |
| 16 |
function gamipress_edd_maybe_award_points_on_purchase( $payment_id ) { |
| 17 |
|
| 18 |
$payment = edd_get_payment( $payment_id ); |
| 19 |
|
| 20 |
if( $payment ) { |
| 21 |
|
| 22 |
$cart_details = $payment->cart_details; |
| 23 |
|
| 24 |
if ( is_array( $cart_details ) ) { |
| 25 |
// Loop each cart item to check if someone awards points to the user |
| 26 |
foreach ( $cart_details as $index => $item ) { |
| 27 |
|
| 28 |
// Check if download was setup to award points |
| 29 |
$user_id = $payment->user_id; |
| 30 |
$award_points = (bool) get_post_meta( $item['id'], '_gamipress_edd_award_points', true ); |
| 31 |
|
| 32 |
/** |
| 33 |
* Filter to check if is available to award points for purchase |
| 34 |
* |
| 35 |
* @since 1.1.1 |
| 36 |
* |
| 37 |
* @param bool $award_points Whatever if should points be awarded to the user or not |
| 38 |
* @param int $user_id The user ID that will be awarded |
| 39 |
* @param int $download_id The download ID that user has purchased |
| 40 |
* @param int $payment_id The payment ID |
| 41 |
* @param array $item The cart item array |
| 42 |
*/ |
| 43 |
$award_points = apply_filters( 'gamipress_edd_award_points_for_purchase', $award_points, $user_id, $item['id'], $payment_id, $item ); |
| 44 |
|
| 45 |
if( $award_points ) { |
| 46 |
|
| 47 |
$quantity = absint( $item['quantity'] ); |
| 48 |
|
| 49 |
// If award points on purchase, then award them |
| 50 |
$points = absint( get_post_meta( $item['id'], '_gamipress_edd_points', true ) ); |
| 51 |
$points_type = get_post_meta( $item['id'], '_gamipress_edd_points_type', true ); |
| 52 |
|
| 53 |
// Setup the custom reason for the log |
| 54 |
if( $quantity === 1 ) { |
| 55 |
$reason = sprintf( |
| 56 |
__( '%d %s awarded for purchase %s', 'gamipress' ), |
| 57 |
$points, |
| 58 |
gamipress_get_points_type_plural( $points_type ), |
| 59 |
$item['name'] |
| 60 |
); |
| 61 |
} else { |
| 62 |
$reason = sprintf( |
| 63 |
__( '%d %s awarded for purchase %d %s', 'gamipress' ), |
| 64 |
$points, |
| 65 |
gamipress_get_points_type_plural( $points_type ), |
| 66 |
$quantity, |
| 67 |
$item['name'] |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
// Setup the points award args |
| 72 |
$args = array( |
| 73 |
'reason' => $reason, |
| 74 |
'log_type' => 'points_earn', |
| 75 |
); |
| 76 |
|
| 77 |
// Award the points to the user |
| 78 |
gamipress_award_points_to_user( $user_id, $points, $points_type, $args ); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
} |
| 85 |
add_action( 'edd_complete_purchase', 'gamipress_edd_maybe_award_points_on_purchase' ); |
| 86 |
|
| 87 |
// Helper function to get download variations dropdown |
| 88 |
function gamipress_edd_get_download_variations_dropdown( $download_id, $selected = 0, $site_id = 0 ) { |
| 89 |
|
| 90 |
if( gamipress_is_network_wide_active() && $site_id !== get_current_blog_id() ) { |
| 91 |
switch_to_blog( $site_id ); |
| 92 |
$variations = edd_get_variable_prices( $download_id ); |
| 93 |
restore_current_blog(); |
| 94 |
} else { |
| 95 |
$variations = edd_get_variable_prices( $download_id ); |
| 96 |
} |
| 97 |
|
| 98 |
if( empty( $variations ) ) { |
| 99 |
return ''; |
| 100 |
} |
| 101 |
|
| 102 |
$output = '<select>'; |
| 103 |
|
| 104 |
foreach( $variations as $index => $variation ) { |
| 105 |
$output .= '<option value="' . $index . '" ' . selected( $selected, absint( $index ), false ) . '>' . $variation['name'] . ' (' . edd_currency_filter( edd_format_amount( $variation['amount'] ) ) . ')</option>'; |
| 106 |
} |
| 107 |
|
| 108 |
$output .= '</select>'; |
| 109 |
|
| 110 |
return $output; |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
// Helper function to get download variation title |
| 115 |
function gamipress_edd_get_download_variation_title( $download_id, $variation_id, $site_id ) { |
| 116 |
|
| 117 |
if( gamipress_is_network_wide_active() && $site_id !== get_current_blog_id() ) { |
| 118 |
switch_to_blog( $site_id ); |
| 119 |
$variations = edd_get_variable_prices( $download_id ); |
| 120 |
restore_current_blog(); |
| 121 |
} else { |
| 122 |
$variations = edd_get_variable_prices( $download_id ); |
| 123 |
} |
| 124 |
|
| 125 |
$selected_variation = false; |
| 126 |
|
| 127 |
foreach( $variations as $index => $variation ) { |
| 128 |
if( absint( $index ) === absint( $variation_id ) ) { |
| 129 |
$selected_variation = $variation; |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
return get_post_field( 'post_title', $download_id ) |
| 135 |
. ( $selected_variation ? ' - ' . $selected_variation['name'] : '' ); |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Retrieves download term ids for a taxonomy. |
| 141 |
* |
| 142 |
* Taken from WooCommerce wc_get_product_term_ids() function |
| 143 |
* |
| 144 |
* @since 1.1.2 |
| 145 |
* |
| 146 |
* @param int $download_id Download ID. |
| 147 |
* @param string $taxonomy Taxonomy slug. |
| 148 |
* |
| 149 |
* @return array |
| 150 |
*/ |
| 151 |
function gamipress_edd_get_download_term_ids( $download_id, $taxonomy ) { |
| 152 |
$terms = get_the_terms( $download_id, $taxonomy ); |
| 153 |
return ( empty( $terms ) || is_wp_error( $terms ) ) ? array() : wp_list_pluck( $terms, 'term_id' ); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Get lifetime conditions |
| 158 |
* |
| 159 |
* @since 1.0.0 |
| 160 |
* |
| 161 |
* @return array |
| 162 |
*/ |
| 163 |
function gamipress_edd_get_lifetime_conditions() { |
| 164 |
|
| 165 |
return array( |
| 166 |
'equal' => __( 'equal to', 'gamipress'), |
| 167 |
'not_equal' => __( 'not equal to', 'gamipress'), |
| 168 |
'less_than' => __( 'less than', 'gamipress' ), |
| 169 |
'greater_than' => __( 'greater than', 'gamipress' ), |
| 170 |
'less_or_equal' => __( 'less or equal to', 'gamipress' ), |
| 171 |
'greater_or_equal' => __( 'greater or equal to', 'gamipress' ), |
| 172 |
); |
| 173 |
|
| 174 |
} |