optimize query and object caching

This commit is contained in:
champsupertramp
2016-04-23 21:35:39 +08:00
parent 811b30281d
commit f46e52912b
4 changed files with 82 additions and 11 deletions
+3
View File
@@ -2,6 +2,9 @@
class UM_Profile {
public $arr_user_slugs = array();
public $arr_user_roles = array();
function __construct() {
add_action('template_redirect', array(&$this, 'active_tab'), 10002);
+33 -5
View File
@@ -2,6 +2,9 @@
class UM_Query {
public $wp_pages = array();
public $roles = array();
function __construct() {
add_action('wp_loaded', array(&$this, 'get_post_types'), 100 );
@@ -13,6 +16,11 @@ class UM_Query {
***/
function wp_pages() {
global $wpdb;
if( isset( $this->wp_pages ) && ! empty( $this->wp_pages ) ){
return $this->wp_pages;
}
$pages = $wpdb->get_results('SELECT * FROM '.$wpdb->posts.' WHERE post_type = "page" AND post_status = "publish" ', OBJECT);
$count_pages = $wpdb->num_rows;
@@ -26,6 +34,8 @@ class UM_Query {
}
}
$this->wp_pages = $array;
return $array;
}
@@ -356,19 +366,34 @@ class UM_Query {
*** @Query for UM roles
***/
function get_roles( $add_default = false, $exclude = null ){
$exclude_str = '';
if( ! is_null( $exclude ) && is_array( $exclude ) ){
$exclude_str = implode('_', $exclude );
}
if( isset( $this->roles['is_add_default_'.$add_default ][ 'is_exclude_'.$exclude_str ] ) ){
return $this->roles['is_add_default_'.$add_default ][ 'is_exclude_'.$exclude_str ];
}
$roles = array();
if ($add_default) $roles[0] = $add_default;
if ( $add_default ) {
$roles[0] = $add_default;
}
$args = array(
'post_type' => 'um_role',
'posts_per_page' => -1,
'post_status' => array('publish')
);
$results = new WP_Query($args);
if ($results->posts){
foreach($results->posts as $post) { setup_postdata($post);
if ( $results->posts ){
foreach($results->posts as $post) { setup_postdata( $post );
if ( $this->is_core( $post->ID ) ){
$roles[ $this->is_core( $post->ID ) ] = $post->post_title;
@@ -377,6 +402,7 @@ class UM_Query {
}
}
} else {
$roles['member'] = 'Member';
@@ -389,7 +415,9 @@ class UM_Query {
unset($roles[$role]);
}
}
$this->roles['is_add_default_'.$add_default ][ 'is_exclude_'.$exclude_str ] = $roles;
return $roles;
}
+10 -2
View File
@@ -1550,7 +1550,11 @@ function um_fetch_user( $user_id ) {
* @return integer
*/
function um_is_meta_value_exists( $key, $value, $return_user_id = false ){
global $wpdb;
global $wpdb, $ultimatemember;
if( isset( $ultimatemember->profile->arr_user_slugs[ 'is_'.$return_user_id ][ $key ] ) ){
return $ultimatemember->profile->arr_user_slugs[ 'is_'.$return_user_id ][ $key ];
}
if( ! $return_user_id ){
$count = $wpdb->get_var( $wpdb->prepare(
@@ -1559,14 +1563,18 @@ function um_fetch_user( $user_id ) {
$value
) );
$ultimatemember->profile->arr_user_slugs[ 'is_'.$return_user_id ][ $key ] = $count;
return $count;
}
$user_id = $wpdb->get_var( $wpdb->prepare(
"SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s ",
$key,
$value
) );
$ultimatemember->profile->arr_user_slugs[ 'is_'.$return_user_id ][ $key ] = $user_id;
return $user_id;
+36 -4
View File
@@ -642,6 +642,8 @@ class UM_User {
*
*/
function get_role() {
global $ultimatemember;
if (isset($this->profile['role']) && !empty( $this->profile['role'] ) ) {
return $this->profile['role'];
} else {
@@ -653,10 +655,40 @@ class UM_User {
}
}
function get_role_name( $slug ) {
global $wpdb;
$post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'um_role' AND post_name = '$slug'");
return get_the_title( $post_id );
function get_role_name( $slug, $return_role_id = false ) {
global $wpdb, $ultimatemember;
if( isset( $ultimatemember->profile->arr_user_roles[ 'is_'.$return_role_id ][ $slug ] ) ){
return $ultimatemember->profile->arr_user_roles[ 'is_'.$return_role_id ][ $slug ];
}
$args = array(
'posts_per_page' => 1,
'post_type' => 'um_role',
'name' => $slug,
'post_status' => array('publish'),
);
$roles = new WP_Query( $args );
$role_id = 0;
$role_title = '';
if ( $roles->have_posts() ) {
while ( $roles->have_posts() ) {
$roles->the_post();
$role_id = get_the_ID();
$role_title = get_the_title();
}
}
$ultimatemember->profile->arr_user_roles[ 'is_1' ][ $slug ] = $role_id;
$ultimatemember->profile->arr_user_roles[ 'is_' ][ $slug ] = $role_title;
if( $return_role_id ){
return $role_id;
}
return $role_title;
}
/***