/**
  * Save Override
  * @since 1.5
  * @version 1.1.1
  */
 public function save_user_override()
 {
     if (isset($_POST['mycred_adjust_users_buyrates_run']) && isset($_POST['mycred_adjust_users_buyrates'])) {
         $ctype = sanitize_key($_GET['ctype']);
         $user_id = absint($_GET['user_id']);
         $mycred = mycred($ctype);
         if ($mycred->edit_plugin_cap() && !$mycred->exclude_user($user_id)) {
             $new_rates = array();
             foreach ((array) $_POST['mycred_adjust_users_buyrates'] as $gateway_id => $rate) {
                 if ($rate == '') {
                     continue;
                 }
                 if ($rate != 1 && in_array(substr($rate, 0, 1), array('.', ','))) {
                     $rate = (double) '0' . $rate;
                 }
                 $new_rates[$gateway_id] = $rate;
             }
             if (!empty($new_rates)) {
                 mycred_update_user_meta($user_id, 'mycred_buycred_rates_' . $ctype, '', $new_rates);
             } else {
                 mycred_delete_user_meta($user_id, 'mycred_buycred_rates_' . $ctype);
             }
             wp_safe_redirect(add_query_arg(array('result' => 'buycred_rates')));
             exit;
         }
     }
 }
 function mycred_check_if_user_gets_badge($user_id = NULL, $badge_ids = array(), $save = true)
 {
     if ($user_id === NULL || empty($badge_ids)) {
         return;
     }
     global $wpdb;
     $ids = array();
     foreach ($badge_ids as $badge_id) {
         $level = false;
         $requirements = mycred_get_badge_requirements($badge_id);
         foreach ($requirements as $req_level => $needs) {
             if ($needs['type'] == '') {
                 $needs['type'] = 'mycred_default';
             }
             $mycred = mycred($needs['type']);
             // Count occurences
             if ($needs['by'] == 'count') {
                 $select = 'COUNT( * )';
                 $amount = absint($needs['amount']);
             } else {
                 $select = 'SUM( creds )';
                 $amount = $mycred->number($needs['amount']);
             }
             $result = $wpdb->get_var(apply_filters('mycred_if_user_gets_badge_sql', $wpdb->prepare("\n\t\t\t\t\tSELECT {$select} \n\t\t\t\t\tFROM {$mycred->log_table} \n\t\t\t\t\tWHERE user_id = %d \n\t\t\t\t\t\tAND ctype = %s \n\t\t\t\t\t\tAND ref = %s;", $user_id, $needs['type'], $needs['reference']), $user_id, $badge_id, $req_level, $needs));
             if ($result === NULL) {
                 $result = 0;
             }
             if ($needs['by'] != 'count') {
                 $result = $mycred->number($result);
             } else {
                 $result = absint($result);
             }
             $level = NULL;
             if ($result >= $amount) {
                 $level = absint($req_level);
             }
             $current = mycred_get_user_meta($user_id, 'mycred_badge' . $badge_id, '', true);
             if ($current == '') {
                 $current = -1;
             }
             // If a level has been reached assign it now unless the user has this level already
             if ($level !== NULL && $current < $level) {
                 if ($save) {
                     mycred_update_user_meta($user_id, 'mycred_badge' . $badge_id, '', apply_filters('mycred_badge_user_value', $level, $user_id, $badge_id));
                 }
                 $ids[$badge_id] = $level;
             }
         }
     }
     return $ids;
 }
 function mycred_find_users_rank($user_id = NULL, $save = false, $type = 'mycred_default')
 {
     global $wpdb;
     $mycred = mycred($type);
     // In case user id is not set
     if ($user_id === NULL) {
         $user_id = get_current_user_id();
     }
     // Get current balanace
     $current_balance = $wpdb->get_var($wpdb->prepare("\n\t\t\tSELECT meta_value \n\t\t\tFROM {$wpdb->usermeta} \n\t\t\tWHERE user_id = %d \n\t\t\tAND meta_key = %s;", $user_id, $type));
     if ($current_balance === NULL) {
         $current_balance = 0;
     }
     // If ranks are based on total we get the total balance which in turn
     // if not set will default to the users current balance.
     if (mycred_rank_based_on_total($type)) {
         $balance = mycred_query_users_total($user_id, $type);
         if ($balance == 0) {
             $balance = $current_balance;
         }
     } else {
         $balance = $current_balance;
     }
     // Prep format for the db query
     $balance_format = '%d';
     if (isset($mycred->format['decimals']) && $mycred->format['decimals'] > 0) {
         $balance_format = 'CAST( %f AS DECIMAL( 10, ' . $mycred->format['decimals'] . ' ) )';
     }
     // Get the appropriate post tables
     if (!mycred_override_settings()) {
         $posts = $wpdb->posts;
         $postmeta = $wpdb->postmeta;
     } else {
         $posts = $wpdb->base_prefix . 'posts';
         $postmeta = $wpdb->base_prefix . 'postmeta';
     }
     $type_filter = $wpdb->prepare("\n\t\t\tINNER JOIN {$postmeta} ctype \n\t\t\t\tON ( ranks.ID = ctype.post_id AND ctype.meta_key = %s AND ctype.meta_value = %s )", 'ctype', $type);
     // Get the rank based on balance
     $rank_id = $wpdb->get_var($wpdb->prepare("\n\t\t\tSELECT ranks.ID \n\t\t\tFROM {$posts} ranks \n\t\t\t{$type_filter}\n\t\t\tINNER JOIN {$postmeta} min \n\t\t\t\tON ( ranks.ID = min.post_id AND min.meta_key = 'mycred_rank_min' )\n\t\t\tINNER JOIN {$postmeta} max \n\t\t\t\tON ( ranks.ID = max.post_id AND max.meta_key = 'mycred_rank_max' )\n\t\t\tWHERE ranks.post_type = 'mycred_rank' \n\t\t\t\tAND ranks.post_status = 'publish'\n\t\t\t\tAND {$balance_format} BETWEEN min.meta_value AND max.meta_value\n\t\t\tLIMIT 0,1;", $balance));
     // Let others play
     if ($rank_id !== NULL) {
         if (mycred_user_got_demoted($user_id, $rank_id)) {
             do_action('mycred_user_got_demoted', $user_id, $rank_id);
         } elseif (mycred_user_got_promoted($user_id, $rank_id)) {
             do_action('mycred_user_got_promoted', $user_id, $rank_id);
         }
     }
     $end = '';
     if ($type != 'mycred_default') {
         $end = $type;
     }
     // Save if requested
     if ($save && $rank_id !== NULL) {
         mycred_update_user_meta($user_id, 'mycred_rank', $end, $rank_id);
     }
     return apply_filters('mycred_find_users_rank', $rank_id, $user_id, $save, $type);
 }
