@@ -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 | } |
@@ -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 | } |
@@ -12,288 +12,288 @@ |
||
12 | 12 | |
13 | 13 | class ValidateReview |
14 | 14 | { |
15 | - const RECAPTCHA_ENDPOINT = 'https://www.google.com/recaptcha/api/siteverify'; |
|
15 | + const RECAPTCHA_ENDPOINT = 'https://www.google.com/recaptcha/api/siteverify'; |
|
16 | 16 | |
17 | - const RECAPTCHA_DISABLED = 0; |
|
18 | - const RECAPTCHA_EMPTY = 1; |
|
19 | - const RECAPTCHA_FAILED = 2; |
|
20 | - const RECAPTCHA_INVALID = 3; |
|
21 | - const RECAPTCHA_VALID = 4; |
|
17 | + const RECAPTCHA_DISABLED = 0; |
|
18 | + const RECAPTCHA_EMPTY = 1; |
|
19 | + const RECAPTCHA_FAILED = 2; |
|
20 | + const RECAPTCHA_INVALID = 3; |
|
21 | + const RECAPTCHA_VALID = 4; |
|
22 | 22 | |
23 | - const VALIDATION_RULES = [ |
|
24 | - 'content' => 'required', |
|
25 | - 'email' => 'required|email', |
|
26 | - 'name' => 'required', |
|
27 | - 'rating' => 'required|number|between:1,5', |
|
28 | - 'terms' => 'accepted', |
|
29 | - 'title' => 'required', |
|
30 | - ]; |
|
23 | + const VALIDATION_RULES = [ |
|
24 | + 'content' => 'required', |
|
25 | + 'email' => 'required|email', |
|
26 | + 'name' => 'required', |
|
27 | + 'rating' => 'required|number|between:1,5', |
|
28 | + 'terms' => 'accepted', |
|
29 | + 'title' => 'required', |
|
30 | + ]; |
|
31 | 31 | |
32 | - /** |
|
33 | - * @var string|void |
|
34 | - */ |
|
35 | - public $error; |
|
32 | + /** |
|
33 | + * @var string|void |
|
34 | + */ |
|
35 | + public $error; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @var string |
|
39 | - */ |
|
40 | - public $form_id; |
|
37 | + /** |
|
38 | + * @var string |
|
39 | + */ |
|
40 | + public $form_id; |
|
41 | 41 | |
42 | - /** |
|
43 | - * @var bool |
|
44 | - */ |
|
45 | - public $recaptchaIsUnset = false; |
|
42 | + /** |
|
43 | + * @var bool |
|
44 | + */ |
|
45 | + public $recaptchaIsUnset = false; |
|
46 | 46 | |
47 | - /** |
|
48 | - * @var array |
|
49 | - */ |
|
50 | - public $request; |
|
47 | + /** |
|
48 | + * @var array |
|
49 | + */ |
|
50 | + public $request; |
|
51 | 51 | |
52 | - /** |
|
53 | - * @var array |
|
54 | - */ |
|
55 | - protected $options; |
|
52 | + /** |
|
53 | + * @var array |
|
54 | + */ |
|
55 | + protected $options; |
|
56 | 56 | |
57 | - /** |
|
58 | - * @return static |
|
59 | - */ |
|
60 | - public function validate(array $request) |
|
61 | - { |
|
62 | - $this->form_id = $request['form_id']; |
|
63 | - $this->options = glsr(OptionManager::class)->all(); |
|
64 | - $this->request = $this->validateRequest($request); |
|
65 | - $this->validateCustom(); |
|
66 | - $this->validateHoneyPot(); |
|
67 | - $this->validateReviewLimits(); |
|
68 | - $this->validateBlacklist(); |
|
69 | - $this->validateAkismet(); |
|
70 | - $this->validateRecaptcha(); |
|
71 | - if (!empty($this->error)) { |
|
72 | - $this->setSessionValues('message', $this->error); |
|
73 | - } |
|
74 | - return $this; |
|
75 | - } |
|
57 | + /** |
|
58 | + * @return static |
|
59 | + */ |
|
60 | + public function validate(array $request) |
|
61 | + { |
|
62 | + $this->form_id = $request['form_id']; |
|
63 | + $this->options = glsr(OptionManager::class)->all(); |
|
64 | + $this->request = $this->validateRequest($request); |
|
65 | + $this->validateCustom(); |
|
66 | + $this->validateHoneyPot(); |
|
67 | + $this->validateReviewLimits(); |
|
68 | + $this->validateBlacklist(); |
|
69 | + $this->validateAkismet(); |
|
70 | + $this->validateRecaptcha(); |
|
71 | + if (!empty($this->error)) { |
|
72 | + $this->setSessionValues('message', $this->error); |
|
73 | + } |
|
74 | + return $this; |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * @param string $path |
|
79 | - * @param mixed $fallback |
|
80 | - * @return mixed |
|
81 | - */ |
|
82 | - protected function getOption($path, $fallback = '') |
|
83 | - { |
|
84 | - return glsr(Helper::class)->dataGet($this->options, $path, $fallback); |
|
85 | - } |
|
77 | + /** |
|
78 | + * @param string $path |
|
79 | + * @param mixed $fallback |
|
80 | + * @return mixed |
|
81 | + */ |
|
82 | + protected function getOption($path, $fallback = '') |
|
83 | + { |
|
84 | + return glsr(Helper::class)->dataGet($this->options, $path, $fallback); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * @return int |
|
89 | - */ |
|
90 | - protected function getRecaptchaStatus() |
|
91 | - { |
|
92 | - if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
93 | - return static::RECAPTCHA_DISABLED; |
|
94 | - } |
|
95 | - if (empty($this->request['_recaptcha-token'])) { |
|
96 | - return $this->request['_counter'] < intval(apply_filters('site-reviews/recaptcha/timeout', 5)) |
|
97 | - ? static::RECAPTCHA_EMPTY |
|
98 | - : static::RECAPTCHA_FAILED; |
|
99 | - } |
|
100 | - return $this->getRecaptchaTokenStatus(); |
|
101 | - } |
|
87 | + /** |
|
88 | + * @return int |
|
89 | + */ |
|
90 | + protected function getRecaptchaStatus() |
|
91 | + { |
|
92 | + if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
93 | + return static::RECAPTCHA_DISABLED; |
|
94 | + } |
|
95 | + if (empty($this->request['_recaptcha-token'])) { |
|
96 | + return $this->request['_counter'] < intval(apply_filters('site-reviews/recaptcha/timeout', 5)) |
|
97 | + ? static::RECAPTCHA_EMPTY |
|
98 | + : static::RECAPTCHA_FAILED; |
|
99 | + } |
|
100 | + return $this->getRecaptchaTokenStatus(); |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * @return int |
|
105 | - */ |
|
106 | - protected function getRecaptchaTokenStatus() |
|
107 | - { |
|
108 | - $endpoint = add_query_arg([ |
|
109 | - 'remoteip' => glsr(Helper::class)->getIpAddress(), |
|
110 | - 'response' => $this->request['_recaptcha-token'], |
|
111 | - 'secret' => $this->getOption('settings.submissions.recaptcha.secret'), |
|
112 | - ], static::RECAPTCHA_ENDPOINT); |
|
113 | - if (is_wp_error($response = wp_remote_get($endpoint))) { |
|
114 | - glsr_log()->error($response->get_error_message()); |
|
115 | - return static::RECAPTCHA_FAILED; |
|
116 | - } |
|
117 | - $response = json_decode(wp_remote_retrieve_body($response)); |
|
118 | - if (!empty($response->success)) { |
|
119 | - return boolval($response->success) |
|
120 | - ? static::RECAPTCHA_VALID |
|
121 | - : static::RECAPTCHA_INVALID; |
|
122 | - } |
|
123 | - foreach ($response->{'error-codes'} as $error) { |
|
124 | - glsr_log()->error('reCAPTCHA error: '.$error); |
|
125 | - } |
|
126 | - return static::RECAPTCHA_INVALID; |
|
127 | - } |
|
103 | + /** |
|
104 | + * @return int |
|
105 | + */ |
|
106 | + protected function getRecaptchaTokenStatus() |
|
107 | + { |
|
108 | + $endpoint = add_query_arg([ |
|
109 | + 'remoteip' => glsr(Helper::class)->getIpAddress(), |
|
110 | + 'response' => $this->request['_recaptcha-token'], |
|
111 | + 'secret' => $this->getOption('settings.submissions.recaptcha.secret'), |
|
112 | + ], static::RECAPTCHA_ENDPOINT); |
|
113 | + if (is_wp_error($response = wp_remote_get($endpoint))) { |
|
114 | + glsr_log()->error($response->get_error_message()); |
|
115 | + return static::RECAPTCHA_FAILED; |
|
116 | + } |
|
117 | + $response = json_decode(wp_remote_retrieve_body($response)); |
|
118 | + if (!empty($response->success)) { |
|
119 | + return boolval($response->success) |
|
120 | + ? static::RECAPTCHA_VALID |
|
121 | + : static::RECAPTCHA_INVALID; |
|
122 | + } |
|
123 | + foreach ($response->{'error-codes'} as $error) { |
|
124 | + glsr_log()->error('reCAPTCHA error: '.$error); |
|
125 | + } |
|
126 | + return static::RECAPTCHA_INVALID; |
|
127 | + } |
|
128 | 128 | |
129 | - /** |
|
130 | - * @return array |
|
131 | - */ |
|
132 | - protected function getValidationRules(array $request) |
|
133 | - { |
|
134 | - $rules = array_intersect_key( |
|
135 | - apply_filters('site-reviews/validation/rules', static::VALIDATION_RULES, $request), |
|
136 | - array_flip($this->getOption('settings.submissions.required', [])) |
|
137 | - ); |
|
138 | - $excluded = explode(',', glsr_get($request, 'excluded')); |
|
139 | - return array_diff_key($rules, array_flip($excluded)); |
|
140 | - } |
|
129 | + /** |
|
130 | + * @return array |
|
131 | + */ |
|
132 | + protected function getValidationRules(array $request) |
|
133 | + { |
|
134 | + $rules = array_intersect_key( |
|
135 | + apply_filters('site-reviews/validation/rules', static::VALIDATION_RULES, $request), |
|
136 | + array_flip($this->getOption('settings.submissions.required', [])) |
|
137 | + ); |
|
138 | + $excluded = explode(',', glsr_get($request, 'excluded')); |
|
139 | + return array_diff_key($rules, array_flip($excluded)); |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * @return bool |
|
144 | - */ |
|
145 | - protected function isRequestValid(array $request) |
|
146 | - { |
|
147 | - $rules = $this->getValidationRules($request); |
|
148 | - $errors = glsr(Validator::class)->validate($request, $rules); |
|
149 | - if (empty($errors)) { |
|
150 | - return true; |
|
151 | - } |
|
152 | - $this->setSessionValues('errors', $errors); |
|
153 | - $this->setSessionValues('values', $request); |
|
154 | - return false; |
|
155 | - } |
|
142 | + /** |
|
143 | + * @return bool |
|
144 | + */ |
|
145 | + protected function isRequestValid(array $request) |
|
146 | + { |
|
147 | + $rules = $this->getValidationRules($request); |
|
148 | + $errors = glsr(Validator::class)->validate($request, $rules); |
|
149 | + if (empty($errors)) { |
|
150 | + return true; |
|
151 | + } |
|
152 | + $this->setSessionValues('errors', $errors); |
|
153 | + $this->setSessionValues('values', $request); |
|
154 | + return false; |
|
155 | + } |
|
156 | 156 | |
157 | - protected function setError($message, $loggedMessage = '') |
|
158 | - { |
|
159 | - $this->setSessionValues('errors', [], $loggedMessage); |
|
160 | - $this->error = $message; |
|
161 | - } |
|
157 | + protected function setError($message, $loggedMessage = '') |
|
158 | + { |
|
159 | + $this->setSessionValues('errors', [], $loggedMessage); |
|
160 | + $this->error = $message; |
|
161 | + } |
|
162 | 162 | |
163 | - /** |
|
164 | - * @param string $type |
|
165 | - * @param mixed $value |
|
166 | - * @param string $loggedMessage |
|
167 | - * @return void |
|
168 | - */ |
|
169 | - protected function setSessionValues($type, $value, $loggedMessage = '') |
|
170 | - { |
|
171 | - glsr()->sessionSet($this->form_id.$type, $value); |
|
172 | - if (!empty($loggedMessage)) { |
|
173 | - glsr_log()->warning($loggedMessage)->debug($this->request); |
|
174 | - } |
|
175 | - } |
|
163 | + /** |
|
164 | + * @param string $type |
|
165 | + * @param mixed $value |
|
166 | + * @param string $loggedMessage |
|
167 | + * @return void |
|
168 | + */ |
|
169 | + protected function setSessionValues($type, $value, $loggedMessage = '') |
|
170 | + { |
|
171 | + glsr()->sessionSet($this->form_id.$type, $value); |
|
172 | + if (!empty($loggedMessage)) { |
|
173 | + glsr_log()->warning($loggedMessage)->debug($this->request); |
|
174 | + } |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * @return void |
|
179 | - */ |
|
180 | - protected function validateAkismet() |
|
181 | - { |
|
182 | - if (!empty($this->error)) { |
|
183 | - return; |
|
184 | - } |
|
185 | - if (!glsr(Akismet::class)->isSpam($this->request)) { |
|
186 | - return; |
|
187 | - } |
|
188 | - $this->setError(__('This review has been flagged as possible spam and cannot be submitted.', 'site-reviews'), |
|
189 | - 'Akismet caught a spam submission (consider adding the IP address to the blacklist):' |
|
190 | - ); |
|
191 | - } |
|
177 | + /** |
|
178 | + * @return void |
|
179 | + */ |
|
180 | + protected function validateAkismet() |
|
181 | + { |
|
182 | + if (!empty($this->error)) { |
|
183 | + return; |
|
184 | + } |
|
185 | + if (!glsr(Akismet::class)->isSpam($this->request)) { |
|
186 | + return; |
|
187 | + } |
|
188 | + $this->setError(__('This review has been flagged as possible spam and cannot be submitted.', 'site-reviews'), |
|
189 | + 'Akismet caught a spam submission (consider adding the IP address to the blacklist):' |
|
190 | + ); |
|
191 | + } |
|
192 | 192 | |
193 | - /** |
|
194 | - * @return void |
|
195 | - */ |
|
196 | - protected function validateBlacklist() |
|
197 | - { |
|
198 | - if (!empty($this->error)) { |
|
199 | - return; |
|
200 | - } |
|
201 | - if (!glsr(Blacklist::class)->isBlacklisted($this->request)) { |
|
202 | - return; |
|
203 | - } |
|
204 | - $blacklistAction = $this->getOption('settings.submissions.blacklist.action'); |
|
205 | - if ('reject' != $blacklistAction) { |
|
206 | - $this->request['blacklisted'] = true; |
|
207 | - return; |
|
208 | - } |
|
209 | - $this->setError(__('Your review cannot be submitted at this time.', 'site-reviews'), |
|
210 | - 'Blacklisted submission detected:' |
|
211 | - ); |
|
212 | - } |
|
193 | + /** |
|
194 | + * @return void |
|
195 | + */ |
|
196 | + protected function validateBlacklist() |
|
197 | + { |
|
198 | + if (!empty($this->error)) { |
|
199 | + return; |
|
200 | + } |
|
201 | + if (!glsr(Blacklist::class)->isBlacklisted($this->request)) { |
|
202 | + return; |
|
203 | + } |
|
204 | + $blacklistAction = $this->getOption('settings.submissions.blacklist.action'); |
|
205 | + if ('reject' != $blacklistAction) { |
|
206 | + $this->request['blacklisted'] = true; |
|
207 | + return; |
|
208 | + } |
|
209 | + $this->setError(__('Your review cannot be submitted at this time.', 'site-reviews'), |
|
210 | + 'Blacklisted submission detected:' |
|
211 | + ); |
|
212 | + } |
|
213 | 213 | |
214 | - /** |
|
215 | - * @return void |
|
216 | - */ |
|
217 | - protected function validateCustom() |
|
218 | - { |
|
219 | - if (!empty($this->error)) { |
|
220 | - return; |
|
221 | - } |
|
222 | - $validated = apply_filters('site-reviews/validate/custom', true, $this->request); |
|
223 | - if (true === $validated) { |
|
224 | - return; |
|
225 | - } |
|
226 | - $errorMessage = is_string($validated) |
|
227 | - ? $validated |
|
228 | - : __('The review submission failed. Please notify the site administrator.', 'site-reviews'); |
|
229 | - $this->setError($errorMessage); |
|
230 | - $this->setSessionValues('values', $this->request); |
|
231 | - } |
|
214 | + /** |
|
215 | + * @return void |
|
216 | + */ |
|
217 | + protected function validateCustom() |
|
218 | + { |
|
219 | + if (!empty($this->error)) { |
|
220 | + return; |
|
221 | + } |
|
222 | + $validated = apply_filters('site-reviews/validate/custom', true, $this->request); |
|
223 | + if (true === $validated) { |
|
224 | + return; |
|
225 | + } |
|
226 | + $errorMessage = is_string($validated) |
|
227 | + ? $validated |
|
228 | + : __('The review submission failed. Please notify the site administrator.', 'site-reviews'); |
|
229 | + $this->setError($errorMessage); |
|
230 | + $this->setSessionValues('values', $this->request); |
|
231 | + } |
|
232 | 232 | |
233 | - /** |
|
234 | - * @return void |
|
235 | - */ |
|
236 | - protected function validateHoneyPot() |
|
237 | - { |
|
238 | - if (!empty($this->error)) { |
|
239 | - return; |
|
240 | - } |
|
241 | - if (empty($this->request['gotcha'])) { |
|
242 | - return; |
|
243 | - } |
|
244 | - $this->setError(__('The review submission failed. Please notify the site administrator.', 'site-reviews'), |
|
245 | - 'The Honeypot caught a bad submission:' |
|
246 | - ); |
|
247 | - } |
|
233 | + /** |
|
234 | + * @return void |
|
235 | + */ |
|
236 | + protected function validateHoneyPot() |
|
237 | + { |
|
238 | + if (!empty($this->error)) { |
|
239 | + return; |
|
240 | + } |
|
241 | + if (empty($this->request['gotcha'])) { |
|
242 | + return; |
|
243 | + } |
|
244 | + $this->setError(__('The review submission failed. Please notify the site administrator.', 'site-reviews'), |
|
245 | + 'The Honeypot caught a bad submission:' |
|
246 | + ); |
|
247 | + } |
|
248 | 248 | |
249 | - /** |
|
250 | - * @return void |
|
251 | - */ |
|
252 | - protected function validateReviewLimits() |
|
253 | - { |
|
254 | - if (!empty($this->error)) { |
|
255 | - return; |
|
256 | - } |
|
257 | - if (!glsr(ReviewLimits::class)->hasReachedLimit($this->request)) { |
|
258 | - return; |
|
259 | - } |
|
260 | - $this->setError(__('You have already submitted a review.', 'site-reviews')); |
|
261 | - } |
|
249 | + /** |
|
250 | + * @return void |
|
251 | + */ |
|
252 | + protected function validateReviewLimits() |
|
253 | + { |
|
254 | + if (!empty($this->error)) { |
|
255 | + return; |
|
256 | + } |
|
257 | + if (!glsr(ReviewLimits::class)->hasReachedLimit($this->request)) { |
|
258 | + return; |
|
259 | + } |
|
260 | + $this->setError(__('You have already submitted a review.', 'site-reviews')); |
|
261 | + } |
|
262 | 262 | |
263 | - /** |
|
264 | - * @return void |
|
265 | - */ |
|
266 | - protected function validateRecaptcha() |
|
267 | - { |
|
268 | - if (!empty($this->error)) { |
|
269 | - return; |
|
270 | - } |
|
271 | - $status = $this->getRecaptchaStatus(); |
|
272 | - if (in_array($status, [static::RECAPTCHA_DISABLED, static::RECAPTCHA_VALID])) { |
|
273 | - return; |
|
274 | - } |
|
275 | - if (static::RECAPTCHA_EMPTY === $status) { |
|
276 | - $this->setSessionValues('recaptcha', 'unset'); |
|
277 | - $this->recaptchaIsUnset = true; |
|
278 | - return; |
|
279 | - } |
|
280 | - $this->setSessionValues('recaptcha', 'reset'); |
|
281 | - $errors = [ |
|
282 | - static::RECAPTCHA_FAILED => __('The reCAPTCHA failed to load, please refresh the page and try again.', 'site-reviews'), |
|
283 | - static::RECAPTCHA_INVALID => __('The reCAPTCHA verification failed, please try again.', 'site-reviews'), |
|
284 | - ]; |
|
285 | - $this->setError($errors[$status]); |
|
286 | - } |
|
263 | + /** |
|
264 | + * @return void |
|
265 | + */ |
|
266 | + protected function validateRecaptcha() |
|
267 | + { |
|
268 | + if (!empty($this->error)) { |
|
269 | + return; |
|
270 | + } |
|
271 | + $status = $this->getRecaptchaStatus(); |
|
272 | + if (in_array($status, [static::RECAPTCHA_DISABLED, static::RECAPTCHA_VALID])) { |
|
273 | + return; |
|
274 | + } |
|
275 | + if (static::RECAPTCHA_EMPTY === $status) { |
|
276 | + $this->setSessionValues('recaptcha', 'unset'); |
|
277 | + $this->recaptchaIsUnset = true; |
|
278 | + return; |
|
279 | + } |
|
280 | + $this->setSessionValues('recaptcha', 'reset'); |
|
281 | + $errors = [ |
|
282 | + static::RECAPTCHA_FAILED => __('The reCAPTCHA failed to load, please refresh the page and try again.', 'site-reviews'), |
|
283 | + static::RECAPTCHA_INVALID => __('The reCAPTCHA verification failed, please try again.', 'site-reviews'), |
|
284 | + ]; |
|
285 | + $this->setError($errors[$status]); |
|
286 | + } |
|
287 | 287 | |
288 | - /** |
|
289 | - * @return array |
|
290 | - */ |
|
291 | - protected function validateRequest(array $request) |
|
292 | - { |
|
293 | - if (!$this->isRequestValid($request)) { |
|
294 | - $this->error = __('Please fix the submission errors.', 'site-reviews'); |
|
295 | - return $request; |
|
296 | - } |
|
297 | - return array_merge(glsr(ValidateReviewDefaults::class)->defaults(), $request); |
|
298 | - } |
|
288 | + /** |
|
289 | + * @return array |
|
290 | + */ |
|
291 | + protected function validateRequest(array $request) |
|
292 | + { |
|
293 | + if (!$this->isRequestValid($request)) { |
|
294 | + $this->error = __('Please fix the submission errors.', 'site-reviews'); |
|
295 | + return $request; |
|
296 | + } |
|
297 | + return array_merge(glsr(ValidateReviewDefaults::class)->defaults(), $request); |
|
298 | + } |
|
299 | 299 | } |
@@ -7,103 +7,103 @@ |
||
7 | 7 | |
8 | 8 | class ReviewLimits |
9 | 9 | { |
10 | - protected $request; |
|
10 | + protected $request; |
|
11 | 11 | |
12 | - /** |
|
13 | - * @return array |
|
14 | - * @filter site-reviews/get/reviews/query |
|
15 | - */ |
|
16 | - public function filterReviewsQuery(array $parameters, array $args) |
|
17 | - { |
|
18 | - if ($authorId = get_current_user_id()) { |
|
19 | - $parameters['author'] = $authorId; |
|
20 | - } |
|
21 | - $parameters['post_status'] = ['pending', 'publish']; |
|
22 | - return apply_filters('site-reviews/reviews-limits/query', $parameters, $args); |
|
23 | - } |
|
12 | + /** |
|
13 | + * @return array |
|
14 | + * @filter site-reviews/get/reviews/query |
|
15 | + */ |
|
16 | + public function filterReviewsQuery(array $parameters, array $args) |
|
17 | + { |
|
18 | + if ($authorId = get_current_user_id()) { |
|
19 | + $parameters['author'] = $authorId; |
|
20 | + } |
|
21 | + $parameters['post_status'] = ['pending', 'publish']; |
|
22 | + return apply_filters('site-reviews/reviews-limits/query', $parameters, $args); |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * @return bool |
|
27 | - */ |
|
28 | - public function hasReachedLimit(array $request = []) |
|
29 | - { |
|
30 | - $this->request = $request; |
|
31 | - $method = glsr(Helper::class)->buildMethodName( |
|
32 | - glsr(OptionManager::class)->get('settings.submissions.limit'), 'validateBy' |
|
33 | - ); |
|
34 | - return method_exists($this, $method) |
|
35 | - ? !call_user_func([$this, $method]) |
|
36 | - : false; |
|
37 | - } |
|
25 | + /** |
|
26 | + * @return bool |
|
27 | + */ |
|
28 | + public function hasReachedLimit(array $request = []) |
|
29 | + { |
|
30 | + $this->request = $request; |
|
31 | + $method = glsr(Helper::class)->buildMethodName( |
|
32 | + glsr(OptionManager::class)->get('settings.submissions.limit'), 'validateBy' |
|
33 | + ); |
|
34 | + return method_exists($this, $method) |
|
35 | + ? !call_user_func([$this, $method]) |
|
36 | + : false; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @param string $value |
|
41 | - * @param string $whitelist |
|
42 | - * @return bool |
|
43 | - */ |
|
44 | - public function isWhitelisted($value, $whitelist) |
|
45 | - { |
|
46 | - if (empty($whitelist)) { |
|
47 | - return false; |
|
48 | - } |
|
49 | - return in_array($value, array_filter(explode("\n", $whitelist), 'trim')); |
|
50 | - } |
|
39 | + /** |
|
40 | + * @param string $value |
|
41 | + * @param string $whitelist |
|
42 | + * @return bool |
|
43 | + */ |
|
44 | + public function isWhitelisted($value, $whitelist) |
|
45 | + { |
|
46 | + if (empty($whitelist)) { |
|
47 | + return false; |
|
48 | + } |
|
49 | + return in_array($value, array_filter(explode("\n", $whitelist), 'trim')); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * @param string $whitelistName |
|
54 | - * @return string |
|
55 | - */ |
|
56 | - protected function getWhitelist($whitelistName) |
|
57 | - { |
|
58 | - return glsr(OptionManager::class)->get('settings.submissions.limit_whitelist.'.$whitelistName); |
|
59 | - } |
|
52 | + /** |
|
53 | + * @param string $whitelistName |
|
54 | + * @return string |
|
55 | + */ |
|
56 | + protected function getWhitelist($whitelistName) |
|
57 | + { |
|
58 | + return glsr(OptionManager::class)->get('settings.submissions.limit_whitelist.'.$whitelistName); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * @return bool |
|
63 | - */ |
|
64 | - protected function validate($key, $value, $addMetaQuery = true) |
|
65 | - { |
|
66 | - if ($this->isWhitelisted($value, $this->getWhitelist($key))) { |
|
67 | - return true; |
|
68 | - } |
|
69 | - add_filter('site-reviews/get/reviews/query', [$this, 'filterReviewsQuery'], 5, 2); |
|
70 | - $args = ['assigned_to' => glsr_get($this->request, 'assign_to')]; |
|
71 | - if ($addMetaQuery) { |
|
72 | - $args[$key] = $value; |
|
73 | - } |
|
74 | - $reviews = glsr_get_reviews($args); |
|
75 | - remove_filter('site-reviews/get/reviews/query', [$this, 'filterReviewsQuery'], 5); |
|
76 | - return 0 === count($reviews); |
|
77 | - } |
|
61 | + /** |
|
62 | + * @return bool |
|
63 | + */ |
|
64 | + protected function validate($key, $value, $addMetaQuery = true) |
|
65 | + { |
|
66 | + if ($this->isWhitelisted($value, $this->getWhitelist($key))) { |
|
67 | + return true; |
|
68 | + } |
|
69 | + add_filter('site-reviews/get/reviews/query', [$this, 'filterReviewsQuery'], 5, 2); |
|
70 | + $args = ['assigned_to' => glsr_get($this->request, 'assign_to')]; |
|
71 | + if ($addMetaQuery) { |
|
72 | + $args[$key] = $value; |
|
73 | + } |
|
74 | + $reviews = glsr_get_reviews($args); |
|
75 | + remove_filter('site-reviews/get/reviews/query', [$this, 'filterReviewsQuery'], 5); |
|
76 | + return 0 === count($reviews); |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @return bool |
|
81 | - */ |
|
82 | - protected function validateByEmail() |
|
83 | - { |
|
84 | - glsr_log()->debug('Email is: '.glsr_get($this->request, 'email')); |
|
85 | - return $this->validate('email', glsr_get($this->request, 'email')); |
|
86 | - } |
|
79 | + /** |
|
80 | + * @return bool |
|
81 | + */ |
|
82 | + protected function validateByEmail() |
|
83 | + { |
|
84 | + glsr_log()->debug('Email is: '.glsr_get($this->request, 'email')); |
|
85 | + return $this->validate('email', glsr_get($this->request, 'email')); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * @return bool |
|
90 | - */ |
|
91 | - protected function validateByIpAddress() |
|
92 | - { |
|
93 | - glsr_log()->debug('IP Address is: '.glsr_get($this->request, 'ip_address')); |
|
94 | - return $this->validate('ip_address', glsr_get($this->request, 'ip_address')); |
|
95 | - } |
|
88 | + /** |
|
89 | + * @return bool |
|
90 | + */ |
|
91 | + protected function validateByIpAddress() |
|
92 | + { |
|
93 | + glsr_log()->debug('IP Address is: '.glsr_get($this->request, 'ip_address')); |
|
94 | + return $this->validate('ip_address', glsr_get($this->request, 'ip_address')); |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * @return bool |
|
99 | - */ |
|
100 | - protected function validateByUsername() |
|
101 | - { |
|
102 | - $user = wp_get_current_user(); |
|
103 | - if (!$user->exists()) { |
|
104 | - return true; |
|
105 | - } |
|
106 | - glsr_log()->debug('Username is: '.$user->user_login); |
|
107 | - return $this->validate('username', $user->user_login, false); |
|
108 | - } |
|
97 | + /** |
|
98 | + * @return bool |
|
99 | + */ |
|
100 | + protected function validateByUsername() |
|
101 | + { |
|
102 | + $user = wp_get_current_user(); |
|
103 | + if (!$user->exists()) { |
|
104 | + return true; |
|
105 | + } |
|
106 | + glsr_log()->debug('Username is: '.$user->user_login); |
|
107 | + return $this->validate('username', $user->user_login, false); |
|
108 | + } |
|
109 | 109 | } |
@@ -10,158 +10,158 @@ |
||
10 | 10 | |
11 | 11 | class EnqueueAdminAssets |
12 | 12 | { |
13 | - /** |
|
14 | - * @var array |
|
15 | - */ |
|
16 | - protected $pointers; |
|
13 | + /** |
|
14 | + * @var array |
|
15 | + */ |
|
16 | + protected $pointers; |
|
17 | 17 | |
18 | - /** |
|
19 | - * @return void |
|
20 | - */ |
|
21 | - public function handle(Command $command) |
|
22 | - { |
|
23 | - $this->generatePointers($command->pointers); |
|
24 | - $this->enqueueAssets(); |
|
25 | - $this->localizeAssets(); |
|
26 | - } |
|
18 | + /** |
|
19 | + * @return void |
|
20 | + */ |
|
21 | + public function handle(Command $command) |
|
22 | + { |
|
23 | + $this->generatePointers($command->pointers); |
|
24 | + $this->enqueueAssets(); |
|
25 | + $this->localizeAssets(); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * @return void |
|
30 | - */ |
|
31 | - public function enqueueAssets() |
|
32 | - { |
|
33 | - wp_enqueue_style( |
|
34 | - Application::ID, |
|
35 | - glsr()->url('assets/styles/'.Application::ID.'-admin.css'), |
|
36 | - [], |
|
37 | - glsr()->version |
|
38 | - ); |
|
39 | - if (!$this->isCurrentScreen()) { |
|
40 | - return; |
|
41 | - } |
|
42 | - wp_enqueue_script( |
|
43 | - Application::ID, |
|
44 | - glsr()->url('assets/scripts/'.Application::ID.'-admin.js'), |
|
45 | - $this->getDependencies(), |
|
46 | - glsr()->version, |
|
47 | - true |
|
48 | - ); |
|
49 | - if (!empty($this->pointers)) { |
|
50 | - wp_enqueue_style('wp-pointer'); |
|
51 | - wp_enqueue_script('wp-pointer'); |
|
52 | - } |
|
53 | - } |
|
28 | + /** |
|
29 | + * @return void |
|
30 | + */ |
|
31 | + public function enqueueAssets() |
|
32 | + { |
|
33 | + wp_enqueue_style( |
|
34 | + Application::ID, |
|
35 | + glsr()->url('assets/styles/'.Application::ID.'-admin.css'), |
|
36 | + [], |
|
37 | + glsr()->version |
|
38 | + ); |
|
39 | + if (!$this->isCurrentScreen()) { |
|
40 | + return; |
|
41 | + } |
|
42 | + wp_enqueue_script( |
|
43 | + Application::ID, |
|
44 | + glsr()->url('assets/scripts/'.Application::ID.'-admin.js'), |
|
45 | + $this->getDependencies(), |
|
46 | + glsr()->version, |
|
47 | + true |
|
48 | + ); |
|
49 | + if (!empty($this->pointers)) { |
|
50 | + wp_enqueue_style('wp-pointer'); |
|
51 | + wp_enqueue_script('wp-pointer'); |
|
52 | + } |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @return void |
|
57 | - */ |
|
58 | - public function localizeAssets() |
|
59 | - { |
|
60 | - $variables = [ |
|
61 | - 'action' => Application::PREFIX.'action', |
|
62 | - 'addons' => [], |
|
63 | - 'ajaxurl' => admin_url('admin-ajax.php'), |
|
64 | - 'hideoptions' => [ |
|
65 | - 'site_reviews' => glsr(SiteReviewsShortcode::class)->getHideOptions(), |
|
66 | - 'site_reviews_form' => glsr(SiteReviewsFormShortcode::class)->getHideOptions(), |
|
67 | - 'site_reviews_summary' => glsr(SiteReviewsSummaryShortcode::class)->getHideOptions(), |
|
68 | - ], |
|
69 | - 'nameprefix' => Application::ID, |
|
70 | - 'nonce' => [ |
|
71 | - 'change-status' => wp_create_nonce('change-status'), |
|
72 | - 'clear-console' => wp_create_nonce('clear-console'), |
|
73 | - 'count-reviews' => wp_create_nonce('count-reviews'), |
|
74 | - 'fetch-console' => wp_create_nonce('fetch-console'), |
|
75 | - 'mce-shortcode' => wp_create_nonce('mce-shortcode'), |
|
76 | - 'sync-reviews' => wp_create_nonce('sync-reviews'), |
|
77 | - 'toggle-pinned' => wp_create_nonce('toggle-pinned'), |
|
78 | - ], |
|
79 | - 'pointers' => $this->pointers, |
|
80 | - 'shortcodes' => [], |
|
81 | - 'tinymce' => [ |
|
82 | - 'glsr_shortcode' => glsr()->url('assets/scripts/mce-plugin.js'), |
|
83 | - ], |
|
84 | - ]; |
|
85 | - if (user_can_richedit()) { |
|
86 | - $variables['shortcodes'] = $this->localizeShortcodes(); |
|
87 | - } |
|
88 | - $variables = apply_filters('site-reviews/enqueue/admin/localize', $variables); |
|
89 | - wp_localize_script(Application::ID, 'GLSR', $variables); |
|
90 | - } |
|
55 | + /** |
|
56 | + * @return void |
|
57 | + */ |
|
58 | + public function localizeAssets() |
|
59 | + { |
|
60 | + $variables = [ |
|
61 | + 'action' => Application::PREFIX.'action', |
|
62 | + 'addons' => [], |
|
63 | + 'ajaxurl' => admin_url('admin-ajax.php'), |
|
64 | + 'hideoptions' => [ |
|
65 | + 'site_reviews' => glsr(SiteReviewsShortcode::class)->getHideOptions(), |
|
66 | + 'site_reviews_form' => glsr(SiteReviewsFormShortcode::class)->getHideOptions(), |
|
67 | + 'site_reviews_summary' => glsr(SiteReviewsSummaryShortcode::class)->getHideOptions(), |
|
68 | + ], |
|
69 | + 'nameprefix' => Application::ID, |
|
70 | + 'nonce' => [ |
|
71 | + 'change-status' => wp_create_nonce('change-status'), |
|
72 | + 'clear-console' => wp_create_nonce('clear-console'), |
|
73 | + 'count-reviews' => wp_create_nonce('count-reviews'), |
|
74 | + 'fetch-console' => wp_create_nonce('fetch-console'), |
|
75 | + 'mce-shortcode' => wp_create_nonce('mce-shortcode'), |
|
76 | + 'sync-reviews' => wp_create_nonce('sync-reviews'), |
|
77 | + 'toggle-pinned' => wp_create_nonce('toggle-pinned'), |
|
78 | + ], |
|
79 | + 'pointers' => $this->pointers, |
|
80 | + 'shortcodes' => [], |
|
81 | + 'tinymce' => [ |
|
82 | + 'glsr_shortcode' => glsr()->url('assets/scripts/mce-plugin.js'), |
|
83 | + ], |
|
84 | + ]; |
|
85 | + if (user_can_richedit()) { |
|
86 | + $variables['shortcodes'] = $this->localizeShortcodes(); |
|
87 | + } |
|
88 | + $variables = apply_filters('site-reviews/enqueue/admin/localize', $variables); |
|
89 | + wp_localize_script(Application::ID, 'GLSR', $variables); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * @return array |
|
94 | - */ |
|
95 | - protected function getDependencies() |
|
96 | - { |
|
97 | - $dependencies = apply_filters('site-reviews/enqueue/admin/dependencies', []); |
|
98 | - $dependencies = array_merge($dependencies, [ |
|
99 | - 'jquery', 'jquery-ui-sortable', 'underscore', 'wp-util', |
|
100 | - ]); |
|
101 | - return $dependencies; |
|
102 | - } |
|
92 | + /** |
|
93 | + * @return array |
|
94 | + */ |
|
95 | + protected function getDependencies() |
|
96 | + { |
|
97 | + $dependencies = apply_filters('site-reviews/enqueue/admin/dependencies', []); |
|
98 | + $dependencies = array_merge($dependencies, [ |
|
99 | + 'jquery', 'jquery-ui-sortable', 'underscore', 'wp-util', |
|
100 | + ]); |
|
101 | + return $dependencies; |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * @return array |
|
106 | - */ |
|
107 | - protected function generatePointer(array $pointer) |
|
108 | - { |
|
109 | - return [ |
|
110 | - 'id' => $pointer['id'], |
|
111 | - 'options' => [ |
|
112 | - 'content' => '<h3>'.$pointer['title'].'</h3><p>'.$pointer['content'].'</p>', |
|
113 | - 'position' => $pointer['position'], |
|
114 | - ], |
|
115 | - 'screen' => $pointer['screen'], |
|
116 | - 'target' => $pointer['target'], |
|
117 | - ]; |
|
118 | - } |
|
104 | + /** |
|
105 | + * @return array |
|
106 | + */ |
|
107 | + protected function generatePointer(array $pointer) |
|
108 | + { |
|
109 | + return [ |
|
110 | + 'id' => $pointer['id'], |
|
111 | + 'options' => [ |
|
112 | + 'content' => '<h3>'.$pointer['title'].'</h3><p>'.$pointer['content'].'</p>', |
|
113 | + 'position' => $pointer['position'], |
|
114 | + ], |
|
115 | + 'screen' => $pointer['screen'], |
|
116 | + 'target' => $pointer['target'], |
|
117 | + ]; |
|
118 | + } |
|
119 | 119 | |
120 | - /** |
|
121 | - * @return void |
|
122 | - */ |
|
123 | - protected function generatePointers(array $pointers) |
|
124 | - { |
|
125 | - $dismissedPointers = get_user_meta(get_current_user_id(), 'dismissed_wp_pointers', true); |
|
126 | - $dismissedPointers = explode(',', (string) $dismissedPointers); |
|
127 | - $generatedPointers = []; |
|
128 | - foreach ($pointers as $pointer) { |
|
129 | - if ($pointer['screen'] != glsr_current_screen()->id) { |
|
130 | - continue; |
|
131 | - } |
|
132 | - if (in_array($pointer['id'], $dismissedPointers)) { |
|
133 | - continue; |
|
134 | - } |
|
135 | - $generatedPointers[] = $this->generatePointer($pointer); |
|
136 | - } |
|
137 | - $this->pointers = $generatedPointers; |
|
138 | - } |
|
120 | + /** |
|
121 | + * @return void |
|
122 | + */ |
|
123 | + protected function generatePointers(array $pointers) |
|
124 | + { |
|
125 | + $dismissedPointers = get_user_meta(get_current_user_id(), 'dismissed_wp_pointers', true); |
|
126 | + $dismissedPointers = explode(',', (string) $dismissedPointers); |
|
127 | + $generatedPointers = []; |
|
128 | + foreach ($pointers as $pointer) { |
|
129 | + if ($pointer['screen'] != glsr_current_screen()->id) { |
|
130 | + continue; |
|
131 | + } |
|
132 | + if (in_array($pointer['id'], $dismissedPointers)) { |
|
133 | + continue; |
|
134 | + } |
|
135 | + $generatedPointers[] = $this->generatePointer($pointer); |
|
136 | + } |
|
137 | + $this->pointers = $generatedPointers; |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * @return bool |
|
142 | - */ |
|
143 | - protected function isCurrentScreen() |
|
144 | - { |
|
145 | - $screen = glsr_current_screen(); |
|
146 | - return Application::POST_TYPE == $screen->post_type |
|
147 | - || 'dashboard' == $screen->id |
|
148 | - || 'plugins_page_'.Application::ID == $screen->id |
|
149 | - || 'post' == $screen->base |
|
150 | - || 'widgets' == $screen->id; |
|
151 | - } |
|
140 | + /** |
|
141 | + * @return bool |
|
142 | + */ |
|
143 | + protected function isCurrentScreen() |
|
144 | + { |
|
145 | + $screen = glsr_current_screen(); |
|
146 | + return Application::POST_TYPE == $screen->post_type |
|
147 | + || 'dashboard' == $screen->id |
|
148 | + || 'plugins_page_'.Application::ID == $screen->id |
|
149 | + || 'post' == $screen->base |
|
150 | + || 'widgets' == $screen->id; |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * @return array |
|
155 | - */ |
|
156 | - protected function localizeShortcodes() |
|
157 | - { |
|
158 | - $variables = []; |
|
159 | - foreach (glsr()->mceShortcodes as $tag => $args) { |
|
160 | - if (empty($args['required'])) { |
|
161 | - continue; |
|
162 | - } |
|
163 | - $variables[$tag] = $args['required']; |
|
164 | - } |
|
165 | - return $variables; |
|
166 | - } |
|
153 | + /** |
|
154 | + * @return array |
|
155 | + */ |
|
156 | + protected function localizeShortcodes() |
|
157 | + { |
|
158 | + $variables = []; |
|
159 | + foreach (glsr()->mceShortcodes as $tag => $args) { |
|
160 | + if (empty($args['required'])) { |
|
161 | + continue; |
|
162 | + } |
|
163 | + $variables[$tag] = $args['required']; |
|
164 | + } |
|
165 | + return $variables; |
|
166 | + } |
|
167 | 167 | } |
@@ -15,222 +15,222 @@ |
||
15 | 15 | |
16 | 16 | class AdminController extends Controller |
17 | 17 | { |
18 | - /** |
|
19 | - * @return void |
|
20 | - * @action admin_enqueue_scripts |
|
21 | - */ |
|
22 | - public function enqueueAssets() |
|
23 | - { |
|
24 | - $command = new EnqueueAdminAssets([ |
|
25 | - 'pointers' => [[ |
|
26 | - 'content' => __('You can pin exceptional reviews so that they are always shown first.', 'site-reviews'), |
|
27 | - 'id' => 'glsr-pointer-pinned', |
|
28 | - 'position' => [ |
|
29 | - 'edge' => 'right', |
|
30 | - 'align' => 'middle', |
|
31 | - ], |
|
32 | - 'screen' => Application::POST_TYPE, |
|
33 | - 'target' => '#misc-pub-pinned', |
|
34 | - 'title' => __('Pin Your Reviews', 'site-reviews'), |
|
35 | - ]], |
|
36 | - ]); |
|
37 | - $this->execute($command); |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * @return array |
|
42 | - * @filter plugin_action_links_site-reviews/site-reviews.php |
|
43 | - */ |
|
44 | - public function filterActionLinks(array $links) |
|
45 | - { |
|
46 | - $links['documentation'] = glsr(Builder::class)->a(__('Help', 'site-reviews'), [ |
|
47 | - 'href' => admin_url('edit.php?post_type='.Application::POST_TYPE.'&page=documentation'), |
|
48 | - ]); |
|
49 | - $links['settings'] = glsr(Builder::class)->a(__('Settings', 'site-reviews'), [ |
|
50 | - 'href' => admin_url('edit.php?post_type='.Application::POST_TYPE.'&page=settings'), |
|
51 | - ]); |
|
52 | - return $links; |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @param array $capabilities |
|
57 | - * @param string $capability |
|
58 | - * @return array |
|
59 | - * @filter map_meta_cap |
|
60 | - */ |
|
61 | - public function filterCreateCapability($capabilities, $capability) |
|
62 | - { |
|
63 | - if ($capability == 'create_'.Application::POST_TYPE) { |
|
64 | - $capabilities[] = 'do_not_allow'; |
|
65 | - } |
|
66 | - return $capabilities; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * @param array $items |
|
71 | - * @return array |
|
72 | - * @filter dashboard_glance_items |
|
73 | - */ |
|
74 | - public function filterDashboardGlanceItems($items) |
|
75 | - { |
|
76 | - $postCount = wp_count_posts(Application::POST_TYPE); |
|
77 | - if (empty($postCount->publish)) { |
|
78 | - return $items; |
|
79 | - } |
|
80 | - $text = _n('%s Review', '%s Reviews', $postCount->publish, 'site-reviews'); |
|
81 | - $text = sprintf($text, number_format_i18n($postCount->publish)); |
|
82 | - $items = glsr(Helper::class)->consolidateArray($items); |
|
83 | - $items[] = current_user_can(get_post_type_object(Application::POST_TYPE)->cap->edit_posts) |
|
84 | - ? glsr(Builder::class)->a($text, [ |
|
85 | - 'class' => 'glsr-review-count', |
|
86 | - 'href' => 'edit.php?post_type='.Application::POST_TYPE, |
|
87 | - ]) |
|
88 | - : glsr(Builder::class)->span($text, [ |
|
89 | - 'class' => 'glsr-review-count', |
|
90 | - ]); |
|
91 | - return $items; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @param array $plugins |
|
96 | - * @return array |
|
97 | - * @filter mce_external_plugins |
|
98 | - */ |
|
99 | - public function filterTinymcePlugins($plugins) |
|
100 | - { |
|
101 | - if (current_user_can('edit_posts') || current_user_can('edit_pages')) { |
|
102 | - $plugins = glsr(Helper::class)->consolidateArray($plugins); |
|
103 | - $plugins['glsr_shortcode'] = glsr()->url('assets/scripts/mce-plugin.js'); |
|
104 | - } |
|
105 | - return $plugins; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * @return void |
|
110 | - * @action admin_init |
|
111 | - */ |
|
112 | - public function registerTinymcePopups() |
|
113 | - { |
|
114 | - $command = new RegisterTinymcePopups([ |
|
115 | - 'site_reviews' => esc_html__('Recent Reviews', 'site-reviews'), |
|
116 | - 'site_reviews_form' => esc_html__('Submit a Review', 'site-reviews'), |
|
117 | - 'site_reviews_summary' => esc_html__('Summary of Reviews', 'site-reviews'), |
|
118 | - ]); |
|
119 | - $this->execute($command); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * @param string $editorId |
|
124 | - * @return void|null |
|
125 | - * @action media_buttons |
|
126 | - */ |
|
127 | - public function renderTinymceButton($editorId) |
|
128 | - { |
|
129 | - $allowedEditors = apply_filters('site-reviews/tinymce/editor-ids', ['content'], $editorId); |
|
130 | - if ('post' != glsr_current_screen()->base || !in_array($editorId, $allowedEditors)) { |
|
131 | - return; |
|
132 | - } |
|
133 | - $shortcodes = []; |
|
134 | - foreach (glsr()->mceShortcodes as $shortcode => $values) { |
|
135 | - $shortcodes[$shortcode] = $values; |
|
136 | - } |
|
137 | - if (empty($shortcodes)) { |
|
138 | - return; |
|
139 | - } |
|
140 | - glsr()->render('partials/editor/tinymce', [ |
|
141 | - 'shortcodes' => $shortcodes, |
|
142 | - ]); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @return void |
|
147 | - */ |
|
148 | - public function routerClearConsole() |
|
149 | - { |
|
150 | - glsr(Console::class)->clear(); |
|
151 | - glsr(Notice::class)->addSuccess(__('Console cleared.', 'site-reviews')); |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * @return void |
|
156 | - */ |
|
157 | - public function routerFetchConsole() |
|
158 | - { |
|
159 | - glsr(Notice::class)->addSuccess(__('Console reloaded.', 'site-reviews')); |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * @param bool $showNotice |
|
164 | - * @return void |
|
165 | - */ |
|
166 | - public function routerCountReviews($showNotice = true) |
|
167 | - { |
|
168 | - glsr(CountsManager::class)->countAll(); |
|
169 | - glsr(OptionManager::class)->set('last_review_count', current_time('timestamp')); |
|
170 | - if ($showNotice) { |
|
171 | - glsr(Notice::class)->clear()->addSuccess(__('Recalculated rating counts.', 'site-reviews')); |
|
172 | - } |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * @return void |
|
177 | - */ |
|
178 | - public function routerDownloadConsole() |
|
179 | - { |
|
180 | - $this->download(Application::ID.'-console.txt', glsr(Console::class)->get()); |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * @return void |
|
185 | - */ |
|
186 | - public function routerDownloadSystemInfo() |
|
187 | - { |
|
188 | - $this->download(Application::ID.'-system-info.txt', glsr(System::class)->get()); |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * @return void |
|
193 | - */ |
|
194 | - public function routerExportSettings() |
|
195 | - { |
|
196 | - $this->download(Application::ID.'-settings.json', glsr(OptionManager::class)->json()); |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * @return void |
|
201 | - */ |
|
202 | - public function routerImportSettings() |
|
203 | - { |
|
204 | - $file = $_FILES['import-file']; |
|
205 | - if (UPLOAD_ERR_OK !== $file['error']) { |
|
206 | - return glsr(Notice::class)->addError($this->getUploadError($file['error'])); |
|
207 | - } |
|
208 | - if ('application/json' !== $file['type'] || !glsr(Helper::class)->endsWith('.json', $file['name'])) { |
|
209 | - return glsr(Notice::class)->addError(__('Please use a valid Site Reviews settings file.', 'site-reviews')); |
|
210 | - } |
|
211 | - $settings = json_decode(file_get_contents($file['tmp_name']), true); |
|
212 | - if (empty($settings)) { |
|
213 | - return glsr(Notice::class)->addWarning(__('There were no settings found to import.', 'site-reviews')); |
|
214 | - } |
|
215 | - glsr(OptionManager::class)->set(glsr(OptionManager::class)->normalize($settings)); |
|
216 | - glsr(Notice::class)->addSuccess(__('Settings imported.', 'site-reviews')); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * @param int $errorCode |
|
221 | - * @return string |
|
222 | - */ |
|
223 | - protected function getUploadError($errorCode) |
|
224 | - { |
|
225 | - $errors = [ |
|
226 | - UPLOAD_ERR_INI_SIZE => __('The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'site-reviews'), |
|
227 | - UPLOAD_ERR_FORM_SIZE => __('The uploaded file is too big.', 'site-reviews'), |
|
228 | - UPLOAD_ERR_PARTIAL => __('The uploaded file was only partially uploaded.', 'site-reviews'), |
|
229 | - UPLOAD_ERR_NO_FILE => __('No file was uploaded.', 'site-reviews'), |
|
230 | - UPLOAD_ERR_NO_TMP_DIR => __('Missing a temporary folder.', 'site-reviews'), |
|
231 | - UPLOAD_ERR_CANT_WRITE => __('Failed to write file to disk.', 'site-reviews'), |
|
232 | - UPLOAD_ERR_EXTENSION => __('A PHP extension stopped the file upload.', 'site-reviews'), |
|
233 | - ]; |
|
234 | - return glsr_get($errors, $errorCode, __('Unknown upload error.', 'site-reviews')); |
|
235 | - } |
|
18 | + /** |
|
19 | + * @return void |
|
20 | + * @action admin_enqueue_scripts |
|
21 | + */ |
|
22 | + public function enqueueAssets() |
|
23 | + { |
|
24 | + $command = new EnqueueAdminAssets([ |
|
25 | + 'pointers' => [[ |
|
26 | + 'content' => __('You can pin exceptional reviews so that they are always shown first.', 'site-reviews'), |
|
27 | + 'id' => 'glsr-pointer-pinned', |
|
28 | + 'position' => [ |
|
29 | + 'edge' => 'right', |
|
30 | + 'align' => 'middle', |
|
31 | + ], |
|
32 | + 'screen' => Application::POST_TYPE, |
|
33 | + 'target' => '#misc-pub-pinned', |
|
34 | + 'title' => __('Pin Your Reviews', 'site-reviews'), |
|
35 | + ]], |
|
36 | + ]); |
|
37 | + $this->execute($command); |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * @return array |
|
42 | + * @filter plugin_action_links_site-reviews/site-reviews.php |
|
43 | + */ |
|
44 | + public function filterActionLinks(array $links) |
|
45 | + { |
|
46 | + $links['documentation'] = glsr(Builder::class)->a(__('Help', 'site-reviews'), [ |
|
47 | + 'href' => admin_url('edit.php?post_type='.Application::POST_TYPE.'&page=documentation'), |
|
48 | + ]); |
|
49 | + $links['settings'] = glsr(Builder::class)->a(__('Settings', 'site-reviews'), [ |
|
50 | + 'href' => admin_url('edit.php?post_type='.Application::POST_TYPE.'&page=settings'), |
|
51 | + ]); |
|
52 | + return $links; |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @param array $capabilities |
|
57 | + * @param string $capability |
|
58 | + * @return array |
|
59 | + * @filter map_meta_cap |
|
60 | + */ |
|
61 | + public function filterCreateCapability($capabilities, $capability) |
|
62 | + { |
|
63 | + if ($capability == 'create_'.Application::POST_TYPE) { |
|
64 | + $capabilities[] = 'do_not_allow'; |
|
65 | + } |
|
66 | + return $capabilities; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * @param array $items |
|
71 | + * @return array |
|
72 | + * @filter dashboard_glance_items |
|
73 | + */ |
|
74 | + public function filterDashboardGlanceItems($items) |
|
75 | + { |
|
76 | + $postCount = wp_count_posts(Application::POST_TYPE); |
|
77 | + if (empty($postCount->publish)) { |
|
78 | + return $items; |
|
79 | + } |
|
80 | + $text = _n('%s Review', '%s Reviews', $postCount->publish, 'site-reviews'); |
|
81 | + $text = sprintf($text, number_format_i18n($postCount->publish)); |
|
82 | + $items = glsr(Helper::class)->consolidateArray($items); |
|
83 | + $items[] = current_user_can(get_post_type_object(Application::POST_TYPE)->cap->edit_posts) |
|
84 | + ? glsr(Builder::class)->a($text, [ |
|
85 | + 'class' => 'glsr-review-count', |
|
86 | + 'href' => 'edit.php?post_type='.Application::POST_TYPE, |
|
87 | + ]) |
|
88 | + : glsr(Builder::class)->span($text, [ |
|
89 | + 'class' => 'glsr-review-count', |
|
90 | + ]); |
|
91 | + return $items; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @param array $plugins |
|
96 | + * @return array |
|
97 | + * @filter mce_external_plugins |
|
98 | + */ |
|
99 | + public function filterTinymcePlugins($plugins) |
|
100 | + { |
|
101 | + if (current_user_can('edit_posts') || current_user_can('edit_pages')) { |
|
102 | + $plugins = glsr(Helper::class)->consolidateArray($plugins); |
|
103 | + $plugins['glsr_shortcode'] = glsr()->url('assets/scripts/mce-plugin.js'); |
|
104 | + } |
|
105 | + return $plugins; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * @return void |
|
110 | + * @action admin_init |
|
111 | + */ |
|
112 | + public function registerTinymcePopups() |
|
113 | + { |
|
114 | + $command = new RegisterTinymcePopups([ |
|
115 | + 'site_reviews' => esc_html__('Recent Reviews', 'site-reviews'), |
|
116 | + 'site_reviews_form' => esc_html__('Submit a Review', 'site-reviews'), |
|
117 | + 'site_reviews_summary' => esc_html__('Summary of Reviews', 'site-reviews'), |
|
118 | + ]); |
|
119 | + $this->execute($command); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * @param string $editorId |
|
124 | + * @return void|null |
|
125 | + * @action media_buttons |
|
126 | + */ |
|
127 | + public function renderTinymceButton($editorId) |
|
128 | + { |
|
129 | + $allowedEditors = apply_filters('site-reviews/tinymce/editor-ids', ['content'], $editorId); |
|
130 | + if ('post' != glsr_current_screen()->base || !in_array($editorId, $allowedEditors)) { |
|
131 | + return; |
|
132 | + } |
|
133 | + $shortcodes = []; |
|
134 | + foreach (glsr()->mceShortcodes as $shortcode => $values) { |
|
135 | + $shortcodes[$shortcode] = $values; |
|
136 | + } |
|
137 | + if (empty($shortcodes)) { |
|
138 | + return; |
|
139 | + } |
|
140 | + glsr()->render('partials/editor/tinymce', [ |
|
141 | + 'shortcodes' => $shortcodes, |
|
142 | + ]); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @return void |
|
147 | + */ |
|
148 | + public function routerClearConsole() |
|
149 | + { |
|
150 | + glsr(Console::class)->clear(); |
|
151 | + glsr(Notice::class)->addSuccess(__('Console cleared.', 'site-reviews')); |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * @return void |
|
156 | + */ |
|
157 | + public function routerFetchConsole() |
|
158 | + { |
|
159 | + glsr(Notice::class)->addSuccess(__('Console reloaded.', 'site-reviews')); |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * @param bool $showNotice |
|
164 | + * @return void |
|
165 | + */ |
|
166 | + public function routerCountReviews($showNotice = true) |
|
167 | + { |
|
168 | + glsr(CountsManager::class)->countAll(); |
|
169 | + glsr(OptionManager::class)->set('last_review_count', current_time('timestamp')); |
|
170 | + if ($showNotice) { |
|
171 | + glsr(Notice::class)->clear()->addSuccess(__('Recalculated rating counts.', 'site-reviews')); |
|
172 | + } |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * @return void |
|
177 | + */ |
|
178 | + public function routerDownloadConsole() |
|
179 | + { |
|
180 | + $this->download(Application::ID.'-console.txt', glsr(Console::class)->get()); |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * @return void |
|
185 | + */ |
|
186 | + public function routerDownloadSystemInfo() |
|
187 | + { |
|
188 | + $this->download(Application::ID.'-system-info.txt', glsr(System::class)->get()); |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * @return void |
|
193 | + */ |
|
194 | + public function routerExportSettings() |
|
195 | + { |
|
196 | + $this->download(Application::ID.'-settings.json', glsr(OptionManager::class)->json()); |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * @return void |
|
201 | + */ |
|
202 | + public function routerImportSettings() |
|
203 | + { |
|
204 | + $file = $_FILES['import-file']; |
|
205 | + if (UPLOAD_ERR_OK !== $file['error']) { |
|
206 | + return glsr(Notice::class)->addError($this->getUploadError($file['error'])); |
|
207 | + } |
|
208 | + if ('application/json' !== $file['type'] || !glsr(Helper::class)->endsWith('.json', $file['name'])) { |
|
209 | + return glsr(Notice::class)->addError(__('Please use a valid Site Reviews settings file.', 'site-reviews')); |
|
210 | + } |
|
211 | + $settings = json_decode(file_get_contents($file['tmp_name']), true); |
|
212 | + if (empty($settings)) { |
|
213 | + return glsr(Notice::class)->addWarning(__('There were no settings found to import.', 'site-reviews')); |
|
214 | + } |
|
215 | + glsr(OptionManager::class)->set(glsr(OptionManager::class)->normalize($settings)); |
|
216 | + glsr(Notice::class)->addSuccess(__('Settings imported.', 'site-reviews')); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * @param int $errorCode |
|
221 | + * @return string |
|
222 | + */ |
|
223 | + protected function getUploadError($errorCode) |
|
224 | + { |
|
225 | + $errors = [ |
|
226 | + UPLOAD_ERR_INI_SIZE => __('The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'site-reviews'), |
|
227 | + UPLOAD_ERR_FORM_SIZE => __('The uploaded file is too big.', 'site-reviews'), |
|
228 | + UPLOAD_ERR_PARTIAL => __('The uploaded file was only partially uploaded.', 'site-reviews'), |
|
229 | + UPLOAD_ERR_NO_FILE => __('No file was uploaded.', 'site-reviews'), |
|
230 | + UPLOAD_ERR_NO_TMP_DIR => __('Missing a temporary folder.', 'site-reviews'), |
|
231 | + UPLOAD_ERR_CANT_WRITE => __('Failed to write file to disk.', 'site-reviews'), |
|
232 | + UPLOAD_ERR_EXTENSION => __('A PHP extension stopped the file upload.', 'site-reviews'), |
|
233 | + ]; |
|
234 | + return glsr_get($errors, $errorCode, __('Unknown upload error.', 'site-reviews')); |
|
235 | + } |
|
236 | 236 | } |