|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Database; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Database\SqlQueries; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Modules\Rating; |
|
8
|
|
|
|
|
9
|
|
|
class CountsManager |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @return array |
|
13
|
|
|
*/ |
|
14
|
|
|
public function build( array $args = [], $limit = 500 ) |
|
15
|
|
|
{ |
|
16
|
|
|
$counts = []; |
|
17
|
|
|
$greaterThanId = 0; |
|
18
|
|
|
while( $reviews = glsr( SqlQueries::class )->getReviewRatings( $args, $greaterThanId, $limit )) { |
|
19
|
|
|
$types = array_keys( array_flip( array_column( $reviews, 'type' ))); |
|
20
|
|
|
foreach( $types as $type ) { |
|
21
|
|
|
if( isset( $counts[$type] ))continue; |
|
22
|
|
|
$counts[$type] = array_fill_keys( range( 0, Rating::MAX_RATING ), 0 ); |
|
23
|
|
|
} |
|
24
|
|
|
foreach( $reviews as $review ) { |
|
25
|
|
|
$counts[$review->type][$review->rating]++; |
|
26
|
|
|
} |
|
27
|
|
|
$greaterThanId = end( $reviews )->ID; |
|
28
|
|
|
} |
|
29
|
|
|
return $counts; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return array |
|
34
|
|
|
*/ |
|
35
|
|
|
public function buildFromIds( array $postIds, $limit = 100 ) |
|
36
|
|
|
{ |
|
37
|
|
|
$counts = array_fill_keys( range( 0, Rating::MAX_RATING ), 0 ); |
|
38
|
|
|
$greaterThanId = 0; |
|
39
|
|
|
while( $reviews = glsr( SqlQueries::class )->getReviewRatingsFromIds( $postIds, $greaterThanId, $limit )) { |
|
40
|
|
|
foreach( $reviews as $review ) { |
|
41
|
|
|
$counts[$review->rating]++; |
|
42
|
|
|
} |
|
43
|
|
|
$greaterThanId = end( $reviews )->ID; |
|
44
|
|
|
} |
|
45
|
|
|
return $counts; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
public function flatten( array $reviewCounts ) |
|
52
|
|
|
{ |
|
53
|
|
|
$counts = []; |
|
54
|
|
|
array_walk_recursive( $reviewCounts, function( $num, $index ) use( &$counts ) { |
|
55
|
|
|
$counts[$index] = isset($counts[$index]) |
|
56
|
|
|
? $num + $counts[$index] |
|
57
|
|
|
: $num; |
|
58
|
|
|
}); |
|
59
|
|
|
return $counts; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function get( array $args = [] ) |
|
66
|
|
|
{ |
|
67
|
1 |
|
$args = wp_parse_args( $args, [ |
|
68
|
1 |
|
'max' => Rating::MAX_RATING, |
|
69
|
1 |
|
'min' => Rating::MIN_RATING, |
|
70
|
1 |
|
'types' => 'local', |
|
71
|
|
|
]); |
|
72
|
1 |
|
$counts = array_intersect_key( |
|
73
|
1 |
|
glsr( OptionManager::class )->get( 'counts', [] ), |
|
74
|
1 |
|
array_flip( array_intersect( array_keys( glsr()->reviewTypes ), (array)$args['types'] )) |
|
75
|
|
|
); |
|
76
|
1 |
|
$counts = $this->normalize( $counts ); |
|
77
|
1 |
|
array_walk( $counts, function( &$ratings ) use( $args ) { |
|
78
|
|
|
$ratings[0] = 0; |
|
79
|
|
|
foreach( $ratings as $index => &$num ) { |
|
80
|
|
|
if( $index >= intval( $args['min'] ) && $index <= intval( $args['max'] ))continue; |
|
81
|
|
|
$num = 0; |
|
82
|
|
|
} |
|
83
|
1 |
|
}); |
|
84
|
1 |
|
return $counts; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getFlattened( array $args = [] ) |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->flatten( $this->get( $args )); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
1 |
|
protected function normalize( array $reviewCounts ) |
|
99
|
|
|
{ |
|
100
|
1 |
|
foreach( $reviewCounts as $type => &$counts ) { |
|
101
|
|
|
foreach( range( 0, Rating::MAX_RATING ) as $rating ) { |
|
102
|
|
|
if( isset( $counts[$rating] ))continue; |
|
103
|
|
|
$counts[$rating] = 0; |
|
104
|
|
|
} |
|
105
|
|
|
ksort( $counts ); |
|
106
|
|
|
} |
|
107
|
1 |
|
return $reviewCounts; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|