<!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>[2351] 2013/rmccue/trunk: Add better taxonomy support</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">
<dt>Revision</dt> <dd><a href="http://gsoc.trac.wordpress.org/changeset/2351">2351</a></dd>
<dt>Author</dt> <dd>rmccue</dd>
<dt>Date</dt> <dd>2013-09-21 12:32:47 +0000 (Sat, 21 Sep 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add better taxonomy support

Fixes <a href="http://gsoc.trac.wordpress.org/ticket/275">#275</a> for now.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#2013rmccuetrunklibclasswpjsonpostsphp">2013/rmccue/trunk/lib/class-wp-json-posts.php</a></li>
<li><a href="#2013rmccuetrunklibclasswpjsonserverphp">2013/rmccue/trunk/lib/class-wp-json-server.php</a></li>
<li><a href="#2013rmccuetrunkpluginphp">2013/rmccue/trunk/plugin.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#2013rmccuetrunklibclasswpjsontaxonomiesphp">2013/rmccue/trunk/lib/class-wp-json-taxonomies.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="2013rmccuetrunklibclasswpjsonpostsphp"></a>
<div class="modfile"><h4>Modified: 2013/rmccue/trunk/lib/class-wp-json-posts.php (2350 => 2351)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/rmccue/trunk/lib/class-wp-json-posts.php    2013-09-21 12:32:40 UTC (rev 2350)
+++ 2013/rmccue/trunk/lib/class-wp-json-posts.php       2013-09-21 12:32:47 UTC (rev 2351)
</span><span class="lines">@@ -564,14 +564,6 @@
</span><span class="cx">          elseif ( 'edit' === $context )
</span><span class="cx">                  return new WP_Error( 'json_cannot_edit', __( 'Sorry, you cannot edit this post' ), array( 'status' => 403 ) );
</span><span class="cx"> 
</span><del>-               // Taxonomies
-               $post_type_taxonomies = get_object_taxonomies( $post['post_type'] );
-               $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
-               $_post['terms'] = array();
-               foreach ( $terms as $term ) {
-                       $_post['terms'][ $term->taxonomy ] = $this->prepare_term( $term );
-               }
-
</del><span class="cx">           // Post meta
</span><span class="cx">          $_post['post_meta'] = $this->prepare_meta( $post['ID'] );
</span><span class="cx"> 
</span><span class="lines">@@ -606,43 +598,6 @@
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><del>-        * Prepares term data for return in an XML-RPC object.
-        *
-        * @access protected
-        *
-        * @param array|object $term The unprepared term data
-        * @return array The prepared term data
-        */
-       protected function prepare_term( $term ) {
-               $_term = $term;
-               if ( ! is_array( $_term ) )
-                       $_term = get_object_vars( $_term );
-
-               $_term['id'] = $term->term_id;
-               $_term['group'] = $term->term_group;
-               $_term['parent'] = $_term['parent'];
-               $_term['count'] = $_term['count'];
-               #unset($_term['term_id'], )
-
-               $data = array(
-                       'ID'     => (int) $term->term_id,
-                       'name'   => $term->name,
-                       'slug'   => $term->slug,
-                       'group'  => (int) $term->term_group,
-                       'parent' => (int) $term->parent,
-                       'count'  => (int) $term->count,
-                       'meta'   => array(
-                               'links' => array(
-                                       'collection' => json_url( '/taxonomy/' . $term->taxonomy ),
-                                       'self' => json_url( '/taxonomy/' . $term->taxonomy . '/terms/' . $term->term_id ),
-                               ),
-                       ),
-               );
-
-               return apply_filters( 'json_prepare_term', $data, $term );
-       }
-
-       /**
</del><span class="cx">    * Retrieve custom fields for post.
</span><span class="cx">   *
</span><span class="cx">   * @since 2.5.0
</span><span class="lines">@@ -906,9 +861,6 @@
</span><span class="cx">                          unstick_post( $data['ID'] );
</span><span class="cx">          }
</span><span class="cx"> 
</span><del>-               // Terms
-               // TODO: implement this
-
</del><span class="cx">           do_action( 'json_insert_post', $post, $data, $update );
</span><span class="cx"> 
</span><span class="cx">          return $post_ID;
</span></span></pre></div>
<a id="2013rmccuetrunklibclasswpjsonserverphp"></a>
<div class="modfile"><h4>Modified: 2013/rmccue/trunk/lib/class-wp-json-server.php (2350 => 2351)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/rmccue/trunk/lib/class-wp-json-server.php   2013-09-21 12:32:40 UTC (rev 2350)
+++ 2013/rmccue/trunk/lib/class-wp-json-server.php      2013-09-21 12:32:47 UTC (rev 2351)
</span><span class="lines">@@ -217,23 +217,6 @@
</span><span class="cx">                  // Meta endpoints
</span><span class="cx">                  '/' => array( array( $this, 'getIndex' ), self::READABLE ),
</span><span class="cx"> 
</span><del>-                       // Taxonomies
-                       '/taxonomies'                                       => array( '__return_null', self::READABLE ),
-                       '/taxonomies/(?P<taxonomy>\w+)'                     => array(
-                               array( '__return_null', self::READABLE ),
-                               array( '__return_null', self::EDITABLE | self::ACCEPT_JSON ),
-                               array( '__return_null', self::DELETABLE ),
-                       ),
-                       '/taxonomies/(?P<taxonomy>\w+)/terms'               => array(
-                               array( '__return_null', self::READABLE ),
-                               array( '__return_null', self::CREATABLE | self::ACCEPT_JSON ),
-                       ),
-                       '/taxonomies/(?P<taxonomy>\w+)/terms/(?P<term>\w+)' => array(
-                               array( '__return_null', self::READABLE ),
-                               array( '__return_null', self::EDITABLE | self::ACCEPT_JSON ),
-                               array( '__return_null', self::DELETABLE ),
-                       ),
-
</del><span class="cx">                   // Users
</span><span class="cx">                  '/users'               => array(
</span><span class="cx">                          array( '__return_null', self::READABLE ),
</span></span></pre></div>
<a id="2013rmccuetrunklibclasswpjsontaxonomiesphp"></a>
<div class="addfile"><h4>Added: 2013/rmccue/trunk/lib/class-wp-json-taxonomies.php (0 => 2351)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/rmccue/trunk/lib/class-wp-json-taxonomies.php                               (rev 0)
+++ 2013/rmccue/trunk/lib/class-wp-json-taxonomies.php  2013-09-21 12:32:47 UTC (rev 2351)
</span><span class="lines">@@ -0,0 +1,211 @@
</span><ins>+<?php
+
+class WP_JSON_Taxonomies {
+       /**
+        * Register the taxonomy-related routes
+        *
+        * @param array $routes Existing routes
+        * @return array Modified routes
+        */
+       public function registerRoutes( $routes ) {
+               $tax_routes = array(
+                       '/posts/types/(?P<type>\w+)/taxonomies' => array(
+                               array( array( $this, 'getTaxonomies' ), WP_JSON_Server::READABLE ),
+                       ),
+                       '/posts/types/(?P<type>\w+)/taxonomies/(?P<taxonomy>\w+)' => array(
+                               array( array( $this, 'getTaxonomy' ), WP_JSON_Server::READABLE ),
+                       ),
+                       '/posts/types/(?P<type>\w+)/taxonomies/(?P<taxonomy>\w+)/terms' => array(
+                               array( array( $this, 'getTerms' ), WP_JSON_Server::READABLE ),
+                               array( '__return_null', WP_JSON_Server::CREATABLE | WP_JSON_Server::ACCEPT_JSON ),
+                       ),
+                       '/posts/types/(?P<type>\w+)/taxonomies/(?P<taxonomy>\w+)/terms/(?P<term>\w+)' => array(
+                               array( array( $this, 'getTerm' ), WP_JSON_Server::READABLE ),
+                               array( '__return_null', WP_JSON_Server::EDITABLE | WP_JSON_Server::ACCEPT_JSON ),
+                               array( '__return_null', WP_JSON_Server::DELETABLE ),
+                       ),
+               );
+               return array_merge( $routes, $tax_routes );
+       }
+
+       /**
+        * Get taxonomies
+        * 
+        * @param string $type Post type to get taxonomies for
+        * @return array Taxonomy data
+        */
+       public function getTaxonomies( $type ) {
+               $taxonomies = get_object_taxonomies( $type, 'objects' );
+
+               $data = array();
+               foreach ($taxonomies as $tax_type => $value) {
+                       $tax = $this->prepare_taxonomy( $value, $type, true );
+                       if ( is_wp_error( $tax ) )
+                               continue;
+
+                       $data[ $tax_type ] = $tax;
+               }
+
+               return $data;
+       }
+
+       /**
+        * Get taxonomies
+        * 
+        * @param string $type Post type to get taxonomies for
+        * @return array Taxonomy data
+        */
+       public function getTaxonomy( $type, $taxonomy ) {
+               $tax = get_taxonomy( $taxonomy );
+               if ( empty( $tax ) )
+                       return new WP_Error( 'json_taxonomy_invalid_id', __( 'Invalid taxonomy ID.' ), array( 'status' => 404 ) );
+
+               return $this->prepare_taxonomy( $tax, $type );
+       }
+
+       /**
+        * Prepare a taxonomy for serialization
+        *
+        * @param stdClass $taxonomy Taxonomy data
+        * @param boolean $_in_collection Are we in a collection?
+        * @return array Taxonomy data
+        */
+       protected function prepare_taxonomy( $taxonomy, $type, $_in_collection = false ) {
+               if ( $taxonomy->public === false )
+                       return new WP_Error( 'json_cannot_read_taxonomy', __( 'Cannot view taxonomy' ), array( 'status' => 403 ) );
+
+               $base_url = '/posts/types/' . $type . '/taxonomies/' . $taxonomy->name;
+
+               $data = array(
+                       'name' => $taxonomy->label,
+                       'slug' => $taxonomy->name,
+                       'labels' => $taxonomy->labels,
+                       'types' => array(),
+                       'show_cloud' => $taxonomy->show_tagcloud,
+                       'hierarchical' => $taxonomy->hierarchical,
+                       'meta' => array(
+                               'links' => array(
+                                       'archives' => json_url( $base_url . '/terms' )
+                               )
+                       ),
+               );
+
+               if ( $_in_collection ) {
+                       $data['meta']['links']['self'] = json_url( $base_url );
+               }
+               else {
+                       $data['meta']['links']['collection'] = json_url( $base_url );
+               }
+
+               return apply_filters( 'json_prepare_taxonomy', $data );
+       }
+
+       /**
+        * Add taxonomy data to post type data
+        *
+        * @param array $data Type data
+        * @param array $post Internal type data
+        * @return array Filtered data
+        */
+       public function add_taxonomy_data( $data, $type ) {
+               $data['taxonomies'] = $this->getTaxonomies( $type->name );
+
+               return $data;
+       }
+
+       /**
+        * Get terms for a post type
+        *
+        * @param string $type Post type
+        * @param string $taxonomy Taxonomy slug
+        * @return array Term collection
+        */
+       public function getTerms( $type, $taxonomy ) {
+               if ( ! taxonomy_exists( $taxonomy ) )
+                       return new WP_Error( 'json_taxonomy_invalid_id', __( 'Invalid taxonomy ID.' ), array( 'status' => 404 ) );
+
+               $args = array(
+                       'hide_empty' => false,
+               );
+               $terms = get_terms( $taxonomy, $args );
+               if ( is_wp_error( $terms ) )
+                       return $terms;
+
+               $data = array();
+               foreach ($terms as $term) {
+                       $data[ $term->slug ] = $this->prepare_term( $term, $type );
+               }
+               return $data;
+       }
+
+       /**
+        * Get term for a post type
+        *
+        * @param string $type Post type
+        * @param string $taxonomy Taxonomy slug
+        * @param string $term Term slug
+        * @param string $context Context (view/view-parent)
+        * @return array Term entity
+        */
+       public function getTerm( $type, $taxonomy, $term, $context = 'view' ) {
+               if ( ! taxonomy_exists( $taxonomy ) )
+                       return new WP_Error( 'json_taxonomy_invalid_id', __( 'Invalid taxonomy ID.' ), array( 'status' => 404 ) );
+
+               $data = get_term( $term, $taxonomy );
+               if ( empty( $data ) )
+                       return new WP_Error( 'json_taxonomy_invalid_term', __( 'Invalid term ID.' ), array( 'status' => 404 ) );
+
+               return $this->prepare_term( $data, $type, $context );
+       }
+
+       /**
+        * Add term data to post data
+        *
+        * @param array $data Post data
+        * @param array $post Internal post data
+        * @param string $context Post context
+        * @return array Filtered data
+        */
+       public function add_term_data( $data, $post, $context ) {
+               $post_type_taxonomies = get_object_taxonomies( $post['post_type'] );
+               $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
+               $data['terms'] = array();
+               foreach ( $terms as $term ) {
+                       $data['terms'][ $term->taxonomy ] = $this->prepare_term( $term, $post['post_type'] );
+               }
+
+               return $data;
+       }
+
+       /**
+        * Prepare term data for serialization
+        *
+        * @param array|object $term The unprepared term data
+        * @return array The prepared term data
+        */
+       protected function prepare_term( $term, $type, $context = 'view' ) {
+               $base_url = '/posts/types/' . $type . '/taxonomies/' . $term->taxonomy . '/terms';
+               $data = array(
+                       'ID'     => (int) $term->term_taxonomy_id,
+                       'name'   => $term->name,
+                       'slug'   => $term->slug,
+                       'parent' => (int) $term->parent,
+                       'count'  => (int) $term->count,
+                       'meta'   => array(
+                               'links' => array(
+                                       'collection' => json_url( $base_url ),
+                                       'self' => json_url( $base_url . '/' . $term->term_id ),
+                               ),
+                       ),
+               );
+
+               if ( ! empty( $data['parent'] ) && $context === 'view' ) {
+                       $data['parent'] = $this->getTerm( $type, $term->taxonomy, $data['parent'], 'view-parent' );
+               }
+               elseif ( empty( $data['parent'] ) ) {
+                       $data['parent'] = null;
+               }
+
+               return apply_filters( 'json_prepare_term', $data, $term );
+       }
+}
</ins><span class="cx">\ No newline at end of file
</span><span class="cx">Property changes on: 2013/rmccue/trunk/lib/class-wp-json-taxonomies.php
</span><span class="cx">___________________________________________________________________
</span></span></pre></div>
<a id="svnexecutable"></a>
<div class="addfile"><h4>Added: svn:executable</h4></div>
<ins>+*
</ins><span class="cx">\ No newline at end of property
</span><a id="2013rmccuetrunkpluginphp"></a>
<div class="modfile"><h4>Modified: 2013/rmccue/trunk/plugin.php (2350 => 2351)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/rmccue/trunk/plugin.php     2013-09-21 12:32:40 UTC (rev 2350)
+++ 2013/rmccue/trunk/plugin.php        2013-09-21 12:32:47 UTC (rev 2351)
</span><span class="lines">@@ -11,6 +11,7 @@
</span><span class="cx"> include_once( dirname( __FILE__ ) . '/lib/class-wp-json-customposttype.php' );
</span><span class="cx"> include_once( dirname( __FILE__ ) . '/lib/class-wp-json-pages.php' );
</span><span class="cx"> include_once( dirname( __FILE__ ) . '/lib/class-wp-json-media.php' );
</span><ins>+include_once( dirname( __FILE__ ) . '/lib/class-wp-json-taxonomies.php' );
</ins><span class="cx"> 
</span><span class="cx"> /**
</span><span class="cx">  * Register our rewrite rules for the API
</span><span class="lines">@@ -30,7 +31,7 @@
</span><span class="cx">  * @internal This will live in default-filters.php
</span><span class="cx">  */
</span><span class="cx"> function json_api_default_filters() {
</span><del>-       global $wp_json_posts, $wp_json_pages, $wp_json_media;
</del><ins>+        global $wp_json_posts, $wp_json_pages, $wp_json_media, $wp_json_taxonomies;
</ins><span class="cx"> 
</span><span class="cx">  // Posts
</span><span class="cx">  $wp_json_posts = new WP_JSON_Posts();
</span><span class="lines">@@ -48,6 +49,12 @@
</span><span class="cx">  add_filter( 'json_pre_insert_post', array( $wp_json_media, 'preinsertCheck' ),   10, 3 );
</span><span class="cx">  add_filter( 'json_insert_post',     array( $wp_json_media, 'attachThumbnail' ),  10, 3 );
</span><span class="cx">  add_filter( 'json_post_type_data',  array( $wp_json_media, 'type_archive_link' ), 10, 2 );
</span><ins>+
+       // Posts
+       $wp_json_taxonomies = new WP_JSON_Taxonomies();
+       add_filter( 'json_endpoints',      array( $wp_json_taxonomies, 'registerRoutes' ), 2 );
+       add_filter( 'json_post_type_data', array( $wp_json_taxonomies, 'add_taxonomy_data' ), 10, 2 );
+       add_filter( 'json_prepare_post',   array( $wp_json_taxonomies, 'add_term_data' ), 10, 3 );
</ins><span class="cx"> }
</span><span class="cx"> add_action( 'plugins_loaded', 'json_api_default_filters' );
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>