Beispiel #4
0
 /**
  * AJAX: Assign Badge
  * @version 1.0
  */
 public function action_assign_badge()
 {
     check_ajax_referer('mycred-assign-badge', 'token');
     $badge_id = absint($_POST['badge_id']);
     $requirements = mycred_get_badge_requirements($badge_id);
     if (empty($requirements)) {
         wp_send_json_error('This badge has no requirements set!');
     }
     $needs = $requirements[0];
     $mycred = mycred($needs['type']);
     $mycred_log = $mycred->log_table;
     global $wpdb;
     $sql = "\n\t\t\tSELECT user_id \n\t\t\tFROM {$mycred_log} \n\t\t\tWHERE " . $wpdb->prepare("ctype = %s AND ref = %s ", $needs['type'], $needs['reference']);
     $sql .= " GROUP by user_id ";
     $amount = $needs['amount'];
     if ($needs['by'] == 'count') {
         $sql .= "HAVING COUNT( id ) >= {$amount}";
     } else {
         $sql .= "HAVING SUM( creds ) >= {$amount}";
     }
     // Let others play
     $users = $wpdb->get_col(apply_filters('mycred_assign_badge_sql', $sql, $badge_id));
     // Empty results = no one has earned this badge yet
     if (empty($users)) {
         wp_send_json_error(__('No users has yet earned this badge.', 'mycred'));
     }
     // Assign badge
     foreach ($users as $user_id) {
         mycred_update_user_meta($user_id, 'mycred_badge' . $badge_id, '', apply_filters('mycred_badge_user_value', 1, $user_id, $badge_id));
     }
     wp_send_json_success(sprintf(__('%d Users earned this badge.', 'mycred'), count($users)));
 }
 function mycred_check_if_user_gets_badge($user_id = NULL, $request = array(), $badge_ids = array())
 {
     if ($user_id === NULL || empty($badge_ids)) {
         return;
     }
     global $wpdb;
     foreach ($badge_ids as $badge_id) {
         // See if user already has badge
         if (mycred_get_user_meta($user_id, 'mycred_badge' . $badge_id, '', true) != '') {
             continue;
         }
         $requirements = mycred_get_badge_requirements($badge_id);
         $needs = $requirements[0];
         $mycred = mycred($needs['type']);
         $mycred_log = $mycred->log_table;
         if ($needs['by'] == 'count') {
             $select = 'COUNT( * )';
             $amount = $needs['amount'];
         } else {
             $select = 'SUM( creds )';
             $amount = $mycred->number($needs['amount']);
         }
         $result = $wpdb->get_var($wpdb->prepare("\n\t\t\tSELECT {$select} \n\t\t\tFROM {$mycred_log} \n\t\t\tWHERE user_id = %d \n\t\t\t\tAND ctype = %s \n\t\t\t\tAND ref = %s;", $user_id, $needs['type'], $needs['reference']));
         // If this function is used by the mycred_add filter, we need to take into
         // account the instance that we are currently being hooked into as the log entry
         // will be added after this code has executed.
         // In case we sum up, add the points the user will gain to the result
         if (!isset($request['done']) && $needs['by'] == 'sum') {
             $result = $result + $request['amount'];
         } elseif (!isset($request['done']) && $needs['by'] == 'count') {
             $result = $result + 1;
         }
         if ($needs['by'] != 'count') {
             $result = $mycred->number($result);
         }
         // Got it!
         if ($result >= $amount) {
             mycred_update_user_meta($user_id, 'mycred_badge' . $badge_id, '', apply_filters('mycred_badge_user_value', 1, $user_id, $badge_id));
         }
     }
 }
 /**
  * Accepting Invites
  * @since 0.1
  * @version 1.2
  */
 public function accept_invite($invited_user_id, $inviters = array())
 {
     if (empty($inviters)) {
         return;
     }
     // Invite Anyone will pass on an array of user IDs of those who have invited this user which we need to loop though
     foreach ((array) $inviters as $inviter_id) {
         // Limit Check
         if ($this->prefs['accept_invite']['limit'] != 0) {
             $key = 'mycred_invite_anyone';
             if (!$this->is_main_type) {
                 $key .= '_' . $this->mycred_type;
             }
             $user_log = mycred_get_user_meta($inviter_id, $key, '', true);
             if (empty($user_log['accepted'])) {
                 $user_log['accepted'] = 0;
             }
             // Continue to next inviter if limit is reached
             if ($user_log['accepted'] >= $this->prefs['accept_invite']['limit']) {
                 continue;
             }
         }
         // Award Points
         $run = true;
         if (function_exists('buddypress') && apply_filters('bp_core_signup_send_activation_key', true)) {
             $run = false;
             // Get pending list
             $pending = get_transient('mycred-pending-bp-signups');
             if ($pending === false) {
                 $pending = array();
             }
             // Add to pending list if not there already
             if (!isset($pending[$invited_user_id])) {
                 $pending[$invited_user_id] = $inviter_id;
                 delete_transient('mycred-pending-bp-signups');
                 set_transient('mycred-pending-bp-signups', $pending, 7 * DAY_IN_SECONDS);
             }
         }
         if ($run) {
             $this->core->add_creds('accepting_an_invite', $inviter_id, $this->prefs['accept_invite']['creds'], $this->prefs['accept_invite']['log'], $invited_user_id, array('ref_type' => 'user'), $this->mycred_type);
         }
         // Update Limit
         if ($this->prefs['accept_invite']['limit'] != 0) {
             $user_log['accepted'] = $user_log['accepted'] + 1;
             mycred_update_user_meta($inviter_id, $key, '', $user_log);
         }
     }
 }
