Completed
Push — prepare/4.1 ( 0b4dc2...26fe0a )
by Jeremy
279:11 queued 269:16
created

WPCOM_JSON_API_Update_Term_Endpoint::callback()   C

Complexity

Conditions 14
Paths 11

Size

Total Lines 32
Code Lines 21

Duplication

Lines 3
Ratio 9.38 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 14
eloc 21
c 2
b 0
f 2
nc 11
nop 4
dl 3
loc 32
rs 5.0864

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $blog_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
		$args = $this->query_args();
49
		$input = $this->input();
50 View Code Duplication
		if ( ! is_array( $input ) || ! $input || ! strlen( $input['name'] ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $input of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $blog_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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 ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $input of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $blog_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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