Making WordPress.org

Changeset 8952


Ignore:
Timestamp:
06/14/2019 05:59:45 AM (6 years ago)
Author:
tellyworth
Message:

Make/meetings: adapt the meeting_time shortcode so it can be used on all Make sites.

Adds several parameters so it can be used in a sidebar. For example, to list all meetings for the Meta team:

[meeting_time team="meta" before="" more=0 limit=-1 /]

See #3078

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-meeting-posttype/wporg-meeting-posttype.php

    r8922 r8952  
    220220            ),
    221221        );
    222         register_post_type( 'meeting', $args );
     222        register_post_type( 'meeting', $args );
    223223    }
    224224
     
    428428    public function meeting_time_shortcode( $attr, $content = '' ) {
    429429
     430        $attr = shortcode_atts( array(
     431            'team' => null,
     432            'limit' => 1,
     433            'before' => __( 'Next meeting: ', 'wporg' ),
     434            'titletag' => 'strong',
     435            'more' => true,
     436        ), $attr );
     437
    430438        if ( empty( $attr['team'] ) ) {
    431439            return '';
     
    435443            $attr['team'] = 'Docs';
    436444        }
     445
     446        if ( ! has_action( 'wp_footer', array( $this, 'time_conversion_script' ) ) ) {
     447            add_action( 'wp_footer', array( $this, 'time_conversion_script' ), 999 );
     448        }
     449
    437450
    438451        // meta query to eliminate expired meetings from query
     
    441454        } );
    442455
     456        switch_to_blog( get_main_site_id() );
     457
    443458        $query = new WP_Query(
    444459            array(
    445460                'post_type' => 'meeting',
    446                 'nopaging'  => true,
     461                'posts_per_page'  => $attr['limit'],
    447462                'meta_query' => array(
    448463                    'relation' => 'AND',
     
    457472        );
    458473
    459         if ( count( $query->posts ) > 0 ) {
    460 
    461             $post = $query->posts[0];
     474        $out = '';
     475        foreach ( $query->posts as $post ) {
    462476            $next_meeting_datestring = $post->next_date;
    463477            $utc_time = strftime( '%H:%M:%S', strtotime( $post->time ) );
    464478            $next_meeting_iso        = $next_meeting_datestring . 'T' . $utc_time . '+00:00';
    465             $next_meeeting_timestamp = strtotime( $next_meeting_datestring . $utc_time );
     479            $next_meeting_timestamp = strtotime( $next_meeting_datestring . ' '. $utc_time );
     480            $next_meeting_display = strftime( '%c %Z', $next_meeting_timestamp );
     481
     482            $slack_channel = null;
     483            if ( $post->location && preg_match( '/^#([-\w]+)$/', trim( $post->location ), $match ) ) {
     484                $slack_channel = sanitize_title( $match[1] );
     485            }
    466486   
    467             $out = '<p>';
    468             $out .= 'Next meeting: ' . __( $post->post_title );
     487            $out .= '<p>';
     488            $out .= esc_html( $attr['before'] );
     489            $out .= '<strong class="meeting-title">' . esc_html( $post->post_title ) . '</strong>';
    469490            $display_count = count( $query->posts ) - 1;
    470             $out .= $display_count === 0 ? '' : ' <a title="Click to view all meetings for this team" href="/meetings#' . esc_attr( $attr['team'] ) . '">' . sprintf( __( '(+%s more)'), $display_count ) . '</a>';
     491            if ( $attr['more'] ) {
     492                $out .= $display_count === 0 ? '' : ' <a title="Click to view all meetings for this team" href="/meetings#' . esc_attr( $attr['team'] ) . '">' . sprintf( __( '(+%s more)'), $display_count ) . '</a>';
     493            }
    471494            $out .= '</br>';
    472             $out .= '<time class="date" date-time="' . esc_attr( $next_meeting_iso ) . '" title="' . esc_attr( $next_meeting_iso ) . '">' . $next_meeting_iso . '</time> ';
    473             $out .= sprintf( __( '(%s from now)' ), human_time_diff( $next_meeeting_timestamp, current_time('timestamp') ) );
    474             $out .= empty( $post->location ) ? '' : ' ' . sprintf( __('at %s on Slack'), $post->location );
     495            $out .= '<time class="date" date-time="' . esc_attr( $next_meeting_iso ) . '" title="' . esc_attr( $next_meeting_iso ) . '">' . $next_meeting_display . '</time> ';
     496            $out .= sprintf( esc_html__( '(%s from now)' ), human_time_diff( $next_meeting_timestamp, current_time('timestamp') ) );
     497            if ( $post->location && $slack_channel ) {
     498                $out .= ' ' . sprintf( wp_kses( __('at <a href="%s">%s</a> on Slack'), array(  'a' => array( 'href' => array() ) ) ), 'https://wordpress.slack.com/messages/' . $slack_channel,   $post->location );
     499            }
    475500            $out .= '</p>';
    476501        }
    477502
     503        restore_current_blog();
    478504
    479505        return $out;
     
    521547            ),
    522548        );
     549
     550    public function time_conversion_script() {
     551        echo <<<EOF
     552<script type="text/javascript">
     553
     554    var parse_date = function (text) {
     555        var m = /^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})\+00:00$/.exec(text);
     556        var d = new Date();
     557        d.setUTCFullYear(+m[1]);
     558        d.setUTCDate(+m[3]);
     559        d.setUTCMonth(+m[2]-1);
     560        d.setUTCHours(+m[4]);
     561        d.setUTCMinutes(+m[5]);
     562        d.setUTCSeconds(+m[6]);
     563        return d;
     564    }
     565    var format_time = function (d) {
     566        return d.toLocaleTimeString(navigator.language, {weekday: 'long', hour: '2-digit', minute: '2-digit', timeZoneName: 'short'});
     567    }
     568
     569    var nodes = document.getElementsByTagName('time');
     570    for (var i=0; i<nodes.length; ++i) {
     571        var node = nodes[i];
     572        if (node.className === 'date') {
     573            var d = parse_date(node.getAttribute('date-time'));
     574            if (d) {
     575                node.textContent = format_time(d);
     576            }
     577        }
     578    }
     579</script>
     580EOF;
     581    }
    523582}
    524583
Note: See TracChangeset for help on using the changeset viewer.