Making WordPress.org

Changeset 10743


Ignore:
Timestamp:
03/04/2021 08:12:11 AM (4 years ago)
Author:
dd32
Message:

Make: Properly hide the "Welcome" box on make o2's for logged out users.

The welcome box functionality is based on cookies, but the logic was server-side and the page output was cached for logged out users without respecting that cookie.

This change moves the logic for showing/hiding it client-side and cleans up the JS a bit.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-breathe/functions.php

    r10661 r10743  
    8383        if ( el ) {
    8484            el.addEventListener( 'click', function( e ) {
    85                 if ( jQuery( '.make-welcome .entry-content' ).is( ':hidden' ) ) {
    86                     document.cookie = el.dataset.cookie + '=' +
    87                         '; expires=Thu, 01 Jan 1970 00:00:00 GMT' +
    88                         '; domain=<?php echo esc_js( $current_site->domain ); ?>' +
    89                         '; path=<?php echo esc_js( $current_site->path ); ?>';
    90                     jQuery( '#make-welcome-toggle' ).text( '<?php esc_attr_e( 'Hide welcome box', 'wporg' ); ?>' );
    91                 } else {
    92                     document.cookie = el.dataset.cookie + '=' + el.dataset.hash +
    93                         '; expires=Fri, 31 Dec 9999 23:59:59 GMT' +
    94                         '; domain=<?php echo esc_js( $current_site->domain ); ?>' +
    95                         '; path=<?php echo esc_js( $current_site->path ); ?>';
    96                     jQuery( '#make-welcome-toggle' ).text( '<?php esc_attr_e( 'Show welcome box', 'wporg' ); ?>' );
    97                 }
    98 
    99                 jQuery( '.make-welcome .entry-content' ).slideToggle();
    100                 jQuery( '.make-welcome .post-edit-link' ).toggle();
     85                var $welcome = jQuery( '.make-welcome' ),
     86                    $toggle  = $welcome.find( '#make-welcome-toggle'),
     87                    $content = $welcome.find( '#make-welcome-content'),
     88                    isHide   = ! $content.is( ':hidden' );
     89
     90                // Toggle it
     91                $toggle.text( $toggle.data( isHide ? 'show' : 'hide' ) );
     92                $content.slideToggle();
     93                $welcome.find('.post-edit-link' ).toggle( ! isHide );
     94
     95                // Remember it
     96                document.cookie = $content.data( 'cookie' ) + '=' +
     97                    ( isHide ? $content.data( 'hash' ) : '' ) +
     98                    '; expires=Fri, 31 Dec 9999 23:59:59 GMT' +
     99                    '; domain=<?php echo esc_js( $current_site->domain ); ?>' +
     100                    '; path=<?php echo esc_js( $current_site->path ); ?>';
    101101            } );
    102102        }
     
    114114    if ( ! $welcome ) {
    115115        return;
    116     }
    117 
    118     if ( ! $hash || $content_hash !== $hash ) {
    119         $class = '';
    120         $label = __( 'Hide welcome box', 'wporg' );
    121     } else {
    122         $class = 'hidden';
    123         $label = __( 'Show welcome box', 'wporg' );
    124116    }
    125117
     
    139131    <div class="make-welcome">
    140132        <div class="entry-meta">
    141             <?php edit_post_link( __( 'Edit', 'wporg' ), '', '', $welcome->ID, 'post-edit-link ' . $class ); ?>
    142             <button type="button" id="make-welcome-toggle" data-hash="<?php echo $content_hash; ?>" data-cookie="<?php echo $cookie; ?>"><?php echo $label; ?></button>
     133            <?php edit_post_link( __( 'Edit', 'wporg' ), '', '', $welcome->ID, 'post-edit-link make-welcome-edit-post-link' ); ?>
     134            <button
     135                type="button"
     136                id="make-welcome-toggle"
     137                data-show="<?php esc_attr_e( 'Show welcome box', 'wporg' ); ?>"
     138                data-hide="<?php esc_attr_e( 'Hide welcome box', 'wporg' ); ?>"
     139            ><?php _e( 'Hide welcome box', 'wporg' ); ?></button>
    143140        </div>
    144         <div class="entry-content clear <?php echo $class; ?>">
     141        <div class="entry-content clear" id="make-welcome-content" data-cookie="<?php echo $cookie; ?>" data-hash="<?php echo $content_hash; ?>">
     142            <script type="text/javascript">
     143                var elContent = document.getElementById( 'make-welcome-content' );
     144                if ( elContent ) {
     145                    if ( -1 !== document.cookie.indexOf( elContent.dataset.cookie + '=' + elContent.dataset.hash ) ) {
     146                        var elToggle = document.getElementById( 'make-welcome-toggle' ),
     147                            elEditLink = document.getElementsByClassName( 'make-welcome-edit-post-link' )
     148
     149                        // It's hidden, hide it ASAP.
     150                        elContent.className += " hidden";
     151                        elToggle.innerText = elToggle.dataset.show;
     152
     153                        if ( elEditLink.length ) {
     154                            elEditLink[0].className += " hidden";
     155                        }
     156                    }
     157                }
     158            </script>
    145159            <?php the_content(); ?>
    146160        </div>
     
    283297
    284298    // Noindex empty/short pages
    285     if ( is_singular() && ( strlen( get_the_content() ) < 100 ) ) {
     299    if ( is_singular() && strlen( get_the_content() ) < 100 ) {
    286300        $noindex = true;
    287301    }
Note: See TracChangeset for help on using the changeset viewer.