Example #1
0
        /**
         * Edit Profile Screen
         * @since 1.5
         * @version 1.0.2
         */
        public function edit_profile_screen()
        {
            if (!isset($_GET['user_id'])) {
                return;
            }
            $user_id = absint($_GET['user_id']);
            if (!isset($_GET['ctype'])) {
                $type = 'mycred_default';
            } else {
                $type = sanitize_text_field($_GET['ctype']);
            }
            $mycred = mycred($type);
            // Security
            if (!$mycred->can_edit_creds()) {
                wp_die(__('Access Denied', 'mycred'));
            }
            // User is excluded
            if ($mycred->exclude_user($user_id)) {
                wp_die(sprintf(__('This user is excluded from using %s', 'mycred'), mycred_label()));
            }
            $user = get_userdata($user_id);
            $balance = $mycred->get_users_balance($user_id);
            if ($type == 'mycred_default') {
                $log_slug = 'myCRED';
            } else {
                $log_slug = 'myCRED_' . $type;
            }
            $history_url = add_query_arg(array('page' => $log_slug, 'user_id' => $user->ID), admin_url('admin.php'));
            $exclude_url = add_query_arg(array('action' => 'exclude'));
            ?>
<style type="text/css">
div#edit-balance-page table.table { width: 100%; margin-top: 24px; }
div#edit-balance-page table.table th { text-align: left; }
div#edit-balance-page table.table td { width: 33%; font-size: 24px; line-height: 48px; }
div#edit-balance-page table tr td table tr td { vertical-align: top; }
div#edit-balance-page table.form-table { border-top: 1px solid #ccc; }
div#edit-balance-page.wrap form#your-profile h3 { margin-top: 3em; }
</style>
<div class="wrap" id="edit-balance-page">
	<h2><?php 
            _e('Edit User', 'mycred');
            if (current_user_can('create_users')) {
                ?>
	<a href="user-new.php" class="add-new-h2"><?php 
                echo esc_html_x('Add New', 'user', 'mycred');
                ?>
</a>
<?php 
            } elseif (is_multisite() && current_user_can('promote_users')) {
                ?>
	<a href="user-new.php" class="add-new-h2"><?php 
                echo esc_html_x('Add Existing', 'user', 'mycred');
                ?>
</a>
<?php 
            }
            ?>
</h2>
	<form id="your-profile" action="" method="post">
		<?php 
            echo $this->user_nav($user, $type);
            ?>

		<div class="clear clearfix"></div>
		<table class="table">
			<thead>
				<tr>
					<th><?php 
            _e('Current Balance', 'mycred');
            ?>
</th>
					<th><?php 
            printf(__('Total %s Accumulated', 'mycred'), $mycred->plural());
            ?>
</th>
					<th><?php 
            printf(__('Total %s Spent', 'mycred'), $mycred->plural());
            ?>
</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td><?php 
            echo $mycred->format_creds($balance);
            ?>
</td>
					<td><?php 
            echo $mycred->format_creds(mycred_get_users_total($user->ID, $type));
            ?>
</td>
					<td><?php 
            echo $mycred->format_creds($this->get_users_total_spent($user->ID, $type));
            ?>
</td>
				</tr>
			</tbody>
		</table>
		<a href="<?php 
            echo esc_url($history_url);
            ?>
" class="button button-secondary"><?php 
            _e('View History', 'mycred');
            ?>
</a>
		<a href="<?php 
            echo esc_url($exclude_url);
            ?>
" class="button button-primary" id="mycred-exclude-this-user"><?php 
            _e('Exclude User', 'mycred');
            ?>
</a>

		<?php 
            do_action('mycred_before_edit_profile', $user, $type);
            ?>

		<h3><?php 
            _e('Adjust Balance', 'mycred');
            ?>
</h3>
		<?php 
            $this->adjust_users_balance($user);
            ?>

		<?php 
            do_action('mycred_edit_profile', $user, $type);
            ?>

	</form>
	<script type="text/javascript">
jQuery(function($) {
	$( 'a#mycred-exclude-this-user' ).click(function(){
		if ( ! confirm( '<?php 
            echo esc_js(esc_attr__('Warning! Excluding this user will result in their balance being deleted along with any entries currently in your log! This can not be undone!', 'mycred'));
            ?>
' ) )
			return false;
	});
});
	</script>
</div>
<?php 
        }
Example #2
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);
     }
 }