@@ -9,25 +9,25 @@ discard block |
||
9 | 9 | * @return mixed |
10 | 10 | */ |
11 | 11 | add_filter('plugins_loaded', function () { |
12 | - $hooks = array( |
|
13 | - 'glsr_calculate_ratings' => 1, |
|
14 | - 'glsr_create_review' => 2, |
|
15 | - 'glsr_debug' => 10, |
|
16 | - 'glsr_get' => 4, |
|
17 | - 'glsr_get_option' => 4, |
|
18 | - 'glsr_get_options' => 1, |
|
19 | - 'glsr_get_review' => 2, |
|
20 | - 'glsr_get_reviews' => 2, |
|
21 | - 'glsr_log' => 3, |
|
22 | - 'glsr_star_rating' => 2, |
|
23 | - ); |
|
24 | - foreach ($hooks as $function => $acceptedArgs) { |
|
25 | - add_filter($function, function () use ($function) { |
|
26 | - $args = func_get_args(); |
|
27 | - array_shift($args); // remove the fallback value |
|
28 | - return call_user_func_array($function, $args); |
|
29 | - }, 10, $acceptedArgs); |
|
30 | - } |
|
12 | + $hooks = array( |
|
13 | + 'glsr_calculate_ratings' => 1, |
|
14 | + 'glsr_create_review' => 2, |
|
15 | + 'glsr_debug' => 10, |
|
16 | + 'glsr_get' => 4, |
|
17 | + 'glsr_get_option' => 4, |
|
18 | + 'glsr_get_options' => 1, |
|
19 | + 'glsr_get_review' => 2, |
|
20 | + 'glsr_get_reviews' => 2, |
|
21 | + 'glsr_log' => 3, |
|
22 | + 'glsr_star_rating' => 2, |
|
23 | + ); |
|
24 | + foreach ($hooks as $function => $acceptedArgs) { |
|
25 | + add_filter($function, function () use ($function) { |
|
26 | + $args = func_get_args(); |
|
27 | + array_shift($args); // remove the fallback value |
|
28 | + return call_user_func_array($function, $args); |
|
29 | + }, 10, $acceptedArgs); |
|
30 | + } |
|
31 | 31 | }); |
32 | 32 | |
33 | 33 | /** |
@@ -35,10 +35,10 @@ discard block |
||
35 | 35 | */ |
36 | 36 | function glsr($alias = null) |
37 | 37 | { |
38 | - $app = \GeminiLabs\SiteReviews\Application::load(); |
|
39 | - return !empty($alias) |
|
40 | - ? $app->make($alias) |
|
41 | - : $app; |
|
38 | + $app = \GeminiLabs\SiteReviews\Application::load(); |
|
39 | + return !empty($alias) |
|
40 | + ? $app->make($alias) |
|
41 | + : $app; |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | /** |
@@ -48,15 +48,15 @@ discard block |
||
48 | 48 | */ |
49 | 49 | function glsr_array_column(array $array, $column) |
50 | 50 | { |
51 | - $result = array(); |
|
52 | - foreach ($array as $subarray) { |
|
53 | - $subarray = (array) $subarray; |
|
54 | - if (!isset($subarray[$column])) { |
|
55 | - continue; |
|
56 | - } |
|
57 | - $result[] = $subarray[$column]; |
|
58 | - } |
|
59 | - return $result; |
|
51 | + $result = array(); |
|
52 | + foreach ($array as $subarray) { |
|
53 | + $subarray = (array) $subarray; |
|
54 | + if (!isset($subarray[$column])) { |
|
55 | + continue; |
|
56 | + } |
|
57 | + $result[] = $subarray[$column]; |
|
58 | + } |
|
59 | + return $result; |
|
60 | 60 | } |
61 | 61 | |
62 | 62 | /** |
@@ -64,8 +64,8 @@ discard block |
||
64 | 64 | */ |
65 | 65 | function glsr_calculate_ratings() |
66 | 66 | { |
67 | - glsr('Controllers\AdminController')->routerCountReviews(false); |
|
68 | - glsr_log()->notice(__('Recalculated rating counts.', 'site-reviews')); |
|
67 | + glsr('Controllers\AdminController')->routerCountReviews(false); |
|
68 | + glsr_log()->notice(__('Recalculated rating counts.', 'site-reviews')); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | /** |
@@ -73,10 +73,10 @@ discard block |
||
73 | 73 | */ |
74 | 74 | function glsr_create_review($reviewValues = array()) |
75 | 75 | { |
76 | - $review = new \GeminiLabs\SiteReviews\Commands\CreateReview( |
|
77 | - glsr('Helper')->consolidateArray($reviewValues) |
|
78 | - ); |
|
79 | - return glsr('Database\ReviewManager')->create($review); |
|
76 | + $review = new \GeminiLabs\SiteReviews\Commands\CreateReview( |
|
77 | + glsr('Helper')->consolidateArray($reviewValues) |
|
78 | + ); |
|
79 | + return glsr('Database\ReviewManager')->create($review); |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | /** |
@@ -84,12 +84,12 @@ discard block |
||
84 | 84 | */ |
85 | 85 | function glsr_current_screen() |
86 | 86 | { |
87 | - if (function_exists('get_current_screen')) { |
|
88 | - $screen = get_current_screen(); |
|
89 | - } |
|
90 | - return empty($screen) |
|
91 | - ? (object) array_fill_keys(['base', 'id', 'post_type'], null) |
|
92 | - : $screen; |
|
87 | + if (function_exists('get_current_screen')) { |
|
88 | + $screen = get_current_screen(); |
|
89 | + } |
|
90 | + return empty($screen) |
|
91 | + ? (object) array_fill_keys(['base', 'id', 'post_type'], null) |
|
92 | + : $screen; |
|
93 | 93 | } |
94 | 94 | |
95 | 95 | /** |
@@ -98,16 +98,16 @@ discard block |
||
98 | 98 | */ |
99 | 99 | function glsr_debug(...$vars) |
100 | 100 | { |
101 | - if (1 == count($vars)) { |
|
102 | - $value = htmlspecialchars(print_r($vars[0], true), ENT_QUOTES, 'UTF-8'); |
|
103 | - printf('<div class="glsr-debug"><pre>%s</pre></div>', $value); |
|
104 | - } else { |
|
105 | - echo '<div class="glsr-debug-group">'; |
|
106 | - foreach ($vars as $var) { |
|
107 | - glsr_debug($var); |
|
108 | - } |
|
109 | - echo '</div>'; |
|
110 | - } |
|
101 | + if (1 == count($vars)) { |
|
102 | + $value = htmlspecialchars(print_r($vars[0], true), ENT_QUOTES, 'UTF-8'); |
|
103 | + printf('<div class="glsr-debug"><pre>%s</pre></div>', $value); |
|
104 | + } else { |
|
105 | + echo '<div class="glsr-debug-group">'; |
|
106 | + foreach ($vars as $var) { |
|
107 | + glsr_debug($var); |
|
108 | + } |
|
109 | + echo '</div>'; |
|
110 | + } |
|
111 | 111 | } |
112 | 112 | |
113 | 113 | /** |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | */ |
119 | 119 | function glsr_get($array, $path = '', $fallback = '') |
120 | 120 | { |
121 | - return glsr('Helper')->dataGet($array, $path, $fallback); |
|
121 | + return glsr('Helper')->dataGet($array, $path, $fallback); |
|
122 | 122 | } |
123 | 123 | |
124 | 124 | /** |
@@ -129,9 +129,9 @@ discard block |
||
129 | 129 | */ |
130 | 130 | function glsr_get_option($path = '', $fallback = '', $cast = '') |
131 | 131 | { |
132 | - return is_string($path) |
|
133 | - ? glsr('Database\OptionManager')->get('settings.'.$path, $fallback, $cast) |
|
134 | - : $fallback; |
|
132 | + return is_string($path) |
|
133 | + ? glsr('Database\OptionManager')->get('settings.'.$path, $fallback, $cast) |
|
134 | + : $fallback; |
|
135 | 135 | } |
136 | 136 | |
137 | 137 | /** |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | */ |
140 | 140 | function glsr_get_options() |
141 | 141 | { |
142 | - return glsr('Database\OptionManager')->get('settings'); |
|
142 | + return glsr('Database\OptionManager')->get('settings'); |
|
143 | 143 | } |
144 | 144 | |
145 | 145 | /** |
@@ -148,13 +148,13 @@ discard block |
||
148 | 148 | */ |
149 | 149 | function glsr_get_review($post) |
150 | 150 | { |
151 | - if (is_numeric($post)) { |
|
152 | - $post = get_post($post); |
|
153 | - } |
|
154 | - if (!($post instanceof WP_Post)) { |
|
155 | - $post = new WP_Post((object) []); |
|
156 | - } |
|
157 | - return glsr('Database\ReviewManager')->single($post); |
|
151 | + if (is_numeric($post)) { |
|
152 | + $post = get_post($post); |
|
153 | + } |
|
154 | + if (!($post instanceof WP_Post)) { |
|
155 | + $post = new WP_Post((object) []); |
|
156 | + } |
|
157 | + return glsr('Database\ReviewManager')->single($post); |
|
158 | 158 | } |
159 | 159 | |
160 | 160 | /** |
@@ -162,7 +162,7 @@ discard block |
||
162 | 162 | */ |
163 | 163 | function glsr_get_reviews($args = array()) |
164 | 164 | { |
165 | - return glsr('Database\ReviewManager')->get(glsr('Helper')->consolidateArray($args)); |
|
165 | + return glsr('Database\ReviewManager')->get(glsr('Helper')->consolidateArray($args)); |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | /** |
@@ -170,12 +170,12 @@ discard block |
||
170 | 170 | */ |
171 | 171 | function glsr_log() |
172 | 172 | { |
173 | - $args = func_get_args(); |
|
174 | - $console = glsr('Modules\Console'); |
|
175 | - if ($value = glsr_get($args, '0')) { |
|
176 | - return $console->debug($value, glsr_get($args, '1', [])); |
|
177 | - } |
|
178 | - return $console; |
|
173 | + $args = func_get_args(); |
|
174 | + $console = glsr('Modules\Console'); |
|
175 | + if ($value = glsr_get($args, '0')) { |
|
176 | + return $console->debug($value, glsr_get($args, '1', [])); |
|
177 | + } |
|
178 | + return $console; |
|
179 | 179 | } |
180 | 180 | |
181 | 181 | /** |
@@ -183,5 +183,5 @@ discard block |
||
183 | 183 | */ |
184 | 184 | function glsr_star_rating($rating) |
185 | 185 | { |
186 | - return glsr('Modules\Html\Partial')->build('star-rating', ['rating' => $rating]); |
|
186 | + return glsr('Modules\Html\Partial')->build('star-rating', ['rating' => $rating]); |
|
187 | 187 | } |
@@ -9,112 +9,112 @@ |
||
9 | 9 | |
10 | 10 | class SettingsController extends Controller |
11 | 11 | { |
12 | - /** |
|
13 | - * @param mixed $input |
|
14 | - * @return array |
|
15 | - * @callback register_setting |
|
16 | - */ |
|
17 | - public function callbackRegisterSettings($input) |
|
18 | - { |
|
19 | - $settings = glsr(Helper::class)->consolidateArray($input); |
|
20 | - if (1 === count($settings) && array_key_exists('settings', $settings)) { |
|
21 | - $options = array_replace_recursive(glsr(OptionManager::class)->all(), $input); |
|
22 | - $options = $this->sanitizeGeneral($input, $options); |
|
23 | - $options = $this->sanitizeSubmissions($input, $options); |
|
24 | - $options = $this->sanitizeTranslations($input, $options); |
|
25 | - if (filter_input(INPUT_POST, 'option_page') == Application::ID.'-settings') { |
|
26 | - glsr(Notice::class)->addSuccess(__('Settings updated.', 'site-reviews')); |
|
27 | - } |
|
28 | - return $options; |
|
29 | - } |
|
30 | - return $input; |
|
31 | - } |
|
12 | + /** |
|
13 | + * @param mixed $input |
|
14 | + * @return array |
|
15 | + * @callback register_setting |
|
16 | + */ |
|
17 | + public function callbackRegisterSettings($input) |
|
18 | + { |
|
19 | + $settings = glsr(Helper::class)->consolidateArray($input); |
|
20 | + if (1 === count($settings) && array_key_exists('settings', $settings)) { |
|
21 | + $options = array_replace_recursive(glsr(OptionManager::class)->all(), $input); |
|
22 | + $options = $this->sanitizeGeneral($input, $options); |
|
23 | + $options = $this->sanitizeSubmissions($input, $options); |
|
24 | + $options = $this->sanitizeTranslations($input, $options); |
|
25 | + if (filter_input(INPUT_POST, 'option_page') == Application::ID.'-settings') { |
|
26 | + glsr(Notice::class)->addSuccess(__('Settings updated.', 'site-reviews')); |
|
27 | + } |
|
28 | + return $options; |
|
29 | + } |
|
30 | + return $input; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * @return void |
|
35 | - * @action admin_init |
|
36 | - */ |
|
37 | - public function registerSettings() |
|
38 | - { |
|
39 | - register_setting(Application::ID.'-settings', OptionManager::databaseKey(), [ |
|
40 | - 'sanitize_callback' => [$this, 'callbackRegisterSettings'], |
|
41 | - ]); |
|
42 | - } |
|
33 | + /** |
|
34 | + * @return void |
|
35 | + * @action admin_init |
|
36 | + */ |
|
37 | + public function registerSettings() |
|
38 | + { |
|
39 | + register_setting(Application::ID.'-settings', OptionManager::databaseKey(), [ |
|
40 | + 'sanitize_callback' => [$this, 'callbackRegisterSettings'], |
|
41 | + ]); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * @return array |
|
46 | - */ |
|
47 | - protected function sanitizeGeneral(array $input, array $options) |
|
48 | - { |
|
49 | - $inputForm = $input['settings']['general']; |
|
50 | - if (!$this->hasMultilingualIntegration($inputForm['support']['multilingual'])) { |
|
51 | - $options['settings']['general']['support']['multilingual'] = ''; |
|
52 | - } |
|
53 | - if ('' == trim($inputForm['notification_message'])) { |
|
54 | - $options['settings']['general']['notification_message'] = glsr()->defaults['settings']['general']['notification_message']; |
|
55 | - } |
|
56 | - $options['settings']['general']['notifications'] = glsr_get($inputForm, 'notifications', []); |
|
57 | - return $options; |
|
58 | - } |
|
44 | + /** |
|
45 | + * @return array |
|
46 | + */ |
|
47 | + protected function sanitizeGeneral(array $input, array $options) |
|
48 | + { |
|
49 | + $inputForm = $input['settings']['general']; |
|
50 | + if (!$this->hasMultilingualIntegration($inputForm['support']['multilingual'])) { |
|
51 | + $options['settings']['general']['support']['multilingual'] = ''; |
|
52 | + } |
|
53 | + if ('' == trim($inputForm['notification_message'])) { |
|
54 | + $options['settings']['general']['notification_message'] = glsr()->defaults['settings']['general']['notification_message']; |
|
55 | + } |
|
56 | + $options['settings']['general']['notifications'] = glsr_get($inputForm, 'notifications', []); |
|
57 | + return $options; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * @return array |
|
62 | - */ |
|
63 | - protected function sanitizeSubmissions(array $input, array $options) |
|
64 | - { |
|
65 | - $inputForm = $input['settings']['submissions']; |
|
66 | - $options['settings']['submissions']['required'] = isset($inputForm['required']) |
|
67 | - ? $inputForm['required'] |
|
68 | - : []; |
|
69 | - return $options; |
|
70 | - } |
|
60 | + /** |
|
61 | + * @return array |
|
62 | + */ |
|
63 | + protected function sanitizeSubmissions(array $input, array $options) |
|
64 | + { |
|
65 | + $inputForm = $input['settings']['submissions']; |
|
66 | + $options['settings']['submissions']['required'] = isset($inputForm['required']) |
|
67 | + ? $inputForm['required'] |
|
68 | + : []; |
|
69 | + return $options; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * @return array |
|
74 | - */ |
|
75 | - protected function sanitizeTranslations(array $input, array $options) |
|
76 | - { |
|
77 | - if (isset($input['settings']['strings'])) { |
|
78 | - $options['settings']['strings'] = array_values(array_filter($input['settings']['strings'])); |
|
79 | - $allowedTags = [ |
|
80 | - 'a' => ['class' => [], 'href' => [], 'target' => []], |
|
81 | - 'span' => ['class' => []], |
|
82 | - ]; |
|
83 | - array_walk($options['settings']['strings'], function (&$string) use ($allowedTags) { |
|
84 | - if (isset($string['s2'])) { |
|
85 | - $string['s2'] = wp_kses($string['s2'], $allowedTags); |
|
86 | - } |
|
87 | - if (isset($string['p2'])) { |
|
88 | - $string['p2'] = wp_kses($string['p2'], $allowedTags); |
|
89 | - } |
|
90 | - }); |
|
91 | - } |
|
92 | - return $options; |
|
93 | - } |
|
72 | + /** |
|
73 | + * @return array |
|
74 | + */ |
|
75 | + protected function sanitizeTranslations(array $input, array $options) |
|
76 | + { |
|
77 | + if (isset($input['settings']['strings'])) { |
|
78 | + $options['settings']['strings'] = array_values(array_filter($input['settings']['strings'])); |
|
79 | + $allowedTags = [ |
|
80 | + 'a' => ['class' => [], 'href' => [], 'target' => []], |
|
81 | + 'span' => ['class' => []], |
|
82 | + ]; |
|
83 | + array_walk($options['settings']['strings'], function (&$string) use ($allowedTags) { |
|
84 | + if (isset($string['s2'])) { |
|
85 | + $string['s2'] = wp_kses($string['s2'], $allowedTags); |
|
86 | + } |
|
87 | + if (isset($string['p2'])) { |
|
88 | + $string['p2'] = wp_kses($string['p2'], $allowedTags); |
|
89 | + } |
|
90 | + }); |
|
91 | + } |
|
92 | + return $options; |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * @return bool |
|
97 | - */ |
|
98 | - protected function hasMultilingualIntegration($integration) |
|
99 | - { |
|
100 | - if (!in_array($integration, ['polylang', 'wpml'])) { |
|
101 | - return false; |
|
102 | - } |
|
103 | - $integrationClass = 'GeminiLabs\SiteReviews\Modules\\'.ucfirst($integration); |
|
104 | - if (!glsr($integrationClass)->isActive()) { |
|
105 | - glsr(Notice::class)->addError(sprintf( |
|
106 | - __('Please install/activate the %s plugin to enable integration.', 'site-reviews'), |
|
107 | - constant($integrationClass.'::PLUGIN_NAME') |
|
108 | - )); |
|
109 | - return false; |
|
110 | - } elseif (!glsr($integrationClass)->isSupported()) { |
|
111 | - glsr(Notice::class)->addError(sprintf( |
|
112 | - __('Please update the %s plugin to v%s or greater to enable integration.', 'site-reviews'), |
|
113 | - constant($integrationClass.'::PLUGIN_NAME'), |
|
114 | - constant($integrationClass.'::SUPPORTED_VERSION') |
|
115 | - )); |
|
116 | - return false; |
|
117 | - } |
|
118 | - return true; |
|
119 | - } |
|
95 | + /** |
|
96 | + * @return bool |
|
97 | + */ |
|
98 | + protected function hasMultilingualIntegration($integration) |
|
99 | + { |
|
100 | + if (!in_array($integration, ['polylang', 'wpml'])) { |
|
101 | + return false; |
|
102 | + } |
|
103 | + $integrationClass = 'GeminiLabs\SiteReviews\Modules\\'.ucfirst($integration); |
|
104 | + if (!glsr($integrationClass)->isActive()) { |
|
105 | + glsr(Notice::class)->addError(sprintf( |
|
106 | + __('Please install/activate the %s plugin to enable integration.', 'site-reviews'), |
|
107 | + constant($integrationClass.'::PLUGIN_NAME') |
|
108 | + )); |
|
109 | + return false; |
|
110 | + } elseif (!glsr($integrationClass)->isSupported()) { |
|
111 | + glsr(Notice::class)->addError(sprintf( |
|
112 | + __('Please update the %s plugin to v%s or greater to enable integration.', 'site-reviews'), |
|
113 | + constant($integrationClass.'::PLUGIN_NAME'), |
|
114 | + constant($integrationClass.'::SUPPORTED_VERSION') |
|
115 | + )); |
|
116 | + return false; |
|
117 | + } |
|
118 | + return true; |
|
119 | + } |
|
120 | 120 | } |
@@ -7,155 +7,155 @@ |
||
7 | 7 | |
8 | 8 | class OptionManager |
9 | 9 | { |
10 | - /** |
|
11 | - * @var array |
|
12 | - */ |
|
13 | - protected $options; |
|
10 | + /** |
|
11 | + * @var array |
|
12 | + */ |
|
13 | + protected $options; |
|
14 | 14 | |
15 | - /** |
|
16 | - * @return string |
|
17 | - */ |
|
18 | - public static function databaseKey($version = null) |
|
19 | - { |
|
20 | - if (null === $version) { |
|
21 | - $version = explode('.', glsr()->version); |
|
22 | - $version = array_shift($version); |
|
23 | - } |
|
24 | - return glsr(Helper::class)->snakeCase( |
|
25 | - Application::ID.'-v'.intval($version) |
|
26 | - ); |
|
27 | - } |
|
15 | + /** |
|
16 | + * @return string |
|
17 | + */ |
|
18 | + public static function databaseKey($version = null) |
|
19 | + { |
|
20 | + if (null === $version) { |
|
21 | + $version = explode('.', glsr()->version); |
|
22 | + $version = array_shift($version); |
|
23 | + } |
|
24 | + return glsr(Helper::class)->snakeCase( |
|
25 | + Application::ID.'-v'.intval($version) |
|
26 | + ); |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * @return array |
|
31 | - */ |
|
32 | - public function all() |
|
33 | - { |
|
34 | - if (empty($this->options)) { |
|
35 | - $this->reset(); |
|
36 | - } |
|
37 | - return $this->options; |
|
38 | - } |
|
29 | + /** |
|
30 | + * @return array |
|
31 | + */ |
|
32 | + public function all() |
|
33 | + { |
|
34 | + if (empty($this->options)) { |
|
35 | + $this->reset(); |
|
36 | + } |
|
37 | + return $this->options; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @param string $path |
|
42 | - * @return bool |
|
43 | - */ |
|
44 | - public function delete($path) |
|
45 | - { |
|
46 | - $keys = explode('.', $path); |
|
47 | - $last = array_pop($keys); |
|
48 | - $options = $this->all(); |
|
49 | - $pointer = &$options; |
|
50 | - foreach ($keys as $key) { |
|
51 | - if (!isset($pointer[$key]) || !is_array($pointer[$key])) { |
|
52 | - continue; |
|
53 | - } |
|
54 | - $pointer = &$pointer[$key]; |
|
55 | - } |
|
56 | - unset($pointer[$last]); |
|
57 | - return $this->set($options); |
|
58 | - } |
|
40 | + /** |
|
41 | + * @param string $path |
|
42 | + * @return bool |
|
43 | + */ |
|
44 | + public function delete($path) |
|
45 | + { |
|
46 | + $keys = explode('.', $path); |
|
47 | + $last = array_pop($keys); |
|
48 | + $options = $this->all(); |
|
49 | + $pointer = &$options; |
|
50 | + foreach ($keys as $key) { |
|
51 | + if (!isset($pointer[$key]) || !is_array($pointer[$key])) { |
|
52 | + continue; |
|
53 | + } |
|
54 | + $pointer = &$pointer[$key]; |
|
55 | + } |
|
56 | + unset($pointer[$last]); |
|
57 | + return $this->set($options); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * @param string $path |
|
62 | - * @param mixed $fallback |
|
63 | - * @param string $cast |
|
64 | - * @return mixed |
|
65 | - */ |
|
66 | - public function get($path = '', $fallback = '', $cast = '') |
|
67 | - { |
|
68 | - $result = glsr(Helper::class)->dataGet($this->all(), $path, $fallback); |
|
69 | - return glsr(Helper::class)->castTo($cast, $result); |
|
70 | - } |
|
60 | + /** |
|
61 | + * @param string $path |
|
62 | + * @param mixed $fallback |
|
63 | + * @param string $cast |
|
64 | + * @return mixed |
|
65 | + */ |
|
66 | + public function get($path = '', $fallback = '', $cast = '') |
|
67 | + { |
|
68 | + $result = glsr(Helper::class)->dataGet($this->all(), $path, $fallback); |
|
69 | + return glsr(Helper::class)->castTo($cast, $result); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * @param string $path |
|
74 | - * @return bool |
|
75 | - */ |
|
76 | - public function getBool($path) |
|
77 | - { |
|
78 | - return 'yes' == $this->get($path) |
|
79 | - ? true |
|
80 | - : false; |
|
81 | - } |
|
72 | + /** |
|
73 | + * @param string $path |
|
74 | + * @return bool |
|
75 | + */ |
|
76 | + public function getBool($path) |
|
77 | + { |
|
78 | + return 'yes' == $this->get($path) |
|
79 | + ? true |
|
80 | + : false; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * @param string $path |
|
85 | - * @param mixed $fallback |
|
86 | - * @param string $cast |
|
87 | - * @return mixed |
|
88 | - */ |
|
89 | - public function getWP($path, $fallback = '', $cast = '') |
|
90 | - { |
|
91 | - $option = get_option($path, $fallback); |
|
92 | - if (empty($option)) { |
|
93 | - $option = $fallback; |
|
94 | - } |
|
95 | - return glsr(Helper::class)->castTo($cast, $option); |
|
96 | - } |
|
83 | + /** |
|
84 | + * @param string $path |
|
85 | + * @param mixed $fallback |
|
86 | + * @param string $cast |
|
87 | + * @return mixed |
|
88 | + */ |
|
89 | + public function getWP($path, $fallback = '', $cast = '') |
|
90 | + { |
|
91 | + $option = get_option($path, $fallback); |
|
92 | + if (empty($option)) { |
|
93 | + $option = $fallback; |
|
94 | + } |
|
95 | + return glsr(Helper::class)->castTo($cast, $option); |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * @return string |
|
100 | - */ |
|
101 | - public function json() |
|
102 | - { |
|
103 | - return json_encode($this->all()); |
|
104 | - } |
|
98 | + /** |
|
99 | + * @return string |
|
100 | + */ |
|
101 | + public function json() |
|
102 | + { |
|
103 | + return json_encode($this->all()); |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * @return array |
|
108 | - */ |
|
109 | - public function normalize(array $options = []) |
|
110 | - { |
|
111 | - $options = wp_parse_args( |
|
112 | - glsr(Helper::class)->flattenArray($options), |
|
113 | - glsr(DefaultsManager::class)->defaults() |
|
114 | - ); |
|
115 | - array_walk($options, function (&$value) { |
|
116 | - if (!is_string($value)) { |
|
117 | - return; |
|
118 | - } |
|
119 | - $value = wp_kses($value, wp_kses_allowed_html('post')); |
|
120 | - }); |
|
121 | - return glsr(Helper::class)->convertDotNotationArray($options); |
|
122 | - } |
|
106 | + /** |
|
107 | + * @return array |
|
108 | + */ |
|
109 | + public function normalize(array $options = []) |
|
110 | + { |
|
111 | + $options = wp_parse_args( |
|
112 | + glsr(Helper::class)->flattenArray($options), |
|
113 | + glsr(DefaultsManager::class)->defaults() |
|
114 | + ); |
|
115 | + array_walk($options, function (&$value) { |
|
116 | + if (!is_string($value)) { |
|
117 | + return; |
|
118 | + } |
|
119 | + $value = wp_kses($value, wp_kses_allowed_html('post')); |
|
120 | + }); |
|
121 | + return glsr(Helper::class)->convertDotNotationArray($options); |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * @return bool |
|
126 | - */ |
|
127 | - public function isRecaptchaEnabled() |
|
128 | - { |
|
129 | - $integration = $this->get('settings.submissions.recaptcha.integration'); |
|
130 | - return 'all' == $integration || ('guest' == $integration && !is_user_logged_in()); |
|
131 | - } |
|
124 | + /** |
|
125 | + * @return bool |
|
126 | + */ |
|
127 | + public function isRecaptchaEnabled() |
|
128 | + { |
|
129 | + $integration = $this->get('settings.submissions.recaptcha.integration'); |
|
130 | + return 'all' == $integration || ('guest' == $integration && !is_user_logged_in()); |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * @return array |
|
135 | - */ |
|
136 | - public function reset() |
|
137 | - { |
|
138 | - $options = $this->getWP(static::databaseKey(), []); |
|
139 | - if (!is_array($options) || empty($options)) { |
|
140 | - delete_option(static::databaseKey()); |
|
141 | - $options = glsr()->defaults; |
|
142 | - } |
|
143 | - $this->options = $options; |
|
144 | - } |
|
133 | + /** |
|
134 | + * @return array |
|
135 | + */ |
|
136 | + public function reset() |
|
137 | + { |
|
138 | + $options = $this->getWP(static::databaseKey(), []); |
|
139 | + if (!is_array($options) || empty($options)) { |
|
140 | + delete_option(static::databaseKey()); |
|
141 | + $options = glsr()->defaults; |
|
142 | + } |
|
143 | + $this->options = $options; |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * @param string|array $pathOrOptions |
|
148 | - * @param mixed $value |
|
149 | - * @return bool |
|
150 | - */ |
|
151 | - public function set($pathOrOptions, $value = '') |
|
152 | - { |
|
153 | - if (is_string($pathOrOptions)) { |
|
154 | - $pathOrOptions = glsr(Helper::class)->dataSet($this->all(), $pathOrOptions, $value); |
|
155 | - } |
|
156 | - if ($result = update_option(static::databaseKey(), (array) $pathOrOptions)) { |
|
157 | - $this->reset(); |
|
158 | - } |
|
159 | - return $result; |
|
160 | - } |
|
146 | + /** |
|
147 | + * @param string|array $pathOrOptions |
|
148 | + * @param mixed $value |
|
149 | + * @return bool |
|
150 | + */ |
|
151 | + public function set($pathOrOptions, $value = '') |
|
152 | + { |
|
153 | + if (is_string($pathOrOptions)) { |
|
154 | + $pathOrOptions = glsr(Helper::class)->dataSet($this->all(), $pathOrOptions, $value); |
|
155 | + } |
|
156 | + if ($result = update_option(static::databaseKey(), (array) $pathOrOptions)) { |
|
157 | + $this->reset(); |
|
158 | + } |
|
159 | + return $result; |
|
160 | + } |
|
161 | 161 | } |
@@ -7,23 +7,23 @@ discard block |
||
7 | 7 | |
8 | 8 | class SqlQueries |
9 | 9 | { |
10 | - protected $db; |
|
11 | - protected $postType; |
|
10 | + protected $db; |
|
11 | + protected $postType; |
|
12 | 12 | |
13 | - public function __construct() |
|
14 | - { |
|
15 | - global $wpdb; |
|
16 | - $this->db = $wpdb; |
|
17 | - $this->postType = Application::POST_TYPE; |
|
18 | - } |
|
13 | + public function __construct() |
|
14 | + { |
|
15 | + global $wpdb; |
|
16 | + $this->db = $wpdb; |
|
17 | + $this->postType = Application::POST_TYPE; |
|
18 | + } |
|
19 | 19 | |
20 | - /** |
|
21 | - * @param string $metaReviewId |
|
22 | - * @return int |
|
23 | - */ |
|
24 | - public function getPostIdFromReviewId($metaReviewId) |
|
25 | - { |
|
26 | - $postId = $this->db->get_var(" |
|
20 | + /** |
|
21 | + * @param string $metaReviewId |
|
22 | + * @return int |
|
23 | + */ |
|
24 | + public function getPostIdFromReviewId($metaReviewId) |
|
25 | + { |
|
26 | + $postId = $this->db->get_var(" |
|
27 | 27 | SELECT p.ID |
28 | 28 | FROM {$this->db->posts} AS p |
29 | 29 | INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id |
@@ -31,17 +31,17 @@ discard block |
||
31 | 31 | AND m.meta_key = '_review_id' |
32 | 32 | AND m.meta_value = '{$metaReviewId}' |
33 | 33 | "); |
34 | - return intval($postId); |
|
35 | - } |
|
34 | + return intval($postId); |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * @param int $lastPostId |
|
39 | - * @param int $limit |
|
40 | - * @return array |
|
41 | - */ |
|
42 | - public function getReviewCounts(array $args, $lastPostId = 0, $limit = 500) |
|
43 | - { |
|
44 | - return (array) $this->db->get_results(" |
|
37 | + /** |
|
38 | + * @param int $lastPostId |
|
39 | + * @param int $limit |
|
40 | + * @return array |
|
41 | + */ |
|
42 | + public function getReviewCounts(array $args, $lastPostId = 0, $limit = 500) |
|
43 | + { |
|
44 | + return (array) $this->db->get_results(" |
|
45 | 45 | SELECT DISTINCT p.ID, m1.meta_value AS rating, m2.meta_value AS type |
46 | 46 | FROM {$this->db->posts} AS p |
47 | 47 | INNER JOIN {$this->db->postmeta} AS m1 ON p.ID = m1.post_id |
@@ -56,17 +56,17 @@ discard block |
||
56 | 56 | ORDER By p.ID ASC |
57 | 57 | LIMIT {$limit} |
58 | 58 | "); |
59 | - } |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * @todo remove this? |
|
63 | - * @param string $metaKey |
|
64 | - * @return array |
|
65 | - */ |
|
66 | - public function getReviewCountsFor($metaKey) |
|
67 | - { |
|
68 | - $metaKey = glsr(Helper::class)->prefix('_', $metaKey); |
|
69 | - return (array) $this->db->get_results(" |
|
61 | + /** |
|
62 | + * @todo remove this? |
|
63 | + * @param string $metaKey |
|
64 | + * @return array |
|
65 | + */ |
|
66 | + public function getReviewCountsFor($metaKey) |
|
67 | + { |
|
68 | + $metaKey = glsr(Helper::class)->prefix('_', $metaKey); |
|
69 | + return (array) $this->db->get_results(" |
|
70 | 70 | SELECT DISTINCT m.meta_value AS name, COUNT(*) num_posts |
71 | 71 | FROM {$this->db->posts} AS p |
72 | 72 | INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id |
@@ -74,16 +74,16 @@ discard block |
||
74 | 74 | AND m.meta_key = '{$metaKey}' |
75 | 75 | GROUP BY name |
76 | 76 | "); |
77 | - } |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @todo remove this? |
|
81 | - * @param string $reviewType |
|
82 | - * @return array |
|
83 | - */ |
|
84 | - public function getReviewIdsByType($reviewType) |
|
85 | - { |
|
86 | - $results = $this->db->get_col(" |
|
79 | + /** |
|
80 | + * @todo remove this? |
|
81 | + * @param string $reviewType |
|
82 | + * @return array |
|
83 | + */ |
|
84 | + public function getReviewIdsByType($reviewType) |
|
85 | + { |
|
86 | + $results = $this->db->get_col(" |
|
87 | 87 | SELECT DISTINCT m1.meta_value AS review_id |
88 | 88 | FROM {$this->db->posts} AS p |
89 | 89 | INNER JOIN {$this->db->postmeta} AS m1 ON p.ID = m1.post_id |
@@ -93,20 +93,20 @@ discard block |
||
93 | 93 | AND m2.meta_key = '_review_type' |
94 | 94 | AND m2.meta_value = '{$reviewType}' |
95 | 95 | "); |
96 | - return array_keys(array_flip($results)); |
|
97 | - } |
|
96 | + return array_keys(array_flip($results)); |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * @param int $greaterThanId |
|
101 | - * @param int $limit |
|
102 | - * @return array |
|
103 | - */ |
|
104 | - public function getReviewRatingsFromIds(array $postIds, $greaterThanId = 0, $limit = 100) |
|
105 | - { |
|
106 | - sort($postIds); |
|
107 | - $postIds = array_slice($postIds, intval(array_search($greaterThanId, $postIds)), $limit); |
|
108 | - $postIds = implode(',', $postIds); |
|
109 | - return (array) $this->db->get_results(" |
|
99 | + /** |
|
100 | + * @param int $greaterThanId |
|
101 | + * @param int $limit |
|
102 | + * @return array |
|
103 | + */ |
|
104 | + public function getReviewRatingsFromIds(array $postIds, $greaterThanId = 0, $limit = 100) |
|
105 | + { |
|
106 | + sort($postIds); |
|
107 | + $postIds = array_slice($postIds, intval(array_search($greaterThanId, $postIds)), $limit); |
|
108 | + $postIds = implode(',', $postIds); |
|
109 | + return (array) $this->db->get_results(" |
|
110 | 110 | SELECT p.ID, m.meta_value AS rating |
111 | 111 | FROM {$this->db->posts} AS p |
112 | 112 | INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id |
@@ -119,17 +119,17 @@ discard block |
||
119 | 119 | ORDER By p.ID ASC |
120 | 120 | LIMIT {$limit} |
121 | 121 | "); |
122 | - } |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * @param string $key |
|
126 | - * @param string $status |
|
127 | - * @return array |
|
128 | - */ |
|
129 | - public function getReviewsMeta($key, $status = 'publish') |
|
130 | - { |
|
131 | - $key = glsr(Helper::class)->prefix('_', $key); |
|
132 | - $values = $this->db->get_col(" |
|
124 | + /** |
|
125 | + * @param string $key |
|
126 | + * @param string $status |
|
127 | + * @return array |
|
128 | + */ |
|
129 | + public function getReviewsMeta($key, $status = 'publish') |
|
130 | + { |
|
131 | + $key = glsr(Helper::class)->prefix('_', $key); |
|
132 | + $values = $this->db->get_col(" |
|
133 | 133 | SELECT DISTINCT m.meta_value |
134 | 134 | FROM {$this->db->postmeta} m |
135 | 135 | LEFT JOIN {$this->db->posts} p ON p.ID = m.post_id |
@@ -140,42 +140,42 @@ discard block |
||
140 | 140 | GROUP BY p.ID -- remove duplicate meta_value entries |
141 | 141 | ORDER BY m.meta_id ASC -- sort by oldest meta_value |
142 | 142 | "); |
143 | - sort($values); |
|
144 | - return $values; |
|
145 | - } |
|
143 | + sort($values); |
|
144 | + return $values; |
|
145 | + } |
|
146 | 146 | |
147 | - /** |
|
148 | - * @param string $and |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - protected function getAndForCounts(array $args, $and = '') |
|
152 | - { |
|
153 | - $postIds = implode(',', array_filter(glsr_get($args, 'post_ids', []))); |
|
154 | - $termIds = implode(',', array_filter(glsr_get($args, 'term_ids', []))); |
|
155 | - if (!empty($args['type'])) { |
|
156 | - $and.= "AND m2.meta_value = '{$args['type']}' "; |
|
157 | - } |
|
158 | - if ($postIds) { |
|
159 | - $and.= "AND m3.meta_key = '_assigned_to' AND m3.meta_value IN ({$postIds}) "; |
|
160 | - } |
|
161 | - if ($termIds) { |
|
162 | - $and.= "AND tr.term_taxonomy_id IN ({$termIds}) "; |
|
163 | - } |
|
164 | - return apply_filters('site-reviews/query/and-for-counts', $and); |
|
165 | - } |
|
147 | + /** |
|
148 | + * @param string $and |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + protected function getAndForCounts(array $args, $and = '') |
|
152 | + { |
|
153 | + $postIds = implode(',', array_filter(glsr_get($args, 'post_ids', []))); |
|
154 | + $termIds = implode(',', array_filter(glsr_get($args, 'term_ids', []))); |
|
155 | + if (!empty($args['type'])) { |
|
156 | + $and.= "AND m2.meta_value = '{$args['type']}' "; |
|
157 | + } |
|
158 | + if ($postIds) { |
|
159 | + $and.= "AND m3.meta_key = '_assigned_to' AND m3.meta_value IN ({$postIds}) "; |
|
160 | + } |
|
161 | + if ($termIds) { |
|
162 | + $and.= "AND tr.term_taxonomy_id IN ({$termIds}) "; |
|
163 | + } |
|
164 | + return apply_filters('site-reviews/query/and-for-counts', $and); |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * @param string $innerJoin |
|
169 | - * @return string |
|
170 | - */ |
|
171 | - protected function getInnerJoinForCounts(array $args, $innerJoin = '') |
|
172 | - { |
|
173 | - if (!empty(glsr_get($args, 'post_ids'))) { |
|
174 | - $innerJoin.= "INNER JOIN {$this->db->postmeta} AS m3 ON p.ID = m3.post_id "; |
|
175 | - } |
|
176 | - if (!empty(glsr_get($args, 'term_ids'))) { |
|
177 | - $innerJoin.= "INNER JOIN {$this->db->term_relationships} AS tr ON p.ID = tr.object_id "; |
|
178 | - } |
|
179 | - return apply_filters('site-reviews/query/inner-join-for-counts', $innerJoin); |
|
180 | - } |
|
167 | + /** |
|
168 | + * @param string $innerJoin |
|
169 | + * @return string |
|
170 | + */ |
|
171 | + protected function getInnerJoinForCounts(array $args, $innerJoin = '') |
|
172 | + { |
|
173 | + if (!empty(glsr_get($args, 'post_ids'))) { |
|
174 | + $innerJoin.= "INNER JOIN {$this->db->postmeta} AS m3 ON p.ID = m3.post_id "; |
|
175 | + } |
|
176 | + if (!empty(glsr_get($args, 'term_ids'))) { |
|
177 | + $innerJoin.= "INNER JOIN {$this->db->term_relationships} AS tr ON p.ID = tr.object_id "; |
|
178 | + } |
|
179 | + return apply_filters('site-reviews/query/inner-join-for-counts', $innerJoin); |
|
180 | + } |
|
181 | 181 | } |
@@ -7,84 +7,84 @@ |
||
7 | 7 | |
8 | 8 | class Upgrader |
9 | 9 | { |
10 | - /** |
|
11 | - * @return string |
|
12 | - */ |
|
13 | - public $currentVersion; |
|
10 | + /** |
|
11 | + * @return string |
|
12 | + */ |
|
13 | + public $currentVersion; |
|
14 | 14 | |
15 | - /** |
|
16 | - * @return void |
|
17 | - */ |
|
18 | - public function run() |
|
19 | - { |
|
20 | - $filenames = []; |
|
21 | - $iterator = new DirectoryIterator(dirname(__FILE__).'/Upgrader'); |
|
22 | - foreach ($iterator as $fileinfo) { |
|
23 | - if ($fileinfo->isFile()) { |
|
24 | - $filenames[] = $fileinfo->getFilename(); |
|
25 | - } |
|
26 | - } |
|
27 | - natsort($filenames); |
|
28 | - $this->currentVersion = $this->currentVersion(); |
|
29 | - array_walk($filenames, function ($file) { |
|
30 | - $className = str_replace('.php', '', $file); |
|
31 | - $upgradeFromVersion = str_replace(['Upgrade_', '_'], ['', '.'], $className); |
|
32 | - $suffix = preg_replace('/[\d.]+(.+)?/', '${1}', glsr()->version); // allow alpha/beta versions |
|
33 | - if ('0.0.0' == $this->currentVersion |
|
34 | - || version_compare($this->currentVersion, $upgradeFromVersion.$suffix, '>=')) { |
|
35 | - return; |
|
36 | - } |
|
37 | - glsr('Modules\\Upgrader\\'.$className); |
|
38 | - glsr_log()->notice('Completed Upgrade for v'.$upgradeFromVersion.$suffix); |
|
39 | - }); |
|
40 | - $this->finish(); |
|
41 | - } |
|
15 | + /** |
|
16 | + * @return void |
|
17 | + */ |
|
18 | + public function run() |
|
19 | + { |
|
20 | + $filenames = []; |
|
21 | + $iterator = new DirectoryIterator(dirname(__FILE__).'/Upgrader'); |
|
22 | + foreach ($iterator as $fileinfo) { |
|
23 | + if ($fileinfo->isFile()) { |
|
24 | + $filenames[] = $fileinfo->getFilename(); |
|
25 | + } |
|
26 | + } |
|
27 | + natsort($filenames); |
|
28 | + $this->currentVersion = $this->currentVersion(); |
|
29 | + array_walk($filenames, function ($file) { |
|
30 | + $className = str_replace('.php', '', $file); |
|
31 | + $upgradeFromVersion = str_replace(['Upgrade_', '_'], ['', '.'], $className); |
|
32 | + $suffix = preg_replace('/[\d.]+(.+)?/', '${1}', glsr()->version); // allow alpha/beta versions |
|
33 | + if ('0.0.0' == $this->currentVersion |
|
34 | + || version_compare($this->currentVersion, $upgradeFromVersion.$suffix, '>=')) { |
|
35 | + return; |
|
36 | + } |
|
37 | + glsr('Modules\\Upgrader\\'.$className); |
|
38 | + glsr_log()->notice('Completed Upgrade for v'.$upgradeFromVersion.$suffix); |
|
39 | + }); |
|
40 | + $this->finish(); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * @return void |
|
45 | - */ |
|
46 | - public function finish() |
|
47 | - { |
|
48 | - if ($this->currentVersion !== glsr()->version) { |
|
49 | - $this->setReviewCounts(); |
|
50 | - $this->updateVersionFrom($this->currentVersion); |
|
51 | - } elseif (!glsr(OptionManager::class)->get('last_review_count', false)) { |
|
52 | - $this->setReviewCounts(); |
|
53 | - } |
|
54 | - } |
|
43 | + /** |
|
44 | + * @return void |
|
45 | + */ |
|
46 | + public function finish() |
|
47 | + { |
|
48 | + if ($this->currentVersion !== glsr()->version) { |
|
49 | + $this->setReviewCounts(); |
|
50 | + $this->updateVersionFrom($this->currentVersion); |
|
51 | + } elseif (!glsr(OptionManager::class)->get('last_review_count', false)) { |
|
52 | + $this->setReviewCounts(); |
|
53 | + } |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @return string |
|
58 | - */ |
|
59 | - protected function currentVersion() |
|
60 | - { |
|
61 | - $fallback = '0.0.0'; |
|
62 | - $majorVersions = [4, 3, 2]; |
|
63 | - foreach ($majorVersions as $majorVersion) { |
|
64 | - $settings = get_option(OptionManager::databaseKey($majorVersion)); |
|
65 | - $version = glsr_get($settings, 'version', $fallback); |
|
66 | - if (version_compare($version, $fallback, '>')) { |
|
67 | - return $version; |
|
68 | - } |
|
69 | - } |
|
70 | - return $fallback; |
|
71 | - } |
|
56 | + /** |
|
57 | + * @return string |
|
58 | + */ |
|
59 | + protected function currentVersion() |
|
60 | + { |
|
61 | + $fallback = '0.0.0'; |
|
62 | + $majorVersions = [4, 3, 2]; |
|
63 | + foreach ($majorVersions as $majorVersion) { |
|
64 | + $settings = get_option(OptionManager::databaseKey($majorVersion)); |
|
65 | + $version = glsr_get($settings, 'version', $fallback); |
|
66 | + if (version_compare($version, $fallback, '>')) { |
|
67 | + return $version; |
|
68 | + } |
|
69 | + } |
|
70 | + return $fallback; |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * @return void |
|
75 | - */ |
|
76 | - protected function setReviewCounts() |
|
77 | - { |
|
78 | - add_action('admin_init', 'glsr_calculate_ratings'); |
|
79 | - } |
|
73 | + /** |
|
74 | + * @return void |
|
75 | + */ |
|
76 | + protected function setReviewCounts() |
|
77 | + { |
|
78 | + add_action('admin_init', 'glsr_calculate_ratings'); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * @param string $previousVersion |
|
83 | - * @return void |
|
84 | - */ |
|
85 | - protected function updateVersionFrom($previousVersion) |
|
86 | - { |
|
87 | - glsr(OptionManager::class)->set('version', glsr()->version); |
|
88 | - glsr(OptionManager::class)->set('version_upgraded_from', $previousVersion); |
|
89 | - } |
|
81 | + /** |
|
82 | + * @param string $previousVersion |
|
83 | + * @return void |
|
84 | + */ |
|
85 | + protected function updateVersionFrom($previousVersion) |
|
86 | + { |
|
87 | + glsr(OptionManager::class)->set('version', glsr()->version); |
|
88 | + glsr(OptionManager::class)->set('version_upgraded_from', $previousVersion); |
|
89 | + } |
|
90 | 90 | } |
@@ -9,59 +9,59 @@ |
||
9 | 9 | |
10 | 10 | class Upgrade_4_0_0 |
11 | 11 | { |
12 | - public function __construct() |
|
13 | - { |
|
14 | - $this->migrateSettings(); |
|
15 | - $this->protectMetaKeys(); |
|
16 | - $this->deleteSessions(); |
|
17 | - delete_transient(Application::ID.'_cloudflare_ips'); |
|
18 | - } |
|
12 | + public function __construct() |
|
13 | + { |
|
14 | + $this->migrateSettings(); |
|
15 | + $this->protectMetaKeys(); |
|
16 | + $this->deleteSessions(); |
|
17 | + delete_transient(Application::ID.'_cloudflare_ips'); |
|
18 | + } |
|
19 | 19 | |
20 | - /** |
|
21 | - * @return void |
|
22 | - */ |
|
23 | - public function deleteSessions() |
|
24 | - { |
|
25 | - global $wpdb; |
|
26 | - $wpdb->query(" |
|
20 | + /** |
|
21 | + * @return void |
|
22 | + */ |
|
23 | + public function deleteSessions() |
|
24 | + { |
|
25 | + global $wpdb; |
|
26 | + $wpdb->query(" |
|
27 | 27 | DELETE |
28 | 28 | FROM {$wpdb->options} |
29 | 29 | WHERE option_name LIKE '_glsr_session%' |
30 | 30 | "); |
31 | - } |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * @return void |
|
35 | - */ |
|
36 | - public function migrateSettings() |
|
37 | - { |
|
38 | - if ($settings = get_option(OptionManager::databaseKey(3))) { |
|
39 | - $previousVersion = glsr(Helper::class)->dataGet($settings, 'version', '0.0.0'); |
|
40 | - $multilingual = 'yes' == glsr(Helper::class)->dataGet($settings, 'settings.general.support.polylang') |
|
41 | - ? 'polylang' |
|
42 | - : ''; |
|
43 | - $settings = glsr(Helper::class)->dataSet($settings, 'settings.general.multilingual', $multilingual); |
|
44 | - $settings = glsr(Helper::class)->dataSet($settings, 'settings.submissions.blacklist.integration', ''); |
|
45 | - unset($settings['settings']['general']['support']); |
|
46 | - update_option(OptionManager::databaseKey(4), $settings); |
|
47 | - } |
|
48 | - } |
|
33 | + /** |
|
34 | + * @return void |
|
35 | + */ |
|
36 | + public function migrateSettings() |
|
37 | + { |
|
38 | + if ($settings = get_option(OptionManager::databaseKey(3))) { |
|
39 | + $previousVersion = glsr(Helper::class)->dataGet($settings, 'version', '0.0.0'); |
|
40 | + $multilingual = 'yes' == glsr(Helper::class)->dataGet($settings, 'settings.general.support.polylang') |
|
41 | + ? 'polylang' |
|
42 | + : ''; |
|
43 | + $settings = glsr(Helper::class)->dataSet($settings, 'settings.general.multilingual', $multilingual); |
|
44 | + $settings = glsr(Helper::class)->dataSet($settings, 'settings.submissions.blacklist.integration', ''); |
|
45 | + unset($settings['settings']['general']['support']); |
|
46 | + update_option(OptionManager::databaseKey(4), $settings); |
|
47 | + } |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @return void |
|
52 | - */ |
|
53 | - public function protectMetaKeys() |
|
54 | - { |
|
55 | - global $wpdb; |
|
56 | - $keys = array_keys(glsr(CreateReviewDefaults::class)->defaults()); |
|
57 | - $keys = implode("','", $keys); |
|
58 | - $postType = Application::POST_TYPE; |
|
59 | - $wpdb->query(" |
|
50 | + /** |
|
51 | + * @return void |
|
52 | + */ |
|
53 | + public function protectMetaKeys() |
|
54 | + { |
|
55 | + global $wpdb; |
|
56 | + $keys = array_keys(glsr(CreateReviewDefaults::class)->defaults()); |
|
57 | + $keys = implode("','", $keys); |
|
58 | + $postType = Application::POST_TYPE; |
|
59 | + $wpdb->query(" |
|
60 | 60 | UPDATE {$wpdb->postmeta} pm |
61 | 61 | INNER JOIN {$wpdb->posts} p ON p.id = pm.post_id |
62 | 62 | SET pm.meta_key = CONCAT('_', pm.meta_key) |
63 | 63 | WHERE pm.meta_key IN ('{$keys}') |
64 | 64 | AND p.post_type = '{$postType}' |
65 | 65 | "); |
66 | - } |
|
66 | + } |
|
67 | 67 | } |
@@ -7,83 +7,83 @@ |
||
7 | 7 | |
8 | 8 | class Wpml implements Contract |
9 | 9 | { |
10 | - const PLUGIN_NAME = 'WPML'; |
|
11 | - const SUPPORTED_VERSION = '3.3.5'; |
|
10 | + const PLUGIN_NAME = 'WPML'; |
|
11 | + const SUPPORTED_VERSION = '3.3.5'; |
|
12 | 12 | |
13 | - /** |
|
14 | - * {@inheritdoc} |
|
15 | - */ |
|
16 | - public function getPost($postId) |
|
17 | - { |
|
18 | - $postId = trim($postId); |
|
19 | - if (!is_numeric($postId)) { |
|
20 | - return; |
|
21 | - } |
|
22 | - if ($this->isEnabled()) { |
|
23 | - $postId = apply_filters('wpml_object_id', $postId, 'any', true); |
|
24 | - } |
|
25 | - return get_post(intval($postId)); |
|
26 | - } |
|
13 | + /** |
|
14 | + * {@inheritdoc} |
|
15 | + */ |
|
16 | + public function getPost($postId) |
|
17 | + { |
|
18 | + $postId = trim($postId); |
|
19 | + if (!is_numeric($postId)) { |
|
20 | + return; |
|
21 | + } |
|
22 | + if ($this->isEnabled()) { |
|
23 | + $postId = apply_filters('wpml_object_id', $postId, 'any', true); |
|
24 | + } |
|
25 | + return get_post(intval($postId)); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * {@inheritdoc} |
|
30 | - */ |
|
31 | - public function getPostIds(array $postIds) |
|
32 | - { |
|
33 | - if (!$this->isEnabled()) { |
|
34 | - return $postIds; |
|
35 | - } |
|
36 | - $newPostIds = []; |
|
37 | - foreach ($this->cleanIds($postIds) as $postId) { |
|
38 | - $postType = get_post_type($postId); |
|
39 | - if (!$postType) { |
|
40 | - continue; |
|
41 | - } |
|
42 | - $elementType = 'post_'.$postType; |
|
43 | - $trid = apply_filters('wpml_element_trid', null, $postId, $elementType); |
|
44 | - $translations = apply_filters('wpml_get_element_translations', null, $trid, $elementType); |
|
45 | - if (!is_array($translations)) { |
|
46 | - $translations = []; |
|
47 | - } |
|
48 | - $newPostIds = array_merge( |
|
49 | - $newPostIds, |
|
50 | - array_column($translations, 'element_id') |
|
51 | - ); |
|
52 | - } |
|
53 | - return $this->cleanIds($newPostIds); |
|
54 | - } |
|
28 | + /** |
|
29 | + * {@inheritdoc} |
|
30 | + */ |
|
31 | + public function getPostIds(array $postIds) |
|
32 | + { |
|
33 | + if (!$this->isEnabled()) { |
|
34 | + return $postIds; |
|
35 | + } |
|
36 | + $newPostIds = []; |
|
37 | + foreach ($this->cleanIds($postIds) as $postId) { |
|
38 | + $postType = get_post_type($postId); |
|
39 | + if (!$postType) { |
|
40 | + continue; |
|
41 | + } |
|
42 | + $elementType = 'post_'.$postType; |
|
43 | + $trid = apply_filters('wpml_element_trid', null, $postId, $elementType); |
|
44 | + $translations = apply_filters('wpml_get_element_translations', null, $trid, $elementType); |
|
45 | + if (!is_array($translations)) { |
|
46 | + $translations = []; |
|
47 | + } |
|
48 | + $newPostIds = array_merge( |
|
49 | + $newPostIds, |
|
50 | + array_column($translations, 'element_id') |
|
51 | + ); |
|
52 | + } |
|
53 | + return $this->cleanIds($newPostIds); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * {@inheritdoc} |
|
58 | - */ |
|
59 | - public function isActive() |
|
60 | - { |
|
61 | - return defined('ICL_SITEPRESS_VERSION'); |
|
62 | - } |
|
56 | + /** |
|
57 | + * {@inheritdoc} |
|
58 | + */ |
|
59 | + public function isActive() |
|
60 | + { |
|
61 | + return defined('ICL_SITEPRESS_VERSION'); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * {@inheritdoc} |
|
66 | - */ |
|
67 | - public function isEnabled() |
|
68 | - { |
|
69 | - return $this->isActive() |
|
70 | - && 'wpml' == glsr(OptionManager::class)->get('settings.general.multilingual'); |
|
71 | - } |
|
64 | + /** |
|
65 | + * {@inheritdoc} |
|
66 | + */ |
|
67 | + public function isEnabled() |
|
68 | + { |
|
69 | + return $this->isActive() |
|
70 | + && 'wpml' == glsr(OptionManager::class)->get('settings.general.multilingual'); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * {@inheritdoc} |
|
75 | - */ |
|
76 | - public function isSupported() |
|
77 | - { |
|
78 | - return $this->isActive() |
|
79 | - && version_compare(ICL_SITEPRESS_VERSION, static::SUPPORTED_VERSION, '>='); |
|
80 | - } |
|
73 | + /** |
|
74 | + * {@inheritdoc} |
|
75 | + */ |
|
76 | + public function isSupported() |
|
77 | + { |
|
78 | + return $this->isActive() |
|
79 | + && version_compare(ICL_SITEPRESS_VERSION, static::SUPPORTED_VERSION, '>='); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @return array |
|
84 | - */ |
|
85 | - protected function cleanIds(array $postIds) |
|
86 | - { |
|
87 | - return array_filter(array_unique($postIds)); |
|
88 | - } |
|
82 | + /** |
|
83 | + * @return array |
|
84 | + */ |
|
85 | + protected function cleanIds(array $postIds) |
|
86 | + { |
|
87 | + return array_filter(array_unique($postIds)); |
|
88 | + } |
|
89 | 89 | } |
@@ -7,79 +7,79 @@ |
||
7 | 7 | |
8 | 8 | class Polylang implements Contract |
9 | 9 | { |
10 | - const PLUGIN_NAME = 'Polylang'; |
|
11 | - const SUPPORTED_VERSION = '2.3'; |
|
10 | + const PLUGIN_NAME = 'Polylang'; |
|
11 | + const SUPPORTED_VERSION = '2.3'; |
|
12 | 12 | |
13 | - /** |
|
14 | - * {@inheritdoc} |
|
15 | - */ |
|
16 | - public function getPost($postId) |
|
17 | - { |
|
18 | - $postId = trim($postId); |
|
19 | - if (!is_numeric($postId)) { |
|
20 | - return; |
|
21 | - } |
|
22 | - if ($this->isEnabled()) { |
|
23 | - $polylangPostId = pll_get_post($postId, pll_get_post_language(get_the_ID())); |
|
24 | - } |
|
25 | - if (!empty($polylangPostId)) { |
|
26 | - $postId = $polylangPostId; |
|
27 | - } |
|
28 | - return get_post(intval($postId)); |
|
29 | - } |
|
13 | + /** |
|
14 | + * {@inheritdoc} |
|
15 | + */ |
|
16 | + public function getPost($postId) |
|
17 | + { |
|
18 | + $postId = trim($postId); |
|
19 | + if (!is_numeric($postId)) { |
|
20 | + return; |
|
21 | + } |
|
22 | + if ($this->isEnabled()) { |
|
23 | + $polylangPostId = pll_get_post($postId, pll_get_post_language(get_the_ID())); |
|
24 | + } |
|
25 | + if (!empty($polylangPostId)) { |
|
26 | + $postId = $polylangPostId; |
|
27 | + } |
|
28 | + return get_post(intval($postId)); |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * {@inheritdoc} |
|
33 | - */ |
|
34 | - public function getPostIds(array $postIds) |
|
35 | - { |
|
36 | - if (!$this->isEnabled()) { |
|
37 | - return $postIds; |
|
38 | - } |
|
39 | - $newPostIds = []; |
|
40 | - foreach ($this->cleanIds($postIds) as $postId) { |
|
41 | - $newPostIds = array_merge( |
|
42 | - $newPostIds, |
|
43 | - array_values(pll_get_post_translations($postId)) |
|
44 | - ); |
|
45 | - } |
|
46 | - return $this->cleanIds($newPostIds); |
|
47 | - } |
|
31 | + /** |
|
32 | + * {@inheritdoc} |
|
33 | + */ |
|
34 | + public function getPostIds(array $postIds) |
|
35 | + { |
|
36 | + if (!$this->isEnabled()) { |
|
37 | + return $postIds; |
|
38 | + } |
|
39 | + $newPostIds = []; |
|
40 | + foreach ($this->cleanIds($postIds) as $postId) { |
|
41 | + $newPostIds = array_merge( |
|
42 | + $newPostIds, |
|
43 | + array_values(pll_get_post_translations($postId)) |
|
44 | + ); |
|
45 | + } |
|
46 | + return $this->cleanIds($newPostIds); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * {@inheritdoc} |
|
51 | - */ |
|
52 | - public function isActive() |
|
53 | - { |
|
54 | - return function_exists('PLL') |
|
55 | - && function_exists('pll_get_post') |
|
56 | - && function_exists('pll_get_post_language') |
|
57 | - && function_exists('pll_get_post_translations'); |
|
58 | - } |
|
49 | + /** |
|
50 | + * {@inheritdoc} |
|
51 | + */ |
|
52 | + public function isActive() |
|
53 | + { |
|
54 | + return function_exists('PLL') |
|
55 | + && function_exists('pll_get_post') |
|
56 | + && function_exists('pll_get_post_language') |
|
57 | + && function_exists('pll_get_post_translations'); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * {@inheritdoc} |
|
62 | - */ |
|
63 | - public function isEnabled() |
|
64 | - { |
|
65 | - return $this->isActive() |
|
66 | - && 'polylang' == glsr(OptionManager::class)->get('settings.general.multilingual'); |
|
67 | - } |
|
60 | + /** |
|
61 | + * {@inheritdoc} |
|
62 | + */ |
|
63 | + public function isEnabled() |
|
64 | + { |
|
65 | + return $this->isActive() |
|
66 | + && 'polylang' == glsr(OptionManager::class)->get('settings.general.multilingual'); |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * {@inheritdoc} |
|
71 | - */ |
|
72 | - public function isSupported() |
|
73 | - { |
|
74 | - return defined('POLYLANG_VERSION') |
|
75 | - && version_compare(POLYLANG_VERSION, static::SUPPORTED_VERSION, '>='); |
|
76 | - } |
|
69 | + /** |
|
70 | + * {@inheritdoc} |
|
71 | + */ |
|
72 | + public function isSupported() |
|
73 | + { |
|
74 | + return defined('POLYLANG_VERSION') |
|
75 | + && version_compare(POLYLANG_VERSION, static::SUPPORTED_VERSION, '>='); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @return array |
|
80 | - */ |
|
81 | - protected function cleanIds(array $postIds) |
|
82 | - { |
|
83 | - return array_filter(array_unique($postIds)); |
|
84 | - } |
|
78 | + /** |
|
79 | + * @return array |
|
80 | + */ |
|
81 | + protected function cleanIds(array $postIds) |
|
82 | + { |
|
83 | + return array_filter(array_unique($postIds)); |
|
84 | + } |
|
85 | 85 | } |
@@ -4,206 +4,206 @@ |
||
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 = round($this->getTotalSum($ratingCounts) / $average, intval($roundBy)); |
|
53 | - } |
|
54 | - return floatval(apply_filters('site-reviews/rating/average', $average, $ratingCounts)); |
|
55 | - } |
|
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 = round($this->getTotalSum($ratingCounts) / $average, intval($roundBy)); |
|
53 | + } |
|
54 | + return floatval(apply_filters('site-reviews/rating/average', $average, $ratingCounts)); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * Get the lower bound for up/down ratings |
|
59 | - * Method receives an up/down ratings array: [1, -1, -1, 1, 1, -1]. |
|
60 | - * @see http://www.evanmiller.org/how-not-to-sort-by-average-rating.html |
|
61 | - * @see https://news.ycombinator.com/item?id=10481507 |
|
62 | - * @see https://dataorigami.net/blogs/napkin-folding/79030467-an-algorithm-to-sort-top-comments |
|
63 | - * @see http://julesjacobs.github.io/2015/08/17/bayesian-scoring-of-ratings.html |
|
64 | - * @param int $confidencePercentage |
|
65 | - * @return int|float |
|
66 | - */ |
|
67 | - public function getLowerBound(array $upDownCounts = [0, 0], $confidencePercentage = 95) |
|
68 | - { |
|
69 | - $numRatings = array_sum($upDownCounts); |
|
70 | - if ($numRatings < 1) { |
|
71 | - return 0; |
|
72 | - } |
|
73 | - $z = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
|
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); |
|
76 | - } |
|
57 | + /** |
|
58 | + * Get the lower bound for up/down ratings |
|
59 | + * Method receives an up/down ratings array: [1, -1, -1, 1, 1, -1]. |
|
60 | + * @see http://www.evanmiller.org/how-not-to-sort-by-average-rating.html |
|
61 | + * @see https://news.ycombinator.com/item?id=10481507 |
|
62 | + * @see https://dataorigami.net/blogs/napkin-folding/79030467-an-algorithm-to-sort-top-comments |
|
63 | + * @see http://julesjacobs.github.io/2015/08/17/bayesian-scoring-of-ratings.html |
|
64 | + * @param int $confidencePercentage |
|
65 | + * @return int|float |
|
66 | + */ |
|
67 | + public function getLowerBound(array $upDownCounts = [0, 0], $confidencePercentage = 95) |
|
68 | + { |
|
69 | + $numRatings = array_sum($upDownCounts); |
|
70 | + if ($numRatings < 1) { |
|
71 | + return 0; |
|
72 | + } |
|
73 | + $z = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
|
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); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @return int|float |
|
80 | - */ |
|
81 | - public function getOverallPercentage(array $ratingCounts) |
|
82 | - { |
|
83 | - return round($this->getAverage($ratingCounts) * 100 / glsr()->constant('MAX_RATING', __CLASS__), 2); |
|
84 | - } |
|
78 | + /** |
|
79 | + * @return int|float |
|
80 | + */ |
|
81 | + public function getOverallPercentage(array $ratingCounts) |
|
82 | + { |
|
83 | + return round($this->getAverage($ratingCounts) * 100 / glsr()->constant('MAX_RATING', __CLASS__), 2); |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * @return array |
|
88 | - */ |
|
89 | - public function getPercentages(array $ratingCounts) |
|
90 | - { |
|
91 | - $total = array_sum($ratingCounts); |
|
92 | - foreach ($ratingCounts as $index => $count) { |
|
93 | - if (empty($count)) { |
|
94 | - continue; |
|
95 | - } |
|
96 | - $ratingCounts[$index] = $count / $total * 100; |
|
97 | - } |
|
98 | - return $this->getRoundedPercentages($ratingCounts); |
|
99 | - } |
|
86 | + /** |
|
87 | + * @return array |
|
88 | + */ |
|
89 | + public function getPercentages(array $ratingCounts) |
|
90 | + { |
|
91 | + $total = array_sum($ratingCounts); |
|
92 | + foreach ($ratingCounts as $index => $count) { |
|
93 | + if (empty($count)) { |
|
94 | + continue; |
|
95 | + } |
|
96 | + $ratingCounts[$index] = $count / $total * 100; |
|
97 | + } |
|
98 | + return $this->getRoundedPercentages($ratingCounts); |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * @return float |
|
103 | - */ |
|
104 | - public function getRanking(array $ratingCounts) |
|
105 | - { |
|
106 | - return floatval(apply_filters('site-reviews/rating/ranking', |
|
107 | - $this->getRankingUsingImdb($ratingCounts), |
|
108 | - $ratingCounts, |
|
109 | - $this |
|
110 | - )); |
|
111 | - } |
|
101 | + /** |
|
102 | + * @return float |
|
103 | + */ |
|
104 | + public function getRanking(array $ratingCounts) |
|
105 | + { |
|
106 | + return floatval(apply_filters('site-reviews/rating/ranking', |
|
107 | + $this->getRankingUsingImdb($ratingCounts), |
|
108 | + $ratingCounts, |
|
109 | + $this |
|
110 | + )); |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * Get the bayesian ranking for an array of reviews |
|
115 | - * This formula is the same one used by IMDB to rank their top 250 films. |
|
116 | - * @see https://www.xkcd.com/937/ |
|
117 | - * @see https://districtdatalabs.silvrback.com/computing-a-bayesian-estimate-of-star-rating-means |
|
118 | - * @see http://fulmicoton.com/posts/bayesian_rating/ |
|
119 | - * @see https://stats.stackexchange.com/questions/93974/is-there-an-equivalent-to-lower-bound-of-wilson-score-confidence-interval-for-va |
|
120 | - * @param int $confidencePercentage |
|
121 | - * @return int|float |
|
122 | - */ |
|
123 | - public function getRankingUsingImdb(array $ratingCounts, $confidencePercentage = 70) |
|
124 | - { |
|
125 | - $avgRating = $this->getAverage($ratingCounts); |
|
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 | - // This could also be the average score of all items instead of a fixed value. |
|
128 | - $bayesMean = ($confidencePercentage / 100) * glsr()->constant('MAX_RATING', __CLASS__); // prior, 70% = 3.5 |
|
129 | - // Represents the number of ratings expected to begin observing a pattern that would put confidence in the prior. |
|
130 | - $bayesMinimal = 10; // confidence |
|
131 | - $numOfReviews = array_sum($ratingCounts); |
|
132 | - return $avgRating > 0 |
|
133 | - ? (($bayesMinimal * $bayesMean) + ($avgRating * $numOfReviews)) / ($bayesMinimal + $numOfReviews) |
|
134 | - : 0; |
|
135 | - } |
|
113 | + /** |
|
114 | + * Get the bayesian ranking for an array of reviews |
|
115 | + * This formula is the same one used by IMDB to rank their top 250 films. |
|
116 | + * @see https://www.xkcd.com/937/ |
|
117 | + * @see https://districtdatalabs.silvrback.com/computing-a-bayesian-estimate-of-star-rating-means |
|
118 | + * @see http://fulmicoton.com/posts/bayesian_rating/ |
|
119 | + * @see https://stats.stackexchange.com/questions/93974/is-there-an-equivalent-to-lower-bound-of-wilson-score-confidence-interval-for-va |
|
120 | + * @param int $confidencePercentage |
|
121 | + * @return int|float |
|
122 | + */ |
|
123 | + public function getRankingUsingImdb(array $ratingCounts, $confidencePercentage = 70) |
|
124 | + { |
|
125 | + $avgRating = $this->getAverage($ratingCounts); |
|
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 | + // This could also be the average score of all items instead of a fixed value. |
|
128 | + $bayesMean = ($confidencePercentage / 100) * glsr()->constant('MAX_RATING', __CLASS__); // prior, 70% = 3.5 |
|
129 | + // Represents the number of ratings expected to begin observing a pattern that would put confidence in the prior. |
|
130 | + $bayesMinimal = 10; // confidence |
|
131 | + $numOfReviews = array_sum($ratingCounts); |
|
132 | + return $avgRating > 0 |
|
133 | + ? (($bayesMinimal * $bayesMean) + ($avgRating * $numOfReviews)) / ($bayesMinimal + $numOfReviews) |
|
134 | + : 0; |
|
135 | + } |
|
136 | 136 | |
137 | - /** |
|
138 | - * The quality of a 5 star rating depends not only on the average number of stars but also on |
|
139 | - * the number of reviews. This method calculates the bayesian ranking of a page by its number |
|
140 | - * of reviews and their rating. |
|
141 | - * @see http://www.evanmiller.org/ranking-items-with-star-ratings.html |
|
142 | - * @see https://stackoverflow.com/questions/1411199/what-is-a-better-way-to-sort-by-a-5-star-rating/1411268 |
|
143 | - * @see http://julesjacobs.github.io/2015/08/17/bayesian-scoring-of-ratings.html |
|
144 | - * @param int $confidencePercentage |
|
145 | - * @return float |
|
146 | - */ |
|
147 | - public function getRankingUsingZScores(array $ratingCounts, $confidencePercentage = 90) |
|
148 | - { |
|
149 | - $ratingCountsSum = array_sum($ratingCounts) + glsr()->constant('MAX_RATING', __CLASS__); |
|
150 | - $weight = $this->getWeight($ratingCounts, $ratingCountsSum); |
|
151 | - $weightPow2 = $this->getWeight($ratingCounts, $ratingCountsSum, true); |
|
152 | - $zScore = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
|
153 | - return $weight - $zScore * sqrt(($weightPow2 - pow($weight, 2)) / ($ratingCountsSum + 1)); |
|
154 | - } |
|
137 | + /** |
|
138 | + * The quality of a 5 star rating depends not only on the average number of stars but also on |
|
139 | + * the number of reviews. This method calculates the bayesian ranking of a page by its number |
|
140 | + * of reviews and their rating. |
|
141 | + * @see http://www.evanmiller.org/ranking-items-with-star-ratings.html |
|
142 | + * @see https://stackoverflow.com/questions/1411199/what-is-a-better-way-to-sort-by-a-5-star-rating/1411268 |
|
143 | + * @see http://julesjacobs.github.io/2015/08/17/bayesian-scoring-of-ratings.html |
|
144 | + * @param int $confidencePercentage |
|
145 | + * @return float |
|
146 | + */ |
|
147 | + public function getRankingUsingZScores(array $ratingCounts, $confidencePercentage = 90) |
|
148 | + { |
|
149 | + $ratingCountsSum = array_sum($ratingCounts) + glsr()->constant('MAX_RATING', __CLASS__); |
|
150 | + $weight = $this->getWeight($ratingCounts, $ratingCountsSum); |
|
151 | + $weightPow2 = $this->getWeight($ratingCounts, $ratingCountsSum, true); |
|
152 | + $zScore = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
|
153 | + return $weight - $zScore * sqrt(($weightPow2 - pow($weight, 2)) / ($ratingCountsSum + 1)); |
|
154 | + } |
|
155 | 155 | |
156 | - /** |
|
157 | - * @param int $target |
|
158 | - * @return array |
|
159 | - */ |
|
160 | - protected function getRoundedPercentages(array $percentages, $totalPercent = 100) |
|
161 | - { |
|
162 | - array_walk($percentages, function (&$percent, $index) { |
|
163 | - $percent = [ |
|
164 | - 'index' => $index, |
|
165 | - 'percent' => floor($percent), |
|
166 | - 'remainder' => fmod($percent, 1), |
|
167 | - ]; |
|
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); |
|
172 | - $i = 0; |
|
173 | - if (array_sum(glsr_array_column($percentages, 'percent')) > 0) { |
|
174 | - while (array_sum(glsr_array_column($percentages, 'percent')) < $totalPercent) { |
|
175 | - ++$percentages[$i]['percent']; |
|
176 | - ++$i; |
|
177 | - } |
|
178 | - } |
|
179 | - array_multisort($indexes, SORT_DESC, $percentages); |
|
180 | - return array_combine($indexes, glsr_array_column($percentages, 'percent')); |
|
181 | - } |
|
156 | + /** |
|
157 | + * @param int $target |
|
158 | + * @return array |
|
159 | + */ |
|
160 | + protected function getRoundedPercentages(array $percentages, $totalPercent = 100) |
|
161 | + { |
|
162 | + array_walk($percentages, function (&$percent, $index) { |
|
163 | + $percent = [ |
|
164 | + 'index' => $index, |
|
165 | + 'percent' => floor($percent), |
|
166 | + 'remainder' => fmod($percent, 1), |
|
167 | + ]; |
|
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); |
|
172 | + $i = 0; |
|
173 | + if (array_sum(glsr_array_column($percentages, 'percent')) > 0) { |
|
174 | + while (array_sum(glsr_array_column($percentages, 'percent')) < $totalPercent) { |
|
175 | + ++$percentages[$i]['percent']; |
|
176 | + ++$i; |
|
177 | + } |
|
178 | + } |
|
179 | + array_multisort($indexes, SORT_DESC, $percentages); |
|
180 | + return array_combine($indexes, glsr_array_column($percentages, 'percent')); |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * @return int |
|
185 | - */ |
|
186 | - protected function getTotalSum(array $ratingCounts) |
|
187 | - { |
|
188 | - return array_reduce(array_keys($ratingCounts), function ($carry, $index) use ($ratingCounts) { |
|
189 | - return $carry + ($index * $ratingCounts[$index]); |
|
190 | - }); |
|
191 | - } |
|
183 | + /** |
|
184 | + * @return int |
|
185 | + */ |
|
186 | + protected function getTotalSum(array $ratingCounts) |
|
187 | + { |
|
188 | + return array_reduce(array_keys($ratingCounts), function ($carry, $index) use ($ratingCounts) { |
|
189 | + return $carry + ($index * $ratingCounts[$index]); |
|
190 | + }); |
|
191 | + } |
|
192 | 192 | |
193 | - /** |
|
194 | - * @param int|float $ratingCountsSum |
|
195 | - * @param bool $powerOf2 |
|
196 | - * @return float |
|
197 | - */ |
|
198 | - protected function getWeight(array $ratingCounts, $ratingCountsSum, $powerOf2 = false) |
|
199 | - { |
|
200 | - return array_reduce(array_keys($ratingCounts), |
|
201 | - function ($count, $rating) use ($ratingCounts, $ratingCountsSum, $powerOf2) { |
|
202 | - $ratingLevel = $powerOf2 |
|
203 | - ? pow($rating, 2) |
|
204 | - : $rating; |
|
205 | - return $count + ($ratingLevel * ($ratingCounts[$rating] + 1)) / $ratingCountsSum; |
|
206 | - } |
|
207 | - ); |
|
208 | - } |
|
193 | + /** |
|
194 | + * @param int|float $ratingCountsSum |
|
195 | + * @param bool $powerOf2 |
|
196 | + * @return float |
|
197 | + */ |
|
198 | + protected function getWeight(array $ratingCounts, $ratingCountsSum, $powerOf2 = false) |
|
199 | + { |
|
200 | + return array_reduce(array_keys($ratingCounts), |
|
201 | + function ($count, $rating) use ($ratingCounts, $ratingCountsSum, $powerOf2) { |
|
202 | + $ratingLevel = $powerOf2 |
|
203 | + ? pow($rating, 2) |
|
204 | + : $rating; |
|
205 | + return $count + ($ratingLevel * ($ratingCounts[$rating] + 1)) / $ratingCountsSum; |
|
206 | + } |
|
207 | + ); |
|
208 | + } |
|
209 | 209 | } |