Beispiel #7
0
 /**
  * Update users balance
  * Returns the updated balance of the given user.
  *
  * @param $user_id (int), required user id
  * @param $amount (int|float), amount to add/deduct from users balance. This value must be pre-formated.
  * @returns the new balance.
  * @since 0.1
  * @version 1.4
  */
 public function update_users_balance($user_id = NULL, $amount = NULL, $type = 'mycred_default')
 {
     // Minimum Requirements: User id and amount can not be null
     if ($user_id === NULL || $amount === NULL) {
         return $amount;
     }
     if (empty($type)) {
         $type = $this->get_cred_id();
     }
     // Enforce max
     if ($this->max() > $this->zero() && $amount > $this->max()) {
         $amount = $this->number($this->max());
         do_action('mycred_max_enforced', $user_id, $_amount, $this->max());
     }
     // Adjust creds
     $current_balance = $this->get_users_balance($user_id, $type);
     $new_balance = $current_balance + $amount;
     // Update creds
     mycred_update_user_meta($user_id, $type, '', $new_balance);
     // Update total creds
     $total = mycred_query_users_total($user_id, $type);
     mycred_update_user_meta($user_id, $type, '_total', $total);
     // Let others play
     do_action('mycred_update_user_balance', $user_id, $current_balance, $amount, $type);
     // Return the new balance
     return $this->number($new_balance);
 }
 /**
  * Save User Override
  * @since 1.5.2
  * @version 1.0.1
  */
 function save_user_override()
 {
     // Save interest rate
     if (isset($_POST['mycred_adjust_users_interest_rate_run']) && isset($_POST['mycred_adjust_users_interest_rate'])) {
         $ctype = sanitize_key($_GET['ctype']);
         $user_id = absint($_GET['user_id']);
         $rate = $_POST['mycred_adjust_users_interest_rate'];
         if ($rate != '') {
             if (isfloat($rate)) {
                 $rate = (double) $rate;
             } else {
                 $rate = (int) $rate;
             }
             mycred_update_user_meta($user_id, 'mycred_banking_rate_' . $ctype, '', $rate);
         } else {
             mycred_delete_user_meta($user_id, 'mycred_banking_rate_' . $ctype);
         }
         wp_safe_redirect(add_query_arg(array('result' => 'banking_interest_rate')));
         exit;
     } elseif (isset($_POST['mycred_exclude_users_interest_rate'])) {
         $ctype = sanitize_key($_GET['ctype']);
         $user_id = absint($_GET['user_id']);
         $excluded = explode(',', $this->prefs['exclude_ids']);
         $clean_ids = array();
         if (!empty($excluded)) {
             foreach ($excluded as $id) {
                 if ($id == 0) {
                     continue;
                 }
                 $clean_ids[] = (int) trim($id);
             }
         }
         if (!in_array($user_id, $clean_ids) && $user_id != 0) {
             $clean_ids[] = $user_id;
         }
         $option_id = 'mycred_pref_bank';
         if (!$this->is_main_type) {
             $option_id .= '_' . $ctype;
         }
         $data = mycred_get_option($option_id);
         $data['service_prefs'][$this->id]['exclude_ids'] = implode(',', $clean_ids);
         mycred_update_option($option_id, $data);
         wp_safe_redirect(add_query_arg(array('result' => 'banking_interest_excluded')));
         exit;
     } elseif (isset($_POST['mycred_include_users_interest_rate'])) {
         $ctype = sanitize_key($_GET['ctype']);
         $user_id = absint($_GET['user_id']);
         $excluded = explode(',', $this->prefs['exclude_ids']);
         if (!empty($excluded)) {
             $clean_ids = array();
             foreach ($excluded as $id) {
                 $clean_id = (int) trim($id);
                 if ($clean_id != $user_id && $user_id != 0) {
                     $clean_ids[] = $clean_id;
                 }
             }
             $option_id = 'mycred_pref_bank';
             if (!$this->is_main_type) {
                 $option_id .= '_' . $ctype;
             }
             $data = mycred_get_option($option_id);
             $data['service_prefs'][$this->id]['exclude_ids'] = implode(',', $clean_ids);
             mycred_update_option($option_id, $data);
             wp_safe_redirect(add_query_arg(array('result' => 'banking_interest_included')));
             exit;
         }
     }
 }
 /**
  * AJAX: Assign Badge
  * @version 1.1.1
  */
 public function action_assign_badge()
 {
     check_ajax_referer('mycred-assign-badge', 'token');
     $badge_id = absint($_POST['badge_id']);
     $requirements = mycred_get_badge_requirements($badge_id);
     if (empty($requirements)) {
         wp_send_json_error('This badge has no requirements set!');
     }
     global $wpdb;
     $levels = array();
     foreach ($requirements as $req_level => $needs) {
         if ($needs['type'] == '') {
             $needs['type'] = 'mycred_default';
         }
         $mycred = mycred($needs['type']);
         if (!array_key_exists($req_level, $levels)) {
             $levels[$req_level] = array();
         }
         $sql = "\n\t\t\t\t\tSELECT user_id \n\t\t\t\t\tFROM {$mycred->log_table} \n\t\t\t\t\tWHERE " . $wpdb->prepare("ctype = %s AND ref = %s ", $needs['type'], $needs['reference']);
         $sql .= " GROUP by user_id ";
         $amount = $needs['amount'];
         if ($needs['by'] == 'count') {
             $sql .= "HAVING COUNT( id ) >= {$amount}";
         } else {
             $sql .= "HAVING SUM( creds ) >= {$amount}";
         }
         // Let others play
         $users = $wpdb->get_col(apply_filters('mycred_assign_badge_sql', $sql, $badge_id));
         if (!empty($users)) {
             $levels[$req_level] = $users;
             $unique = array();
             foreach ($levels[$req_level] as $user_id) {
                 if ($req_level == 0) {
                     $unique[] = (int) $user_id;
                 } elseif (isset($levels[$req_level - 1]) && in_array($user_id, $levels[$req_level - 1])) {
                     $unique[] = (int) $user_id;
                     $prev_key = array_search($user_id, $levels[$req_level - 1]);
                     if ($prev_key !== false) {
                         unset($levels[$req_level - 1][$prev_key]);
                     }
                 }
             }
             $levels[$req_level] = $unique;
         }
     }
     if (!empty($levels)) {
         $count = 0;
         foreach ($levels as $level => $user_ids) {
             if (empty($user_ids)) {
                 continue;
             }
             foreach ($user_ids as $user_id) {
                 mycred_update_user_meta($user_id, 'mycred_badge' . $badge_id, '', apply_filters('mycred_badge_user_value', $level, $user_id, $badge_id));
                 $count++;
             }
         }
         if ($count > 0) {
             wp_send_json_success(sprintf(__('%d Users earned this badge.', 'mycred'), $count));
         }
     }
     wp_send_json_error(__('No users has yet earned this badge.', 'mycred'));
 }
