Skip to content

Commit

Permalink
Remove some redundant database calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Flynsarmy committed Mar 31, 2019
1 parent 6b8e02d commit bbc08e2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
31 changes: 16 additions & 15 deletions mabs_admin_bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class MABS_Admin_Bar extends WP_Admin_Bar
{
protected $active_blog_for_user_cache = [];

/**
* This is the same as WP_Admin_Bar's initialize method but
* $this->user->blogs serves our cached copy of user blogs for
Expand Down Expand Up @@ -75,23 +77,22 @@ public function initialize()
* @global wpdb $wpdb
*
* @param int $user_id The unique ID of the user
* @return object|void The blog object
* @return object|array The blog object
*/
function mabs_get_active_blog_for_user( $user_id ) {
$cache = get_site_transient('mabs_activeblog_'.$user_id);
if ($cache)
return $cache;
if ($this->active_blog_for_user_cache)
return $this->active_blog_for_user_cache;

global $wpdb;
$blogs = mabs_get_blogs_of_user( $user_id );
if ( empty( $blogs ) )
return;
return [];

if ( !is_multisite() )
{
$cache = $blogs[$wpdb->blogid];
set_site_transient('mabs_activeblog_'.$user_id, $cache, apply_filters('mabs_cache_duration', 60*60*30));
return $cache;
$this->active_blog_for_user_cache = $blogs[$wpdb->blogid];
set_site_transient('mabs_activeblog_'.$user_id, $this->active_blog_for_user_cache, apply_filters('mabs_cache_duration', 60*60*30));
return $this->active_blog_for_user_cache;
}

$primary_blog = get_user_meta( $user_id, 'primary_blog', true );
Expand Down Expand Up @@ -128,15 +129,15 @@ function mabs_get_active_blog_for_user( $user_id ) {
}
}
} else {
return;
return [];
}
$cache = $ret;
set_site_transient('mabs_activeblog_'.$user_id, $cache, apply_filters('mabs_cache_duration', 60*60*30));
return $cache;
$this->active_blog_for_user_cache = $ret;

} else {
$cache = $primary;
set_site_transient('mabs_activeblog_'.$user_id, $cache, apply_filters('mabs_cache_duration', 60*60*30));
return $cache;
$this->active_blog_for_user_cache = $primary;
}

set_site_transient('mabs_activeblog_'.$user_id, $this->active_blog_for_user_cache, apply_filters('mabs_cache_duration', 60*60*30));
return $this->active_blog_for_user_cache;
}
}
2 changes: 1 addition & 1 deletion multisite-admin-bar-switcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Multisite Admin bar Switcher
Plugin URI: http://www.flynsarmy.com
Description: Replaces the built in 'My Sites' drop down with a better layed out one
Version: 1.3.0
Version: 1.3.1
Author: Flyn San
Author URI: http://www.flynsarmy.com/
Expand Down
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: flynsarmy
Tags: multisite, toolbar, switcher, switch, network, admin, wpmu
Requires at least: 3.2.1
Tested up to: 5.1.1
Stable tag: 1.3.0
Stable tag: 1.3.1

== Description ==

Expand Down Expand Up @@ -78,6 +78,9 @@ add_filter('mabs_cache_duration', function($cachetime) {

== Changelog ==

= 1.3.1 =
* Remove some redundant database calls

= 1.3.0 =
* Cache admin URLs to avoid hitting the DB

Expand Down

0 comments on commit bbc08e2

Please sign in to comment.