Code Duplication    Length = 8-9 lines in 3 locations

lib/endpoints/class-wp-rest-attachments-controller.php 1 location

@@ 118-125 (lines=8) @@
115
		}
116
117
		$id = wp_insert_post( $attachment, true );
118
		if ( is_wp_error( $id ) ) {
119
			if ( in_array( $id->get_error_code(), array( 'db_update_error' ) ) ) {
120
				$id->add_data( array( 'status' => 500 ) );
121
			} else {
122
				$id->add_data( array( 'status' => 400 ) );
123
			}
124
			return $id;
125
		}
126
		$attachment = $this->get_post( $id );
127
128
		/** Include admin functions to get access to wp_generate_attachment_metadata() */

lib/endpoints/class-wp-rest-posts-controller.php 2 locations

@@ 380-388 (lines=9) @@
377
		$post->post_type = $this->post_type;
378
		$post_id = wp_insert_post( $post, true );
379
380
		if ( is_wp_error( $post_id ) ) {
381
382
			if ( in_array( $post_id->get_error_code(), array( 'db_insert_error' ) ) ) {
383
				$post_id->add_data( array( 'status' => 500 ) );
384
			} else {
385
				$post_id->add_data( array( 'status' => 400 ) );
386
			}
387
			return $post_id;
388
		}
389
		$post->ID = $post_id;
390
391
		$schema = $this->get_item_schema();
@@ 487-494 (lines=8) @@
484
		}
485
		// convert the post object to an array, otherwise wp_update_post will expect non-escaped input
486
		$post_id = wp_update_post( (array) $post, true );
487
		if ( is_wp_error( $post_id ) ) {
488
			if ( in_array( $post_id->get_error_code(), array( 'db_update_error' ) ) ) {
489
				$post_id->add_data( array( 'status' => 500 ) );
490
			} else {
491
				$post_id->add_data( array( 'status' => 400 ) );
492
			}
493
			return $post_id;
494
		}
495
496
		$schema = $this->get_item_schema();
497