Making WordPress.org

Changeset 4315


Ignore:
Timestamp:
11/01/2016 08:58:38 PM (8 years ago)
Author:
iandunn
Message:

CampTix Badge Generator: Acesss post meta through WP_Post's magic getter

This is much cleaner and more readable than manually calling get_post_meta().

Location:
sites/trunk/wordcamp.org/public_html/wp-content/plugins/camptix-badge-generator
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/camptix-badge-generator/TODO.md

    r3015 r4315  
    11## HTML v2
    2  
     2
    33* page-breaking issues in chrome/safari
    44    * lots of potential solutions on StackOverflow, but none worked so far.
     
    1818* add checkbox to include twitter, option to pick arbitrary image, option for name instead of image, etc
    1919    * can use the [gear] icon like the Menus section does
    20    
     20
    2121* move documentation from make/comm/handbook to a `?` icon like menu/widget panels have.
    2222    * might need custom section markup for that.
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/camptix-badge-generator/includes/html-badges.php

    r3015 r4315  
    1212add_action( 'wp_enqueue_scripts',    __NAMESPACE__ . '\enqueue_previewer_scripts',   999 );  // after remove_all_previewer_styles()
    1313add_filter( 'template_include',      __NAMESPACE__ . '\use_badges_template'              );
     14add_filter( 'get_post_metadata',     __NAMESPACE__ . '\add_dynamic_post_meta', 10, 3     );
    1415
    1516/**
     
    176177 */
    177178function is_badges_preview() {
     179    /** @global \WP_Customize_Manager $wp_customize */
    178180    global $wp_customize;
    179181
     
    204206 */
    205207function render_badges_template() {
    206     /** @var $camptix \CampTix_Plugin */
     208    /** @global \CampTix_Plugin $camptix */
    207209    global $camptix;
    208    
     210
    209211    $allowed_html = array(
    210212        'span' => array(
     
    212214        ),
    213215    );
    214    
     216
    215217    $attendees = get_posts( array(
    216218        'post_type'      => 'tix_attendee',
     
    223225
    224226/**
    225  * Get all the data required to render an attendee badge
     227 * Add dynamically-generated "post meta" to `\WP_Post` objects
     228 *
     229 * This makes it possible to access dynamic data related to a post object by simply referencing `$post->foo`.
     230 * That keeps the calling code much cleaner than if it were to have to do something like
     231 * `$foo = some_custom_logic( get_post_meta( $post->ID, 'bar', true ) ); echo esc_html( $foo )`.
     232 *
     233 * @param mixed  $value
     234 * @param int    $post_id
     235 * @param string $meta_key
     236 *
     237 * @return mixed
     238 *      `null` to instruct `get_metadata()` to pull the value from the database
     239 *      Any non-null value will be returned as if it were pulled from the database
     240 */
     241function add_dynamic_post_meta( $value, $post_id, $meta_key ) {
     242    /** @global \CampTix_Plugin $camptix */
     243    global $camptix;
     244
     245    $attendee = get_post( $post_id );
     246
     247    if ( 'tix_attendee' != $attendee->post_type ) {
     248        return $value;
     249    }
     250
     251    switch ( $meta_key ) {
     252        case 'avatar_url':
     253            $value = get_avatar_url( $attendee->tix_email, array( 'size' => 600 ) );
     254            break;
     255
     256        case 'css_classes':
     257            $value = get_css_classes( $attendee );
     258            break;
     259
     260        case 'formatted_name':
     261            $value = $camptix->format_name_string(
     262                '<span class="first-name">%first%</span>
     263                 <span class="last-name">%last%</span>',
     264                $attendee->tix_first_name,
     265                $attendee->tix_last_name
     266            );
     267            break;
     268    }
     269
     270    return $value;
     271}
     272
     273/**
     274 * Get the CSS classes for an attendee element
    226275 *
    227276 * @param \WP_Post $attendee
    228277 *
    229  * @return array
    230  */
    231 function get_attendee_data( $attendee ) {
    232     /** @var $camptix \CampTix_Plugin */
    233     global $camptix;
    234     $css_classes = array( 'attendee-' . $attendee->post_name );
    235 
    236     $first_name     = get_post_meta( $attendee->ID, 'tix_first_name', true );
    237     $last_name      = get_post_meta( $attendee->ID, 'tix_last_name',  true );
    238     $formatted_name = $camptix->format_name_string(
    239         '<span class="first-name">%first%</span>
    240          <span class="last-name">%last%</span>',
    241         $first_name,
    242         $last_name
    243     );
    244 
    245     $email_address = get_post_meta( $attendee->ID, 'tix_email', true );
    246     $avatar_url    = get_avatar_url( $email_address, array( 'size' => 600 ) );
    247 
    248     $ticket        = get_post( get_post_meta( $attendee->ID, 'tix_ticket_id', true ) );
    249     $css_classes[] = 'ticket-' . $ticket->post_name;
    250 
    251     $coupon_id = get_post_meta( $attendee->ID, 'tix_coupon_id', true );
    252     if ( $coupon_id ) {
    253         $coupon        = get_post( $coupon_id );
    254         $css_classes[] = 'coupon-' . $coupon->post_name;
    255     }
    256 
    257     $css_classes = implode( ' ', $css_classes );
    258 
    259     return compact( 'formatted_name', 'email_address', 'avatar_url', 'css_classes' );
     278 * @return string
     279 */
     280function get_css_classes( $attendee ) {
     281    // Name
     282    $classes = array( 'attendee-' . $attendee->post_name );
     283
     284    // Ticket
     285    $ticket    = get_post( $attendee->tix_ticket_id );
     286    $classes[] = 'ticket-' . $ticket->post_name;
     287
     288    // Coupon
     289    if ( $attendee->tix_coupon_id ) {
     290        $coupon    = get_post( $attendee->tix_coupon_id );
     291        $classes[] = 'coupon-' . $coupon->post_name;
     292    }
     293
     294    return implode( ' ', $classes );
    260295}
    261296
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/camptix-badge-generator/javascript/html-badges-previewer.js

    r3015 r4315  
    55        removedCSS : false
    66    };
    7    
     7
    88    /**
    99     * Initialize
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/camptix-badge-generator/views/html-badges/template-badges.php

    r3153 r4315  
    3030        else :
    3131
    32             foreach ( $attendees as $attendee_id ) :
    33                 $attendee_data = get_attendee_data( $attendee_id );
    34                
     32            foreach ( $attendees as $attendee ) :
    3533                ?>
    3634
    37                 <article class="attendee <?php echo esc_attr( $attendee_data['css_classes'] ); ?>">
     35                <article class="attendee <?php echo esc_attr( $attendee->css_classes ); ?>">
    3836                    <section class="badge badge-back">
    3937                        <?php require( __DIR__ . '/template-part-badge-contents.php' ); ?>
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/camptix-badge-generator/views/html-badges/template-part-badge-contents.php

    r3015 r4315  
    33namespace CampTix\Badge_Generator\HTML;
    44defined( 'WPINC' ) or die();
     5
     6/**
     7 * @var \WP_Post $attendee
     8 * @var array    $allowed_html
     9 */
    510
    611?>
     
    2126    <img
    2227        class="attendee-avatar"
    23         src="<?php echo esc_url( $attendee_data['avatar_url'] ); ?>"
    24         alt="<?php echo esc_attr( strip_tags( $attendee_data['formatted_name'] ) ); ?>"
     28        src="<?php echo esc_url( $attendee->avatar_url ); ?>"
     29        alt="<?php echo esc_attr( strip_tags( $attendee->formatted_name ) ); ?>"
    2530    />
    2631
    2732    <figcaption>
    2833        <h1 class="attendee-name">
    29             <?php echo wp_kses( $attendee_data['formatted_name'], $allowed_html ); ?>
     34            <?php echo wp_kses( $attendee->formatted_name, $allowed_html ); ?>
    3035        </h1>
    3136    </figcaption>
Note: See TracChangeset for help on using the changeset viewer.