Making WordPress.org

Changeset 779


Ignore:
Timestamp:
08/11/2014 07:10:43 PM (12 years ago)
Author:
iandunn
Message:

WordCamp Post Types: Add 'track' parameter to [speakers] shortcode.

The id attribute on each speaker div is now deprecated, because the shortcode now supports use cases where the
shortcode is used multiple times on a single page, and assigning duplicate IDs violates the HTML spec.

Fixes #561
props hlashbrooke

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types/wc-post-types.php

    r758 r779  
    250250                global $post;
    251251
     252                // Prepare the shortcode arguments
    252253                $attr = shortcode_atts( array(
    253254                        'show_avatars' => true,
    … …  
    256257                        'orderby' => 'date',
    257258                        'order' => 'desc',
     259                        'track' => 'all',
    258260                ), $attr );
    259261
    … …  
    262264                $attr['order'] = ( in_array( $attr['order'], array( 'asc', 'desc') ) ) ? $attr['order'] : 'desc';
    263265
    264                 $speakers = new WP_Query( array(
     266                /*
     267                 * Only allow 2014.capetown to use the new track attribute
     268                 * @todo Remove this and update docs after stakeholder review
     269                 */
     270                if ( ! in_array( get_current_blog_id(), apply_filters( 'wcpt_filter_speakers_by_track_allowed_sites', array( 423 ) ) ) ) {
     271                        $attr['track'] = 'all';
     272                }
     273
     274                // Fetch all the relevant sessions
     275                $session_args = array(
     276                        'post_type'      => 'wcb_session',
     277                        'posts_per_page' => -1,
     278                );
     279
     280                if ( 'all' != $attr['track'] ) {
     281                        $session_args['tax_query'] = array(
     282                                array(
     283                                        'taxonomy' => 'wcb_track',
     284                                        'field'    => 'slug',
     285                                        'terms'    => explode( ',', $attr['track'] ),
     286                                ),
     287                        );
     288                }
     289
     290                $sessions = get_posts( $session_args );
     291
     292                // Parse the sessions
     293                $speaker_ids = $speakers_tracks = array();
     294                foreach ( $sessions as $session ) {
     295                        // Get the speaker IDs for all the sessions in the requested tracks
     296                        $session_speaker_ids = get_post_meta( $session->ID, '_wcpt_speaker_id' );
     297                        $speaker_ids = array_merge( $speaker_ids, $session_speaker_ids );
     298
     299                        // Map speaker IDs to their corresponding tracks
     300                        $session_terms = wp_get_object_terms( $session->ID, 'wcb_track' );
     301                        foreach ( $session_speaker_ids as $speaker_id ) {
     302                                if ( isset ( $speakers_tracks[ $speaker_id ] ) ) {
     303                                        $speakers_tracks[ $speaker_id ] = array_merge( $speakers_tracks[ $speaker_id ], wp_list_pluck( $session_terms, 'slug' ) );
     304                                } else {
     305                                        $speakers_tracks[ $speaker_id ] = wp_list_pluck( $session_terms, 'slug' );
     306                                }
     307                        }
     308                }
     309
     310                // Remove duplicate entries
     311                $speaker_ids = array_unique( $speaker_ids );
     312                foreach ( $speakers_tracks as $speaker_id => $tracks ) {
     313                        $speakers_tracks[ $speaker_id ] = array_unique( $tracks );
     314                }
     315
     316                // Fetch all specified speakers
     317                $speaker_args = array(
    265318                        'post_type' => 'wcb_speaker',
    266319                        'posts_per_page' => intval( $attr['posts_per_page'] ),
     320                        'post__in' => $speaker_ids,
    267321                        'orderby' => $attr['orderby'],
    268322                        'order' => $attr['order'],
    269                 ) );
     323                );
     324
     325                $speakers = new WP_Query( $speaker_args );
    270326
    271327                if ( ! $speakers->have_posts() )
    272328                        return '';
    273329
     330                // Render the HTML for the shortcode
    274331                ob_start();
    275332                ?>
    … …  
    279336                        <?php while ( $speakers->have_posts() ) : $speakers->the_post(); ?>
    280337
    281                                 <div id="wcorg-speaker-<?php echo sanitize_html_class( $post->post_name ); ?>" class="wcorg-speaker">
     338                                <?php
     339                                        $speaker_classes = array( 'wcorg-speaker', 'wcorg-speaker-' . sanitize_html_class( $post->post_name ) );
     340                                        foreach ( $speakers_tracks[ get_the_ID() ] as $track ) {
     341                                                $speaker_classes[] = sanitize_html_class( 'wcorg-track-' . $track );
     342                                        }
     343                                ?>
     344
     345                                <!-- Organizers note: The id attribute is deprecated and only remains for backwards compatibility, please use the corresponding class to target individual speakers -->
     346                                <div id="wcorg-speaker-<?php echo sanitize_html_class( $post->post_name ); ?>" class="<?php echo implode( ' ', $speaker_classes ); ?>">
    282347                                        <h2><?php the_title(); ?></h2>
    283348                                        <div class="wcorg-speaker-description">
    … …  
    285350                                                <?php the_content(); ?>
    286351                                        </div>
    287                                 </div>
     352                                </div><!-- .wcorg-speaker -->
    288353
    289354                        <?php endwhile; ?>
    290355
    291356                </div><!-- .wcorg-speakers -->
     357
    292358                <?php
    293359
    294360                wp_reset_postdata();
    295                 $content = ob_get_contents();
    296                 ob_end_clean();
    297                 return $content;
     361                return ob_get_clean();
    298362        }
    299363
Note: See TracChangeset for help on using the changeset viewer.