WPCOM_JSON_API_Update_Term_Endpoint

The WordPress Core WPCOM JSON API Update Term Endpoint class.

Defined (1)

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

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