WPCOM_JSON_API_Update_Taxonomy_Endpoint

The Jetpack by WordPress.com WPCOM JSON API Update Taxonomy Endpoint class.

Defined (1)

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

/json-endpoints/class.wpcom-json-api-update-taxonomy-endpoint.php  
  1. class WPCOM_JSON_API_Update_Taxonomy_Endpoint extends WPCOM_JSON_API_Taxonomy_Endpoint { 
  2. // /sites/%s/tags|categories/new -> $blog_id 
  3. // /sites/%s/tags|categories/slug:%s -> $blog_id, $taxonomy_id 
  4. // /sites/%s/tags|categories/slug:%s/delete -> $blog_id, $taxonomy_id 
  5. function callback( $path = '', $blog_id = 0, $object_id = 0 ) { 
  6. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); 
  7. if ( is_wp_error( $blog_id ) ) { 
  8. return $blog_id; 
  9.  
  10. if ( preg_match( '#/tags/#i', $path ) ) { 
  11. $taxonomy_type = "post_tag"; 
  12. } else { 
  13. $taxonomy_type = "category"; 
  14.  
  15. if ( $this->api->ends_with( $path, '/delete' ) ) { 
  16. return $this->delete_taxonomy( $path, $blog_id, $object_id, $taxonomy_type ); 
  17. } elseif ( $this->api->ends_with( $path, '/new' ) ) { 
  18. return $this->new_taxonomy( $path, $blog_id, $taxonomy_type ); 
  19.  
  20. return $this->update_taxonomy( $path, $blog_id, $object_id, $taxonomy_type ); 
  21.  
  22. // /sites/%s/tags|categories/new -> $blog_id 
  23. function new_taxonomy( $path, $blog_id, $taxonomy_type ) { 
  24. $args = $this->query_args(); 
  25. $input = $this->input(); 
  26. if ( !is_array( $input ) || !$input || !strlen( $input['name'] ) ) { 
  27. return new WP_Error( 'invalid_input', 'Unknown data passed', 400 ); 
  28.  
  29. $user = wp_get_current_user(); 
  30. if ( !$user || is_wp_error( $user ) || !$user->ID ) { 
  31. return new WP_Error( 'authorization_required', 'An active access token must be used to manage taxonomies.', 403 ); 
  32.  
  33. $tax = get_taxonomy( $taxonomy_type ); 
  34. if ( !current_user_can( $tax->cap->edit_terms ) ) { 
  35. return new WP_Error( 'unauthorized', 'User cannot edit taxonomy', 403 ); 
  36.  
  37. if ( 'category' !== $taxonomy_type || ! isset( $input['parent'] ) ) 
  38. $input['parent'] = 0; 
  39.  
  40. if ( $term = get_term_by( 'name', $input['name'], $taxonomy_type ) ) { 
  41. // get_term_by is not case-sensitive, but a name with different casing is allowed 
  42. // also, the exact same name is allowed as long as the parents are different 
  43. if ( $input['name'] === $term->name && $input['parent'] === $term->parent ) { 
  44. return new WP_Error( 'duplicate', 'A taxonomy with that name already exists', 400 ); 
  45.  
  46. $data = wp_insert_term( addslashes( $input['name'] ), $taxonomy_type,  
  47. array( 
  48. 'description' => addslashes( $input['description'] ),  
  49. 'parent' => $input['parent'] 
  50. ); 
  51.  
  52. if ( is_wp_error( $data ) ) 
  53. return $data; 
  54.  
  55. $taxonomy = get_term_by( 'id', $data['term_id'], $taxonomy_type ); 
  56.  
  57. $return = $this->get_taxonomy( $taxonomy->slug, $taxonomy_type, $args['context'] ); 
  58. if ( !$return || is_wp_error( $return ) ) { 
  59. return $return; 
  60.  
  61. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */ 
  62. do_action( 'wpcom_json_api_objects', 'taxonomies' ); 
  63. return $return; 
  64.  
  65. // /sites/%s/tags|categories/slug:%s -> $blog_id, $taxonomy_id 
  66. function update_taxonomy( $path, $blog_id, $object_id, $taxonomy_type ) { 
  67. $taxonomy = get_term_by( 'slug', $object_id, $taxonomy_type ); 
  68. $tax = get_taxonomy( $taxonomy_type ); 
  69. if ( !current_user_can( $tax->cap->edit_terms ) ) 
  70. return new WP_Error( 'unauthorized', 'User cannot edit taxonomy', 403 ); 
  71.  
  72. if ( !$taxonomy || is_wp_error( $taxonomy ) ) { 
  73. return new WP_Error( 'unknown_taxonomy', 'Unknown taxonomy', 404 ); 
  74.  
  75. if ( false === term_exists( $object_id, $taxonomy_type ) ) { 
  76. return new WP_Error( 'unknown_taxonomy', 'That taxonomy does not exist', 404 ); 
  77.  
  78. $args = $this->query_args(); 
  79. $input = $this->input( false ); 
  80. if ( !is_array( $input ) || !$input ) { 
  81. return new WP_Error( 'invalid_input', 'Invalid request input', 400 ); 
  82.  
  83. $update = array(); 
  84. if ( 'category' === $taxonomy_type && !empty( $input['parent'] ) ) 
  85. $update['parent'] = $input['parent']; 
  86.  
  87. if ( !empty( $input['description'] ) ) 
  88. $update['description'] = addslashes( $input['description'] ); 
  89.  
  90. if ( !empty( $input['name'] ) ) 
  91. $update['name'] = addslashes( $input['name'] ); 
  92.  
  93.  
  94. $data = wp_update_term( $taxonomy->term_id, $taxonomy_type, $update ); 
  95. $taxonomy = get_term_by( 'id', $data['term_id'], $taxonomy_type ); 
  96.  
  97. $return = $this->get_taxonomy( $taxonomy->slug, $taxonomy_type, $args['context'] ); 
  98. if ( !$return || is_wp_error( $return ) ) { 
  99. return $return; 
  100.  
  101. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */ 
  102. do_action( 'wpcom_json_api_objects', 'taxonomies' ); 
  103. return $return; 
  104.  
  105. // /sites/%s/tags|categories/%s/delete -> $blog_id, $taxonomy_id 
  106. function delete_taxonomy( $path, $blog_id, $object_id, $taxonomy_type ) { 
  107. $taxonomy = get_term_by( 'slug', $object_id, $taxonomy_type ); 
  108. $tax = get_taxonomy( $taxonomy_type ); 
  109. if ( !current_user_can( $tax->cap->delete_terms ) ) 
  110. return new WP_Error( 'unauthorized', 'User cannot edit taxonomy', 403 ); 
  111.  
  112. if ( !$taxonomy || is_wp_error( $taxonomy ) ) { 
  113. return new WP_Error( 'unknown_taxonomy', 'Unknown taxonomy', 404 ); 
  114.  
  115. if ( false === term_exists( $object_id, $taxonomy_type ) ) { 
  116. return new WP_Error( 'unknown_taxonomy', 'That taxonomy does not exist', 404 ); 
  117.  
  118. $args = $this->query_args(); 
  119. $return = $this->get_taxonomy( $taxonomy->slug, $taxonomy_type, $args['context'] ); 
  120. if ( !$return || is_wp_error( $return ) ) { 
  121. return $return; 
  122.  
  123. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */ 
  124. do_action( 'wpcom_json_api_objects', 'taxonomies' ); 
  125.  
  126. wp_delete_term( $taxonomy->term_id, $taxonomy_type ); 
  127.  
  128. return array( 
  129. 'slug' => (string) $taxonomy->slug,  
  130. 'success' => 'true',  
  131. );