Making WordPress.org


Ignore:
Timestamp:
04/30/2015 05:31:30 PM (9 years ago)
Author:
iandunn
Message:

WordCamp JSON API: Expose Speaker's avatar URL.

Fixes #1007

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/wcorg-json-api.php

    r1517 r1525  
    6262/**
    6363 * Expose a whitelisted set of meta data to unauthenticated JSON API requests
     64 *
     65 * Note: Some additional fields are added in `wcorg_json_expose_additional_post_data()`
    6466 *
    6567 * @param array  $prepared_post
     
    110112    return $prepared_post;
    111113}
    112 add_filter( 'json_prepare_post', 'wcorg_json_expose_whitelisted_meta_data', 999, 3 );
     114add_filter( 'json_prepare_post', 'wcorg_json_expose_whitelisted_meta_data', 998, 3 );
     115
     116/**
     117 * Expose additional data on post responses.
     118 *
     119 * Some fields can't be exposed directly for privacy or other reasons, but we can still provide interesting data
     120 * that is derived from those fields. For example, we can't expose a Speaker's e-mail address, but can we go ahead
     121 * and derive their Gravatar URL and expose that instead.
     122 *
     123 * In other cases, some data wouldn't be particularly useful or meaningful on its own, like the `_wcpt_speaker_id`
     124 * attached to a `wcb_session` post. Instead of providing that raw to the API, we can instead expand it into a
     125 * a full `wcb_speaker` object, so that clients don't have to make additional requests to fetch the data they
     126 * actually want.
     127 *
     128 * @param array  $prepared_post
     129 * @param array  $raw_post
     130 * @param string $context
     131 *
     132 * @return array
     133 */
     134function wcorg_json_expose_additional_post_data( $prepared_post, $raw_post, $context ) {
     135    if ( is_wp_error( $prepared_post ) || empty ( $prepared_post['type'] ) ) {
     136        return $prepared_post;
     137    }
     138
     139    switch( $prepared_post['type'] ) {
     140        case 'wcb_speaker':
     141            $prepared_post['avatar'] = wcorg_json_get_speaker_avatar( $prepared_post['ID'] );
     142            break;
     143    }
     144
     145    return $prepared_post;
     146}
     147add_filter( 'json_prepare_post', 'wcorg_json_expose_additional_post_data', 999, 3 );   // after `wcorg_json_expose_whitelisted_meta_data()`, because anything added before that method gets wiped out
     148
     149/**
     150 * Get the avatar URL for the given speaker
     151 *
     152 * @param int $speaker_post_id
     153 *
     154 * @return string
     155 */
     156function wcorg_json_get_speaker_avatar( $speaker_post_id ) {
     157    $avatar = '';
     158
     159    if ( $speaker_email = get_post_meta( $speaker_post_id, '_wcb_speaker_email', true ) ) {
     160        $avatar = json_get_avatar_url( $speaker_email );
     161    } elseif ( $speaker_user_id = get_post_meta( $speaker_post_id, '_wcpt_user_id', true ) ) {
     162        $avatar = json_get_avatar_url( $speaker_user_id );
     163    }
     164
     165    return $avatar;
     166}
    113167
    114168/**
     
    137191    remove_filter( 'json_prepare_post', array( $wp_json_media, 'add_thumbnail_data' ), 10, 3 );
    138192}
    139 add_action( 'wp_json_server_before_serve', 'wcorg_json_avoid_nested_callback_conflicts', 11 );    // after `json_api_default_filters()`
     193add_action( 'wp_json_server_before_serve', 'wcorg_json_avoid_nested_callback_conflicts', 11 );    // after the default endpoints are added in `json_api_default_filters()`
    140194
    141195/*
Note: See TracChangeset for help on using the changeset viewer.