Beispiel #10
0
 /**
  * Balance Adjustment
  * Check if users rank should change.
  * @since 1.1
  * @version 1.3.1
  */
 public function update_balance($user_id, $current_balance, $amount, $type)
 {
     if ($type != 'mycred_default') {
         return;
     }
     if ($this->rank['base'] == 'total') {
         $total = mycred_get_users_total($user_id);
         $balance = $this->core->number($total + $amount);
         mycred_update_users_total($type, compact('user_id', 'amount'), $this->core);
     } else {
         $balance = $this->core->number($current_balance + $amount);
     }
     $balance_format = '%d';
     if (isset($this->core->format['decimals']) && $this->core->format['decimals'] > 0) {
         $balance_format = 'CAST( %f AS DECIMAL( 10, ' . $this->core->format['decimals'] . ' ) )';
     }
     global $wpdb;
     $rank_id = $wpdb->get_var($wpdb->prepare("\n\t\t\tSELECT rank.ID \n\t\t\tFROM {$wpdb->posts} rank \n\t\t\tINNER JOIN {$wpdb->postmeta} min \n\t\t\t\tON ( min.post_id = rank.ID AND min.meta_key = 'mycred_rank_min' )\n\t\t\tINNER JOIN {$wpdb->postmeta} max \n\t\t\t\tON ( max.post_id = rank.ID AND max.meta_key = 'mycred_rank_max' )\n\t\t\tWHERE rank.post_type = 'mycred_rank' \n\t\t\t\tAND rank.post_status = 'publish'\n\t\t\t\tAND {$balance_format} BETWEEN min.meta_value AND max.meta_value\n\t\t\tLIMIT 0,1;", $balance));
     if ($rank_id !== NULL) {
         if (mycred_user_got_demoted($user_id, $rank_id)) {
             do_action('mycred_user_got_demoted', $user_id, $rank_id);
         } elseif (mycred_user_got_promoted($user_id, $rank_id)) {
             do_action('mycred_user_got_promoted', $user_id, $rank_id);
         } else {
             do_action('mycred_find_users_rank', $user_id, $rank_id);
         }
         $rank_meta_key = 'mycred_rank';
         if (is_multisite() && !mycred_override_settings()) {
             $rank_meta_key .= $GLOBALS['blog_id'];
         }
         mycred_update_user_meta($user_id, 'mycred_rank', '', $rank_id);
     }
 }
