Skip to:
Content

BuddyPress.org

Changeset 9726


Ignore:
Timestamp:
04/09/2015 07:25:22 PM (10 years ago)
Author:
johnjamesjacoby
Message:

Members: Improvements to BP_Core_Recently_Active_Widget

  • Escape gettext output
  • Add missing semicolons after PHP execution
  • Introduce parse_settings() method for setting smart default values
  • Remove extract() usages
  • Code formatting improvements

Fixes #6363.

File:
1 edited

Legend:

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

    r9725 r9726  
    5656     * @see WP_Widget::widget() for description of parameters.
    5757     *
    58      * @param array $args Widget arguments.
     58     * @param array $args     Widget arguments.
    5959     * @param array $instance Widget settings, as saved by the user.
    6060     */
     
    153153     * Update the Members widget options.
    154154     *
    155      * @param array $new_instance The new instance options.
    156      * @param array $old_instance The old instance options.
    157      * @return array $instance The parsed options to be saved.
     155     * @param  array $new_instance The new instance options.
     156     * @param  array $old_instance The old instance options.
     157     * @return array $instance     The parsed options to be saved.
    158158     */
    159159    public function update( $new_instance, $old_instance ) {
     
    258258     * @see WP_Widget::widget() for description of parameters.
    259259     *
    260      * @param array $args Widget arguments.
     260     * @param array $args     Widget arguments.
    261261     * @param array $instance Widget settings, as saved by the user.
    262262     */
     
    321321     * Update the Who's Online widget options.
    322322     *
    323      * @param array $new_instance The new instance options.
    324      * @param array $old_instance The old instance options.
    325      * @return array $instance The parsed options to be saved.
     323     * @param  array $new_instance The new instance options.
     324     * @param  array $old_instance The old instance options.
     325     * @return array $instance     The parsed options to be saved.
    326326     */
    327327    public function update( $new_instance, $old_instance ) {
     
    388388     * Constructor method.
    389389     */
    390     function __construct() {
    391         $widget_ops = array(
    392             'description' => __( 'Profile photos of recently active members', 'buddypress' ),
    393             'classname' => 'widget_bp_core_recently_active_widget buddypress widget',
    394         );
    395         parent::__construct( false, $name = _x( '(BuddyPress) Recently Active Members', 'widget name', 'buddypress' ), $widget_ops );
     390    public function __construct() {
     391        $name        = _x( '(BuddyPress) Recently Active Members', 'widget name', 'buddypress' );
     392        $description = __( 'Profile photos of recently active members', 'buddypress' );
     393        parent::__construct( false, $name, array(
     394            'description' => $description,
     395            'classname'   => 'widget_bp_core_recently_active_widget buddypress widget',
     396        ) );
    396397    }
    397398
     
    401402     * @see WP_Widget::widget() for description of parameters.
    402403     *
    403      * @param array $args Widget arguments.
     404     * @param array $args     Widget arguments.
    404405     * @param array $instance Widget settings, as saved by the user.
    405406     */
    406     function widget( $args, $instance ) {
    407 
    408         extract( $args );
     407    public function widget( $args, $instance ) {
     408
     409        // Get widget settings
     410        $settings = $this->parse_settings( $instance );
    409411
    410412        /**
     
    415417         *
    416418         * @param string $title    The widget title.
    417          * @param array  $instance The settings for the particular instance of the widget.
     419         * @param array  $settings The settings for the particular instance of the widget.
    418420         * @param string $id_base  Root ID for all widgets of this type.
    419421         */
    420         $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
    421 
    422         echo $before_widget;
    423         echo $before_title
     422        $title = apply_filters( 'widget_title', $settings['title'], $settings, $this->id_base );
     423
     424        echo $args['before_widget'];
     425        echo $args['before_title']
    424426           . $title
    425            . $after_title;
    426 
     427           . $args['after_title'];
     428
     429        // Setup args for querying members
    427430        $members_args = array(
    428431            'user_id'         => 0,
    429432            'type'            => 'active',
    430             'per_page'        => $instance['max_members'],
    431             'max'             => $instance['max_members'],
     433            'per_page'        => $settings['max_members'],
     434            'max'             => $settings['max_members'],
    432435            'populate_extras' => true,
    433436            'search_terms'    => false,
    434         );
    435 
    436         ?>
     437        ); ?>
    437438
    438439        <?php if ( bp_has_members( $members_args ) ) : ?>
     440
    439441            <div class="avatar-block">
     442
    440443                <?php while ( bp_members() ) : bp_the_member(); ?>
     444
    441445                    <div class="item-avatar">
    442                         <a href="<?php bp_member_permalink() ?>" title="<?php bp_member_name() ?>"><?php bp_member_avatar() ?></a>
     446                        <a href="<?php bp_member_permalink(); ?>" title="<?php bp_member_name(); ?>"><?php bp_member_avatar(); ?></a>
    443447                    </div>
     448
    444449                <?php endwhile; ?>
     450
    445451            </div>
     452
    446453        <?php else: ?>
    447454
    448455            <div class="widget-error">
    449                 <?php _e( 'There are no recently active members', 'buddypress' ) ?>
     456                <?php esc_html_e( 'There are no recently active members', 'buddypress' ); ?>
    450457            </div>
    451458
    452459        <?php endif; ?>
    453460
    454         <?php echo $after_widget; ?>
    455     <?php
     461        <?php echo $args['after_widget'];
    456462    }
    457463
     
    463469     * @return array $instance The parsed options to be saved.
    464470     */
    465     function update( $new_instance, $old_instance ) {
    466         $instance = $old_instance;
    467         $instance['title'] = strip_tags( $new_instance['title'] );
     471    public function update( $new_instance, $old_instance ) {
     472        $instance                = $old_instance;
     473        $instance['title']       = strip_tags( $new_instance['title'] );
    468474        $instance['max_members'] = strip_tags( $new_instance['max_members'] );
    469475
     
    476482     * @param $instance Settings for this widget.
    477483     */
    478     function form( $instance ) {
    479         $defaults = array(
    480             'title' => __( 'Recently Active Members', 'buddypress' ),
    481             'max_members' => 15
    482         );
    483         $instance = wp_parse_args( (array) $instance, $defaults );
    484 
    485         $title = strip_tags( $instance['title'] );
    486         $max_members = strip_tags( $instance['max_members'] );
    487         ?>
    488 
    489         <p><label for="bp-core-widget-members-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>
    490 
    491         <p><label for="bp-core-widget-members-max"><?php _e('Max Members to show:', 'buddypress'); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_members' ); ?>" name="<?php echo $this->get_field_name( 'max_members' ); ?>" type="text" value="<?php echo esc_attr( $max_members ); ?>" style="width: 30%" /></label></p>
     484    public function form( $instance ) {
     485
     486        // Get widget settings
     487        $settings    = $this->parse_settings( $instance );
     488        $title       = strip_tags( $settings['title'] );
     489        $max_members = strip_tags( $settings['max_members'] ); ?>
     490
     491        <p>
     492            <label for="bp-core-widget-members-title">
     493                <?php esc_html_e( 'Title:', 'buddypress' ); ?>
     494                <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%" />
     495            </label>
     496        </p>
     497
     498        <p>
     499            <label for="bp-core-widget-members-max">
     500                <?php esc_html_e( 'Max Members to show:', 'buddypress' ); ?>
     501                <input class="widefat" id="<?php echo $this->get_field_id( 'max_members' ); ?>" name="<?php echo $this->get_field_name( 'max_members' ); ?>" type="text" value="<?php echo esc_attr( $max_members ); ?>" style="width: 30%" />
     502            </label>
     503        </p>
     504
    492505    <?php
     506    }
     507
     508    /**
     509     * Merge the widget settings into defaults array.
     510     *
     511     * @since BuddyPress (2.3.0)
     512     *
     513     * @param $instance Instance
     514     * @uses bp_parse_args() To merge widget settings into defaults
     515     */
     516    public function parse_settings( $instance = array() ) {
     517        return bp_parse_args( $instance, array(
     518            'title'          => __( 'Recently Active Members', 'buddypress' ),
     519            'max_members'    => 15,
     520        ), 'recently_active_members_widget_settings' );
    493521    }
    494522}
Note: See TracChangeset for help on using the changeset viewer.