@@ -6,41 +6,41 @@ |
||
6 | 6 | |
7 | 7 | class CreateReviewDefaults extends Defaults |
8 | 8 | { |
9 | - /** |
|
10 | - * @var array |
|
11 | - */ |
|
12 | - protected $guarded = [ |
|
13 | - 'assigned_to', |
|
14 | - 'content', |
|
15 | - 'date', |
|
16 | - 'pinned', |
|
17 | - 'response', |
|
18 | - 'review_id', |
|
19 | - 'review_type', |
|
20 | - 'title', |
|
21 | - ]; |
|
9 | + /** |
|
10 | + * @var array |
|
11 | + */ |
|
12 | + protected $guarded = [ |
|
13 | + 'assigned_to', |
|
14 | + 'content', |
|
15 | + 'date', |
|
16 | + 'pinned', |
|
17 | + 'response', |
|
18 | + 'review_id', |
|
19 | + 'review_type', |
|
20 | + 'title', |
|
21 | + ]; |
|
22 | 22 | |
23 | - /** |
|
24 | - * @return array |
|
25 | - */ |
|
26 | - protected function defaults() |
|
27 | - { |
|
28 | - return [ |
|
29 | - 'assigned_to' => '', |
|
30 | - 'author' => '', |
|
31 | - 'avatar' => '', |
|
32 | - 'content' => '', |
|
33 | - 'custom' => '', |
|
34 | - 'date' => '', |
|
35 | - 'email' => '', |
|
36 | - 'ip_address' => '', |
|
37 | - 'pinned' => false, |
|
38 | - 'rating' => '', |
|
39 | - 'response' => '', |
|
40 | - 'review_id' => md5(time().mt_rand()), |
|
41 | - 'review_type' => 'local', |
|
42 | - 'title' => '', |
|
43 | - 'url' => '', |
|
44 | - ]; |
|
45 | - } |
|
23 | + /** |
|
24 | + * @return array |
|
25 | + */ |
|
26 | + protected function defaults() |
|
27 | + { |
|
28 | + return [ |
|
29 | + 'assigned_to' => '', |
|
30 | + 'author' => '', |
|
31 | + 'avatar' => '', |
|
32 | + 'content' => '', |
|
33 | + 'custom' => '', |
|
34 | + 'date' => '', |
|
35 | + 'email' => '', |
|
36 | + 'ip_address' => '', |
|
37 | + 'pinned' => false, |
|
38 | + 'rating' => '', |
|
39 | + 'response' => '', |
|
40 | + 'review_id' => md5(time().mt_rand()), |
|
41 | + 'review_type' => 'local', |
|
42 | + 'title' => '', |
|
43 | + 'url' => '', |
|
44 | + ]; |
|
45 | + } |
|
46 | 46 | } |
@@ -6,22 +6,22 @@ |
||
6 | 6 | |
7 | 7 | class ValidateReviewDefaults extends Defaults |
8 | 8 | { |
9 | - /** |
|
10 | - * @return array |
|
11 | - */ |
|
12 | - protected function defaults() |
|
13 | - { |
|
14 | - return [ |
|
15 | - 'assign_to' => '', |
|
16 | - 'category' => '', |
|
17 | - 'content' => '', |
|
18 | - 'email' => '', |
|
19 | - 'form_id' => '', |
|
20 | - 'ip_address' => '', |
|
21 | - 'name' => '', |
|
22 | - 'rating' => '0', |
|
23 | - 'terms' => '', |
|
24 | - 'title' => '', |
|
25 | - ]; |
|
26 | - } |
|
9 | + /** |
|
10 | + * @return array |
|
11 | + */ |
|
12 | + protected function defaults() |
|
13 | + { |
|
14 | + return [ |
|
15 | + 'assign_to' => '', |
|
16 | + 'category' => '', |
|
17 | + 'content' => '', |
|
18 | + 'email' => '', |
|
19 | + 'form_id' => '', |
|
20 | + 'ip_address' => '', |
|
21 | + 'name' => '', |
|
22 | + 'rating' => '0', |
|
23 | + 'terms' => '', |
|
24 | + 'title' => '', |
|
25 | + ]; |
|
26 | + } |
|
27 | 27 | } |
@@ -9,148 +9,148 @@ |
||
9 | 9 | |
10 | 10 | class EnqueuePublicAssets |
11 | 11 | { |
12 | - /** |
|
13 | - * @return void |
|
14 | - */ |
|
15 | - public function handle() |
|
16 | - { |
|
17 | - $this->enqueueAssets(); |
|
18 | - $this->enqueuePolyfillService(); |
|
19 | - $this->enqueueRecaptchaScript(); |
|
20 | - $this->inlineScript(); |
|
21 | - $this->inlineStyles(); |
|
22 | - } |
|
12 | + /** |
|
13 | + * @return void |
|
14 | + */ |
|
15 | + public function handle() |
|
16 | + { |
|
17 | + $this->enqueueAssets(); |
|
18 | + $this->enqueuePolyfillService(); |
|
19 | + $this->enqueueRecaptchaScript(); |
|
20 | + $this->inlineScript(); |
|
21 | + $this->inlineStyles(); |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * @return void |
|
26 | - */ |
|
27 | - public function enqueueAssets() |
|
28 | - { |
|
29 | - if (apply_filters('site-reviews/assets/css', true)) { |
|
30 | - wp_enqueue_style( |
|
31 | - Application::ID, |
|
32 | - $this->getStylesheet(), |
|
33 | - [], |
|
34 | - glsr()->version |
|
35 | - ); |
|
36 | - } |
|
37 | - if (apply_filters('site-reviews/assets/js', true)) { |
|
38 | - $dependencies = apply_filters('site-reviews/assets/polyfill', true) |
|
39 | - ? [Application::ID.'/polyfill'] |
|
40 | - : []; |
|
41 | - $dependencies = apply_filters('site-reviews/enqueue/public/dependencies', $dependencies); |
|
42 | - wp_enqueue_script( |
|
43 | - Application::ID, |
|
44 | - glsr()->url('assets/scripts/'.Application::ID.'.js'), |
|
45 | - $dependencies, |
|
46 | - glsr()->version, |
|
47 | - true |
|
48 | - ); |
|
49 | - } |
|
50 | - } |
|
24 | + /** |
|
25 | + * @return void |
|
26 | + */ |
|
27 | + public function enqueueAssets() |
|
28 | + { |
|
29 | + if (apply_filters('site-reviews/assets/css', true)) { |
|
30 | + wp_enqueue_style( |
|
31 | + Application::ID, |
|
32 | + $this->getStylesheet(), |
|
33 | + [], |
|
34 | + glsr()->version |
|
35 | + ); |
|
36 | + } |
|
37 | + if (apply_filters('site-reviews/assets/js', true)) { |
|
38 | + $dependencies = apply_filters('site-reviews/assets/polyfill', true) |
|
39 | + ? [Application::ID.'/polyfill'] |
|
40 | + : []; |
|
41 | + $dependencies = apply_filters('site-reviews/enqueue/public/dependencies', $dependencies); |
|
42 | + wp_enqueue_script( |
|
43 | + Application::ID, |
|
44 | + glsr()->url('assets/scripts/'.Application::ID.'.js'), |
|
45 | + $dependencies, |
|
46 | + glsr()->version, |
|
47 | + true |
|
48 | + ); |
|
49 | + } |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function enqueuePolyfillService() |
|
56 | - { |
|
57 | - if (!apply_filters('site-reviews/assets/polyfill', true)) { |
|
58 | - return; |
|
59 | - } |
|
60 | - wp_enqueue_script(Application::ID.'/polyfill', add_query_arg([ |
|
61 | - 'features' => 'Array.prototype.findIndex,CustomEvent,Element.prototype.closest,Element.prototype.dataset,Event,XMLHttpRequest,MutationObserver', |
|
62 | - 'flags' => 'gated', |
|
63 | - ], 'https://polyfill.io/v3/polyfill.min.js')); |
|
64 | - } |
|
52 | + /** |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function enqueuePolyfillService() |
|
56 | + { |
|
57 | + if (!apply_filters('site-reviews/assets/polyfill', true)) { |
|
58 | + return; |
|
59 | + } |
|
60 | + wp_enqueue_script(Application::ID.'/polyfill', add_query_arg([ |
|
61 | + 'features' => 'Array.prototype.findIndex,CustomEvent,Element.prototype.closest,Element.prototype.dataset,Event,XMLHttpRequest,MutationObserver', |
|
62 | + 'flags' => 'gated', |
|
63 | + ], 'https://polyfill.io/v3/polyfill.min.js')); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @return void |
|
68 | - */ |
|
69 | - public function enqueueRecaptchaScript() |
|
70 | - { |
|
71 | - // wpforms-recaptcha |
|
72 | - // google-recaptcha |
|
73 | - // nf-google-recaptcha |
|
74 | - if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
75 | - return; |
|
76 | - } |
|
77 | - $language = apply_filters('site-reviews/recaptcha/language', get_locale()); |
|
78 | - wp_enqueue_script(Application::ID.'/google-recaptcha', add_query_arg([ |
|
79 | - 'hl' => $language, |
|
80 | - 'render' => 'explicit', |
|
81 | - ], 'https://www.google.com/recaptcha/api.js')); |
|
82 | - } |
|
66 | + /** |
|
67 | + * @return void |
|
68 | + */ |
|
69 | + public function enqueueRecaptchaScript() |
|
70 | + { |
|
71 | + // wpforms-recaptcha |
|
72 | + // google-recaptcha |
|
73 | + // nf-google-recaptcha |
|
74 | + if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
75 | + return; |
|
76 | + } |
|
77 | + $language = apply_filters('site-reviews/recaptcha/language', get_locale()); |
|
78 | + wp_enqueue_script(Application::ID.'/google-recaptcha', add_query_arg([ |
|
79 | + 'hl' => $language, |
|
80 | + 'render' => 'explicit', |
|
81 | + ], 'https://www.google.com/recaptcha/api.js')); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * @return void |
|
86 | - */ |
|
87 | - public function inlineScript() |
|
88 | - { |
|
89 | - $variables = [ |
|
90 | - 'action' => Application::PREFIX.'action', |
|
91 | - 'ajaxpagination' => $this->getFixedSelectorsForPagination(), |
|
92 | - 'ajaxurl' => admin_url('admin-ajax.php'), |
|
93 | - 'nameprefix' => Application::ID, |
|
94 | - 'validationconfig' => glsr(Style::class)->validation, |
|
95 | - 'validationstrings' => glsr(ValidationStringsDefaults::class)->defaults(), |
|
96 | - ]; |
|
97 | - $variables = apply_filters('site-reviews/enqueue/public/localize', $variables); |
|
98 | - wp_add_inline_script(Application::ID, $this->buildInlineScript($variables), 'before'); |
|
99 | - } |
|
84 | + /** |
|
85 | + * @return void |
|
86 | + */ |
|
87 | + public function inlineScript() |
|
88 | + { |
|
89 | + $variables = [ |
|
90 | + 'action' => Application::PREFIX.'action', |
|
91 | + 'ajaxpagination' => $this->getFixedSelectorsForPagination(), |
|
92 | + 'ajaxurl' => admin_url('admin-ajax.php'), |
|
93 | + 'nameprefix' => Application::ID, |
|
94 | + 'validationconfig' => glsr(Style::class)->validation, |
|
95 | + 'validationstrings' => glsr(ValidationStringsDefaults::class)->defaults(), |
|
96 | + ]; |
|
97 | + $variables = apply_filters('site-reviews/enqueue/public/localize', $variables); |
|
98 | + wp_add_inline_script(Application::ID, $this->buildInlineScript($variables), 'before'); |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * @return void |
|
103 | - */ |
|
104 | - public function inlineStyles() |
|
105 | - { |
|
106 | - $inlineStylesheetPath = glsr()->path('assets/styles/inline-styles.css'); |
|
107 | - if (!apply_filters('site-reviews/assets/css', true)) { |
|
108 | - return; |
|
109 | - } |
|
110 | - if (!file_exists($inlineStylesheetPath)) { |
|
111 | - glsr_log()->error('Inline stylesheet is missing: '.$inlineStylesheetPath); |
|
112 | - return; |
|
113 | - } |
|
114 | - $inlineStylesheetValues = glsr()->config('inline-styles'); |
|
115 | - $stylesheet = str_replace( |
|
116 | - array_keys($inlineStylesheetValues), |
|
117 | - array_values($inlineStylesheetValues), |
|
118 | - file_get_contents($inlineStylesheetPath) |
|
119 | - ); |
|
120 | - wp_add_inline_style(Application::ID, $stylesheet); |
|
121 | - } |
|
101 | + /** |
|
102 | + * @return void |
|
103 | + */ |
|
104 | + public function inlineStyles() |
|
105 | + { |
|
106 | + $inlineStylesheetPath = glsr()->path('assets/styles/inline-styles.css'); |
|
107 | + if (!apply_filters('site-reviews/assets/css', true)) { |
|
108 | + return; |
|
109 | + } |
|
110 | + if (!file_exists($inlineStylesheetPath)) { |
|
111 | + glsr_log()->error('Inline stylesheet is missing: '.$inlineStylesheetPath); |
|
112 | + return; |
|
113 | + } |
|
114 | + $inlineStylesheetValues = glsr()->config('inline-styles'); |
|
115 | + $stylesheet = str_replace( |
|
116 | + array_keys($inlineStylesheetValues), |
|
117 | + array_values($inlineStylesheetValues), |
|
118 | + file_get_contents($inlineStylesheetPath) |
|
119 | + ); |
|
120 | + wp_add_inline_style(Application::ID, $stylesheet); |
|
121 | + } |
|
122 | 122 | |
123 | - /** |
|
124 | - * @return string |
|
125 | - */ |
|
126 | - protected function buildInlineScript(array $variables) |
|
127 | - { |
|
128 | - $script = 'window.hasOwnProperty("GLSR")||(window.GLSR={});'; |
|
129 | - foreach ($variables as $key => $value) { |
|
130 | - $script.= sprintf('GLSR.%s=%s;', $key, json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); |
|
131 | - } |
|
132 | - $pattern = '/\"([^ \-\"]+)\"(:[{\[\"])/'; // removes unnecessary quotes surrounding object keys |
|
133 | - $optimizedScript = preg_replace($pattern, '$1$2', $script); |
|
134 | - return apply_filters('site-reviews/enqueue/public/inline-script', $optimizedScript, $script, $variables); |
|
135 | - } |
|
123 | + /** |
|
124 | + * @return string |
|
125 | + */ |
|
126 | + protected function buildInlineScript(array $variables) |
|
127 | + { |
|
128 | + $script = 'window.hasOwnProperty("GLSR")||(window.GLSR={});'; |
|
129 | + foreach ($variables as $key => $value) { |
|
130 | + $script.= sprintf('GLSR.%s=%s;', $key, json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); |
|
131 | + } |
|
132 | + $pattern = '/\"([^ \-\"]+)\"(:[{\[\"])/'; // removes unnecessary quotes surrounding object keys |
|
133 | + $optimizedScript = preg_replace($pattern, '$1$2', $script); |
|
134 | + return apply_filters('site-reviews/enqueue/public/inline-script', $optimizedScript, $script, $variables); |
|
135 | + } |
|
136 | 136 | |
137 | - /** |
|
138 | - * @return array |
|
139 | - */ |
|
140 | - protected function getFixedSelectorsForPagination() |
|
141 | - { |
|
142 | - $selectors = ['#wpadminbar', '.site-navigation-fixed']; |
|
143 | - return apply_filters('site-reviews/enqueue/public/localize/ajax-pagination', $selectors); |
|
144 | - } |
|
137 | + /** |
|
138 | + * @return array |
|
139 | + */ |
|
140 | + protected function getFixedSelectorsForPagination() |
|
141 | + { |
|
142 | + $selectors = ['#wpadminbar', '.site-navigation-fixed']; |
|
143 | + return apply_filters('site-reviews/enqueue/public/localize/ajax-pagination', $selectors); |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * @return string |
|
148 | - */ |
|
149 | - protected function getStylesheet() |
|
150 | - { |
|
151 | - $currentStyle = glsr(Style::class)->style; |
|
152 | - return file_exists(glsr()->path('assets/styles/custom/'.$currentStyle.'.css')) |
|
153 | - ? glsr()->url('assets/styles/custom/'.$currentStyle.'.css') |
|
154 | - : glsr()->url('assets/styles/'.Application::ID.'.css'); |
|
155 | - } |
|
146 | + /** |
|
147 | + * @return string |
|
148 | + */ |
|
149 | + protected function getStylesheet() |
|
150 | + { |
|
151 | + $currentStyle = glsr(Style::class)->style; |
|
152 | + return file_exists(glsr()->path('assets/styles/custom/'.$currentStyle.'.css')) |
|
153 | + ? glsr()->url('assets/styles/custom/'.$currentStyle.'.css') |
|
154 | + : glsr()->url('assets/styles/'.Application::ID.'.css'); |
|
155 | + } |
|
156 | 156 | } |
@@ -6,23 +6,23 @@ |
||
6 | 6 | |
7 | 7 | class StyleValidationDefaults extends Defaults |
8 | 8 | { |
9 | - /** |
|
10 | - * @return array |
|
11 | - */ |
|
12 | - protected function defaults() |
|
13 | - { |
|
14 | - return [ |
|
15 | - 'error_tag' => 'div', |
|
16 | - 'error_tag_class' => 'glsr-field-error', |
|
17 | - 'field_class' => 'glsr-field', |
|
18 | - 'field_error_class' => 'glsr-has-error', |
|
19 | - 'input_error_class' => 'glsr-is-invalid', |
|
20 | - 'input_valid_class' => 'glsr-is-valid', |
|
21 | - 'message_error_class' => 'glsr-has-errors', |
|
22 | - 'message_initial_class' => 'glsr-is-visible', |
|
23 | - 'message_success_class' => 'glsr-has-success', |
|
24 | - 'message_tag' => 'div', |
|
25 | - 'message_tag_class' => 'glsr-form-message', |
|
26 | - ]; |
|
27 | - } |
|
9 | + /** |
|
10 | + * @return array |
|
11 | + */ |
|
12 | + protected function defaults() |
|
13 | + { |
|
14 | + return [ |
|
15 | + 'error_tag' => 'div', |
|
16 | + 'error_tag_class' => 'glsr-field-error', |
|
17 | + 'field_class' => 'glsr-field', |
|
18 | + 'field_error_class' => 'glsr-has-error', |
|
19 | + 'input_error_class' => 'glsr-is-invalid', |
|
20 | + 'input_valid_class' => 'glsr-is-valid', |
|
21 | + 'message_error_class' => 'glsr-has-errors', |
|
22 | + 'message_initial_class' => 'glsr-is-visible', |
|
23 | + 'message_success_class' => 'glsr-has-success', |
|
24 | + 'message_tag' => 'div', |
|
25 | + 'message_tag_class' => 'glsr-form-message', |
|
26 | + ]; |
|
27 | + } |
|
28 | 28 | } |
@@ -4,207 +4,207 @@ |
||
4 | 4 | |
5 | 5 | class Rating |
6 | 6 | { |
7 | - /** |
|
8 | - * The more sure we are of the confidence interval (the higher the confidence level), the less |
|
9 | - * precise the estimation will be as the margin for error will be higher. |
|
10 | - * @see http://homepages.math.uic.edu/~bpower6/stat101/Confidence%20Intervals.pdf |
|
11 | - * @see https://www.thecalculator.co/math/Confidence-Interval-Calculator-210.html |
|
12 | - * @see https://www.youtube.com/watch?v=grodoLzThy4 |
|
13 | - * @see https://en.wikipedia.org/wiki/Standard_score |
|
14 | - * @var array |
|
15 | - */ |
|
16 | - const CONFIDENCE_LEVEL_Z_SCORES = [ |
|
17 | - 50 => 0.67449, |
|
18 | - 70 => 1.04, |
|
19 | - 75 => 1.15035, |
|
20 | - 80 => 1.282, |
|
21 | - 85 => 1.44, |
|
22 | - 90 => 1.64485, |
|
23 | - 92 => 1.75, |
|
24 | - 95 => 1.95996, |
|
25 | - 96 => 2.05, |
|
26 | - 97 => 2.17009, |
|
27 | - 98 => 2.326, |
|
28 | - 99 => 2.57583, |
|
29 | - '99.5' => 2.81, |
|
30 | - '99.8' => 3.08, |
|
31 | - '99.9' => 3.29053, |
|
32 | - ]; |
|
7 | + /** |
|
8 | + * The more sure we are of the confidence interval (the higher the confidence level), the less |
|
9 | + * precise the estimation will be as the margin for error will be higher. |
|
10 | + * @see http://homepages.math.uic.edu/~bpower6/stat101/Confidence%20Intervals.pdf |
|
11 | + * @see https://www.thecalculator.co/math/Confidence-Interval-Calculator-210.html |
|
12 | + * @see https://www.youtube.com/watch?v=grodoLzThy4 |
|
13 | + * @see https://en.wikipedia.org/wiki/Standard_score |
|
14 | + * @var array |
|
15 | + */ |
|
16 | + const CONFIDENCE_LEVEL_Z_SCORES = [ |
|
17 | + 50 => 0.67449, |
|
18 | + 70 => 1.04, |
|
19 | + 75 => 1.15035, |
|
20 | + 80 => 1.282, |
|
21 | + 85 => 1.44, |
|
22 | + 90 => 1.64485, |
|
23 | + 92 => 1.75, |
|
24 | + 95 => 1.95996, |
|
25 | + 96 => 2.05, |
|
26 | + 97 => 2.17009, |
|
27 | + 98 => 2.326, |
|
28 | + 99 => 2.57583, |
|
29 | + '99.5' => 2.81, |
|
30 | + '99.8' => 3.08, |
|
31 | + '99.9' => 3.29053, |
|
32 | + ]; |
|
33 | 33 | |
34 | - /** |
|
35 | - * @var int |
|
36 | - */ |
|
37 | - const MAX_RATING = 5; |
|
34 | + /** |
|
35 | + * @var int |
|
36 | + */ |
|
37 | + const MAX_RATING = 5; |
|
38 | 38 | |
39 | - /** |
|
40 | - * @var int |
|
41 | - */ |
|
42 | - const MIN_RATING = 1; |
|
39 | + /** |
|
40 | + * @var int |
|
41 | + */ |
|
42 | + const MIN_RATING = 1; |
|
43 | 43 | |
44 | - /** |
|
45 | - * @param int $roundBy |
|
46 | - * @return float |
|
47 | - */ |
|
48 | - public function getAverage(array $ratingCounts, $roundBy = 1) |
|
49 | - { |
|
50 | - $average = array_sum($ratingCounts); |
|
51 | - if ($average > 0) { |
|
52 | - $average = $this->getTotalSum($ratingCounts) / $average; |
|
53 | - } |
|
54 | - $roundedAverage = round($average, intval($roundBy)); |
|
55 | - return floatval(apply_filters('site-reviews/rating/average', $roundedAverage, $ratingCounts, $average)); |
|
56 | - } |
|
44 | + /** |
|
45 | + * @param int $roundBy |
|
46 | + * @return float |
|
47 | + */ |
|
48 | + public function getAverage(array $ratingCounts, $roundBy = 1) |
|
49 | + { |
|
50 | + $average = array_sum($ratingCounts); |
|
51 | + if ($average > 0) { |
|
52 | + $average = $this->getTotalSum($ratingCounts) / $average; |
|
53 | + } |
|
54 | + $roundedAverage = round($average, intval($roundBy)); |
|
55 | + return floatval(apply_filters('site-reviews/rating/average', $roundedAverage, $ratingCounts, $average)); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Get the lower bound for up/down ratings |
|
60 | - * Method receives an up/down ratings array: [1, -1, -1, 1, 1, -1]. |
|
61 | - * @see http://www.evanmiller.org/how-not-to-sort-by-average-rating.html |
|
62 | - * @see https://news.ycombinator.com/item?id=10481507 |
|
63 | - * @see https://dataorigami.net/blogs/napkin-folding/79030467-an-algorithm-to-sort-top-comments |
|
64 | - * @see http://julesjacobs.github.io/2015/08/17/bayesian-scoring-of-ratings.html |
|
65 | - * @param int $confidencePercentage |
|
66 | - * @return int|float |
|
67 | - */ |
|
68 | - public function getLowerBound(array $upDownCounts = [0, 0], $confidencePercentage = 95) |
|
69 | - { |
|
70 | - $numRatings = array_sum($upDownCounts); |
|
71 | - if ($numRatings < 1) { |
|
72 | - return 0; |
|
73 | - } |
|
74 | - $z = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
|
75 | - $phat = 1 * $upDownCounts[1] / $numRatings; |
|
76 | - return ($phat + $z * $z / (2 * $numRatings) - $z * sqrt(($phat * (1 - $phat) + $z * $z / (4 * $numRatings)) / $numRatings)) / (1 + $z * $z / $numRatings); |
|
77 | - } |
|
58 | + /** |
|
59 | + * Get the lower bound for up/down ratings |
|
60 | + * Method receives an up/down ratings array: [1, -1, -1, 1, 1, -1]. |
|
61 | + * @see http://www.evanmiller.org/how-not-to-sort-by-average-rating.html |
|
62 | + * @see https://news.ycombinator.com/item?id=10481507 |
|
63 | + * @see https://dataorigami.net/blogs/napkin-folding/79030467-an-algorithm-to-sort-top-comments |
|
64 | + * @see http://julesjacobs.github.io/2015/08/17/bayesian-scoring-of-ratings.html |
|
65 | + * @param int $confidencePercentage |
|
66 | + * @return int|float |
|
67 | + */ |
|
68 | + public function getLowerBound(array $upDownCounts = [0, 0], $confidencePercentage = 95) |
|
69 | + { |
|
70 | + $numRatings = array_sum($upDownCounts); |
|
71 | + if ($numRatings < 1) { |
|
72 | + return 0; |
|
73 | + } |
|
74 | + $z = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
|
75 | + $phat = 1 * $upDownCounts[1] / $numRatings; |
|
76 | + return ($phat + $z * $z / (2 * $numRatings) - $z * sqrt(($phat * (1 - $phat) + $z * $z / (4 * $numRatings)) / $numRatings)) / (1 + $z * $z / $numRatings); |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @return int|float |
|
81 | - */ |
|
82 | - public function getOverallPercentage(array $ratingCounts) |
|
83 | - { |
|
84 | - return round($this->getAverage($ratingCounts) * 100 / glsr()->constant('MAX_RATING', __CLASS__), 2); |
|
85 | - } |
|
79 | + /** |
|
80 | + * @return int|float |
|
81 | + */ |
|
82 | + public function getOverallPercentage(array $ratingCounts) |
|
83 | + { |
|
84 | + return round($this->getAverage($ratingCounts) * 100 / glsr()->constant('MAX_RATING', __CLASS__), 2); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * @return array |
|
89 | - */ |
|
90 | - public function getPercentages(array $ratingCounts) |
|
91 | - { |
|
92 | - $total = array_sum($ratingCounts); |
|
93 | - foreach ($ratingCounts as $index => $count) { |
|
94 | - if (empty($count)) { |
|
95 | - continue; |
|
96 | - } |
|
97 | - $ratingCounts[$index] = $count / $total * 100; |
|
98 | - } |
|
99 | - return $this->getRoundedPercentages($ratingCounts); |
|
100 | - } |
|
87 | + /** |
|
88 | + * @return array |
|
89 | + */ |
|
90 | + public function getPercentages(array $ratingCounts) |
|
91 | + { |
|
92 | + $total = array_sum($ratingCounts); |
|
93 | + foreach ($ratingCounts as $index => $count) { |
|
94 | + if (empty($count)) { |
|
95 | + continue; |
|
96 | + } |
|
97 | + $ratingCounts[$index] = $count / $total * 100; |
|
98 | + } |
|
99 | + return $this->getRoundedPercentages($ratingCounts); |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * @return float |
|
104 | - */ |
|
105 | - public function getRanking(array $ratingCounts) |
|
106 | - { |
|
107 | - return floatval(apply_filters('site-reviews/rating/ranking', |
|
108 | - $this->getRankingUsingImdb($ratingCounts), |
|
109 | - $ratingCounts, |
|
110 | - $this |
|
111 | - )); |
|
112 | - } |
|
102 | + /** |
|
103 | + * @return float |
|
104 | + */ |
|
105 | + public function getRanking(array $ratingCounts) |
|
106 | + { |
|
107 | + return floatval(apply_filters('site-reviews/rating/ranking', |
|
108 | + $this->getRankingUsingImdb($ratingCounts), |
|
109 | + $ratingCounts, |
|
110 | + $this |
|
111 | + )); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Get the bayesian ranking for an array of reviews |
|
116 | - * This formula is the same one used by IMDB to rank their top 250 films. |
|
117 | - * @see https://www.xkcd.com/937/ |
|
118 | - * @see https://districtdatalabs.silvrback.com/computing-a-bayesian-estimate-of-star-rating-means |
|
119 | - * @see http://fulmicoton.com/posts/bayesian_rating/ |
|
120 | - * @see https://stats.stackexchange.com/questions/93974/is-there-an-equivalent-to-lower-bound-of-wilson-score-confidence-interval-for-va |
|
121 | - * @param int $confidencePercentage |
|
122 | - * @return int|float |
|
123 | - */ |
|
124 | - public function getRankingUsingImdb(array $ratingCounts, $confidencePercentage = 70) |
|
125 | - { |
|
126 | - $avgRating = $this->getAverage($ratingCounts); |
|
127 | - // Represents a prior (your prior opinion without data) for the average star rating. A higher prior also means a higher margin for error. |
|
128 | - // This could also be the average score of all items instead of a fixed value. |
|
129 | - $bayesMean = ($confidencePercentage / 100) * glsr()->constant('MAX_RATING', __CLASS__); // prior, 70% = 3.5 |
|
130 | - // Represents the number of ratings expected to begin observing a pattern that would put confidence in the prior. |
|
131 | - $bayesMinimal = 10; // confidence |
|
132 | - $numOfReviews = array_sum($ratingCounts); |
|
133 | - return $avgRating > 0 |
|
134 | - ? (($bayesMinimal * $bayesMean) + ($avgRating * $numOfReviews)) / ($bayesMinimal + $numOfReviews) |
|
135 | - : 0; |
|
136 | - } |
|
114 | + /** |
|
115 | + * Get the bayesian ranking for an array of reviews |
|
116 | + * This formula is the same one used by IMDB to rank their top 250 films. |
|
117 | + * @see https://www.xkcd.com/937/ |
|
118 | + * @see https://districtdatalabs.silvrback.com/computing-a-bayesian-estimate-of-star-rating-means |
|
119 | + * @see http://fulmicoton.com/posts/bayesian_rating/ |
|
120 | + * @see https://stats.stackexchange.com/questions/93974/is-there-an-equivalent-to-lower-bound-of-wilson-score-confidence-interval-for-va |
|
121 | + * @param int $confidencePercentage |
|
122 | + * @return int|float |
|
123 | + */ |
|
124 | + public function getRankingUsingImdb(array $ratingCounts, $confidencePercentage = 70) |
|
125 | + { |
|
126 | + $avgRating = $this->getAverage($ratingCounts); |
|
127 | + // Represents a prior (your prior opinion without data) for the average star rating. A higher prior also means a higher margin for error. |
|
128 | + // This could also be the average score of all items instead of a fixed value. |
|
129 | + $bayesMean = ($confidencePercentage / 100) * glsr()->constant('MAX_RATING', __CLASS__); // prior, 70% = 3.5 |
|
130 | + // Represents the number of ratings expected to begin observing a pattern that would put confidence in the prior. |
|
131 | + $bayesMinimal = 10; // confidence |
|
132 | + $numOfReviews = array_sum($ratingCounts); |
|
133 | + return $avgRating > 0 |
|
134 | + ? (($bayesMinimal * $bayesMean) + ($avgRating * $numOfReviews)) / ($bayesMinimal + $numOfReviews) |
|
135 | + : 0; |
|
136 | + } |
|
137 | 137 | |
138 | - /** |
|
139 | - * The quality of a 5 star rating depends not only on the average number of stars but also on |
|
140 | - * the number of reviews. This method calculates the bayesian ranking of a page by its number |
|
141 | - * of reviews and their rating. |
|
142 | - * @see http://www.evanmiller.org/ranking-items-with-star-ratings.html |
|
143 | - * @see https://stackoverflow.com/questions/1411199/what-is-a-better-way-to-sort-by-a-5-star-rating/1411268 |
|
144 | - * @see http://julesjacobs.github.io/2015/08/17/bayesian-scoring-of-ratings.html |
|
145 | - * @param int $confidencePercentage |
|
146 | - * @return float |
|
147 | - */ |
|
148 | - public function getRankingUsingZScores(array $ratingCounts, $confidencePercentage = 90) |
|
149 | - { |
|
150 | - $ratingCountsSum = array_sum($ratingCounts) + glsr()->constant('MAX_RATING', __CLASS__); |
|
151 | - $weight = $this->getWeight($ratingCounts, $ratingCountsSum); |
|
152 | - $weightPow2 = $this->getWeight($ratingCounts, $ratingCountsSum, true); |
|
153 | - $zScore = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
|
154 | - return $weight - $zScore * sqrt(($weightPow2 - pow($weight, 2)) / ($ratingCountsSum + 1)); |
|
155 | - } |
|
138 | + /** |
|
139 | + * The quality of a 5 star rating depends not only on the average number of stars but also on |
|
140 | + * the number of reviews. This method calculates the bayesian ranking of a page by its number |
|
141 | + * of reviews and their rating. |
|
142 | + * @see http://www.evanmiller.org/ranking-items-with-star-ratings.html |
|
143 | + * @see https://stackoverflow.com/questions/1411199/what-is-a-better-way-to-sort-by-a-5-star-rating/1411268 |
|
144 | + * @see http://julesjacobs.github.io/2015/08/17/bayesian-scoring-of-ratings.html |
|
145 | + * @param int $confidencePercentage |
|
146 | + * @return float |
|
147 | + */ |
|
148 | + public function getRankingUsingZScores(array $ratingCounts, $confidencePercentage = 90) |
|
149 | + { |
|
150 | + $ratingCountsSum = array_sum($ratingCounts) + glsr()->constant('MAX_RATING', __CLASS__); |
|
151 | + $weight = $this->getWeight($ratingCounts, $ratingCountsSum); |
|
152 | + $weightPow2 = $this->getWeight($ratingCounts, $ratingCountsSum, true); |
|
153 | + $zScore = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
|
154 | + return $weight - $zScore * sqrt(($weightPow2 - pow($weight, 2)) / ($ratingCountsSum + 1)); |
|
155 | + } |
|
156 | 156 | |
157 | - /** |
|
158 | - * @param int $target |
|
159 | - * @return array |
|
160 | - */ |
|
161 | - protected function getRoundedPercentages(array $percentages, $totalPercent = 100) |
|
162 | - { |
|
163 | - array_walk($percentages, function (&$percent, $index) { |
|
164 | - $percent = [ |
|
165 | - 'index' => $index, |
|
166 | - 'percent' => floor($percent), |
|
167 | - 'remainder' => fmod($percent, 1), |
|
168 | - ]; |
|
169 | - }); |
|
170 | - $indexes = glsr_array_column($percentages, 'index'); |
|
171 | - $remainders = glsr_array_column($percentages, 'remainder'); |
|
172 | - array_multisort($remainders, SORT_DESC, SORT_STRING, $indexes, SORT_DESC, $percentages); |
|
173 | - $i = 0; |
|
174 | - if (array_sum(glsr_array_column($percentages, 'percent')) > 0) { |
|
175 | - while (array_sum(glsr_array_column($percentages, 'percent')) < $totalPercent) { |
|
176 | - ++$percentages[$i]['percent']; |
|
177 | - ++$i; |
|
178 | - } |
|
179 | - } |
|
180 | - array_multisort($indexes, SORT_DESC, $percentages); |
|
181 | - return array_combine($indexes, glsr_array_column($percentages, 'percent')); |
|
182 | - } |
|
157 | + /** |
|
158 | + * @param int $target |
|
159 | + * @return array |
|
160 | + */ |
|
161 | + protected function getRoundedPercentages(array $percentages, $totalPercent = 100) |
|
162 | + { |
|
163 | + array_walk($percentages, function (&$percent, $index) { |
|
164 | + $percent = [ |
|
165 | + 'index' => $index, |
|
166 | + 'percent' => floor($percent), |
|
167 | + 'remainder' => fmod($percent, 1), |
|
168 | + ]; |
|
169 | + }); |
|
170 | + $indexes = glsr_array_column($percentages, 'index'); |
|
171 | + $remainders = glsr_array_column($percentages, 'remainder'); |
|
172 | + array_multisort($remainders, SORT_DESC, SORT_STRING, $indexes, SORT_DESC, $percentages); |
|
173 | + $i = 0; |
|
174 | + if (array_sum(glsr_array_column($percentages, 'percent')) > 0) { |
|
175 | + while (array_sum(glsr_array_column($percentages, 'percent')) < $totalPercent) { |
|
176 | + ++$percentages[$i]['percent']; |
|
177 | + ++$i; |
|
178 | + } |
|
179 | + } |
|
180 | + array_multisort($indexes, SORT_DESC, $percentages); |
|
181 | + return array_combine($indexes, glsr_array_column($percentages, 'percent')); |
|
182 | + } |
|
183 | 183 | |
184 | - /** |
|
185 | - * @return int |
|
186 | - */ |
|
187 | - protected function getTotalSum(array $ratingCounts) |
|
188 | - { |
|
189 | - return array_reduce(array_keys($ratingCounts), function ($carry, $index) use ($ratingCounts) { |
|
190 | - return $carry + ($index * $ratingCounts[$index]); |
|
191 | - }); |
|
192 | - } |
|
184 | + /** |
|
185 | + * @return int |
|
186 | + */ |
|
187 | + protected function getTotalSum(array $ratingCounts) |
|
188 | + { |
|
189 | + return array_reduce(array_keys($ratingCounts), function ($carry, $index) use ($ratingCounts) { |
|
190 | + return $carry + ($index * $ratingCounts[$index]); |
|
191 | + }); |
|
192 | + } |
|
193 | 193 | |
194 | - /** |
|
195 | - * @param int|float $ratingCountsSum |
|
196 | - * @param bool $powerOf2 |
|
197 | - * @return float |
|
198 | - */ |
|
199 | - protected function getWeight(array $ratingCounts, $ratingCountsSum, $powerOf2 = false) |
|
200 | - { |
|
201 | - return array_reduce(array_keys($ratingCounts), |
|
202 | - function ($count, $rating) use ($ratingCounts, $ratingCountsSum, $powerOf2) { |
|
203 | - $ratingLevel = $powerOf2 |
|
204 | - ? pow($rating, 2) |
|
205 | - : $rating; |
|
206 | - return $count + ($ratingLevel * ($ratingCounts[$rating] + 1)) / $ratingCountsSum; |
|
207 | - } |
|
208 | - ); |
|
209 | - } |
|
194 | + /** |
|
195 | + * @param int|float $ratingCountsSum |
|
196 | + * @param bool $powerOf2 |
|
197 | + * @return float |
|
198 | + */ |
|
199 | + protected function getWeight(array $ratingCounts, $ratingCountsSum, $powerOf2 = false) |
|
200 | + { |
|
201 | + return array_reduce(array_keys($ratingCounts), |
|
202 | + function ($count, $rating) use ($ratingCounts, $ratingCountsSum, $powerOf2) { |
|
203 | + $ratingLevel = $powerOf2 |
|
204 | + ? pow($rating, 2) |
|
205 | + : $rating; |
|
206 | + return $count + ($ratingLevel * ($ratingCounts[$rating] + 1)) / $ratingCountsSum; |
|
207 | + } |
|
208 | + ); |
|
209 | + } |
|
210 | 210 | } |
@@ -12,168 +12,168 @@ |
||
12 | 12 | |
13 | 13 | class Database |
14 | 14 | { |
15 | - /** |
|
16 | - * @param int $postId |
|
17 | - * @param string $key |
|
18 | - * @param bool $single |
|
19 | - * @return mixed |
|
20 | - */ |
|
21 | - public function get($postId, $key, $single = true) |
|
22 | - { |
|
23 | - $key = Str::prefix('_', $key); |
|
24 | - return get_post_meta(intval($postId), $key, $single); |
|
25 | - } |
|
15 | + /** |
|
16 | + * @param int $postId |
|
17 | + * @param string $key |
|
18 | + * @param bool $single |
|
19 | + * @return mixed |
|
20 | + */ |
|
21 | + public function get($postId, $key, $single = true) |
|
22 | + { |
|
23 | + $key = Str::prefix('_', $key); |
|
24 | + return get_post_meta(intval($postId), $key, $single); |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * @param int $postId |
|
29 | - * @param string $assignedTo |
|
30 | - * @return void|WP_Post |
|
31 | - */ |
|
32 | - public function getAssignedToPost($postId, $assignedTo = '') |
|
33 | - { |
|
34 | - if (empty($assignedTo)) { |
|
35 | - $assignedTo = $this->get($postId, 'assigned_to'); |
|
36 | - } |
|
37 | - if (empty($assignedTo)) { |
|
38 | - return; |
|
39 | - } |
|
40 | - $assignedPost = get_post($assignedTo); |
|
41 | - if ($assignedPost instanceof WP_Post && $assignedPost->ID != $postId) { |
|
42 | - return $assignedPost; |
|
43 | - } |
|
44 | - } |
|
27 | + /** |
|
28 | + * @param int $postId |
|
29 | + * @param string $assignedTo |
|
30 | + * @return void|WP_Post |
|
31 | + */ |
|
32 | + public function getAssignedToPost($postId, $assignedTo = '') |
|
33 | + { |
|
34 | + if (empty($assignedTo)) { |
|
35 | + $assignedTo = $this->get($postId, 'assigned_to'); |
|
36 | + } |
|
37 | + if (empty($assignedTo)) { |
|
38 | + return; |
|
39 | + } |
|
40 | + $assignedPost = get_post($assignedTo); |
|
41 | + if ($assignedPost instanceof WP_Post && $assignedPost->ID != $postId) { |
|
42 | + return $assignedPost; |
|
43 | + } |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * @param string $metaKey |
|
48 | - * @param string $metaValue |
|
49 | - * @return array|int |
|
50 | - */ |
|
51 | - public function getReviewCount($metaKey = '', $metaValue = '') |
|
52 | - { |
|
53 | - if (!$metaKey) { |
|
54 | - return (array) wp_count_posts(Application::POST_TYPE); |
|
55 | - } |
|
56 | - $counts = glsr(Cache::class)->getReviewCountsFor($metaKey); |
|
57 | - if (!$metaValue) { |
|
58 | - return $counts; |
|
59 | - } |
|
60 | - return Arr::get($counts, $metaValue, 0); |
|
61 | - } |
|
46 | + /** |
|
47 | + * @param string $metaKey |
|
48 | + * @param string $metaValue |
|
49 | + * @return array|int |
|
50 | + */ |
|
51 | + public function getReviewCount($metaKey = '', $metaValue = '') |
|
52 | + { |
|
53 | + if (!$metaKey) { |
|
54 | + return (array) wp_count_posts(Application::POST_TYPE); |
|
55 | + } |
|
56 | + $counts = glsr(Cache::class)->getReviewCountsFor($metaKey); |
|
57 | + if (!$metaValue) { |
|
58 | + return $counts; |
|
59 | + } |
|
60 | + return Arr::get($counts, $metaValue, 0); |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * @param string $metaReviewType |
|
65 | - * @return array |
|
66 | - */ |
|
67 | - public function getReviewIdsByType($metaReviewType) |
|
68 | - { |
|
69 | - return glsr(SqlQueries::class)->getReviewIdsByType($metaReviewType); |
|
70 | - } |
|
63 | + /** |
|
64 | + * @param string $metaReviewType |
|
65 | + * @return array |
|
66 | + */ |
|
67 | + public function getReviewIdsByType($metaReviewType) |
|
68 | + { |
|
69 | + return glsr(SqlQueries::class)->getReviewIdsByType($metaReviewType); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * @param string $key |
|
74 | - * @param string $status |
|
75 | - * @return array |
|
76 | - */ |
|
77 | - public function getReviewsMeta($key, $status = 'publish') |
|
78 | - { |
|
79 | - return glsr(SqlQueries::class)->getReviewsMeta($key, $status); |
|
80 | - } |
|
72 | + /** |
|
73 | + * @param string $key |
|
74 | + * @param string $status |
|
75 | + * @return array |
|
76 | + */ |
|
77 | + public function getReviewsMeta($key, $status = 'publish') |
|
78 | + { |
|
79 | + return glsr(SqlQueries::class)->getReviewsMeta($key, $status); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @param string $field |
|
84 | - * @return array |
|
85 | - */ |
|
86 | - public function getTermIds(array $values, $field) |
|
87 | - { |
|
88 | - $termIds = []; |
|
89 | - foreach ($values as $value) { |
|
90 | - $term = get_term_by($field, $value, Application::TAXONOMY); |
|
91 | - if (!isset($term->term_id)) { |
|
92 | - continue; |
|
93 | - } |
|
94 | - $termIds[] = $term->term_id; |
|
95 | - } |
|
96 | - return $termIds; |
|
97 | - } |
|
82 | + /** |
|
83 | + * @param string $field |
|
84 | + * @return array |
|
85 | + */ |
|
86 | + public function getTermIds(array $values, $field) |
|
87 | + { |
|
88 | + $termIds = []; |
|
89 | + foreach ($values as $value) { |
|
90 | + $term = get_term_by($field, $value, Application::TAXONOMY); |
|
91 | + if (!isset($term->term_id)) { |
|
92 | + continue; |
|
93 | + } |
|
94 | + $termIds[] = $term->term_id; |
|
95 | + } |
|
96 | + return $termIds; |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * @return array |
|
101 | - */ |
|
102 | - public function getTerms(array $args = []) |
|
103 | - { |
|
104 | - $args = wp_parse_args($args, [ |
|
105 | - 'count' => false, |
|
106 | - 'fields' => 'id=>name', |
|
107 | - 'hide_empty' => false, |
|
108 | - 'taxonomy' => Application::TAXONOMY, |
|
109 | - ]); |
|
110 | - $terms = get_terms($args); |
|
111 | - if (is_wp_error($terms)) { |
|
112 | - glsr_log()->error($terms->get_error_message()); |
|
113 | - return []; |
|
114 | - } |
|
115 | - return $terms; |
|
116 | - } |
|
99 | + /** |
|
100 | + * @return array |
|
101 | + */ |
|
102 | + public function getTerms(array $args = []) |
|
103 | + { |
|
104 | + $args = wp_parse_args($args, [ |
|
105 | + 'count' => false, |
|
106 | + 'fields' => 'id=>name', |
|
107 | + 'hide_empty' => false, |
|
108 | + 'taxonomy' => Application::TAXONOMY, |
|
109 | + ]); |
|
110 | + $terms = get_terms($args); |
|
111 | + if (is_wp_error($terms)) { |
|
112 | + glsr_log()->error($terms->get_error_message()); |
|
113 | + return []; |
|
114 | + } |
|
115 | + return $terms; |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * @param string $searchTerm |
|
120 | - * @return void|string |
|
121 | - */ |
|
122 | - public function searchPosts($searchTerm) |
|
123 | - { |
|
124 | - $args = [ |
|
125 | - 'post_status' => 'publish', |
|
126 | - 'post_type' => 'any', |
|
127 | - ]; |
|
128 | - if (is_numeric($searchTerm)) { |
|
129 | - $args['post__in'] = [$searchTerm]; |
|
130 | - } else { |
|
131 | - $args['orderby'] = 'relevance'; |
|
132 | - $args['posts_per_page'] = 10; |
|
133 | - $args['s'] = $searchTerm; |
|
134 | - } |
|
135 | - $queryBuilder = glsr(QueryBuilder::class); |
|
136 | - add_filter('posts_search', [$queryBuilder, 'filterSearchByTitle'], 500, 2); |
|
137 | - $search = new WP_Query($args); |
|
138 | - remove_filter('posts_search', [$queryBuilder, 'filterSearchByTitle'], 500); |
|
139 | - if (!$search->have_posts()) { |
|
140 | - return; |
|
141 | - } |
|
142 | - $results = ''; |
|
143 | - while ($search->have_posts()) { |
|
144 | - $search->the_post(); |
|
145 | - ob_start(); |
|
146 | - glsr()->render('partials/editor/search-result', [ |
|
147 | - 'ID' => get_the_ID(), |
|
148 | - 'permalink' => esc_url((string) get_permalink()), |
|
149 | - 'title' => esc_attr(get_the_title()), |
|
150 | - ]); |
|
151 | - $results.= ob_get_clean(); |
|
152 | - } |
|
153 | - wp_reset_postdata(); |
|
154 | - return $results; |
|
155 | - } |
|
118 | + /** |
|
119 | + * @param string $searchTerm |
|
120 | + * @return void|string |
|
121 | + */ |
|
122 | + public function searchPosts($searchTerm) |
|
123 | + { |
|
124 | + $args = [ |
|
125 | + 'post_status' => 'publish', |
|
126 | + 'post_type' => 'any', |
|
127 | + ]; |
|
128 | + if (is_numeric($searchTerm)) { |
|
129 | + $args['post__in'] = [$searchTerm]; |
|
130 | + } else { |
|
131 | + $args['orderby'] = 'relevance'; |
|
132 | + $args['posts_per_page'] = 10; |
|
133 | + $args['s'] = $searchTerm; |
|
134 | + } |
|
135 | + $queryBuilder = glsr(QueryBuilder::class); |
|
136 | + add_filter('posts_search', [$queryBuilder, 'filterSearchByTitle'], 500, 2); |
|
137 | + $search = new WP_Query($args); |
|
138 | + remove_filter('posts_search', [$queryBuilder, 'filterSearchByTitle'], 500); |
|
139 | + if (!$search->have_posts()) { |
|
140 | + return; |
|
141 | + } |
|
142 | + $results = ''; |
|
143 | + while ($search->have_posts()) { |
|
144 | + $search->the_post(); |
|
145 | + ob_start(); |
|
146 | + glsr()->render('partials/editor/search-result', [ |
|
147 | + 'ID' => get_the_ID(), |
|
148 | + 'permalink' => esc_url((string) get_permalink()), |
|
149 | + 'title' => esc_attr(get_the_title()), |
|
150 | + ]); |
|
151 | + $results.= ob_get_clean(); |
|
152 | + } |
|
153 | + wp_reset_postdata(); |
|
154 | + return $results; |
|
155 | + } |
|
156 | 156 | |
157 | - /** |
|
158 | - * @param int $postId |
|
159 | - * @param string $key |
|
160 | - * @param mixed $value |
|
161 | - * @return int|bool |
|
162 | - */ |
|
163 | - public function set($postId, $key, $value) |
|
164 | - { |
|
165 | - $key = Str::prefix('_', $key); |
|
166 | - return update_post_meta($postId, $key, $value); |
|
167 | - } |
|
157 | + /** |
|
158 | + * @param int $postId |
|
159 | + * @param string $key |
|
160 | + * @param mixed $value |
|
161 | + * @return int|bool |
|
162 | + */ |
|
163 | + public function set($postId, $key, $value) |
|
164 | + { |
|
165 | + $key = Str::prefix('_', $key); |
|
166 | + return update_post_meta($postId, $key, $value); |
|
167 | + } |
|
168 | 168 | |
169 | - /** |
|
170 | - * @param int $postId |
|
171 | - * @param string $key |
|
172 | - * @param mixed $value |
|
173 | - * @return int|bool |
|
174 | - */ |
|
175 | - public function update($postId, $key, $value) |
|
176 | - { |
|
177 | - return $this->set($postId, $key, $value); |
|
178 | - } |
|
169 | + /** |
|
170 | + * @param int $postId |
|
171 | + * @param string $key |
|
172 | + * @param mixed $value |
|
173 | + * @return int|bool |
|
174 | + */ |
|
175 | + public function update($postId, $key, $value) |
|
176 | + { |
|
177 | + return $this->set($postId, $key, $value); |
|
178 | + } |
|
179 | 179 | } |
@@ -14,52 +14,52 @@ |
||
14 | 14 | */ |
15 | 15 | class Multilingual |
16 | 16 | { |
17 | - protected $integration; |
|
17 | + protected $integration; |
|
18 | 18 | |
19 | - /** |
|
20 | - * @param string $method |
|
21 | - * @param array $args |
|
22 | - * @return |
|
23 | - */ |
|
24 | - public function __call($method, $args = []) |
|
25 | - { |
|
26 | - if ($this->isIntegrated() && method_exists($this->integration, $method)) { |
|
27 | - return call_user_func_array([$this->integration, $method], $args); |
|
28 | - } |
|
29 | - return Arr::get($args, 0, false); |
|
30 | - } |
|
19 | + /** |
|
20 | + * @param string $method |
|
21 | + * @param array $args |
|
22 | + * @return |
|
23 | + */ |
|
24 | + public function __call($method, $args = []) |
|
25 | + { |
|
26 | + if ($this->isIntegrated() && method_exists($this->integration, $method)) { |
|
27 | + return call_user_func_array([$this->integration, $method], $args); |
|
28 | + } |
|
29 | + return Arr::get($args, 0, false); |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * @param string $integration |
|
34 | - * @return false|\GeminiLabs\SiteReviews\Modules\Multilingual\Polylang|\GeminiLabs\SiteReviews\Modules\Multilingual\Wpml |
|
35 | - */ |
|
36 | - public function getIntegration($integration = '') |
|
37 | - { |
|
38 | - if (empty($integration)) { |
|
39 | - $integration = glsr(OptionManager::class)->get('settings.general.multilingual'); |
|
40 | - } |
|
41 | - if (!empty($integration)) { |
|
42 | - $integrationClass = 'GeminiLabs\SiteReviews\Modules\Multilingual\\'.ucfirst($integration); |
|
43 | - if (class_exists($integrationClass)) { |
|
44 | - return glsr($integrationClass); |
|
45 | - } |
|
46 | - glsr_log()->error($integrationClass.' does not exist'); |
|
47 | - } |
|
48 | - return false; |
|
49 | - } |
|
32 | + /** |
|
33 | + * @param string $integration |
|
34 | + * @return false|\GeminiLabs\SiteReviews\Modules\Multilingual\Polylang|\GeminiLabs\SiteReviews\Modules\Multilingual\Wpml |
|
35 | + */ |
|
36 | + public function getIntegration($integration = '') |
|
37 | + { |
|
38 | + if (empty($integration)) { |
|
39 | + $integration = glsr(OptionManager::class)->get('settings.general.multilingual'); |
|
40 | + } |
|
41 | + if (!empty($integration)) { |
|
42 | + $integrationClass = 'GeminiLabs\SiteReviews\Modules\Multilingual\\'.ucfirst($integration); |
|
43 | + if (class_exists($integrationClass)) { |
|
44 | + return glsr($integrationClass); |
|
45 | + } |
|
46 | + glsr_log()->error($integrationClass.' does not exist'); |
|
47 | + } |
|
48 | + return false; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * return bool |
|
53 | - */ |
|
54 | - public function isIntegrated() |
|
55 | - { |
|
56 | - if (!empty($this->integration)) { |
|
57 | - return true; |
|
58 | - } |
|
59 | - if ($integration = $this->getIntegration()) { |
|
60 | - $this->integration = $integration; |
|
61 | - return true; |
|
62 | - } |
|
63 | - return false; |
|
64 | - } |
|
51 | + /** |
|
52 | + * return bool |
|
53 | + */ |
|
54 | + public function isIntegrated() |
|
55 | + { |
|
56 | + if (!empty($this->integration)) { |
|
57 | + return true; |
|
58 | + } |
|
59 | + if ($integration = $this->getIntegration()) { |
|
60 | + $this->integration = $integration; |
|
61 | + return true; |
|
62 | + } |
|
63 | + return false; |
|
64 | + } |
|
65 | 65 | } |
@@ -8,105 +8,105 @@ |
||
8 | 8 | |
9 | 9 | class ReviewLimits |
10 | 10 | { |
11 | - protected $request; |
|
11 | + protected $request; |
|
12 | 12 | |
13 | - /** |
|
14 | - * @return array |
|
15 | - * @filter site-reviews/get/reviews/query |
|
16 | - */ |
|
17 | - public function filterReviewsQuery(array $parameters, array $args) |
|
18 | - { |
|
19 | - if ($authorId = get_current_user_id()) { |
|
20 | - $parameters['author'] = $authorId; |
|
21 | - } |
|
22 | - $parameters['post_status'] = ['pending', 'publish']; |
|
23 | - return apply_filters('site-reviews/review-limits/query', $parameters, $args); |
|
24 | - } |
|
13 | + /** |
|
14 | + * @return array |
|
15 | + * @filter site-reviews/get/reviews/query |
|
16 | + */ |
|
17 | + public function filterReviewsQuery(array $parameters, array $args) |
|
18 | + { |
|
19 | + if ($authorId = get_current_user_id()) { |
|
20 | + $parameters['author'] = $authorId; |
|
21 | + } |
|
22 | + $parameters['post_status'] = ['pending', 'publish']; |
|
23 | + return apply_filters('site-reviews/review-limits/query', $parameters, $args); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * @return bool |
|
28 | - */ |
|
29 | - public function hasReachedLimit(array $request = []) |
|
30 | - { |
|
31 | - $this->request = $request; |
|
32 | - $method = Helper::buildMethodName( |
|
33 | - glsr(OptionManager::class)->get('settings.submissions.limit'), 'validateBy' |
|
34 | - ); |
|
35 | - return method_exists($this, $method) |
|
36 | - ? !call_user_func([$this, $method]) |
|
37 | - : false; |
|
38 | - } |
|
26 | + /** |
|
27 | + * @return bool |
|
28 | + */ |
|
29 | + public function hasReachedLimit(array $request = []) |
|
30 | + { |
|
31 | + $this->request = $request; |
|
32 | + $method = Helper::buildMethodName( |
|
33 | + glsr(OptionManager::class)->get('settings.submissions.limit'), 'validateBy' |
|
34 | + ); |
|
35 | + return method_exists($this, $method) |
|
36 | + ? !call_user_func([$this, $method]) |
|
37 | + : false; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @param string $value |
|
42 | - * @param string $whitelist |
|
43 | - * @return bool |
|
44 | - */ |
|
45 | - public function isWhitelisted($value, $whitelist) |
|
46 | - { |
|
47 | - if (empty($whitelist)) { |
|
48 | - return false; |
|
49 | - } |
|
50 | - return in_array($value, array_filter(explode("\n", $whitelist), 'trim')); |
|
51 | - } |
|
40 | + /** |
|
41 | + * @param string $value |
|
42 | + * @param string $whitelist |
|
43 | + * @return bool |
|
44 | + */ |
|
45 | + public function isWhitelisted($value, $whitelist) |
|
46 | + { |
|
47 | + if (empty($whitelist)) { |
|
48 | + return false; |
|
49 | + } |
|
50 | + return in_array($value, array_filter(explode("\n", $whitelist), 'trim')); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @param string $whitelistName |
|
55 | - * @return string |
|
56 | - */ |
|
57 | - protected function getWhitelist($whitelistName) |
|
58 | - { |
|
59 | - return glsr(OptionManager::class)->get('settings.submissions.limit_whitelist.'.$whitelistName); |
|
60 | - } |
|
53 | + /** |
|
54 | + * @param string $whitelistName |
|
55 | + * @return string |
|
56 | + */ |
|
57 | + protected function getWhitelist($whitelistName) |
|
58 | + { |
|
59 | + return glsr(OptionManager::class)->get('settings.submissions.limit_whitelist.'.$whitelistName); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * @return bool |
|
64 | - */ |
|
65 | - protected function validate($key, $value, $addMetaQuery = true) |
|
66 | - { |
|
67 | - if ($this->isWhitelisted($value, $this->getWhitelist($key))) { |
|
68 | - return true; |
|
69 | - } |
|
70 | - add_filter('site-reviews/get/reviews/query', [$this, 'filterReviewsQuery'], 5, 2); |
|
71 | - $args = ['assigned_to' => Arr::get($this->request, 'assign_to')]; |
|
72 | - if ($addMetaQuery) { |
|
73 | - $args[$key] = $value; |
|
74 | - } |
|
75 | - $reviews = glsr_get_reviews($args); |
|
76 | - remove_filter('site-reviews/get/reviews/query', [$this, 'filterReviewsQuery'], 5); |
|
77 | - $result = 0 === count($reviews); |
|
78 | - $result = apply_filters('site-reviews/review-limits/validate', $result, $reviews, $this->request, $key); |
|
79 | - return wp_validate_boolean($result); |
|
80 | - } |
|
62 | + /** |
|
63 | + * @return bool |
|
64 | + */ |
|
65 | + protected function validate($key, $value, $addMetaQuery = true) |
|
66 | + { |
|
67 | + if ($this->isWhitelisted($value, $this->getWhitelist($key))) { |
|
68 | + return true; |
|
69 | + } |
|
70 | + add_filter('site-reviews/get/reviews/query', [$this, 'filterReviewsQuery'], 5, 2); |
|
71 | + $args = ['assigned_to' => Arr::get($this->request, 'assign_to')]; |
|
72 | + if ($addMetaQuery) { |
|
73 | + $args[$key] = $value; |
|
74 | + } |
|
75 | + $reviews = glsr_get_reviews($args); |
|
76 | + remove_filter('site-reviews/get/reviews/query', [$this, 'filterReviewsQuery'], 5); |
|
77 | + $result = 0 === count($reviews); |
|
78 | + $result = apply_filters('site-reviews/review-limits/validate', $result, $reviews, $this->request, $key); |
|
79 | + return wp_validate_boolean($result); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @return bool |
|
84 | - */ |
|
85 | - protected function validateByEmail() |
|
86 | - { |
|
87 | - glsr_log()->debug('Email is: '.Arr::get($this->request, 'email')); |
|
88 | - return $this->validate('email', Arr::get($this->request, 'email')); |
|
89 | - } |
|
82 | + /** |
|
83 | + * @return bool |
|
84 | + */ |
|
85 | + protected function validateByEmail() |
|
86 | + { |
|
87 | + glsr_log()->debug('Email is: '.Arr::get($this->request, 'email')); |
|
88 | + return $this->validate('email', Arr::get($this->request, 'email')); |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * @return bool |
|
93 | - */ |
|
94 | - protected function validateByIpAddress() |
|
95 | - { |
|
96 | - glsr_log()->debug('IP Address is: '.Arr::get($this->request, 'ip_address')); |
|
97 | - return $this->validate('ip_address', Arr::get($this->request, 'ip_address')); |
|
98 | - } |
|
91 | + /** |
|
92 | + * @return bool |
|
93 | + */ |
|
94 | + protected function validateByIpAddress() |
|
95 | + { |
|
96 | + glsr_log()->debug('IP Address is: '.Arr::get($this->request, 'ip_address')); |
|
97 | + return $this->validate('ip_address', Arr::get($this->request, 'ip_address')); |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * @return bool |
|
102 | - */ |
|
103 | - protected function validateByUsername() |
|
104 | - { |
|
105 | - $user = wp_get_current_user(); |
|
106 | - if (!$user->exists()) { |
|
107 | - return true; |
|
108 | - } |
|
109 | - glsr_log()->debug('Username is: '.$user->user_login); |
|
110 | - return $this->validate('username', $user->user_login, false); |
|
111 | - } |
|
100 | + /** |
|
101 | + * @return bool |
|
102 | + */ |
|
103 | + protected function validateByUsername() |
|
104 | + { |
|
105 | + $user = wp_get_current_user(); |
|
106 | + if (!$user->exists()) { |
|
107 | + return true; |
|
108 | + } |
|
109 | + glsr_log()->debug('Username is: '.$user->user_login); |
|
110 | + return $this->validate('username', $user->user_login, false); |
|
111 | + } |
|
112 | 112 | } |
@@ -10,126 +10,126 @@ |
||
10 | 10 | */ |
11 | 11 | trait ValidationRules |
12 | 12 | { |
13 | - /** |
|
14 | - * Get the size of an attribute. |
|
15 | - * @param string $attribute |
|
16 | - * @param mixed $value |
|
17 | - * @return mixed |
|
18 | - */ |
|
19 | - abstract protected function getSize($attribute, $value); |
|
13 | + /** |
|
14 | + * Get the size of an attribute. |
|
15 | + * @param string $attribute |
|
16 | + * @param mixed $value |
|
17 | + * @return mixed |
|
18 | + */ |
|
19 | + abstract protected function getSize($attribute, $value); |
|
20 | 20 | |
21 | - /** |
|
22 | - * Replace all placeholders. |
|
23 | - * @param string $message |
|
24 | - * @return string |
|
25 | - */ |
|
26 | - protected function replace($message, array $parameters) |
|
27 | - { |
|
28 | - if (!Str::contains($message, '%s')) { |
|
29 | - return $message; |
|
30 | - } |
|
31 | - return preg_replace_callback('/(%s)/', function () use (&$parameters) { |
|
32 | - foreach ($parameters as $key => $value) { |
|
33 | - return array_shift($parameters); |
|
34 | - } |
|
35 | - }, $message); |
|
36 | - } |
|
21 | + /** |
|
22 | + * Replace all placeholders. |
|
23 | + * @param string $message |
|
24 | + * @return string |
|
25 | + */ |
|
26 | + protected function replace($message, array $parameters) |
|
27 | + { |
|
28 | + if (!Str::contains($message, '%s')) { |
|
29 | + return $message; |
|
30 | + } |
|
31 | + return preg_replace_callback('/(%s)/', function () use (&$parameters) { |
|
32 | + foreach ($parameters as $key => $value) { |
|
33 | + return array_shift($parameters); |
|
34 | + } |
|
35 | + }, $message); |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * Validate that an attribute was "accepted". |
|
40 | - * This validation rule implies the attribute is "required". |
|
41 | - * @param string $attribute |
|
42 | - * @param mixed $value |
|
43 | - * @return bool |
|
44 | - */ |
|
45 | - public function validateAccepted($value) |
|
46 | - { |
|
47 | - $acceptable = ['yes', 'on', '1', 1, true, 'true']; |
|
48 | - return $this->validateRequired($value) && in_array($value, $acceptable, true); |
|
49 | - } |
|
38 | + /** |
|
39 | + * Validate that an attribute was "accepted". |
|
40 | + * This validation rule implies the attribute is "required". |
|
41 | + * @param string $attribute |
|
42 | + * @param mixed $value |
|
43 | + * @return bool |
|
44 | + */ |
|
45 | + public function validateAccepted($value) |
|
46 | + { |
|
47 | + $acceptable = ['yes', 'on', '1', 1, true, 'true']; |
|
48 | + return $this->validateRequired($value) && in_array($value, $acceptable, true); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Validate the size of an attribute is between a set of values. |
|
53 | - * @param string $attribute |
|
54 | - * @param mixed $value |
|
55 | - * @return bool |
|
56 | - */ |
|
57 | - public function validateBetween($value, $attribute, array $parameters) |
|
58 | - { |
|
59 | - $this->requireParameterCount(2, $parameters, 'between'); |
|
60 | - $size = $this->getSize($attribute, $value); |
|
61 | - return $size >= $parameters[0] && $size <= $parameters[1]; |
|
62 | - } |
|
51 | + /** |
|
52 | + * Validate the size of an attribute is between a set of values. |
|
53 | + * @param string $attribute |
|
54 | + * @param mixed $value |
|
55 | + * @return bool |
|
56 | + */ |
|
57 | + public function validateBetween($value, $attribute, array $parameters) |
|
58 | + { |
|
59 | + $this->requireParameterCount(2, $parameters, 'between'); |
|
60 | + $size = $this->getSize($attribute, $value); |
|
61 | + return $size >= $parameters[0] && $size <= $parameters[1]; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * Validate that an attribute is a valid e-mail address. |
|
66 | - * @param mixed $value |
|
67 | - * @return bool |
|
68 | - */ |
|
69 | - public function validateEmail($value) |
|
70 | - { |
|
71 | - return false !== filter_var($value, FILTER_VALIDATE_EMAIL); |
|
72 | - } |
|
64 | + /** |
|
65 | + * Validate that an attribute is a valid e-mail address. |
|
66 | + * @param mixed $value |
|
67 | + * @return bool |
|
68 | + */ |
|
69 | + public function validateEmail($value) |
|
70 | + { |
|
71 | + return false !== filter_var($value, FILTER_VALIDATE_EMAIL); |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Validate the size of an attribute is less than a maximum value. |
|
76 | - * @param string $attribute |
|
77 | - * @param mixed $value |
|
78 | - * @return bool |
|
79 | - */ |
|
80 | - public function validateMax($value, $attribute, array $parameters) |
|
81 | - { |
|
82 | - $this->requireParameterCount(1, $parameters, 'max'); |
|
83 | - return $this->getSize($attribute, $value) <= $parameters[0]; |
|
84 | - } |
|
74 | + /** |
|
75 | + * Validate the size of an attribute is less than a maximum value. |
|
76 | + * @param string $attribute |
|
77 | + * @param mixed $value |
|
78 | + * @return bool |
|
79 | + */ |
|
80 | + public function validateMax($value, $attribute, array $parameters) |
|
81 | + { |
|
82 | + $this->requireParameterCount(1, $parameters, 'max'); |
|
83 | + return $this->getSize($attribute, $value) <= $parameters[0]; |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Validate the size of an attribute is greater than a minimum value. |
|
88 | - * @param string $attribute |
|
89 | - * @param mixed $value |
|
90 | - * @return bool |
|
91 | - */ |
|
92 | - public function validateMin($value, $attribute, array $parameters) |
|
93 | - { |
|
94 | - $this->requireParameterCount(1, $parameters, 'min'); |
|
95 | - return $this->getSize($attribute, $value) >= $parameters[0]; |
|
96 | - } |
|
86 | + /** |
|
87 | + * Validate the size of an attribute is greater than a minimum value. |
|
88 | + * @param string $attribute |
|
89 | + * @param mixed $value |
|
90 | + * @return bool |
|
91 | + */ |
|
92 | + public function validateMin($value, $attribute, array $parameters) |
|
93 | + { |
|
94 | + $this->requireParameterCount(1, $parameters, 'min'); |
|
95 | + return $this->getSize($attribute, $value) >= $parameters[0]; |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * Validate that an attribute is numeric. |
|
100 | - * @param mixed $value |
|
101 | - * @return bool |
|
102 | - */ |
|
103 | - public function validateNumber($value) |
|
104 | - { |
|
105 | - return is_numeric($value); |
|
106 | - } |
|
98 | + /** |
|
99 | + * Validate that an attribute is numeric. |
|
100 | + * @param mixed $value |
|
101 | + * @return bool |
|
102 | + */ |
|
103 | + public function validateNumber($value) |
|
104 | + { |
|
105 | + return is_numeric($value); |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * Validate that a required attribute exists. |
|
110 | - * @param mixed $value |
|
111 | - * @return bool |
|
112 | - */ |
|
113 | - public function validateRequired($value) |
|
114 | - { |
|
115 | - return is_null($value) |
|
116 | - || (is_string($value) && in_array(trim($value), ['', '[]'])) |
|
117 | - || (is_array($value) && empty($value)) |
|
118 | - ? false |
|
119 | - : true; |
|
120 | - } |
|
108 | + /** |
|
109 | + * Validate that a required attribute exists. |
|
110 | + * @param mixed $value |
|
111 | + * @return bool |
|
112 | + */ |
|
113 | + public function validateRequired($value) |
|
114 | + { |
|
115 | + return is_null($value) |
|
116 | + || (is_string($value) && in_array(trim($value), ['', '[]'])) |
|
117 | + || (is_array($value) && empty($value)) |
|
118 | + ? false |
|
119 | + : true; |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * Require a certain number of parameters to be present. |
|
124 | - * @param int $count |
|
125 | - * @param string $rule |
|
126 | - * @return void |
|
127 | - * @throws InvalidArgumentException |
|
128 | - */ |
|
129 | - protected function requireParameterCount($count, array $parameters, $rule) |
|
130 | - { |
|
131 | - if (count($parameters) < $count) { |
|
132 | - throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters."); |
|
133 | - } |
|
134 | - } |
|
122 | + /** |
|
123 | + * Require a certain number of parameters to be present. |
|
124 | + * @param int $count |
|
125 | + * @param string $rule |
|
126 | + * @return void |
|
127 | + * @throws InvalidArgumentException |
|
128 | + */ |
|
129 | + protected function requireParameterCount($count, array $parameters, $rule) |
|
130 | + { |
|
131 | + if (count($parameters) < $count) { |
|
132 | + throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters."); |
|
133 | + } |
|
134 | + } |
|
135 | 135 | } |