<?php
/*
Plugin Name: BuddyPress Group Tags
Plugin URI: http://wordpress.org/extend/plugins/buddypress-group-tags/
Description: This plugin allow Groups to be organized by tags via a tag cloud
Version: 1.2.2
Revision Date: June 5, 2010
License: GNU General Public License 2.0 (GPL) http://www.gnu.org/licenses/gpl.html
Author: Deryk Wenaus, updated for wp 3.0 by Nit3watch and r-a-y at buddypress.org
Author URI: http://www.bluemandala.com
*/
/*
TODO: Get the axaj tag selector to work
*/
/* CONFIG VARIABLES */
/* Show the tag cloud above the group listings */
$gtags_show_cloud = true ;
/* Show group tags in group directory view. */
$gtags_show_tags_in_directory = true ;
/* Show group tags in group header */
$gtags_show_tags_in_header = true ;
/* Edit this array in order to change the tag could appearance
see http://codex.wordpress.org/Template_Tags/wp_tag_cloud for details */
$gtags_args = array( 'smallest' => 8, 'largest' => 22, 'number' => 45, 'orderby' => 'name', 'order' => 'ASC' );
/* how many popular tags to show in the tag editing and creation form */
$popular_tag_limit = 30;
/* - HERE BEGINS THE CODE - */
// load the javascript for the group tag clicks to work
function gtags_enqueue() {
wp_enqueue_script('gtags=group-tags', WP_PLUGIN_URL.'/buddypress-group-tags/group-tags.js');
load_plugin_textdomain( 'gtags', false, dirname( plugin_basename( __FILE__ ) ).'/lang' );
}
add_action('init', 'gtags_enqueue');
//add css
function gtags_header() {
echo '<link rel="stylesheet" type="text/css" href="'.WP_PLUGIN_URL.'/buddypress-group-tags/group-tags.css" media="screen" />'."\n";
}
add_action('wp_head', 'gtags_header');
// this catches the ajax call, and loads the groups template
function gtags_ajax() {
locate_template( array( "groups/groups-loop.php" ), true );
}
add_action( 'wp_ajax_gtags', 'gtags_ajax' );
// hook into group listing function, output the groups that match a given tag
function gtags_show_groups_for_tag( $groups ) {
global $bp, $groups_template;
//echo '<pre>'; print_r( $_POST ); echo '</pre>';
if ( $_POST['action'] == 'groups_filter' )
return $groups;
if ( $_POST['tag'] )
$tag = urldecode( $_POST['tag'] ); // this is what ajax sends if we are in group directory
else if ( $bp->current_action == 'tag' )
$tag = urldecode( $bp->action_variables[0] ); // this is for the widget from all other places
if ( $tag ) {
echo '<div id="gtags-results">'.__('Results for tag', 'gtags').': <b>' . $tag . '</b></div>';
$gtags_groups = gtags_get_groups_by_tag( null, null, false, false, $tag );
$groups_template->groups = $gtags_groups[groups];
// turn off pagination
$groups_template->group_count = $gtags_groups[total];
$groups_template->total_group_count = $gtags_groups[total];
$groups_template->pag_num = $gtags_groups[total];
$groups_template->pag_page = 1;
$groups_template->pag_links = '';
$groups = $gtags_groups;
}
//echo '<pre>'; print_r( $bp->current_action ); echo '</pre>';
//echo '<pre>'; print_r( $bp->action_variables[0] ); echo '</pre>';
return $groups;
}
add_filter( 'bp_has_groups', 'gtags_show_groups_for_tag' );
// Return an array of the groups for a specific tag (plus number of groups)
// this is a modified copy of many similar functions found in bp-groups-classes.php
function gtags_get_groups_by_tag( $limit = null, $page = null, $user_id = false, $search_terms = false, $group_tag = null ) {
global $wpdb, $bp;
if ( $limit && $page ) {
$pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
}
if ( !is_user_logged_in() || ( !is_site_admin() && ( $user_id != $bp->loggedin_user->id ) ) )
$hidden_sql = "AND g.status != 'hidden'";
if ( $search_terms ) {
$search_terms = like_escape( $wpdb->escape( $search_terms ) );
$search_sql = " AND ( g.name LIKE '%%{$search_terms}%%' OR g.description LIKE '%%{$search_terms}%%' )";
}
if ( $group_tag ) {
$group_tag = like_escape( $wpdb->escape( $group_tag ) );
$tag_sql = " AND ( gm3.meta_value LIKE '%%{$group_tag}%%' )";
}
$paged_groups = $wpdb->get_results( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity, gm3.meta_value as gtags_group_tags FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_groupmeta} gm3, {$bp->groups->table_name} g WHERE g.id = gm1.group_id AND g.id = gm2.group_id AND g.id = gm3.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' AND gm3.meta_key = 'gtags_group_tags' {$hidden_sql} {$search_sql} {$tag_sql} ORDER BY CONVERT(gm1.meta_value, SIGNED) DESC {$pag_sql}" );
// this is commented out because it doesn't really work due to the over-inclusive issue.
//$total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT g.id) FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_groupmeta} gm3, {$bp->groups->table_name} g WHERE g.id = gm1.group_id AND g.id = gm2.group_id AND g.id = gm3.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' AND gm3.meta_key = 'gtags_group_tags' {$hidden_sql} {$search_sql} {$tag_sql}" );
// loop through results and return only exact matches in comma separated list.
$paged_groups2 = array();
foreach ( (array)$paged_groups as $group ) {
$items = explode( ",", $group->gtags_group_tags );
$match = false;
foreach( $items as $item ) {
if ( trim( strtolower( $item ) ) == strtolower( $group_tag ) ) $match = true;
}
if ( $match == true ) $paged_groups2[] = $group;
}
$total_groups = count($paged_groups2); // in place of the commented out code above
//echo "<pre>"; print_r($paged_groups2); echo "</pre>";
foreach ( (array)$paged_groups2 as $group ) $group_ids[] = $group->id;
$group_ids = $wpdb->escape( join( ',', (array)$group_ids ) );
$paged_groups2 = BP_Groups_Group::get_group_extras( &$paged_groups2, $group_ids, 'popular' );
return array( 'groups' => $paged_groups2, 'total' => $total_groups );
}
// Return an array with tag objects
function gtags_make_tags( $urlencode=false ) {
global $bp, $wpdb, $gtags_args;
$all_group_tags = $wpdb->get_col( $wpdb->prepare(
"SELECT meta_value FROM " . $bp->groups->table_name_groupmeta . " WHERE meta_key = 'gtags_group_tags' " ) );
//count the occurances
$all_tags = array();
foreach( $all_group_tags as $group_tags ) {
$items = explode( ",", $group_tags );
foreach( $items as $item ) {
$item = trim( strtolower( $item ) );
if ($item=='') continue;
if ( isset( $all_tags[ $item ] ) ) {
$all_tags[ $item ] += 1;
} else {
$all_tags[ $item ] = 1;
}
}
}
$tags = array(); $i=0;
foreach( $all_tags as $tag => $count ) {
$tag = stripcslashes( $tag );
$link = $bp->root_domain . '/' . BP_GROUPS_SLUG . '/tag/' . urlencode( $tag ) ;
$tags[$i] = (object)array( 'name' => $tag, 'count' => $count, 'link' => $link, 'id' => $i );
++$i;
}
//echo "<pre>"; print_r($tags); echo "</pre>";
return $tags;
}
// show the tags above the group directory
function gtags_display_tags() {
global$gtags_show_cloud;
if ( $gtags_show_cloud ){
global $gtags_args;
echo '<div id="gtags-top" class="gtags">';
echo wp_generate_tag_cloud( gtags_make_tags(), $gtags_args ); // use the built in wordrpess tag function :)
echo '</div>';
}
global $bp;
}
add_action( 'bp_before_directory_groups_content', 'gtags_display_tags' );
// create the form to add tags
function gtags_add_tags_form() {
global $show_group_add_form;
if ($show_group_add_form)
return;
$show_group_add_form = true;
?>
<label for="group-tags"><?php _e( 'Resturant Tags', 'gtags' ) ?></label>
<input type="text" name="group-tags" id="group-tags" value="<?php gtags_group_tags() ?>" /><br>
<i><?php _e('Separate tags with commas', 'gtags'); ?></i><br>
<?php
gtags_show_tags_chooser();
}
add_action( 'groups_custom_group_fields_editable', 'gtags_add_tags_form' );
// create easy tag adding links. tags added to form via javascript
function gtags_show_tags_chooser(){
?>
<div class="tags_chooser"><?php _e('Common Tags', 'gtags'); ?>: <?php echo gtags_show_tags_in_add_form(); ?></div>
<?php
}
// show list of the most popular tags - used when adding tags
function gtags_show_tags_in_add_form() {
global $popular_tag_limit;
$tags = gtags_make_tags();
uasort( $tags, create_function('$a, $b', 'return ($b->count > $a->count);') );
foreach ( $tags as $tag ) {
echo ' <span class="gtags-add">' . $tag->name .'</span>';
//$sep = ' ';
++$i;
if ( $i >= $popular_tag_limit ) break;
}
}
// Save the tag values in the group meta - perhaps use serialize() and maybe_unserialize()
function gtags_save_tags( $group_id ) {
global $bp;
if($bp->groups->new_group_id)
$id = $bp->groups->new_group_id;
else
$id = $group_id;
if ( $_POST['group-tags'] )
groups_update_groupmeta( $id, 'gtags_group_tags', $_POST['group-tags'] );
}
add_action( 'groups_create_group_step_save_group-details', 'gtags_save_tags' );
add_action( 'groups_details_updated', 'gtags_save_tags' );
// Get or return the tag values
function gtags_group_tags() {
echo gtags_get_group_tags();
}
function gtags_get_group_tags( $group = false ) {
global $groups_template;
if ( !$group )
$group =& $groups_template->group;
$group_tags = groups_get_groupmeta( $group->id, 'gtags_group_tags' );
$group_tags = stripcslashes( $group_tags );
return apply_filters( 'gtags_get_group_tags', $group_tags );
}
// show tags in group directory listing
function gtags_show_tags_in_directory() {
global $gtags_show_tags_in_directory;
if ( gtags_get_group_tags() && $gtags_show_tags_in_directory ) {
?>
<div class="item-desc item-tags"><?php _e('Tags', 'gtags'); ?>: <?php echo gtags_make_tags_for_group() ?></div>
<?php
}
}
add_action( 'bp_directory_groups_item', 'gtags_show_tags_in_directory' );
// show tags in group header
function gtags_show_tags_in_header( $description ) {
global $gtags_show_tags_in_header;
if ( gtags_get_group_tags() && $gtags_show_tags_in_header ) {
$description .= '<div class="gtags-header">'. __('Tags', 'gtags').': '. gtags_make_tags_for_group().'</div>';
}
return $description;
}
add_filter( 'bp_get_group_description', 'gtags_show_tags_in_header' );
// show tags for an individual group with links
function gtags_make_tags_for_group() {
global $bp, $wpdb, $gtags_args;
$group_tags = gtags_get_group_tags();
$items = explode( ",", $group_tags );
foreach( $items as $item ) {
$item = trim( strtolower( $item ) );
if ($item=='') continue;
$link = $bp->root_domain . '/' . BP_GROUPS_SLUG . '/tag/' . urlencode( $item );
$output .= ' <a href="'.$link.'">'.$item.'</a> ';
}
return $output;
}
// add a happy group tag widget
function gtags_group_tags_widget() {
global $gtags_args;
?>
<h3 class="widgettitle"><?php _e('Group Tags', 'gtags'); ?></h3>
<div class="gtags">
<?php
echo wp_generate_tag_cloud( gtags_make_tags(), $gtags_args );
?>
</div>
<?php
}
function gtags_widget_init() {
register_sidebar_widget(__('Group Tags'), 'gtags_group_tags_widget');
}
add_action("plugins_loaded", "gtags_widget_init");
function bp_gtags_setup_globals() {
global $bp;
$bp->gtags->id = 'gtags';
$bp->gtags->slug = 'tag';
}
add_action( 'bp_setup_globals', 'bp_gtags_setup_globals' );
// this gets called as a hook, but never shows on the page due to low precedence level - strange but works
// it does however show up on /members/admin/groups/ so I hacked it so the name of the sub nav item does not show.
function bp_gtags_setup_nav() {
global $bp;
bp_core_new_subnav_item( array( 'name' => 'Tags', 'slug' => $bp->gtags->slug, 'parent_slug' => BP_GROUPS_SLUG, 'parent_url' => $bp->root_domain .'/'. BP_GROUPS_SLUG . '/', 'screen_function' => 'gtags_display_hook', 'position' => 1 ) );
}
add_action( 'bp_setup_nav', 'bp_gtags_setup_nav', 1 );
function gtags_display_hook() {
global $bp;
add_action( 'bp_has_groups', 'gtags_show_groups_for_tag' );
bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'groups/index' ) );
}
function mybp(){
global $bp;
//echo '<pre>'; print_r( $bp ); echo '</pre>';
}
//add_action('bp_before_footer','mybp');
?>