Code Duplication    Length = 63-63 lines in 2 locations

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

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

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

@@ 149-211 (lines=63) @@
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
				if ( ctype_digit( $term ) ) {
173
					$term = (int) $term;
174
				}
175
176
				$term_info = term_exists( $term, $taxonomy );
177
178
				if ( ! $term_info ) {
179
					// A term ID that doesn't already exist. Ignore it: we don't know what name to give it.
180
					if ( is_int( $term ) ){
181
						continue;
182
					}
183
					// only add a new tag/cat if the user has access to
184
					$tax = get_taxonomy( $taxonomy );
185
186
					// see https://core.trac.wordpress.org/ticket/26409
187
					if ( 'category' === $taxonomy && ! current_user_can( $tax->cap->edit_terms ) ) {
188
						continue;
189
					} else if ( ! current_user_can( $tax->cap->assign_terms ) ) {
190
						continue;
191
					}
192
193
					$term_info = wp_insert_term( $term, $taxonomy );
194
				}
195
196
				if ( ! is_wp_error( $term_info ) ) {
197
					if ( $is_hierarchical ) {
198
						// Categories must be added by ID
199
						$tax_input[$taxonomy][] = (int) $term_info['term_id'];
200
					} else {
201
						// Tags must be added by name
202
						if ( is_int( $term ) ) {
203
							$term = get_term( $term, $taxonomy );
204
							$tax_input[$taxonomy][] = $term->name;
205
						} else {
206
							$tax_input[$taxonomy][] = $term;
207
						}
208
					}
209
				}
210
			}
211
		}
212
213
		if ( isset( $input['categories'] ) && empty( $tax_input['category'] ) && 'revision' !== $post_type->name ) {
214
			$tax_input['category'][] = get_option( 'default_category' );