/bp-group-extension.php Secret
Last active
December 31, 2015 03:39
Star
You must be signed in to star a gist
BP_Group_Extension
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 | |
/** | |
* Extend BuddyPress Groups to add additional pages to the Groups pages | |
* | |
* @since 1.0 | |
*/ | |
if ( class_exists( 'BP_Group_Extension' ) ) : | |
class My_Custom_Group_Files_Extension extends BP_Group_Extension { | |
/** | |
* Here you can see more customization of the config options | |
*/ | |
function __construct() { | |
$args = array( | |
'slug' => 'files', | |
'name' => __( 'Files', 'textdomain' ), | |
'screens' => array( | |
'edit' => array( | |
'name' => __( 'Files', 'textdomain' ) | |
), | |
'create' => array( | |
'position' => 100, | |
), | |
), | |
); | |
parent::init( $args ); | |
} | |
function display() { | |
// Get the Group ID | |
$group_id = bp_get_group_ID(); | |
// Get our post ID | |
$post_id = groups_get_groupmeta( $group_id, '_my_custom_post_id' ); | |
// Get our group files | |
$group_files = get_post_meta( $post_id, '_my_custom_group_files', true ); | |
echo '<div id="files-display">'; | |
if ( $group_files ) { | |
printf( __( '<h4 class="files-name">%s</h4>', 'textdomain' ), 'Group Files' ); | |
echo '<ul id="file-list">'; | |
foreach ( $group_files as $file ) { | |
echo '<li>'; | |
// Strip the file name from our full URL for a nicer display | |
$parts = explode( '/', $file ); | |
for ( $i = 0; $i < count( $parts ); ++$i ) { | |
$title = $parts[$i]; | |
} | |
echo __( 'File: ', 'textdomain' ) . '<strong>' . $title . '</strong> (<a href="' . $file . '">' . __( 'Download', 'textdomain' ) . '</a>)'; | |
echo '</li>'; | |
} | |
echo '</ul>'; | |
} else { | |
printf( __( '<h4 class="files-name">%s</h4>', 'textdomain' ), 'No Files Uploaded' ); | |
} | |
echo '</div>'; | |
} | |
function settings_screen( $group_id ) { | |
$post_id = groups_get_groupmeta( $group_id, '_my_custom_post_id' ); | |
$setting = get_post_meta( $post_id, '_my_custom_group_files', true ); | |
$meta_boxes = my_custom_group_files_metabox(); | |
$meta_box = isset( $meta_boxes['group_files'] ) ? $meta_boxes['group_files'] : false; | |
if ( function_exists( 'cmb_metabox_form' ) ) { | |
add_filter( 'cmb_frontend_form_format', array( $this, 'remove_form_markup' ), 10, 4 ); | |
cmb_metabox_form( $meta_box, $post_id ); | |
} | |
} | |
function settings_screen_save( $group_id ) { | |
$post_id = groups_get_groupmeta( $group_id, '_my_custom_post_id' ); | |
$setting = isset( $_POST['_my_custom_group_files'] ) ? $_POST['_my_custom_group_files'] : ''; | |
update_post_meta( $post_id, '_my_custom_group_files', $setting ); | |
} | |
function remove_form_markup( $format, $object_id, $meta_box, $form ) { | |
if ( isset( $meta_box['id'] ) && $meta_box['id'] == 'group_fields' ) { | |
return '<div class="cmb-form-wrap" id="%s"><input type="hidden" name="object_id" value="%s">%s<input type="submit" name="submit-cmb" value="%s"></div>'; | |
} | |
return $format; | |
} | |
} | |
class My_Custom_Group_News_Extension extends BP_Group_Extension { | |
/** | |
* Here you can see more customization of the config options | |
*/ | |
function __construct() { | |
$args = array( | |
'slug' => 'news', | |
'name' => __( 'News', 'textdomain' ), | |
'screens' => array( | |
'edit' => array( | |
'name' => __( 'News', 'textdomain' ) | |
), | |
'create' => array( | |
'position' => 100, | |
), | |
), | |
); | |
parent::init( $args ); | |
} | |
function display() { | |
// Get the Group ID | |
$group_id = bp_get_group_ID(); | |
// Get our post ID | |
$post_id = groups_get_groupmeta( $group_id, '_my_custom_post_id' ); | |
// Get our group files | |
$group_files = get_post_meta( $post_id, '_my_custom_group_news', true ); | |
echo '<div id="files-display">'; | |
if ( $group_files ) { | |
printf( __( '<h4 class="files-name">%s</h4>', 'textdomain' ), 'Group Files' ); | |
echo '<ul id="file-list">'; | |
foreach ( $group_files as $file ) { | |
echo '<li>'; | |
// Strip the file name from our full URL for a nicer display | |
$parts = explode( '/', $file ); | |
for ( $i = 0; $i < count( $parts ); ++$i ) { | |
$title = $parts[$i]; | |
} | |
echo __( 'File: ', 'textdomain' ) . '<strong>' . $title . '</strong> (<a href="' . $file . '">' . __( 'Download', 'textdomain' ) . '</a>)'; | |
echo '</li>'; | |
} | |
echo '</ul>'; | |
} else { | |
printf( __( '<h4 class="files-name">%s</h4>', 'textdomain' ), 'No Files Uploaded' ); | |
} | |
echo '</div>'; | |
} | |
function settings_screen( $group_id ) { | |
$post_id = groups_get_groupmeta( $group_id, '_my_custom_post_id' ); | |
$setting = get_post_meta( $post_id, '_my_custom_group_news', true ); | |
$meta_boxes = my_custom_group_files_metabox(); | |
$meta_box = isset( $meta_boxes['group_news'] ) ? $meta_boxes['group_news'] : false; | |
if ( function_exists( 'cmb_metabox_form' ) ) { | |
add_filter( 'cmb_frontend_form_format', array( $this, 'remove_form_markup' ), 10, 4 ); | |
cmb_metabox_form( $meta_box, $post_id ); | |
} | |
} | |
function settings_screen_save( $group_id ) { | |
$post_id = groups_get_groupmeta( $group_id, '_my_custom_post_id' ); | |
$setting = isset( $_POST['_my_custom_group_news'] ) ? $_POST['_my_custom_group_news'] : ''; | |
update_post_meta( $post_id, '_my_custom_group_news', $setting ); | |
} | |
function remove_form_markup( $format, $object_id, $meta_box, $form ) { | |
if ( isset( $meta_box['id'] ) && $meta_box['id'] == 'group_fields' ) { | |
return '<div class="cmb-form-wrap" id="%s"><input type="hidden" name="object_id" value="%s">%s<input type="submit" name="submit-cmb" value="%s"></div>'; | |
} | |
return $format; | |
} | |
} | |
} | |
endif; | |
add_action( 'init', 'activate_extension' ); | |
function activate_extension() { | |
global $bp; | |
if( bp_has_groups() ) { | |
// Grab our current group ID | |
bp_the_group(); | |
$group_id = bp_get_group_ID(); | |
// Build an array of all of our class names | |
$extensions = array( 'My_Custom_Group_Files_Extension', 'My_Custom_Group_News_Extension' ); | |
// Register our extensions | |
foreach ( $extensions as $extension ) { | |
if ( $group_id == 1 ) { | |
$extension = new $extension; | |
add_action( 'wp', array( $extension, '_register' ), 2 ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment