[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-friends/classes/ -> class-bp-core-friends-widget.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Friends Widget.
   4   *
   5   * @package BuddyPress
   6   * @subpackage FriendsWidget
   7   * @since 1.9.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * The User Friends widget class.
  15   *
  16   * @since 1.9.0
  17   */
  18  class BP_Core_Friends_Widget extends WP_Widget {
  19  
  20      /**
  21       * Class constructor.
  22       *
  23       * @since 1.9.0
  24       * @since 9.0.0 Adds the `show_instance_in_rest` property to Widget options.
  25       */
  26  	public function __construct() {
  27          $widget_ops = array(
  28              'description'                 => __( 'A dynamic list of recently active, popular, and newest Friends of the displayed member. Widget is only shown when viewing a member profile.', 'buddypress' ),
  29              'classname'                   => 'widget_bp_core_friends_widget buddypress widget',
  30              'customize_selective_refresh' => true,
  31              'show_instance_in_rest'       => true,
  32          );
  33          parent::__construct( false, $name = _x( '(BuddyPress) Friends', 'widget name', 'buddypress' ), $widget_ops );
  34  
  35          if ( is_customize_preview() || bp_is_widget_block_active( '', $this->id_base ) ) {
  36              add_action( 'bp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
  37          }
  38      }
  39  
  40      /**
  41       * Enqueue scripts.
  42       *
  43       * @since 2.6.0
  44       */
  45  	public function enqueue_scripts() {
  46          $min = bp_core_get_minified_asset_suffix();
  47          wp_enqueue_script( 'bp_core_widget_friends-js', buddypress()->plugin_url . "bp-friends/js/widget-friends{$min}.js", array( 'jquery' ), bp_get_version() );
  48      }
  49  
  50      /**
  51       * Display the widget.
  52       *
  53       * @since 1.9.0
  54       *
  55       * @global BP_Core_Members_Template $members_template
  56       *
  57       * @param array $args Widget arguments.
  58       * @param array $instance The widget settings, as saved by the user.
  59       */
  60  	public function widget( $args, $instance ) {
  61          global $members_template;
  62  
  63          extract( $args );
  64  
  65          if ( ! bp_displayed_user_id() ) {
  66              return;
  67          }
  68  
  69          $user_id           = bp_displayed_user_id();
  70          $link              = trailingslashit( bp_displayed_user_domain() . bp_get_friends_slug() );
  71          $instance['title'] = sprintf( __( "%s's Friends", 'buddypress' ), bp_get_displayed_user_fullname() );
  72  
  73          if ( empty( $instance['friend_default'] ) ) {
  74              $instance['friend_default'] = 'active';
  75          }
  76  
  77          /**
  78           * Filters the Friends widget title.
  79           *
  80           * @since 1.8.0
  81           * @since 2.3.0 Added 'instance' and 'id_base' to arguments passed to filter.
  82           *
  83           * @param string $title    The widget title.
  84           * @param array  $instance The settings for the particular instance of the widget.
  85           * @param string $id_base  Root ID for all widgets of this type.
  86           */
  87          $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
  88  
  89          echo $before_widget;
  90  
  91          $title = $instance['link_title'] ? '<a href="' . esc_url( $link ) . '">' . esc_html( $title ) . '</a>' : esc_html( $title );
  92  
  93          echo $before_title . $title . $after_title;
  94  
  95          $members_args = array(
  96              'user_id'         => absint( $user_id ),
  97              'type'            => sanitize_text_field( $instance['friend_default'] ),
  98              'max'             => absint( $instance['max_friends'] ),
  99              'populate_extras' => 1,
 100          );
 101  
 102          // Back up the global.
 103          $old_members_template = $members_template;
 104  
 105          ?>
 106  
 107          <?php if ( bp_has_members( $members_args ) ) : ?>
 108              <div class="item-options" id="friends-list-options">
 109                  <a href="<?php bp_members_directory_permalink(); ?>" id="newest-friends" <?php if ( $instance['friend_default'] == 'newest' ) : ?>class="selected"<?php endif; ?>><?php _e( 'Newest', 'buddypress' ); ?></a>
 110                  | <a href="<?php bp_members_directory_permalink(); ?>" id="recently-active-friends" <?php if ( $instance['friend_default'] == 'active' ) : ?>class="selected"<?php endif; ?>><?php _e( 'Active', 'buddypress' ); ?></a>
 111                  | <a href="<?php bp_members_directory_permalink(); ?>" id="popular-friends" <?php if ( $instance['friend_default'] == 'popular' ) : ?>class="selected"<?php endif; ?>><?php _e( 'Popular', 'buddypress' ); ?></a>
 112              </div>
 113  
 114              <ul id="friends-list" class="item-list">
 115                  <?php while ( bp_members() ) : bp_the_member(); ?>
 116                      <li class="vcard">
 117                          <div class="item-avatar">
 118                              <a href="<?php bp_member_permalink(); ?>" class="bp-tooltip" data-bp-tooltip="<?php bp_member_name(); ?>"><?php bp_member_avatar(); ?></a>
 119                          </div>
 120  
 121                          <div class="item">
 122                              <div class="item-title fn"><a href="<?php bp_member_permalink(); ?>"><?php bp_member_name(); ?></a></div>
 123                              <div class="item-meta">
 124                                  <?php if ( 'newest' == $instance['friend_default'] ) : ?>
 125                                      <span class="activity" data-livestamp="<?php bp_core_iso8601_date( bp_get_member_registered( array( 'relative' => false ) ) ); ?>"><?php bp_member_registered(); ?></span>
 126                                  <?php elseif ( 'active' == $instance['friend_default'] ) : ?>
 127                                      <span class="activity" data-livestamp="<?php bp_core_iso8601_date( bp_get_member_last_active( array( 'relative' => false ) ) ); ?>"><?php bp_member_last_active(); ?></span>
 128                                  <?php else : ?>
 129                                      <span class="activity"><?php bp_member_total_friend_count(); ?></span>
 130                                  <?php endif; ?>
 131                              </div>
 132                          </div>
 133                      </li>
 134  
 135                  <?php endwhile; ?>
 136              </ul>
 137              <?php wp_nonce_field( 'bp_core_widget_friends', '_wpnonce-friends' ); ?>
 138              <input type="hidden" name="friends_widget_max" id="friends_widget_max" value="<?php echo absint( $instance['max_friends'] ); ?>" />
 139  
 140          <?php else: ?>
 141  
 142              <div class="widget-error">
 143                  <?php _e( 'Sorry, no members were found.', 'buddypress' ); ?>
 144              </div>
 145  
 146          <?php endif; ?>
 147  
 148          <?php echo $after_widget;
 149  
 150          // Restore the global.
 151          $members_template = $old_members_template;
 152      }
 153  
 154      /**
 155       * Process a widget save.
 156       *
 157       * @since 1.9.0
 158       *
 159       * @param array $new_instance The parameters saved by the user.
 160       * @param array $old_instance The parameters as previously saved to the database.
 161       * @return array $instance The processed settings to save.
 162       */
 163  	public function update( $new_instance, $old_instance ) {
 164          $instance = $old_instance;
 165  
 166          $instance['max_friends']    = absint( $new_instance['max_friends'] );
 167          $instance['friend_default'] = sanitize_text_field( $new_instance['friend_default'] );
 168          $instance['link_title']        = ! empty( $new_instance['link_title'] );
 169  
 170          return $instance;
 171      }
 172  
 173      /**
 174       * Render the widget edit form.
 175       *
 176       * @since 1.9.0
 177       *
 178       * @param array $instance The saved widget settings.
 179       */
 180  	public function form( $instance ) {
 181          $defaults = array(
 182              'max_friends'    => 5,
 183              'friend_default' => 'active',
 184              'link_title'     => false,
 185          );
 186  
 187          $instance = bp_parse_args(
 188              (array) $instance,
 189              $defaults
 190          );
 191  
 192          $max_friends    = $instance['max_friends'];
 193          $friend_default = $instance['friend_default'];
 194          $link_title     = (bool) $instance['link_title'];
 195          ?>
 196  
 197          <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 Members directory', 'buddypress' ); ?></label></p>
 198  
 199          <p><label for="<?php echo $this->get_field_id( 'max_friends' ); ?>"><?php esc_html_e( 'Max friends to show:', 'buddypress' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_friends' ); ?>" name="<?php echo $this->get_field_name( 'max_friends' ); ?>" type="text" value="<?php echo absint( $max_friends ); ?>" style="width: 30%" /></label></p>
 200  
 201          <p>
 202              <label for="<?php echo $this->get_field_id( 'friend_default' ) ?>"><?php esc_html_e( 'Default friends to show:', 'buddypress' ); ?></label>
 203              <select name="<?php echo $this->get_field_name( 'friend_default' ); ?>" id="<?php echo $this->get_field_id( 'friend_default' ); ?>">
 204                  <option value="newest" <?php selected( $friend_default, 'newest' ); ?>><?php esc_html_e( 'Newest', 'buddypress' ); ?></option>
 205                  <option value="active" <?php selected( $friend_default, 'active' ); ?>><?php esc_html_e( 'Active', 'buddypress' ); ?></option>
 206                  <option value="popular"  <?php selected( $friend_default, 'popular' ); ?>><?php esc_html_e( 'Popular', 'buddypress' ); ?></option>
 207              </select>
 208          </p>
 209      <?php
 210      }
 211  }


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1