/**
  * Adjust Column Body
  * @since 1.4
  * @version 1.1.1
  */
 public function adjust_column_content($column_name, $post_id)
 {
     global $mycred;
     switch ($column_name) {
         case 'value':
             $value = mycred_get_coupon_value($post_id);
             if (empty($value)) {
                 $value = 0;
             }
             echo $mycred->format_creds($value);
             break;
         case 'usage':
             $count = mycred_get_global_coupon_count($post_id);
             if (empty($count)) {
                 _e('not yet used', 'mycred');
             } else {
                 $set_type = get_post_meta($post_id, 'type', true);
                 $page = 'myCRED';
                 if ($set_type != 'mycred_default' && array_key_exists($set_type, $this->point_types)) {
                     $page .= '_' . $set_type;
                 }
                 $url = add_query_arg(array('page' => $page, 'ref' => 'coupon', 'data' => get_the_title($post_id)), admin_url('admin.php'));
                 echo '<a href="' . esc_url($url) . '">' . sprintf(__('1 time', '%d times', $count, 'mycred'), $count) . '</a>';
             }
             break;
         case 'limits':
             $total = mycred_get_coupon_global_max($post_id);
             $user = mycred_get_coupon_user_max($post_id);
             printf('%1$s: %2$d<br />%3$s: %4$d', __('Total', 'mycred'), $total, __('Per User', 'mycred'), $user);
             break;
         case 'expires':
             $expires = mycred_get_coupon_expire_date($post_id, true);
             if (empty($expires) || $expires === 0) {
                 _e('Never', 'mycred');
             } else {
                 if ($expires < date_i18n('U')) {
                     wp_trash_post($post_id);
                     echo '<span style="color:red;">' . __('Expired', 'mycred') . '</span>';
                 } else {
                     echo sprintf(__('In %s time', 'mycred'), human_time_diff($expires)) . '<br /><small class="description">' . date_i18n(get_option('date_format'), $expires) . '</small>';
                 }
             }
             break;
         case 'ctype':
             $type = get_post_meta($post_id, 'type', true);
             if (isset($this->point_types[$type])) {
                 echo $this->point_types[$type];
             } else {
                 echo '-';
             }
             break;
     }
 }
 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);
 }
示例#3
0
 /**
  * Adjust Column Body
  * @since 1.4
  * @version 1.0
  */
 public function adjust_column_content($column_name, $post_id)
 {
     global $mycred;
     switch ($column_name) {
         case 'value':
             $value = mycred_get_coupon_value($post_id);
             if (empty($value)) {
                 $value = 0;
             }
             echo $mycred->format_creds($value);
             break;
         case 'usage':
             $count = mycred_get_global_coupon_count($post_id);
             if (empty($count)) {
                 _e('not yet used', 'mycred');
             } else {
                 echo $count;
             }
             break;
         case 'limits':
             $total = mycred_get_coupon_global_max($post_id);
             $user = mycred_get_coupon_user_max($post_id);
             printf('%1$s: %2$d<br />%3$s: %4$d', __('Total', 'mycred'), $total, __('Per User', 'mycred'), $user);
             break;
         case 'expires':
             $expires = mycred_get_coupon_expire_date($post_id, true);
             if (empty($expires) || $expires === 0) {
                 _e('Never', 'mycred');
             } else {
                 if ($expires < date_i18n('U')) {
                     wp_trash_post($post_id);
                     echo '<span style="color:red;">' . __('Expired', 'mycred') . '</span>';
                 } else {
                     echo sprintf(__('In %s time', 'mycred'), human_time_diff($expires)) . '<br /><small class="description">' . date_i18n(get_option('date_format'), $expires) . '</small>';
                 }
             }
             break;
     }
 }