PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/buddypress/bp-groups/bp-groups-widgets.php

https://github.com/bfay/maniacal-kitten
PHP | 203 lines | 153 code | 41 blank | 9 comment | 39 complexity | efb85f84f5060be1e826b957a7ed20a8 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, LGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * BuddyPress Groups Widgets
  4. *
  5. * @package BuddyPress
  6. * @subpackage GroupsWidgets
  7. */
  8. // Exit if accessed directly
  9. if ( !defined( 'ABSPATH' ) ) exit;
  10. /* Register widgets for groups component */
  11. function groups_register_widgets() {
  12. add_action('widgets_init', create_function('', 'return register_widget("BP_Groups_Widget");') );
  13. }
  14. add_action( 'bp_register_widgets', 'groups_register_widgets' );
  15. /*** GROUPS WIDGET *****************/
  16. class BP_Groups_Widget extends WP_Widget {
  17. function bp_groups_widget() {
  18. $this->_construct();
  19. }
  20. function __construct() {
  21. $widget_ops = array(
  22. 'description' => __( 'A dynamic list of recently active, popular, and newest groups', 'buddypress' ),
  23. 'classname' => 'widget_bp_groups_widget buddypress',
  24. );
  25. parent::__construct( false, _x( '(BuddyPress) Groups', 'widget name', 'buddypress' ), $widget_ops );
  26. if ( is_active_widget( false, false, $this->id_base ) && !is_admin() && !is_network_admin() ) {
  27. $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
  28. wp_enqueue_script( 'groups_widget_groups_list-js', BP_PLUGIN_URL . "bp-groups/js/widget-groups{$min}.js", array( 'jquery' ), bp_get_version() );
  29. }
  30. }
  31. function widget( $args, $instance ) {
  32. $user_id = apply_filters( 'bp_group_widget_user_id', '0' );
  33. extract( $args );
  34. if ( empty( $instance['group_default'] ) )
  35. $instance['group_default'] = 'popular';
  36. if ( empty( $instance['title'] ) )
  37. $instance['title'] = __( 'Groups', 'buddypress' );
  38. echo $before_widget;
  39. $title = !empty( $instance['link_title'] ) ? '<a href="' . trailingslashit( bp_get_root_domain() . '/' . bp_get_groups_root_slug() ) . '">' . $instance['title'] . '</a>' : $instance['title'];
  40. echo $before_title . $title . $after_title; ?>
  41. <?php if ( bp_has_groups( 'user_id=' . $user_id . '&type=' . $instance['group_default'] . '&max=' . $instance['max_groups'] ) ) : ?>
  42. <div class="item-options" id="groups-list-options">
  43. <a href="<?php echo site_url( bp_get_groups_root_slug() ); ?>" id="newest-groups"<?php if ( $instance['group_default'] == 'newest' ) : ?> class="selected"<?php endif; ?>><?php _e("Newest", 'buddypress') ?></a> |
  44. <a href="<?php echo site_url( bp_get_groups_root_slug() ); ?>" id="recently-active-groups"<?php if ( $instance['group_default'] == 'active' ) : ?> class="selected"<?php endif; ?>><?php _e("Active", 'buddypress') ?></a> |
  45. <a href="<?php echo site_url( bp_get_groups_root_slug() ); ?>" id="popular-groups" <?php if ( $instance['group_default'] == 'popular' ) : ?> class="selected"<?php endif; ?>><?php _e("Popular", 'buddypress') ?></a>
  46. </div>
  47. <ul id="groups-list" class="item-list">
  48. <?php while ( bp_groups() ) : bp_the_group(); ?>
  49. <li>
  50. <div class="item-avatar">
  51. <a href="<?php bp_group_permalink() ?>" title="<?php bp_group_name() ?>"><?php bp_group_avatar_thumb() ?></a>
  52. </div>
  53. <div class="item">
  54. <div class="item-title"><a href="<?php bp_group_permalink() ?>" title="<?php bp_group_name() ?>"><?php bp_group_name() ?></a></div>
  55. <div class="item-meta">
  56. <span class="activity">
  57. <?php
  58. if ( 'newest' == $instance['group_default'] )
  59. printf( __( 'created %s', 'buddypress' ), bp_get_group_date_created() );
  60. if ( 'active' == $instance['group_default'] )
  61. printf( __( 'active %s', 'buddypress' ), bp_get_group_last_active() );
  62. else if ( 'popular' == $instance['group_default'] )
  63. bp_group_member_count();
  64. ?>
  65. </span>
  66. </div>
  67. </div>
  68. </li>
  69. <?php endwhile; ?>
  70. </ul>
  71. <?php wp_nonce_field( 'groups_widget_groups_list', '_wpnonce-groups' ); ?>
  72. <input type="hidden" name="groups_widget_max" id="groups_widget_max" value="<?php echo esc_attr( $instance['max_groups'] ); ?>" />
  73. <?php else: ?>
  74. <div class="widget-error">
  75. <?php _e('There are no groups to display.', 'buddypress') ?>
  76. </div>
  77. <?php endif; ?>
  78. <?php echo $after_widget; ?>
  79. <?php
  80. }
  81. function update( $new_instance, $old_instance ) {
  82. $instance = $old_instance;
  83. $instance['title'] = strip_tags( $new_instance['title'] );
  84. $instance['max_groups'] = strip_tags( $new_instance['max_groups'] );
  85. $instance['group_default'] = strip_tags( $new_instance['group_default'] );
  86. $instance['link_title'] = (bool)$new_instance['link_title'];
  87. return $instance;
  88. }
  89. function form( $instance ) {
  90. $defaults = array(
  91. 'title' => __( 'Groups', 'buddypress' ),
  92. 'max_groups' => 5,
  93. 'group_default' => 'active',
  94. 'link_title' => false
  95. );
  96. $instance = wp_parse_args( (array) $instance, $defaults );
  97. $title = strip_tags( $instance['title'] );
  98. $max_groups = strip_tags( $instance['max_groups'] );
  99. $group_default = strip_tags( $instance['group_default'] );
  100. $link_title = (bool)$instance['link_title'];
  101. ?>
  102. <p><label for="bp-groups-widget-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>
  103. <p><label for="<?php echo $this->get_field_name('link_title') ?>"><input type="checkbox" name="<?php echo $this->get_field_name('link_title') ?>" value="1" <?php checked( $link_title ) ?> /> <?php _e( 'Link widget title to Groups directory', 'buddypress' ) ?></label></p>
  104. <p><label for="bp-groups-widget-groups-max"><?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>
  105. <p>
  106. <label for="bp-groups-widget-groups-default"><?php _e('Default groups to show:', 'buddypress'); ?>
  107. <select name="<?php echo $this->get_field_name( 'group_default' ); ?>">
  108. <option value="newest" <?php if ( $group_default == 'newest' ) : ?>selected="selected"<?php endif; ?>><?php _e( 'Newest', 'buddypress' ) ?></option>
  109. <option value="active" <?php if ( $group_default == 'active' ) : ?>selected="selected"<?php endif; ?>><?php _e( 'Active', 'buddypress' ) ?></option>
  110. <option value="popular" <?php if ( $group_default == 'popular' ) : ?>selected="selected"<?php endif; ?>><?php _e( 'Popular', 'buddypress' ) ?></option>
  111. </select>
  112. </label>
  113. </p>
  114. <?php
  115. }
  116. }
  117. function groups_ajax_widget_groups_list() {
  118. check_ajax_referer('groups_widget_groups_list');
  119. switch ( $_POST['filter'] ) {
  120. case 'newest-groups':
  121. $type = 'newest';
  122. break;
  123. case 'recently-active-groups':
  124. $type = 'active';
  125. break;
  126. case 'popular-groups':
  127. $type = 'popular';
  128. break;
  129. }
  130. if ( bp_has_groups( 'type=' . $type . '&per_page=' . $_POST['max_groups'] . '&max=' . $_POST['max_groups'] ) ) : ?>
  131. <?php echo "0[[SPLIT]]"; ?>
  132. <?php while ( bp_groups() ) : bp_the_group(); ?>
  133. <li>
  134. <div class="item-avatar">
  135. <a href="<?php bp_group_permalink() ?>"><?php bp_group_avatar_thumb() ?></a>
  136. </div>
  137. <div class="item">
  138. <div class="item-title"><a href="<?php bp_group_permalink() ?>" title="<?php bp_group_name() ?>"><?php bp_group_name() ?></a></div>
  139. <div class="item-meta">
  140. <span class="activity">
  141. <?php
  142. if ( 'newest-groups' == $_POST['filter'] ) {
  143. printf( __( 'created %s', 'buddypress' ), bp_get_group_date_created() );
  144. } else if ( 'recently-active-groups' == $_POST['filter'] ) {
  145. printf( __( 'active %s', 'buddypress' ), bp_get_group_last_active() );
  146. } else if ( 'popular-groups' == $_POST['filter'] ) {
  147. bp_group_member_count();
  148. }
  149. ?>
  150. </span>
  151. </div>
  152. </div>
  153. </li>
  154. <?php endwhile; ?>
  155. <?php wp_nonce_field( 'groups_widget_groups_list', '_wpnonce-groups' ); ?>
  156. <input type="hidden" name="groups_widget_max" id="groups_widget_max" value="<?php echo esc_attr( $_POST['max_groups'] ); ?>" />
  157. <?php else: ?>
  158. <?php echo "-1[[SPLIT]]<li>" . __("No groups matched the current filter.", 'buddypress'); ?>
  159. <?php endif;
  160. }
  161. add_action( 'wp_ajax_widget_groups_list', 'groups_ajax_widget_groups_list' );
  162. add_action( 'wp_ajax_nopriv_widget_groups_list', 'groups_ajax_widget_groups_list' );