Code Duplication    Length = 55-56 lines in 2 locations

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

@@ 361-415 (lines=55) @@
358
359
		$return         = array();
360
		$excluded_count = 0;
361
		foreach ( array_keys( $this->response_format ) as $key ) {
362
			switch ( $key ) {
363
				case 'found':
364
					$return[ $key ] = (int) $wp_query->found_posts;
365
					break;
366
				case 'posts':
367
					$posts = array();
368
					foreach ( $wp_query->posts as $post_ID ) {
369
						$the_post = $this->get_post_by( 'ID', $post_ID, $args['context'] );
370
						if ( $the_post && ! is_wp_error( $the_post ) ) {
371
							$posts[] = $the_post;
372
						} else {
373
							$excluded_count++;
374
						}
375
					}
376
377
					if ( $posts ) {
378
						/** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
379
						do_action( 'wpcom_json_api_objects', 'posts', count( $posts ) );
380
					}
381
382
					$return[ $key ] = $posts;
383
					break;
384
385
				case 'meta':
386
					if ( ! is_array( $args['type'] ) ) {
387
						$return[ $key ] = (object) array(
388
							'links' => (object) array(
389
								'counts' => (string) $this->links->get_site_link( $blog_id, 'post-counts/' . $args['type'] ),
390
							),
391
						);
392
					}
393
394
					if ( $is_eligible_for_page_handle && $return['posts'] ) {
395
						$last_post = end( $return['posts'] );
396
						reset( $return['posts'] );
397
						if ( ( $return['found'] > count( $return['posts'] ) ) && $last_post ) {
398
							if ( ! isset( $return[ $key ] ) ) {
399
								$return[ $key ] = (object) array();
400
							}
401
							if ( isset( $last_post['ID'] ) ) {
402
								$return[ $key ]->next_page = $this->build_page_handle( $last_post, $query );
403
							}
404
						}
405
					}
406
407
					if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
408
						if ( ! isset( $return[ $key ] ) ) {
409
							$return[ $key ] = new stdClass();
410
						}
411
						$return[ $key ]->wpcom = true;
412
					}
413
414
					break;
415
			}
416
		}
417
418
		$return['found'] -= $excluded_count;

json-endpoints/class.wpcom-json-api-list-posts-v1-2-endpoint.php 1 location

@@ 355-410 (lines=56) @@
352
353
		$return         = array();
354
		$excluded_count = 0;
355
		foreach ( array_keys( $this->response_format ) as $key ) {
356
			switch ( $key ) {
357
				case 'found':
358
					$return[ $key ] = (int) $wp_query->found_posts;
359
					break;
360
				case 'posts':
361
					$posts = array();
362
					foreach ( $wp_query->posts as $post_ID ) {
363
						$the_post = $this->get_post_by( 'ID', $post_ID, $args['context'] );
364
						if ( $the_post && ! is_wp_error( $the_post ) ) {
365
							$posts[] = $the_post;
366
						} else {
367
							$excluded_count++;
368
						}
369
					}
370
371
					if ( $posts ) {
372
						/** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
373
						do_action( 'wpcom_json_api_objects', 'posts', count( $posts ) );
374
					}
375
376
					$return[ $key ] = $posts;
377
					break;
378
379
				case 'meta':
380
					if ( ! is_array( $args['type'] ) ) {
381
						$return[ $key ] = (object) array(
382
							'links' => (object) array(
383
								'counts' => (string) $this->links->get_site_link( $blog_id, 'post-counts/' . $args['type'] ),
384
							),
385
						);
386
					}
387
388
					if ( $is_eligible_for_page_handle && $return['posts'] ) {
389
						$last_post = end( $return['posts'] );
390
						reset( $return['posts'] );
391
						if ( ( $return['found'] > count( $return['posts'] ) ) && $last_post ) {
392
							if ( ! isset( $return[ $key ] ) ) {
393
								$return[ $key ] = (object) array();
394
							}
395
396
							if ( isset( $last_post['ID'] ) ) {
397
								$return[ $key ]->next_page = $this->build_page_handle( $last_post, $query );
398
							}
399
						}
400
					}
401
402
					if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
403
						if ( ! isset( $return[ $key ] ) ) {
404
							$return[ $key ] = new stdClass();
405
						}
406
						$return[ $key ]->wpcom = true;
407
					}
408
409
					break;
410
			}
411
		}
412
413
		$return['found'] -= $excluded_count;