Beispiel #11
0
        /**
         * import function.
         */
        function import($file)
        {
            global $wpdb, $mycred;
            $this->imported = $this->skipped = 0;
            if (!is_file($file)) {
                echo '<p><strong>' . __('Sorry, there has been an error.', 'mycred') . '</strong><br />';
                echo __('The file does not exist, please try again.', 'mycred') . '</p>';
                $this->footer();
                die;
            }
            ini_set('auto_detect_line_endings', '1');
            if (($handle = fopen($file, "r")) !== FALSE) {
                $header = fgetcsv($handle, 0, $this->delimiter);
                $no_of_columns = sizeof($header);
                if ($no_of_columns == 3 || $no_of_columns == 4) {
                    $loop = 0;
                    $mycred_types = mycred_get_types();
                    while (($row = fgetcsv($handle, 0, $this->delimiter)) !== FALSE) {
                        $log_entry = '';
                        if ($no_of_columns == 3) {
                            list($id, $balance, $point_type) = $row;
                        } else {
                            list($id, $balance, $point_type, $log_entry) = $row;
                        }
                        $user = false;
                        if (is_numeric($id)) {
                            $user = get_userdata($id);
                        }
                        if ($user === false) {
                            $user = get_user_by('email', $id);
                        }
                        if ($user === false) {
                            $user = get_user_by('login', $id);
                        }
                        if ($user === false) {
                            $this->skipped++;
                            continue;
                        }
                        if (!isset($mycred_types[$point_type])) {
                            if ($point_type != '') {
                                $log_entry = $point_type;
                            }
                        }
                        if ($point_type == '') {
                            $point_type = 'mycred_default';
                        }
                        $method = trim($_POST['method']);
                        if ($method == 'add') {
                            $current_balance = mycred_get_user_meta($user->ID, $point_type, '', true);
                            $balance = $current_balance + $balance;
                        }
                        mycred_update_user_meta($user->ID, $point_type, '', $balance);
                        if (!empty($log_entry)) {
                            $wpdb->insert($mycred->log_table, array('ref' => 'import', 'ref_id' => NULL, 'user_id' => $user->ID, 'creds' => $mycred->number($balance), 'ctype' => $point_type, 'time' => date_i18n('U'), 'entry' => sanitize_text_field($log_entry), 'data' => ''));
                        }
                        $loop++;
                        $this->imported++;
                    }
                } else {
                    echo '<p><strong>' . __('Sorry, there has been an error.', 'mycred') . '</strong><br />';
                    echo __('The CSV is invalid.', 'mycred') . '</p>';
                    $this->footer();
                    die;
                }
                fclose($handle);
            }
            // Show Result
            echo '<div class="updated settings-error below-h2"><p>
				' . sprintf(__('Import complete - A total of <strong>%d</strong> balances were successfully imported. <strong>%d</strong> was skipped.', 'mycred'), $this->imported, $this->skipped) . '
			</p></div>';
            $this->import_end();
        }
