Passed
Push — master ( 6f6d9e...e32a3c )
by Paul
04:05
created

Database::setReviewTerms()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Database\Cache;
7
use GeminiLabs\SiteReviews\Database\OptionManager;
8
use GeminiLabs\SiteReviews\Database\QueryBuilder;
9
use GeminiLabs\SiteReviews\Database\SqlQueries;
10
use GeminiLabs\SiteReviews\Modules\Rating;
11
use WP_Post;
12
use WP_Query;
13
14
class Database
15
{
16
	/**
17
	 * @return array
18
	 */
19
	public function buildReviewCounts()
20
	{
21
		$counts = [];
22
		$greaterThanId = 0;
23
		while( $reviews = glsr( SqlQueries::class )->getReviewRatings( $greaterThanId )) {
24
			$types = array_keys( array_flip( array_column( $reviews, 'type' )));
25
			foreach( $types as $type ) {
26
				if( isset( $counts[$type] ))continue;
27
				$counts[$type] = array_fill_keys( range( 0, Rating::MAX_RATING ), 0 );
28
			}
29
			foreach( $reviews as $review ) {
30
				$counts[$review->type][$review->rating]++;
31
			}
32
			$greaterThanId = end( $reviews )->ID;
33
		}
34
		return $counts;
35
	}
36
37
	/**
38
	 * @return array
39
	 */
40
	public function buildReviewCountsFromIds( array $postIds )
41
	{
42
		$counts = array_fill_keys( range( 0, Rating::MAX_RATING ), 0 );
43
		$greaterThanId = 0;
44
		while( $reviews = glsr( SqlQueries::class )->getReviewRatingsFromIds( $postIds, $greaterThanId )) {
45
			foreach( $reviews as $review ) {
46
				$counts[$review->rating]++;
47
			}
48
			$greaterThanId = end( $reviews )->ID;
49
		}
50
		return $counts;
51
	}
52
53
	/**
54
	 * @param int $postId
55
	 * @param string $assignedTo
56
	 * @return void|WP_Post
57
	 */
58
	public function getAssignedToPost( $postId, $assignedTo = '' )
59
	{
60
		if( empty( $assignedTo )) {
61
			$assignedTo = get_post_meta( $postId, 'assigned_to', true );
62
		}
63
		if( empty( $assignedTo ))return;
64
		$assignedPost = get_post( $assignedTo );
65
		if( $assignedPost instanceof WP_Post && $assignedPost->ID != $postId ) {
66
			return $assignedPost;
67
		}
68
	}
69
70
	/**
71
	 * @param string $metaKey
72
	 * @param string $metaValue
73
	 * @return array|int
74
	 */
75
	public function getReviewCount( $metaKey = '', $metaValue = '' )
76
	{
77
		if( !$metaKey ) {
78
			return (array)wp_count_posts( Application::POST_TYPE );
79
		}
80
		$counts = glsr( Cache::class )->getReviewCountsFor( $metaKey );
81
		if( !$metaValue ) {
82
			return $counts;
83
		}
84
		return isset( $counts[$metaValue] )
85
			? $counts[$metaValue]
86
			: 0;
87
	}
88
89
	/**
90
	 * @return array
91
	 */
92
	public function getReviewCounts( array $args = [] )
93
	{
94
		$args = wp_parse_args( $args, [
95
			'max' => Rating::MAX_RATING,
96
			'min' => Rating::MIN_RATING,
97
			'types' => 'local',
98
		]);
99
		$counts = array_intersect_key(
100
			glsr( OptionManager::class )->get( 'counts', [] ),
101
			array_flip( array_intersect( glsr()->reviewTypes, (array)$args['types'] ))
102
		);
103
		array_walk( $counts, function( &$ratings ) use( $args ) {
104
			$ratings[0] = 0;
105
			foreach( $ratings as $index => &$num ) {
106
				if( $index >= intval( $args['min'] ) && $index <= intval( $args['max'] ))continue;
107
				$num = 0;
108
			}
109
		});
110
		return $counts;
111
	}
112
113
	/**
114
	 * @param string $metaReviewType
115
	 * @return array
116
	 */
117
	public function getReviewIdsByType( $metaReviewType )
118
	{
119
		return glsr( SqlQueries::class )->getReviewIdsByType( $metaReviewType );
120
	}
121
122
	/**
123
	 * @param string $key
124
	 * @param string $status
125
	 * @return array
126
	 */
127
	public function getReviewsMeta( $key, $status = 'publish' )
128
	{
129
		if( $status == 'all' || empty( $status )) {
130
			$status = get_post_stati( ['exclude_from_search' => false] );
131
		}
132
		return glsr( SqlQueries::class )->getReviewsMeta( $key, $status );
133
	}
134
135
	/**
136
	 * @return array
137
	 */
138 1
	public function getTerms( array $args = [] )
139
	{
140 1
		$args = wp_parse_args( $args, [
141 1
			'fields' => 'id=>name',
142
			'hide_empty' => false,
143
			'taxonomy' => Application::TAXONOMY,
144
		]);
145 1
		unset( $args['count'] ); // we don't want a term count
146 1
		$terms = get_terms( $args );
147 1
		if( is_wp_error( $terms )) {
148
			glsr_log()->error( $terms->get_error_message() );
149
			return [];
150
		}
151 1
		return $terms;
152
	}
153
154
	/**
155
	 * @param string $searchTerm
156
	 * @return void|string
157
	 */
158
	public function searchPosts( $searchTerm )
159
	{
160
		$args = [
161
			'post_status' => 'publish',
162
			'post_type' => 'any',
163
		];
164
		if( is_numeric( $searchTerm )) {
165
			$args['post__in'] = [$searchTerm];
166
		}
167
		else {
168
			$args['orderby'] = 'relevance';
169
			$args['posts_per_page'] = 10;
170
			$args['s'] = $searchTerm;
171
		}
172
		$queryBuilder = glsr( QueryBuilder::class );
173
		add_filter( 'posts_search', [$queryBuilder, 'filterSearchByTitle'], 500, 2 );
174
		$search = new WP_Query( $args );
175
		remove_filter( 'posts_search', [$queryBuilder, 'filterSearchByTitle'], 500 );
176
		if( !$search->have_posts() )return;
177
		$results = '';
178
		while( $search->have_posts() ) {
179
			$search->the_post();
180
			ob_start();
181
			glsr()->render( 'partials/editor/search-result', [
182
				'ID' => get_the_ID(),
183
				'permalink' => esc_url( (string) get_permalink() ),
184
				'title' => esc_attr( get_the_title() ),
185
			]);
186
			$results .= ob_get_clean();
187
		}
188
		wp_reset_postdata();
189
		return $results;
190
	}
191
}
192