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