Completed
Push — develop ( 9892da...c6a3a9 )
by
unknown
28s queued 10s
created

Filler_Posts_Util   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 99
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A extract_post_ids() 0 8 1
A get_filler_posts() 0 29 3
A get_filler_response() 0 21 3
1
<?php
2
3
namespace Wordlift\Widgets\Navigator\Filler_Posts;
4
/**
5
 * @since 3.27.8
6
 * @author Naveen Muthusamy <[email protected]>
7
 */
8
class Filler_Posts_Util {
9
10
	/**
11
	 * @var array<Filler_Posts>
12
	 */
13
	private $sources = array();
14
15
	public function __construct( $post_id ) {
16
17
		$post_type = get_post_type( $post_id );
18
19
		if ( $post_type === 'post' ) {
20
			$this->sources = array(
21
				new Same_Category_Filler_Posts( $post_id ),
22
				new Same_Post_Type_Filler_Posts( $post_id ),
23
			);
24
		} else {
25
			$this->sources = array(
26
				new Same_Post_Type_Filler_Posts( $post_id ),
27
			);
28
		}
29
	}
30
31
32
	/**
33
	 * @param $posts array<\WP_Post>
34
	 *
35
	 * @return array<int>
36
	 */
37
	private function extract_post_ids( $posts ) {
38
		return array_map( function ( $post ) {
39
			/**
40
			 * @var $post \WP_Post
41
			 */
42
			return $post->ID;
43
		}, $posts );
44
	}
45
46
	public function get_filler_posts( $filler_count, $post_ids_to_be_excluded ) {
47
48
		$filler_posts = array();
49
50
		foreach ( $this->sources as $source ) {
51
52
			if ( $filler_count <= 0 ) {
53
				break;
54
			}
55
			/**
56
			 * @var Filler_Posts $source
57
			 */
58
			$source->post_ids_to_be_excluded = $post_ids_to_be_excluded;
59
			$source->filler_count            = $filler_count;
60
61
			$posts    = $source->get_posts( $filler_count, $post_ids_to_be_excluded );
62
			$post_ids = $this->extract_post_ids( $posts );
63
64
			// Update the post ids, filler posts and filler count
65
			$post_ids_to_be_excluded = array_merge( $post_ids_to_be_excluded, $post_ids );
66
			$filler_count            = $filler_count - count( $posts );
67
			$filler_posts            = array_merge( $filler_posts, $posts );
68
69
70
		}
71
72
		return $filler_posts;
73
74
	}
75
76
	/**
77
	 * Called by wordlift navigator, converts all the posts to response format.
78
	 * @param $filler_count
79
	 * @param $post_ids_to_be_excluded
80
	 *
81
	 * @return array
82
	 */
83
	public function get_filler_response( $filler_count, $post_ids_to_be_excluded ) {
84
		$filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded );
85
		// Add thumbnail and permalink to filler posts
86
		$filler_response = array();
87
		foreach ( $filler_posts as $post_obj ) {
88
			$thumbnail         = get_the_post_thumbnail_url( $post_obj, 'medium' );
89
			$filler_response[] = array(
90
				'post'   => array(
91
					'id'        => $post_obj->ID,
92
					'permalink' => get_permalink( $post_obj->ID ),
93
					'thumbnail' => ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH,
94
					'title'     => get_the_title( $post_obj->ID )
95
				),
96
				'entity' => array(
97
					'id' => 0
98
				)
99
			);
100
		}
101
102
		return $filler_response;
103
	}
104
105
106
}
107