function mycred_assign_ranks($type = 'mycred_default') { global $wpdb; $mycred = mycred($type); // Get rank key $rank_meta_key = 'mycred_rank'; if ($mycred->is_multisite && $GLOBALS['blog_id'] > 1 && !$mycred->use_master_template) { $rank_meta_key .= '_' . $GLOBALS['blog_id']; } do_action('mycred_assign_ranks_start'); if (!mycred_override_settings()) { $posts = $wpdb->posts; $postmeta = $wpdb->postmeta; } else { $posts = $wpdb->base_prefix . 'posts'; $postmeta = $wpdb->base_prefix . 'postmeta'; } // Check for published ranks $published_ranks = mycred_get_published_ranks_count($type); // Point Type Filter $type_filter = ''; if ($type !== NULL && sanitize_text_field($type) != '') { $type_filter = $wpdb->prepare("\n\t\t\t\tINNER JOIN {$postmeta} ctype \n\t\t\t\t\tON ( ranks.ID = ctype.post_id AND ctype.meta_key = %s AND ctype.meta_value = %s )", 'ctype', $type); if ($type != 'mycred_default') { $rank_meta_key .= $type; } } $result = 0; if ($published_ranks > 0) { // Get balance key for this type $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 all user balances $users = array( [0] => array( [ID] => user id [balance] => current balance ) ); */ $users = $wpdb->get_results($wpdb->prepare("\n\t\t\t\tSELECT user_id AS ID, meta_value AS balance \n\t\t\t\tFROM {$wpdb->usermeta} \n\t\t\t\tWHERE meta_key = %s;", $balance_key), 'ARRAY_A'); /* Get rank ids with each ranks min and max values $ranks = array( [0] => array( [ID] => rank id [min] => mycred_rank_min value [max] => mycred_rank_max value ) ); */ $ranks = $wpdb->get_results("\n\t\t\t\tSELECT ranks.ID AS ID, min.meta_value AS min, max.meta_value AS max \n\t\t\t\tFROM {$posts} ranks \n\t\t\t\t{$type_filter} \n\t\t\t\tINNER JOIN {$postmeta} min \n\t\t\t\t\tON ( min.post_id = ranks.ID AND min.meta_key = 'mycred_rank_min' )\n\t\t\t\tINNER JOIN {$postmeta} max \n\t\t\t\t\tON ( max.post_id = ranks.ID AND max.meta_key = 'mycred_rank_max' )\n\t\t\t\tWHERE ranks.post_type = 'mycred_rank' \n\t\t\t\t\tAND ranks.post_status = 'publish';", 'ARRAY_A'); $count = 0; foreach ($users as $user) { foreach ($ranks as $rank) { if ($user['balance'] >= $rank['min'] && $user['balance'] <= $rank['max']) { $end = ''; if ($type != 'mycred_default') { $end = $type; } mycred_update_user_meta($user['ID'], 'mycred_rank', $end, $rank['ID']); $count = $count + 1; break 1; } } } $wpdb->flush(); $result = $count; } do_action('mycred_assign_ranks_end'); return $result; }
/** * Management * @since 1.3.2 * @version 1.0 */ public function rank_management() { $data = mycred_get_published_ranks_count(); $reset_block = false; if ($data == 0 || $data === false) { $reset_block = true; } ?> <label class="subheader"><?php _e('Ranks', 'mycred'); ?> </label> <ol id="myCRED-rank-actions" class="inline"> <li> <label><?php _e('Rank Post Type', 'mycred'); ?> </label> <div class="h2"><input type="text" id="mycred-rank-post-type" disabled="disabled" value="mycred_rank" class="readonly" /></div> </li> <li> <label><?php _e('No. of ranks', 'mycred'); ?> </label> <div class="h2"><input type="text" id="mycred-ranks-no-of-ranks" disabled="disabled" value="<?php echo $data; ?> " class="readonly short" /></div> </li> <li> <label><?php _e('Actions', 'mycred'); ?> </label> <div class="h2"><input type="button" id="mycred-manage-action-reset-ranks" value="<?php _e('Remove All Ranks', 'mycred'); ?> " class="button button-large large <?php if ($reset_block) { echo '" disabled="disabled'; } else { echo 'button-primary'; } ?> " /> <input type="button" id="mycred-manage-action-assign-ranks" value="<?php _e('Assign Ranks to Users', 'mycred'); ?> " class="button button-large large <?php if ($reset_block) { echo '" disabled="disabled'; } ?> " /></div> </li> </ol> <?php }
/** * Management * @since 1.3.2 * @version 1.1 */ public function rank_management($mycred) { $count = mycred_get_published_ranks_count($mycred->mycred_type); $reset_block = false; if ($count == 0 || $count === false) { $reset_block = true; } $rank_meta_key = 'mycred_rank'; if ($this->core->is_multisite && $GLOBALS['blog_id'] > 1 && !$this->core->use_master_template) { $rank_meta_key .= '_' . $GLOBALS['blog_id']; } if ($mycred->mycred_type != 'mycred_default') { $rank_meta_key .= $mycred->mycred_type; } ?> <label class="subheader"><?php _e('Ranks', 'mycred'); ?> </label> <ol id="myCRED-rank-actions" class="inline"> <li> <label><?php _e('User Meta Key', 'mycred'); ?> </label> <div class="h2"><input type="text" id="mycred-rank-post-type" disabled="disabled" value="<?php echo $rank_meta_key; ?> " class="readonly" /></div> </li> <li> <label><?php _e('No. of ranks', 'mycred'); ?> </label> <div class="h2"><input type="text" id="mycred-ranks-no-of-ranks" disabled="disabled" value="<?php echo $count; ?> " class="readonly short" /></div> </li> <li> <label><?php _e('Actions', 'mycred'); ?> </label> <div class="h2"><input type="button" id="mycred-manage-action-reset-ranks" data-type="<?php echo $mycred->mycred_type; ?> " value="<?php _e('Remove All Ranks', 'mycred'); ?> " class="button button-large large <?php if ($reset_block) { echo '" disabled="disabled'; } else { echo 'button-primary'; } ?> " /> <input type="button" id="mycred-manage-action-assign-ranks" data-type="<?php echo $mycred->mycred_type; ?> " value="<?php _e('Assign Ranks to Users', 'mycred'); ?> " class="button button-large large <?php if ($reset_block) { echo '" disabled="disabled'; } ?> " /></div> </li> </ol> <?php }
function mycred_assign_ranks() { global $mycred_ranks, $wpdb; $mycred = mycred(); // Get rank key $rank_meta_key = 'mycred_rank'; if ($mycred->is_multisite && $GLOBALS['blog_id'] > 1 && !$mycred->use_master_template) { $rank_meta_key .= '_' . $GLOBALS['blog_id']; } do_action('mycred_assign_ranks_start'); if (!mycred_override_settings()) { $posts = $wpdb->posts; $postmeta = $wpdb->postmeta; } else { $posts = $wpdb->base_prefix . 'posts'; $postmeta = $wpdb->base_prefix . 'postmeta'; } // Check for published ranks $published_ranks = mycred_get_published_ranks_count(); // Only one rank exists if ($published_ranks == 1) { // Get this single rank $rank_id = $wpdb->get_var("\n\t\t\t\tSELECT ID FROM {$posts} \n\t\t\t\tWHERE post_type = 'mycred_rank' AND post_status = 'publish';"); // Update all users rank to this single rank $wpdb->query($wpdb->prepare("\n\t\t\t\tUPDATE {$wpdb->usermeta} \n\t\t\t\tSET meta_value = %d \n\t\t\t\tWHERE meta_key = %s;", $rank_id, $rank_meta_key)); $wpdb->flush(); return 0 - 1; } elseif ($published_ranks > 1) { // Get balance key $balance_key = 'mycred_default'; 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 all user balances $users = array( [0] => array( [ID] => user id [balance] => current balance ) ); */ $users = $wpdb->get_results($wpdb->prepare("\n\t\t\t\tSELECT user_id AS ID, meta_value AS balance \n\t\t\t\tFROM {$wpdb->usermeta} \n\t\t\t\tWHERE meta_key = %s;", $balance_key), 'ARRAY_A'); /* Get rank ids with each ranks min and max values $ranks = array( [0] => array( [ID] => rank id [min] => mycred_rank_min value [max] => mycred_rank_max value ) ); */ $ranks = $wpdb->get_results("\n\t\t\t\tSELECT rank.ID AS ID, min.meta_value AS min, max.meta_value AS max \n\t\t\t\tFROM {$posts} rank \n\t\t\t\tINNER JOIN {$postmeta} min \n\t\t\t\t\tON ( min.post_id = rank.ID AND min.meta_key = 'mycred_rank_min' )\n\t\t\t\tINNER JOIN {$postmeta} max \n\t\t\t\t\tON ( max.post_id = rank.ID AND max.meta_key = 'mycred_rank_max' )\n\t\t\t\tWHERE rank.post_type = 'mycred_rank' \n\t\t\t\t\tAND rank.post_status = 'publish';", 'ARRAY_A'); $count = 0; foreach ($users as $user) { foreach ($ranks as $rank) { if ($user['balance'] >= $rank['min'] && $user['balance'] <= $rank['max']) { mycred_update_user_meta($user['ID'], 'mycred_rank', '', $rank['ID']); $count = $count + 1; break 1; } } } $wpdb->flush(); unset($users); unset($ranks); return $count; } else { // nothing to do when there are no ranks return 0; } do_action('mycred_assign_ranks_end'); }