class.wpcom-json-api-update-term-endpoint.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. new WPCOM_JSON_API_Update_Term_Endpoint( array(
  3. 'description' => 'Create a new term.',
  4. 'group' => 'taxonomy',
  5. 'stat' => 'terms:new',
  6. 'method' => 'POST',
  7. 'path' => '/sites/%s/taxonomies/%s/terms/new',
  8. 'path_labels' => array(
  9. '$site' => '(int|string) Site ID or domain',
  10. '$taxonomy' => '(string) Taxonomy',
  11. ),
  12. 'request_format' => array(
  13. 'name' => '(string) Name of the term',
  14. 'description' => '(string) A description of the term',
  15. 'parent' => '(int) The parent ID for the term, if hierarchical',
  16. ),
  17. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/taxonomies/post_tag/terms/new',
  18. 'example_request_data' => array(
  19. 'headers' => array(
  20. 'authorization' => 'Bearer YOUR_API_TOKEN'
  21. ),
  22. 'body' => array(
  23. 'name' => 'Ribs & Chicken'
  24. )
  25. )
  26. ) );
  27. new WPCOM_JSON_API_Update_Term_Endpoint( array(
  28. 'description' => 'Edit a term.',
  29. 'group' => 'taxonomy',
  30. 'stat' => 'terms:1:POST',
  31. 'method' => 'POST',
  32. 'path' => '/sites/%s/taxonomies/%s/terms/slug:%s',
  33. 'path_labels' => array(
  34. '$site' => '(int|string) Site ID or domain',
  35. '$taxonomy' => '(string) Taxonomy',
  36. '$slug' => '(string) The term slug',
  37. ),
  38. 'request_format' => array(
  39. 'name' => '(string) Name of the term',
  40. 'description' => '(string) A description of the term',
  41. 'parent' => '(int) The parent ID for the term, if hierarchical',
  42. ),
  43. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/taxonomies/post_tag/terms/slug:testing-term',
  44. 'example_request_data' => array(
  45. 'headers' => array(
  46. 'authorization' => 'Bearer YOUR_API_TOKEN'
  47. ),
  48. 'body' => array(
  49. 'description' => 'The most delicious'
  50. )
  51. )
  52. ) );
  53. new WPCOM_JSON_API_Update_Term_Endpoint( array(
  54. 'description' => 'Delete a term.',
  55. 'group' => 'taxonomy',
  56. 'stat' => 'terms:1:delete',
  57. 'method' => 'POST',
  58. 'path' => '/sites/%s/taxonomies/%s/terms/slug:%s/delete',
  59. 'path_labels' => array(
  60. '$site' => '(int|string) Site ID or domain',
  61. '$taxonomy' => '(string) Taxonomy',
  62. '$slug' => '(string) The term slug',
  63. ),
  64. 'response_format' => array(
  65. 'slug' => '(string) The slug of the deleted term',
  66. 'success' => '(bool) Whether the operation was successful',
  67. ),
  68. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/taxonomies/post_tag/terms/slug:$term/delete',
  69. 'example_request_data' => array(
  70. 'headers' => array(
  71. 'authorization' => 'Bearer YOUR_API_TOKEN'
  72. ),
  73. )
  74. ) );
  75. class WPCOM_JSON_API_Update_Term_Endpoint extends WPCOM_JSON_API_Taxonomy_Endpoint {
  76. // /sites/%s/taxonomies/%s/terms/new -> $blog_id, $taxonomy
  77. // /sites/%s/taxonomies/%s/terms/slug:%s -> $blog_id, $taxonomy, $slug
  78. // /sites/%s/taxonomies/%s/terms/slug:%s/delete -> $blog_id, $taxonomy, $slug
  79. function callback( $path = '', $blog_id = 0, $taxonomy = 'category', $slug = 0 ) {
  80. $slug = urldecode( $slug );
  81. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  82. if ( is_wp_error( $blog_id ) ) {
  83. return $blog_id;
  84. }
  85. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  86. $this->load_theme_functions();
  87. }
  88. $user = wp_get_current_user();
  89. if ( ! $user || is_wp_error( $user ) || ! $user->ID ) {
  90. return new WP_Error( 'authorization_required', 'An active access token must be used to manage taxonomies.', 403 );
  91. }
  92. $taxonomy_meta = get_taxonomy( $taxonomy );
  93. if ( false === $taxonomy_meta || (
  94. ! $taxonomy_meta->public &&
  95. ! current_user_can( $taxonomy_meta->cap->manage_terms ) &&
  96. ! current_user_can( $taxonomy_meta->cap->edit_terms ) &&
  97. ! current_user_can( $taxonomy_meta->cap->delete_terms ) ) ) {
  98. return new WP_Error( 'invalid_taxonomy', 'The taxonomy does not exist', 400 );
  99. }
  100. if ( $this->api->ends_with( $path, '/delete' ) ) {
  101. return $this->delete_term( $path, $blog_id, $slug, $taxonomy );
  102. } else if ( $this->api->ends_with( $path, '/new' ) ) {
  103. return $this->new_term( $path, $blog_id, $taxonomy );
  104. }
  105. return $this->update_term( $path, $blog_id, $slug, $taxonomy );
  106. }
  107. // /sites/%s/taxonomies/%s/terms/new -> $blog_id, $taxonomy
  108. function new_term( $path, $blog_id, $taxonomy ) {
  109. $args = $this->query_args();
  110. $input = $this->input();
  111. if ( ! is_array( $input ) || ! $input || ! strlen( $input['name'] ) ) {
  112. return new WP_Error( 'invalid_input', 'Unknown data passed', 400 );
  113. }
  114. $tax = get_taxonomy( $taxonomy );
  115. if ( ! current_user_can( $tax->cap->manage_terms ) ) {
  116. return new WP_Error( 'unauthorized', 'User cannot edit taxonomy', 403 );
  117. }
  118. if ( ! isset( $input['parent'] ) || ! is_taxonomy_hierarchical( $taxonomy ) ) {
  119. $input['parent'] = 0;
  120. }
  121. if ( $term = get_term_by( 'name', $input['name'], $taxonomy ) ) {
  122. // the same name is allowed as long as the parents are different
  123. if ( $input['parent'] === $term->parent ) {
  124. return new WP_Error( 'duplicate', 'A taxonomy with that name already exists', 409 );
  125. }
  126. }
  127. $data = wp_insert_term( addslashes( $input['name'] ), $taxonomy, array(
  128. 'description' => isset( $input['description'] ) ? addslashes( $input['description'] ) : '',
  129. 'parent' => $input['parent']
  130. ) );
  131. if ( is_wp_error( $data ) ) {
  132. return $data;
  133. }
  134. $term = get_term_by( 'id', $data['term_id'], $taxonomy );
  135. $return = $this->get_taxonomy( $term->slug, $taxonomy, $args['context'] );
  136. if ( ! $return || is_wp_error( $return ) ) {
  137. return $return;
  138. }
  139. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  140. do_action( 'wpcom_json_api_objects', 'terms' );
  141. return $return;
  142. }
  143. // /sites/%s/taxonomies/%s/terms/slug:%s -> $blog_id, $taxonomy, $slug
  144. function update_term( $path, $blog_id, $slug, $taxonomy ) {
  145. $tax = get_taxonomy( $taxonomy );
  146. if ( ! current_user_can( $tax->cap->edit_terms ) ) {
  147. return new WP_Error( 'unauthorized', 'User cannot edit taxonomy', 403 );
  148. }
  149. $term = get_term_by( 'slug', $slug, $taxonomy );
  150. if ( ! $term || is_wp_error( $term ) ) {
  151. return new WP_Error( 'unknown_taxonomy', 'Unknown taxonomy', 404 );
  152. }
  153. $args = $this->query_args();
  154. $input = $this->input( false );
  155. if ( ! is_array( $input ) || ! $input ) {
  156. return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
  157. }
  158. $update = array();
  159. if ( ! empty( $input['parent'] ) || is_taxonomy_hierarchical( $taxonomy ) ) {
  160. $update['parent'] = $input['parent'];
  161. }
  162. if ( ! empty( $input['description'] ) ) {
  163. $update['description'] = addslashes( $input['description'] );
  164. }
  165. if ( ! empty( $input['name'] ) ) {
  166. $update['name'] = addslashes( $input['name'] );
  167. }
  168. $data = wp_update_term( $term->term_id, $taxonomy, $update );
  169. if ( is_wp_error( $data ) ) {
  170. return $data;
  171. }
  172. $term = get_term_by( 'id', $data['term_id'], $taxonomy );
  173. $return = $this->get_taxonomy( $term->slug, $taxonomy, $args['context'] );
  174. if ( ! $return || is_wp_error( $return ) ) {
  175. return $return;
  176. }
  177. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  178. do_action( 'wpcom_json_api_objects', 'terms' );
  179. return $return;
  180. }
  181. // /sites/%s/taxonomies/%s/terms/slug:%s/delete -> $blog_id, $taxonomy, $slug
  182. function delete_term( $path, $blog_id, $slug, $taxonomy ) {
  183. $term = get_term_by( 'slug', $slug, $taxonomy );
  184. $tax = get_taxonomy( $taxonomy );
  185. if ( ! current_user_can( $tax->cap->delete_terms ) ) {
  186. return new WP_Error( 'unauthorized', 'User cannot edit taxonomy', 403 );
  187. }
  188. if ( ! $term || is_wp_error( $term ) ) {
  189. return new WP_Error( 'unknown_taxonomy', 'Unknown taxonomy', 404 );
  190. }
  191. $args = $this->query_args();
  192. $return = $this->get_taxonomy( $term->slug, $taxonomy, $args['context'] );
  193. if ( ! $return || is_wp_error( $return ) ) {
  194. return $return;
  195. }
  196. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  197. do_action( 'wpcom_json_api_objects', 'terms' );
  198. wp_delete_term( $term->term_id, $taxonomy );
  199. return array(
  200. 'slug' => (string) $term->slug,
  201. 'success' => true
  202. );
  203. }
  204. }