Code Duplication    Length = 23-23 lines in 2 locations

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

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

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' );