Passed
Push — hotfix/fix_counts_manager ( 20120a )
by Paul
06:22 queued 01:24
created
plugin/Database/CountsManager.php 1 patch
Spacing   +35 added lines, -35 removed lines patch added patch discarded remove patch
@@ -64,7 +64,7 @@  discard block
 block discarded – undo
64 64
 			$this->getCounts(),
65 65
 			$review->review_type,
66 66
 			$review->rating
67
-		));
67
+		) );
68 68
 	}
69 69
 
70 70
 	/**
@@ -72,7 +72,7 @@  discard block
 block discarded – undo
72 72
 	 */
73 73
 	public function decreasePostCounts( Review $review )
74 74
 	{
75
-		if( empty( $counts = $this->getPostCounts( $review->assigned_to )))return;
75
+		if( empty($counts = $this->getPostCounts( $review->assigned_to )) )return;
76 76
 		$counts = $this->decreaseRating( $counts, $review->review_type, $review->rating );
77 77
 		$this->setPostCounts( $review->assigned_to, $counts );
78 78
 	}
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
 	public function decreaseTermCounts( Review $review )
84 84
 	{
85 85
 		foreach( $review->term_ids as $termId ) {
86
-			if( empty( $counts = $this->getTermCounts( $termId )))continue;
86
+			if( empty($counts = $this->getTermCounts( $termId )) )continue;
87 87
 			$counts = $this->decreaseRating( $counts, $review->review_type, $review->rating );
88 88
 			$this->setTermCounts( $termId, $counts );
89 89
 		}
@@ -95,15 +95,15 @@  discard block
 block discarded – undo
95 95
 	public function flatten( array $reviewCounts, array $args = [] )
96 96
 	{
97 97
 		$counts = [];
98
-		array_walk_recursive( $reviewCounts, function( $num, $index ) use( &$counts ) {
99
-			$counts[$index] = $num + intval( glsr_get( $counts, $index, 0 ));
98
+		array_walk_recursive( $reviewCounts, function( $num, $index ) use(&$counts) {
99
+			$counts[$index] = $num + intval( glsr_get( $counts, $index, 0 ) );
100 100
 		});
101 101
 		$args = wp_parse_args( $args, [
102 102
 			'max' => Rating::MAX_RATING,
103 103
 			'min' => Rating::MIN_RATING,
104
-		]);
104
+		] );
105 105
 		foreach( $counts as $index => &$num ) {
106
-			if( $index >= intval( $args['min'] ) && $index <= intval( $args['max'] ))continue;
106
+			if( $index >= intval( $args['min'] ) && $index <= intval( $args['max'] ) )continue;
107 107
 			$num = 0;
108 108
 		}
109 109
 		return $counts;
@@ -116,7 +116,7 @@  discard block
 block discarded – undo
116 116
 	{
117 117
 		$args = $this->normalizeArgs( $args );
118 118
 
119
-		if( !empty( $args['post_ids'] ) && !empty( $args['term_ids'] )) {
119
+		if( !empty($args['post_ids']) && !empty($args['term_ids']) ) {
120 120
 			$counts = [$this->build( $args )];
121 121
 		}
122 122
 		else {
@@ -130,13 +130,13 @@  discard block
 block discarded – undo
130 130
 				// @todo if multiple ids are provided, a query must be used!
131 131
 				$counts[] = $this->getTermCounts( $termId );
132 132
 			}
133
-			if( empty( $counts )) {
133
+			if( empty($counts) ) {
134 134
 				$counts[] = $this->getCounts();
135 135
 			}
136 136
 		}
137 137
 		return in_array( $args['type'], ['', 'all'] )
138 138
 			? $this->normalize( [$this->flatten( $counts )] )
139
-			: $this->normalize( glsr_array_column( $counts, $args['type'] ));
139
+			: $this->normalize( glsr_array_column( $counts, $args['type'] ) );
140 140
 	}
141 141
 
142 142
 	/**
@@ -145,7 +145,7 @@  discard block
 block discarded – undo
145 145
 	public function getCounts()
146 146
 	{
147 147
 		$counts = glsr( OptionManager::class )->get( 'counts', [] );
148
-		if( !is_array( $counts )) {
148
+		if( !is_array( $counts ) ) {
149 149
 			glsr_log()->error( '$counts is not an array' )->debug( $counts );
150 150
 			return [];
151 151
 		}
@@ -158,7 +158,7 @@  discard block
 block discarded – undo
158 158
 	 */
159 159
 	public function getPostCounts( $postId )
160 160
 	{
161
-		return array_filter( (array)get_post_meta( $postId, static::META_COUNT, true ));
161
+		return array_filter( (array)get_post_meta( $postId, static::META_COUNT, true ) );
162 162
 	}
163 163
 
164 164
 	/**
@@ -167,7 +167,7 @@  discard block
 block discarded – undo
167 167
 	 */
168 168
 	public function getTermCounts( $termId )
169 169
 	{
170
-		return array_filter( (array)get_term_meta( $termId, static::META_COUNT, true ));
170
+		return array_filter( (array)get_term_meta( $termId, static::META_COUNT, true ) );
171 171
 	}
172 172
 
173 173
 	/**
@@ -185,10 +185,10 @@  discard block
 block discarded – undo
185 185
 	 */
186 186
 	public function increaseCounts( Review $review )
187 187
 	{
188
-		if( empty( $counts = $this->getCounts() )) {
188
+		if( empty($counts = $this->getCounts()) ) {
189 189
 			$counts = $this->buildCounts();
190 190
 		}
191
-		$this->setCounts( $this->increaseRating( $counts, $review->review_type, $review->rating ));
191
+		$this->setCounts( $this->increaseRating( $counts, $review->review_type, $review->rating ) );
192 192
 	}
193 193
 
194 194
 	/**
@@ -196,9 +196,9 @@  discard block
 block discarded – undo
196 196
 	 */
197 197
 	public function increasePostCounts( Review $review )
198 198
 	{
199
-		if( !( get_post( $review->assigned_to ) instanceof WP_Post ))return;
199
+		if( !(get_post( $review->assigned_to ) instanceof WP_Post) )return;
200 200
 		$counts = $this->getPostCounts( $review->assigned_to );
201
-		$counts = empty( $counts )
201
+		$counts = empty($counts)
202 202
 			? $this->buildPostCounts( $review->assigned_to )
203 203
 			: $this->increaseRating( $counts, $review->review_type, $review->rating );
204 204
 		$this->setPostCounts( $review->assigned_to, $counts );
@@ -209,10 +209,10 @@  discard block
 block discarded – undo
209 209
 	 */
210 210
 	public function increaseTermCounts( Review $review )
211 211
 	{
212
-		$terms = glsr( ReviewManager::class )->normalizeTerms( implode( ',', $review->term_ids ));
212
+		$terms = glsr( ReviewManager::class )->normalizeTerms( implode( ',', $review->term_ids ) );
213 213
 		foreach( $terms as $term ) {
214 214
 			$counts = $this->getTermCounts( $term['term_id'] );
215
-			$counts = empty( $counts )
215
+			$counts = empty($counts)
216 216
 				? $this->buildTermCounts( $term['term_taxonomy_id'] )
217 217
 				: $this->increaseRating( $counts, $review->review_type, $review->rating );
218 218
 			$this->setTermCounts( $term['term_id'], $counts );
@@ -235,8 +235,8 @@  discard block
 block discarded – undo
235 235
 	{
236 236
 		$ratingCounts = $this->flatten( $reviewCounts );
237 237
 		update_post_meta( $postId, static::META_COUNT, $reviewCounts );
238
-		update_post_meta( $postId, static::META_AVERAGE, glsr( Rating::class )->getAverage( $ratingCounts ));
239
-		update_post_meta( $postId, static::META_RANKING, glsr( Rating::class )->getRanking( $ratingCounts ));
238
+		update_post_meta( $postId, static::META_AVERAGE, glsr( Rating::class )->getAverage( $ratingCounts ) );
239
+		update_post_meta( $postId, static::META_RANKING, glsr( Rating::class )->getRanking( $ratingCounts ) );
240 240
 	}
241 241
 
242 242
 	/**
@@ -246,11 +246,11 @@  discard block
 block discarded – undo
246 246
 	public function setTermCounts( $termId, array $reviewCounts )
247 247
 	{
248 248
 		$term = get_term( $termId, Application::TAXONOMY );
249
-		if( !isset( $term->term_id ))return;
249
+		if( !isset($term->term_id) )return;
250 250
 		$ratingCounts = $this->flatten( $reviewCounts );
251 251
 		update_term_meta( $termId, static::META_COUNT, $reviewCounts );
252
-		update_term_meta( $termId, static::META_AVERAGE, glsr( Rating::class )->getAverage( $ratingCounts ));
253
-		update_term_meta( $termId, static::META_RANKING, glsr( Rating::class )->getRanking( $ratingCounts ));
252
+		update_term_meta( $termId, static::META_AVERAGE, glsr( Rating::class )->getAverage( $ratingCounts ) );
253
+		update_term_meta( $termId, static::META_RANKING, glsr( Rating::class )->getRanking( $ratingCounts ) );
254 254
 	}
255 255
 
256 256
 	/**
@@ -262,12 +262,12 @@  discard block
 block discarded – undo
262 262
 	{
263 263
 		$counts = [];
264 264
 		$lastPostId = 0;
265
-		while( $reviews = $this->queryReviews( $args, $lastPostId )) {
266
-			$types = array_keys( array_flip( glsr_array_column( $reviews, 'type' )));
267
-			$types = array_unique( array_merge( ['local'], $types ));
265
+		while( $reviews = $this->queryReviews( $args, $lastPostId ) ) {
266
+			$types = array_keys( array_flip( glsr_array_column( $reviews, 'type' ) ) );
267
+			$types = array_unique( array_merge( ['local'], $types ) );
268 268
 			foreach( $types as $type ) {
269 269
 				$type = $this->normalizeType( $type );
270
-				if( isset( $counts[$type] ))continue;
270
+				if( isset($counts[$type]) )continue;
271 271
 				$counts[$type] = array_fill_keys( range( 0, Rating::MAX_RATING ), 0 );
272 272
 			}
273 273
 			foreach( $reviews as $review ) {
@@ -286,7 +286,7 @@  discard block
 block discarded – undo
286 286
 	 */
287 287
 	protected function decreaseRating( array $reviewCounts, $type, $rating )
288 288
 	{
289
-		if( isset( $reviewCounts[$type][$rating] )) {
289
+		if( isset($reviewCounts[$type][$rating]) ) {
290 290
 			$reviewCounts[$type][$rating] = max( 0, $reviewCounts[$type][$rating] - 1 );
291 291
 		}
292 292
 		return $reviewCounts;
@@ -299,10 +299,10 @@  discard block
 block discarded – undo
299 299
 	 */
300 300
 	protected function increaseRating( array $reviewCounts, $type, $rating )
301 301
 	{
302
-		if( !array_key_exists( $type, glsr()->reviewTypes )) {
302
+		if( !array_key_exists( $type, glsr()->reviewTypes ) ) {
303 303
 			return $reviewCounts;
304 304
 		}
305
-		if( !array_key_exists( $type, $reviewCounts )) {
305
+		if( !array_key_exists( $type, $reviewCounts ) ) {
306 306
 			$reviewCounts[$type] = [];
307 307
 		}
308 308
 		$reviewCounts = $this->normalize( $reviewCounts );
@@ -315,12 +315,12 @@  discard block
 block discarded – undo
315 315
 	 */
316 316
 	protected function normalize( array $reviewCounts )
317 317
 	{
318
-		if( empty( $reviewCounts )) {
318
+		if( empty($reviewCounts) ) {
319 319
 			$reviewCounts = [[]];
320 320
 		}
321 321
 		foreach( $reviewCounts as &$counts ) {
322 322
 			foreach( range( 0, Rating::MAX_RATING ) as $index ) {
323
-				if( isset( $counts[$index] ))continue;
323
+				if( isset($counts[$index]) )continue;
324 324
 				$counts[$index] = 0;
325 325
 			}
326 326
 			ksort( $counts );
@@ -337,7 +337,7 @@  discard block
 block discarded – undo
337 337
 			'post_ids' => [],
338 338
 			'term_ids' => [],
339 339
 			'type' => 'local',
340
-		]);
340
+		] );
341 341
 		$args['post_ids'] = glsr( Polylang::class )->getPostIds( $args['post_ids'] );
342 342
 		$args['type'] = $this->normalizeType( $args['type'] );
343 343
 		return $args;
@@ -349,7 +349,7 @@  discard block
 block discarded – undo
349 349
 	 */
350 350
 	protected function normalizeType( $type )
351 351
 	{
352
-		return empty( $type ) || !is_string( $type )
352
+		return empty($type) || !is_string( $type )
353 353
 			? 'local'
354 354
 			: $type;
355 355
 	}
Please login to merge, or discard this patch.
plugin/Database/SqlQueries.php 1 patch
Spacing   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -21,11 +21,11 @@  discard block
 block discarded – undo
21 21
 	 */
22 22
 	public function deleteAllSessions( $sessionCookiePrefix )
23 23
 	{
24
-		return $this->db->query("
24
+		return $this->db->query( "
25 25
 			DELETE
26 26
 			FROM {$this->db->options}
27 27
 			WHERE option_name LIKE '{$sessionCookiePrefix}_%'
28
-		");
28
+		" );
29 29
 	}
30 30
 
31 31
 	/**
@@ -34,11 +34,11 @@  discard block
 block discarded – undo
34 34
 	 */
35 35
 	public function deleteExpiredSessions( $expiredSessions )
36 36
 	{
37
-		return $this->db->query("
37
+		return $this->db->query( "
38 38
 			DELETE
39 39
 			FROM {$this->db->options}
40 40
 			WHERE option_name IN ('{$expiredSessions}')
41
-		");
41
+		" );
42 42
 	}
43 43
 
44 44
 	/**
@@ -48,13 +48,13 @@  discard block
 block discarded – undo
48 48
 	 */
49 49
 	public function getExpiredSessions( $sessionCookiePrefix, $limit )
50 50
 	{
51
-		return $this->db->get_results("
51
+		return $this->db->get_results( "
52 52
 			SELECT option_name AS name, option_value AS expiration
53 53
 			FROM {$this->db->options}
54 54
 			WHERE option_name LIKE '{$sessionCookiePrefix}_expires_%'
55 55
 			ORDER BY option_value ASC
56 56
 			LIMIT 0, {$limit}
57
-		");
57
+		" );
58 58
 	}
59 59
 
60 60
 	/**
@@ -63,14 +63,14 @@  discard block
 block discarded – undo
63 63
 	 */
64 64
 	public function getPostIdFromReviewId( $metaReviewId )
65 65
 	{
66
-		$postId = $this->db->get_var("
66
+		$postId = $this->db->get_var( "
67 67
 			SELECT p.ID
68 68
 			FROM {$this->db->posts} AS p
69 69
 			INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id
70 70
 			WHERE p.post_type = '{Application::POST_TYPE}'
71 71
 			AND m.meta_key = 'review_id'
72 72
 			AND m.meta_value = '{$metaReviewId}'
73
-		");
73
+		" );
74 74
 		return intval( $postId );
75 75
 	}
76 76
 
@@ -81,7 +81,7 @@  discard block
 block discarded – undo
81 81
 	 */
82 82
 	public function getReviewCounts( array $args, $lastPostId = 0, $limit = 500 )
83 83
 	{
84
-		return $this->db->get_results("
84
+		return $this->db->get_results( "
85 85
 			SELECT p.ID, m1.meta_value AS rating, m2.meta_value AS type
86 86
 			FROM {$this->db->posts} AS p
87 87
 			INNER JOIN {$this->db->postmeta} AS m1 ON p.ID = m1.post_id
@@ -104,14 +104,14 @@  discard block
 block discarded – undo
104 104
 	 */
105 105
 	public function getReviewCountsFor( $metaKey )
106 106
 	{
107
-		return (array) $this->db->get_results("
107
+		return (array)$this->db->get_results( "
108 108
 			SELECT m.meta_value AS name, COUNT(*) num_posts
109 109
 			FROM {$this->db->posts} AS p
110 110
 			INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id
111 111
 			WHERE p.post_type = '{Application::POST_TYPE}'
112 112
 			AND m.meta_key = '{$metaKey}'
113 113
 			GROUP BY name
114
-		");
114
+		" );
115 115
 	}
116 116
 
117 117
 	/**
@@ -120,7 +120,7 @@  discard block
 block discarded – undo
120 120
 	 */
121 121
 	public function getReviewIdsByType( $reviewType )
122 122
 	{
123
-		$query = $this->db->get_col("
123
+		$query = $this->db->get_col( "
124 124
 			SELECT m1.meta_value AS review_id
125 125
 			FROM {$this->db->posts} AS p
126 126
 			INNER JOIN {$this->db->postmeta} AS m1 ON p.ID = m1.post_id
@@ -129,8 +129,8 @@  discard block
 block discarded – undo
129 129
 			AND m1.meta_key = 'review_id'
130 130
 			AND m2.meta_key = 'review_type'
131 131
 			AND m2.meta_value = '{$reviewType}'
132
-		");
133
-		return array_keys( array_flip( $query ));
132
+		" );
133
+		return array_keys( array_flip( $query ) );
134 134
 	}
135 135
 
136 136
 	/**
@@ -141,9 +141,9 @@  discard block
 block discarded – undo
141 141
 	public function getReviewRatingsFromIds( array $postIds, $greaterThanId = 0, $limit = 100 )
142 142
 	{
143 143
 		sort( $postIds );
144
-		$postIds = array_slice( $postIds, intval( array_search( $greaterThanId, $postIds )), $limit );
144
+		$postIds = array_slice( $postIds, intval( array_search( $greaterThanId, $postIds ) ), $limit );
145 145
 		$postIds = implode( ',', $postIds );
146
-		return $this->db->get_results("
146
+		return $this->db->get_results( "
147 147
 			SELECT p.ID, m.meta_value AS rating
148 148
 			FROM {$this->db->posts} AS p
149 149
 			INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id
@@ -167,7 +167,7 @@  discard block
 block discarded – undo
167 167
 		$queryBuilder = glsr( QueryBuilder::class );
168 168
 		$key = $queryBuilder->buildSqlOr( $key, "m.meta_key = '%s'" );
169 169
 		$status = $queryBuilder->buildSqlOr( $status, "p.post_status = '%s'" );
170
-		return $this->db->get_col("
170
+		return $this->db->get_col( "
171 171
 			SELECT DISTINCT m.meta_value
172 172
 			FROM {$this->db->postmeta} m
173 173
 			LEFT JOIN {$this->db->posts} p ON p.ID = m.post_id
@@ -175,7 +175,7 @@  discard block
 block discarded – undo
175 175
 			AND ({$key})
176 176
 			AND ({$status})
177 177
 			ORDER BY m.meta_value
178
-		");
178
+		" );
179 179
 	}
180 180
 
181 181
 	/**
@@ -184,11 +184,11 @@  discard block
 block discarded – undo
184 184
 	 */
185 185
 	protected function getAndForCounts( array $args, $and = '' )
186 186
 	{
187
-		if( !empty( $args['post_id'] )) {
188
-			$and.= "AND m3.meta_key = 'assigned_to' AND m3.meta_value = {$args['post_id']}";
187
+		if( !empty($args['post_id']) ) {
188
+			$and .= "AND m3.meta_key = 'assigned_to' AND m3.meta_value = {$args['post_id']}";
189 189
 		}
190
-		if( !empty( $args['term_taxonomy_id'] )) {
191
-			$and.= "AND tr.term_taxonomy_id = {$args['term_taxonomy_id']}";
190
+		if( !empty($args['term_taxonomy_id']) ) {
191
+			$and .= "AND tr.term_taxonomy_id = {$args['term_taxonomy_id']}";
192 192
 		}
193 193
 		return $and;
194 194
 	}
@@ -199,11 +199,11 @@  discard block
 block discarded – undo
199 199
 	 */
200 200
 	protected function getInnerJoinForCounts( array $args, $innerJoin = '' )
201 201
 	{
202
-		if( !empty( $args['post_id'] )) {
203
-			$innerJoin.= "INNER JOIN {$this->db->postmeta} AS m3 ON p.ID = m3.post_id";
202
+		if( !empty($args['post_id']) ) {
203
+			$innerJoin .= "INNER JOIN {$this->db->postmeta} AS m3 ON p.ID = m3.post_id";
204 204
 		}
205
-		if( !empty( $args['term_taxonomy_id'] )) {
206
-			$innerJoin.= "INNER JOIN {$this->db->term_relationships} AS tr ON p.ID = tr.object_id";
205
+		if( !empty($args['term_taxonomy_id']) ) {
206
+			$innerJoin .= "INNER JOIN {$this->db->term_relationships} AS tr ON p.ID = tr.object_id";
207 207
 		}
208 208
 		return $innerJoin;
209 209
 	}
Please login to merge, or discard this patch.