Code Duplication    Length = 64-64 lines in 2 locations

json-endpoints/class.wpcom-json-api-update-post-endpoint.php 1 location

@@ 149-212 (lines=64) @@
146
147
		$tax_input = array();
148
149
		foreach ( array( 'categories' => 'category', 'tags' => 'post_tag' ) as $key => $taxonomy ) {
150
			if ( ! isset( $input[ $key ] ) ) {
151
				continue;
152
			}
153
154
			$tax_input[ $taxonomy ] = array();
155
156
			$is_hierarchical = is_taxonomy_hierarchical( $taxonomy );
157
158
			if ( is_array( $input[$key] ) ) {
159
				$terms = $input[$key];
160
			} else {
161
				$terms = explode( ',', $input[$key] );
162
			}
163
164
			foreach ( $terms as $term ) {
165
				/**
166
				 * `curl --data 'category[]=123'` should be interpreted as a category ID,
167
				 * not a category whose name is '123'.
168
				 *
169
				 * Consequence: To add a category/tag whose name is '123', the client must
170
				 * first look up its ID.
171
				 */
172
				$term = (string) $term; // ctype_digit compat
173
				if ( ctype_digit( $term ) ) {
174
					$term = (int) $term;
175
				}
176
177
				$term_info = term_exists( $term, $taxonomy );
178
179
				if ( ! $term_info ) {
180
					// A term ID that doesn't already exist. Ignore it: we don't know what name to give it.
181
					if ( is_int( $term ) ){
182
						continue;
183
					}
184
					// only add a new tag/cat if the user has access to
185
					$tax = get_taxonomy( $taxonomy );
186
187
					// see https://core.trac.wordpress.org/ticket/26409
188
					if ( 'category' === $taxonomy && ! current_user_can( $tax->cap->edit_terms ) ) {
189
						continue;
190
					} else if ( ! current_user_can( $tax->cap->assign_terms ) ) {
191
						continue;
192
					}
193
194
					$term_info = wp_insert_term( $term, $taxonomy );
195
				}
196
197
				if ( ! is_wp_error( $term_info ) ) {
198
					if ( $is_hierarchical ) {
199
						// Categories must be added by ID
200
						$tax_input[$taxonomy][] = (int) $term_info['term_id'];
201
					} else {
202
						// Tags must be added by name
203
						if ( is_int( $term ) ) {
204
							$term = get_term( $term, $taxonomy );
205
							$tax_input[$taxonomy][] = $term->name;
206
						} else {
207
							$tax_input[$taxonomy][] = $term;
208
						}
209
					}
210
				}
211
			}
212
		}
213
214
		if ( isset( $input['categories'] ) && empty( $tax_input['category'] ) && 'revision' !== $post_type->name ) {
215
			$tax_input['category'][] = get_option( 'default_category' );

json-endpoints/class.wpcom-json-api-update-post-v1-1-endpoint.php 1 location

@@ 154-217 (lines=64) @@
151
152
		$tax_input = array();
153
154
		foreach ( array( 'categories' => 'category', 'tags' => 'post_tag' ) as $key => $taxonomy ) {
155
			if ( ! isset( $input[ $key ] ) ) {
156
				continue;
157
			}
158
159
			$tax_input[ $taxonomy ] = array();
160
161
			$is_hierarchical = is_taxonomy_hierarchical( $taxonomy );
162
163
			if ( is_array( $input[$key] ) ) {
164
				$terms = $input[$key];
165
			} else {
166
				$terms = explode( ',', $input[$key] );
167
			}
168
169
			foreach ( $terms as $term ) {
170
				/**
171
				 * `curl --data 'category[]=123'` should be interpreted as a category ID,
172
				 * not a category whose name is '123'.
173
				 *
174
				 * Consequence: To add a category/tag whose name is '123', the client must
175
				 * first look up its ID.
176
				 */
177
				$term = (string) $term; // ctype_digit compat
178
				if ( ctype_digit( $term ) ) {
179
					$term = (int) $term;
180
				}
181
182
				$term_info = term_exists( $term, $taxonomy );
183
184
				if ( ! $term_info ) {
185
					// A term ID that doesn't already exist. Ignore it: we don't know what name to give it.
186
					if ( is_int( $term ) ){
187
						continue;
188
					}
189
					// only add a new tag/cat if the user has access to
190
					$tax = get_taxonomy( $taxonomy );
191
192
					// see https://core.trac.wordpress.org/ticket/26409
193
					if ( 'category' === $taxonomy && ! current_user_can( $tax->cap->edit_terms ) ) {
194
						continue;
195
					} else if ( ! current_user_can( $tax->cap->assign_terms ) ) {
196
						continue;
197
					}
198
199
					$term_info = wp_insert_term( $term, $taxonomy );
200
				}
201
202
				if ( ! is_wp_error( $term_info ) ) {
203
					if ( $is_hierarchical ) {
204
						// Categories must be added by ID
205
						$tax_input[$taxonomy][] = (int) $term_info['term_id'];
206
					} else {
207
						// Tags must be added by name
208
						if ( is_int( $term ) ) {
209
							$term = get_term( $term, $taxonomy );
210
							$tax_input[$taxonomy][] = $term->name;
211
						} else {
212
							$tax_input[$taxonomy][] = $term;
213
						}
214
					}
215
				}
216
			}
217
		}
218
219
		if ( isset( $input['categories'] ) && empty( $tax_input['category'] ) && 'revision' !== $post_type->name ) {
220
			$tax_input['category'][] = get_option( 'default_category' );