Completed
Push — sync/taxonomy-endpoint ( 626369 )
by
unknown
193:57 queued 184:45
created

new_taxonomy()   D

Complexity

Conditions 16
Paths 17

Size

Total Lines 48
Code Lines 28

Duplication

Lines 12
Ratio 25 %

Importance

Changes 0
Metric Value
cc 16
eloc 28
nc 17
nop 3
dl 12
loc 48
rs 4.9765
c 0
b 0
f 0

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
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 ) {
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...
30
		$args  = $this->query_args();
31
		$input = $this->input();
32 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...
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'] );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 3 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
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 ) {
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...
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 ) {
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...
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'] );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 3 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

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