Skip to content

Instantly share code, notes, and snippets.

@kadamwhite
Created May 28, 2014 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kadamwhite/e382c82c65f8c39d3527 to your computer and use it in GitHub Desktop.
Save kadamwhite/e382c82c65f8c39d3527 to your computer and use it in GitHub Desktop.
Proposed solution for embedding custom types into taxonomies
diff --git a/lib/class-wp-json-posts.php b/lib/class-wp-json-posts.php
index 941c796..a06ed0f 100644
--- a/lib/class-wp-json-posts.php
+++ b/lib/class-wp-json-posts.php
@@ -519,9 +519,10 @@ class WP_JSON_Posts {
*
* @param string|object $type Type name, or type object (internal use)
* @param boolean $_in_collection Is this in a collection? (internal use)
+ * @param boolean $_in_taxonomy Is this request being added to a taxonomy record? (internal use)
* @return array Post type data
*/
- public function get_post_type( $type, $_in_collection = false ) {
+ public function get_post_type( $type, $_in_collection = false, $_in_taxonomy = false ) {
if ( ! is_object( $type ) )
$type = get_post_type_object($type);
@@ -542,7 +543,7 @@ class WP_JSON_Posts {
),
);
- if ( $_in_collection )
+ if ( $_in_collection || $_in_taxonomy )
$data['meta']['links']['self'] = json_url( '/posts/types/' . $type->name );
else
$data['meta']['links']['collection'] = json_url( '/posts/types' );
@@ -554,7 +555,7 @@ class WP_JSON_Posts {
$data['meta']['links']['archives'] = json_url( add_query_arg( 'type', $type->name, '/posts' ) );
}
- return apply_filters( 'json_post_type_data', $data, $type );
+ return apply_filters( 'json_post_type_data', $data, $type, $_in_taxonomy );
}
/**
@@ -1090,6 +1091,22 @@ class WP_JSON_Posts {
}
/**
+ * Embed post type data into taxonomy data
+ *
+ * @uses self::get_post_type()
+ * @param array $data Taxonomy data
+ * @param array $taxonomy Internal taxonomy data
+ * @return array Filtered data
+ */
+ public function add_post_type_data( $data, $taxonomy ) {
+ foreach( $taxonomy->object_type as $type ) {
+ $data['types'][ $type ] = $this->get_post_type( $type, false, true );
+ }
+
+ return $data;
+ }
+
+ /**
* Helper method for {@see new_post} and {@see edit_post}, containing shared logic.
*
* @since 3.4.0
diff --git a/lib/class-wp-json-taxonomies.php b/lib/class-wp-json-taxonomies.php
index 328e5d5..049d6ba 100644
--- a/lib/class-wp-json-taxonomies.php
+++ b/lib/class-wp-json-taxonomies.php
@@ -110,7 +110,7 @@ class WP_JSON_Taxonomies {
$data['meta']['links']['collection'] = json_url( $base_url );
}
- return apply_filters( 'json_prepare_taxonomy', $data );
+ return apply_filters( 'json_prepare_taxonomy', $data, $taxonomy );
}
/**
@@ -120,8 +120,10 @@ class WP_JSON_Taxonomies {
* @param array $post Internal type data
* @return array Filtered data
*/
- public function add_taxonomy_data( $data, $type ) {
- $data['taxonomies'] = $this->get_taxonomies( $type->name );
+ public function add_taxonomy_data( $data, $type, $_in_taxonomy ) {
+ if ( ! $_in_taxonomy ) {
+ $data['taxonomies'] = $this->get_taxonomies( $type->name );
+ }
return $data;
}
diff --git a/plugin.php b/plugin.php
index 3571b04..82067b3 100644
--- a/plugin.php
+++ b/plugin.php
@@ -75,6 +75,7 @@ function json_api_default_filters($server) {
// Posts
$wp_json_posts = new WP_JSON_Posts($server);
add_filter( 'json_endpoints', array( $wp_json_posts, 'register_routes' ), 0 );
+ add_filter( 'json_prepare_taxonomy', array( $wp_json_posts, 'add_post_type_data' ), 10, 2 );
// Users
$wp_json_users = new WP_JSON_Users($server);
@@ -97,7 +98,7 @@ function json_api_default_filters($server) {
// Posts
$wp_json_taxonomies = new WP_JSON_Taxonomies($server);
add_filter( 'json_endpoints', array( $wp_json_taxonomies, 'register_routes' ), 2 );
- add_filter( 'json_post_type_data', array( $wp_json_taxonomies, 'add_taxonomy_data' ), 10, 2 );
+ add_filter( 'json_post_type_data', array( $wp_json_taxonomies, 'add_taxonomy_data' ), 10, 3 );
add_filter( 'json_prepare_post', array( $wp_json_taxonomies, 'add_term_data' ), 10, 3 );
}
add_action( 'wp_json_server_before_serve', 'json_api_default_filters', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment