Last active
May 18, 2023 17:45
-
-
Save christianwach/897a0e94add0f477efdd03c089e25019 to your computer and use it in GitHub Desktop.
Resolves action conflicts between the BuddyPress and Groups plugins.
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: BuddyPress Groups Action Resolver | |
* Plugin URI: https://gist.github.com/christianwach/897a0e94add0f477efdd03c089e25019 | |
* Description: Resolves action conflicts between the BuddyPress and Groups plugins. | |
* Version: 1.0.0 | |
* Author: Christian Wach | |
* Author URI: https://haystack.co.uk | |
* License: GPLv3 | |
* | |
* Put this plugin in /wp-content/mu-plugins/ | |
* | |
* Based on code by itthinx <https://www.itthinx.com> | |
* | |
* @see https://github.com/itthinx/groups-buddypress-compatibility | |
* | |
* @package CMW_BuddyPress_Groups_Actions_Resolver | |
*/ | |
// Exit if accessed directly. | |
defined( 'ABSPATH' ) || exit; | |
/** | |
* BuddyPress Groups Action Resolver Class. | |
* | |
* A class that holds plugin functionality. | |
* | |
* @since 1.0 | |
*/ | |
class CMW_BuddyPress_Groups_Actions_Resolver { | |
/** | |
* Compatibility flag. | |
* | |
* True when BuddyPress actions have been removed, false otherwise. | |
* | |
* @since 1.0.0 | |
* @access private | |
* @var bool | |
*/ | |
private $compatibility = false; | |
/** | |
* Constructor. | |
* | |
* @since 1.0 | |
*/ | |
public function __construct() { | |
// Add compatibility actions. | |
add_action( 'groups_created_group', [ $this, 'actions_remove' ], 0 ); | |
add_action( 'groups_created_group', [ $this, 'actions_add' ], PHP_INT_MAX - 1 ); | |
} | |
/** | |
* Removes BuddyPress actions. | |
* | |
* Identifies whether a call to the "groups_created_group" action comes from the | |
* Groups plugin or from BuddyPress. Disables the related BuddyPress actions when | |
* appropriate, i.e. when the call comes from Groups and not from BuddyPress. | |
* | |
* @since 1.0.0 | |
* | |
* @param int $group_id The numeric ID of the Group. | |
*/ | |
public function actions_remove( $group_id ) { | |
// Determine if the call comes from Groups. | |
$is_groups = false; | |
$traces = debug_backtrace(); | |
if ( is_array( $traces ) ) { | |
foreach( $traces as $trace ) { | |
if ( isset( $trace['file'] ) ) { | |
if ( false !== strpos( $trace['file'], 'class-groups-group.php' ) ) { | |
$is_groups = true; | |
break; | |
} | |
} | |
} | |
} | |
// Bail if not from Groups. | |
if ( ! $is_groups ) { | |
return; | |
} | |
// Remove the BuddyPress actions. | |
remove_action( 'groups_created_group', 'groups_update_last_activity' ); | |
remove_action( 'groups_created_group', 'bp_groups_clear_group_creator_cache', 10 ); | |
remove_action( 'groups_created_group', 'bp_core_clear_cache' ); | |
/** | |
* Fires when BuddyPress actions have been removed. | |
* | |
* This action allows plugins to remove their callbacks to the BuddyPress | |
* "groups_created_group" action when the call is made by the Groups plugin. | |
* | |
* @since 1.0.0 | |
*/ | |
do_action( 'cmw_buddypress_groups_actions_resolver/removed' ); | |
// Set flag. | |
$this->compatibility = true; | |
} | |
/** | |
* Adds BuddyPress actions. | |
* | |
* @since 1.0.0 | |
* | |
* @param int $group_id The numeric ID of the Group. | |
*/ | |
public function actions_add( $group_id ) { | |
// Bail if flag not set. | |
if ( true !== $this->compatibility ) { | |
return; | |
} | |
// Add the BuddyPress actions. | |
add_action( 'groups_created_group', 'groups_update_last_activity' ); | |
add_action( 'groups_created_group', 'bp_groups_clear_group_creator_cache', 10, 2 ); | |
add_action( 'groups_created_group', 'bp_core_clear_cache' ); | |
/** | |
* Fires when BuddyPress actions have been added. | |
* | |
* This action allows plugins to reinstate their callbacks to the BuddyPress | |
* "groups_created_group" action. | |
* | |
* @since 1.0.0 | |
*/ | |
do_action( 'cmw_buddypress_groups_actions_resolver/added' ); | |
} | |
} | |
/** | |
* Gets a reference to this plugin. | |
* | |
* @since 1.0 | |
* | |
* @return object $plugin The plugin reference. | |
*/ | |
function cmw_buddypress_groups_actions_resolver() { | |
// Return instance. | |
static $plugin; | |
if ( ! isset( $plugin ) ) { | |
$plugin = new CMW_BuddyPress_Groups_Actions_Resolver(); | |
} | |
return $plugin; | |
} | |
// Bootstrap plugin. | |
cmw_buddypress_groups_actions_resolver(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment