/wp-content/plugins/jetpack/json-endpoints/class.wpcom-json-api-update-taxonomy-endpoint.php

https://gitlab.com/juanito.abelo/nlmobile · PHP · 148 lines · 110 code · 32 blank · 6 comment · 34 complexity · 4cbc69dd9ac7584f98b5db18f4658389 MD5 · raw file

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