Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/09/2015 07:41:58 PM (9 years ago)
Author:
johnjamesjacoby
Message:

Members: Improvements to BP_Core_Members_Widget's ajax handler:

  • Escape gettext output
  • Add missing semicolons after PHP execution
  • Smarter filter handling and default values
  • Code formatting improvements

See #6361.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-members/bp-members-widgets.php

    r9726 r9727  
    526526 *
    527527 * @since BuddyPress (1.0.0)
     528 *
     529 * @see BP_Core_Members_Widget
    528530 */
    529531function bp_core_ajax_widget_members() {
     
    531533    check_ajax_referer( 'bp_core_widget_members' );
    532534
    533     switch ( $_POST['filter'] ) {
    534         case 'newest-members':
     535    // Setup some variables to check
     536    $filter      = ! empty( $_POST['filter']      ) ? $_POST['filter']                : 'recently-active-members';
     537    $max_members = ! empty( $_POST['max-members'] ) ? absint( $_POST['max-members'] ) : 5;
     538
     539    // Determine the type of members query to perform
     540    switch ( $filter ) {
     541
     542        // Newest activated
     543        case 'newest-members' :
    535544            $type = 'newest';
    536545            break;
    537546
    538         case 'recently-active-members':
     547        // Popular by friends
     548        case 'popular-members' :
     549            if ( bp_is_active( 'friends' ) ) {
     550                $type = 'popular';
     551            } else {
     552                $type = 'active';
     553            }
     554            break;
     555
     556        // Default
     557        case 'recently-active-members' :
     558        default :
    539559            $type = 'active';
    540560            break;
    541 
    542         case 'popular-members':
    543             if ( bp_is_active( 'friends' ) )
    544                 $type = 'popular';
    545             else
    546                 $type = 'active';
    547 
    548             break;
    549     }
    550 
     561    }
     562
     563    // Setup args for querying members
    551564    $members_args = array(
    552565        'user_id'         => 0,
    553566        'type'            => $type,
    554         'per_page'        => $_POST['max-members'],
    555         'max'             => $_POST['max-members'],
    556         'populate_extras' => 1,
     567        'per_page'        => $max_members,
     568        'max'             => $max_members,
     569        'populate_extras' => true,
    557570        'search_terms'    => false,
    558571    );
    559572
     573    // Query for members
    560574    if ( bp_has_members( $members_args ) ) : ?>
    561575        <?php echo '0[[SPLIT]]'; // return valid result. TODO: remove this. ?>
     
    563577            <li class="vcard">
    564578                <div class="item-avatar">
    565                     <a href="<?php bp_member_permalink() ?>"><?php bp_member_avatar() ?></a>
     579                    <a href="<?php bp_member_permalink(); ?>"><?php bp_member_avatar(); ?></a>
    566580                </div>
    567581
    568582                <div class="item">
    569                     <div class="item-title fn"><a href="<?php bp_member_permalink() ?>" title="<?php bp_member_name() ?>"><?php bp_member_name() ?></a></div>
    570                     <?php if ( 'active' == $type ) : ?>
    571                         <div class="item-meta"><span class="activity"><?php bp_member_last_active() ?></span></div>
    572                     <?php elseif ( 'newest' == $type ) : ?>
    573                         <div class="item-meta"><span class="activity"><?php bp_member_registered() ?></span></div>
     583                    <div class="item-title fn"><a href="<?php bp_member_permalink(); ?>" title="<?php bp_member_name(); ?>"><?php bp_member_name(); ?></a></div>
     584                    <?php if ( 'active' === $type ) : ?>
     585                        <div class="item-meta"><span class="activity"><?php bp_member_last_active(); ?></span></div>
     586                    <?php elseif ( 'newest' === $type ) : ?>
     587                        <div class="item-meta"><span class="activity"><?php bp_member_registered(); ?></span></div>
    574588                    <?php elseif ( bp_is_active( 'friends' ) ) : ?>
    575                         <div class="item-meta"><span class="activity"><?php bp_member_total_friend_count() ?></span></div>
     589                        <div class="item-meta"><span class="activity"><?php bp_member_total_friend_count(); ?></span></div>
    576590                    <?php endif; ?>
    577591                </div>
    578592            </li>
     593
    579594        <?php endwhile; ?>
    580595
    581596    <?php else: ?>
    582597        <?php echo "-1[[SPLIT]]<li>"; ?>
    583         <?php _e( 'There were no members found, please try another filter.', 'buddypress' ) ?>
     598        <?php esc_html_e( 'There were no members found, please try another filter.', 'buddypress' ) ?>
    584599        <?php echo "</li>"; ?>
    585600    <?php endif;
    586601}
    587 add_action( 'wp_ajax_widget_members', 'bp_core_ajax_widget_members' );
     602add_action( 'wp_ajax_widget_members',        'bp_core_ajax_widget_members' );
    588603add_action( 'wp_ajax_nopriv_widget_members', 'bp_core_ajax_widget_members' );
Note: See TracChangeset for help on using the changeset viewer.