Code Duplication    Length = 45-45 lines in 2 locations

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

@@ 539-583 (lines=45) @@
536
			$media_id_string = join( ',', array_filter( array_map( 'absint', $media_results['media_ids'] ) ) );
537
		}
538
539
		if ( $new ) {
540
			if ( isset( $input['content'] ) && ! has_shortcode( $input['content'], 'gallery' ) && ( $has_media || $has_media_by_url ) ) {
541
				switch ( ( $has_media + $has_media_by_url ) ) {
542
				case 0 :
543
					// No images - do nothing.
544
					break;
545
				case 1 :
546
					// 1 image - make it big
547
					$insert['post_content'] = $input['content'] = sprintf(
548
						"[gallery size=full ids='%s' columns=1]\n\n",
549
						$media_id_string
550
					) . $input['content'];
551
					break;
552
				default :
553
					// Several images - 3 column gallery
554
					$insert['post_content'] = $input['content'] = sprintf(
555
						"[gallery ids='%s']\n\n",
556
						$media_id_string
557
					) . $input['content'];
558
					break;
559
				}
560
			}
561
562
			$post_id = wp_insert_post( add_magic_quotes( $insert ), true );
563
		} else {
564
			$insert['ID'] = $post->ID;
565
566
			// wp_update_post ignores date unless edit_date is set
567
			// See: https://codex.wordpress.org/Function_Reference/wp_update_post#Scheduling_posts
568
			// See: https://core.trac.wordpress.org/browser/tags/3.9.2/src/wp-includes/post.php#L3302
569
			if ( isset( $input['date_gmt'] ) || isset( $input['date'] ) ) {
570
				$insert['edit_date'] = true;
571
			}
572
573
			// this two-step process ensures any changes submitted along with status=trash get saved before trashing
574
			if ( isset( $input['status'] ) && 'trash' === $input['status'] ) {
575
				// if we insert it with status='trash', it will get double-trashed, so insert it as a draft first
576
				unset( $insert['status'] );
577
				$post_id = wp_update_post( (object) $insert );
578
				// now call wp_trash_post so post_meta gets set and any filters get called
579
				wp_trash_post( $post_id );
580
			} else {
581
				$post_id = wp_update_post( (object) $insert );
582
			}
583
		}
584
585
586
		if ( !$post_id || is_wp_error( $post_id ) ) {

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

@@ 510-554 (lines=45) @@
507
			$media_id_string = join( ',', array_filter( array_map( 'absint', $media_results['media_ids'] ) ) );
508
		}
509
510
		if ( $new ) {
511
			if ( isset( $input['content'] ) && ! has_shortcode( $input['content'], 'gallery' ) && ( $has_media || $has_media_by_url ) ) {
512
				switch ( ( $has_media + $has_media_by_url ) ) {
513
				case 0 :
514
					// No images - do nothing.
515
					break;
516
				case 1 :
517
					// 1 image - make it big
518
					$insert['post_content'] = $input['content'] = sprintf(
519
						"[gallery size=full ids='%s' columns=1]\n\n",
520
						$media_id_string
521
					) . $input['content'];
522
					break;
523
				default :
524
					// Several images - 3 column gallery
525
					$insert['post_content'] = $input['content'] = sprintf(
526
						"[gallery ids='%s']\n\n",
527
						$media_id_string
528
					) . $input['content'];
529
					break;
530
				}
531
			}
532
533
			$post_id = wp_insert_post( add_magic_quotes( $insert ), true );
534
		} else {
535
			$insert['ID'] = $post->ID;
536
537
			// wp_update_post ignores date unless edit_date is set
538
			// See: https://codex.wordpress.org/Function_Reference/wp_update_post#Scheduling_posts
539
			// See: https://core.trac.wordpress.org/browser/tags/3.9.2/src/wp-includes/post.php#L3302
540
			if ( isset( $input['date_gmt'] ) || isset( $input['date'] ) ) {
541
				$insert['edit_date'] = true;
542
			}
543
544
			// this two-step process ensures any changes submitted along with status=trash get saved before trashing
545
			if ( isset( $input['status'] ) && 'trash' === $input['status'] ) {
546
				// if we insert it with status='trash', it will get double-trashed, so insert it as a draft first
547
				unset( $insert['status'] );
548
				$post_id = wp_update_post( (object) $insert );
549
				// now call wp_trash_post so post_meta gets set and any filters get called
550
				wp_trash_post( $post_id );
551
			} else {
552
				$post_id = wp_update_post( (object) $insert );
553
			}
554
		}
555
556
557
		if ( !$post_id || is_wp_error( $post_id ) ) {