Commit e1d1e87f authored by Christian Wach's avatar Christian Wach
Browse files

More efficient site switching

parent d0d1bf9b
......@@ -23,6 +23,15 @@ class CiviCRM_Network_Groups_Hooks {
*/
public $plugin;
/**
* Switched flag.
*
* @since 0.1.1
* @access public
* @var object $plugin True if switched to main site, false otherwise.
*/
public $switched = false;
/**
......@@ -51,14 +60,20 @@ class CiviCRM_Network_Groups_Hooks {
*/
public function initialise() {
// Switch to the main site.
add_filter( 'civicrm_network_groups_synced_groups', array( $this, 'switch_to_main_site' ), 10 );
// Filter groups to keep only those on the main site.
add_filter( 'civicrm_network_groups_synced_groups', array( $this, 'filter_by_main_site_groups' ), 10 );
add_filter( 'civicrm_network_groups_synced_groups', array( $this, 'filter_by_main_site_groups' ), 20 );
// Filter groups to keep only those defined in our constant.
add_filter( 'civicrm_network_groups_synced_groups', array( $this, 'filter_by_defined_groups' ), 20 );
add_filter( 'civicrm_network_groups_synced_groups', array( $this, 'filter_by_defined_groups' ), 30 );
// Filter groups by logged-in user precedence.
add_filter( 'civicrm_network_groups_synced_groups', array( $this, 'filter_by_precedence' ), 30 );
add_filter( 'civicrm_network_groups_synced_groups', array( $this, 'filter_by_precedence' ), 40 );
// Switch to original site.
add_filter( 'civicrm_network_groups_synced_groups', array( $this, 'switch_back' ), 50 );
}
......@@ -86,24 +101,12 @@ class CiviCRM_Network_Groups_Hooks {
// Init return.
$groups = array();
// Maybe switch to main site.
$switched = false;
if ( is_multisite() AND ! is_main_site() ) {
switch_to_blog( get_main_site_id() );
$switched = true;
}
// Filter by existence of corresponding "Groups" group.
foreach( $civicrm_groups AS $group ) {
$wp_group = civicrm_groups_sync()->wordpress->group_get_by_civicrm_id( $group['id'] );
if ( $wp_group !== false ) $groups[] = $group;
}
// Switch back.
if ( $switched === true ) {
restore_current_blog();
}
// --<
return $groups;
......@@ -202,21 +205,9 @@ class CiviCRM_Network_Groups_Hooks {
return $groups;
}
// Maybe switch to main site.
$switched = false;
if ( is_multisite() AND ! is_main_site() ) {
switch_to_blog( get_main_site_id() );
$switched = true;
}
// Get user's "Groups" groups on the main site.
$wp_group_ids = $this->plugin->wordpress->group_ids_get_for_user( $user->ID );
// Switch back.
if ( $switched === true ) {
restore_current_blog();
}
// Init allowed flag.
$allowed = false;
......@@ -259,4 +250,55 @@ class CiviCRM_Network_Groups_Hooks {
//##########################################################################
/**
* Switch to the main site.
*
* @since 0.1.1
*
* @param array $civicrm_groups All CiviCRM groups with sync.
* @return array $civicrm_groups Unfiltered array of CiviCRM groups with sync.
*/
public function switch_to_main_site( $civicrm_groups ) {
// Maybe switch to main site.
$this->switched = false;
if ( is_multisite() AND ! is_main_site() ) {
switch_to_blog( get_main_site_id() );
$this->switched = true;
}
// --<
return $civicrm_groups;
}
/**
* Switch back to the current site.
*
* @since 0.1.1
*
* @param array $civicrm_groups All CiviCRM groups with sync.
* @return array $civicrm_groups Unfiltered array of CiviCRM groups with sync.
*/
public function switch_back( $civicrm_groups ) {
// Maybe switch back.
if ( $this->switched === true ) {
restore_current_blog();
$this->switched = false;
}
// --<
return $civicrm_groups;
}
} // Class ends.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment