[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * BuddyPress Friends Widget. 4 * 5 * @package BuddyPress 6 * @subpackage Friends 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 */ 25 function __construct() { 26 $widget_ops = array( 27 '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' ), 28 'classname' => 'widget_bp_core_friends_widget buddypress widget', 29 'customize_selective_refresh' => true, 30 ); 31 parent::__construct( false, $name = _x( '(BuddyPress) Friends', '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 38 /** 39 * Enqueue scripts. 40 * 41 * @since 2.6.0 42 */ 43 public function enqueue_scripts() { 44 $min = bp_core_get_minified_asset_suffix(); 45 wp_enqueue_script( 'bp_core_widget_friends-js', buddypress()->plugin_url . "bp-friends/js/widget-friends{$min}.js", array( 'jquery' ), bp_get_version() ); 46 } 47 48 /** 49 * Display the widget. 50 * 51 * @since 1.9.0 52 * 53 * @param array $args Widget arguments. 54 * @param array $instance The widget settings, as saved by the user. 55 */ 56 function widget( $args, $instance ) { 57 global $members_template; 58 59 extract( $args ); 60 61 if ( ! bp_displayed_user_id() ) { 62 return; 63 } 64 65 $user_id = bp_displayed_user_id(); 66 $link = trailingslashit( bp_displayed_user_domain() . bp_get_friends_slug() ); 67 $instance['title'] = sprintf( __( "%s's Friends", 'buddypress' ), bp_get_displayed_user_fullname() ); 68 69 if ( empty( $instance['friend_default'] ) ) { 70 $instance['friend_default'] = 'active'; 71 } 72 73 /** 74 * Filters the Friends widget title. 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 echo $before_widget; 86 87 $title = $instance['link_title'] ? '<a href="' . esc_url( $link ) . '">' . esc_html( $title ) . '</a>' : esc_html( $title ); 88 89 echo $before_title . $title . $after_title; 90 91 $members_args = array( 92 'user_id' => absint( $user_id ), 93 'type' => sanitize_text_field( $instance['friend_default'] ), 94 'max' => absint( $instance['max_friends'] ), 95 'populate_extras' => 1, 96 ); 97 98 // Back up the global. 99 $old_members_template = $members_template; 100 101 ?> 102 103 <?php if ( bp_has_members( $members_args ) ) : ?> 104 <div class="item-options" id="friends-list-options"> 105 <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> 106 | <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> 107 | <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> 108 </div> 109 110 <ul id="friends-list" class="item-list"> 111 <?php while ( bp_members() ) : bp_the_member(); ?> 112 <li class="vcard"> 113 <div class="item-avatar"> 114 <a href="<?php bp_member_permalink(); ?>" class="bp-tooltip" data-bp-tooltip="<?php bp_member_name(); ?>"><?php bp_member_avatar(); ?></a> 115 </div> 116 117 <div class="item"> 118 <div class="item-title fn"><a href="<?php bp_member_permalink(); ?>"><?php bp_member_name(); ?></a></div> 119 <div class="item-meta"> 120 <?php if ( 'newest' == $instance['friend_default'] ) : ?> 121 <span class="activity" data-livestamp="<?php bp_core_iso8601_date( bp_get_member_registered( array( 'relative' => false ) ) ); ?>"><?php bp_member_registered(); ?></span> 122 <?php elseif ( 'active' == $instance['friend_default'] ) : ?> 123 <span class="activity" data-livestamp="<?php bp_core_iso8601_date( bp_get_member_last_active( array( 'relative' => false ) ) ); ?>"><?php bp_member_last_active(); ?></span> 124 <?php else : ?> 125 <span class="activity"><?php bp_member_total_friend_count(); ?></span> 126 <?php endif; ?> 127 </div> 128 </div> 129 </li> 130 131 <?php endwhile; ?> 132 </ul> 133 <?php wp_nonce_field( 'bp_core_widget_friends', '_wpnonce-friends' ); ?> 134 <input type="hidden" name="friends_widget_max" id="friends_widget_max" value="<?php echo absint( $instance['max_friends'] ); ?>" /> 135 136 <?php else: ?> 137 138 <div class="widget-error"> 139 <?php _e( 'Sorry, no members were found.', 'buddypress' ); ?> 140 </div> 141 142 <?php endif; ?> 143 144 <?php echo $after_widget; 145 146 // Restore the global. 147 $members_template = $old_members_template; 148 } 149 150 /** 151 * Process a widget save. 152 * 153 * @since 1.9.0 154 * 155 * @param array $new_instance The parameters saved by the user. 156 * @param array $old_instance The parameters as previously saved to the database. 157 * @return array $instance The processed settings to save. 158 */ 159 function update( $new_instance, $old_instance ) { 160 $instance = $old_instance; 161 162 $instance['max_friends'] = absint( $new_instance['max_friends'] ); 163 $instance['friend_default'] = sanitize_text_field( $new_instance['friend_default'] ); 164 $instance['link_title'] = ! empty( $new_instance['link_title'] ); 165 166 return $instance; 167 } 168 169 /** 170 * Render the widget edit form. 171 * 172 * @since 1.9.0 173 * 174 * @param array $instance The saved widget settings. 175 * @return void 176 */ 177 function form( $instance ) { 178 $defaults = array( 179 'max_friends' => 5, 180 'friend_default' => 'active', 181 'link_title' => false 182 ); 183 $instance = wp_parse_args( (array) $instance, $defaults ); 184 185 $max_friends = $instance['max_friends']; 186 $friend_default = $instance['friend_default']; 187 $link_title = (bool) $instance['link_title']; 188 ?> 189 190 <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> 191 192 <p><label for="<?php echo $this->get_field_id( 'max_friends' ); ?>"><?php _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> 193 194 <p> 195 <label for="<?php echo $this->get_field_id( 'friend_default' ) ?>"><?php _e( 'Default friends to show:', 'buddypress' ); ?></label> 196 <select name="<?php echo $this->get_field_name( 'friend_default' ); ?>" id="<?php echo $this->get_field_id( 'friend_default' ); ?>"> 197 <option value="newest" <?php selected( $friend_default, 'newest' ); ?>><?php _e( 'Newest', 'buddypress' ); ?></option> 198 <option value="active" <?php selected( $friend_default, 'active' );?>><?php _e( 'Active', 'buddypress' ); ?></option> 199 <option value="popular" <?php selected( $friend_default, 'popular' ); ?>><?php _e( 'Popular', 'buddypress' ); ?></option> 200 </select> 201 </p> 202 203 <?php 204 } 205 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Apr 22 01:01:41 2021 | Cross-referenced by PHPXref 0.7.1 |