@@ -66,14 +66,14 @@ discard block |
||
| 66 | 66 | * @param mixed $data |
| 67 | 67 | * @return array |
| 68 | 68 | */ |
| 69 | - public function validate($data, array $rules = []) |
|
| 69 | + public function validate( $data, array $rules = [] ) |
|
| 70 | 70 | { |
| 71 | - $this->normalizeData($data); |
|
| 72 | - $this->setRules($rules); |
|
| 73 | - foreach ($this->rules as $attribute => $rules) { |
|
| 74 | - foreach ($rules as $rule) { |
|
| 75 | - $this->validateAttribute($attribute, $rule); |
|
| 76 | - if ($this->shouldStopValidating($attribute)) { |
|
| 71 | + $this->normalizeData( $data ); |
|
| 72 | + $this->setRules( $rules ); |
|
| 73 | + foreach( $this->rules as $attribute => $rules ) { |
|
| 74 | + foreach( $rules as $rule ) { |
|
| 75 | + $this->validateAttribute( $attribute, $rule ); |
|
| 76 | + if( $this->shouldStopValidating( $attribute ) ) { |
|
| 77 | 77 | break; |
| 78 | 78 | } |
| 79 | 79 | } |
@@ -88,18 +88,18 @@ discard block |
||
| 88 | 88 | * @return void |
| 89 | 89 | * @throws BadMethodCallException |
| 90 | 90 | */ |
| 91 | - public function validateAttribute($attribute, $rule) |
|
| 91 | + public function validateAttribute( $attribute, $rule ) |
|
| 92 | 92 | { |
| 93 | - list($rule, $parameters) = $this->parseRule($rule); |
|
| 94 | - if ('' == $rule) { |
|
| 93 | + list($rule, $parameters) = $this->parseRule( $rule ); |
|
| 94 | + if( '' == $rule ) { |
|
| 95 | 95 | return; |
| 96 | 96 | } |
| 97 | - $value = $this->getValue($attribute); |
|
| 98 | - if (!method_exists($this, $method = 'validate'.$rule)) { |
|
| 99 | - throw new BadMethodCallException("Method [$method] does not exist."); |
|
| 97 | + $value = $this->getValue( $attribute ); |
|
| 98 | + if( !method_exists( $this, $method = 'validate'.$rule ) ) { |
|
| 99 | + throw new BadMethodCallException( "Method [$method] does not exist." ); |
|
| 100 | 100 | } |
| 101 | - if (!$this->$method($value, $attribute, $parameters)) { |
|
| 102 | - $this->addFailure($attribute, $rule, $parameters); |
|
| 101 | + if( !$this->$method( $value, $attribute, $parameters ) ) { |
|
| 102 | + $this->addFailure( $attribute, $rule, $parameters ); |
|
| 103 | 103 | } |
| 104 | 104 | } |
| 105 | 105 | |
@@ -109,9 +109,9 @@ discard block |
||
| 109 | 109 | * @param string $rule |
| 110 | 110 | * @return void |
| 111 | 111 | */ |
| 112 | - protected function addError($attribute, $rule, array $parameters) |
|
| 112 | + protected function addError( $attribute, $rule, array $parameters ) |
|
| 113 | 113 | { |
| 114 | - $message = $this->getMessage($attribute, $rule, $parameters); |
|
| 114 | + $message = $this->getMessage( $attribute, $rule, $parameters ); |
|
| 115 | 115 | $this->errors[$attribute][] = $message; |
| 116 | 116 | } |
| 117 | 117 | |
@@ -121,9 +121,9 @@ discard block |
||
| 121 | 121 | * @param string $rule |
| 122 | 122 | * @return void |
| 123 | 123 | */ |
| 124 | - protected function addFailure($attribute, $rule, array $parameters) |
|
| 124 | + protected function addFailure( $attribute, $rule, array $parameters ) |
|
| 125 | 125 | { |
| 126 | - $this->addError($attribute, $rule, $parameters); |
|
| 126 | + $this->addError( $attribute, $rule, $parameters ); |
|
| 127 | 127 | $this->failedRules[$attribute][$rule] = $parameters; |
| 128 | 128 | } |
| 129 | 129 | |
@@ -132,9 +132,9 @@ discard block |
||
| 132 | 132 | * @param string $attribute |
| 133 | 133 | * @return string |
| 134 | 134 | */ |
| 135 | - protected function getAttributeType($attribute) |
|
| 135 | + protected function getAttributeType( $attribute ) |
|
| 136 | 136 | { |
| 137 | - return !$this->hasRule($attribute, $this->numericRules) |
|
| 137 | + return !$this->hasRule( $attribute, $this->numericRules ) |
|
| 138 | 138 | ? 'length' |
| 139 | 139 | : ''; |
| 140 | 140 | } |
@@ -145,13 +145,13 @@ discard block |
||
| 145 | 145 | * @param string $rule |
| 146 | 146 | * @return string|null |
| 147 | 147 | */ |
| 148 | - protected function getMessage($attribute, $rule, array $parameters) |
|
| 148 | + protected function getMessage( $attribute, $rule, array $parameters ) |
|
| 149 | 149 | { |
| 150 | - if (in_array($rule, $this->sizeRules)) { |
|
| 151 | - return $this->getSizeMessage($attribute, $rule, $parameters); |
|
| 150 | + if( in_array( $rule, $this->sizeRules ) ) { |
|
| 151 | + return $this->getSizeMessage( $attribute, $rule, $parameters ); |
|
| 152 | 152 | } |
| 153 | - $lowerRule = glsr(Helper::class)->snakeCase($rule); |
|
| 154 | - return $this->translator($lowerRule, $parameters); |
|
| 153 | + $lowerRule = glsr( Helper::class )->snakeCase( $rule ); |
|
| 154 | + return $this->translator( $lowerRule, $parameters ); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
@@ -160,15 +160,15 @@ discard block |
||
| 160 | 160 | * @param string|array $rules |
| 161 | 161 | * @return array|null |
| 162 | 162 | */ |
| 163 | - protected function getRule($attribute, $rules) |
|
| 163 | + protected function getRule( $attribute, $rules ) |
|
| 164 | 164 | { |
| 165 | - if (!array_key_exists($attribute, $this->rules)) { |
|
| 165 | + if( !array_key_exists( $attribute, $this->rules ) ) { |
|
| 166 | 166 | return; |
| 167 | 167 | } |
| 168 | - $rules = (array) $rules; |
|
| 169 | - foreach ($this->rules[$attribute] as $rule) { |
|
| 170 | - list($rule, $parameters) = $this->parseRule($rule); |
|
| 171 | - if (in_array($rule, $rules)) { |
|
| 168 | + $rules = (array)$rules; |
|
| 169 | + foreach( $this->rules[$attribute] as $rule ) { |
|
| 170 | + list($rule, $parameters) = $this->parseRule( $rule ); |
|
| 171 | + if( in_array( $rule, $rules ) ) { |
|
| 172 | 172 | return [$rule, $parameters]; |
| 173 | 173 | } |
| 174 | 174 | } |
@@ -180,17 +180,17 @@ discard block |
||
| 180 | 180 | * @param mixed $value |
| 181 | 181 | * @return mixed |
| 182 | 182 | */ |
| 183 | - protected function getSize($attribute, $value) |
|
| 183 | + protected function getSize( $attribute, $value ) |
|
| 184 | 184 | { |
| 185 | - $hasNumeric = $this->hasRule($attribute, $this->numericRules); |
|
| 186 | - if (is_numeric($value) && $hasNumeric) { |
|
| 185 | + $hasNumeric = $this->hasRule( $attribute, $this->numericRules ); |
|
| 186 | + if( is_numeric( $value ) && $hasNumeric ) { |
|
| 187 | 187 | return $value; |
| 188 | - } elseif (is_array($value)) { |
|
| 189 | - return count($value); |
|
| 188 | + } elseif( is_array( $value ) ) { |
|
| 189 | + return count( $value ); |
|
| 190 | 190 | } |
| 191 | - return function_exists('mb_strlen') |
|
| 192 | - ? mb_strlen($value) |
|
| 193 | - : strlen($value); |
|
| 191 | + return function_exists( 'mb_strlen' ) |
|
| 192 | + ? mb_strlen( $value ) |
|
| 193 | + : strlen( $value ); |
|
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | /** |
@@ -199,11 +199,11 @@ discard block |
||
| 199 | 199 | * @param string $rule |
| 200 | 200 | * @return string|null |
| 201 | 201 | */ |
| 202 | - protected function getSizeMessage($attribute, $rule, array $parameters) |
|
| 202 | + protected function getSizeMessage( $attribute, $rule, array $parameters ) |
|
| 203 | 203 | { |
| 204 | - $type = $this->getAttributeType($attribute); |
|
| 205 | - $lowerRule = glsr(Helper::class)->snakeCase($rule.$type); |
|
| 206 | - return $this->translator($lowerRule, $parameters); |
|
| 204 | + $type = $this->getAttributeType( $attribute ); |
|
| 205 | + $lowerRule = glsr( Helper::class )->snakeCase( $rule.$type ); |
|
| 206 | + return $this->translator( $lowerRule, $parameters ); |
|
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | /** |
@@ -211,9 +211,9 @@ discard block |
||
| 211 | 211 | * @param string $attribute |
| 212 | 212 | * @return mixed |
| 213 | 213 | */ |
| 214 | - protected function getValue($attribute) |
|
| 214 | + protected function getValue( $attribute ) |
|
| 215 | 215 | { |
| 216 | - if (isset($this->data[$attribute])) { |
|
| 216 | + if( isset($this->data[$attribute]) ) { |
|
| 217 | 217 | return $this->data[$attribute]; |
| 218 | 218 | } |
| 219 | 219 | } |
@@ -224,9 +224,9 @@ discard block |
||
| 224 | 224 | * @param string|array $rules |
| 225 | 225 | * @return bool |
| 226 | 226 | */ |
| 227 | - protected function hasRule($attribute, $rules) |
|
| 227 | + protected function hasRule( $attribute, $rules ) |
|
| 228 | 228 | { |
| 229 | - return !is_null($this->getRule($attribute, $rules)); |
|
| 229 | + return !is_null( $this->getRule( $attribute, $rules ) ); |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | /** |
@@ -234,10 +234,10 @@ discard block |
||
| 234 | 234 | * @param mixed $data |
| 235 | 235 | * @return void |
| 236 | 236 | */ |
| 237 | - protected function normalizeData($data) |
|
| 237 | + protected function normalizeData( $data ) |
|
| 238 | 238 | { |
| 239 | - $this->data = is_object($data) |
|
| 240 | - ? get_object_vars($data) |
|
| 239 | + $this->data = is_object( $data ) |
|
| 240 | + ? get_object_vars( $data ) |
|
| 241 | 241 | : $data; |
| 242 | 242 | } |
| 243 | 243 | |
@@ -247,11 +247,11 @@ discard block |
||
| 247 | 247 | * @param string $parameter |
| 248 | 248 | * @return array |
| 249 | 249 | */ |
| 250 | - protected function parseParameters($rule, $parameter) |
|
| 250 | + protected function parseParameters( $rule, $parameter ) |
|
| 251 | 251 | { |
| 252 | - return 'regex' == strtolower($rule) |
|
| 252 | + return 'regex' == strtolower( $rule ) |
|
| 253 | 253 | ? [$parameter] |
| 254 | - : str_getcsv($parameter); |
|
| 254 | + : str_getcsv( $parameter ); |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | /** |
@@ -259,14 +259,14 @@ discard block |
||
| 259 | 259 | * @param string $rule |
| 260 | 260 | * @return array |
| 261 | 261 | */ |
| 262 | - protected function parseRule($rule) |
|
| 262 | + protected function parseRule( $rule ) |
|
| 263 | 263 | { |
| 264 | 264 | $parameters = []; |
| 265 | - if (false !== strpos($rule, ':')) { |
|
| 266 | - list($rule, $parameter) = explode(':', $rule, 2); |
|
| 267 | - $parameters = $this->parseParameters($rule, $parameter); |
|
| 265 | + if( false !== strpos( $rule, ':' ) ) { |
|
| 266 | + list($rule, $parameter) = explode( ':', $rule, 2 ); |
|
| 267 | + $parameters = $this->parseParameters( $rule, $parameter ); |
|
| 268 | 268 | } |
| 269 | - $rule = glsr(Helper::class)->camelCase($rule); |
|
| 269 | + $rule = glsr( Helper::class )->camelCase( $rule ); |
|
| 270 | 270 | return [$rule, $parameters]; |
| 271 | 271 | } |
| 272 | 272 | |
@@ -274,11 +274,11 @@ discard block |
||
| 274 | 274 | * Set the validation rules. |
| 275 | 275 | * @return void |
| 276 | 276 | */ |
| 277 | - protected function setRules(array $rules) |
|
| 277 | + protected function setRules( array $rules ) |
|
| 278 | 278 | { |
| 279 | - foreach ($rules as $key => $rule) { |
|
| 280 | - $rules[$key] = is_string($rule) |
|
| 281 | - ? explode('|', $rule) |
|
| 279 | + foreach( $rules as $key => $rule ) { |
|
| 280 | + $rules[$key] = is_string( $rule ) |
|
| 281 | + ? explode( '|', $rule ) |
|
| 282 | 282 | : $rule; |
| 283 | 283 | } |
| 284 | 284 | $this->rules = $rules; |
@@ -289,11 +289,11 @@ discard block |
||
| 289 | 289 | * @param string $attribute |
| 290 | 290 | * @return bool |
| 291 | 291 | */ |
| 292 | - protected function shouldStopValidating($attribute) |
|
| 292 | + protected function shouldStopValidating( $attribute ) |
|
| 293 | 293 | { |
| 294 | - return $this->hasRule($attribute, $this->implicitRules) |
|
| 294 | + return $this->hasRule( $attribute, $this->implicitRules ) |
|
| 295 | 295 | && isset($this->failedRules[$attribute]) |
| 296 | - && array_intersect(array_keys($this->failedRules[$attribute]), $this->implicitRules); |
|
| 296 | + && array_intersect( array_keys( $this->failedRules[$attribute] ), $this->implicitRules ); |
|
| 297 | 297 | } |
| 298 | 298 | |
| 299 | 299 | /** |
@@ -302,11 +302,11 @@ discard block |
||
| 302 | 302 | * @param string $attribute |
| 303 | 303 | * @return void|string |
| 304 | 304 | */ |
| 305 | - protected function translator($key, array $parameters) |
|
| 305 | + protected function translator( $key, array $parameters ) |
|
| 306 | 306 | { |
| 307 | - $strings = glsr(ValidationStringsDefaults::class)->defaults(); |
|
| 308 | - if (isset($strings[$key])) { |
|
| 309 | - return $this->replace($strings[$key], $parameters); |
|
| 307 | + $strings = glsr( ValidationStringsDefaults::class )->defaults(); |
|
| 308 | + if( isset($strings[$key]) ) { |
|
| 309 | + return $this->replace( $strings[$key], $parameters ); |
|
| 310 | 310 | } |
| 311 | 311 | return 'error'; |
| 312 | 312 | } |
@@ -15,23 +15,23 @@ discard block |
||
| 15 | 15 | * @param mixed $value |
| 16 | 16 | * @return mixed |
| 17 | 17 | */ |
| 18 | - abstract protected function getSize($attribute, $value); |
|
| 18 | + abstract protected function getSize( $attribute, $value ); |
|
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * Replace all placeholders. |
| 22 | 22 | * @param string $message |
| 23 | 23 | * @return string |
| 24 | 24 | */ |
| 25 | - protected function replace($message, array $parameters) |
|
| 25 | + protected function replace( $message, array $parameters ) |
|
| 26 | 26 | { |
| 27 | - if (false === strpos($message, '%s')) { |
|
| 27 | + if( false === strpos( $message, '%s' ) ) { |
|
| 28 | 28 | return $message; |
| 29 | 29 | } |
| 30 | - return preg_replace_callback('/(%s)/', function () use (&$parameters) { |
|
| 31 | - foreach ($parameters as $key => $value) { |
|
| 32 | - return array_shift($parameters); |
|
| 30 | + return preg_replace_callback( '/(%s)/', function() use (&$parameters) { |
|
| 31 | + foreach( $parameters as $key => $value ) { |
|
| 32 | + return array_shift( $parameters ); |
|
| 33 | 33 | } |
| 34 | - }, $message); |
|
| 34 | + }, $message ); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /** |
@@ -41,10 +41,10 @@ discard block |
||
| 41 | 41 | * @param mixed $value |
| 42 | 42 | * @return bool |
| 43 | 43 | */ |
| 44 | - public function validateAccepted($value) |
|
| 44 | + public function validateAccepted( $value ) |
|
| 45 | 45 | { |
| 46 | 46 | $acceptable = ['yes', 'on', '1', 1, true, 'true']; |
| 47 | - return $this->validateRequired($value) && in_array($value, $acceptable, true); |
|
| 47 | + return $this->validateRequired( $value ) && in_array( $value, $acceptable, true ); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | /** |
@@ -53,10 +53,10 @@ discard block |
||
| 53 | 53 | * @param mixed $value |
| 54 | 54 | * @return bool |
| 55 | 55 | */ |
| 56 | - public function validateBetween($value, $attribute, array $parameters) |
|
| 56 | + public function validateBetween( $value, $attribute, array $parameters ) |
|
| 57 | 57 | { |
| 58 | - $this->requireParameterCount(2, $parameters, 'between'); |
|
| 59 | - $size = $this->getSize($attribute, $value); |
|
| 58 | + $this->requireParameterCount( 2, $parameters, 'between' ); |
|
| 59 | + $size = $this->getSize( $attribute, $value ); |
|
| 60 | 60 | return $size >= $parameters[0] && $size <= $parameters[1]; |
| 61 | 61 | } |
| 62 | 62 | |
@@ -65,9 +65,9 @@ discard block |
||
| 65 | 65 | * @param mixed $value |
| 66 | 66 | * @return bool |
| 67 | 67 | */ |
| 68 | - public function validateEmail($value) |
|
| 68 | + public function validateEmail( $value ) |
|
| 69 | 69 | { |
| 70 | - return false !== filter_var($value, FILTER_VALIDATE_EMAIL); |
|
| 70 | + return false !== filter_var( $value, FILTER_VALIDATE_EMAIL ); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
@@ -76,10 +76,10 @@ discard block |
||
| 76 | 76 | * @param mixed $value |
| 77 | 77 | * @return bool |
| 78 | 78 | */ |
| 79 | - public function validateMax($value, $attribute, array $parameters) |
|
| 79 | + public function validateMax( $value, $attribute, array $parameters ) |
|
| 80 | 80 | { |
| 81 | - $this->requireParameterCount(1, $parameters, 'max'); |
|
| 82 | - return $this->getSize($attribute, $value) <= $parameters[0]; |
|
| 81 | + $this->requireParameterCount( 1, $parameters, 'max' ); |
|
| 82 | + return $this->getSize( $attribute, $value ) <= $parameters[0]; |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
@@ -88,10 +88,10 @@ discard block |
||
| 88 | 88 | * @param mixed $value |
| 89 | 89 | * @return bool |
| 90 | 90 | */ |
| 91 | - public function validateMin($value, $attribute, array $parameters) |
|
| 91 | + public function validateMin( $value, $attribute, array $parameters ) |
|
| 92 | 92 | { |
| 93 | - $this->requireParameterCount(1, $parameters, 'min'); |
|
| 94 | - return $this->getSize($attribute, $value) >= $parameters[0]; |
|
| 93 | + $this->requireParameterCount( 1, $parameters, 'min' ); |
|
| 94 | + return $this->getSize( $attribute, $value ) >= $parameters[0]; |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | /** |
@@ -99,9 +99,9 @@ discard block |
||
| 99 | 99 | * @param mixed $value |
| 100 | 100 | * @return bool |
| 101 | 101 | */ |
| 102 | - public function validateNumber($value) |
|
| 102 | + public function validateNumber( $value ) |
|
| 103 | 103 | { |
| 104 | - return is_numeric($value); |
|
| 104 | + return is_numeric( $value ); |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | /** |
@@ -109,11 +109,11 @@ discard block |
||
| 109 | 109 | * @param mixed $value |
| 110 | 110 | * @return bool |
| 111 | 111 | */ |
| 112 | - public function validateRequired($value) |
|
| 112 | + public function validateRequired( $value ) |
|
| 113 | 113 | { |
| 114 | - return is_null($value) |
|
| 115 | - || (is_string($value) && '' === trim($value)) |
|
| 116 | - || (is_array($value) && count($value) < 1) |
|
| 114 | + return is_null( $value ) |
|
| 115 | + || (is_string( $value ) && '' === trim( $value )) |
|
| 116 | + || (is_array( $value ) && count( $value ) < 1) |
|
| 117 | 117 | ? false |
| 118 | 118 | : true; |
| 119 | 119 | } |
@@ -125,10 +125,10 @@ discard block |
||
| 125 | 125 | * @return void |
| 126 | 126 | * @throws InvalidArgumentException |
| 127 | 127 | */ |
| 128 | - protected function requireParameterCount($count, array $parameters, $rule) |
|
| 128 | + protected function requireParameterCount( $count, array $parameters, $rule ) |
|
| 129 | 129 | { |
| 130 | - if (count($parameters) < $count) { |
|
| 131 | - throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters."); |
|
| 130 | + if( count( $parameters ) < $count ) { |
|
| 131 | + throw new InvalidArgumentException( "Validation rule $rule requires at least $count parameters." ); |
|
| 132 | 132 | } |
| 133 | 133 | } |
| 134 | 134 | } |
@@ -45,13 +45,13 @@ discard block |
||
| 45 | 45 | * @param int $roundBy |
| 46 | 46 | * @return float |
| 47 | 47 | */ |
| 48 | - public function getAverage(array $ratingCounts, $roundBy = 1) |
|
| 48 | + public function getAverage( array $ratingCounts, $roundBy = 1 ) |
|
| 49 | 49 | { |
| 50 | - $average = array_sum($ratingCounts); |
|
| 51 | - if ($average > 0) { |
|
| 52 | - $average = round($this->getTotalSum($ratingCounts) / $average, intval($roundBy)); |
|
| 50 | + $average = array_sum( $ratingCounts ); |
|
| 51 | + if( $average > 0 ) { |
|
| 52 | + $average = round( $this->getTotalSum( $ratingCounts ) / $average, intval( $roundBy ) ); |
|
| 53 | 53 | } |
| 54 | - return floatval(apply_filters('site-reviews/rating/average', $average, $ratingCounts)); |
|
| 54 | + return floatval( apply_filters( 'site-reviews/rating/average', $average, $ratingCounts ) ); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
@@ -64,50 +64,50 @@ discard block |
||
| 64 | 64 | * @param int $confidencePercentage |
| 65 | 65 | * @return int|float |
| 66 | 66 | */ |
| 67 | - public function getLowerBound(array $upDownCounts = [0, 0], $confidencePercentage = 95) |
|
| 67 | + public function getLowerBound( array $upDownCounts = [0, 0], $confidencePercentage = 95 ) |
|
| 68 | 68 | { |
| 69 | - $numRatings = array_sum($upDownCounts); |
|
| 70 | - if ($numRatings < 1) { |
|
| 69 | + $numRatings = array_sum( $upDownCounts ); |
|
| 70 | + if( $numRatings < 1 ) { |
|
| 71 | 71 | return 0; |
| 72 | 72 | } |
| 73 | 73 | $z = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
| 74 | 74 | $phat = 1 * $upDownCounts[1] / $numRatings; |
| 75 | - return ($phat + $z * $z / (2 * $numRatings) - $z * sqrt(($phat * (1 - $phat) + $z * $z / (4 * $numRatings)) / $numRatings)) / (1 + $z * $z / $numRatings); |
|
| 75 | + return ($phat + $z * $z / (2 * $numRatings) - $z * sqrt( ($phat * (1 - $phat) + $z * $z / (4 * $numRatings)) / $numRatings )) / (1 + $z * $z / $numRatings); |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | 79 | * @return int|float |
| 80 | 80 | */ |
| 81 | - public function getOverallPercentage(array $ratingCounts) |
|
| 81 | + public function getOverallPercentage( array $ratingCounts ) |
|
| 82 | 82 | { |
| 83 | - return round($this->getAverage($ratingCounts) * 100 / glsr()->constant('MAX_RATING'), 2); |
|
| 83 | + return round( $this->getAverage( $ratingCounts ) * 100 / glsr()->constant( 'MAX_RATING' ), 2 ); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | /** |
| 87 | 87 | * @return array |
| 88 | 88 | */ |
| 89 | - public function getPercentages(array $ratingCounts) |
|
| 89 | + public function getPercentages( array $ratingCounts ) |
|
| 90 | 90 | { |
| 91 | - $total = array_sum($ratingCounts); |
|
| 92 | - foreach ($ratingCounts as $index => $count) { |
|
| 93 | - if (empty($count)) { |
|
| 91 | + $total = array_sum( $ratingCounts ); |
|
| 92 | + foreach( $ratingCounts as $index => $count ) { |
|
| 93 | + if( empty($count) ) { |
|
| 94 | 94 | continue; |
| 95 | 95 | } |
| 96 | 96 | $ratingCounts[$index] = $count / $total * 100; |
| 97 | 97 | } |
| 98 | - return $this->getRoundedPercentages($ratingCounts); |
|
| 98 | + return $this->getRoundedPercentages( $ratingCounts ); |
|
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | /** |
| 102 | 102 | * @return float |
| 103 | 103 | */ |
| 104 | - public function getRanking(array $ratingCounts) |
|
| 104 | + public function getRanking( array $ratingCounts ) |
|
| 105 | 105 | { |
| 106 | - return floatval(apply_filters('site-reviews/rating/ranking', |
|
| 107 | - $this->getRankingUsingImdb($ratingCounts), |
|
| 106 | + return floatval( apply_filters( 'site-reviews/rating/ranking', |
|
| 107 | + $this->getRankingUsingImdb( $ratingCounts ), |
|
| 108 | 108 | $ratingCounts, |
| 109 | 109 | $this |
| 110 | - )); |
|
| 110 | + ) ); |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | /** |
@@ -120,15 +120,15 @@ discard block |
||
| 120 | 120 | * @param int $confidencePercentage |
| 121 | 121 | * @return int|float |
| 122 | 122 | */ |
| 123 | - public function getRankingUsingImdb(array $ratingCounts, $confidencePercentage = 70) |
|
| 123 | + public function getRankingUsingImdb( array $ratingCounts, $confidencePercentage = 70 ) |
|
| 124 | 124 | { |
| 125 | - $avgRating = $this->getAverage($ratingCounts); |
|
| 125 | + $avgRating = $this->getAverage( $ratingCounts ); |
|
| 126 | 126 | // Represents a prior (your prior opinion without data) for the average star rating. A higher prior also means a higher margin for error. |
| 127 | 127 | // This could also be the average score of all items instead of a fixed value. |
| 128 | - $bayesMean = ($confidencePercentage / 100) * glsr()->constant('MAX_RATING'); // prior, 70% = 3.5 |
|
| 128 | + $bayesMean = ($confidencePercentage / 100) * glsr()->constant( 'MAX_RATING' ); // prior, 70% = 3.5 |
|
| 129 | 129 | // Represents the number of ratings expected to begin observing a pattern that would put confidence in the prior. |
| 130 | 130 | $bayesMinimal = 10; // confidence |
| 131 | - $numOfReviews = array_sum($ratingCounts); |
|
| 131 | + $numOfReviews = array_sum( $ratingCounts ); |
|
| 132 | 132 | return $avgRating > 0 |
| 133 | 133 | ? (($bayesMinimal * $bayesMean) + ($avgRating * $numOfReviews)) / ($bayesMinimal + $numOfReviews) |
| 134 | 134 | : 0; |
@@ -144,48 +144,48 @@ discard block |
||
| 144 | 144 | * @param int $confidencePercentage |
| 145 | 145 | * @return float |
| 146 | 146 | */ |
| 147 | - public function getRankingUsingZScores(array $ratingCounts, $confidencePercentage = 90) |
|
| 147 | + public function getRankingUsingZScores( array $ratingCounts, $confidencePercentage = 90 ) |
|
| 148 | 148 | { |
| 149 | - $ratingCountsSum = array_sum($ratingCounts) + glsr()->constant('MAX_RATING'); |
|
| 150 | - $weight = $this->getWeight($ratingCounts, $ratingCountsSum); |
|
| 151 | - $weightPow2 = $this->getWeight($ratingCounts, $ratingCountsSum, true); |
|
| 149 | + $ratingCountsSum = array_sum( $ratingCounts ) + glsr()->constant( 'MAX_RATING' ); |
|
| 150 | + $weight = $this->getWeight( $ratingCounts, $ratingCountsSum ); |
|
| 151 | + $weightPow2 = $this->getWeight( $ratingCounts, $ratingCountsSum, true ); |
|
| 152 | 152 | $zScore = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
| 153 | - return $weight - $zScore * sqrt(($weightPow2 - pow($weight, 2)) / ($ratingCountsSum + 1)); |
|
| 153 | + return $weight - $zScore * sqrt( ($weightPow2 - pow( $weight, 2 )) / ($ratingCountsSum + 1) ); |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | /** |
| 157 | 157 | * @param int $target |
| 158 | 158 | * @return array |
| 159 | 159 | */ |
| 160 | - protected function getRoundedPercentages(array $percentages, $totalPercent = 100) |
|
| 160 | + protected function getRoundedPercentages( array $percentages, $totalPercent = 100 ) |
|
| 161 | 161 | { |
| 162 | - array_walk($percentages, function (&$percent, $index) { |
|
| 162 | + array_walk( $percentages, function( &$percent, $index ) { |
|
| 163 | 163 | $percent = [ |
| 164 | 164 | 'index' => $index, |
| 165 | - 'percent' => floor($percent), |
|
| 166 | - 'remainder' => fmod($percent, 1), |
|
| 165 | + 'percent' => floor( $percent ), |
|
| 166 | + 'remainder' => fmod( $percent, 1 ), |
|
| 167 | 167 | ]; |
| 168 | 168 | }); |
| 169 | - $indexes = glsr_array_column($percentages, 'index'); |
|
| 170 | - $remainders = glsr_array_column($percentages, 'remainder'); |
|
| 171 | - array_multisort($remainders, SORT_DESC, SORT_STRING, $indexes, SORT_DESC, $percentages); |
|
| 169 | + $indexes = glsr_array_column( $percentages, 'index' ); |
|
| 170 | + $remainders = glsr_array_column( $percentages, 'remainder' ); |
|
| 171 | + array_multisort( $remainders, SORT_DESC, SORT_STRING, $indexes, SORT_DESC, $percentages ); |
|
| 172 | 172 | $i = 0; |
| 173 | - if (array_sum(glsr_array_column($percentages, 'percent')) > 0) { |
|
| 174 | - while (array_sum(glsr_array_column($percentages, 'percent')) < $totalPercent) { |
|
| 173 | + if( array_sum( glsr_array_column( $percentages, 'percent' ) ) > 0 ) { |
|
| 174 | + while( array_sum( glsr_array_column( $percentages, 'percent' ) ) < $totalPercent ) { |
|
| 175 | 175 | ++$percentages[$i]['percent']; |
| 176 | 176 | ++$i; |
| 177 | 177 | } |
| 178 | 178 | } |
| 179 | - array_multisort($indexes, SORT_DESC, $percentages); |
|
| 180 | - return array_combine($indexes, glsr_array_column($percentages, 'percent')); |
|
| 179 | + array_multisort( $indexes, SORT_DESC, $percentages ); |
|
| 180 | + return array_combine( $indexes, glsr_array_column( $percentages, 'percent' ) ); |
|
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | /** |
| 184 | 184 | * @return int |
| 185 | 185 | */ |
| 186 | - protected function getTotalSum(array $ratingCounts) |
|
| 186 | + protected function getTotalSum( array $ratingCounts ) |
|
| 187 | 187 | { |
| 188 | - return array_reduce(array_keys($ratingCounts), function ($carry, $index) use ($ratingCounts) { |
|
| 188 | + return array_reduce( array_keys( $ratingCounts ), function( $carry, $index ) use ($ratingCounts) { |
|
| 189 | 189 | return $carry + ($index * $ratingCounts[$index]); |
| 190 | 190 | }); |
| 191 | 191 | } |
@@ -195,12 +195,12 @@ discard block |
||
| 195 | 195 | * @param bool $powerOf2 |
| 196 | 196 | * @return float |
| 197 | 197 | */ |
| 198 | - protected function getWeight(array $ratingCounts, $ratingCountsSum, $powerOf2 = false) |
|
| 198 | + protected function getWeight( array $ratingCounts, $ratingCountsSum, $powerOf2 = false ) |
|
| 199 | 199 | { |
| 200 | - return array_reduce(array_keys($ratingCounts), |
|
| 201 | - function ($count, $rating) use ($ratingCounts, $ratingCountsSum, $powerOf2) { |
|
| 200 | + return array_reduce( array_keys( $ratingCounts ), |
|
| 201 | + function( $count, $rating ) use ($ratingCounts, $ratingCountsSum, $powerOf2) { |
|
| 202 | 202 | $ratingLevel = $powerOf2 |
| 203 | - ? pow($rating, 2) |
|
| 203 | + ? pow( $rating, 2 ) |
|
| 204 | 204 | : $rating; |
| 205 | 205 | return $count + ($ratingLevel * ($ratingCounts[$rating] + 1)) / $ratingCountsSum; |
| 206 | 206 | } |
@@ -8,5 +8,5 @@ |
||
| 8 | 8 | * @params string|array $atts |
| 9 | 9 | * @return string |
| 10 | 10 | */ |
| 11 | - public function buildShortcode($atts = []); |
|
| 11 | + public function buildShortcode( $atts = [] ); |
|
| 12 | 12 | } |
@@ -7,5 +7,5 @@ |
||
| 7 | 7 | /** |
| 8 | 8 | * @return void|string |
| 9 | 9 | */ |
| 10 | - public function build(array $args = []); |
|
| 10 | + public function build( array $args = [] ); |
|
| 11 | 11 | } |
@@ -9,5 +9,5 @@ |
||
| 9 | 9 | /** |
| 10 | 10 | * @return void |
| 11 | 11 | */ |
| 12 | - public function register(Application $app); |
|
| 12 | + public function register( Application $app ); |
|
| 13 | 13 | } |
@@ -13,25 +13,25 @@ discard block |
||
| 13 | 13 | */ |
| 14 | 14 | public function getCloudflareIps() |
| 15 | 15 | { |
| 16 | - $ipAddresses = get_transient(Application::ID.'_cloudflare_ips'); |
|
| 17 | - if (false === $ipAddresses) { |
|
| 18 | - $ipAddresses = array_fill_keys(['v4', 'v6'], []); |
|
| 19 | - foreach (array_keys($ipAddresses) as $version) { |
|
| 16 | + $ipAddresses = get_transient( Application::ID.'_cloudflare_ips' ); |
|
| 17 | + if( false === $ipAddresses ) { |
|
| 18 | + $ipAddresses = array_fill_keys( ['v4', 'v6'], [] ); |
|
| 19 | + foreach( array_keys( $ipAddresses ) as $version ) { |
|
| 20 | 20 | $url = 'https://www.cloudflare.com/ips-'.$version; |
| 21 | - $response = wp_remote_get($url); |
|
| 22 | - if (is_wp_error($response)) { |
|
| 23 | - glsr_log()->error($response->get_error_message()); |
|
| 21 | + $response = wp_remote_get( $url ); |
|
| 22 | + if( is_wp_error( $response ) ) { |
|
| 23 | + glsr_log()->error( $response->get_error_message() ); |
|
| 24 | 24 | continue; |
| 25 | 25 | } |
| 26 | - if ('200' != ($statusCode = wp_remote_retrieve_response_code($response))) { |
|
| 27 | - glsr_log()->error('Unable to connect to '.$url.' ['.$statusCode.']'); |
|
| 26 | + if( '200' != ($statusCode = wp_remote_retrieve_response_code( $response )) ) { |
|
| 27 | + glsr_log()->error( 'Unable to connect to '.$url.' ['.$statusCode.']' ); |
|
| 28 | 28 | continue; |
| 29 | 29 | } |
| 30 | 30 | $ipAddresses[$version] = array_filter( |
| 31 | - (array) preg_split('/\R/', wp_remote_retrieve_body($response)) |
|
| 31 | + (array)preg_split( '/\R/', wp_remote_retrieve_body( $response ) ) |
|
| 32 | 32 | ); |
| 33 | 33 | } |
| 34 | - set_transient(Application::ID.'_cloudflare_ips', $ipAddresses, static::EXPIRY_TIME); |
|
| 34 | + set_transient( Application::ID.'_cloudflare_ips', $ipAddresses, static::EXPIRY_TIME ); |
|
| 35 | 35 | } |
| 36 | 36 | return $ipAddresses; |
| 37 | 37 | } |
@@ -40,16 +40,16 @@ discard block |
||
| 40 | 40 | * @param string $metaKey |
| 41 | 41 | * @return array |
| 42 | 42 | */ |
| 43 | - public function getReviewCountsFor($metaKey) |
|
| 43 | + public function getReviewCountsFor( $metaKey ) |
|
| 44 | 44 | { |
| 45 | - $counts = wp_cache_get(Application::ID, $metaKey.'_count'); |
|
| 46 | - if (false === $counts) { |
|
| 45 | + $counts = wp_cache_get( Application::ID, $metaKey.'_count' ); |
|
| 46 | + if( false === $counts ) { |
|
| 47 | 47 | $counts = []; |
| 48 | - $results = glsr(SqlQueries::class)->getReviewCountsFor($metaKey); |
|
| 49 | - foreach ($results as $result) { |
|
| 48 | + $results = glsr( SqlQueries::class )->getReviewCountsFor( $metaKey ); |
|
| 49 | + foreach( $results as $result ) { |
|
| 50 | 50 | $counts[$result->name] = $result->num_posts; |
| 51 | 51 | } |
| 52 | - wp_cache_set(Application::ID, $counts, $metaKey.'_count'); |
|
| 52 | + wp_cache_set( Application::ID, $counts, $metaKey.'_count' ); |
|
| 53 | 53 | } |
| 54 | 54 | return $counts; |
| 55 | 55 | } |
@@ -59,14 +59,14 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | public function getRemotePostTest() |
| 61 | 61 | { |
| 62 | - $test = get_transient(Application::ID.'_remote_post_test'); |
|
| 63 | - if (false === $test) { |
|
| 64 | - $response = wp_remote_post('https://api.wordpress.org/stats/php/1.0/'); |
|
| 65 | - $test = !is_wp_error($response) |
|
| 66 | - && in_array($response['response']['code'], range(200, 299)) |
|
| 62 | + $test = get_transient( Application::ID.'_remote_post_test' ); |
|
| 63 | + if( false === $test ) { |
|
| 64 | + $response = wp_remote_post( 'https://api.wordpress.org/stats/php/1.0/' ); |
|
| 65 | + $test = !is_wp_error( $response ) |
|
| 66 | + && in_array( $response['response']['code'], range( 200, 299 ) ) |
|
| 67 | 67 | ? 'Works' |
| 68 | 68 | : 'Does not work'; |
| 69 | - set_transient(Application::ID.'_remote_post_test', $test, static::EXPIRY_TIME); |
|
| 69 | + set_transient( Application::ID.'_remote_post_test', $test, static::EXPIRY_TIME ); |
|
| 70 | 70 | } |
| 71 | 71 | return $test; |
| 72 | 72 | } |
@@ -12,11 +12,11 @@ discard block |
||
| 12 | 12 | public function defaults() |
| 13 | 13 | { |
| 14 | 14 | $settings = $this->settings(); |
| 15 | - $defaults = array_combine(array_keys($settings), glsr_array_column($settings, 'default')); |
|
| 16 | - return wp_parse_args($defaults, [ |
|
| 15 | + $defaults = array_combine( array_keys( $settings ), glsr_array_column( $settings, 'default' ) ); |
|
| 16 | + return wp_parse_args( $defaults, [ |
|
| 17 | 17 | 'version' => '', |
| 18 | 18 | 'version_upgraded_from' => '', |
| 19 | - ]); |
|
| 19 | + ] ); |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | */ |
| 25 | 25 | public function get() |
| 26 | 26 | { |
| 27 | - return glsr(Helper::class)->convertDotNotationArray($this->defaults()); |
|
| 27 | + return glsr( Helper::class )->convertDotNotationArray( $this->defaults() ); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | /** |
@@ -32,11 +32,11 @@ discard block |
||
| 32 | 32 | */ |
| 33 | 33 | public function set() |
| 34 | 34 | { |
| 35 | - $settings = glsr(OptionManager::class)->all(); |
|
| 36 | - $currentSettings = glsr(Helper::class)->removeEmptyArrayValues($settings); |
|
| 37 | - $defaultSettings = array_replace_recursive($this->get(), $currentSettings); |
|
| 38 | - $updatedSettings = array_replace_recursive($settings, $defaultSettings); |
|
| 39 | - update_option(OptionManager::databaseKey(), $updatedSettings); |
|
| 35 | + $settings = glsr( OptionManager::class )->all(); |
|
| 36 | + $currentSettings = glsr( Helper::class )->removeEmptyArrayValues( $settings ); |
|
| 37 | + $defaultSettings = array_replace_recursive( $this->get(), $currentSettings ); |
|
| 38 | + $updatedSettings = array_replace_recursive( $settings, $defaultSettings ); |
|
| 39 | + update_option( OptionManager::databaseKey(), $updatedSettings ); |
|
| 40 | 40 | return $defaultSettings; |
| 41 | 41 | } |
| 42 | 42 | |
@@ -45,17 +45,17 @@ discard block |
||
| 45 | 45 | */ |
| 46 | 46 | public function settings() |
| 47 | 47 | { |
| 48 | - $settings = apply_filters('site-reviews/addon/settings', glsr()->config('settings')); |
|
| 49 | - return $this->normalize($settings); |
|
| 48 | + $settings = apply_filters( 'site-reviews/addon/settings', glsr()->config( 'settings' ) ); |
|
| 49 | + return $this->normalize( $settings ); |
|
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * @return array |
| 54 | 54 | */ |
| 55 | - protected function normalize(array $settings) |
|
| 55 | + protected function normalize( array $settings ) |
|
| 56 | 56 | { |
| 57 | - array_walk($settings, function (&$setting) { |
|
| 58 | - if (isset($setting['default'])) { |
|
| 57 | + array_walk( $settings, function( &$setting ) { |
|
| 58 | + if( isset($setting['default']) ) { |
|
| 59 | 59 | return; |
| 60 | 60 | } |
| 61 | 61 | $setting['default'] = ''; |
@@ -20,27 +20,27 @@ discard block |
||
| 20 | 20 | * @return array |
| 21 | 21 | * @todo verify the additional type checks are needed |
| 22 | 22 | */ |
| 23 | - public function buildCounts(array $args = []) |
|
| 23 | + public function buildCounts( array $args = [] ) |
|
| 24 | 24 | { |
| 25 | 25 | $counts = []; |
| 26 | - $query = $this->queryReviews($args); |
|
| 27 | - while ($query) { |
|
| 26 | + $query = $this->queryReviews( $args ); |
|
| 27 | + while( $query ) { |
|
| 28 | 28 | // glsr_log($query); |
| 29 | - $types = array_keys(array_flip(glsr_array_column($query->reviews, 'type'))); |
|
| 30 | - $types = array_unique(array_merge(['local'], $types)); |
|
| 31 | - foreach ($types as $type) { |
|
| 32 | - $type = $this->normalizeType($type); |
|
| 33 | - if (isset($counts[$type])) { |
|
| 29 | + $types = array_keys( array_flip( glsr_array_column( $query->reviews, 'type' ) ) ); |
|
| 30 | + $types = array_unique( array_merge( ['local'], $types ) ); |
|
| 31 | + foreach( $types as $type ) { |
|
| 32 | + $type = $this->normalizeType( $type ); |
|
| 33 | + if( isset($counts[$type]) ) { |
|
| 34 | 34 | continue; |
| 35 | 35 | } |
| 36 | - $counts[$type] = array_fill_keys(range(0, glsr()->constant('MAX_RATING', Rating::class)), 0); |
|
| 36 | + $counts[$type] = array_fill_keys( range( 0, glsr()->constant( 'MAX_RATING', Rating::class ) ), 0 ); |
|
| 37 | 37 | } |
| 38 | - foreach ($query->reviews as $review) { |
|
| 39 | - $type = $this->normalizeType($review->type); |
|
| 38 | + foreach( $query->reviews as $review ) { |
|
| 39 | + $type = $this->normalizeType( $review->type ); |
|
| 40 | 40 | ++$counts[$type][$review->rating]; |
| 41 | 41 | } |
| 42 | 42 | $query = $query->has_more |
| 43 | - ? $this->queryReviews($args, end($query->reviews)->ID) |
|
| 43 | + ? $this->queryReviews( $args, end( $query->reviews )->ID ) |
|
| 44 | 44 | : false; |
| 45 | 45 | } |
| 46 | 46 | return $counts; |
@@ -50,18 +50,18 @@ discard block |
||
| 50 | 50 | * @param int $postId |
| 51 | 51 | * @return array |
| 52 | 52 | */ |
| 53 | - public function buildPostCounts($postId) |
|
| 53 | + public function buildPostCounts( $postId ) |
|
| 54 | 54 | { |
| 55 | - return $this->buildCounts(['post_ids' => [$postId]]); |
|
| 55 | + return $this->buildCounts( ['post_ids' => [$postId]] ); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | 59 | * @param int $termTaxonomyId |
| 60 | 60 | * @return array |
| 61 | 61 | */ |
| 62 | - public function buildTermCounts($termTaxonomyId) |
|
| 62 | + public function buildTermCounts( $termTaxonomyId ) |
|
| 63 | 63 | { |
| 64 | - return $this->buildCounts(['term_ids' => [$termTaxonomyId]]); |
|
| 64 | + return $this->buildCounts( ['term_ids' => [$termTaxonomyId]] ); |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | /** |
@@ -69,80 +69,80 @@ discard block |
||
| 69 | 69 | */ |
| 70 | 70 | public function countAll() |
| 71 | 71 | { |
| 72 | - $terms = glsr(Database::class)->getTerms(['fields' => 'all']); |
|
| 73 | - foreach ($terms as $term) { |
|
| 74 | - $this->setTermCounts($term->term_id, $this->buildTermCounts($term->term_taxonomy_id)); |
|
| 72 | + $terms = glsr( Database::class )->getTerms( ['fields' => 'all'] ); |
|
| 73 | + foreach( $terms as $term ) { |
|
| 74 | + $this->setTermCounts( $term->term_id, $this->buildTermCounts( $term->term_taxonomy_id ) ); |
|
| 75 | 75 | } |
| 76 | - $postIds = glsr(SqlQueries::class)->getReviewsMeta('assigned_to'); |
|
| 77 | - foreach ($postIds as $postId) { |
|
| 78 | - $this->setPostCounts($postId, $this->buildPostCounts($postId)); |
|
| 76 | + $postIds = glsr( SqlQueries::class )->getReviewsMeta( 'assigned_to' ); |
|
| 77 | + foreach( $postIds as $postId ) { |
|
| 78 | + $this->setPostCounts( $postId, $this->buildPostCounts( $postId ) ); |
|
| 79 | 79 | } |
| 80 | - $this->setCounts($this->buildCounts()); |
|
| 80 | + $this->setCounts( $this->buildCounts() ); |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | /** |
| 84 | 84 | * @return void |
| 85 | 85 | */ |
| 86 | - public function decrease(Review $review) |
|
| 86 | + public function decrease( Review $review ) |
|
| 87 | 87 | { |
| 88 | - $this->decreaseCounts($review); |
|
| 89 | - $this->decreasePostCounts($review); |
|
| 90 | - $this->decreaseTermCounts($review); |
|
| 88 | + $this->decreaseCounts( $review ); |
|
| 89 | + $this->decreasePostCounts( $review ); |
|
| 90 | + $this->decreaseTermCounts( $review ); |
|
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | /** |
| 94 | 94 | * @return void |
| 95 | 95 | */ |
| 96 | - public function decreaseCounts(Review $review) |
|
| 96 | + public function decreaseCounts( Review $review ) |
|
| 97 | 97 | { |
| 98 | - $this->setCounts($this->decreaseRating( |
|
| 98 | + $this->setCounts( $this->decreaseRating( |
|
| 99 | 99 | $this->getCounts(), |
| 100 | 100 | $review->review_type, |
| 101 | 101 | $review->rating |
| 102 | - )); |
|
| 102 | + ) ); |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | /** |
| 106 | 106 | * @return void |
| 107 | 107 | */ |
| 108 | - public function decreasePostCounts(Review $review) |
|
| 108 | + public function decreasePostCounts( Review $review ) |
|
| 109 | 109 | { |
| 110 | - if (empty($counts = $this->getPostCounts($review->assigned_to))) { |
|
| 110 | + if( empty($counts = $this->getPostCounts( $review->assigned_to )) ) { |
|
| 111 | 111 | return; |
| 112 | 112 | } |
| 113 | - $counts = $this->decreaseRating($counts, $review->review_type, $review->rating); |
|
| 114 | - $this->setPostCounts($review->assigned_to, $counts); |
|
| 113 | + $counts = $this->decreaseRating( $counts, $review->review_type, $review->rating ); |
|
| 114 | + $this->setPostCounts( $review->assigned_to, $counts ); |
|
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | /** |
| 118 | 118 | * @return void |
| 119 | 119 | */ |
| 120 | - public function decreaseTermCounts(Review $review) |
|
| 120 | + public function decreaseTermCounts( Review $review ) |
|
| 121 | 121 | { |
| 122 | - foreach ($review->term_ids as $termId) { |
|
| 123 | - if (empty($counts = $this->getTermCounts($termId))) { |
|
| 122 | + foreach( $review->term_ids as $termId ) { |
|
| 123 | + if( empty($counts = $this->getTermCounts( $termId )) ) { |
|
| 124 | 124 | continue; |
| 125 | 125 | } |
| 126 | - $counts = $this->decreaseRating($counts, $review->review_type, $review->rating); |
|
| 127 | - $this->setTermCounts($termId, $counts); |
|
| 126 | + $counts = $this->decreaseRating( $counts, $review->review_type, $review->rating ); |
|
| 127 | + $this->setTermCounts( $termId, $counts ); |
|
| 128 | 128 | } |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | /** |
| 132 | 132 | * @return array |
| 133 | 133 | */ |
| 134 | - public function flatten(array $reviewCounts, array $args = []) |
|
| 134 | + public function flatten( array $reviewCounts, array $args = [] ) |
|
| 135 | 135 | { |
| 136 | 136 | $counts = []; |
| 137 | - array_walk_recursive($reviewCounts, function ($num, $index) use (&$counts) { |
|
| 138 | - $counts[$index] = $num + intval(glsr_get($counts, $index, 0)); |
|
| 137 | + array_walk_recursive( $reviewCounts, function( $num, $index ) use (&$counts) { |
|
| 138 | + $counts[$index] = $num + intval( glsr_get( $counts, $index, 0 ) ); |
|
| 139 | 139 | }); |
| 140 | - $args = wp_parse_args($args, [ |
|
| 141 | - 'max' => glsr()->constant('MAX_RATING', Rating::class), |
|
| 142 | - 'min' => glsr()->constant('MIN_RATING', Rating::class), |
|
| 143 | - ]); |
|
| 144 | - foreach ($counts as $index => &$num) { |
|
| 145 | - if ($index >= intval($args['min']) && $index <= intval($args['max'])) { |
|
| 140 | + $args = wp_parse_args( $args, [ |
|
| 141 | + 'max' => glsr()->constant( 'MAX_RATING', Rating::class ), |
|
| 142 | + 'min' => glsr()->constant( 'MIN_RATING', Rating::class ), |
|
| 143 | + ] ); |
|
| 144 | + foreach( $counts as $index => &$num ) { |
|
| 145 | + if( $index >= intval( $args['min'] ) && $index <= intval( $args['max'] ) ) { |
|
| 146 | 146 | continue; |
| 147 | 147 | } |
| 148 | 148 | $num = 0; |
@@ -153,26 +153,26 @@ discard block |
||
| 153 | 153 | /** |
| 154 | 154 | * @return array |
| 155 | 155 | */ |
| 156 | - public function get(array $args = []) |
|
| 156 | + public function get( array $args = [] ) |
|
| 157 | 157 | { |
| 158 | - $args = $this->normalizeArgs($args); |
|
| 158 | + $args = $this->normalizeArgs( $args ); |
|
| 159 | 159 | $counts = []; |
| 160 | - if ($this->isMixedCount($args)) { |
|
| 161 | - $counts = [$this->buildCounts($args)]; // force query the database |
|
| 160 | + if( $this->isMixedCount( $args ) ) { |
|
| 161 | + $counts = [$this->buildCounts( $args )]; // force query the database |
|
| 162 | 162 | } else { |
| 163 | - foreach ($args['post_ids'] as $postId) { |
|
| 164 | - $counts[] = $this->getPostCounts($postId); |
|
| 163 | + foreach( $args['post_ids'] as $postId ) { |
|
| 164 | + $counts[] = $this->getPostCounts( $postId ); |
|
| 165 | 165 | } |
| 166 | - foreach ($args['term_ids'] as $termId) { |
|
| 167 | - $counts[] = $this->getTermCounts($termId); |
|
| 166 | + foreach( $args['term_ids'] as $termId ) { |
|
| 167 | + $counts[] = $this->getTermCounts( $termId ); |
|
| 168 | 168 | } |
| 169 | - if (empty($counts)) { |
|
| 169 | + if( empty($counts) ) { |
|
| 170 | 170 | $counts[] = $this->getCounts(); |
| 171 | 171 | } |
| 172 | 172 | } |
| 173 | - return in_array($args['type'], ['', 'all']) |
|
| 174 | - ? $this->normalize([$this->flatten($counts)]) |
|
| 175 | - : $this->normalize(glsr_array_column($counts, $args['type'])); |
|
| 173 | + return in_array( $args['type'], ['', 'all'] ) |
|
| 174 | + ? $this->normalize( [$this->flatten( $counts )] ) |
|
| 175 | + : $this->normalize( glsr_array_column( $counts, $args['type'] ) ); |
|
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | /** |
@@ -180,9 +180,9 @@ discard block |
||
| 180 | 180 | */ |
| 181 | 181 | public function getCounts() |
| 182 | 182 | { |
| 183 | - $counts = glsr(OptionManager::class)->get('counts', []); |
|
| 184 | - if (!is_array($counts)) { |
|
| 185 | - glsr_log()->error('$counts is not an array')->debug($counts); |
|
| 183 | + $counts = glsr( OptionManager::class )->get( 'counts', [] ); |
|
| 184 | + if( !is_array( $counts ) ) { |
|
| 185 | + glsr_log()->error( '$counts is not an array' )->debug( $counts ); |
|
| 186 | 186 | return []; |
| 187 | 187 | } |
| 188 | 188 | return $counts; |
@@ -192,105 +192,105 @@ discard block |
||
| 192 | 192 | * @param int $postId |
| 193 | 193 | * @return array |
| 194 | 194 | */ |
| 195 | - public function getPostCounts($postId) |
|
| 195 | + public function getPostCounts( $postId ) |
|
| 196 | 196 | { |
| 197 | - return array_filter((array) get_post_meta($postId, static::META_COUNT, true)); |
|
| 197 | + return array_filter( (array)get_post_meta( $postId, static::META_COUNT, true ) ); |
|
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | /** |
| 201 | 201 | * @param int $termId |
| 202 | 202 | * @return array |
| 203 | 203 | */ |
| 204 | - public function getTermCounts($termId) |
|
| 204 | + public function getTermCounts( $termId ) |
|
| 205 | 205 | { |
| 206 | - return array_filter((array) get_term_meta($termId, static::META_COUNT, true)); |
|
| 206 | + return array_filter( (array)get_term_meta( $termId, static::META_COUNT, true ) ); |
|
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | /** |
| 210 | 210 | * @return void |
| 211 | 211 | */ |
| 212 | - public function increase(Review $review) |
|
| 212 | + public function increase( Review $review ) |
|
| 213 | 213 | { |
| 214 | - $this->increaseCounts($review); |
|
| 215 | - $this->increasePostCounts($review); |
|
| 216 | - $this->increaseTermCounts($review); |
|
| 214 | + $this->increaseCounts( $review ); |
|
| 215 | + $this->increasePostCounts( $review ); |
|
| 216 | + $this->increaseTermCounts( $review ); |
|
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | /** |
| 220 | 220 | * @return void |
| 221 | 221 | */ |
| 222 | - public function increaseCounts(Review $review) |
|
| 222 | + public function increaseCounts( Review $review ) |
|
| 223 | 223 | { |
| 224 | - if (empty($counts = $this->getCounts())) { |
|
| 224 | + if( empty($counts = $this->getCounts()) ) { |
|
| 225 | 225 | $counts = $this->buildCounts(); |
| 226 | 226 | } |
| 227 | - $this->setCounts($this->increaseRating($counts, $review->review_type, $review->rating)); |
|
| 227 | + $this->setCounts( $this->increaseRating( $counts, $review->review_type, $review->rating ) ); |
|
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | /** |
| 231 | 231 | * @return void |
| 232 | 232 | */ |
| 233 | - public function increasePostCounts(Review $review) |
|
| 233 | + public function increasePostCounts( Review $review ) |
|
| 234 | 234 | { |
| 235 | - if (!(get_post($review->assigned_to) instanceof WP_Post)) { |
|
| 235 | + if( !(get_post( $review->assigned_to ) instanceof WP_Post) ) { |
|
| 236 | 236 | return; |
| 237 | 237 | } |
| 238 | - $counts = $this->getPostCounts($review->assigned_to); |
|
| 238 | + $counts = $this->getPostCounts( $review->assigned_to ); |
|
| 239 | 239 | $counts = empty($counts) |
| 240 | - ? $this->buildPostCounts($review->assigned_to) |
|
| 241 | - : $this->increaseRating($counts, $review->review_type, $review->rating); |
|
| 242 | - $this->setPostCounts($review->assigned_to, $counts); |
|
| 240 | + ? $this->buildPostCounts( $review->assigned_to ) |
|
| 241 | + : $this->increaseRating( $counts, $review->review_type, $review->rating ); |
|
| 242 | + $this->setPostCounts( $review->assigned_to, $counts ); |
|
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | /** |
| 246 | 246 | * @return void |
| 247 | 247 | */ |
| 248 | - public function increaseTermCounts(Review $review) |
|
| 248 | + public function increaseTermCounts( Review $review ) |
|
| 249 | 249 | { |
| 250 | - $terms = glsr(ReviewManager::class)->normalizeTerms(implode(',', $review->term_ids)); |
|
| 251 | - foreach ($terms as $term) { |
|
| 252 | - $counts = $this->getTermCounts($term['term_id']); |
|
| 250 | + $terms = glsr( ReviewManager::class )->normalizeTerms( implode( ',', $review->term_ids ) ); |
|
| 251 | + foreach( $terms as $term ) { |
|
| 252 | + $counts = $this->getTermCounts( $term['term_id'] ); |
|
| 253 | 253 | $counts = empty($counts) |
| 254 | - ? $this->buildTermCounts($term['term_taxonomy_id']) |
|
| 255 | - : $this->increaseRating($counts, $review->review_type, $review->rating); |
|
| 256 | - $this->setTermCounts($term['term_id'], $counts); |
|
| 254 | + ? $this->buildTermCounts( $term['term_taxonomy_id'] ) |
|
| 255 | + : $this->increaseRating( $counts, $review->review_type, $review->rating ); |
|
| 256 | + $this->setTermCounts( $term['term_id'], $counts ); |
|
| 257 | 257 | } |
| 258 | 258 | } |
| 259 | 259 | |
| 260 | 260 | /** |
| 261 | 261 | * @return void |
| 262 | 262 | */ |
| 263 | - public function setCounts(array $reviewCounts) |
|
| 263 | + public function setCounts( array $reviewCounts ) |
|
| 264 | 264 | { |
| 265 | - glsr(OptionManager::class)->set('counts', $reviewCounts); |
|
| 265 | + glsr( OptionManager::class )->set( 'counts', $reviewCounts ); |
|
| 266 | 266 | } |
| 267 | 267 | |
| 268 | 268 | /** |
| 269 | 269 | * @param int $postId |
| 270 | 270 | * @return void |
| 271 | 271 | */ |
| 272 | - public function setPostCounts($postId, array $reviewCounts) |
|
| 272 | + public function setPostCounts( $postId, array $reviewCounts ) |
|
| 273 | 273 | { |
| 274 | - $ratingCounts = $this->flatten($reviewCounts); |
|
| 275 | - update_post_meta($postId, static::META_COUNT, $reviewCounts); |
|
| 276 | - update_post_meta($postId, static::META_AVERAGE, glsr(Rating::class)->getAverage($ratingCounts)); |
|
| 277 | - update_post_meta($postId, static::META_RANKING, glsr(Rating::class)->getRanking($ratingCounts)); |
|
| 274 | + $ratingCounts = $this->flatten( $reviewCounts ); |
|
| 275 | + update_post_meta( $postId, static::META_COUNT, $reviewCounts ); |
|
| 276 | + update_post_meta( $postId, static::META_AVERAGE, glsr( Rating::class )->getAverage( $ratingCounts ) ); |
|
| 277 | + update_post_meta( $postId, static::META_RANKING, glsr( Rating::class )->getRanking( $ratingCounts ) ); |
|
| 278 | 278 | } |
| 279 | 279 | |
| 280 | 280 | /** |
| 281 | 281 | * @param int $termId |
| 282 | 282 | * @return void |
| 283 | 283 | */ |
| 284 | - public function setTermCounts($termId, array $reviewCounts) |
|
| 284 | + public function setTermCounts( $termId, array $reviewCounts ) |
|
| 285 | 285 | { |
| 286 | - $term = get_term($termId, Application::TAXONOMY); |
|
| 287 | - if (!isset($term->term_id)) { |
|
| 286 | + $term = get_term( $termId, Application::TAXONOMY ); |
|
| 287 | + if( !isset($term->term_id) ) { |
|
| 288 | 288 | return; |
| 289 | 289 | } |
| 290 | - $ratingCounts = $this->flatten($reviewCounts); |
|
| 291 | - update_term_meta($termId, static::META_COUNT, $reviewCounts); |
|
| 292 | - update_term_meta($termId, static::META_AVERAGE, glsr(Rating::class)->getAverage($ratingCounts)); |
|
| 293 | - update_term_meta($termId, static::META_RANKING, glsr(Rating::class)->getRanking($ratingCounts)); |
|
| 290 | + $ratingCounts = $this->flatten( $reviewCounts ); |
|
| 291 | + update_term_meta( $termId, static::META_COUNT, $reviewCounts ); |
|
| 292 | + update_term_meta( $termId, static::META_AVERAGE, glsr( Rating::class )->getAverage( $ratingCounts ) ); |
|
| 293 | + update_term_meta( $termId, static::META_RANKING, glsr( Rating::class )->getRanking( $ratingCounts ) ); |
|
| 294 | 294 | } |
| 295 | 295 | |
| 296 | 296 | /** |
@@ -298,10 +298,10 @@ discard block |
||
| 298 | 298 | * @param int $rating |
| 299 | 299 | * @return array |
| 300 | 300 | */ |
| 301 | - protected function decreaseRating(array $reviewCounts, $type, $rating) |
|
| 301 | + protected function decreaseRating( array $reviewCounts, $type, $rating ) |
|
| 302 | 302 | { |
| 303 | - if (isset($reviewCounts[$type][$rating])) { |
|
| 304 | - $reviewCounts[$type][$rating] = max(0, $reviewCounts[$type][$rating] - 1); |
|
| 303 | + if( isset($reviewCounts[$type][$rating]) ) { |
|
| 304 | + $reviewCounts[$type][$rating] = max( 0, $reviewCounts[$type][$rating] - 1 ); |
|
| 305 | 305 | } |
| 306 | 306 | return $reviewCounts; |
| 307 | 307 | } |
@@ -311,23 +311,23 @@ discard block |
||
| 311 | 311 | * @param int $rating |
| 312 | 312 | * @return array |
| 313 | 313 | */ |
| 314 | - protected function increaseRating(array $reviewCounts, $type, $rating) |
|
| 314 | + protected function increaseRating( array $reviewCounts, $type, $rating ) |
|
| 315 | 315 | { |
| 316 | - if (!array_key_exists($type, glsr()->reviewTypes)) { |
|
| 316 | + if( !array_key_exists( $type, glsr()->reviewTypes ) ) { |
|
| 317 | 317 | return $reviewCounts; |
| 318 | 318 | } |
| 319 | - if (!array_key_exists($type, $reviewCounts)) { |
|
| 319 | + if( !array_key_exists( $type, $reviewCounts ) ) { |
|
| 320 | 320 | $reviewCounts[$type] = []; |
| 321 | 321 | } |
| 322 | - $reviewCounts = $this->normalize($reviewCounts); |
|
| 323 | - $reviewCounts[$type][$rating] = intval($reviewCounts[$type][$rating]) + 1; |
|
| 322 | + $reviewCounts = $this->normalize( $reviewCounts ); |
|
| 323 | + $reviewCounts[$type][$rating] = intval( $reviewCounts[$type][$rating] ) + 1; |
|
| 324 | 324 | return $reviewCounts; |
| 325 | 325 | } |
| 326 | 326 | |
| 327 | 327 | /** |
| 328 | 328 | * @return bool |
| 329 | 329 | */ |
| 330 | - protected function isMixedCount(array $args) |
|
| 330 | + protected function isMixedCount( array $args ) |
|
| 331 | 331 | { |
| 332 | 332 | return !empty($args['post_ids']) && !empty($args['term_ids']); |
| 333 | 333 | } |
@@ -335,19 +335,19 @@ discard block |
||
| 335 | 335 | /** |
| 336 | 336 | * @return array |
| 337 | 337 | */ |
| 338 | - protected function normalize(array $reviewCounts) |
|
| 338 | + protected function normalize( array $reviewCounts ) |
|
| 339 | 339 | { |
| 340 | - if (empty($reviewCounts)) { |
|
| 340 | + if( empty($reviewCounts) ) { |
|
| 341 | 341 | $reviewCounts = [[]]; |
| 342 | 342 | } |
| 343 | - foreach ($reviewCounts as &$counts) { |
|
| 344 | - foreach (range(0, glsr()->constant('MAX_RATING', Rating::class)) as $index) { |
|
| 345 | - if (isset($counts[$index])) { |
|
| 343 | + foreach( $reviewCounts as &$counts ) { |
|
| 344 | + foreach( range( 0, glsr()->constant( 'MAX_RATING', Rating::class ) ) as $index ) { |
|
| 345 | + if( isset($counts[$index]) ) { |
|
| 346 | 346 | continue; |
| 347 | 347 | } |
| 348 | 348 | $counts[$index] = 0; |
| 349 | 349 | } |
| 350 | - ksort($counts); |
|
| 350 | + ksort( $counts ); |
|
| 351 | 351 | } |
| 352 | 352 | return $reviewCounts; |
| 353 | 353 | } |
@@ -355,15 +355,15 @@ discard block |
||
| 355 | 355 | /** |
| 356 | 356 | * @return array |
| 357 | 357 | */ |
| 358 | - protected function normalizeArgs(array $args) |
|
| 358 | + protected function normalizeArgs( array $args ) |
|
| 359 | 359 | { |
| 360 | - $args = wp_parse_args(array_filter($args), [ |
|
| 360 | + $args = wp_parse_args( array_filter( $args ), [ |
|
| 361 | 361 | 'post_ids' => [], |
| 362 | 362 | 'term_ids' => [], |
| 363 | 363 | 'type' => 'local', |
| 364 | - ]); |
|
| 365 | - $args['post_ids'] = glsr(Polylang::class)->getPostIds($args['post_ids']); |
|
| 366 | - $args['type'] = $this->normalizeType($args['type']); |
|
| 364 | + ] ); |
|
| 365 | + $args['post_ids'] = glsr( Polylang::class )->getPostIds( $args['post_ids'] ); |
|
| 366 | + $args['type'] = $this->normalizeType( $args['type'] ); |
|
| 367 | 367 | return $args; |
| 368 | 368 | } |
| 369 | 369 | |
@@ -371,9 +371,9 @@ discard block |
||
| 371 | 371 | * @param string $type |
| 372 | 372 | * @return string |
| 373 | 373 | */ |
| 374 | - protected function normalizeType($type) |
|
| 374 | + protected function normalizeType( $type ) |
|
| 375 | 375 | { |
| 376 | - return empty($type) || !is_string($type) |
|
| 376 | + return empty($type) || !is_string( $type ) |
|
| 377 | 377 | ? 'local' |
| 378 | 378 | : $type; |
| 379 | 379 | } |
@@ -382,13 +382,13 @@ discard block |
||
| 382 | 382 | * @param int $lastPostId |
| 383 | 383 | * @return object |
| 384 | 384 | */ |
| 385 | - protected function queryReviews(array $args = [], $lastPostId = 0) |
|
| 385 | + protected function queryReviews( array $args = [], $lastPostId = 0 ) |
|
| 386 | 386 | { |
| 387 | - $reviews = glsr(SqlQueries::class)->getReviewCounts($args, $lastPostId, static::LIMIT); |
|
| 388 | - $hasMore = is_array($reviews) |
|
| 389 | - ? count($reviews) == static::LIMIT |
|
| 387 | + $reviews = glsr( SqlQueries::class )->getReviewCounts( $args, $lastPostId, static::LIMIT ); |
|
| 388 | + $hasMore = is_array( $reviews ) |
|
| 389 | + ? count( $reviews ) == static::LIMIT |
|
| 390 | 390 | : false; |
| 391 | - return (object) [ |
|
| 391 | + return (object)[ |
|
| 392 | 392 | 'has_more' => $hasMore, |
| 393 | 393 | 'reviews' => $reviews, |
| 394 | 394 | ]; |