/** * Construct */ function __construct($type = 'mycred_default') { // Prep $this->is_multisite = is_multisite(); $this->use_master_template = mycred_override_settings(); $this->use_central_logging = mycred_centralize_log(); if ($type == '' || $type === NULL) { $type = 'mycred_default'; } // Load Settings $option_id = 'mycred_pref_core'; if ($type != 'mycred_default' && $type != '') { $option_id .= '_' . $type; } $this->core = mycred_get_option($option_id, $this->defaults()); if ($this->core !== false) { foreach ((array) $this->core as $key => $value) { $this->{$key} = $value; } } if ($type != '') { $this->cred_id = $type; } if (defined('MYCRED_LOG_TABLE')) { $this->log_table = MYCRED_LOG_TABLE; } else { global $wpdb; if ($this->is_multisite && $this->use_central_logging) { $this->log_table = $wpdb->base_prefix . 'myCRED_log'; } else { $this->log_table = $wpdb->prefix . 'myCRED_log'; } } do_action_ref_array('mycred_settings', array(&$this)); }
/** * Add Admin Menu Item * @since 0.1 * @version 1.2.1 */ function add_menu() { // Network Setting for Multisites if (mycred_override_settings() && $GLOBALS['blog_id'] > 1 && substr($this->screen_id, 0, 6) == 'myCRED' && strlen($this->screen_id) > 6) { return; } if (!empty($this->labels) && !empty($this->screen_id)) { // Menu Slug $menu_slug = 'myCRED'; if (!$this->is_main_type) { $menu_slug = 'myCRED_' . $this->mycred_type; } // Menu Label if (!isset($this->labels['page_title']) && !isset($this->labels['menu'])) { $label_menu = __('Surprise', 'mycred'); } elseif (isset($this->labels['menu'])) { $label_menu = $this->labels['menu']; } else { $label_menu = $this->labels['page_title']; } // Page Title if (!isset($this->labels['page_title']) && !isset($this->labels['menu'])) { $label_title = __('Surprise', 'mycred'); } elseif (isset($this->labels['page_title'])) { $label_title = $this->labels['page_title']; } else { $label_title = $this->labels['menu']; } if ($this->cap != 'plugin') { $cap = $this->core->edit_creds_cap(); } else { $cap = $this->core->edit_plugin_cap(); } // Add Submenu Page $page = add_submenu_page($menu_slug, $label_menu, $label_title, $cap, $this->screen_id, array($this, 'admin_page')); add_action('admin_print_styles-' . $page, array($this, 'settings_page_enqueue')); add_action('load-' . $page, array($this, 'screen_options')); } }
function mycred_user_got_promoted($user_id = NULL, $rank_id = NULL) { $type = get_post_meta($rank_id, 'ctype', true); if ($type == '') { $type = 'mycred_default'; update_post_meta($rank_id, 'ctype', $type); } $end = ''; if ($type != 'mycred_default') { $end = $type; } $current_rank_id = mycred_get_user_meta($user_id, 'mycred_rank', $end, true); // No promotion if ($current_rank_id == $rank_id) { return false; } // User did not have a rank before but will have now, that is assumed to be a promotion if (empty($current_rank_id) && !empty($rank_id)) { return true; } // Get minimums if (!mycred_override_settings()) { $current_min = get_post_meta($current_rank_id, 'mycred_rank_min', true); $new_min = get_post_meta($rank_id, 'mycred_rank_min', true); } else { $original_blog_id = get_current_blog_id(); switch_to_blog(1); $current_min = get_post_meta($current_rank_id, 'mycred_rank_min', true); $new_min = get_post_meta($rank_id, 'mycred_rank_min', true); switch_to_blog($original_blog_id); } // Compare if ($new_min > $current_min) { return true; } return false; }
/** * Delete Ranks * @since 1.3.2 * @version 1.1 */ public function action_delete_ranks() { // Security check_ajax_referer('mycred-management-actions-roles', 'token'); // Define type $type = 'mycred_default'; if (isset($_POST['ctype']) && array_key_exists($_POST['ctype'], $this->point_types)) { $type = sanitize_text_field($_POST['ctype']); } global $wpdb; // Get the appropriate tables based on setup if (!mycred_override_settings()) { $posts = $wpdb->posts; $postmeta = $wpdb->postmeta; } else { $posts = $wpdb->base_prefix . 'posts'; $postmeta = $wpdb->base_prefix . 'postmeta'; } // First get the ids of all existing ranks $rank_ids = $wpdb->get_col($wpdb->prepare("\n\t\t\t\tSELECT DISTINCT ranks.ID \n\t\t\t\tFROM {$posts} ranks \n\t\t\t\tINNER JOIN {$postmeta} ctype \n\t\t\t\t\tON ( ranks.ID = ctype.post_id AND ctype.meta_key = %s )\n\t\t\t\tWHERE ranks.post_type = %s\n\t\t\t\tAND ctype.meta_value = %s;", 'ctype', 'mycred_rank', $type)); // If ranks were found $rows = 0; if (!empty($rank_ids)) { $id_list = implode(',', $rank_ids); // Remove posts $wpdb->query("\n\t\t\t\t\tDELETE FROM {$posts} \n\t\t\t\t\tWHERE post_type = 'mycred_rank'\n\t\t\t\t\tAND post_id IN ({$id_list});"); // Remove post meta $wpdb->query("\n\t\t\t\t\tDELETE FROM {$postmeta} \n\t\t\t\t\tWHERE post_id IN ({$id_list});"); // Confirm that ranks are gone by counting ranks // If all went well this should return zero. $rows = $wpdb->get_var($wpdb->prepare("\n\t\t\t\t\tSELECT COUNT(*) \n\t\t\t\t\tFROM {$posts} ranks \n\t\t\t\t\tINNER JOIN {$postmeta} ctype \n\t\t\t\t\t\tON ( ranks.ID = ctype.post_id AND ctype.meta_key = %s )\n\t\t\t\t\tWHERE ranks.post_type = %s\n\t\t\t\t\tAND ctype.meta_value = %s;", 'ctype', 'mycred_rank', $type)); } die(json_encode(array('status' => 'OK', 'rows' => $rows))); }
function mycred_render_shortcode_total_points($atts) { extract(shortcode_atts(array('type' => 'mycred_default', 'ref' => '', 'ref_id' => '', 'user_id' => '', 'formatted' => 1), $atts)); $types = mycred_get_types(); if (!array_key_exists($type, $types)) { $type = 'mycred_default'; } // First we construct the meta_key $point_type = $type; if (is_multisite() && $GLOBALS['blog_id'] > 1 && !mycred_centralize_log()) { $type .= '_' . $GLOBALS['blog_id']; } elseif (is_multisite() && $GLOBALS['blog_id'] > 1 && !mycred_override_settings()) { $type .= '_' . $GLOBALS['blog_id']; } $mycred = mycred($point_type); global $wpdb; // Simple if ($ref == '' && $ref_id == '' && $user_id == '') { // Add up all balances $total = $wpdb->get_var($wpdb->prepare("SELECT SUM( meta_value ) FROM {$wpdb->usermeta} WHERE meta_key = %s", $type)); } else { $wheres = array(); $wheres[] = $wpdb->prepare("ctype = %s", $point_type); $ref = sanitize_key($ref); if (strlen($ref) > 0) { // Either we have just one reference $multiple = explode(',', $ref); if (count($multiple) == 1) { $wheres[] = $wpdb->prepare("ref = %s", $ref); } else { $_clean = array(); foreach ($multiple as $ref) { $ref = sanitize_key($ref); if (strlen($ref) > 0) { $_clean[] = $ref; } } if (!empty($_clean)) { $wheres[] = "ref IN ( '" . implode("', '", $_clean) . "' )"; } } } $ref_id = sanitize_text_field($ref); if (strlen($ref_id) > 0) { $wheres[] = $wpdb->prepare("ref_id = %d", $ref_id); } $user_id = sanitize_text_field($ref); if (strlen($user_id) > 0) { $wheres[] = $wpdb->prepare("user_id = %d", $user_id); } $wheres = implode(" AND ", $wheres); $total = $wpdb->get_var("SELECT SUM( creds ) FROM {$mycred->log_table} WHERE {$wheres};"); } if ($total === NULL) { $total = 0; } if ($formatted == 1) { return $mycred->format_creds($total); } return $total; }
/** * 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); } }
function mycred_user_got_promoted($user_id = NULL, $rank_id = NULL) { $current_rank_id = mycred_get_user_meta($user_id, 'mycred_rank', '', true); if ($current_rank_id == $rank_id) { return false; } if (empty($current_rank_id) && !empty($rank_id)) { return true; } if (!mycred_override_settings()) { $current_min = get_post_meta($current_rank_id, 'mycred_rank_min', true); $new_min = get_post_meta($rank_id, 'mycred_rank_min', true); } else { $original_blog_id = get_current_blog_id(); switch_to_blog(1); $current_min = get_post_meta($current_rank_id, 'mycred_rank_min', true); $new_min = get_post_meta($rank_id, 'mycred_rank_min', true); switch_to_blog($original_blog_id); } if ($new_min > $current_min) { return true; } return false; }