Last active
March 2, 2023 09:24
-
-
Save christianwach/334816adcb29565dbfe35fd8d5c6d5b5 to your computer and use it in GitHub Desktop.
A WordPress plugin that manages the "Groups" plugin filters across a Multisite network.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Manage Groups Filters | |
* Plugin URI: https://gist.github.com/christianwach/334816adcb29565dbfe35fd8d5c6d5b5 | |
* Description: Manages the "Groups" plugin filters across a Multisite network. | |
* Version: 1.0 | |
* | |
* Put this plugin in /wp-content/mu-plugins/ | |
* | |
* @package CMW_Groups_Filters_Manage | |
*/ | |
// Exit if accessed directly. | |
defined( 'ABSPATH' ) || exit; | |
/** | |
* Manage Groups Filters Class. | |
* | |
* A class that holds plugin functionality. | |
* | |
* @since 1.0 | |
*/ | |
class CMW_Groups_Filters_Manage { | |
/** | |
* Constructor. | |
* | |
* @since 1.0 | |
*/ | |
public function __construct() { | |
// Init when all plugins have loaded. | |
add_action( 'plugins_loaded', [ $this, 'initialise' ] ); | |
} | |
/** | |
* Initialises this object. | |
* | |
* @since 1.0 | |
*/ | |
public function initialise() { | |
// Bail if the "Groups" plugin is not active on the loaded Site. | |
if ( ! defined( 'GROUPS_CORE_VERSION' ) ) { | |
return; | |
} | |
// Register hooks. | |
$this->register_hooks(); | |
} | |
/** | |
* Registers hooks. | |
* | |
* @since 1.0 | |
*/ | |
public function register_hooks() { | |
// Ensure "Groups" only filters on Sites where it is active. | |
add_action( 'switch_blog', [ $this, 'switch_to' ], 20, 3 ); | |
add_action( 'switch_blog', [ $this, 'switch_back' ], 21, 3 ); | |
} | |
/** | |
* Toggles the "Groups" plugin callbacks when switching to a Site. | |
* | |
* @since 1.0 | |
*/ | |
public function switch_to( $new_blog_id, $prev_blog_id, $context ) { | |
// Bail if not "switching to". | |
if ( 'switch' !== $context ) { | |
return; | |
} | |
// We need to make sure "is_plugin_active_for_network" is available. | |
if ( ! function_exists( 'is_plugin_active_for_network' ) ) { | |
require_once ABSPATH . '/wp-admin/includes/plugin.php'; | |
} | |
// Toggle filter depending on whether the "Groups" plugin is active on the Site. | |
if ( ! is_plugin_active( 'groups/groups.php' ) ) { | |
$this->user_has_cap_hook_remove(); | |
} else { | |
$this->user_has_cap_hook_add(); | |
} | |
} | |
/** | |
* Toggles the "Groups" plugin callbacks when restoring a Site. | |
* | |
* @since 1.0 | |
*/ | |
public function switch_back( $new_blog_id, $prev_blog_id, $context ) { | |
// Bail if not "switching back". | |
if ( 'restore' !== $context ) { | |
return; | |
} | |
// We need to make sure "is_plugin_active_for_network" is available. | |
if ( ! function_exists( 'is_plugin_active_for_network' ) ) { | |
require_once ABSPATH . '/wp-admin/includes/plugin.php'; | |
} | |
// Toggle filter depending on whether the "Groups" plugin is active on the Site. | |
if ( ! is_plugin_active( 'groups/groups.php' ) ) { | |
$this->user_has_cap_hook_remove(); | |
} else { | |
$this->user_has_cap_hook_add(); | |
} | |
} | |
/** | |
* Removes the "Groups" plugin "user_has_cap" callback. | |
* | |
* @since 1.0 | |
*/ | |
public function user_has_cap_hook_remove() { | |
// Don't remove when class not present. | |
if ( ! class_exists( 'Groups_WordPress' ) ) { | |
return; | |
} | |
// Don't remove when filter not present. | |
if ( ! has_filter( 'user_has_cap', [ 'Groups_WordPress', 'user_has_cap' ] ) ) { | |
return; | |
} | |
// Okay, remove the filter. | |
remove_filter( 'user_has_cap', [ 'Groups_WordPress', 'user_has_cap' ], PHP_INT_MAX ); | |
} | |
/** | |
* Adds the "Groups" plugin "user_has_cap" callback. | |
* | |
* @since 1.0 | |
*/ | |
public function user_has_cap_hook_add() { | |
// Don't add when class not present. | |
if ( ! class_exists( 'Groups_WordPress' ) ) { | |
return; | |
} | |
// Don't add when filter is present. | |
if ( has_filter( 'user_has_cap', [ 'Groups_WordPress', 'user_has_cap' ] ) ) { | |
return; | |
} | |
// Okay, add the filter. | |
add_filter( 'user_has_cap', [ 'Groups_WordPress', 'user_has_cap' ], PHP_INT_MAX, 4 ); | |
} | |
} | |
/** | |
* Gets a reference to this plugin. | |
* | |
* @since 1.0 | |
* | |
* @return CMW_Groups_Filters_Manage $plugin The plugin reference. | |
*/ | |
function cmw_groups_filters_manage() { | |
// Return instance. | |
static $plugin; | |
if ( ! isset( $plugin ) ) { | |
$plugin = new CMW_Groups_Filters_Manage(); | |
} | |
return $plugin; | |
} | |
// Bootstrap plugin. | |
cmw_groups_filters_manage(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment