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