Skip to:
Content

BuddyPress.org

Changeset 10519


Ignore:
Timestamp:
02/05/2016 04:37:45 AM (10 years ago)
Author:
boonebgorges
Message:

Move bp-friends classes to their own files.

See #6870.

Location:
trunk/src/bp-friends
Files:
2 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-friends/bp-friends-loader.php

    r10417 r10519  
    1313defined( 'ABSPATH' ) || exit;
    1414
    15 /**
    16  * Defines the BuddyPress Friends Component.
    17  */
    18 class BP_Friends_Component extends BP_Component {
    19 
    20         /**
    21          * Start the friends component creation process.
    22          *
    23          * @since 1.5.0
    24          */
    25         public function __construct() {
    26                 parent::start(
    27                         'friends',
    28                         _x( 'Friend Connections', 'Friends screen page <title>', 'buddypress' ),
    29                         buddypress()->plugin_dir,
    30                         array(
    31                                 'adminbar_myaccount_order' => 60
    32                         )
    33                 );
    34         }
    35 
    36         /**
    37          * Include bp-friends files.
    38          *
    39          * @see BP_Component::includes() for description of parameters.
    40          *
    41          * @param array $includes See {@link BP_Component::includes()}.
    42          */
    43         public function includes( $includes = array() ) {
    44                 $includes = array(
    45                         'cache',
    46                         'actions',
    47                         'screens',
    48                         'filters',
    49                         'classes',
    50                         'activity',
    51                         'template',
    52                         'functions',
    53                         'notifications',
    54                         'widgets',
    55                 );
    56 
    57                 parent::includes( $includes );
    58         }
    59 
    60         /**
    61          * Set up bp-friends global settings.
    62          *
    63          * The BP_FRIENDS_SLUG constant is deprecated, and only used here for
    64          * backwards compatibility.
    65          *
    66          * @since 1.5.0
    67          *
    68          * @see BP_Component::setup_globals() for description of parameters.
    69          *
    70          * @param array $args See {@link BP_Component::setup_globals()}.
    71          */
    72         public function setup_globals( $args = array() ) {
    73                 $bp = buddypress();
    74 
    75                 // Deprecated. Do not use.
    76                 // Defined conditionally to support unit tests.
    77                 if ( ! defined( 'BP_FRIENDS_DB_VERSION' ) ) {
    78                         define( 'BP_FRIENDS_DB_VERSION', '1800' );
    79                 }
    80 
    81                 // Define a slug, if necessary.
    82                 if ( ! defined( 'BP_FRIENDS_SLUG' ) ) {
    83                         define( 'BP_FRIENDS_SLUG', $this->id );
    84                 }
    85 
    86                 // Global tables for the friends component.
    87                 $global_tables = array(
    88                         'table_name'      => $bp->table_prefix . 'bp_friends',
    89                         'table_name_meta' => $bp->table_prefix . 'bp_friends_meta',
    90                 );
    91 
    92                 // All globals for the friends component.
    93                 // Note that global_tables is included in this array.
    94                 $args = array(
    95                         'slug'                  => BP_FRIENDS_SLUG,
    96                         'has_directory'         => false,
    97                         'search_string'         => __( 'Search Friends...', 'buddypress' ),
    98                         'notification_callback' => 'friends_format_notifications',
    99                         'global_tables'         => $global_tables
    100                 );
    101 
    102                 parent::setup_globals( $args );
    103         }
    104 
    105         /**
    106          * Set up component navigation.
    107          *
    108          * @since 1.5.0
    109          *
    110          * @see BP_Component::setup_nav() for a description of arguments.
    111          *
    112          * @param array $main_nav Optional. See BP_Component::setup_nav() for
    113          *                        description.
    114          * @param array $sub_nav  Optional. See BP_Component::setup_nav() for
    115          *                        description.
    116          */
    117         public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
    118 
    119                 // Determine user to use.
    120                 if ( bp_displayed_user_domain() ) {
    121                         $user_domain = bp_displayed_user_domain();
    122                 } elseif ( bp_loggedin_user_domain() ) {
    123                         $user_domain = bp_loggedin_user_domain();
    124                 } else {
    125                         return;
    126                 }
    127 
    128                 $access       = bp_core_can_edit_settings();
    129                 $slug         = bp_get_friends_slug();
    130                 $friends_link = trailingslashit( $user_domain . $slug );
    131 
    132                 // Add 'Friends' to the main navigation.
    133                 $count    = friends_get_total_friend_count();
    134                 $class    = ( 0 === $count ) ? 'no-count' : 'count';
    135                 $main_nav = array(
    136                         'name'                => sprintf( __( 'Friends <span class="%s">%s</span>', 'buddypress' ), esc_attr( $class ), bp_core_number_format( $count ) ),
    137                         'slug'                => $slug,
    138                         'position'            => 60,
    139                         'screen_function'     => 'friends_screen_my_friends',
    140                         'default_subnav_slug' => 'my-friends',
    141                         'item_css_id'         => $this->id
    142                 );
    143 
    144                 // Add the subnav items to the friends nav item.
    145                 $sub_nav[] = array(
    146                         'name'            => _x( 'Friendships', 'Friends screen sub nav', 'buddypress' ),
    147                         'slug'            => 'my-friends',
    148                         'parent_url'      => $friends_link,
    149                         'parent_slug'     => $slug,
    150                         'screen_function' => 'friends_screen_my_friends',
    151                         'position'        => 10,
    152                         'item_css_id'     => 'friends-my-friends'
    153                 );
    154 
    155                 $sub_nav[] = array(
    156                         'name'            => _x( 'Requests', 'Friends screen sub nav', 'buddypress' ),
    157                         'slug'            => 'requests',
    158                         'parent_url'      => $friends_link,
    159                         'parent_slug'     => $slug,
    160                         'screen_function' => 'friends_screen_requests',
    161                         'position'        => 20,
    162                         'user_has_access' => $access
    163                 );
    164 
    165                 parent::setup_nav( $main_nav, $sub_nav );
    166         }
    167 
    168         /**
    169          * Set up bp-friends integration with the WordPress admin bar.
    170          *
    171          * @since 1.5.0
    172          *
    173          * @see BP_Component::setup_admin_bar() for a description of arguments.
    174          *
    175          * @param array $wp_admin_nav See BP_Component::setup_admin_bar()
    176          *                            for description.
    177          */
    178         public function setup_admin_bar( $wp_admin_nav = array() ) {
    179 
    180                 // Menus for logged in user.
    181                 if ( is_user_logged_in() ) {
    182 
    183                         // Setup the logged in user variables.
    184                         $friends_link = trailingslashit( bp_loggedin_user_domain() . bp_get_friends_slug() );
    185 
    186                         // Pending friend requests.
    187                         $count = count( friends_get_friendship_request_user_ids( bp_loggedin_user_id() ) );
    188                         if ( !empty( $count ) ) {
    189                                 $title   = sprintf( _x( 'Friends <span class="count">%s</span>',          'My Account Friends menu',         'buddypress' ), bp_core_number_format( $count ) );
    190                                 $pending = sprintf( _x( 'Pending Requests <span class="count">%s</span>', 'My Account Friends menu sub nav', 'buddypress' ), bp_core_number_format( $count ) );
    191                         } else {
    192                                 $title   = _x( 'Friends',            'My Account Friends menu',         'buddypress' );
    193                                 $pending = _x( 'No Pending Requests','My Account Friends menu sub nav', 'buddypress' );
    194                         }
    195 
    196                         // Add the "My Account" sub menus.
    197                         $wp_admin_nav[] = array(
    198                                 'parent' => buddypress()->my_account_menu_id,
    199                                 'id'     => 'my-account-' . $this->id,
    200                                 'title'  => $title,
    201                                 'href'   => $friends_link
    202                         );
    203 
    204                         // My Friends.
    205                         $wp_admin_nav[] = array(
    206                                 'parent' => 'my-account-' . $this->id,
    207                                 'id'     => 'my-account-' . $this->id . '-friendships',
    208                                 'title'  => _x( 'Friendships', 'My Account Friends menu sub nav', 'buddypress' ),
    209                                 'href'   => $friends_link
    210                         );
    211 
    212                         // Requests.
    213                         $wp_admin_nav[] = array(
    214                                 'parent' => 'my-account-' . $this->id,
    215                                 'id'     => 'my-account-' . $this->id . '-requests',
    216                                 'title'  => $pending,
    217                                 'href'   => trailingslashit( $friends_link . 'requests' )
    218                         );
    219                 }
    220 
    221                 parent::setup_admin_bar( $wp_admin_nav );
    222         }
    223 
    224         /**
    225          * Set up the title for pages and <title>.
    226          */
    227         public function setup_title() {
    228 
    229                 // Adjust title.
    230                 if ( bp_is_friends_component() ) {
    231                         $bp = buddypress();
    232 
    233                         if ( bp_is_my_profile() ) {
    234                                 $bp->bp_options_title = __( 'Friendships', 'buddypress' );
    235                         } else {
    236                                 $bp->bp_options_avatar = bp_core_fetch_avatar( array(
    237                                         'item_id' => bp_displayed_user_id(),
    238                                         'type'    => 'thumb',
    239                                         'alt'     => sprintf( __( 'Profile picture of %s', 'buddypress' ), bp_get_displayed_user_fullname() )
    240                                 ) );
    241                                 $bp->bp_options_title = bp_get_displayed_user_fullname();
    242                         }
    243                 }
    244 
    245                 parent::setup_title();
    246         }
    247 
    248         /**
    249          * Setup cache groups.
    250          *
    251          * @since 2.2.0
    252          */
    253         public function setup_cache_groups() {
    254 
    255                 // Global groups.
    256                 wp_cache_add_global_groups( array(
    257                         'bp_friends_requests'
    258                 ) );
    259 
    260                 parent::setup_cache_groups();
    261         }
    262 }
     15require dirname( __FILE__ ) . '/classes/class-bp-friends-component.php';
    26316
    26417/**
  • trunk/src/bp-friends/bp-friends-widgets.php

    r10323 r10519  
    11<?php
    22/**
    3  * BuddyPress Widgets.
     3 * BuddyPress Friends Widgets.
    44 *
    55 * @package BuddyPress
     
    1010// Exit if accessed directly.
    1111defined( 'ABSPATH' ) || exit;
     12
     13require dirname( __FILE__ ) . '/classes/class-bp-friends-widget.php';
    1214
    1315/**
     
    3133}
    3234add_action( 'bp_register_widgets', 'bp_friends_register_widgets' );
    33 
    34 /*** MEMBER FRIENDS WIDGET *****************/
    35 
    36 /**
    37  * The User Friends widget class.
    38  *
    39  * @since 1.9.0
    40  */
    41 class BP_Core_Friends_Widget extends WP_Widget {
    42 
    43         /**
    44          * Class constructor.
    45          */
    46         function __construct() {
    47                 $widget_ops = array(
    48                         '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' ),
    49                         'classname' => 'widget_bp_core_friends_widget buddypress widget',
    50                 );
    51                 parent::__construct( false, $name = _x( '(BuddyPress) Friends', 'widget name', 'buddypress' ), $widget_ops );
    52 
    53         }
    54 
    55         /**
    56          * Display the widget.
    57          *
    58          * @param array $args Widget arguments.
    59          * @param array $instance The widget settings, as saved by the user.
    60          */
    61         function widget( $args, $instance ) {
    62                 extract( $args );
    63 
    64                 if ( ! bp_displayed_user_id() ) {
    65                         return;
    66                 }
    67 
    68                 $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
    69                 wp_enqueue_script( 'bp_core_widget_friends-js', buddypress()->plugin_url . "bp-friends/js/widget-friends{$min}.js", array( 'jquery' ), bp_get_version() );
    70 
    71                 $user_id = bp_displayed_user_id();
    72                 $link = trailingslashit( bp_displayed_user_domain() . bp_get_friends_slug() );
    73                 $instance['title'] = sprintf( __( "%s's Friends", 'buddypress' ), bp_get_displayed_user_fullname() );
    74 
    75                 if ( empty( $instance['friend_default'] ) ) {
    76                         $instance['friend_default'] = 'active';
    77                 }
    78 
    79                 /**
    80                  * Filters the Friends widget title.
    81                  *
    82                  * @since 1.8.0
    83                  * @since 2.3.0 Added 'instance' and 'id_base' to arguments passed to filter.
    84                  *
    85                  * @param string $title    The widget title.
    86                  * @param array  $instance The settings for the particular instance of the widget.
    87                  * @param string $id_base  Root ID for all widgets of this type.
    88                  */
    89                 $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
    90 
    91                 echo $before_widget;
    92 
    93                 $title = $instance['link_title'] ? '<a href="' . esc_url( $link ) . '">' . esc_html( $title ) . '</a>' : esc_html( $title );
    94 
    95                 echo $before_title . $title . $after_title;
    96 
    97                 $members_args = array(
    98                         'user_id'         => absint( $user_id ),
    99                         'type'            => sanitize_text_field( $instance['friend_default'] ),
    100                         'max'             => absint( $instance['max_friends'] ),
    101                         'populate_extras' => 1,
    102                 );
    103 
    104                 ?>
    105 
    106                 <?php if ( bp_has_members( $members_args ) ) : ?>
    107                         <div class="item-options" id="friends-list-options">
    108                                 <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>
    109                                 | <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>
    110                                 | <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>
    111                         </div>
    112 
    113                         <ul id="friends-list" class="item-list">
    114                                 <?php while ( bp_members() ) : bp_the_member(); ?>
    115                                         <li class="vcard">
    116                                                 <div class="item-avatar">
    117                                                         <a href="<?php bp_member_permalink(); ?>" title="<?php bp_member_name(); ?>"><?php bp_member_avatar(); ?></a>
    118                                                 </div>
    119 
    120                                                 <div class="item">
    121                                                         <div class="item-title fn"><a href="<?php bp_member_permalink(); ?>" title="<?php bp_member_name(); ?>"><?php bp_member_name(); ?></a></div>
    122                                                         <div class="item-meta">
    123                                                                 <span class="activity">
    124                                                                 <?php
    125                                                                         if ( 'newest' == $instance['friend_default'] )
    126                                                                                 bp_member_registered();
    127                                                                         if ( 'active' == $instance['friend_default'] )
    128                                                                                 bp_member_last_active();
    129                                                                         if ( 'popular' == $instance['friend_default'] )
    130                                                                                 bp_member_total_friend_count();
    131                                                                 ?>
    132                                                                 </span>
    133                                                         </div>
    134                                                 </div>
    135                                         </li>
    136 
    137                                 <?php endwhile; ?>
    138                         </ul>
    139                         <?php wp_nonce_field( 'bp_core_widget_friends', '_wpnonce-friends' ); ?>
    140                         <input type="hidden" name="friends_widget_max" id="friends_widget_max" value="<?php echo absint( $instance['max_friends'] ); ?>" />
    141 
    142                 <?php else: ?>
    143 
    144                         <div class="widget-error">
    145                                 <?php _e( 'Sorry, no members were found.', 'buddypress' ); ?>
    146                         </div>
    147 
    148                 <?php endif; ?>
    149 
    150                 <?php echo $after_widget; ?>
    151         <?php
    152         }
    153 
    154         /**
    155          * Process a widget save.
    156          *
    157          * @param array $new_instance The parameters saved by the user.
    158          * @param array $old_instance The parameters as previously saved to the database.
    159          * @return array $instance The processed settings to save.
    160          */
    161         function update( $new_instance, $old_instance ) {
    162                 $instance = $old_instance;
    163 
    164                 $instance['max_friends']    = absint( $new_instance['max_friends'] );
    165                 $instance['friend_default'] = sanitize_text_field( $new_instance['friend_default'] );
    166                 $instance['link_title']     = (bool) $new_instance['link_title'];
    167 
    168                 return $instance;
    169         }
    170 
    171         /**
    172          * Render the widget edit form.
    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 }
    20635
    20736/** Widget AJAX ***************************************************************/
  • trunk/src/bp-friends/classes/class-bp-friends-component.php

    r10515 r10519  
    261261        }
    262262}
    263 
    264 /**
    265  * Set up the bp-forums component.
    266  */
    267 function bp_setup_friends() {
    268         buddypress()->friends = new BP_Friends_Component();
    269 }
    270 add_action( 'bp_setup_components', 'bp_setup_friends', 6 );
  • trunk/src/bp-friends/classes/class-bp-friends-widget.php

    r10515 r10519  
    11<?php
    22/**
    3  * BuddyPress Widgets.
     3 * BuddyPress Friends Widget.
    44 *
    55 * @package BuddyPress
     
    1010// Exit if accessed directly.
    1111defined( 'ABSPATH' ) || exit;
    12 
    13 /**
    14  * Register the friends widget.
    15  *
    16  * @since 1.9.0
    17  */
    18 function bp_friends_register_widgets() {
    19         if ( ! bp_is_active( 'friends' ) ) {
    20                 return;
    21         }
    22 
    23         // The Friends widget works only when looking an a displayed user,
    24         // and the concept of "displayed user" doesn't exist on non-root blogs,
    25         // so we don't register the widget there.
    26         if ( ! bp_is_root_blog() ) {
    27                 return;
    28         }
    29 
    30         add_action( 'widgets_init', create_function( '', 'return register_widget("BP_Core_Friends_Widget");' ) );
    31 }
    32 add_action( 'bp_register_widgets', 'bp_friends_register_widgets' );
    33 
    34 /*** MEMBER FRIENDS WIDGET *****************/
    3512
    3613/**
     
    204181        }
    205182}
    206 
    207 /** Widget AJAX ***************************************************************/
    208 
    209 /**
    210  * Process AJAX pagination or filtering for the Friends widget.
    211  *
    212  * @since 1.9.0
    213  */
    214 function bp_core_ajax_widget_friends() {
    215 
    216         check_ajax_referer( 'bp_core_widget_friends' );
    217 
    218         switch ( $_POST['filter'] ) {
    219                 case 'newest-friends':
    220                         $type = 'newest';
    221                         break;
    222 
    223                 case 'recently-active-friends':
    224                         $type = 'active';
    225                         break;
    226 
    227                 case 'popular-friends':
    228                         $type = 'popular';
    229                         break;
    230         }
    231 
    232         $members_args = array(
    233                 'user_id'         => bp_displayed_user_id(),
    234                 'type'            => $type,
    235                 'max'             => absint( $_POST['max-friends'] ),
    236                 'populate_extras' => 1,
    237         );
    238 
    239         if ( bp_has_members( $members_args ) ) : ?>
    240                 <?php echo '0[[SPLIT]]'; // Return valid result. TODO: remove this. ?>
    241                 <?php while ( bp_members() ) : bp_the_member(); ?>
    242                         <li class="vcard">
    243                                 <div class="item-avatar">
    244                                         <a href="<?php bp_member_permalink(); ?>"><?php bp_member_avatar(); ?></a>
    245                                 </div>
    246 
    247                                 <div class="item">
    248                                         <div class="item-title fn"><a href="<?php bp_member_permalink(); ?>" title="<?php bp_member_name(); ?>"><?php bp_member_name(); ?></a></div>
    249                                         <?php if ( 'active' == $type ) : ?>
    250                                                 <div class="item-meta"><span class="activity"><?php bp_member_last_active(); ?></span></div>
    251                                         <?php elseif ( 'newest' == $type ) : ?>
    252                                                 <div class="item-meta"><span class="activity"><?php bp_member_registered(); ?></span></div>
    253                                         <?php elseif ( bp_is_active( 'friends' ) ) : ?>
    254                                                 <div class="item-meta"><span class="activity"><?php bp_member_total_friend_count(); ?></span></div>
    255                                         <?php endif; ?>
    256                                 </div>
    257                         </li>
    258                 <?php endwhile; ?>
    259 
    260         <?php else: ?>
    261                 <?php echo "-1[[SPLIT]]<li>"; ?>
    262                 <?php _e( 'There were no members found, please try another filter.', 'buddypress' ); ?>
    263                 <?php echo "</li>"; ?>
    264         <?php endif;
    265 }
    266 add_action( 'wp_ajax_widget_friends', 'bp_core_ajax_widget_friends' );
    267 add_action( 'wp_ajax_nopriv_widget_friends', 'bp_core_ajax_widget_friends' );
Note: See TracChangeset for help on using the changeset viewer.