<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[5533] sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types: WordCamp Post Types: Consolidate v2 REST functionality, parity w/ v1</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/5533">5533</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/5533","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>coreymckrill</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-06-01 20:46:01 +0000 (Thu, 01 Jun 2017)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>WordCamp Post Types: Consolidate v2 REST functionality, parity w/ v1

This moves all the actions/filters related to v2 REST API endpoints
to a separate file and adds the (hopefully) last bit of response data
that was included in v1, but was missing in v2 (speaker avatars).

For the speaker avatar data, I decided to emulate how Core presents
the avatar data for post authors in API responses. So there is a top-
level `avatar_urls` field that is an object containing multiple
Gravatar URLs for different image sizes.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordcamporgpublic_htmlwpcontentpluginswcposttypeswcposttypesphp">sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types/wc-post-types.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkwordcamporgpublic_htmlwpcontentpluginswcposttypesincrestapiphp">sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types/inc/rest-api.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordcamporgpublic_htmlwpcontentpluginswcposttypesincrestapiphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types/inc/rest-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types/inc/rest-api.php                            (rev 0)
+++ sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types/inc/rest-api.php      2017-06-01 20:46:01 UTC (rev 5533)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,179 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Functions related to the v2 REST API for WordCamp Post Types
+ *
+ * @package WordCamp\Post_Types\REST_API
+ */
+
+namespace WordCamp\Post_Types\REST_API;
+defined( 'WPINC' ) || die();
+
+/**
+ * Add non-sensitive meta fields to the speaker/session REST API endpoints
+ *
+ * If we ever want to register meta for purposes other than exposing it in the API, then this function will
+ * probably need to be re-thought and re-factored.
+ *
+ * @uses wcorg_register_meta_only_on_endpoint()
+ *
+ * @return void
+ */
+function expose_public_post_meta() {
+       $public_session_fields = array(
+               '_wcpt_session_time' => array(
+                       'type'   => 'integer',
+                       'single' => true,
+               ),
+
+               '_wcpt_session_type' => array(
+                       'single' => true,
+               ),
+
+               '_wcpt_session_slides' => array(
+                       'single' => true,
+               ),
+
+               '_wcpt_session_video' => array(
+                       'single' => true,
+               ),
+       );
+
+       wcorg_register_meta_only_on_endpoint( 'post', $public_session_fields, '/wp-json/wp/v2/sessions/' );
+
+       $public_sponsor_fields = array(
+               '_wcpt_sponsor_website' => array(
+                       'single' => true,
+               ),
+       );
+
+       wcorg_register_meta_only_on_endpoint( 'post', $public_sponsor_fields, '/wp-json/wp/v2/sponsors/' );
+}
+
+add_action( 'init', __NAMESPACE__ . '\expose_public_post_meta' );
+
+/**
+ * Additional fields to include in API responses for WordCamp post types.
+ *
+ * Note that this is for special cases. To expose specific post meta values, see `expose_public_post_meta()`.
+ *
+ * @return void
+ */
+function register_additional_rest_fields() {
+       /**
+        * Speaker avatars.
+        *
+        * We can't expose a Speaker's e-mail address in the API response, but can we go ahead
+        * and derive their Gravatar URL and expose that instead.
+        */
+       if ( get_option( 'show_avatars' ) ) {
+               $avatar_properties = array();
+               $avatar_sizes = rest_get_avatar_sizes();
+
+               foreach ( $avatar_sizes as $size ) {
+                       $avatar_properties[ $size ] = array(
+                               /* translators: %d: avatar image size in pixels */
+                               'description' => sprintf( __( 'Avatar URL with image size of %d pixels.' ), $size ),
+                               'type'        => 'string',
+                               'format'      => 'uri',
+                               'context'     => array( 'embed', 'view', 'edit' ),
+                       );
+               }
+
+               register_rest_field(
+                       'wcb_speaker',
+                       'avatar_urls',
+                       array(
+                               'get_callback' => function ( $object ) {
+                                       $object      = (object) $object;
+                                       $avatar_urls = array_fill_keys( rest_get_avatar_sizes(), '' );
+
+                                       if ( $speaker_email = get_post_meta( $object->id, '_wcb_speaker_email', true ) ) {
+                                               $avatar_urls = rest_get_avatar_urls( $speaker_email );
+                                       } elseif ( $speaker_user_id = get_post_meta( $object->id, '_wcpt_user_id', true ) ) {
+                                               $speaker = get_user_by( 'id', $speaker_user_id );
+
+                                               if ( $speaker ) {
+                                                       $avatar_urls = rest_get_avatar_urls( $speaker->user_email );
+                                               }
+                                       }
+
+                                       return $avatar_urls;
+                               },
+                               'schema'       => array(
+                                       'description' => __( 'Avatar URLs for the speaker.' ),
+                                       'type'        => 'object',
+                                       'context'     => array( 'embed', 'view', 'edit' ),
+                                       'readonly'    => true,
+                                       'properties'  => $avatar_properties,
+                               ),
+                       )
+               );
+       } // End if().
+}
+
+add_action( 'rest_api_init', __NAMESPACE__ . '\register_additional_rest_fields' );
+
+/**
+ * Link all sessions to the speaker in the `speakers` API endpoint
+ *
+ * This allows clients to request a speaker and get all their sessions embedded in the response, avoiding
+ * extra HTTP requests
+ *
+ * @param \WP_REST_Response $response
+ * @param \WP_Post          $post
+ *
+ * @return \WP_REST_Response
+ */
+function link_speaker_to_sessions( $response, $post ) {
+       $sessions = get_posts( array(
+               'post_type'      => 'wcb_session',
+               'posts_per_page' => 100,
+               'fields'         => 'ids',
+
+               'meta_query' => array(
+                       array(
+                               'key'   => '_wcpt_speaker_id',
+                               'value' => $post->ID,
+                       ),
+               ),
+       ) );
+
+       foreach ( $sessions as $session_id ) {
+               $response->add_link(
+                       'sessions',
+                       get_rest_url( null, "/wp/v2/sessions/$session_id" ),
+                       array( 'embeddable' => true )
+               );
+       }
+
+       return $response;
+}
+
+add_filter( 'rest_prepare_wcb_speaker', __NAMESPACE__ . '\link_speaker_to_sessions', 10, 2 );
+
+/**
+ * Link all speakers to the session in the `sessions` API endpoint
+ *
+ * This allows clients to request a session and get all its speakers embedded in the response, avoiding extra
+ * HTTP requests
+ *
+ * @param \WP_REST_Response $response
+ * @param \WP_Post          $post
+ *
+ * @return \WP_REST_Response
+ */
+function link_session_to_speakers( $response, $post ) {
+       $speaker_ids = get_post_meta( $post->ID, '_wcpt_speaker_id', false );
+
+       foreach ( $speaker_ids as $speaker_id ) {
+               $response->add_link(
+                       'speakers',
+                       get_rest_url( null, "/wp/v2/speakers/$speaker_id" ),
+                       array( 'embeddable' => true )
+               );
+       }
+
+       return $response;
+}
+
+add_filter( 'rest_prepare_wcb_session', __NAMESPACE__ . '\link_session_to_speakers', 10, 2 );
</ins></span></pre></div>
<a id="sitestrunkwordcamporgpublic_htmlwpcontentpluginswcposttypeswcposttypesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types/wc-post-types.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types/wc-post-types.php   2017-06-01 17:03:50 UTC (rev 5532)
+++ sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types/wc-post-types.php     2017-06-01 20:46:01 UTC (rev 5533)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -56,14 +56,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'the_content', array( $this, 'add_video_info_to_session_posts' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'the_content', array( $this, 'add_session_info_to_speaker_posts' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // REST API
-               add_action( 'init',                     array( $this, 'expose_public_post_meta'  )        );
-               add_filter( 'rest_prepare_wcb_speaker', array( $this, 'link_speaker_to_sessions' ), 10, 2 );
-               add_filter( 'rest_prepare_wcb_session', array( $this, 'link_session_to_speakers' ), 10, 2 );
-
</del><span class="cx" style="display: block; padding: 0 10px">                 add_filter( 'dashboard_glance_items', array( $this, 'glance_items' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'option_default_comment_status', array( $this, 'default_comment_ping_status' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'option_default_ping_status', array( $this, 'default_comment_ping_status' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               add_action( 'init', array( $this, 'rest_init' ), 9 );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        function init() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -79,6 +76,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Runs during init, because rest_api_init is too late.
+        */
+       function rest_init() {
+               require_once( 'inc/rest-api.php' );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Runs during admin_menu
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        function admin_menu() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1400,104 +1404,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Add non-sensitive meta fields to the speaker/session REST API endpoints
-        *
-        * if we ever want to register meta for purposes other than exposing it in the API, then this function will
-        * probably need to be re-thought and re-factored.
-        */
-       public function expose_public_post_meta() {
-               $public_session_fields = array(
-                       '_wcpt_session_time' => array(
-                               'type'   => 'integer',
-                               'single' => true,
-                       ),
-
-                       '_wcpt_session_type' => array(
-                               'single' => true,
-                       ),
-
-                       '_wcpt_session_slides' => array(
-                               'single' => true,
-                       ),
-
-                       '_wcpt_session_video' => array(
-                               'single' => true,
-                       ),
-               );
-
-               wcorg_register_meta_only_on_endpoint( 'post', $public_session_fields, '/wp-json/wp/v2/sessions/' );
-
-               $public_sponsor_fields = array(
-                       '_wcpt_sponsor_website' => array(
-                               'single' => true,
-                       )
-               );
-
-               wcorg_register_meta_only_on_endpoint( 'post', $public_sponsor_fields, '/wp-json/wp/v2/sponsors/' );
-       }
-
-       /**
-        * Link all sessions to the speaker in the `speakers` API endpoint
-        *
-        * This allows clients to request a speaker and get all their sessions embedded in the response, avoiding
-        * extra HTTP requests
-        *
-        * @param WP_REST_Response $response
-        * @param WP_Post          $post
-        *
-        * @return WP_REST_Response
-        */
-       function link_speaker_to_sessions( $response, $post ) {
-               $sessions = get_posts( array(
-                       'post_type'      => 'wcb_session',
-                       'posts_per_page' => 100,
-                       'fields'         => 'ids',
-
-                       'meta_query' => array(
-                               array(
-                                       'key'   => '_wcpt_speaker_id',
-                                       'value' => $post->ID,
-                               ),
-                       ),
-               ) );
-
-               foreach( $sessions as $session_id ) {
-                       $response->add_link(
-                               'sessions',
-                               get_rest_url( null, "/wp/v2/sessions/$session_id" ),
-                               array( 'embeddable' => true )
-                       );
-               }
-
-               return $response;
-       }
-
-       /**
-        * Link all speakers to the session in the `sessions` API endpoint
-        *
-        * This allows clients to request a session and get all its speakers embedded in the response, avoiding extra
-        * HTTP requests
-        *
-        * @param WP_REST_Response $response
-        * @param WP_Post          $post
-        *
-        * @return WP_REST_Response
-        */
-       function link_session_to_speakers( $response, $post ) {
-               $speaker_ids = get_post_meta( $post->ID, '_wcpt_speaker_id', false );
-
-               foreach( $speaker_ids as $speaker_id ) {
-                       $response->add_link(
-                               'speakers',
-                               get_rest_url( null, "/wp/v2/speakers/$speaker_id" ),
-                               array( 'embeddable' => true )
-                       );
-               }
-
-               return $response;
-       }
-
-       /**
</del><span class="cx" style="display: block; padding: 0 10px">          * Fired during add_meta_boxes, adds extra meta boxes to our custom post types.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        function add_meta_boxes() {
</span></span></pre>
</div>
</div>

</body>
</html>