@@ -10,13 +10,13 @@ |
||
10 | 10 | { |
11 | 11 | public function run() |
12 | 12 | { |
13 | - $routines = (new ReflectionClass( __CLASS__ ))->getMethods( ReflectionMethod::IS_PROTECTED ); |
|
14 | - $routines = array_column( $routines, 'name' ); |
|
15 | - natsort( $routines ); |
|
16 | - array_walk( $routines, function( $routine ) { |
|
17 | - $version = str_replace( strtolower( __CLASS__ ).'_', '', $routine ); |
|
18 | - if( version_compare( glsr()->version, $version, '>=' ))return; |
|
19 | - call_user_func( [$this, $routine] ); |
|
13 | + $routines = (new ReflectionClass(__CLASS__))->getMethods(ReflectionMethod::IS_PROTECTED); |
|
14 | + $routines = array_column($routines, 'name'); |
|
15 | + natsort($routines); |
|
16 | + array_walk($routines, function($routine) { |
|
17 | + $version = str_replace(strtolower(__CLASS__).'_', '', $routine); |
|
18 | + if (version_compare(glsr()->version, $version, '>='))return; |
|
19 | + call_user_func([$this, $routine]); |
|
20 | 20 | }); |
21 | 21 | } |
22 | 22 |
@@ -38,10 +38,10 @@ discard block |
||
38 | 38 | /** |
39 | 39 | * @return array|string |
40 | 40 | */ |
41 | - public function validate( array $request ) |
|
41 | + public function validate(array $request) |
|
42 | 42 | { |
43 | 43 | $this->form_id = $this->request['form_id']; // @todo verify this exists |
44 | - $this->request = $this->validateRequest( $request ); |
|
44 | + $this->request = $this->validateRequest($request); |
|
45 | 45 | $this->validateCustom(); |
46 | 46 | $this->validateHoneyPot(); |
47 | 47 | $this->validateBlacklist(); |
@@ -52,31 +52,31 @@ discard block |
||
52 | 52 | /** |
53 | 53 | * @return array |
54 | 54 | */ |
55 | - public function validateRequest( array $request ) |
|
55 | + public function validateRequest(array $request) |
|
56 | 56 | { |
57 | - if( !$this->isRequestValid( $request )) { |
|
58 | - $this->error = __( 'Please fix the submission errors.', 'site-reviews' ); |
|
57 | + if (!$this->isRequestValid($request)) { |
|
58 | + $this->error = __('Please fix the submission errors.', 'site-reviews'); |
|
59 | 59 | return $request; |
60 | 60 | } |
61 | - if( empty( $request['title'] )) { |
|
62 | - $request['title'] = __( 'No Title', 'site-reviews' ); |
|
61 | + if (empty($request['title'])) { |
|
62 | + $request['title'] = __('No Title', 'site-reviews'); |
|
63 | 63 | } |
64 | - return array_merge( glsr( ValidateReviewDefaults::class )->defaults(), $request ); |
|
64 | + return array_merge(glsr(ValidateReviewDefaults::class)->defaults(), $request); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | /** |
68 | 68 | * @return array |
69 | 69 | */ |
70 | - protected function getValidationRules( array $request ) |
|
70 | + protected function getValidationRules(array $request) |
|
71 | 71 | { |
72 | 72 | $rules = array_intersect_key( |
73 | - apply_filters( 'site-reviews/validation/rules', static::VALIDATION_RULES ), |
|
74 | - array_flip( array_merge( ['rating','terms'], glsr_get_option( 'reviews-form.required', [] ))) |
|
73 | + apply_filters('site-reviews/validation/rules', static::VALIDATION_RULES), |
|
74 | + array_flip(array_merge(['rating', 'terms'], glsr_get_option('reviews-form.required', []))) |
|
75 | 75 | ); |
76 | - $excluded = isset( $request['excluded'] ) |
|
77 | - ? json_decode( $request['excluded'] ) |
|
76 | + $excluded = isset($request['excluded']) |
|
77 | + ? json_decode($request['excluded']) |
|
78 | 78 | : []; |
79 | - return array_diff_key( $rules, array_flip( $excluded )); |
|
79 | + return array_diff_key($rules, array_flip($excluded)); |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | /** |
@@ -84,17 +84,17 @@ discard block |
||
84 | 84 | */ |
85 | 85 | protected function isRecaptchaResponseValid() |
86 | 86 | { |
87 | - $integration = glsr_get_option( 'reviews-form.recaptcha.integration' ); |
|
88 | - if( !$integration ) { |
|
87 | + $integration = glsr_get_option('reviews-form.recaptcha.integration'); |
|
88 | + if (!$integration) { |
|
89 | 89 | return true; |
90 | 90 | } |
91 | - $recaptchaResponse = filter_input( INPUT_POST, 'g-recaptcha-response' ); |
|
92 | - if( empty( $recaptchaResponse ))return; //if response is empty we need to return null |
|
93 | - if( $integration == 'custom' ) { |
|
94 | - return $this->isRecaptchaValid( $recaptchaResponse ); |
|
91 | + $recaptchaResponse = filter_input(INPUT_POST, 'g-recaptcha-response'); |
|
92 | + if (empty($recaptchaResponse))return; //if response is empty we need to return null |
|
93 | + if ($integration == 'custom') { |
|
94 | + return $this->isRecaptchaValid($recaptchaResponse); |
|
95 | 95 | } |
96 | - if( $integration == 'invisible-recaptcha' ) { |
|
97 | - return boolval( apply_filters( 'google_invre_is_valid_request_filter', true )); |
|
96 | + if ($integration == 'invisible-recaptcha') { |
|
97 | + return boolval(apply_filters('google_invre_is_valid_request_filter', true)); |
|
98 | 98 | } |
99 | 99 | return false; |
100 | 100 | } |
@@ -102,16 +102,16 @@ discard block |
||
102 | 102 | /** |
103 | 103 | * @return bool |
104 | 104 | */ |
105 | - protected function isRecaptchaValid( $recaptchaResponse ) |
|
105 | + protected function isRecaptchaValid($recaptchaResponse) |
|
106 | 106 | { |
107 | 107 | $endpoint = add_query_arg([ |
108 | - 'remoteip' => glsr( Helper::class )->getIpAddress(), |
|
108 | + 'remoteip' => glsr(Helper::class)->getIpAddress(), |
|
109 | 109 | 'response' => $recaptchaResponse, |
110 | - 'secret' => glsr_get_option( 'reviews-form.recaptcha.secret' ), |
|
111 | - ], 'https://www.google.com/recaptcha/api/siteverify' ); |
|
112 | - $response = json_decode( wp_remote_retrieve_body( wp_remote_get( $endpoint ))); |
|
113 | - if( !empty( $response->success )) { |
|
114 | - return boolval( $response->success ); |
|
110 | + 'secret' => glsr_get_option('reviews-form.recaptcha.secret'), |
|
111 | + ], 'https://www.google.com/recaptcha/api/siteverify'); |
|
112 | + $response = json_decode(wp_remote_retrieve_body(wp_remote_get($endpoint))); |
|
113 | + if (!empty($response->success)) { |
|
114 | + return boolval($response->success); |
|
115 | 115 | } |
116 | 116 | $errorCodes = [ |
117 | 117 | 'missing-input-secret' => 'The secret parameter is missing.', |
@@ -120,8 +120,8 @@ discard block |
||
120 | 120 | 'invalid-input-response' => 'The response parameter is invalid or malformed.', |
121 | 121 | 'bad-request' => 'The request is invalid or malformed.', |
122 | 122 | ]; |
123 | - foreach( $response->{'error-codes'} as $error ) { |
|
124 | - glsr_log()->error( 'reCAPTCHA: '.$errorCodes[$error] ); |
|
123 | + foreach ($response->{'error-codes'} as $error) { |
|
124 | + glsr_log()->error('reCAPTCHA: '.$errorCodes[$error]); |
|
125 | 125 | } |
126 | 126 | return false; |
127 | 127 | } |
@@ -129,15 +129,15 @@ discard block |
||
129 | 129 | /** |
130 | 130 | * @return bool |
131 | 131 | */ |
132 | - protected function isRequestValid( array $request ) |
|
132 | + protected function isRequestValid(array $request) |
|
133 | 133 | { |
134 | - $rules = $this->getValidationRules( $request ); |
|
135 | - $errors = glsr( Validator::class )->validate( $request, $rules ); |
|
136 | - if( empty( $errors )) { |
|
134 | + $rules = $this->getValidationRules($request); |
|
135 | + $errors = glsr(Validator::class)->validate($request, $rules); |
|
136 | + if (empty($errors)) { |
|
137 | 137 | return true; |
138 | 138 | } |
139 | - $this->setSession( 'errors', $errors ); |
|
140 | - $this->setSession( 'values', $request ); |
|
139 | + $this->setSession('errors', $errors); |
|
140 | + $this->setSession('values', $request); |
|
141 | 141 | return false; |
142 | 142 | } |
143 | 143 | |
@@ -147,12 +147,12 @@ discard block |
||
147 | 147 | * @param string $loggedMessage |
148 | 148 | * @return void |
149 | 149 | */ |
150 | - protected function setSessionValues( $type, $value, $loggedMessage = '' ) |
|
150 | + protected function setSessionValues($type, $value, $loggedMessage = '') |
|
151 | 151 | { |
152 | - glsr( Session::class )->set( $this->form_id.$type, $value ); |
|
153 | - if( !empty( $loggedMessage )) { |
|
154 | - glsr_log()->warning( $loggedMessage ); |
|
155 | - glsr_log()->warning( $this->request ); |
|
152 | + glsr(Session::class)->set($this->form_id.$type, $value); |
|
153 | + if (!empty($loggedMessage)) { |
|
154 | + glsr_log()->warning($loggedMessage); |
|
155 | + glsr_log()->warning($this->request); |
|
156 | 156 | } |
157 | 157 | } |
158 | 158 | |
@@ -161,10 +161,10 @@ discard block |
||
161 | 161 | */ |
162 | 162 | protected function validateAkismet() |
163 | 163 | { |
164 | - if( !empty( $this->error ))return; |
|
165 | - if( !glsr( Akismet::class )->isSpam( $this->request ))return; |
|
166 | - $this->setSessionValues( 'errors', [], 'Akismet caught a spam submission:' ); |
|
167 | - $this->error = __( 'Your review cannot be submitted at this time. Please try again later.', 'site-reviews' ); |
|
164 | + if (!empty($this->error))return; |
|
165 | + if (!glsr(Akismet::class)->isSpam($this->request))return; |
|
166 | + $this->setSessionValues('errors', [], 'Akismet caught a spam submission:'); |
|
167 | + $this->error = __('Your review cannot be submitted at this time. Please try again later.', 'site-reviews'); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | /** |
@@ -172,15 +172,15 @@ discard block |
||
172 | 172 | */ |
173 | 173 | protected function validateBlacklist() |
174 | 174 | { |
175 | - if( !empty( $this->error ))return; |
|
176 | - if( !glsr( Blacklist::class )->isBlacklisted( $this->request ))return; |
|
177 | - $blacklistAction = glsr_get_option( 'reviews-form.blacklist.action' ); |
|
178 | - if( $blacklistAction == 'unapprove' ) { |
|
175 | + if (!empty($this->error))return; |
|
176 | + if (!glsr(Blacklist::class)->isBlacklisted($this->request))return; |
|
177 | + $blacklistAction = glsr_get_option('reviews-form.blacklist.action'); |
|
178 | + if ($blacklistAction == 'unapprove') { |
|
179 | 179 | $this->request['blacklisted'] = true; |
180 | 180 | } |
181 | - else if( $blacklistAction == 'reject' ) { |
|
182 | - $this->setSessionValues( 'errors', [], 'Blacklisted submission detected:' ); |
|
183 | - $this->error = __( 'Your review cannot be submitted at this time.', 'site-reviews' ); |
|
181 | + else if ($blacklistAction == 'reject') { |
|
182 | + $this->setSessionValues('errors', [], 'Blacklisted submission detected:'); |
|
183 | + $this->error = __('Your review cannot be submitted at this time.', 'site-reviews'); |
|
184 | 184 | } |
185 | 185 | } |
186 | 186 | |
@@ -189,42 +189,42 @@ discard block |
||
189 | 189 | */ |
190 | 190 | protected function validateCustom() |
191 | 191 | { |
192 | - if( !empty( $this->error ))return; |
|
193 | - $validated = apply_filters( 'site-reviews/validate/review/submission', true, $this->request ); |
|
194 | - if( $validated === true )return; |
|
195 | - $this->setSessionValues( 'errors', [] ); |
|
196 | - $this->setSessionValues( 'values', $this->request ); |
|
197 | - $this->error = is_string( $validated ) |
|
192 | + if (!empty($this->error))return; |
|
193 | + $validated = apply_filters('site-reviews/validate/review/submission', true, $this->request); |
|
194 | + if ($validated === true)return; |
|
195 | + $this->setSessionValues('errors', []); |
|
196 | + $this->setSessionValues('values', $this->request); |
|
197 | + $this->error = is_string($validated) |
|
198 | 198 | ? $validated |
199 | - : __( 'The review submission failed. Please notify the site administrator.', 'site-reviews' ); |
|
199 | + : __('The review submission failed. Please notify the site administrator.', 'site-reviews'); |
|
200 | 200 | } |
201 | 201 | |
202 | 202 | /** |
203 | 203 | * @return void |
204 | 204 | */ |
205 | - protected function validateHoneyPot( array $request ) |
|
205 | + protected function validateHoneyPot(array $request) |
|
206 | 206 | { |
207 | - if( !empty( $this->error ))return; |
|
208 | - if( empty( $request['gotcha'] ))return; |
|
209 | - $this->setSessionValues( 'errors', [], 'The Honeypot caught a bad submission:' ); |
|
210 | - $this->error = __( 'The review submission failed. Please notify the site administrator.', 'site-reviews' ); |
|
207 | + if (!empty($this->error))return; |
|
208 | + if (empty($request['gotcha']))return; |
|
209 | + $this->setSessionValues('errors', [], 'The Honeypot caught a bad submission:'); |
|
210 | + $this->error = __('The review submission failed. Please notify the site administrator.', 'site-reviews'); |
|
211 | 211 | } |
212 | 212 | |
213 | 213 | /** |
214 | 214 | * @return void |
215 | 215 | */ |
216 | - protected function validateRecaptcha( array $request ) |
|
216 | + protected function validateRecaptcha(array $request) |
|
217 | 217 | { |
218 | - if( !empty( $this->error ))return; |
|
218 | + if (!empty($this->error))return; |
|
219 | 219 | $isValid = $this->isRecaptchaResponseValid(); |
220 | - if( is_null( $isValid )) { |
|
221 | - $this->setSessionValues( 'recaptcha', true ); |
|
220 | + if (is_null($isValid)) { |
|
221 | + $this->setSessionValues('recaptcha', true); |
|
222 | 222 | $this->recaptchaIsUnset = true; |
223 | 223 | } |
224 | - else if( !$isValid ) { |
|
225 | - $this->setSessionValues( 'errors', [] ); |
|
226 | - $this->setSessionValues( 'recaptcha', 'reset' ); |
|
227 | - $this->error = __( 'The reCAPTCHA verification failed. Please notify the site administrator.', 'site-reviews' ); |
|
224 | + else if (!$isValid) { |
|
225 | + $this->setSessionValues('errors', []); |
|
226 | + $this->setSessionValues('recaptcha', 'reset'); |
|
227 | + $this->error = __('The reCAPTCHA verification failed. Please notify the site administrator.', 'site-reviews'); |
|
228 | 228 | } |
229 | 229 | } |
230 | 230 | } |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | * @param mixed $value |
17 | 17 | * @return mixed |
18 | 18 | */ |
19 | - abstract protected function getSize( $attribute, $value ); |
|
19 | + abstract protected function getSize($attribute, $value); |
|
20 | 20 | |
21 | 21 | |
22 | 22 | /** |
@@ -24,9 +24,9 @@ discard block |
||
24 | 24 | * @param string $message |
25 | 25 | * @return string |
26 | 26 | */ |
27 | - protected function replaceBetween( $message, array $parameters ) |
|
27 | + protected function replaceBetween($message, array $parameters) |
|
28 | 28 | { |
29 | - return str_replace( [':min', ':max'], $parameters, $message ); |
|
29 | + return str_replace([':min', ':max'], $parameters, $message); |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | /** |
@@ -34,9 +34,9 @@ discard block |
||
34 | 34 | * @param string $message |
35 | 35 | * @return string |
36 | 36 | */ |
37 | - protected function replaceMax( $message, array $parameters ) |
|
37 | + protected function replaceMax($message, array $parameters) |
|
38 | 38 | { |
39 | - return str_replace( ':max', $parameters[0], $message ); |
|
39 | + return str_replace(':max', $parameters[0], $message); |
|
40 | 40 | } |
41 | 41 | |
42 | 42 | /** |
@@ -44,9 +44,9 @@ discard block |
||
44 | 44 | * @param string $message |
45 | 45 | * @return string |
46 | 46 | */ |
47 | - protected function replaceMin( $message, array $parameters ) |
|
47 | + protected function replaceMin($message, array $parameters) |
|
48 | 48 | { |
49 | - return str_replace( ':min', $parameters[0], $message ); |
|
49 | + return str_replace(':min', $parameters[0], $message); |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | /** |
@@ -56,10 +56,10 @@ discard block |
||
56 | 56 | * @param mixed $value |
57 | 57 | * @return bool |
58 | 58 | */ |
59 | - public function validateAccepted( $attribute, $value ) |
|
59 | + public function validateAccepted($attribute, $value) |
|
60 | 60 | { |
61 | 61 | $acceptable = ['yes', 'on', '1', 1, true, 'true']; |
62 | - return $this->validateRequired( $attribute, $value ) && in_array( $value, $acceptable, true ); |
|
62 | + return $this->validateRequired($attribute, $value) && in_array($value, $acceptable, true); |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | /** |
@@ -68,10 +68,10 @@ discard block |
||
68 | 68 | * @param mixed $value |
69 | 69 | * @return bool |
70 | 70 | */ |
71 | - public function validateBetween( $attribute, $value, array $parameters ) |
|
71 | + public function validateBetween($attribute, $value, array $parameters) |
|
72 | 72 | { |
73 | - $this->requireParameterCount( 2, $parameters, 'between' ); |
|
74 | - $size = $this->getSize( $attribute, $value ); |
|
73 | + $this->requireParameterCount(2, $parameters, 'between'); |
|
74 | + $size = $this->getSize($attribute, $value); |
|
75 | 75 | return $size >= $parameters[0] && $size <= $parameters[1]; |
76 | 76 | } |
77 | 77 | |
@@ -81,9 +81,9 @@ discard block |
||
81 | 81 | * @param mixed $value |
82 | 82 | * @return bool |
83 | 83 | */ |
84 | - public function validateEmail( $attribute, $value ) |
|
84 | + public function validateEmail($attribute, $value) |
|
85 | 85 | { |
86 | - return filter_var( $value, FILTER_VALIDATE_EMAIL ) !== false; |
|
86 | + return filter_var($value, FILTER_VALIDATE_EMAIL) !== false; |
|
87 | 87 | } |
88 | 88 | |
89 | 89 | /** |
@@ -92,10 +92,10 @@ discard block |
||
92 | 92 | * @param mixed $value |
93 | 93 | * @return bool |
94 | 94 | */ |
95 | - public function validateMax( $attribute, $value, array $parameters ) |
|
95 | + public function validateMax($attribute, $value, array $parameters) |
|
96 | 96 | { |
97 | - $this->requireParameterCount( 1, $parameters, 'max' ); |
|
98 | - return $this->getSize( $attribute, $value ) <= $parameters[0]; |
|
97 | + $this->requireParameterCount(1, $parameters, 'max'); |
|
98 | + return $this->getSize($attribute, $value) <= $parameters[0]; |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | /** |
@@ -104,10 +104,10 @@ discard block |
||
104 | 104 | * @param mixed $value |
105 | 105 | * @return bool |
106 | 106 | */ |
107 | - public function validateMin( $attribute, $value, array $parameters ) |
|
107 | + public function validateMin($attribute, $value, array $parameters) |
|
108 | 108 | { |
109 | - $this->requireParameterCount( 1, $parameters, 'min' ); |
|
110 | - return $this->getSize( $attribute, $value ) >= $parameters[0]; |
|
109 | + $this->requireParameterCount(1, $parameters, 'min'); |
|
110 | + return $this->getSize($attribute, $value) >= $parameters[0]; |
|
111 | 111 | } |
112 | 112 | |
113 | 113 | /** |
@@ -116,9 +116,9 @@ discard block |
||
116 | 116 | * @param mixed $value |
117 | 117 | * @return bool |
118 | 118 | */ |
119 | - public function validateNumeric( $attribute, $value ) |
|
119 | + public function validateNumeric($attribute, $value) |
|
120 | 120 | { |
121 | - return is_numeric( $value ); |
|
121 | + return is_numeric($value); |
|
122 | 122 | } |
123 | 123 | |
124 | 124 | /** |
@@ -127,11 +127,11 @@ discard block |
||
127 | 127 | * @param mixed $value |
128 | 128 | * @return bool |
129 | 129 | */ |
130 | - public function validateRequired( $attribute, $value ) |
|
130 | + public function validateRequired($attribute, $value) |
|
131 | 131 | { |
132 | - return is_null( $value ) |
|
133 | - || ( is_string( $value ) && trim( $value ) === '' ) |
|
134 | - || ( is_array( $value ) && count( $value ) < 1 ) |
|
132 | + return is_null($value) |
|
133 | + || (is_string($value) && trim($value) === '') |
|
134 | + || (is_array($value) && count($value) < 1) |
|
135 | 135 | ? false |
136 | 136 | : true; |
137 | 137 | } |
@@ -143,10 +143,10 @@ discard block |
||
143 | 143 | * @return void |
144 | 144 | * @throws InvalidArgumentException |
145 | 145 | */ |
146 | - protected function requireParameterCount( $count, array $parameters, $rule ) |
|
146 | + protected function requireParameterCount($count, array $parameters, $rule) |
|
147 | 147 | { |
148 | - if( count( $parameters ) < $count ) { |
|
149 | - throw new InvalidArgumentException( "Validation rule $rule requires at least $count parameters." ); |
|
148 | + if (count($parameters) < $count) { |
|
149 | + throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters."); |
|
150 | 150 | } |
151 | 151 | } |
152 | 152 | } |
@@ -46,24 +46,24 @@ discard block |
||
46 | 46 | * @param int $roundBy |
47 | 47 | * @return float |
48 | 48 | */ |
49 | - public function getAverage( array $reviews, $roundBy = 1 ) |
|
49 | + public function getAverage(array $reviews, $roundBy = 1) |
|
50 | 50 | { |
51 | - $average = count( $reviews ); |
|
52 | - if( $average > 0 ) { |
|
53 | - $average = round( $this->getTotal( $reviews ) / $average, intval( $roundBy )); |
|
51 | + $average = count($reviews); |
|
52 | + if ($average > 0) { |
|
53 | + $average = round($this->getTotal($reviews) / $average, intval($roundBy)); |
|
54 | 54 | } |
55 | - return floatval( apply_filters( 'site-reviews/rating/average', $average, $reviews )); |
|
55 | + return floatval(apply_filters('site-reviews/rating/average', $average, $reviews)); |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | /** |
59 | 59 | * @return array |
60 | 60 | */ |
61 | - public function getCounts( array $reviews ) |
|
61 | + public function getCounts(array $reviews) |
|
62 | 62 | { |
63 | - $counts = array_fill_keys( [5,4,3,2,1], [] ); |
|
64 | - array_walk( $counts, function( &$count, $key ) use( $reviews ) { |
|
65 | - $count = count( array_filter( $reviews, function( $review ) use( $key ) { |
|
66 | - if( !isset( $review->rating ))return; |
|
63 | + $counts = array_fill_keys([5, 4, 3, 2, 1], []); |
|
64 | + array_walk($counts, function(&$count, $key) use($reviews) { |
|
65 | + $count = count(array_filter($reviews, function($review) use($key) { |
|
66 | + if (!isset($review->rating))return; |
|
67 | 67 | return $review->rating == $key; |
68 | 68 | })); |
69 | 69 | }); |
@@ -80,51 +80,51 @@ discard block |
||
80 | 80 | * @param int $confidencePercentage |
81 | 81 | * @return int|float |
82 | 82 | */ |
83 | - public function getLowerBound( array $upDownRatings, $confidencePercentage = 95 ) |
|
83 | + public function getLowerBound(array $upDownRatings, $confidencePercentage = 95) |
|
84 | 84 | { |
85 | - $numRatings = count( $upDownRatings ); |
|
86 | - if( !$numRatings )return 0; |
|
87 | - $positiveRatings = count( array_filter( $upDownRatings, function( $value ) { |
|
85 | + $numRatings = count($upDownRatings); |
|
86 | + if (!$numRatings)return 0; |
|
87 | + $positiveRatings = count(array_filter($upDownRatings, function($value) { |
|
88 | 88 | return $value > 0; |
89 | 89 | })); |
90 | 90 | $z = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
91 | 91 | $phat = 1 * $positiveRatings / $numRatings; |
92 | - return ( $phat + $z * $z / ( 2 * $numRatings ) - $z * sqrt(( $phat * ( 1 - $phat ) + $z * $z / ( 4 * $numRatings )) / $numRatings )) / ( 1 + $z * $z / $numRatings ); |
|
92 | + return ($phat + $z * $z / (2 * $numRatings) - $z * sqrt(($phat * (1 - $phat) + $z * $z / (4 * $numRatings)) / $numRatings)) / (1 + $z * $z / $numRatings); |
|
93 | 93 | } |
94 | 94 | |
95 | 95 | /** |
96 | 96 | * Get the overall percentage rating for an array of reviews |
97 | 97 | * @return int|float |
98 | 98 | */ |
99 | - public function getPercentage( array $reviews ) |
|
99 | + public function getPercentage(array $reviews) |
|
100 | 100 | { |
101 | - return round( $this->getAverage( $reviews ) * 100 / static::MAX_RATING, 2 ); |
|
101 | + return round($this->getAverage($reviews) * 100 / static::MAX_RATING, 2); |
|
102 | 102 | } |
103 | 103 | |
104 | 104 | /** |
105 | 105 | * Get the percentage ratings for an array of reviews |
106 | 106 | * @return array |
107 | 107 | */ |
108 | - public function getPercentages( array $reviews ) |
|
108 | + public function getPercentages(array $reviews) |
|
109 | 109 | { |
110 | - $counts = $this->getCounts( $reviews ); |
|
111 | - array_walk( $counts, function( &$count, $rating ) use( $counts ) { |
|
112 | - $total = array_sum( $counts ); |
|
113 | - $count = !empty( $total ) && !empty( $counts[$rating] ) |
|
114 | - ? $counts[$rating] / array_sum( $counts ) * 100 |
|
110 | + $counts = $this->getCounts($reviews); |
|
111 | + array_walk($counts, function(&$count, $rating) use($counts) { |
|
112 | + $total = array_sum($counts); |
|
113 | + $count = !empty($total) && !empty($counts[$rating]) |
|
114 | + ? $counts[$rating] / array_sum($counts) * 100 |
|
115 | 115 | : 0; |
116 | 116 | }); |
117 | - return $this->getRoundedPercentages( $counts ); |
|
117 | + return $this->getRoundedPercentages($counts); |
|
118 | 118 | } |
119 | 119 | |
120 | 120 | /** |
121 | 121 | * Get the bayesian ranking for an array of reviews |
122 | 122 | * @return float |
123 | 123 | */ |
124 | - public function getRanking( array $reviews ) |
|
124 | + public function getRanking(array $reviews) |
|
125 | 125 | { |
126 | - return floatval( apply_filters( 'site-reviews/bayesian/ranking', |
|
127 | - $this->getRankingUsingImdb( $reviews ), |
|
126 | + return floatval(apply_filters('site-reviews/bayesian/ranking', |
|
127 | + $this->getRankingUsingImdb($reviews), |
|
128 | 128 | $reviews, |
129 | 129 | $this |
130 | 130 | )); |
@@ -134,10 +134,10 @@ discard block |
||
134 | 134 | * Get the sum of all review ratings |
135 | 135 | * @return int |
136 | 136 | */ |
137 | - public function getTotal( array $reviews ) |
|
137 | + public function getTotal(array $reviews) |
|
138 | 138 | { |
139 | - return array_reduce( $reviews, function( $sum, $review ) { |
|
140 | - return $sum + intval( $review->rating ); |
|
139 | + return array_reduce($reviews, function($sum, $review) { |
|
140 | + return $sum + intval($review->rating); |
|
141 | 141 | }); |
142 | 142 | } |
143 | 143 | |
@@ -151,17 +151,17 @@ discard block |
||
151 | 151 | * @param int $confidencePercentage |
152 | 152 | * @return int|float |
153 | 153 | */ |
154 | - protected function getRankingUsingImdb( array $reviews, $confidencePercentage = 70 ) |
|
154 | + protected function getRankingUsingImdb(array $reviews, $confidencePercentage = 70) |
|
155 | 155 | { |
156 | 156 | // Represents the number of ratings expected to begin observing a pattern that would put confidence in the prior. |
157 | 157 | $bayesMinimal = 10; // confidence |
158 | 158 | // Represents a prior (your prior opinion without data) for the average star rating. A higher prior also means a higher margin for error. |
159 | 159 | // This could also be the average score of all items instead of a fixed value. |
160 | - $bayesMean = ( $confidencePercentage / 100 ) * static::MAX_RATING; // prior, 70% = 3.5 |
|
161 | - $numOfReviews = count( $reviews ); |
|
162 | - $avgRating = $this->getAverage( $reviews ); |
|
160 | + $bayesMean = ($confidencePercentage / 100) * static::MAX_RATING; // prior, 70% = 3.5 |
|
161 | + $numOfReviews = count($reviews); |
|
162 | + $avgRating = $this->getAverage($reviews); |
|
163 | 163 | return $avgRating > 0 |
164 | - ? (( $bayesMinimal * $bayesMean ) + ( $avgRating * $numOfReviews )) / ( $bayesMinimal + $numOfReviews ) |
|
164 | + ? (($bayesMinimal * $bayesMean) + ($avgRating * $numOfReviews)) / ($bayesMinimal + $numOfReviews) |
|
165 | 165 | : 0; |
166 | 166 | } |
167 | 167 | |
@@ -175,41 +175,41 @@ discard block |
||
175 | 175 | * @param int $confidencePercentage |
176 | 176 | * @return float |
177 | 177 | */ |
178 | - protected function getRankingUsingZScores( array $reviews, $confidencePercentage = 90 ) |
|
178 | + protected function getRankingUsingZScores(array $reviews, $confidencePercentage = 90) |
|
179 | 179 | { |
180 | - $ratingCounts = $this->getCounts( $reviews ); |
|
181 | - $ratingCountsSum = array_sum( $ratingCounts ) + count( $ratingCounts ); |
|
182 | - $weight = $this->getWeight( $ratingCounts, $ratingCountsSum ); |
|
183 | - $weightPow2 = $this->getWeight( $ratingCounts, $ratingCountsSum, true ); |
|
180 | + $ratingCounts = $this->getCounts($reviews); |
|
181 | + $ratingCountsSum = array_sum($ratingCounts) + count($ratingCounts); |
|
182 | + $weight = $this->getWeight($ratingCounts, $ratingCountsSum); |
|
183 | + $weightPow2 = $this->getWeight($ratingCounts, $ratingCountsSum, true); |
|
184 | 184 | $zScore = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
185 | - return $weight - $zScore * sqrt(( $weightPow2 - pow( $weight, 2 )) / ( $ratingCountsSum + 1 )); |
|
185 | + return $weight - $zScore * sqrt(($weightPow2 - pow($weight, 2)) / ($ratingCountsSum + 1)); |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | /** |
189 | 189 | * @param int $target The target total percentage |
190 | 190 | * @return array |
191 | 191 | */ |
192 | - protected function getRoundedPercentages( array $percentages, $target = 100 ) |
|
192 | + protected function getRoundedPercentages(array $percentages, $target = 100) |
|
193 | 193 | { |
194 | - array_walk( $percentages, function( &$value, $index ) { |
|
194 | + array_walk($percentages, function(&$value, $index) { |
|
195 | 195 | $value = [ |
196 | 196 | 'index' => $index, |
197 | - 'percent' => floor( $value ), |
|
198 | - 'remainder' => fmod( $value, 1 ), |
|
197 | + 'percent' => floor($value), |
|
198 | + 'remainder' => fmod($value, 1), |
|
199 | 199 | ]; |
200 | 200 | }); |
201 | - $indexes = array_column( $percentages, 'index' ); |
|
202 | - $remainders = array_column( $percentages, 'remainder' ); |
|
203 | - array_multisort( $remainders, SORT_DESC, SORT_STRING, $indexes, SORT_DESC, $percentages ); |
|
201 | + $indexes = array_column($percentages, 'index'); |
|
202 | + $remainders = array_column($percentages, 'remainder'); |
|
203 | + array_multisort($remainders, SORT_DESC, SORT_STRING, $indexes, SORT_DESC, $percentages); |
|
204 | 204 | $i = 0; |
205 | - if( array_sum( array_column( $percentages, 'percent' )) > 0 ) { |
|
206 | - while( array_sum( array_column( $percentages, 'percent' )) < $target ) { |
|
205 | + if (array_sum(array_column($percentages, 'percent')) > 0) { |
|
206 | + while (array_sum(array_column($percentages, 'percent')) < $target) { |
|
207 | 207 | $percentages[$i]['percent']++; |
208 | 208 | $i++; |
209 | 209 | } |
210 | 210 | } |
211 | - array_multisort( $indexes, SORT_DESC, $percentages ); |
|
212 | - return array_combine( $indexes, array_column( $percentages, 'percent' )); |
|
211 | + array_multisort($indexes, SORT_DESC, $percentages); |
|
212 | + return array_combine($indexes, array_column($percentages, 'percent')); |
|
213 | 213 | } |
214 | 214 | |
215 | 215 | /** |
@@ -217,12 +217,12 @@ discard block |
||
217 | 217 | * @param bool $powerOf2 |
218 | 218 | * @return float |
219 | 219 | */ |
220 | - protected function getWeight( array $ratingCounts, $ratingCountsSum, $powerOf2 = false ) |
|
220 | + protected function getWeight(array $ratingCounts, $ratingCountsSum, $powerOf2 = false) |
|
221 | 221 | { |
222 | - return array_reduce( array_keys( $ratingCounts ), |
|
223 | - function( $count, $rating ) use( $ratingCounts, $ratingCountsSum, $powerOf2 ) { |
|
224 | - $ratingLevel = $powerOf2 ? pow( $rating, 2 ) : $rating; |
|
225 | - return $count + ( $ratingLevel * ( $ratingCounts[$rating] + 1 )) / $ratingCountsSum; |
|
222 | + return array_reduce(array_keys($ratingCounts), |
|
223 | + function($count, $rating) use($ratingCounts, $ratingCountsSum, $powerOf2) { |
|
224 | + $ratingLevel = $powerOf2 ? pow($rating, 2) : $rating; |
|
225 | + return $count + ($ratingLevel * ($ratingCounts[$rating] + 1)) / $ratingCountsSum; |
|
226 | 226 | } |
227 | 227 | ); |
228 | 228 | } |
@@ -13,8 +13,8 @@ discard block |
||
13 | 13 | */ |
14 | 14 | public static function databaseKey() |
15 | 15 | { |
16 | - return glsr( Helper::class )->snakeCase( |
|
17 | - Application::ID.'-v'.explode( '.', glsr()->version )[0] |
|
16 | + return glsr(Helper::class)->snakeCase( |
|
17 | + Application::ID.'-v'.explode('.', glsr()->version)[0] |
|
18 | 18 | ); |
19 | 19 | } |
20 | 20 | |
@@ -23,9 +23,9 @@ discard block |
||
23 | 23 | */ |
24 | 24 | public function all() |
25 | 25 | { |
26 | - $options = get_option( static::databaseKey(), [] ); |
|
27 | - if( !is_array( $options )) { |
|
28 | - delete_option( static::databaseKey() ); |
|
26 | + $options = get_option(static::databaseKey(), []); |
|
27 | + if (!is_array($options)) { |
|
28 | + delete_option(static::databaseKey()); |
|
29 | 29 | $options = []; |
30 | 30 | } |
31 | 31 | return $options; |
@@ -35,18 +35,18 @@ discard block |
||
35 | 35 | * @param string $path |
36 | 36 | * @return bool |
37 | 37 | */ |
38 | - public function delete( $path ) |
|
38 | + public function delete($path) |
|
39 | 39 | { |
40 | - $keys = explode( '.', $path ); |
|
41 | - $last = array_pop( $keys ); |
|
40 | + $keys = explode('.', $path); |
|
41 | + $last = array_pop($keys); |
|
42 | 42 | $options = $this->all(); |
43 | 43 | $pointer = &$options; |
44 | - foreach( $keys as $key ) { |
|
45 | - if( !isset( $pointer[$key] ) || !is_array( $pointer[$key] ))continue; |
|
44 | + foreach ($keys as $key) { |
|
45 | + if (!isset($pointer[$key]) || !is_array($pointer[$key]))continue; |
|
46 | 46 | $pointer = &$pointer[$key]; |
47 | 47 | } |
48 | - unset( $pointer[$last] ); |
|
49 | - return $this->set( $options ); |
|
48 | + unset($pointer[$last]); |
|
49 | + return $this->set($options); |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | /** |
@@ -54,15 +54,15 @@ discard block |
||
54 | 54 | * @param mixed $fallback |
55 | 55 | * @return mixed |
56 | 56 | */ |
57 | - public function get( $path = '', $fallback = '' ) |
|
57 | + public function get($path = '', $fallback = '') |
|
58 | 58 | { |
59 | 59 | $options = $this->all(); |
60 | - if( empty( $options )) { |
|
60 | + if (empty($options)) { |
|
61 | 61 | $options = [ |
62 | - 'settings' => glsr( DefaultsManager::class )->get(), |
|
62 | + 'settings' => glsr(DefaultsManager::class)->get(), |
|
63 | 63 | ]; |
64 | 64 | } |
65 | - return glsr( Helper::class )->getPathValue( $path, $fallback, $options ); |
|
65 | + return glsr(Helper::class)->getPathValue($path, $fallback, $options); |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | /** |
@@ -70,11 +70,11 @@ discard block |
||
70 | 70 | * @param mixed $value |
71 | 71 | * @return bool |
72 | 72 | */ |
73 | - public function set( $pathOrOptions, $value = '' ) |
|
73 | + public function set($pathOrOptions, $value = '') |
|
74 | 74 | { |
75 | - if( is_string( $pathOrOptions )) { |
|
76 | - $pathOrOptions = glsr( Helper::class )->setPathValue( $pathOrOptions, $value, $this->all() ); |
|
75 | + if (is_string($pathOrOptions)) { |
|
76 | + $pathOrOptions = glsr(Helper::class)->setPathValue($pathOrOptions, $value, $this->all()); |
|
77 | 77 | } |
78 | - return update_option( static::databaseKey(), (array)$pathOrOptions ); |
|
78 | + return update_option(static::databaseKey(), (array)$pathOrOptions); |
|
79 | 79 | } |
80 | 80 | } |
@@ -12,15 +12,15 @@ discard block |
||
12 | 12 | * Build a WP_Query meta_query/tax_query |
13 | 13 | * @return array |
14 | 14 | */ |
15 | - public function buildQuery( array $keys = [], array $values = [] ) |
|
15 | + public function buildQuery(array $keys = [], array $values = []) |
|
16 | 16 | { |
17 | 17 | $queries = []; |
18 | - foreach( $keys as $key ) { |
|
19 | - if( !array_key_exists( $key, $values ))continue; |
|
20 | - $methodName = glsr( Helper::class )->buildMethodName( $key, $prefix = __METHOD__ ); |
|
21 | - if( !method_exists( $this, $methodName ))continue; |
|
22 | - $query = call_user_func( [$this, $methodName], $values[$key] ); |
|
23 | - if( is_array( $query )) { |
|
18 | + foreach ($keys as $key) { |
|
19 | + if (!array_key_exists($key, $values))continue; |
|
20 | + $methodName = glsr(Helper::class)->buildMethodName($key, $prefix = __METHOD__); |
|
21 | + if (!method_exists($this, $methodName))continue; |
|
22 | + $query = call_user_func([$this, $methodName], $values[$key]); |
|
23 | + if (is_array($query)) { |
|
24 | 24 | $queries[] = $query; |
25 | 25 | } |
26 | 26 | } |
@@ -33,16 +33,16 @@ discard block |
||
33 | 33 | * @param string $sprintfFormat |
34 | 34 | * @return string |
35 | 35 | */ |
36 | - public function buildSqlOr( $values, $sprintfFormat ) |
|
36 | + public function buildSqlOr($values, $sprintfFormat) |
|
37 | 37 | { |
38 | - if( !is_array( $values )) { |
|
39 | - $values = explode( ',', $values ); |
|
38 | + if (!is_array($values)) { |
|
39 | + $values = explode(',', $values); |
|
40 | 40 | } |
41 | - $values = array_filter( array_map( 'trim', (array)$values )); |
|
42 | - $values = array_map( function( $value ) use( $sprintfFormat ) { |
|
43 | - return sprintf( $sprintfFormat, $value ); |
|
44 | - }, $values ); |
|
45 | - return implode( ' OR ', $values ); |
|
41 | + $values = array_filter(array_map('trim', (array)$values)); |
|
42 | + $values = array_map(function($value) use($sprintfFormat) { |
|
43 | + return sprintf($sprintfFormat, $value); |
|
44 | + }, $values); |
|
45 | + return implode(' OR ', $values); |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | /** |
@@ -52,23 +52,23 @@ discard block |
||
52 | 52 | * @return string |
53 | 53 | * @filter posts_search |
54 | 54 | */ |
55 | - public function filterSearchByTitle( $search, WP_Query $query ) |
|
55 | + public function filterSearchByTitle($search, WP_Query $query) |
|
56 | 56 | { |
57 | - if( empty( $search ) || empty( $query->get( 'search_terms' ))) { |
|
57 | + if (empty($search) || empty($query->get('search_terms'))) { |
|
58 | 58 | return $search; |
59 | 59 | } |
60 | 60 | global $wpdb; |
61 | - $n = empty( $query->get( 'exact' )) |
|
61 | + $n = empty($query->get('exact')) |
|
62 | 62 | ? '%' |
63 | 63 | : ''; |
64 | 64 | $search = []; |
65 | - foreach( (array)$query->get( 'search_terms' ) as $term ) { |
|
66 | - $search[] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", $n.$wpdb->esc_like( $term ).$n ); |
|
65 | + foreach ((array)$query->get('search_terms') as $term) { |
|
66 | + $search[] = $wpdb->prepare("{$wpdb->posts}.post_title LIKE %s", $n.$wpdb->esc_like($term).$n); |
|
67 | 67 | } |
68 | - if( !is_user_logged_in() ) { |
|
68 | + if (!is_user_logged_in()) { |
|
69 | 69 | $search[] = "{$wpdb->posts}.post_password = ''"; |
70 | 70 | } |
71 | - return ' AND '.implode( ' AND ', $search ); |
|
71 | + return ' AND '.implode(' AND ', $search); |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | /** |
@@ -76,25 +76,25 @@ discard block |
||
76 | 76 | * @param bool $isEnabled |
77 | 77 | * @return int |
78 | 78 | */ |
79 | - public function getPaged( $isEnabled = true ) |
|
79 | + public function getPaged($isEnabled = true) |
|
80 | 80 | { |
81 | 81 | $paged = $isEnabled |
82 | - ? intval( get_query_var( is_front_page() ? 'page' : Application::PAGED_QUERY_VAR )) |
|
82 | + ? intval(get_query_var(is_front_page() ? 'page' : Application::PAGED_QUERY_VAR)) |
|
83 | 83 | : 1; |
84 | - return max( 1, $paged ); |
|
84 | + return max(1, $paged); |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | /** |
88 | 88 | * @param string $value |
89 | 89 | * @return void|array |
90 | 90 | */ |
91 | - protected function buildQueryAssignedTo( $value ) |
|
91 | + protected function buildQueryAssignedTo($value) |
|
92 | 92 | { |
93 | - if( empty( $value ))return; |
|
93 | + if (empty($value))return; |
|
94 | 94 | return [ |
95 | 95 | 'compare' => 'IN', |
96 | 96 | 'key' => 'assigned_to', |
97 | - 'value' => array_filter( array_map( 'trim', explode( ',', $value )), 'is_numeric' ), |
|
97 | + 'value' => array_filter(array_map('trim', explode(',', $value)), 'is_numeric'), |
|
98 | 98 | ]; |
99 | 99 | } |
100 | 100 | |
@@ -102,9 +102,9 @@ discard block |
||
102 | 102 | * @param array $value |
103 | 103 | * @return void|array |
104 | 104 | */ |
105 | - protected function buildQueryCategory( $value ) |
|
105 | + protected function buildQueryCategory($value) |
|
106 | 106 | { |
107 | - if( empty( $value ))return; |
|
107 | + if (empty($value))return; |
|
108 | 108 | return [ |
109 | 109 | 'field' => 'term_id', |
110 | 110 | 'taxonomy' => Application::TAXONOMY, |
@@ -116,9 +116,9 @@ discard block |
||
116 | 116 | * @param string $value |
117 | 117 | * @return void|array |
118 | 118 | */ |
119 | - protected function buildQueryRating( $value ) |
|
119 | + protected function buildQueryRating($value) |
|
120 | 120 | { |
121 | - if( !is_numeric( $value ) || !in_array( intval( $value ), range( 1, 5 )))return; |
|
121 | + if (!is_numeric($value) || !in_array(intval($value), range(1, 5)))return; |
|
122 | 122 | return [ |
123 | 123 | 'compare' => '>=', |
124 | 124 | 'key' => 'rating', |
@@ -130,9 +130,9 @@ discard block |
||
130 | 130 | * @param string $value |
131 | 131 | * @return void|array |
132 | 132 | */ |
133 | - protected function buildQueryType( $value ) |
|
133 | + protected function buildQueryType($value) |
|
134 | 134 | { |
135 | - if( in_array( $value, ['','all'] ))return; |
|
135 | + if (in_array($value, ['', 'all']))return; |
|
136 | 136 | return [ |
137 | 137 | 'key' => 'review_type', |
138 | 138 | 'value' => $value, |
@@ -21,9 +21,9 @@ discard block |
||
21 | 21 | * @param $metaKey |
22 | 22 | * @return array |
23 | 23 | */ |
24 | - public function getReviewCounts( $metaKey ) |
|
24 | + public function getReviewCounts($metaKey) |
|
25 | 25 | { |
26 | - return (array) $this->db->get_results(" |
|
26 | + return (array)$this->db->get_results(" |
|
27 | 27 | SELECT m.meta_value AS name, COUNT(*) num_posts |
28 | 28 | FROM {$this->db->posts} AS p |
29 | 29 | INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | * @param $metaReviewId |
37 | 37 | * @return int |
38 | 38 | */ |
39 | - public function getReviewPostId( $metaReviewId ) |
|
39 | + public function getReviewPostId($metaReviewId) |
|
40 | 40 | { |
41 | 41 | $postId = $this->db->get_var(" |
42 | 42 | SELECT p.ID |
@@ -46,14 +46,14 @@ discard block |
||
46 | 46 | AND pm.meta_key = 'review_id' |
47 | 47 | AND pm.meta_value = '{$metaReviewId}' |
48 | 48 | "); |
49 | - return intval( $postId ); |
|
49 | + return intval($postId); |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | /** |
53 | 53 | * @param string $metaReviewType |
54 | 54 | * @return array |
55 | 55 | */ |
56 | - public function getReviewIdsByType( $metaReviewType ) |
|
56 | + public function getReviewIdsByType($metaReviewType) |
|
57 | 57 | { |
58 | 58 | $query = $this->db->get_col(" |
59 | 59 | SELECT m1.meta_value AS review_id |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | AND m2.meta_key = 'review_type' |
66 | 66 | AND m2.meta_value = '{$metaReviewType}' |
67 | 67 | "); |
68 | - return array_keys( array_flip( $query )); |
|
68 | + return array_keys(array_flip($query)); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | /** |
@@ -73,11 +73,11 @@ discard block |
||
73 | 73 | * @param string $status |
74 | 74 | * @return array |
75 | 75 | */ |
76 | - public function getReviewsMeta( $keys, $status ) |
|
76 | + public function getReviewsMeta($keys, $status) |
|
77 | 77 | { |
78 | - $queryBuilder = glsr( QueryBuilder::class ); |
|
79 | - $keys = $queryBuilder->buildSqlOr( $keys, "pm.meta_key = '%s'" ); |
|
80 | - $status = $queryBuilder->buildSqlOr( $status, "p.post_status = '%s'" ); |
|
78 | + $queryBuilder = glsr(QueryBuilder::class); |
|
79 | + $keys = $queryBuilder->buildSqlOr($keys, "pm.meta_key = '%s'"); |
|
80 | + $status = $queryBuilder->buildSqlOr($status, "p.post_status = '%s'"); |
|
81 | 81 | return $this->db->get_col(" |
82 | 82 | SELECT DISTINCT pm.meta_value FROM {$this->db->postmeta} pm |
83 | 83 | LEFT JOIN {$this->db->posts} p ON p.ID = pm.post_id |
@@ -12,11 +12,11 @@ discard block |
||
12 | 12 | */ |
13 | 13 | public function get() |
14 | 14 | { |
15 | - $settings = include glsr()->path( 'config/settings.php' ); |
|
16 | - $settings = apply_filters( 'site-reviews/addon/settings', $settings ); |
|
17 | - $settings = $this->normalize( $settings ); |
|
18 | - $defaults = array_combine( array_keys( $settings ), array_column( $settings, 'default' )); |
|
19 | - return glsr( Helper::class )->convertDotNotationArray( $defaults ); |
|
15 | + $settings = include glsr()->path('config/settings.php'); |
|
16 | + $settings = apply_filters('site-reviews/addon/settings', $settings); |
|
17 | + $settings = $this->normalize($settings); |
|
18 | + $defaults = array_combine(array_keys($settings), array_column($settings, 'default')); |
|
19 | + return glsr(Helper::class)->convertDotNotationArray($defaults); |
|
20 | 20 | } |
21 | 21 | |
22 | 22 | /** |
@@ -24,21 +24,21 @@ discard block |
||
24 | 24 | */ |
25 | 25 | public function set() |
26 | 26 | { |
27 | - $settings = glsr( OptionManager::class )->all(); |
|
28 | - $currentSettings = glsr( Helper::class )->removeEmptyArrayValues( $settings ); |
|
29 | - $defaultSettings = array_replace_recursive( $this->getDefaults(), $currentSettings ); |
|
30 | - $updatedSettings = array_replace_recursive( $settings, $defaultSettings ); |
|
31 | - update_option( OptionManager::databaseKey(), $updatedSettings ); |
|
27 | + $settings = glsr(OptionManager::class)->all(); |
|
28 | + $currentSettings = glsr(Helper::class)->removeEmptyArrayValues($settings); |
|
29 | + $defaultSettings = array_replace_recursive($this->getDefaults(), $currentSettings); |
|
30 | + $updatedSettings = array_replace_recursive($settings, $defaultSettings); |
|
31 | + update_option(OptionManager::databaseKey(), $updatedSettings); |
|
32 | 32 | return $defaultSettings; |
33 | 33 | } |
34 | 34 | |
35 | 35 | /** |
36 | 36 | * @return array |
37 | 37 | */ |
38 | - protected function normalize( array $settings ) |
|
38 | + protected function normalize(array $settings) |
|
39 | 39 | { |
40 | - array_walk( $settings, function( &$setting ) { |
|
41 | - if( isset( $setting['default'] ))return; |
|
40 | + array_walk($settings, function(&$setting) { |
|
41 | + if (isset($setting['default']))return; |
|
42 | 42 | $setting['default'] = null; |
43 | 43 | }); |
44 | 44 | return $settings; |
@@ -11,15 +11,15 @@ discard block |
||
11 | 11 | */ |
12 | 12 | public function getCloudflareIps() |
13 | 13 | { |
14 | - $ipAddresses = wp_cache_get( glsr()->id, '_cloudflare_ips' ); |
|
15 | - if( $ipAddresses === false ) { |
|
16 | - $ipAddresses = array_fill_keys( ['v4', 'v6'], [] ); |
|
17 | - foreach( array_keys( $ipAddresses ) as $version ) { |
|
18 | - $response = wp_remote_get( 'https://www.cloudflare.com/ips-'.$version ); |
|
19 | - if( is_wp_error( $response ))continue; |
|
20 | - $ipAddresses[$version] = array_filter( explode( PHP_EOL, wp_remote_retrieve_body( $response ))); |
|
14 | + $ipAddresses = wp_cache_get(glsr()->id, '_cloudflare_ips'); |
|
15 | + if ($ipAddresses === false) { |
|
16 | + $ipAddresses = array_fill_keys(['v4', 'v6'], []); |
|
17 | + foreach (array_keys($ipAddresses) as $version) { |
|
18 | + $response = wp_remote_get('https://www.cloudflare.com/ips-'.$version); |
|
19 | + if (is_wp_error($response))continue; |
|
20 | + $ipAddresses[$version] = array_filter(explode(PHP_EOL, wp_remote_retrieve_body($response))); |
|
21 | 21 | } |
22 | - wp_cache_set( glsr()->id, $ipAddresses, '_cloudflare_ips' ); |
|
22 | + wp_cache_set(glsr()->id, $ipAddresses, '_cloudflare_ips'); |
|
23 | 23 | } |
24 | 24 | return $ipAddresses; |
25 | 25 | } |
@@ -28,16 +28,16 @@ discard block |
||
28 | 28 | * @param string $metaKey |
29 | 29 | * @return array |
30 | 30 | */ |
31 | - public function getReviewCountsFor( $metaKey ) |
|
31 | + public function getReviewCountsFor($metaKey) |
|
32 | 32 | { |
33 | - $counts = wp_cache_get( glsr()->id, $metaKey.'_count' ); |
|
34 | - if( $counts === false ) { |
|
33 | + $counts = wp_cache_get(glsr()->id, $metaKey.'_count'); |
|
34 | + if ($counts === false) { |
|
35 | 35 | $counts = []; |
36 | - $results = glsr( SqlQueries::class )->getReviewCounts( $metaKey ); |
|
37 | - foreach( $results as $result ) { |
|
36 | + $results = glsr(SqlQueries::class)->getReviewCounts($metaKey); |
|
37 | + foreach ($results as $result) { |
|
38 | 38 | $counts[$result->name] = $result->num_posts; |
39 | 39 | } |
40 | - wp_cache_set( glsr()->id, $counts, $metaKey.'_count' ); |
|
40 | + wp_cache_set(glsr()->id, $counts, $metaKey.'_count'); |
|
41 | 41 | } |
42 | 42 | return $counts; |
43 | 43 | } |