Skip to content

Instantly share code, notes, and snippets.

@sbrajesh
Created July 27, 2016 19:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbrajesh/4cab20cbf688fbded8ffcf18fc5c0290 to your computer and use it in GitHub Desktop.
Save sbrajesh/4cab20cbf688fbded8ffcf18fc5c0290 to your computer and use it in GitHub Desktop.
group open close example for Jaume
//do not let the activity be posted by any means(do not allow from directory etc)
function bd_check_post_update() {
$item_id = 0;
$object = '';
// Try to get the item id from posted variables.
if ( ! empty( $_POST['item_id'] ) ) {
$item_id = (int) $_POST['item_id'];
}
// Try to get the object from posted variables.
if ( ! empty( $_POST['object'] ) ) {
$object = sanitize_key( $_POST['object'] );
// If the object is not set and we're in a group, set the item id and the object
} elseif ( bp_is_group() ) {
$item_id = bp_get_current_group_id();
$object = 'groups';
}
if ( $object =='groups' && bd_is_group_closed( $item_id ) ) {
exit( '-1<div id="message" class="error bp-ajax-message"><p>' . __( 'This group is closed.', 'buddypress' ) . '</p></div>' );
}
//let the default one handle posting
}
add_action( 'wp_ajax_post_update', 'bd_check_post_update' );
//filter the activity commenting capability
function bd_check_comment_status( $can_comment ){
global $activities_template;
$activity = $activities_template->activity;
if ( bd_is_group_activity_closed( $activity->id ) ) {
$can_comment = false;
}
return $can_comment;
}
add_filter( 'bp_activity_can_comment', 'bd_check_comment_status' );
//filter activity comment reply capability
function bd_check_comment_reply_status( $can_comment, $comment ){
global $activities_template;
$activity = $activities_template->activity;
if ( bd_is_group_activity_closed( $activity->id ) ) {
$can_comment = false;
}
return $can_comment;
}
add_filter( 'bp_activity_can_comment_reply', 'bd_check_comment_reply_status', 10, 2 );
//Hide post from on Group home page?
function bd_maybe_hide_form_start() {
if ( bd_is_group_closed() ) {
ob_start();//start buffer only if closed
}
}
add_action( 'bp_before_group_activity_post_form', 'bd_maybe_hide_form_start' );
//end code to hide form
function bd_maybe_hide_form_end() {
if ( bd_is_group_closed() ) {
ob_end_clean();
}
}
add_action( 'bp_after_group_activity_post_form', 'bd_maybe_hide_form_end' );
/**
* Is group closed?
* @param bool $group_id
*
* @return bool
*/
function bd_is_group_closed( $group_id = false ) {
if ( ! $group_id ) {
$group = groups_get_current_group();
if ( ! empty( $group ) ) {
$group_id = $group->id;
}
}
if ( ! $group_id ) {
return false;
}
//check for group settings
$is_enabled = groups_get_groupmeta( $group_id, '_is_group_closed', true );
return $is_enabled == 'yes';
}
/**
* Is this activity a group activity and is gropup closed?
*
* @param int $activity_id
*
* @return bool
*/
function bd_is_group_activity_closed( $activity_id = null ) {
$activity = new BP_Activity_Activity( $activity_id );
if ( $activity->component == 'groups' && bd_is_group_closed( $activity->item_id ) ) {
return true;
}
return false;
}
/**
* Add the option on Group->Manage->settings screen,
*/
function bd_group_open_close_form() {
?>
<div class="checkbox bd-opne-close-group">
<label><input type="checkbox" name="_is_group_closed" id="_is_group_closed" value="yes" <?php echo checked( true, bd_is_group_closed() );?>/> <?php _e( 'Archive Group' ) ?></label>
</div>
<?php
}
add_action( 'bp_before_group_settings_admin', 'bd_group_open_close_form' );
/**
* Save poreference on update of the gropup
* @param $group_id
*/
function bd_group_save_preference( $group_id ) {
$enabled= isset( $_POST['_is_group_closed'] ) ? $_POST['_is_group_closed'] : 'no';
if ( $enabled != 'yes' && $enabled != 'no' ) {//invalid value
$enabled = 'no';//set it to no
}
groups_update_groupmeta( $group_id, '_is_group_closed', $enabled );
}
add_action( 'groups_group_settings_edited', 'bd_group_save_preference' );
add_action( 'groups_update_group', 'bd_group_save_preference' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment