WPCOM_JSON_API_List_Post_Type_Taxonomies_Endpoint

The WordPress Core WPCOM JSON API List Post Type Taxonomies Endpoint class.

Defined (1)

The class is defined in the following location(s).

/json-endpoints/class.wpcom-json-api-list-post-type-taxonomies-endpoint.php  
  1. class WPCOM_JSON_API_List_Post_Type_Taxonomies_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2. static $taxonomy_keys_to_include = array( 
  3. 'name' => 'name',  
  4. 'label' => 'label',  
  5. 'labels' => 'labels',  
  6. 'description' => 'description',  
  7. 'hierarchical' => 'hierarchical',  
  8. 'public' => 'public',  
  9. 'cap' => 'capabilities',  
  10. ); 
  11.  
  12. // /sites/%s/post-types/%s/taxonomies -> $blog_id, $post_type 
  13. function callback( $path = '', $blog_id = 0, $post_type = 'post' ) { 
  14. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); 
  15. if ( is_wp_error( $blog_id ) ) { 
  16. return $blog_id; 
  17.  
  18. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { 
  19. $this->load_theme_functions(); 
  20.  
  21. /** This filter is documented in jetpack/json-endpoints/class.wpcom-json-api-list-post-types-endpoint.php */ 
  22. if ( apply_filters( 'rest_api_localize_response', false ) ) { 
  23. // API localization occurs after the initial taxonomies have been 
  24. // registered, so re-register if localizing response 
  25. create_initial_taxonomies(); 
  26.  
  27. $args = $this->query_args(); 
  28.  
  29. $post_type_object = get_post_type_object( $post_type ); 
  30. if ( ! $post_type_object || ( ! $post_type_object->publicly_queryable && ( 
  31. ! current_user_can( $post_type_object->cap->edit_posts ) ) ) ) { 
  32. return new WP_Error( 'unknown_post_type', 'Unknown post type', 404 ); 
  33.  
  34. // Get a list of available taxonomies 
  35. $taxonomy_objects = get_object_taxonomies( $post_type, 'objects' ); 
  36.  
  37. // Construct array of formatted objects 
  38. $formatted_taxonomy_objects = array(); 
  39. foreach ( $taxonomy_objects as $taxonomy_object ) { 
  40. // Omit private taxonomies unless user has assign capability 
  41. if ( ! $taxonomy_object->public && ! current_user_can( $taxonomy_object->cap->assign_terms ) ) { 
  42. continue; 
  43.  
  44. // Include only the desired keys in the response 
  45. $formatted_taxonomy_object = array(); 
  46. foreach ( self::$taxonomy_keys_to_include as $key => $value ) { 
  47. $formatted_taxonomy_object[ $value ] = $taxonomy_object->{ $key }; 
  48.  
  49. $formatted_taxonomy_objects[] = $formatted_taxonomy_object; 
  50.  
  51. return array( 
  52. 'found' => count( $formatted_taxonomy_objects ),  
  53. 'taxonomies' => $formatted_taxonomy_objects,  
  54. );