Beispiel #12
0
 /**
  * Update Daily Limit
  * Updates a given users daily limit.
  * @since 1.3.3
  * @version 1.1
  */
 public function update_daily_limit($user_id, $id)
 {
     // No limit used
     if ($this->prefs[$id]['limit'] == 0) {
         return;
     }
     $today = date_i18n('Y-m-d');
     $key = 'mycred_simplepress_limits_' . $id;
     if (!$this->is_main_type) {
         $key .= '_' . $this->mycred_type;
     }
     $current = mycred_get_user_meta($user_id, $key, '', true);
     if (empty($current) || !array_key_exists($today, (array) $current)) {
         $current[$today] = 0;
     }
     $current[$today] = $current[$today] + 1;
     mycred_update_user_meta($user_id, $key, '', $current);
 }
 /**
  * Update users balance
  * Returns the updated balance of the given user.
  *
  * @param $user_id (int), required user id
  * @param $amount (int|float), amount to add/deduct from users balance. This value must be pre-formated.
  * @returns the new balance.
  * @since 0.1
  * @version 1.4.2
  */
 public function update_users_balance($user_id = NULL, $amount = NULL, $type = NULL)
 {
     // Minimum Requirements: User id and amount can not be null
     if ($user_id === NULL || $amount === NULL) {
         return $amount;
     }
     // Type
     $point_types = mycred_get_types();
     if ($type === NULL || !array_key_exists($type, $point_types)) {
         $type = $this->get_cred_id();
     }
     // Enforce max
     if ($this->max() > $this->zero() && $amount > $this->max()) {
         $_amount = $amount;
         $amount = $this->number($this->max());
         do_action('mycred_max_enforced', $user_id, $_amount, $this->max());
     }
     // Adjust creds
     $current_balance = $this->get_users_balance($user_id, $type);
     $new_balance = $current_balance + $amount;
     // Update creds
     mycred_update_user_meta($user_id, $type, '', $new_balance);
     // Update total creds
     $total = mycred_query_users_total($user_id, $type);
     mycred_update_user_meta($user_id, $type, '_total', $total);
     // Clear caches
     mycred_delete_option('mycred-cache-total-' . $type);
     // Let others play
     do_action('mycred_update_user_balance', $user_id, $current_balance, $amount, $type);
     // Return the new balance
     return $this->number($new_balance);
 }
Beispiel #14
0
 /**
  * Update Daily Limit
  * Updates a given users daily limit.
  * @since 1.2
  * @version 1.2
  */
 public function update_daily_limit($user_id, $limit, $remove = false)
 {
     // No limit used
     if ($this->prefs[$limit]['limit'] == 0) {
         return;
     }
     $today = date_i18n('Y-m-d');
     $current = $this->get_users_limit($user_id, $limit);
     if (empty($current) || !array_key_exists($today, $current)) {
         $current[$today] = 0;
     }
     if (!$remove) {
         $current[$today] = $current[$today] + 1;
     } else {
         $current[$today] = $current[$today] - 1;
     }
     $key = 'mycred_bbp_limits_' . $limit;
     if (!$this->is_main_type) {
         $key .= '_' . $this->mycred_type;
     }
     mycred_update_user_meta($user_id, $key, '', $current);
 }
        /**
         * Subscription Shortcode
         * @since 1.4.6
         * @version 1.0
         */
        public function render_subscription_shortcode($attr, $content = NULL)
        {
            extract(shortcode_atts(array('success' => __('Settings saved.', 'mycred')), $attr));
            if (!is_user_logged_in()) {
                return $content;
            }
            $user_id = get_current_user_id();
            $unsubscriptions = mycred_get_user_meta($user_id, 'mycred_email_unsubscriptions', '', true);
            if ($unsubscriptions == '') {
                $unsubscriptions = array();
            }
            // Save
            $saved = false;
            if (isset($_REQUEST['do']) && $_REQUEST['do'] == 'mycred-unsubscribe' && wp_verify_nonce($_REQUEST['token'], 'update-mycred-email-subscriptions')) {
                if (isset($_POST['mycred_email_unsubscribe']) && !empty($_POST['mycred_email_unsubscribe'])) {
                    $new_selection = $_POST['mycred_email_unsubscribe'];
                } else {
                    $new_selection = array();
                }
                mycred_update_user_meta($user_id, 'mycred_email_unsubscriptions', '', $new_selection);
                $unsubscriptions = $new_selection;
                $saved = true;
            }
            global $wpdb;
            $email_notices = $wpdb->get_results($wpdb->prepare("\n\t\t\t\tSELECT * \n\t\t\t\tFROM {$wpdb->posts} notices\n\n\t\t\t\tLEFT JOIN {$wpdb->postmeta} prefs \n\t\t\t\t\tON ( notices.ID = prefs.post_id AND prefs.meta_key = 'mycred_email_settings' )\n\n\t\t\t\tWHERE notices.post_type = 'mycred_email_notice' \n\t\t\t\t\tAND notices.post_status = 'publish'\n\t\t\t\t\tAND ( prefs.meta_value LIKE %s OR prefs.meta_value LIKE %s );", '%s:9:"recipient";s:4:"user";%', '%s:9:"recipient";s:4:"both";%'));
            ob_start();
            if ($saved) {
                echo '<p class="updated-email-subscriptions">' . $success . '</p>';
            }
            $url = add_query_arg(array('do' => 'mycred-unsubscribe', 'user' => get_current_user_id(), 'token' => wp_create_nonce('update-mycred-email-subscriptions')));
            ?>
<form action="<?php 
            echo esc_url($url);
            ?>
" id="mycred-email-subscriptions" method="post">
	<table class="table">
		<thead>
			<tr>
				<th class="check"><?php 
            _e('Unsubscribe', 'mycred');
            ?>
</th>
				<th class="notice-title"><?php 
            _e('Email Notice', 'mycred');
            ?>
</th>
			</tr>
		</thead>
		<tbody>

		<?php 
            if (!empty($email_notices)) {
                ?>
		
			<?php 
                foreach ($email_notices as $notice) {
                    $settings = $this->get_email_settings($notice->ID);
                    ?>

			<?php 
                    if ($settings['label'] == '') {
                        continue;
                    }
                    ?>

			<tr>
				<td class="check"><input type="checkbox" name="mycred_email_unsubscribe[]"<?php 
                    if (in_array($notice->ID, $unsubscriptions)) {
                        echo ' checked="checked"';
                    }
                    ?>
 value="<?php 
                    echo $notice->ID;
                    ?>
" /></td>
				<td class="notice-title"><?php 
                    echo $settings['label'];
                    ?>
</td>
			</tr>

			<?php 
                }
                ?>
		
		<?php 
            } else {
                ?>

			<tr>
				<td colspan="2"><?php 
                _e('There are no email notifications yet.', 'mycred');
                ?>
</td>
			</tr>

		<?php 
            }
            ?>

		</tbody>
	</table>
	<input type="submit" class="btn btn-primary button button-primary pull-right" value="<?php 
            _e('Save Changes', 'mycred');
            ?>
" />
</form>
<?php 
            $content = ob_get_contents();
            ob_end_clean();
            return apply_filters('mycred_render_email_subscriptions', $content, $attr);
        }
