Code Duplication    Length = 48-48 lines in 2 locations

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

@@ 357-404 (lines=48) @@
354
				$terms = explode( ',', $input[$key] );
355
			}
356
357
			foreach ( $terms as $term ) {
358
				/**
359
				 * `curl --data 'category[]=123'` should be interpreted as a category ID,
360
				 * not a category whose name is '123'.
361
				 *
362
				 * Consequence: To add a category/tag whose name is '123', the client must
363
				 * first look up its ID.
364
				 */
365
				$term = (string) $term; // ctype_digit compat
366
				if ( ctype_digit( $term ) ) {
367
					$term = (int) $term;
368
				}
369
370
				$term_info = term_exists( $term, $taxonomy );
371
372
				if ( ! $term_info ) {
373
					// A term ID that doesn't already exist. Ignore it: we don't know what name to give it.
374
					if ( is_int( $term ) ){
375
						continue;
376
					}
377
					// only add a new tag/cat if the user has access to
378
					$tax = get_taxonomy( $taxonomy );
379
380
					// see https://core.trac.wordpress.org/ticket/26409
381
					if ( 'category' === $taxonomy && ! current_user_can( $tax->cap->edit_terms ) ) {
382
						continue;
383
					} else if ( ! current_user_can( $tax->cap->assign_terms ) ) {
384
						continue;
385
					}
386
387
					$term_info = wp_insert_term( $term, $taxonomy );
388
				}
389
390
				if ( ! is_wp_error( $term_info ) ) {
391
					if ( $is_hierarchical ) {
392
						// Categories must be added by ID
393
						$tax_input[$taxonomy][] = (int) $term_info['term_id'];
394
					} else {
395
						// Tags must be added by name
396
						if ( is_int( $term ) ) {
397
							$term = get_term( $term, $taxonomy );
398
							$tax_input[$taxonomy][] = $term->name;
399
						} else {
400
							$tax_input[$taxonomy][] = $term;
401
						}
402
					}
403
				}
404
			}
405
		}
406
407
		if ( isset( $input['categories'] ) && empty( $tax_input['category'] ) && 'revision' !== $post_type->name ) {

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

@@ 404-451 (lines=48) @@
401
			$tax_input[ $taxonomy ] = array();
402
			$is_hierarchical = is_taxonomy_hierarchical( $taxonomy );
403
404
			foreach ( $terms as $term ) {
405
				/**
406
				 * `curl --data 'terms[category][]=123'` should be interpreted as a category ID,
407
				 * not a category whose name is '123'.
408
				 *
409
				 * Consequence: To add a category/tag whose name is '123', the client must
410
				 * first look up its ID.
411
				 */
412
				$term = (string) $term; // ctype_digit compat
413
				if ( ctype_digit( $term ) ) {
414
					$term = (int) $term;
415
				}
416
417
				$term_info = term_exists( $term, $taxonomy );
418
419
				if ( ! $term_info ) {
420
					// A term ID that doesn't already exist. Ignore it: we don't know what name to give it.
421
					if ( is_int( $term ) ){
422
						continue;
423
					}
424
					// only add a new tag/cat if the user has access to
425
					$tax = get_taxonomy( $taxonomy );
426
427
					// see https://core.trac.wordpress.org/ticket/26409
428
					if ( $is_hierarchical && ! current_user_can( $tax->cap->edit_terms ) ) {
429
						continue;
430
					} else if ( ! current_user_can( $tax->cap->assign_terms ) ) {
431
						continue;
432
					}
433
434
					$term_info = wp_insert_term( $term, $taxonomy );
435
				}
436
437
				if ( ! is_wp_error( $term_info ) ) {
438
					if ( $is_hierarchical ) {
439
						// Hierarchical terms must be added by ID
440
						$tax_input[$taxonomy][] = (int) $term_info['term_id'];
441
					} else {
442
						// Non-hierarchical terms must be added by name
443
						if ( is_int( $term ) ) {
444
							$term = get_term( $term, $taxonomy );
445
							$tax_input[$taxonomy][] = $term->name;
446
						} else {
447
							$tax_input[$taxonomy][] = $term;
448
						}
449
					}
450
				}
451
			}
452
		}
453
454
		if ( isset( $input['terms']['category'] ) && empty( $tax_input['category'] ) && 'revision' !== $post_type->name ) {