class.wpcom-json-api-update-taxonomy-endpoint.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. new WPCOM_JSON_API_Update_Taxonomy_Endpoint( array(
  3. 'description' => 'Create a new category.',
  4. 'group' => 'taxonomy',
  5. 'stat' => 'categories:new',
  6. 'method' => 'POST',
  7. 'path' => '/sites/%s/categories/new',
  8. 'path_labels' => array(
  9. '$site' => '(int|string) Site ID or domain',
  10. ),
  11. 'request_format' => array(
  12. 'name' => '(string) Name of the category',
  13. 'description' => '(string) A description of the category',
  14. 'parent' => '(int) ID of the parent category',
  15. ),
  16. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/categories/new/',
  17. 'example_request_data' => array(
  18. 'headers' => array(
  19. 'authorization' => 'Bearer YOUR_API_TOKEN'
  20. ),
  21. 'body' => array(
  22. 'name' => 'Puppies',
  23. )
  24. )
  25. ) );
  26. new WPCOM_JSON_API_Update_Taxonomy_Endpoint( array(
  27. 'description' => 'Create a new tag.',
  28. 'group' => 'taxonomy',
  29. 'stat' => 'tags:new',
  30. 'method' => 'POST',
  31. 'path' => '/sites/%s/tags/new',
  32. 'path_labels' => array(
  33. '$site' => '(int|string) Site ID or domain',
  34. ),
  35. 'request_format' => array(
  36. 'name' => '(string) Name of the tag',
  37. 'description' => '(string) A description of the tag',
  38. ),
  39. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/tags/new/',
  40. 'example_request_data' => array(
  41. 'headers' => array(
  42. 'authorization' => 'Bearer YOUR_API_TOKEN'
  43. ),
  44. 'body' => array(
  45. 'name' => 'Kitties'
  46. )
  47. )
  48. ) );
  49. new WPCOM_JSON_API_Update_Taxonomy_Endpoint( array(
  50. 'description' => 'Edit a tag.',
  51. 'group' => 'taxonomy',
  52. 'stat' => 'tags:1:POST',
  53. 'method' => 'POST',
  54. 'path' => '/sites/%s/tags/slug:%s',
  55. 'path_labels' => array(
  56. '$site' => '(int|string) Site ID or domain',
  57. '$tag' => '(string) The tag slug',
  58. ),
  59. 'request_format' => array(
  60. 'name' => '(string) Name of the tag',
  61. 'description' => '(string) A description of the tag',
  62. ),
  63. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/tags/slug:testing-tag',
  64. 'example_request_data' => array(
  65. 'headers' => array(
  66. 'authorization' => 'Bearer YOUR_API_TOKEN'
  67. ),
  68. 'body' => array(
  69. 'description' => 'Kitties are awesome!'
  70. )
  71. )
  72. ) );
  73. new WPCOM_JSON_API_Update_Taxonomy_Endpoint( array(
  74. 'description' => 'Edit a category.',
  75. 'group' => 'taxonomy',
  76. 'stat' => 'categories:1:POST',
  77. 'method' => 'POST',
  78. 'path' => '/sites/%s/categories/slug:%s',
  79. 'path_labels' => array(
  80. '$site' => '(int|string) Site ID or domain',
  81. '$category' => '(string) The category slug',
  82. ),
  83. 'request_format' => array(
  84. 'name' => '(string) Name of the category',
  85. 'description' => '(string) A description of the category',
  86. 'parent' => '(int) ID of the parent category',
  87. ),
  88. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/categories/slug:testing-category',
  89. 'example_request_data' => array(
  90. 'headers' => array(
  91. 'authorization' => 'Bearer YOUR_API_TOKEN'
  92. ),
  93. 'body' => array(
  94. 'description' => 'Puppies are great!'
  95. )
  96. )
  97. ) );
  98. new WPCOM_JSON_API_Update_Taxonomy_Endpoint( array(
  99. 'description' => 'Delete a category.',
  100. 'group' => 'taxonomy',
  101. 'stat' => 'categories:1:delete',
  102. 'method' => 'POST',
  103. 'path' => '/sites/%s/categories/slug:%s/delete',
  104. 'path_labels' => array(
  105. '$site' => '(int|string) Site ID or domain',
  106. '$category' => '(string) The category slug',
  107. ),
  108. 'response_format' => array(
  109. 'slug' => '(string) The slug of the deleted category',
  110. 'success' => '(bool) Was the operation successful?',
  111. ),
  112. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/categories/slug:$category/delete',
  113. 'example_request_data' => array(
  114. 'headers' => array(
  115. 'authorization' => 'Bearer YOUR_API_TOKEN'
  116. ),
  117. )
  118. ) );
  119. new WPCOM_JSON_API_Update_Taxonomy_Endpoint( array(
  120. 'description' => 'Delete a tag.',
  121. 'group' => 'taxonomy',
  122. 'stat' => 'tags:1:delete',
  123. 'method' => 'POST',
  124. 'path' => '/sites/%s/tags/slug:%s/delete',
  125. 'path_labels' => array(
  126. '$site' => '(int|string) Site ID or domain',
  127. '$tag' => '(string) The tag slug',
  128. ),
  129. 'response_format' => array(
  130. 'slug' => '(string) The slug of the deleted tag',
  131. 'success' => '(bool) Was the operation successful?',
  132. ),
  133. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/tags/slug:$tag/delete',
  134. 'example_request_data' => array(
  135. 'headers' => array(
  136. 'authorization' => 'Bearer YOUR_API_TOKEN'
  137. ),
  138. )
  139. ) );
  140. class WPCOM_JSON_API_Update_Taxonomy_Endpoint extends WPCOM_JSON_API_Taxonomy_Endpoint {
  141. // /sites/%s/tags|categories/new -> $blog_id
  142. // /sites/%s/tags|categories/slug:%s -> $blog_id, $taxonomy_id
  143. // /sites/%s/tags|categories/slug:%s/delete -> $blog_id, $taxonomy_id
  144. function callback( $path = '', $blog_id = 0, $object_id = 0 ) {
  145. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  146. if ( is_wp_error( $blog_id ) ) {
  147. return $blog_id;
  148. }
  149. if ( preg_match( '#/tags/#i', $path ) ) {
  150. $taxonomy_type = "post_tag";
  151. } else {
  152. $taxonomy_type = "category";
  153. }
  154. if ( $this->api->ends_with( $path, '/delete' ) ) {
  155. return $this->delete_taxonomy( $path, $blog_id, $object_id, $taxonomy_type );
  156. } elseif ( $this->api->ends_with( $path, '/new' ) ) {
  157. return $this->new_taxonomy( $path, $blog_id, $taxonomy_type );
  158. }
  159. return $this->update_taxonomy( $path, $blog_id, $object_id, $taxonomy_type );
  160. }
  161. // /sites/%s/tags|categories/new -> $blog_id
  162. function new_taxonomy( $path, $blog_id, $taxonomy_type ) {
  163. $args = $this->query_args();
  164. $input = $this->input();
  165. if ( !is_array( $input ) || !$input || !strlen( $input['name'] ) ) {
  166. return new WP_Error( 'invalid_input', 'Unknown data passed', 400 );
  167. }
  168. $user = wp_get_current_user();
  169. if ( !$user || is_wp_error( $user ) || !$user->ID ) {
  170. return new WP_Error( 'authorization_required', 'An active access token must be used to manage taxonomies.', 403 );
  171. }
  172. $tax = get_taxonomy( $taxonomy_type );
  173. if ( !current_user_can( $tax->cap->edit_terms ) ) {
  174. return new WP_Error( 'unauthorized', 'User cannot edit taxonomy', 403 );
  175. }
  176. if ( 'category' !== $taxonomy_type || ! isset( $input['parent'] ) )
  177. $input['parent'] = 0;
  178. if ( $term = get_term_by( 'name', $input['name'], $taxonomy_type ) ) {
  179. // the same name is allowed as long as the parents are different
  180. if ( $input['parent'] === $term->parent ) {
  181. return new WP_Error( 'duplicate', 'A taxonomy with that name already exists', 400 );
  182. }
  183. }
  184. $data = wp_insert_term( addslashes( $input['name'] ), $taxonomy_type,
  185. array(
  186. 'description' => isset( $input['description'] ) ? addslashes( $input['description'] ) : '',
  187. 'parent' => $input['parent']
  188. )
  189. );
  190. if ( is_wp_error( $data ) )
  191. return $data;
  192. $taxonomy = get_term_by( 'id', $data['term_id'], $taxonomy_type );
  193. $return = $this->get_taxonomy( $taxonomy->slug, $taxonomy_type, $args['context'] );
  194. if ( !$return || is_wp_error( $return ) ) {
  195. return $return;
  196. }
  197. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  198. do_action( 'wpcom_json_api_objects', 'taxonomies' );
  199. return $return;
  200. }
  201. // /sites/%s/tags|categories/slug:%s -> $blog_id, $taxonomy_id
  202. function update_taxonomy( $path, $blog_id, $object_id, $taxonomy_type ) {
  203. $taxonomy = get_term_by( 'slug', $object_id, $taxonomy_type );
  204. $tax = get_taxonomy( $taxonomy_type );
  205. if ( !current_user_can( $tax->cap->edit_terms ) )
  206. return new WP_Error( 'unauthorized', 'User cannot edit taxonomy', 403 );
  207. if ( !$taxonomy || is_wp_error( $taxonomy ) ) {
  208. return new WP_Error( 'unknown_taxonomy', 'Unknown taxonomy', 404 );
  209. }
  210. if ( false === term_exists( $object_id, $taxonomy_type ) ) {
  211. return new WP_Error( 'unknown_taxonomy', 'That taxonomy does not exist', 404 );
  212. }
  213. $args = $this->query_args();
  214. $input = $this->input( false );
  215. if ( !is_array( $input ) || !$input ) {
  216. return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
  217. }
  218. $update = array();
  219. if ( 'category' === $taxonomy_type && !empty( $input['parent'] ) )
  220. $update['parent'] = $input['parent'];
  221. if ( !empty( $input['description'] ) )
  222. $update['description'] = addslashes( $input['description'] );
  223. if ( !empty( $input['name'] ) )
  224. $update['name'] = addslashes( $input['name'] );
  225. $data = wp_update_term( $taxonomy->term_id, $taxonomy_type, $update );
  226. $taxonomy = get_term_by( 'id', $data['term_id'], $taxonomy_type );
  227. $return = $this->get_taxonomy( $taxonomy->slug, $taxonomy_type, $args['context'] );
  228. if ( !$return || is_wp_error( $return ) ) {
  229. return $return;
  230. }
  231. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  232. do_action( 'wpcom_json_api_objects', 'taxonomies' );
  233. return $return;
  234. }
  235. // /sites/%s/tags|categories/%s/delete -> $blog_id, $taxonomy_id
  236. function delete_taxonomy( $path, $blog_id, $object_id, $taxonomy_type ) {
  237. $taxonomy = get_term_by( 'slug', $object_id, $taxonomy_type );
  238. $tax = get_taxonomy( $taxonomy_type );
  239. if ( !current_user_can( $tax->cap->delete_terms ) )
  240. return new WP_Error( 'unauthorized', 'User cannot edit taxonomy', 403 );
  241. if ( !$taxonomy || is_wp_error( $taxonomy ) ) {
  242. return new WP_Error( 'unknown_taxonomy', 'Unknown taxonomy', 404 );
  243. }
  244. if ( false === term_exists( $object_id, $taxonomy_type ) ) {
  245. return new WP_Error( 'unknown_taxonomy', 'That taxonomy does not exist', 404 );
  246. }
  247. $args = $this->query_args();
  248. $return = $this->get_taxonomy( $taxonomy->slug, $taxonomy_type, $args['context'] );
  249. if ( !$return || is_wp_error( $return ) ) {
  250. return $return;
  251. }
  252. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  253. do_action( 'wpcom_json_api_objects', 'taxonomies' );
  254. wp_delete_term( $taxonomy->term_id, $taxonomy_type );
  255. return array(
  256. 'slug' => (string) $taxonomy->slug,
  257. 'success' => 'true',
  258. );
  259. }
  260. }