Created
November 4, 2013 03:39
-
-
Save imath/7297764 to your computer and use it in GitHub Desktop.
This is the complete class for the BuddyPress Codex article : Group Meta Queries: Usage Example.
You can test it copying it in the functions.php of your active theme
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 | |
/* you can copy & paste from here */ | |
//it's important to check the BP_Group_Extension is available | |
if( class_exists( 'BP_Group_Extension' ) ) : | |
/** | |
* This is a quick and dirty class to illustrate "bpgmq" | |
* bpgmq stands for BuddyPress Group Meta Query... | |
* The goal is to store a groupmeta in order to let the community administrator | |
* feature a group. | |
* Featured groups will be filterable from the groups directory thanks to a new option | |
* and to a filter applied on bp_ajax_query_string() | |
* | |
* This class is an example, it would be much better to use the group extension API | |
*/ | |
class bpgmq_feature_group { | |
public function __construct() { | |
$this->setup_hooks(); | |
} | |
private function setup_hooks() { | |
// in Group Admin UI, you add a new metabox to display a checkbox to featured the displayed group | |
add_action( 'bp_groups_admin_meta_boxes', array( $this, 'admin_ui_edit_featured' ) ); | |
// Once the group is saved you store a groupmeta in db, the one you will search for in your group meta query | |
add_action( 'bp_group_admin_edit_after', array( $this, 'admin_ui_save_featured'), 10, 1 ); | |
/* The groups loop uses bp_ajax_querystring( 'groups' ) to filter the groups | |
depending on the selected option */ | |
add_filter( 'bp_ajax_querystring', array( $this, 'filter_ajax_querystring' ), 20, 2 ); | |
/* finally you create your options in the different select boxes */ | |
// you need to do it for the group directory | |
add_action( 'bp_groups_directory_order_options', array( $this, 'featured_option' ) ); | |
// and for the groups tab of the user's profile | |
add_action( 'bp_member_group_order_options', array( $this, 'featured_option' ) ); | |
} | |
/** | |
* registers a new metabox in Group Admin UI, edit group panel | |
* @uses add_meta_box() to register our meta box | |
*/ | |
public function admin_ui_edit_featured() { | |
add_meta_box( | |
'bpgmq_feature_group_mb', | |
__( 'Featured Group' ), | |
array( &$this, 'admin_ui_metabox_featured'), | |
get_current_screen()->id, | |
'side', | |
'core' | |
); | |
} | |
/** | |
* Displays the meta box | |
* @param BP_Groups_Group $item the group being edited | |
* @uses groups_get_groupmeta() to get the featured attribute of the group | |
* @uses checked() to eventually add a checked attribute if the group is featured | |
* @uses wp_nonce_field() for security reasons | |
*/ | |
public function admin_ui_metabox_featured( $item = false ) { | |
if( empty( $item ) ) | |
return; | |
// Using groups_get_groupmeta to check if the group is featured | |
$is_featured = groups_get_groupmeta( $item->id, '_bpgmq_featured_group' ); | |
?> | |
<p> | |
<input type="checkbox" id="bpgmq-featured-cb" name="bpgmq-featured-cb" value="1" <?php checked( 1, $is_featured );?>> <?php _e( 'Mark this group as featured' );?> | |
</p> | |
<?php | |
wp_nonce_field( 'bpgmq_featured_save_' . $item->id, 'bpgmq_featured_admin' ); | |
} | |
function admin_ui_save_featured( $group_id = 0 ) { | |
if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) || empty( $group_id ) ) | |
return false; | |
check_admin_referer( 'bpgmq_featured_save_' . $group_id, 'bpgmq_featured_admin' ); | |
// You need to check if the group was featured so that you can eventually delete the group meta | |
$was_featured = groups_get_groupmeta( $group_id, '_bpgmq_featured_group' ); | |
$to_feature = !empty( $_POST['bpgmq-featured-cb'] ) ? true : false; | |
if( !empty( $to_feature ) && empty( $was_featured ) ) | |
groups_update_groupmeta( $group_id, '_bpgmq_featured_group', 1 ); | |
if( empty( $to_feature ) && !empty( $was_featured ) ) | |
groups_delete_groupmeta( $group_id, '_bpgmq_featured_group' ); | |
} | |
public function filter_ajax_querystring( $querystring = '', $object = '' ) { | |
/* bp_ajax_querystring is also used by other components, so you need | |
to check the object is groups, else simply return the querystring and stop the process */ | |
if( $object != 'groups' ) | |
return $querystring; | |
// Let's rebuild the querystring as an array to ease the job | |
$defaults = array( | |
'type' => 'active', | |
'action' => 'active', | |
'scope' => 'all', | |
'page' => 1, | |
'user_id' => 0, | |
'search_terms' => '', | |
'exclude' => false, | |
); | |
$bpgmq_querystring = wp_parse_args( $querystring, $defaults ); | |
/* if your featured option has not been requested | |
simply return the querystring to stop the process | |
*/ | |
if( $bpgmq_querystring['type'] != 'featured' ) | |
return $querystring; | |
/* this is your meta_query */ | |
$bpgmq_querystring['meta_query'] = array( | |
array( | |
'key' => '_bpgmq_featured_group', | |
'value' => 1, | |
'type' => 'numeric', | |
'compare' => '=' | |
) | |
); | |
// using a filter will help other plugins to eventually extend this feature | |
return apply_filters( 'bpgmq_filter_ajax_querystring', $bpgmq_querystring, $querystring ); | |
} | |
public function featured_option() { | |
?> | |
<option value="featured"><?php _e( 'Featured' ); ?></option> | |
<?php | |
} | |
} | |
/** | |
* Let's launch ! | |
* | |
* Using bp_is_active() in this case is not needed | |
* But i think it's a good practice to use this kind of check | |
* just in case :) | |
* | |
* @uses bp_is_active([component]) to check the group component is active | |
*/ | |
function bpgmq_feature_group() { | |
if( bp_is_active( 'groups') ) | |
return new BPGMQ_Feature_Group(); | |
} | |
add_action( 'bp_init', 'bpgmq_feature_group' ); | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there a way to turn this into a plugin? I tied adding the standard header, but when activated, the feature didn't work like it does in the functions.php.