Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/20/2013 06:28:48 PM (6 years ago)
Author:
boonebgorges
Message:

Introduce BuddyPress Login Widget

This sidebar widget shows a set of login fields to logged-out users, along with
a link to the Register page (if registration is enabled). Logged-in users will
see their avatar, a link to their profile, and a Log Out link (as in the old
bp-default sidebar). This is particularly important now that BP can be used
with any theme, even themes that do not have obvious login mechanisms on the
front end.

Borrowed in part from bbPress, and in part from the BuddyPress Default theme.

Props modemlooper for an initial patch.

Fixes #1474

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-widgets.php

    r7356 r7450  
    1212/* Register widgets for the core component */
    1313function bp_core_register_widgets() {
     14    add_action('widgets_init', create_function('', 'return register_widget("BP_Core_Login_Widget");') );
    1415    add_action('widgets_init', create_function('', 'return register_widget("BP_Core_Members_Widget");') );
    1516    add_action('widgets_init', create_function('', 'return register_widget("BP_Core_Whos_Online_Widget");') );
     
    1718}
    1819add_action( 'bp_register_widgets', 'bp_core_register_widgets' );
     20
     21/**
     22 * BuddyPress Login Widget
     23 *
     24 * @since BuddyPress (1.9.0)
     25 */
     26class BP_Core_Login_Widget extends WP_Widget {
     27
     28    /**
     29     * Constructor method.
     30     */
     31    public function __construct() {
     32        parent::__construct(
     33            false,
     34            _x( '(BuddyPress) Log In', 'Title of the login widget', 'buddypress' ),
     35            array(
     36                'description' => __( 'Show a Log In form to logged-out visitors, and a Log Out link to those who are logged in.', 'buddypress' ),
     37                'classname' => 'widget_bp_core_login_widget buddypress widget',
     38            )
     39        );
     40    }
     41
     42    /**
     43     * Display the login widget.
     44     *
     45     * @see WP_Widget::widget() for description of parameters.
     46     *
     47     * @param array $args Widget arguments.
     48     * @param array $instance Widget settings, as saved by the user.
     49     */
     50    public function widget( $args, $instance ) {
     51        $title = apply_filters( 'widget_title', $instance['title'] );
     52
     53        echo $args['before_widget'];
     54
     55        echo $args['before_title'] . esc_html( $title ) . $args['after_title']; ?>
     56
     57        <?php if ( is_user_logged_in() ) : ?>
     58
     59            <?php do_action( 'bp_before_login_widget_loggedin' ); ?>
     60
     61            <div class="bp-login-widget-user-avatar">
     62                <a href="<?php echo bp_loggedin_user_domain(); ?>">
     63                    <?php bp_loggedin_user_avatar( 'type=thumb&width=50&height=50' ); ?>
     64                </a>
     65            </div>
     66
     67            <div class="bp-login-widget-user-links">
     68                <div class="bp-login-widget-user-link"><?php echo bp_core_get_userlink( bp_loggedin_user_id() ); ?></div>
     69                <div class="bp-login-widget-user-logout"><a class="logout" href="<?php echo wp_logout_url( wp_guess_url() ); ?>"><?php _e( 'Log Out', 'buddypress' ); ?></a></div>
     70            </div>
     71
     72            <?php do_action( 'bp_after_login_widget_loggedin' ); ?>
     73
     74        <?php else : ?>
     75
     76            <?php do_action( 'bp_before_login_widget_loggedout' ); ?>
     77
     78            <form name="bp-login-form" id="bp-login-widget-form" class="standard-form" action="<?php echo site_url( 'wp-login.php', 'login_post' ); ?>" method="post">
     79                <label for="bp-login-widget-user-login"><?php _e( 'Username', 'buddypress' ); ?></label>
     80                <input type="text" name="log" id="bp-login-widget-user-login" class="input" value="" />
     81
     82                <label for="bp-login-widget-user-pass"><?php _e( 'Password', 'buddypress' ); ?></label>
     83                <input type="password" name="pwd" id="bp-login-widget-user-pass" class="input" value=""  />
     84
     85                <div class="forgetmenot"><label><input name="rememberme" type="checkbox" id="bp-login-widget-rememberme" value="forever" /> <?php _e( 'Remember Me', 'buddypress' ); ?></label></div>
     86
     87                <input type="submit" name="wp-submit" id="bp-login-widget-submit" value="<?php _e( 'Log In', 'buddypress' ); ?>" />
     88
     89                <?php if ( bp_get_signup_allowed() ) : ?>
     90
     91                    <span class="bp-login-widget-register-link"><?php printf( __( '<a href="%s" title="Register for a new account">Register</a>', 'buddypress' ), bp_get_signup_page() ); ?></span>
     92
     93                <?php endif; ?>
     94
     95                <input type="hidden" name="testcookie" value="1" />
     96            </form>
     97
     98
     99            <?php do_action( 'bp_after_login_widget_loggedout' ); ?>
     100
     101        <?php endif;
     102
     103        echo $args['after_widget'];
     104    }
     105
     106    /**
     107     * Update the login widget options.
     108     *
     109     * @param array $new_instance The new instance options.
     110     * @param array $old_instance The old instance options.
     111     * @return array $instance The parsed options to be saved.
     112     */
     113    public function update( $new_instance, $old_instance ) {
     114        $instance             = $old_instance;
     115        $instance['title']    = strip_tags( $new_instance['title'] );
     116        $instance['register'] = esc_url_raw( $new_instance['register'] );
     117        $instance['lostpass'] = esc_url_raw( $new_instance['lostpass'] );
     118
     119        return $instance;
     120    }
     121
     122    /**
     123     * Output the login widget options form.
     124     *
     125     * @param $instance Settings for this widget.
     126     */
     127    public function form( $instance = array() ) {
     128
     129        $settings = wp_parse_args( $instance, array(
     130            'title' => '',
     131        ) ); ?>
     132
     133        <p>
     134            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'buddypress' ); ?>
     135            <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( $settings['title'] ); ?>" /></label>
     136        </p>
     137
     138        <?php
     139    }
     140}
    19141
    20142/*** MEMBERS WIDGET *****************/
Note: See TracChangeset for help on using the changeset viewer.