Code Duplication    Length = 23-23 lines in 3 locations

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

@@ 661-683 (lines=23) @@
658
	 *
659
	 * @return int|object Post ID on success, WP_Error object on failure
660
	 **/
661
	protected function get_post_id_by_name( $name ) {
662
		$name = sanitize_title( $name );
663
664
		if ( ! $name ) {
665
			return new WP_Error( 'invalid_post', 'Invalid post', 400 );
666
		}
667
668
		$posts = get_posts( array( 'name' => $name ) );
669
670
		if ( ! $posts || ! isset( $posts[0]->ID ) || ! $posts[0]->ID ) {
671
			$page = get_page_by_path( $name );
672
673
			if ( ! $page ) {
674
				return new WP_Error( 'unknown_post', 'Unknown post', 404 );
675
			}
676
677
			$post_id = $page->ID;
678
		} else {
679
			$post_id = (int) $posts[0]->ID;
680
		}
681
682
		return $post_id;
683
	}
684
}
685

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

@@ 688-710 (lines=23) @@
685
	 *
686
	 * @return int|object Post ID on success, WP_Error object on failure
687
	 **/
688
	protected function get_post_id_by_name( $name ) {
689
		$name = sanitize_title( $name );
690
691
		if ( ! $name ) {
692
			return new WP_Error( 'invalid_post', 'Invalid post', 400 );
693
		}
694
695
		$posts = get_posts( array( 'name' => $name ) );
696
697
		if ( ! $posts || ! isset( $posts[0]->ID ) || ! $posts[0]->ID ) {
698
			$page = get_page_by_path( $name );
699
700
			if ( ! $page ) {
701
				return new WP_Error( 'unknown_post', 'Unknown post', 404 );
702
			}
703
704
			$post_id = $page->ID;
705
		} else {
706
			$post_id = (int) $posts[0]->ID;
707
		}
708
709
		return $post_id;
710
	}
711
712
}
713

sal/class.json-api-site-base.php 1 location

@@ 255-277 (lines=23) @@
252
	 *
253
	 * @return int|object Post ID on success, WP_Error object on failure
254
	 **/
255
	public function get_post_by_name( $name, $context ) {
256
		$name = sanitize_title( $name );
257
258
		if ( ! $name ) {
259
			return new WP_Error( 'invalid_post', 'Invalid post', 400 );
260
		}
261
262
		$posts = get_posts( array( 'name' => $name, 'numberposts' => 1 ) );
263
264
		if ( ! $posts || ! isset( $posts[0]->ID ) || ! $posts[0]->ID ) {
265
			$page = get_page_by_path( $name );
266
267
			if ( ! $page ) {
268
				return new WP_Error( 'unknown_post', 'Unknown post', 404 );
269
			}
270
271
			$post_id = $page->ID;
272
		} else {
273
			$post_id = (int) $posts[0]->ID;
274
		}
275
276
		return $this->get_post_by_id( $post_id, $context );
277
	}
278
279
	function user_can_manage() {
280
		current_user_can( 'manage_options' );