Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jul 10th, 2010  |  syntax: None  |  size: 12.31 KB  |  views: 165  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3. Plugin Name: BuddyPress Group Tags
  4. Plugin URI: http://wordpress.org/extend/plugins/buddypress-group-tags/
  5. Description: This plugin allow Groups to be organized by tags via a tag cloud
  6. Version: 1.2.2
  7. Revision Date: June 5, 2010
  8. License: GNU General Public License 2.0 (GPL) http://www.gnu.org/licenses/gpl.html
  9. Author: Deryk Wenaus, updated for wp 3.0 by Nit3watch and r-a-y at buddypress.org
  10. Author URI: http://www.bluemandala.com
  11. */
  12.  
  13. /*
  14. TODO:   Get the axaj tag selector to work
  15. */             
  16.  
  17.  
  18. /* CONFIG VARIABLES */
  19.  
  20. /* Show the tag cloud above the group listings */
  21. $gtags_show_cloud = true ;
  22.  
  23. /* Show group tags in group directory view. */
  24. $gtags_show_tags_in_directory = true ;
  25.  
  26. /* Show group tags in group header */
  27. $gtags_show_tags_in_header = true ;
  28.  
  29. /* Edit this array in order to change the tag could appearance
  30.    see http://codex.wordpress.org/Template_Tags/wp_tag_cloud for details */
  31. $gtags_args = array( 'smallest' => 8, 'largest' => 22, 'number' => 45, 'orderby' => 'name', 'order' => 'ASC' );
  32.  
  33. /* how many popular tags to show in the tag editing and creation form */
  34. $popular_tag_limit = 30;
  35.  
  36.  
  37.  
  38. /* - HERE BEGINS THE CODE - */
  39.  
  40.  
  41. // load the javascript for the group tag clicks to work
  42. function gtags_enqueue() {
  43.         wp_enqueue_script('gtags=group-tags', WP_PLUGIN_URL.'/buddypress-group-tags/group-tags.js');
  44.         load_plugin_textdomain( 'gtags', false, dirname( plugin_basename( __FILE__ ) ).'/lang'  );
  45. }
  46. add_action('init', 'gtags_enqueue');
  47.  
  48. //add css
  49. function gtags_header() {
  50.         echo '<link rel="stylesheet" type="text/css" href="'.WP_PLUGIN_URL.'/buddypress-group-tags/group-tags.css" media="screen" />'."\n";
  51. }
  52. add_action('wp_head', 'gtags_header');
  53.  
  54.  
  55. // this catches the ajax call, and loads the groups template
  56. function gtags_ajax() {
  57.         locate_template( array( "groups/groups-loop.php" ), true );
  58. }
  59. add_action( 'wp_ajax_gtags', 'gtags_ajax' );
  60.  
  61.  
  62. // hook into group listing function, output the groups that match a given tag
  63. function gtags_show_groups_for_tag( $groups ) {
  64.         global $bp, $groups_template;
  65.        
  66.         //echo '<pre>'; print_r( $_POST ); echo '</pre>';
  67.         if ( $_POST['action'] == 'groups_filter' )
  68.                 return $groups;
  69.                
  70.         if ( $_POST['tag'] )
  71.                 $tag = urldecode( $_POST['tag'] ); // this is what ajax sends if we are in group directory
  72.         else if ( $bp->current_action == 'tag' )
  73.                 $tag = urldecode( $bp->action_variables[0] ); // this is for the widget from all other places
  74.                        
  75.         if ( $tag ) {
  76.                 echo '<div id="gtags-results">'.__('Results for tag', 'gtags').': <b>' . $tag . '</b></div>';
  77.                 $gtags_groups = gtags_get_groups_by_tag( null, null, false, false, $tag );
  78.                 $groups_template->groups = $gtags_groups[groups];
  79.                 // turn off pagination
  80.                 $groups_template->group_count = $gtags_groups[total];
  81.                 $groups_template->total_group_count = $gtags_groups[total];
  82.                 $groups_template->pag_num = $gtags_groups[total];
  83.                 $groups_template->pag_page = 1;
  84.                 $groups_template->pag_links = '';
  85.                 $groups = $gtags_groups;
  86.         }
  87.        
  88.         //echo '<pre>'; print_r( $bp->current_action ); echo '</pre>';  
  89.         //echo '<pre>'; print_r( $bp->action_variables[0] ); echo '</pre>';
  90.  
  91.         return $groups;
  92. }
  93. add_filter( 'bp_has_groups', 'gtags_show_groups_for_tag' );
  94.  
  95.  
  96. // Return an array of the groups for a specific tag (plus number of groups)
  97. // this is a modified copy of many similar functions found in bp-groups-classes.php
  98. function gtags_get_groups_by_tag( $limit = null, $page = null, $user_id = false, $search_terms = false, $group_tag = null ) {
  99.         global $wpdb, $bp;
  100.        
  101.         if ( $limit && $page ) {
  102.                 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
  103.         }
  104.  
  105.         if ( !is_user_logged_in() || ( !is_site_admin() && ( $user_id != $bp->loggedin_user->id ) ) )
  106.                 $hidden_sql = "AND g.status != 'hidden'";
  107.  
  108.         if ( $search_terms ) {
  109.                 $search_terms = like_escape( $wpdb->escape( $search_terms ) );
  110.                 $search_sql = " AND ( g.name LIKE '%%{$search_terms}%%' OR g.description LIKE '%%{$search_terms}%%' )";
  111.         }
  112.  
  113.         if ( $group_tag ) {
  114.                 $group_tag = like_escape( $wpdb->escape( $group_tag ) );
  115.                 $tag_sql = " AND ( gm3.meta_value LIKE '%%{$group_tag}%%' )";
  116.         }
  117.        
  118.         $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}" );
  119.         // this is commented out because it doesn't really work due to the over-inclusive issue.
  120.         //$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}" );
  121.        
  122.         // loop through results and return only exact matches in comma separated list.
  123.         $paged_groups2 = array();
  124.         foreach ( (array)$paged_groups as $group ) {
  125.                 $items = explode( ",", $group->gtags_group_tags );
  126.                 $match = false;
  127.                 foreach( $items as $item ) {
  128.                         if ( trim( strtolower( $item ) ) == strtolower( $group_tag ) ) $match = true;
  129.                 }
  130.                 if ( $match == true ) $paged_groups2[] = $group;
  131.         }
  132.        
  133.         $total_groups = count($paged_groups2);  // in place of the commented out code above
  134.         //echo "<pre>"; print_r($paged_groups2); echo "</pre>";
  135.  
  136.         foreach ( (array)$paged_groups2 as $group ) $group_ids[] = $group->id;
  137.         $group_ids = $wpdb->escape( join( ',', (array)$group_ids ) );
  138.         $paged_groups2 = BP_Groups_Group::get_group_extras( &$paged_groups2, $group_ids, 'popular' );
  139.  
  140.         return array( 'groups' => $paged_groups2, 'total' => $total_groups );
  141. }
  142.  
  143.  
  144. // Return an array with tag objects
  145. function gtags_make_tags( $urlencode=false ) {
  146.         global $bp, $wpdb, $gtags_args;
  147.         $all_group_tags = $wpdb->get_col( $wpdb->prepare(
  148.                 "SELECT meta_value FROM " . $bp->groups->table_name_groupmeta . " WHERE meta_key = 'gtags_group_tags' " ) );
  149.         //count the occurances 
  150.         $all_tags = array();   
  151.         foreach( $all_group_tags as $group_tags ) {
  152.                 $items = explode( ",", $group_tags );
  153.                 foreach( $items as $item ) {
  154.                         $item = trim( strtolower( $item ) );
  155.                         if ($item=='') continue;
  156.                         if ( isset( $all_tags[ $item ] ) ) {
  157.                                 $all_tags[ $item ] += 1;
  158.                         } else {
  159.                                 $all_tags[ $item ] = 1;
  160.                         }
  161.                 }
  162.         }      
  163.         $tags = array(); $i=0;
  164.         foreach( $all_tags as $tag => $count ) {
  165.                 $tag = stripcslashes( $tag );
  166.                 $link = $bp->root_domain . '/' . BP_GROUPS_SLUG . '/tag/' . urlencode( $tag ) ;
  167.                 $tags[$i] = (object)array( 'name' => $tag, 'count' => $count, 'link' => $link, 'id' => $i );
  168.                 ++$i;
  169.         }
  170.         //echo "<pre>"; print_r($tags); echo "</pre>";
  171.         return $tags;
  172. }
  173.  
  174.  
  175. // show the tags above the group directory
  176. function gtags_display_tags() {
  177.         global$gtags_show_cloud;
  178.         if ( $gtags_show_cloud ){
  179.                 global $gtags_args;    
  180.                 echo '<div id="gtags-top" class="gtags">';
  181.                 echo  wp_generate_tag_cloud( gtags_make_tags(), $gtags_args );  // use the built in wordrpess tag function :)
  182.                 echo '</div>';
  183.         }
  184.         global $bp;
  185. }
  186. add_action( 'bp_before_directory_groups_content', 'gtags_display_tags' );
  187.  
  188. // create the form to add tags
  189. function gtags_add_tags_form() {
  190.         global $show_group_add_form;
  191.         if ($show_group_add_form)
  192.                 return;
  193.         $show_group_add_form = true;
  194.         ?>
  195.         <label for="group-tags"><?php _e( 'Resturant Tags', 'gtags' ) ?></label>
  196.         <input type="text" name="group-tags" id="group-tags" value="<?php gtags_group_tags() ?>" /><br>
  197.         <i><?php _e('Separate tags with commas', 'gtags'); ?></i><br>
  198.         <?php
  199.         gtags_show_tags_chooser();
  200. }
  201. add_action( 'groups_custom_group_fields_editable', 'gtags_add_tags_form' );
  202.  
  203. // create easy tag adding links. tags added to form via javascript
  204. function gtags_show_tags_chooser(){
  205.         ?>
  206.         <div class="tags_chooser"><?php _e('Common Tags', 'gtags'); ?>: <?php echo gtags_show_tags_in_add_form(); ?></div>
  207.         <?php
  208. }
  209.  
  210. // show list of the most popular tags - used when adding tags
  211. function gtags_show_tags_in_add_form() {
  212.         global $popular_tag_limit;
  213.         $tags = gtags_make_tags();
  214.         uasort( $tags, create_function('$a, $b', 'return ($b->count > $a->count);') );
  215.         foreach ( $tags as $tag ) {
  216.                 echo ' <span class="gtags-add">' . $tag->name .'</span>';
  217.                 //$sep = '&nbsp; ';
  218.                 ++$i;
  219.                 if ( $i >= $popular_tag_limit ) break;
  220.         }
  221. }
  222.  
  223. // Save the tag values in the group meta - perhaps use serialize() and maybe_unserialize()
  224. function gtags_save_tags( $group_id ) {
  225.         global $bp;
  226.        
  227.         if($bp->groups->new_group_id)
  228.                 $id = $bp->groups->new_group_id;
  229.         else
  230.                 $id = $group_id;
  231.                
  232.         if ( $_POST['group-tags']  )
  233.                 groups_update_groupmeta( $id, 'gtags_group_tags', $_POST['group-tags'] );
  234. }
  235. add_action( 'groups_create_group_step_save_group-details', 'gtags_save_tags' );
  236. add_action( 'groups_details_updated', 'gtags_save_tags' );
  237.  
  238. // Get or return the tag values
  239. function gtags_group_tags() {
  240.         echo gtags_get_group_tags();
  241. }
  242.         function gtags_get_group_tags( $group = false ) {
  243.                 global $groups_template;
  244.                 if ( !$group )
  245.                         $group =& $groups_template->group;
  246.                 $group_tags = groups_get_groupmeta( $group->id, 'gtags_group_tags' );
  247.                 $group_tags = stripcslashes( $group_tags );
  248.                 return apply_filters( 'gtags_get_group_tags', $group_tags );
  249.         }
  250.  
  251.  
  252. // show tags in group directory listing
  253. function gtags_show_tags_in_directory() {
  254.         global $gtags_show_tags_in_directory;
  255.         if ( gtags_get_group_tags() && $gtags_show_tags_in_directory ) {
  256.                 ?>
  257.                 <div class="item-desc item-tags"><?php _e('Tags', 'gtags'); ?>: <?php echo gtags_make_tags_for_group() ?></div>
  258.                 <?php
  259.         }
  260. }
  261. add_action( 'bp_directory_groups_item', 'gtags_show_tags_in_directory' );
  262.  
  263. // show tags in group header
  264. function gtags_show_tags_in_header( $description ) {
  265.         global $gtags_show_tags_in_header;
  266.         if ( gtags_get_group_tags() && $gtags_show_tags_in_header ) {
  267.                 $description .= '<div class="gtags-header">'. __('Tags', 'gtags').': '. gtags_make_tags_for_group().'</div>';
  268.         }
  269.         return $description;
  270. }
  271. add_filter( 'bp_get_group_description', 'gtags_show_tags_in_header' );
  272.  
  273. // show tags for an individual group with links
  274. function gtags_make_tags_for_group() {
  275.         global $bp, $wpdb, $gtags_args;
  276.        
  277.         $group_tags = gtags_get_group_tags();
  278.        
  279.         $items = explode( ",", $group_tags );
  280.         foreach( $items as $item ) {
  281.                 $item = trim( strtolower( $item ) );
  282.                 if ($item=='') continue;
  283.                 $link = $bp->root_domain . '/' . BP_GROUPS_SLUG . '/tag/' . urlencode( $item );
  284.                 $output .= ' <a href="'.$link.'">'.$item.'</a> ';
  285.         }
  286.         return $output;
  287. }
  288.  
  289.  
  290.  
  291. // add a happy group tag widget
  292. function gtags_group_tags_widget() {
  293.         global $gtags_args;    
  294. ?>
  295.   <h3 class="widgettitle"><?php _e('Group Tags', 'gtags'); ?></h3>
  296.   <div class="gtags">
  297.   <?php
  298.         echo wp_generate_tag_cloud( gtags_make_tags(), $gtags_args );
  299.   ?>
  300.   </div>
  301. <?php
  302. }
  303.  
  304. function gtags_widget_init() {
  305.   register_sidebar_widget(__('Group Tags'), 'gtags_group_tags_widget');
  306. }
  307. add_action("plugins_loaded", "gtags_widget_init");
  308.  
  309.  
  310. function bp_gtags_setup_globals() {
  311.         global $bp;
  312.  
  313.         $bp->gtags->id = 'gtags';
  314.         $bp->gtags->slug = 'tag';
  315. }
  316. add_action( 'bp_setup_globals', 'bp_gtags_setup_globals' );
  317.  
  318. // this gets called as a hook, but never shows on the page due to low precedence level - strange but works
  319. // it does however show up on /members/admin/groups/ so I hacked it so the name of the sub nav item does not show.
  320. function bp_gtags_setup_nav() {
  321.         global $bp;
  322.         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 ) );
  323. }
  324. add_action( 'bp_setup_nav', 'bp_gtags_setup_nav', 1 );
  325.  
  326. function gtags_display_hook() {
  327.         global $bp;
  328.         add_action( 'bp_has_groups', 'gtags_show_groups_for_tag' );
  329.         bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'groups/index' ) );
  330. }
  331.  
  332.  
  333. function mybp(){
  334.         global $bp;
  335.         //echo '<pre>'; print_r( $bp ); echo '</pre>';  
  336. }
  337. //add_action('bp_before_footer','mybp');
  338.  
  339. ?>
clone this paste RAW Paste Data