/bp-groups/classes/class-bp-groups-widget.php

  1. <?php 
  2. /** 
  3. * BuddyPress Groups Widget. 
  4. * 
  5. * @package BuddyPress 
  6. * @subpackage GroupsWidgets 
  7. * @since 1.0.0 
  8. */ 
  9.  
  10. // Exit if accessed directly. 
  11. defined( 'ABSPATH' ) || exit; 
  12.  
  13. /** 
  14. * Groups widget. 
  15. * 
  16. * @since 1.0.3 
  17. */ 
  18. class BP_Groups_Widget extends WP_Widget { 
  19.  
  20. /** 
  21. * Working as a group, we get things done better. 
  22. * 
  23. * @since 1.0.3 
  24. */ 
  25. public function __construct() { 
  26. $widget_ops = array( 
  27. 'description' => __( 'A dynamic list of recently active, popular, newest, or alphabetical groups', 'buddypress' ),  
  28. 'classname' => 'widget_bp_groups_widget buddypress widget',  
  29. 'customize_selective_refresh' => true,  
  30. ); 
  31. parent::__construct( false, _x( '(BuddyPress) Groups', 'widget name', 'buddypress' ), $widget_ops ); 
  32.  
  33. if ( is_customize_preview() || is_active_widget( false, false, $this->id_base ) ) { 
  34. add_action( 'bp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 
  35.  
  36. /** 
  37. * Enqueue scripts. 
  38. * 
  39. * @since 2.6.0 
  40. */ 
  41. public function enqueue_scripts() { 
  42. $min = bp_core_get_minified_asset_suffix(); 
  43. wp_enqueue_script( 'groups_widget_groups_list-js', buddypress()->plugin_url . "bp-groups/js/widget-groups{$min}.js", array( 'jquery' ), bp_get_version() ); 
  44.  
  45. /** 
  46. * Extends our front-end output method. 
  47. * 
  48. * @since 1.0.3 
  49. * 
  50. * @param array $args Array of arguments for the widget. 
  51. * @param array $instance Widget instance data. 
  52. */ 
  53. public function widget( $args, $instance ) { 
  54. global $groups_template; 
  55.  
  56. /** 
  57. * Filters the user ID to use with the widget instance. 
  58. * 
  59. * @since 1.5.0 
  60. * 
  61. * @param string $value Empty user ID. 
  62. */ 
  63. $user_id = apply_filters( 'bp_group_widget_user_id', '0' ); 
  64.  
  65. extract( $args ); 
  66.  
  67. if ( empty( $instance['group_default'] ) ) { 
  68. $instance['group_default'] = 'popular'; 
  69.  
  70. if ( empty( $instance['title'] ) ) { 
  71. $instance['title'] = __( 'Groups', 'buddypress' ); 
  72.  
  73. /** 
  74. * Filters the title of the Groups widget. 
  75. * 
  76. * @since 1.8.0 
  77. * @since 2.3.0 Added 'instance' and 'id_base' to arguments passed to filter. 
  78. * 
  79. * @param string $title The widget title. 
  80. * @param array $instance The settings for the particular instance of the widget. 
  81. * @param string $id_base Root ID for all widgets of this type. 
  82. */ 
  83. $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); 
  84.  
  85. /** 
  86. * Filters the separator of the group widget links. 
  87. * 
  88. * @since 2.4.0 
  89. * 
  90. * @param string $separator Separator string. Default '|'. 
  91. */ 
  92. $separator = apply_filters( 'bp_groups_widget_separator', '|' ); 
  93.  
  94. echo $before_widget; 
  95.  
  96. $title = ! empty( $instance['link_title'] ) ? '<a href="' . bp_get_groups_directory_permalink() . '">' . $title . '</a>' : $title; 
  97.  
  98. echo $before_title . $title . $after_title; 
  99.  
  100. $max_groups = ! empty( $instance['max_groups'] ) ? (int) $instance['max_groups'] : 5; 
  101.  
  102. $group_args = array( 
  103. 'user_id' => $user_id,  
  104. 'type' => $instance['group_default'],  
  105. 'per_page' => $max_groups,  
  106. 'max' => $max_groups,  
  107. ); 
  108.  
  109. // Back up the global. 
  110. $old_groups_template = $groups_template; 
  111.  
  112. ?> 
  113.  
  114. <?php if ( bp_has_groups( $group_args ) ) : ?> 
  115. <div class="item-options" id="groups-list-options"> 
  116. <a href="<?php bp_groups_directory_permalink(); ?>" id="newest-groups"<?php if ( $instance['group_default'] == 'newest' ) : ?> class="selected"<?php endif; ?>><?php _e("Newest", 'buddypress') ?></a> 
  117. <span class="bp-separator" role="separator"><?php echo esc_html( $separator ); ?></span> 
  118. <a href="<?php bp_groups_directory_permalink(); ?>" id="recently-active-groups"<?php if ( $instance['group_default'] == 'active' ) : ?> class="selected"<?php endif; ?>><?php _e("Active", 'buddypress') ?></a> 
  119. <span class="bp-separator" role="separator"><?php echo esc_html( $separator ); ?></span> 
  120. <a href="<?php bp_groups_directory_permalink(); ?>" id="popular-groups" <?php if ( $instance['group_default'] == 'popular' ) : ?> class="selected"<?php endif; ?>><?php _e("Popular", 'buddypress') ?></a> 
  121. <span class="bp-separator" role="separator"><?php echo esc_html( $separator ); ?></span> 
  122. <a href="<?php bp_groups_directory_permalink(); ?>" id="alphabetical-groups" <?php if ( $instance['group_default'] == 'alphabetical' ) : ?> class="selected"<?php endif; ?>><?php _e("Alphabetical", 'buddypress') ?></a> 
  123. </div> 
  124.  
  125. <ul id="groups-list" class="item-list" aria-live="polite" aria-relevant="all" aria-atomic="true"> 
  126. <?php while ( bp_groups() ) : bp_the_group(); ?> 
  127. <li <?php bp_group_class(); ?>> 
  128. <div class="item-avatar"> 
  129. <a href="<?php bp_group_permalink() ?>" title="<?php bp_group_name() ?>"><?php bp_group_avatar_thumb() ?></a> 
  130. </div> 
  131.  
  132. <div class="item"> 
  133. <div class="item-title"><a href="<?php bp_group_permalink() ?>"><?php bp_group_name() ?></a></div> 
  134. <div class="item-meta"> 
  135. <span class="activity"> 
  136. <?php 
  137. if ( 'newest' == $instance['group_default'] ) { 
  138. printf( __( 'created %s', 'buddypress' ), bp_get_group_date_created() ); 
  139. } elseif ( 'popular' == $instance['group_default'] ) { 
  140. bp_group_member_count(); 
  141. } else { 
  142. printf( __( 'active %s', 'buddypress' ), bp_get_group_last_active() ); 
  143. ?> 
  144. </span> 
  145. </div> 
  146. </div> 
  147. </li> 
  148.  
  149. <?php endwhile; ?> 
  150. </ul> 
  151. <?php wp_nonce_field( 'groups_widget_groups_list', '_wpnonce-groups' ); ?> 
  152. <input type="hidden" name="groups_widget_max" id="groups_widget_max" value="<?php echo esc_attr( $max_groups ); ?>" /> 
  153.  
  154. <?php else: ?> 
  155.  
  156. <div class="widget-error"> 
  157. <?php _e('There are no groups to display.', 'buddypress') ?> 
  158. </div> 
  159.  
  160. <?php endif; ?> 
  161.  
  162. <?php echo $after_widget; 
  163.  
  164. // Restore the global. 
  165. $groups_template = $old_groups_template; 
  166.  
  167. /** 
  168. * Extends our update method. 
  169. * 
  170. * @since 1.0.3 
  171. * 
  172. * @param array $new_instance New instance data. 
  173. * @param array $old_instance Original instance data. 
  174. * @return array 
  175. */ 
  176. public function update( $new_instance, $old_instance ) { 
  177. $instance = $old_instance; 
  178.  
  179. $instance['title'] = strip_tags( $new_instance['title'] ); 
  180. $instance['max_groups'] = strip_tags( $new_instance['max_groups'] ); 
  181. $instance['group_default'] = strip_tags( $new_instance['group_default'] ); 
  182. $instance['link_title'] = (bool) $new_instance['link_title']; 
  183.  
  184. return $instance; 
  185.  
  186. /** 
  187. * Extends our form method. 
  188. * 
  189. * @since 1.0.3 
  190. * 
  191. * @param array $instance Current instance. 
  192. * @return mixed 
  193. */ 
  194. public function form( $instance ) { 
  195. $defaults = array( 
  196. 'title' => __( 'Groups', 'buddypress' ),  
  197. 'max_groups' => 5,  
  198. 'group_default' => 'active',  
  199. 'link_title' => false 
  200. ); 
  201. $instance = wp_parse_args( (array) $instance, $defaults ); 
  202.  
  203. $title = strip_tags( $instance['title'] ); 
  204. $max_groups = strip_tags( $instance['max_groups'] ); 
  205. $group_default = strip_tags( $instance['group_default'] ); 
  206. $link_title = (bool) $instance['link_title']; 
  207. ?> 
  208.  
  209. <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'buddypress'); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" style="width: 100%" /></label></p> 
  210.  
  211. <p><label for="<?php echo $this->get_field_id('link_title') ?>"><input type="checkbox" name="<?php echo $this->get_field_name('link_title') ?>" id="<?php echo $this->get_field_id('link_title') ?>" value="1" <?php checked( $link_title ) ?> /> <?php _e( 'Link widget title to Groups directory', 'buddypress' ) ?></label></p> 
  212.  
  213. <p><label for="<?php echo $this->get_field_id( 'max_groups' ); ?>"><?php _e('Max groups to show:', 'buddypress'); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_groups' ); ?>" name="<?php echo $this->get_field_name( 'max_groups' ); ?>" type="text" value="<?php echo esc_attr( $max_groups ); ?>" style="width: 30%" /></label></p> 
  214.  
  215. <p> 
  216. <label for="<?php echo $this->get_field_id( 'group_default' ); ?>"><?php _e('Default groups to show:', 'buddypress'); ?></label> 
  217. <select name="<?php echo $this->get_field_name( 'group_default' ); ?>" id="<?php echo $this->get_field_id( 'group_default' ); ?>"> 
  218. <option value="newest" <?php selected( $group_default, 'newest' ); ?>><?php _e( 'Newest', 'buddypress' ) ?></option> 
  219. <option value="active" <?php selected( $group_default, 'active' ); ?>><?php _e( 'Active', 'buddypress' ) ?></option> 
  220. <option value="popular" <?php selected( $group_default, 'popular' ); ?>><?php _e( 'Popular', 'buddypress' ) ?></option> 
  221. <option value="alphabetical" <?php selected( $group_default, 'alphabetical' ); ?>><?php _e( 'Alphabetical', 'buddypress' ) ?></option> 
  222. </select> 
  223. </p> 
  224. <?php 
.