function CountMembers($activeOnly = false) { global $wpdb, $WishListMemberInstance; $table = $WishListMemberInstance->TablePrefix . 'userlevels'; $table_options = $WishListMemberInstance->TablePrefix . 'userlevel_options'; $member_count = wlm_cache_get('wishlist_member_all_levels_members_count', 'wishlist-member'); if ($member_count === false) { $results = $wpdb->get_results("SELECT `level_id`,COUNT(*) AS `cnt` FROM `{$table}` GROUP BY `level_id`"); foreach ($results as $result) { $member_count[$result->level_id] = $result->cnt; } wlm_cache_set('wishlist_member_all_levels_members_count', $member_count, 'wishlist-member'); } if ($activeOnly) { $date = $this->noexpire == 1 ? '1000-00-00 00:00:00' : date('Y-m-d H:i:s', strtotime("-{$this->expire} {$this->calendar}")); $query = sprintf("SELECT COUNT(DISTINCT `%1\$s`.`user_id`) FROM `%1\$s` LEFT JOIN `%2\$s`\n\t\t\t\t\tON `%1\$s`.`ID`=`%2\$s`.`userlevel_id`\n\t\t\t\t\tAND (`%2\$s`.`option_name` IN ('cancelled','forapproval','unconfirmed','registration_date')\n\t\t\t\t\tAND `%2\$s`.`option_value`<>''\n\t\t\t\t\tAND `%2\$s`.`option_value`<>0\n\t\t\t\t\tand `%2\$s`.`option_value`<='%3\$s')\n\t\t\t\t\t\tWHERE `%1\$s`.`level_id`=%4\$d\n\t\t\t\t\t\tAND `%2\$s`.`userlevel_id` IS NULL", $table, $table_options, $date, $this->ID); return $wpdb->get_var($query); } else { return $member_count[$this->ID]; } }
/** * Return Member's Membership Levels * @global object $wpdb * @param int $id User ID * @param boolean $names (optional) Default False. TRUE to return Level names instead of IDs * @param boolean $activeOnly (optional) Default False. TRUE to return active levels only * @param boolean $no_cache (optional) Default False. TRUE to skip cache data * @param boolean $no_userlevels (optional) Default False. TRUE to exclude User Level (U-xxx) * @param boolean $include_scheduled (optional) Default TRUE. FALSE to exclude scheduled levels * @return array Levels */ function GetMembershipLevels($id, $names = null, $activeOnly = null, $no_cache = null, $no_userlevels = null, $include_scheduled = null) { global $wpdb; $names = is_null($names) ? false : $names; $activeOnly = is_null($activeOnly) ? false : $activeOnly; $no_cache = is_null($no_cache) ? false : $no_cache; $no_userlevels = is_null($no_userlevels) ? false : $no_userlevels; if (is_null($include_scheduled)) { $include_scheduled = true; } $levels = $no_cache === true ? false : wlm_cache_get($id, $this->Tables->userlevels); #empty user == no membership levels if (empty($id)) { return array(); } if ($levels === false) { if ($include_scheduled) { $query = $wpdb->prepare("SELECT `level_id` FROM `{$this->Tables->userlevels}` WHERE `user_id`=%d", $id); } else { $query = $wpdb->prepare("SELECT `level_id`,MAX(`option_name`='scheduled_move' OR `option_name`='scheduled_add' OR `option_name`='scheduled_remove') `scheduled` FROM `{$this->Tables->userlevels}` `l` LEFT JOIN `{$this->Tables->userlevel_options}` `lo` ON `l`.`ID`=`lo`.`userlevel_id` WHERE `user_id`=%d GROUP BY `level_id` HAVING `scheduled`=0", $id); } $levels = $wpdb->get_col($query); wlm_cache_set($id, $levels, $this->Tables->userlevels); } if ($names) { $wpm_levels = $this->GetOption('wpm_levels'); $names = array(); foreach ((array) $levels as $level) { $name = $wpm_levels[$level]['name']; if ($this->LevelCancelled($level, $id) or $this->LevelForApproval($level, $id) or $this->LevelUnConfirmed($level, $id) or $this->LevelExpired($level, $id)) { $name = '<strike>' . $name . '</strike>'; } $names[] = $name; } return implode(', ', $names); } else { if ($activeOnly) { foreach ((array) $levels as $key => $level) { if ($this->IsLevelScheduled($level, $id) or $this->LevelCancelled($level, $id) or $this->LevelForApproval($level, $id) or $this->LevelUnConfirmed($level, $id) or $this->LevelExpired($level, $id)) { unset($levels[$key]); } } $levels = array_merge($levels, array()); } if (!$no_userlevels) { // force individual user level $levels[] = 'U-' . $id; } return $levels; } }
/** * Cache User Level Information for the specified IDs * @param array $ids User IDs */ function PreLoad_UserLevelsMeta($ids, $force_cache = null) { global $wpdb; if (!is_array($ids) || !count($ids)) { return; } if (!is_array($force_cache)) { $force_cache = array('cancelled' => 0, 'unconfirmed' => 0, 'forapproval' => 0, 'registration_date' => '', 'transaction_id' => ''); } foreach ($ids as $k => $v) { $ids[$k] = (int) $v; } $ids = "'" . implode("','", $ids) . "'"; $query = "SELECT `ulm`.`option_name` AS `option_name`, `ulm`.`option_value` AS `option_value`, `ul`.`level_id`, `ul`.`user_id` AS `user_id` FROM `{$this->Tables->userlevel_options}` AS `ulm` JOIN `{$this->Tables->userlevels}` AS `ul` ON `ul`.`ID`=`ulm`.`userlevel_id` WHERE `ul`.`user_id` IN ({$ids}) ORDER BY `ul`.`user_id` ASC, `ul`.`level_id` ASC"; $results = $wpdb->get_results($query); if (!count($results)) { return; } $cache_group = $this->Tables->userlevel_options; $fcache = $force_cache; $prev_key = ''; foreach ($results as $result) { $key = $result->user_id . '_' . $result->level_id; if ($prev_key != $key && $prev_key != '') { foreach ($fcache as $meta => $value) { wlm_cache_set($key . '_' . $meta, $value, $cache_group); } $fcache = $force_cache; } $cache_key = $result->user_id . '_' . $result->level_id . '_' . $result->option_name; $value = maybe_unserialize($result->option_value); wlm_cache_set($cache_key, $value, $cache_group); unset($fcache[$result->option_name]); $prev_key = $key; } }
/** * Retrieves an option's value * @param string $option The name of the option * @param boolean $dec (optional) True to decrypt the return value * @param boolean $no_cache (optional) True to skip cache data * @return string The option value */ function GetOption($option, $dec = null, $no_cache = null) { global $wpdb; $cache_key = $option; $cache_group = $this->OptionsTable; if (is_null($dec)) { $dec = false; } if (is_null($no_cache)) { $no_cache = false; } $value = $no_cache === true ? false : wlm_cache_get($cache_key, $cache_group); if ($value === false) { $row = $wpdb->get_row($wpdb->prepare("SELECT `option_value` FROM `{$this->OptionsTable}` WHERE `option_name`='%s'", $option)); if (!is_object($row)) { return false; } $value = $row->option_value; $value = maybe_unserialize($value); wlm_cache_set($cache_key, $value, $cache_group); } if ($dec) { $value = $this->WLMDecrypt($value); } return $value; }