Created
August 14, 2014 13:18
-
-
Save imath/59875514ff46ba5e191b to your computer and use it in GitHub Desktop.
This a snippet to auto join "just activated" members in one or more BuddyPress groups.
This file contains hidden or 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 | |
if ( ! class_exists( 'Imath_Auto_Join_Groups' ) ) : | |
/** | |
* AutoJoin new members to chosen groups | |
* | |
* This is a little snippet, feel free to : | |
* - use it | |
* - extend it.. | |
* - copy it... | |
* - build a plugin out of it :) | |
* | |
* But sorry, i have no time to support it :( | |
*/ | |
class Imath_Auto_Join_Groups { | |
/** | |
* Start the class. | |
* | |
* @access public | |
* | |
* @uses buddypress() to get BuddyPress main instance. | |
* @static | |
*/ | |
public static function start_groups_autojoin() { | |
// Bail if groups component is not active | |
if ( ! bp_is_active( 'groups' ) ) { | |
return false; | |
} | |
$bp = buddypress(); | |
if ( empty( $bp->groups->imath_aj ) ) { | |
$bp->groups->imath_aj = new self; | |
} | |
return $bp->groups->imath_aj; | |
} | |
/** | |
* Construct. | |
* | |
* @access public | |
*/ | |
public function __construct() { | |
$this->setup_globals(); | |
$this->setup_hooks(); | |
} | |
/** | |
* Set Globals | |
* | |
* Using public here so that filters can be reached | |
* | |
* @access public | |
*/ | |
public function setup_globals() { | |
// What are the auto joinable groups ? | |
$this->group_ids = bp_get_option( 'imath_aj_group_ids', array() ); | |
// Use the filter or directly edit in favor of your language.. | |
// Sorry, this is a quick snippet so i won't handle internationalisation. | |
$this->bulk_label_set = apply_filters( 'imath_aj_bulk_label_set', __( 'Set as Auto-join', 'imath-aj' ) ); | |
$this->bulk_label_unset = apply_filters( 'imath_aj_bulk_label_unset', __( 'Unset as Auto-join', 'imath-aj' ) ); | |
$this->status_label = apply_filters( 'imath_aj_status_label', __( 'Auto-join', 'imath-aj' ) ); | |
} | |
/** | |
* Define hooks ( actions & filters ) | |
* | |
* Using public here so that filters can be reached | |
* | |
* @access private | |
*/ | |
private function setup_hooks() { | |
/** "Create" the UI to define the autojoin groups *****************************/ | |
/** | |
* We will use the Groups Admin Panel within WordPress Administration | |
* | |
* > add a new bulk actions to available ones (by default BuddyPress only have the bulk delete one) | |
* > add an info near the group visibity status (is auto join ?) | |
*/ | |
// Add a bulk action in the groups admin panel to define auto joinable groups. | |
add_filter( 'bp_groups_list_table_get_bulk_actions', array( $this, 'bulk_auto_join' ), 10, 1 ); | |
// Should probably create a new WP List Table column, but, hey! This is a quick snippet ;) | |
// So i'll use the visibility status column of the groups admin panel | |
add_filter( 'bp_groups_admin_get_group_status', array( $this, 'auto_join_status'), 10, 2 ); | |
// Process autojoin / unautojoin | |
add_action( 'bp_groups_admin_load', array( $this, 'handle_bulk_auto_join' ), 10, 1 ); | |
/** Auto join new members *****************************************************/ | |
// Catch user just after activation | |
add_action( 'bp_core_activated_user', array( $this, 'autojoin_user' ), 11, 1 ); | |
} | |
/** | |
* New bulk actions | |
* | |
* @access public | |
* | |
* @param array $bulk_actions list of available bulk actions | |
* @return array same list with our actions added | |
*/ | |
public function bulk_auto_join( $bulk_actions = array() ) { | |
return array_merge( $bulk_actions, array( | |
'autojoin' => $this->bulk_label_set, | |
'unautojoin' => $this->bulk_label_unset | |
) ); | |
} | |
/** | |
* Info to inform the "row" group is autojoin | |
* | |
* @access public | |
* | |
* @param string $group_status visibility of the group | |
* @param array $group the row group | |
* @return string same visibility with our autojoin info added | |
*/ | |
public function auto_join_status( $group_status = '', $group = array() ) { | |
if ( ! empty( $group['id'] ) && in_array( $group['id'], $this->group_ids ) ) { | |
$group_status .= '<br/><span class="attention">' . esc_html( $this->status_label ) . '</span>'; | |
} | |
return $group_status; | |
} | |
/** | |
* Process the bulk actions and gives a feedback to admins | |
* | |
* @access public | |
* | |
* @param string $do_action the bulk actions beeing requested | |
*/ | |
public function handle_bulk_auto_join( $do_action = '' ) { | |
/** Admin Feedback ************************************************************/ | |
// First, let's check the url to see if bulk have been processed | |
if ( ! empty( $_GET['updated'] ) && 'aj' == $_GET['updated'] ) { | |
// Get the result message | |
$feedback = (array) get_transient( '_imath_aj_feedback' ); | |
// This should never happen.. | |
if ( empty( $feedback ) ) { | |
return; | |
} | |
// Delete the feedback transient | |
delete_transient( '_imath_aj_feedback' ); | |
// Init the notice | |
$admin_notice = ''; | |
$feedback_type = array_keys( $feedback ); | |
// Build the message | |
// arhg sorry for internationalisation :( | |
switch ( $feedback_type[0] ) { | |
case 'autojoin' : | |
$admin_notice = sprintf( __( 'Number of groups added to autojoins: %s', 'imath-aj' ), intval( $feedback['autojoin'] ) ); | |
break; | |
case 'unautojoin' : | |
$admin_notice = sprintf( __( 'Number of groups removed from autojoins: %s', 'imath-aj' ), intval( $feedback['unautojoin'] ) ); | |
break; | |
case 'error' : | |
default: | |
$admin_notice = __( 'Something went wrong, please try again later.', 'imath-aj' ); | |
break; | |
} | |
// Using bp_core_add_admin_notice() is convenient ;) | |
if ( ! empty( $admin_notice ) ) { | |
bp_core_add_admin_notice( $admin_notice ); | |
} | |
} | |
/** Bulk actions **************************************************************/ | |
// Bail if not our targetted bulk actions or no group to auto join | |
if ( ! in_array( $do_action, array( 'autojoin', 'unautojoin' ) ) || empty( $_GET['gid'] ) ) { | |
return; | |
} | |
// Global bulk nonce check ( "bulk-" . plural argument of WP_Liste_Table item ) | |
check_admin_referer( 'bulk-groups' ); | |
// Build redirect url | |
$admin_url = admin_url( 'admin.php' ); | |
// BuddyPress is network activated ? | |
if ( bp_core_do_network_admin() ) { | |
$admin_url = network_admin_url( 'admin.php' ); | |
} | |
$redirect = add_query_arg( array( | |
'page' => 'bp-groups', | |
'updated' => 'aj' | |
), $admin_url ); | |
// Init the feedback message type | |
$feedback_message = array( 'error' => 1 ); | |
// Get the group ids | |
$group_ids = wp_parse_id_list( $_GET['gid'] ); | |
// Process | |
if ( ! empty( $group_ids ) ) { | |
foreach ( (array) $group_ids as $key => $group_id ) { | |
// Already in & autojoin action : no need to add the group | |
// unset to have the correct count | |
if ( in_array( $group_id, $this->group_ids ) && 'autojoin' == $do_action ) { | |
unset( $group_ids[ $key ] ); | |
} | |
// Not in & unautojoin action : no need to remove from the group | |
if ( ! in_array( $group_id, $this->group_ids ) && 'unautojoin' == $do_action ) { | |
unset( $group_ids[ $key ] ); | |
} | |
} | |
// Prepare the updated autojoin groups | |
$groups = array(); | |
if ( 'autojoin' == $do_action ) { | |
$groups = array_merge( $this->group_ids, $group_ids ); | |
} else { | |
$groups = array_diff( $this->group_ids, $group_ids ); | |
} | |
if ( bp_update_option( 'imath_aj_group_ids', $groups ) ) { | |
$feedback_message = array( $do_action => count( $group_ids ) ); | |
} else { | |
$feedback_message = array( 'error' => $do_action ); | |
} | |
} | |
// Set a transient for the feedback message | |
set_transient( '_imath_aj_feedback', $feedback_message, 30 ); | |
// Finally safely redirect the admin | |
bp_core_redirect( $redirect ); | |
} | |
/** | |
* Autojoin new member to the groups | |
* | |
* @access public | |
* | |
* @param int $user_id the just activated user id | |
*/ | |
public function autojoin_user( $user_id = 0 ) { | |
// YeeeeAH no user stop! | |
if ( empty( $user_id ) ) { | |
return; | |
} | |
// UhhOuu no autojoin stop! | |
if ( empty( $this->group_ids ) ) { | |
return; | |
} | |
// Before we go.. let's check the groups still exist... | |
$groups = groups_get_groups( array( | |
'include' => (array) $this->group_ids, | |
'show_hidden' => true, | |
'per_page' => false, | |
) ); | |
// Dang autojoin groups have disappeared! | |
if ( empty( $groups['groups'] ) ) { | |
return; | |
} | |
// Now we can safely add the user to groups | |
foreach ( $groups['groups'] as $group ) { | |
groups_join_group( $group->id, $user_id ); | |
} | |
} | |
} | |
endif; // class_exists check | |
// Load the auto join class | |
add_action( 'bp_init', array( 'Imath_Auto_Join_Groups', 'start_groups_autojoin' ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
doesn't work.. :(