Making WordPress.org

Changeset 8922


Ignore:
Timestamp:
06/05/2019 06:59:37 AM (7 years ago)
Author:
tellyworth
Message:

Make/Meetings: add a shortcode for displaying the next meeting time for a team.

This uses the meetings from the CPT on the Meetings tab, and will enable us to deprecate the redundant Weekly Meeting meta in the Sites CPT.

Props ck3lee.
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

    r7786 r8922  
    3131                add_action( 'admin_bar_menu',                     array( $mpt, 'add_edit_meetings_item_to_admin_bar' ), 80 );
    3232                add_action( 'wp_enqueue_scripts',                 array( $mpt, 'add_edit_meetings_icon_to_admin_bar' ) );
     33                add_shortcode( 'meeting_time',                    array( $mpt, 'meeting_time_shortcode' ) );
    3334        }
    3435
     
    6566
    6667                // meta query to eliminate expired meetings from query
    67                 $query->set( 'meta_query', array(
    68                         'relation'=>'OR',
    69                                 // not recurring  AND start_date >= CURDATE() = one-time meeting today or still in future
    70                                 array(
    71                                         'relation'=>'AND',
    72                                         array(
    73                                                 'key'=>'recurring',
    74                                                 'value'=>array( 'weekly', 'biweekly', 'occurrence', 'monthly', '1' ),
    75                                                 'compare'=>'NOT IN',
    76                                         ),
    77                                         array(
    78                                                 'key'=>'start_date',
    79                                                 'type'=>'DATE',
    80                                                 'compare'=>'>=',
    81                                                 'value'=>'CURDATE()',
    82                                         )
    83                                 ),
    84                                 // recurring = 1 AND ( end_date = '' OR end_date > CURDATE() ) = recurring meeting that has no end or has not ended yet
    85                                 array(
    86                                         'relation'=>'AND',
    87                                         array(
    88                                                 'key'=>'recurring',
    89                                                 'value'=>array( 'weekly', 'biweekly', 'occurrence', 'monthly', '1' ),
    90                                                 'compare'=>'IN',
    91                                         ),
    92                                         array(
    93                                                 'relation'=>'OR',
    94                                                 array(
    95                                                         'key'=>'end_date',
    96                                                         'value'=>'',
    97                                                         'compare'=>'=',
    98                                                 ),
    99                                                 array(
    100                                                         'key'=>'end_date',
    101                                                         'type'=>'DATE',
    102                                                         'compare'=>'>',
    103                                                         'value'=>'CURDATE()',
    104                                                 )
    105                                         )
    106                                 ),
    107                         )
    108                 );
     68                $query->set( 'meta_query', $this->meeting_meta_query );
    10969
    11070                // WP doesn't understand CURDATE() and prepares it as a quoted string. Repair this:
     
    463423        }
    464424
     425        /**
     426         * Renders meeting information with the next meeting time based on user's local timezone. Used in Make homepage.
     427         */
     428        public function meeting_time_shortcode( $attr, $content = '' ) {
     429
     430                if ( empty( $attr['team'] ) ) {
     431                        return '';
     432                }
     433
     434                if ( $attr['team'] === 'Documentation' ) {
     435                        $attr['team'] = 'Docs';
     436                }
     437
     438                // meta query to eliminate expired meetings from query
     439                add_filter( 'get_meta_sql', function ($sql) {
     440                        return str_replace( "'CURDATE()'", 'CURDATE()', $sql );
     441                } );
     442
     443                $query = new WP_Query(
     444                        array(
     445                                'post_type' => 'meeting',
     446                                'nopaging'  => true,
     447                                'meta_query' => array(
     448                                        'relation' => 'AND',
     449                                        array(
     450                                                'key'     => 'team',
     451                                                'value'   => $attr['team'],
     452                                                'compare' => 'EQUALS',
     453                                        ),
     454                                        $this->meeting_meta_query
     455                                )
     456                        )
     457                );
     458
     459                if ( count( $query->posts ) > 0 ) {
     460
     461                        $post = $query->posts[0];
     462                        $next_meeting_datestring = $post->next_date;
     463                        $utc_time = strftime( '%H:%M:%S', strtotime( $post->time ) );
     464                        $next_meeting_iso        = $next_meeting_datestring . 'T' . $utc_time . '+00:00';
     465                        $next_meeeting_timestamp = strtotime( $next_meeting_datestring . $utc_time );
     466       
     467                        $out = '<p>';
     468                        $out .= 'Next meeting: ' . __( $post->post_title );
     469                        $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>';
     471                        $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 );
     475                        $out .= '</p>';
     476                }
     477
     478
     479                return $out;
     480        }
     481
     482        private $meeting_meta_query = array(
     483                'relation'=>'OR',
     484                        // not recurring  AND start_date >= CURDATE() = one-time meeting today or still in future
     485                        array(
     486                                'relation'=>'AND',
     487                                array(
     488                                        'key'=>'recurring',
     489                                        'value'=>array( 'weekly', 'biweekly', 'occurrence', 'monthly', '1' ),
     490                                        'compare'=>'NOT IN',
     491                                ),
     492                                array(
     493                                        'key'=>'start_date',
     494                                        'type'=>'DATE',
     495                                        'compare'=>'>=',
     496                                        'value'=>'CURDATE()',
     497                                )
     498                        ),
     499                        // recurring = 1 AND ( end_date = '' OR end_date > CURDATE() ) = recurring meeting that has no end or has not ended yet
     500                        array(
     501                                'relation'=>'AND',
     502                                array(
     503                                        'key'=>'recurring',
     504                                        'value'=>array( 'weekly', 'biweekly', 'occurrence', 'monthly', '1' ),
     505                                        'compare'=>'IN',
     506                                ),
     507                                array(
     508                                        'relation'=>'OR',
     509                                        array(
     510                                                'key'=>'end_date',
     511                                                'value'=>'',
     512                                                'compare'=>'=',
     513                                        ),
     514                                        array(
     515                                                'key'=>'end_date',
     516                                                'type'=>'DATE',
     517                                                'compare'=>'>',
     518                                                'value'=>'CURDATE()',
     519                                        )
     520                                )
     521                        ),
     522                );
    465523}
    466524
Note: See TracChangeset for help on using the changeset viewer.