Making WordPress.org

Changeset 10655


Ignore:
Timestamp:
02/11/2021 02:05:04 AM (5 years ago)
Author:
dd32
Message:

Support Forums: Also Viewing: Move the toggle into the forum profile screen.

This allows non-moderators with at least 20 replies to opt-in to the feature.

This change also adds a heading to the user options page to give the checkboxes a heading, and makes the banner fixing to the page smoother and more inline with the forum styling.

See #2552.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-user-notes.php

    r10615 r10655  
    3131        add_action( 'bbp_theme_before_reply_content',       array( $this, 'display_user_notes_in_content' ) );
    3232        add_action( 'bbp_template_after_user_profile',      array( $this, 'display_user_notes_in_profile' ) );
    33         add_action( 'bbp_user_edit_after',                  array( $this, 'display_user_notes_in_profile_edit' ) );
     33        add_action( 'bbp_user_edit_after',                  array( $this, 'display_user_notes_in_profile_edit' ), 20 );
    3434    }
    3535
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-users.php

    r10321 r10655  
    88        // Add custom fields to user's profile.
    99        add_action( 'bbp_user_edit_after_name',        array( $this, 'add_custom_title_input' ) );
     10        add_action( 'bbp_user_edit_after',             array( $this, 'add_options_section_header' ), 0 );
    1011        add_action( 'bbp_user_edit_after',             array( $this, 'add_auto_topic_subscription_checkbox' ) );
    1112
     
    8081        </div>
    8182        <?php
     83    }
     84
     85    /**
     86     * Add a section header to the extra user options.
     87     */
     88    public function add_options_section_header() {
     89        printf(
     90            '<h2 id="user-settings" class="entry-title">%s</h2>',
     91            esc_html__( 'User Options', 'wporg-forums' )
     92        );
    8293    }
    8394
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-bbp-also-viewing/wporg-bbp-also-viewing.js

    r10653 r10655  
    1010        page = options.currentPage || '',
    1111        banner = false,
     12        bannerOffset = 200,
    1213        isTyping = false,
    1314        _n = wp.i18n._n,
     
    4344
    4445            jQuery('#main').before(
    45                 '<div id="also-viewing-banner" style="display: none; font-size: 14px; color: #fff; line-height: 30px; font-family: Helvetica,sans-serif; background: #d54e21; border-bottom: 1px solid #dfdfdf; width:100%; height:30px; text-align: center; position: initial; top: 32px; left: 0; z-index: 9999;"></div>'
     46                '<div id="also-viewing-banner" style="display: none; font-size: 0.8rem; color: #fff; line-height: 2rem; background: #d54e21; width:100%; text-align: center; position: initial; top: 32px; left: 0; z-index: 9999;"></div>'
    4647            );
    4748            banner = jQuery( '#also-viewing-banner' );
     
    4950
    5051        if ( ! userCount ) {
    51             banner.show( false );
     52            banner.hide();
    5253        } else {
    5354            userList = currentlyViewing.map( function( item ) {
    54                 return item.who + ( item.isTyping ? ' ' + __( '(is typing)', 'wporg-bbp-also-viewing' ) : '' );
     55                return item.who + ( item.isTyping ? ' ' + __( '(is typing)', 'wporg-forums' ) : '' );
    5556            } );
    5657
    5758            if ( userCount > 1 ) {
    58                 userlistPretty = __( '%1$s and %2$s' )
     59                userlistPretty = __( '%1$s and %2$s', 'wporg-forums' )
    5960                .replace( '%1$s', userList.slice( 0, -1 ).join( ', ' ) + ( userCount > 2 ? ',' : '' ) )
    6061                .replace( '%2$s', userList.slice( -1 ) );
     
    6465
    6566            banner.text(
    66                 _n( '%s is also viewing this page.', '%s are also viewing this page.', userCount, 'wporg-bbp-also-viewing' )
     67                _n( '%s is also viewing this page.', '%s are also viewing this page.', userCount, 'wporg-forums' )
    6768                .replace( '%s', userlistPretty )
    6869            );
    69             banner.show( true );
     70            banner.show();
     71            bannerOffset = banner.offset().top - jQuery('#wpadminbar').height();
    7072        }
    7173    }
     
    7779        }
    7880
    79         if ( jQuery(window).scrollTop() > 200 ) {
     81        var $main = jQuery('#main');
     82        if ( jQuery(window).scrollTop() > bannerOffset ) {
    8083            banner.css( 'position', 'fixed' );
     84            $main.css( 'padding-top', banner.height() );
    8185        } else {
    8286            banner.css( 'position', 'initial' );
     87            $main.css( 'padding-top', 0 );
    8388        }
    84     });
     89    } );
    8590
    8691    // When a textarea is focused, mark the user as typing.
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-bbp-also-viewing/wporg-bbp-also-viewing.php

    r10654 r10655  
    2727 */
    2828
    29 const TOGGLE_KEY   = 'toggle-also-viewing';
    3029const USER_OPTION  = 'also-viewing';
    31 const NONCE        = 'also-viewing';
    3230const TIMEOUT      = 5 * \MINUTE_IN_SECONDS;
    33 const REFRESH_INT  = 45; // How often the client should check for new viewers.
     31const REFRESH_INT  = 45; // How often the client should check for new viewers in seconds.
    3432const CACHE_GROUP  = 'also-viewing';
    3533const CACHE_TIME   = 5 * \MINUTE_IN_SECONDS;
     34const REPLY_THRESH = 20; // The number of replies a user must have before the feature can be opt'd into.
    3635
    3736function init() {
     
    5150
    5251    // Add a UI to enable/disable the feature.
    53     add_action( 'admin_bar_menu', __NAMESPACE__ . '\admin_bar_menu', 1000 );
    54     maybe_toggle();
     52    add_action( 'bbp_user_edit_after', __NAMESPACE__ . '\bbp_user_edit_after' );
     53    add_action( 'bbp_profile_update', __NAMESPACE__ . '\bbp_profile_update', 10, 1 );
    5554
    5655    // If enabled, queue up the JS, and register the API endpoints.
     
    5958
    6059        // Record the user as being on the page.
    61         add_action( 'wp_head', __NAMESPACE__ . '\user_viewing', 10, 0 );
     60        add_action( 'wp_head', function() {
     61            user_viewing(); // Record the user as being on the current page.
     62        } );
    6263    }
    6364
     
    8788 */
    8889function allowed_for_user() {
    89     // TODO: Enable for non-moderator? users with more than x replies?
    90     return is_user_logged_in() && current_user_can( 'moderate' );
     90    return
     91        is_user_logged_in() &&
     92        (
     93            current_user_can( 'moderate' ) ||
     94            bbp_get_user_reply_count( get_current_user_id(), true ) >= REPLY_THRESH
     95        );
    9196}
    9297
     
    148153        filemtime( __DIR__ . '/wporg-bbp-also-viewing.js' )
    149154    );
    150     wp_set_script_translations( 'also-viewing', 'wporg-bbp-also-viewing' );
     155    wp_set_script_translations( 'also-viewing', 'wporg-forums' );
    151156
    152157    wp_localize_script(
     
    166171
    167172/**
    168  * Add a Admin bar entry to enable/disable the Also Viewing tool.
    169  */
    170 function admin_bar_menu( $wp_admin_bar ) {
    171     $args = [
    172         'id'    => 'toggle_translator',
    173         'title' => '<span class="ab-icon dashicons-welcome-view-site"></span> ' . __( 'Also Viewing', 'wporg-bbp-also-viewing' ),
    174         'href'  => wp_nonce_url( add_query_arg( TOGGLE_KEY, (int)( ! enabled() ) ), NONCE ),
    175         'meta'  => [
    176             'class' => 'toggle-also-viewing',
    177             'title' => ( enabled() ? __( 'Disable also viewing', 'wporg-bbp-also-viewing' ) : __( 'Enable also viewing', 'wporg-bbp-also-viewing' ) )
    178         ]
    179     ];
    180     $wp_admin_bar->add_node( $args );
    181 
    182     // Add a descriptive sub-child menu.
    183     $args['title'] = $args['meta']['title'];
    184     $args['parent'] = $args['id'];
    185     $args['id'] .= '-child';
    186     $wp_admin_bar->add_node( $args );
    187 }
    188 
    189 /**
    190  * Handle the admin bar toggle actions.
    191  */
    192 function maybe_toggle() {
    193     if (
    194         ! isset( $_GET[ TOGGLE_KEY ] ) ||
    195         ! isset( $_GET['_wpnonce'] ) ||
    196         ! wp_verify_nonce( $_GET['_wpnonce'], NONCE )
    197     ) {
     173 * Add an option to the user profile to enable/disable it.
     174 */
     175function bbp_user_edit_after() {
     176    if ( ! allowed_for_user() ) {
    198177        return;
    199178    }
    200179
    201     update_user_meta( get_current_user_id(), USER_OPTION, (int) $_GET[ TOGGLE_KEY ] );
     180    printf(
     181        '<p>
     182        <input name="also_viewing" id="also_viewing_toggle" type="checkbox" value="yes" %s>
     183        <label for="also_viewing_toggle">%s</label>
     184        </p>',
     185        checked( enabled(), true, false ),
     186        sprintf(
     187            'Enable the <a href="%s">Also Viewing</a> feature.', // TODO: Translate once text is figured out __( , 'wporg-forums' ),
     188            esc_attr( 'javascript:alert("The Also Viewing feature allows regular support forum contributors to opt-in to allowing other users of this feature to see who else is viewing the current page.");' ) // TODO: This needs a support page explaining what it is.
     189        )
     190    );
     191}
     192
     193/**
     194 * Save the user option to enable/disable.
     195 */
     196function bbp_profile_update( $user_id ) {
     197    $enabled = ! empty( $_REQUEST['also_viewing'] ) && 'yes' === $_REQUEST['also_viewing'];
     198
     199    update_user_meta( $user_id, USER_OPTION, (int) $enabled );
    202200
    203201    // Cleanup.
    204     if ( ! enabled() ) {
    205         clear_viewing( null, get_current_user_id() );
    206     }
    207 
    208     wp_safe_redirect( remove_query_arg( [ TOGGLE_KEY, '_wpnonce' ] ) );
    209     die();
     202    if ( ! $enabled ) {
     203        clear_viewing( null, $user_id );
     204    }
     205
    210206}
    211207
Note: See TracChangeset for help on using the changeset viewer.