Esempio n. 1
0
    function mycred_render_shortcode_load_coupon($atts, $content = NULL)
    {
        if (!is_user_logged_in()) {
            return $content;
        }
        $mycred = mycred();
        if (!isset($mycred->coupons)) {
            return '<p><strong>Coupon Add-on settings are missing! Please visit the myCRED > Settings page to save your settings before using this shortcode.</strong></p>';
        }
        // Prep
        $output = '
<div class="mycred-coupon-form">';
        $user_id = get_current_user_id();
        // No show for excluded users
        if ($mycred->exclude_user($user_id)) {
            return '';
        }
        // On submits
        if (isset($_POST['mycred_coupon_load']['token']) && wp_verify_nonce($_POST['mycred_coupon_load']['token'], 'mycred-load-coupon' . $user_id)) {
            $coupon = mycred_get_coupon_post($_POST['mycred_coupon_load']['couponkey']);
            $load = mycred_use_coupon($_POST['mycred_coupon_load']['couponkey'], $user_id);
            // Coupon does not exist
            if ($load === 'missing') {
                $output .= '<p class="mycred-coupon-status">' . $mycred->coupons['invalid'] . '</p>';
            } elseif ($load === 'expired') {
                $output .= '<p class="mycred-coupon-status">' . $mycred->coupons['expired'] . '</p>';
            } elseif ($load === 'max') {
                $output .= '<p class="mycred-coupon-status">' . $mycred->coupons['user_limit'] . '</p>';
            } elseif ($load === 'min_balance') {
                $min = get_post_meta($coupon->ID, 'min', true);
                $template = str_replace('%min%', $min, $mycred->coupons['min']);
                $output .= '<p class="mycred-coupon-status">' . $template . '</p>';
            } elseif ($load === 'max_balance') {
                $max = get_post_meta($coupon->ID, 'max', true);
                $template = str_replace('%max%', $max, $mycred->coupons['max']);
                $output .= '<p class="mycred-coupon-status">' . $template . '</p>';
            } else {
                $output .= '<p class="mycred-coupon-status">' . $mycred->coupons['success'] . '</p>';
            }
        }
        $output .= '
	<form action="" method="post">
		<p>
			<label for="mycred-coupon-code">' . __('Coupon', 'mycred') . '</label><br />
			<input type="text" name="mycred_coupon_load[couponkey]" id="mycred-coupon-couponkey" value="" /> 
			<input type="hidden" name="mycred_coupon_load[token]" value="' . wp_create_nonce('mycred-load-coupon' . $user_id) . '" />
			<input type="submit" class="btn btn-primary btn-large button button-large button-primary" value="' . __('Apply Coupon', 'mycred') . '" />
		</p>
	</form>
</div>';
        return apply_filters('mycred_load_coupon', $output, $atts, $content);
    }
 function mycred_use_coupon($code = '', $user_id = 0)
 {
     // Missing required information
     if (empty($code) || $user_id === 0) {
         return 'missing';
     }
     // Get coupon by code (post title)
     $coupon = mycred_get_coupon_post($code);
     // Coupon does not exist
     if ($coupon === NULL) {
         return 'missing';
     }
     // Check Expiration
     $now = current_time('timestamp');
     $expires = mycred_get_coupon_expire_date($coupon->ID, true);
     if (!empty($expires) && $expires !== 0 && $expires <= $now) {
         wp_trash_post($coupon->ID);
         return 'expired';
     }
     // Get Global Count
     $global_count = mycred_get_global_coupon_count($coupon->ID);
     // We start with enforcing the global count
     $global_max = mycred_get_coupon_global_max($coupon->ID);
     if ($global_count >= $global_max) {
         wp_trash_post($coupon->ID);
         return 'expired';
     }
     $type = get_post_meta($coupon->ID, 'type', true);
     if ($type == '') {
         $type = 'mycred_default';
     }
     $mycred = mycred($type);
     // Get User max
     $user_count = mycred_get_users_coupon_count($code, $user_id);
     // Next we enforce the user max
     $user_max = mycred_get_coupon_user_max($coupon->ID);
     if ($user_count >= $user_max) {
         return 'max';
     }
     // Min balance requirement
     $users_balance = $mycred->get_users_cred($user_id, $type);
     $min_balance = mycred_get_coupon_min_balance($coupon->ID);
     if ($min_balance > $mycred->zero() && $users_balance < $min_balance) {
         return 'min_balance';
     }
     // Max balance requirement
     $max_balance = mycred_get_coupon_max_balance($coupon->ID);
     if ($max_balance > $mycred->zero() && $users_balance >= $max_balance) {
         return 'max_balance';
     }
     // Ready to use coupon!
     $value = mycred_get_coupon_value($coupon->ID);
     $value = $mycred->number($value);
     // Get Coupon log template
     if (!isset($mycred->core['coupons']['log'])) {
         $mycred->core['coupons']['log'] = 'Coupon redemption';
     }
     // Apply Coupon
     $mycred->add_creds('coupon', $user_id, $value, $mycred->core['coupons']['log'], $coupon->ID, $code, $type);
     do_action('mycred_use_coupon', $user_id, $coupon);
     // Increment global counter
     $global_count++;
     update_post_meta($coupon->ID, 'global_count', $global_count);
     // If the updated counter reaches the max, trash the coupon now
     if ($global_count >= $global_max) {
         wp_trash_post($coupon->ID);
     }
     return $mycred->number($users_balance + $value);
 }