Beispiel #16
0
 /**
  * AJAX: Calculate Totals
  * @since 1.2
  * @version 1.3.1
  */
 public function calculate_totals()
 {
     // Security
     check_ajax_referer('mycred-calc-totals', 'token');
     $type = 'mycred_default';
     if (isset($_POST['ctype']) && array_key_exists($_POST['ctype'], $this->point_types)) {
         $type = sanitize_text_field($_POST['ctype']);
     }
     $balance_key = $type;
     $mycred = mycred($type);
     if ($mycred->is_multisite && $GLOBALS['blog_id'] > 1 && !$mycred->use_central_logging) {
         $balance_key .= '_' . $GLOBALS['blog_id'];
     }
     global $wpdb;
     // Get all users that have a balance. Excluded users will have no balance
     $users = $wpdb->get_col($wpdb->prepare("\n\t\t\t\tSELECT DISTINCT user_id \n\t\t\t\tFROM {$wpdb->usermeta} \n\t\t\t\tWHERE meta_key = %s", $balance_key));
     $count = 0;
     if (!empty($users)) {
         // Get the total for each user with a balance
         foreach ($users as $user_id) {
             $total = mycred_query_users_total($user_id, $type);
             mycred_update_user_meta($user_id, $type, '_total', $total);
             $count++;
         }
     }
     die(json_encode(sprintf(__('Completed - Total of %d users effected', 'mycred'), $count)));
 }
 /**
  * Save Log Entries per page
  * @since 0.1
  * @version 1.0
  */
 function set_entries_per_page()
 {
     if (!isset($_REQUEST['wp_screen_options']['option']) || !isset($_REQUEST['wp_screen_options']['value'])) {
         return;
     }
     $settings_key = 'mycred_epp_' . $_GET['page'];
     if (!$this->is_main_type) {
         $settings_key .= '_' . $this->mycred_type;
     }
     if ($_REQUEST['wp_screen_options']['option'] == $settings_key) {
         $value = absint($_REQUEST['wp_screen_options']['value']);
         mycred_update_user_meta(get_current_user_id(), $settings_key, '', $value);
     }
 }
 /**
  * Save Override
  * @since 1.5
  * @version 1.1
  */
 function save_user_override()
 {
     if (isset($_POST['mycred_adjust_users_profitshare_run']) && isset($_POST['mycred_adjust_users_profitshare'])) {
         $ctype = sanitize_key($_GET['ctype']);
         $user_id = absint($_GET['user_id']);
         $share = $_POST['mycred_adjust_users_profitshare']['share'];
         if ($share != '') {
             if (isfloat($share)) {
                 $share = (double) $share;
             } else {
                 $share = (int) $share;
             }
             mycred_update_user_meta($user_id, 'mycred_sell_content_share_' . $ctype, '', $share);
         } else {
             mycred_delete_user_meta($user_id, 'mycred_sell_content_share_' . $ctype);
         }
         wp_safe_redirect(add_query_arg(array('result' => 'sell_content_share')));
         exit;
     }
 }
 /**
  * Do Payout
  * Runs though all user compounded interest and pays.
  * @since 1.2
  * @version 1.2.1
  */
 public function do_interest_batch($batch)
 {
     if (!empty($batch) && is_array($batch)) {
         set_time_limit($this->prefs['run_time']);
         foreach ($batch as $user_id) {
             $user_id = intval($user_id);
             // Get past interest
             $past_interest = mycred_get_user_meta($user_id, $this->core->get_cred_id() . '_comp', '', true);
             if (empty($past_interest) || $past_interest == 0) {
                 continue;
             }
             // Pay / Charge
             $this->core->add_creds('payout', $user_id, $past_interest, $this->prefs['log']);
             // Reset past interest
             mycred_update_user_meta($user_id, $this->core->get_cred_id() . '_comp', '', 0);
         }
     }
 }
 function mycred_update_users_transfer_history($user_id, $history, $type = 'mycred_default', $key = NULL)
 {
     if ($key === NULL) {
         $key = 'mycred_transactions';
     }
     if ($type != 'mycred_default' && $type != '') {
         $key .= '_' . $type;
     }
     // Get current history
     $current = mycred_get_users_transfer_history($user_id, $type, $key);
     // Reset
     if ($history === true) {
         $new_history = array('frame' => '', 'amount' => 0);
     } else {
         $new_history = mycred_apply_defaults($current, $history);
     }
     mycred_update_user_meta($user_id, $key, '', $new_history);
 }
 /**
  * Get Ref ID
  * Returns a given users referral ID.
  * @since 1.4
  * @version 1.0
  */
 public function get_ref_id($user_id)
 {
     // Link format
     switch ($this->prefs['setup']['links']) {
         case 'username':
             $user = get_userdata($user_id);
             if ($user === false) {
                 $ref_id = 0;
             } else {
                 $ref_id = urlencode($user->user_login);
             }
             break;
         case 'numeric':
             $ref_id = mycred_get_user_meta($user_id, 'mycred_affiliate_link', '', true);
             if (empty($ref_id)) {
                 $counter = absint(get_option('mycred_affiliate_counter', 0));
                 $number = $counter + 1;
                 update_option('mycred_affiliate_counter', $number);
                 mycred_update_user_meta($user_id, 'mycred_affiliate_link', '', $number);
                 $ref_id = $number;
             }
             break;
     }
     return apply_filters('mycred_affiliate_get_ref_id', $ref_id, $user_id, $this);
 }
 function mycred_find_users_rank($user_id = NULL, $save = false, $amount = 0)
 {
     global $wpdb;
     $type = 'mycred_default';
     $mycred = mycred($type);
     // Check for exclusion
     if ($mycred->exclude_user($user_id)) {
         return false;
     }
     // In case user id is not set
     if ($user_id === NULL) {
         $user_id = get_current_user_id();
     }
     if (!isset($mycred->rank)) {
         return false;
     }
     // Get balance key
     $balance_key = $type;
     if ($mycred->is_multisite && $GLOBALS['blog_id'] > 1 && !$mycred->use_central_logging) {
         $balance_key .= '_' . $GLOBALS['blog_id'];
     }
     if (isset($mycred->rank['base']) && $mycred->rank['base'] == 'total') {
         $balance_key .= '_total';
     }
     // Get the balance as get_user_meta() will return the previous value if it was just changed.
     $balance = $wpdb->get_var($wpdb->prepare("\n\t\t\tSELECT meta_value \n\t\t\tFROM {$wpdb->usermeta} \n\t\t\tWHERE user_id = %d \n\t\t\t\tAND meta_key = %s;", $user_id, $balance_key));
     // User does not yet have a balance.
     if ($balance === NULL) {
         // If rank is based on total, we get their current balance
         if (isset($mycred->rank['base']) && $mycred->rank['base'] == 'total') {
             $balance = $mycred->get_users_balance($user_id, $type);
         } else {
             $balance = $mycred->zero();
         }
     }
     // The new balance before it is saved
     if ($amount < 0) {
         $balance = $mycred->number($balance - $amount);
     } else {
         $balance = $mycred->number($balance + $amount);
     }
     $balance_format = '%d';
     if (isset($mycred->format['decimals']) && $mycred->format['decimals'] > 0) {
         $balance_format = 'CAST( %f AS DECIMAL( 10, ' . $mycred->format['decimals'] . ' ) )';
     }
     if (!mycred_override_settings()) {
         $posts = $wpdb->posts;
         $postmeta = $wpdb->postmeta;
     } else {
         $posts = $wpdb->base_prefix . 'posts';
         $postmeta = $wpdb->base_prefix . 'postmeta';
     }
     $rank_id = $wpdb->get_var($wpdb->prepare("\n\t\t\tSELECT rank.ID \n\t\t\tFROM {$posts} rank \n\t\t\tINNER JOIN {$postmeta} min \n\t\t\t\tON ( min.post_id = rank.ID AND min.meta_key = 'mycred_rank_min' )\n\t\t\tINNER JOIN {$postmeta} max \n\t\t\t\tON ( max.post_id = rank.ID AND max.meta_key = 'mycred_rank_max' )\n\t\t\tWHERE rank.post_type = 'mycred_rank' \n\t\t\t\tAND rank.post_status = 'publish'\n\t\t\t\tAND {$balance_format} BETWEEN min.meta_value AND max.meta_value\n\t\t\tLIMIT 0,1;", $balance));
     // Let others play
     if ($rank_id !== NULL) {
         if (mycred_user_got_demoted($user_id, $rank_id)) {
             do_action('mycred_user_got_demoted', $user_id, $rank_id);
         } elseif (mycred_user_got_promoted($user_id, $rank_id)) {
             do_action('mycred_user_got_promoted', $user_id, $rank_id);
         } else {
             do_action('mycred_find_users_rank', $user_id, $rank_id);
         }
     }
     // Save if requested
     if ($save && $rank_id !== NULL) {
         mycred_update_user_meta($user_id, 'mycred_rank', '', $rank_id);
     }
     return apply_filters('mycred_find_users_rank', $rank_id, $user_id, $save, $amount);
 }