@@ -7,87 +7,87 @@ |
||
7 | 7 | |
8 | 8 | class Date |
9 | 9 | { |
10 | - /** |
|
11 | - * [60, 1], |
|
12 | - * [60 * 100, 60], |
|
13 | - * [3600 * 70, 3600], |
|
14 | - * [3600 * 24 * 10, 3600 * 24], |
|
15 | - * [3600 * 24 * 30, 3600 * 24 * 7], |
|
16 | - * [3600 * 24 * 30 * 30, 3600 * 24 * 30], |
|
17 | - * [INF, 3600 * 24 * 265],. |
|
18 | - */ |
|
19 | - protected static $TIME_PERIODS = [ |
|
20 | - [60, 1], |
|
21 | - [6000, 60], |
|
22 | - [252000, 3600], |
|
23 | - [864000, 86400], |
|
24 | - [2592000, 604800], |
|
25 | - [77760000, 2592000], |
|
26 | - [INF, 22896000], |
|
27 | - ]; |
|
10 | + /** |
|
11 | + * [60, 1], |
|
12 | + * [60 * 100, 60], |
|
13 | + * [3600 * 70, 3600], |
|
14 | + * [3600 * 24 * 10, 3600 * 24], |
|
15 | + * [3600 * 24 * 30, 3600 * 24 * 7], |
|
16 | + * [3600 * 24 * 30 * 30, 3600 * 24 * 30], |
|
17 | + * [INF, 3600 * 24 * 265],. |
|
18 | + */ |
|
19 | + protected static $TIME_PERIODS = [ |
|
20 | + [60, 1], |
|
21 | + [6000, 60], |
|
22 | + [252000, 3600], |
|
23 | + [864000, 86400], |
|
24 | + [2592000, 604800], |
|
25 | + [77760000, 2592000], |
|
26 | + [INF, 22896000], |
|
27 | + ]; |
|
28 | 28 | |
29 | - /** |
|
30 | - * @param mixed $date |
|
31 | - * @param string $format |
|
32 | - * @return bool |
|
33 | - */ |
|
34 | - public function isDate($date, $format = 'Y-m-d H:i:s') |
|
35 | - { |
|
36 | - $datetime = DateTime::createFromFormat($format, $date); |
|
37 | - return $datetime && $date == $datetime->format($format); |
|
38 | - } |
|
29 | + /** |
|
30 | + * @param mixed $date |
|
31 | + * @param string $format |
|
32 | + * @return bool |
|
33 | + */ |
|
34 | + public function isDate($date, $format = 'Y-m-d H:i:s') |
|
35 | + { |
|
36 | + $datetime = DateTime::createFromFormat($format, $date); |
|
37 | + return $datetime && $date == $datetime->format($format); |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @param mixed $date |
|
42 | - * @return bool |
|
43 | - */ |
|
44 | - public function isTimestamp($date) |
|
45 | - { |
|
46 | - return ctype_digit($date) |
|
47 | - ? true |
|
48 | - : false; |
|
49 | - } |
|
40 | + /** |
|
41 | + * @param mixed $date |
|
42 | + * @return bool |
|
43 | + */ |
|
44 | + public function isTimestamp($date) |
|
45 | + { |
|
46 | + return ctype_digit($date) |
|
47 | + ? true |
|
48 | + : false; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @param mixed $date |
|
53 | - * @param string $fallback |
|
54 | - * @return string |
|
55 | - */ |
|
56 | - public function localized($date, $fallback = '') |
|
57 | - { |
|
58 | - return $this->isDate($date) || $this->isTimestamp($date) |
|
59 | - ? date_i18n('Y-m-d H:i', $date) |
|
60 | - : $fallback; |
|
61 | - } |
|
51 | + /** |
|
52 | + * @param mixed $date |
|
53 | + * @param string $fallback |
|
54 | + * @return string |
|
55 | + */ |
|
56 | + public function localized($date, $fallback = '') |
|
57 | + { |
|
58 | + return $this->isDate($date) || $this->isTimestamp($date) |
|
59 | + ? date_i18n('Y-m-d H:i', $date) |
|
60 | + : $fallback; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * @param mixed $date |
|
65 | - * @return string |
|
66 | - */ |
|
67 | - public function relative($date) |
|
68 | - { |
|
69 | - if (!$this->isDate($date)) { |
|
70 | - return ''; |
|
71 | - } |
|
72 | - $diff = time() - strtotime($date); |
|
73 | - foreach (static::$TIME_PERIODS as $i => $timePeriod) { |
|
74 | - if ($diff > $timePeriod[0]) { |
|
75 | - continue; |
|
76 | - } |
|
77 | - $unit = intval(floor($diff / $timePeriod[1])); |
|
78 | - $relativeDates = [ |
|
79 | - _n('%s second ago', '%s seconds ago', $unit, 'site-reviews'), |
|
80 | - _n('%s minute ago', '%s minutes ago', $unit, 'site-reviews'), |
|
81 | - _n('an hour ago', '%s hours ago', $unit, 'site-reviews'), |
|
82 | - _n('yesterday', '%s days ago', $unit, 'site-reviews'), |
|
83 | - _n('a week ago', '%s weeks ago', $unit, 'site-reviews'), |
|
84 | - _n('%s month ago', '%s months ago', $unit, 'site-reviews'), |
|
85 | - _n('%s year ago', '%s years ago', $unit, 'site-reviews'), |
|
86 | - ]; |
|
87 | - $relativeDate = $relativeDates[$i]; |
|
88 | - return Str::contains($relativeDate, '%s') |
|
89 | - ? sprintf($relativeDate, $unit) |
|
90 | - : $relativeDate; |
|
91 | - } |
|
92 | - } |
|
63 | + /** |
|
64 | + * @param mixed $date |
|
65 | + * @return string |
|
66 | + */ |
|
67 | + public function relative($date) |
|
68 | + { |
|
69 | + if (!$this->isDate($date)) { |
|
70 | + return ''; |
|
71 | + } |
|
72 | + $diff = time() - strtotime($date); |
|
73 | + foreach (static::$TIME_PERIODS as $i => $timePeriod) { |
|
74 | + if ($diff > $timePeriod[0]) { |
|
75 | + continue; |
|
76 | + } |
|
77 | + $unit = intval(floor($diff / $timePeriod[1])); |
|
78 | + $relativeDates = [ |
|
79 | + _n('%s second ago', '%s seconds ago', $unit, 'site-reviews'), |
|
80 | + _n('%s minute ago', '%s minutes ago', $unit, 'site-reviews'), |
|
81 | + _n('an hour ago', '%s hours ago', $unit, 'site-reviews'), |
|
82 | + _n('yesterday', '%s days ago', $unit, 'site-reviews'), |
|
83 | + _n('a week ago', '%s weeks ago', $unit, 'site-reviews'), |
|
84 | + _n('%s month ago', '%s months ago', $unit, 'site-reviews'), |
|
85 | + _n('%s year ago', '%s years ago', $unit, 'site-reviews'), |
|
86 | + ]; |
|
87 | + $relativeDate = $relativeDates[$i]; |
|
88 | + return Str::contains($relativeDate, '%s') |
|
89 | + ? sprintf($relativeDate, $unit) |
|
90 | + : $relativeDate; |
|
91 | + } |
|
92 | + } |
|
93 | 93 | } |
@@ -9,70 +9,70 @@ |
||
9 | 9 | |
10 | 10 | class Migrate_4_0_2 |
11 | 11 | { |
12 | - /** |
|
13 | - * @return void |
|
14 | - */ |
|
15 | - public function deleteSessions() |
|
16 | - { |
|
17 | - global $wpdb; |
|
18 | - $wpdb->query(" |
|
12 | + /** |
|
13 | + * @return void |
|
14 | + */ |
|
15 | + public function deleteSessions() |
|
16 | + { |
|
17 | + global $wpdb; |
|
18 | + $wpdb->query(" |
|
19 | 19 | DELETE |
20 | 20 | FROM {$wpdb->options} |
21 | 21 | WHERE option_name LIKE '_glsr_session%' |
22 | 22 | "); |
23 | - } |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * @return void |
|
27 | - */ |
|
28 | - public function migrateSettings() |
|
29 | - { |
|
30 | - if ($settings = get_option(OptionManager::databaseKey(3))) { |
|
31 | - $multilingual = 'yes' == Arr::get($settings, 'settings.general.support.polylang') |
|
32 | - ? 'polylang' |
|
33 | - : ''; |
|
34 | - $settings = Arr::set($settings, 'settings.general.multilingual', $multilingual); |
|
35 | - $settings = Arr::set($settings, 'settings.general.rebusify', 'no'); |
|
36 | - $settings = Arr::set($settings, 'settings.general.rebusify_email', ''); |
|
37 | - $settings = Arr::set($settings, 'settings.general.rebusify_serial', ''); |
|
38 | - $settings = Arr::set($settings, 'settings.reviews.name.format', ''); |
|
39 | - $settings = Arr::set($settings, 'settings.reviews.name.initial', ''); |
|
40 | - $settings = Arr::set($settings, 'settings.submissions.blacklist.integration', ''); |
|
41 | - $settings = Arr::set($settings, 'settings.submissions.limit', ''); |
|
42 | - $settings = Arr::set($settings, 'settings.submissions.limit_whitelist.email', ''); |
|
43 | - $settings = Arr::set($settings, 'settings.submissions.limit_whitelist.ip_address', ''); |
|
44 | - $settings = Arr::set($settings, 'settings.submissions.limit_whitelist.username', ''); |
|
45 | - unset($settings['settings']['general']['support']); |
|
46 | - update_option(OptionManager::databaseKey(4), $settings); |
|
47 | - } |
|
48 | - } |
|
25 | + /** |
|
26 | + * @return void |
|
27 | + */ |
|
28 | + public function migrateSettings() |
|
29 | + { |
|
30 | + if ($settings = get_option(OptionManager::databaseKey(3))) { |
|
31 | + $multilingual = 'yes' == Arr::get($settings, 'settings.general.support.polylang') |
|
32 | + ? 'polylang' |
|
33 | + : ''; |
|
34 | + $settings = Arr::set($settings, 'settings.general.multilingual', $multilingual); |
|
35 | + $settings = Arr::set($settings, 'settings.general.rebusify', 'no'); |
|
36 | + $settings = Arr::set($settings, 'settings.general.rebusify_email', ''); |
|
37 | + $settings = Arr::set($settings, 'settings.general.rebusify_serial', ''); |
|
38 | + $settings = Arr::set($settings, 'settings.reviews.name.format', ''); |
|
39 | + $settings = Arr::set($settings, 'settings.reviews.name.initial', ''); |
|
40 | + $settings = Arr::set($settings, 'settings.submissions.blacklist.integration', ''); |
|
41 | + $settings = Arr::set($settings, 'settings.submissions.limit', ''); |
|
42 | + $settings = Arr::set($settings, 'settings.submissions.limit_whitelist.email', ''); |
|
43 | + $settings = Arr::set($settings, 'settings.submissions.limit_whitelist.ip_address', ''); |
|
44 | + $settings = Arr::set($settings, 'settings.submissions.limit_whitelist.username', ''); |
|
45 | + unset($settings['settings']['general']['support']); |
|
46 | + update_option(OptionManager::databaseKey(4), $settings); |
|
47 | + } |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @return void |
|
52 | - */ |
|
53 | - public function protectMetaKeys() |
|
54 | - { |
|
55 | - global $wpdb; |
|
56 | - $keys = array_keys(glsr(CreateReviewDefaults::class)->defaults()); |
|
57 | - $keys = implode("','", $keys); |
|
58 | - $postType = Application::POST_TYPE; |
|
59 | - $wpdb->query(" |
|
50 | + /** |
|
51 | + * @return void |
|
52 | + */ |
|
53 | + public function protectMetaKeys() |
|
54 | + { |
|
55 | + global $wpdb; |
|
56 | + $keys = array_keys(glsr(CreateReviewDefaults::class)->defaults()); |
|
57 | + $keys = implode("','", $keys); |
|
58 | + $postType = Application::POST_TYPE; |
|
59 | + $wpdb->query(" |
|
60 | 60 | UPDATE {$wpdb->postmeta} pm |
61 | 61 | INNER JOIN {$wpdb->posts} p ON p.id = pm.post_id |
62 | 62 | SET pm.meta_key = CONCAT('_', pm.meta_key) |
63 | 63 | WHERE pm.meta_key IN ('{$keys}') |
64 | 64 | AND p.post_type = '{$postType}' |
65 | 65 | "); |
66 | - } |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @return void |
|
70 | - */ |
|
71 | - public function run() |
|
72 | - { |
|
73 | - $this->migrateSettings(); |
|
74 | - $this->protectMetaKeys(); |
|
75 | - $this->deleteSessions(); |
|
76 | - delete_transient(Application::ID.'_cloudflare_ips'); |
|
77 | - } |
|
68 | + /** |
|
69 | + * @return void |
|
70 | + */ |
|
71 | + public function run() |
|
72 | + { |
|
73 | + $this->migrateSettings(); |
|
74 | + $this->protectMetaKeys(); |
|
75 | + $this->deleteSessions(); |
|
76 | + delete_transient(Application::ID.'_cloudflare_ips'); |
|
77 | + } |
|
78 | 78 | } |
@@ -9,75 +9,75 @@ |
||
9 | 9 | |
10 | 10 | class Wpml implements Contract |
11 | 11 | { |
12 | - public $pluginName = 'WPML'; |
|
13 | - public $supportedVersion = '3.3.5'; |
|
12 | + public $pluginName = 'WPML'; |
|
13 | + public $supportedVersion = '3.3.5'; |
|
14 | 14 | |
15 | - /** |
|
16 | - * {@inheritdoc} |
|
17 | - */ |
|
18 | - public function getPostId($postId) |
|
19 | - { |
|
20 | - $postId = trim($postId); |
|
21 | - if (!is_numeric($postId)) { |
|
22 | - return 0; |
|
23 | - } |
|
24 | - if ($this->isEnabled()) { |
|
25 | - $postId = apply_filters('wpml_object_id', $postId, 'any', true); |
|
26 | - } |
|
27 | - return intval($postId); |
|
28 | - } |
|
15 | + /** |
|
16 | + * {@inheritdoc} |
|
17 | + */ |
|
18 | + public function getPostId($postId) |
|
19 | + { |
|
20 | + $postId = trim($postId); |
|
21 | + if (!is_numeric($postId)) { |
|
22 | + return 0; |
|
23 | + } |
|
24 | + if ($this->isEnabled()) { |
|
25 | + $postId = apply_filters('wpml_object_id', $postId, 'any', true); |
|
26 | + } |
|
27 | + return intval($postId); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * {@inheritdoc} |
|
32 | - */ |
|
33 | - public function getPostIds(array $postIds) |
|
34 | - { |
|
35 | - if (!$this->isEnabled()) { |
|
36 | - return $postIds; |
|
37 | - } |
|
38 | - $newPostIds = []; |
|
39 | - foreach (Arr::unique($postIds) as $postId) { |
|
40 | - $postType = get_post_type($postId); |
|
41 | - if (!$postType) { |
|
42 | - continue; |
|
43 | - } |
|
44 | - $elementType = 'post_'.$postType; |
|
45 | - $trid = apply_filters('wpml_element_trid', null, $postId, $elementType); |
|
46 | - $translations = apply_filters('wpml_get_element_translations', null, $trid, $elementType); |
|
47 | - if (!is_array($translations)) { |
|
48 | - $translations = []; |
|
49 | - } |
|
50 | - $newPostIds = array_merge( |
|
51 | - $newPostIds, |
|
52 | - array_column($translations, 'element_id') |
|
53 | - ); |
|
54 | - } |
|
55 | - return Arr::unique($newPostIds); |
|
56 | - } |
|
30 | + /** |
|
31 | + * {@inheritdoc} |
|
32 | + */ |
|
33 | + public function getPostIds(array $postIds) |
|
34 | + { |
|
35 | + if (!$this->isEnabled()) { |
|
36 | + return $postIds; |
|
37 | + } |
|
38 | + $newPostIds = []; |
|
39 | + foreach (Arr::unique($postIds) as $postId) { |
|
40 | + $postType = get_post_type($postId); |
|
41 | + if (!$postType) { |
|
42 | + continue; |
|
43 | + } |
|
44 | + $elementType = 'post_'.$postType; |
|
45 | + $trid = apply_filters('wpml_element_trid', null, $postId, $elementType); |
|
46 | + $translations = apply_filters('wpml_get_element_translations', null, $trid, $elementType); |
|
47 | + if (!is_array($translations)) { |
|
48 | + $translations = []; |
|
49 | + } |
|
50 | + $newPostIds = array_merge( |
|
51 | + $newPostIds, |
|
52 | + array_column($translations, 'element_id') |
|
53 | + ); |
|
54 | + } |
|
55 | + return Arr::unique($newPostIds); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * {@inheritdoc} |
|
60 | - */ |
|
61 | - public function isActive() |
|
62 | - { |
|
63 | - return defined('ICL_SITEPRESS_VERSION'); |
|
64 | - } |
|
58 | + /** |
|
59 | + * {@inheritdoc} |
|
60 | + */ |
|
61 | + public function isActive() |
|
62 | + { |
|
63 | + return defined('ICL_SITEPRESS_VERSION'); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * {@inheritdoc} |
|
68 | - */ |
|
69 | - public function isEnabled() |
|
70 | - { |
|
71 | - return $this->isActive() |
|
72 | - && 'wpml' == glsr(OptionManager::class)->get('settings.general.multilingual'); |
|
73 | - } |
|
66 | + /** |
|
67 | + * {@inheritdoc} |
|
68 | + */ |
|
69 | + public function isEnabled() |
|
70 | + { |
|
71 | + return $this->isActive() |
|
72 | + && 'wpml' == glsr(OptionManager::class)->get('settings.general.multilingual'); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * {@inheritdoc} |
|
77 | - */ |
|
78 | - public function isSupported() |
|
79 | - { |
|
80 | - return $this->isActive() |
|
81 | - && Helper::isGreaterThanOrEqual(ICL_SITEPRESS_VERSION, $this->supportedVersion); |
|
82 | - } |
|
75 | + /** |
|
76 | + * {@inheritdoc} |
|
77 | + */ |
|
78 | + public function isSupported() |
|
79 | + { |
|
80 | + return $this->isActive() |
|
81 | + && Helper::isGreaterThanOrEqual(ICL_SITEPRESS_VERSION, $this->supportedVersion); |
|
82 | + } |
|
83 | 83 | } |
@@ -9,71 +9,71 @@ |
||
9 | 9 | |
10 | 10 | class Polylang implements Contract |
11 | 11 | { |
12 | - public $pluginName = 'Polylang'; |
|
13 | - public $supportedVersion = '2.3'; |
|
12 | + public $pluginName = 'Polylang'; |
|
13 | + public $supportedVersion = '2.3'; |
|
14 | 14 | |
15 | - /** |
|
16 | - * {@inheritdoc} |
|
17 | - */ |
|
18 | - public function getPostId($postId) |
|
19 | - { |
|
20 | - $postId = trim($postId); |
|
21 | - if (!is_numeric($postId)) { |
|
22 | - return 0; |
|
23 | - } |
|
24 | - if ($this->isEnabled()) { |
|
25 | - $polylangPostId = pll_get_post($postId, pll_get_post_language(get_the_ID())); |
|
26 | - } |
|
27 | - if (!empty($polylangPostId)) { |
|
28 | - $postId = $polylangPostId; |
|
29 | - } |
|
30 | - return intval($postId); |
|
31 | - } |
|
15 | + /** |
|
16 | + * {@inheritdoc} |
|
17 | + */ |
|
18 | + public function getPostId($postId) |
|
19 | + { |
|
20 | + $postId = trim($postId); |
|
21 | + if (!is_numeric($postId)) { |
|
22 | + return 0; |
|
23 | + } |
|
24 | + if ($this->isEnabled()) { |
|
25 | + $polylangPostId = pll_get_post($postId, pll_get_post_language(get_the_ID())); |
|
26 | + } |
|
27 | + if (!empty($polylangPostId)) { |
|
28 | + $postId = $polylangPostId; |
|
29 | + } |
|
30 | + return intval($postId); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * {@inheritdoc} |
|
35 | - */ |
|
36 | - public function getPostIds(array $postIds) |
|
37 | - { |
|
38 | - if (!$this->isEnabled()) { |
|
39 | - return $postIds; |
|
40 | - } |
|
41 | - $newPostIds = []; |
|
42 | - foreach (Arr::unique($postIds) as $postId) { |
|
43 | - $newPostIds = array_merge( |
|
44 | - $newPostIds, |
|
45 | - array_values(pll_get_post_translations($postId)) |
|
46 | - ); |
|
47 | - } |
|
48 | - return Arr::unique($newPostIds); |
|
49 | - } |
|
33 | + /** |
|
34 | + * {@inheritdoc} |
|
35 | + */ |
|
36 | + public function getPostIds(array $postIds) |
|
37 | + { |
|
38 | + if (!$this->isEnabled()) { |
|
39 | + return $postIds; |
|
40 | + } |
|
41 | + $newPostIds = []; |
|
42 | + foreach (Arr::unique($postIds) as $postId) { |
|
43 | + $newPostIds = array_merge( |
|
44 | + $newPostIds, |
|
45 | + array_values(pll_get_post_translations($postId)) |
|
46 | + ); |
|
47 | + } |
|
48 | + return Arr::unique($newPostIds); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * {@inheritdoc} |
|
53 | - */ |
|
54 | - public function isActive() |
|
55 | - { |
|
56 | - return function_exists('PLL') |
|
57 | - && function_exists('pll_get_post') |
|
58 | - && function_exists('pll_get_post_language') |
|
59 | - && function_exists('pll_get_post_translations'); |
|
60 | - } |
|
51 | + /** |
|
52 | + * {@inheritdoc} |
|
53 | + */ |
|
54 | + public function isActive() |
|
55 | + { |
|
56 | + return function_exists('PLL') |
|
57 | + && function_exists('pll_get_post') |
|
58 | + && function_exists('pll_get_post_language') |
|
59 | + && function_exists('pll_get_post_translations'); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * {@inheritdoc} |
|
64 | - */ |
|
65 | - public function isEnabled() |
|
66 | - { |
|
67 | - return $this->isActive() |
|
68 | - && 'polylang' == glsr(OptionManager::class)->get('settings.general.multilingual'); |
|
69 | - } |
|
62 | + /** |
|
63 | + * {@inheritdoc} |
|
64 | + */ |
|
65 | + public function isEnabled() |
|
66 | + { |
|
67 | + return $this->isActive() |
|
68 | + && 'polylang' == glsr(OptionManager::class)->get('settings.general.multilingual'); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * {@inheritdoc} |
|
73 | - */ |
|
74 | - public function isSupported() |
|
75 | - { |
|
76 | - return defined('POLYLANG_VERSION') |
|
77 | - && Helper::isGreaterThanOrEqual(POLYLANG_VERSION, $this->supportedVersion); |
|
78 | - } |
|
71 | + /** |
|
72 | + * {@inheritdoc} |
|
73 | + */ |
|
74 | + public function isSupported() |
|
75 | + { |
|
76 | + return defined('POLYLANG_VERSION') |
|
77 | + && Helper::isGreaterThanOrEqual(POLYLANG_VERSION, $this->supportedVersion); |
|
78 | + } |
|
79 | 79 | } |
@@ -14,248 +14,248 @@ |
||
14 | 14 | |
15 | 15 | class CountsManager |
16 | 16 | { |
17 | - const LIMIT = 500; |
|
18 | - const META_AVERAGE = '_glsr_average'; |
|
19 | - const META_COUNT = '_glsr_count'; |
|
20 | - const META_RANKING = '_glsr_ranking'; |
|
17 | + const LIMIT = 500; |
|
18 | + const META_AVERAGE = '_glsr_average'; |
|
19 | + const META_COUNT = '_glsr_count'; |
|
20 | + const META_RANKING = '_glsr_ranking'; |
|
21 | 21 | |
22 | - /** |
|
23 | - * @return array |
|
24 | - */ |
|
25 | - public function buildCounts(array $args = []) |
|
26 | - { |
|
27 | - $counts = [ |
|
28 | - 'local' => $this->generateEmptyCountsArray(), |
|
29 | - ]; |
|
30 | - $query = $this->queryReviews($args); |
|
31 | - while ($query) { |
|
32 | - $counts = $this->populateCountsFromQuery($query, $counts); |
|
33 | - $query = $query->has_more |
|
34 | - ? $this->queryReviews($args, end($query->reviews)->ID) |
|
35 | - : false; |
|
36 | - } |
|
37 | - return $counts; |
|
38 | - } |
|
22 | + /** |
|
23 | + * @return array |
|
24 | + */ |
|
25 | + public function buildCounts(array $args = []) |
|
26 | + { |
|
27 | + $counts = [ |
|
28 | + 'local' => $this->generateEmptyCountsArray(), |
|
29 | + ]; |
|
30 | + $query = $this->queryReviews($args); |
|
31 | + while ($query) { |
|
32 | + $counts = $this->populateCountsFromQuery($query, $counts); |
|
33 | + $query = $query->has_more |
|
34 | + ? $this->queryReviews($args, end($query->reviews)->ID) |
|
35 | + : false; |
|
36 | + } |
|
37 | + return $counts; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @return void |
|
42 | - */ |
|
43 | - public function decreaseAll(Review $review) |
|
44 | - { |
|
45 | - glsr(GlobalCountsManager::class)->decrease($review); |
|
46 | - glsr(PostCountsManager::class)->decrease($review); |
|
47 | - glsr(TermCountsManager::class)->decrease($review); |
|
48 | - } |
|
40 | + /** |
|
41 | + * @return void |
|
42 | + */ |
|
43 | + public function decreaseAll(Review $review) |
|
44 | + { |
|
45 | + glsr(GlobalCountsManager::class)->decrease($review); |
|
46 | + glsr(PostCountsManager::class)->decrease($review); |
|
47 | + glsr(TermCountsManager::class)->decrease($review); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @param string $type |
|
52 | - * @param int $rating |
|
53 | - * @return array |
|
54 | - */ |
|
55 | - public function decreaseRating(array $reviewCounts, $type, $rating) |
|
56 | - { |
|
57 | - if (isset($reviewCounts[$type][$rating])) { |
|
58 | - $reviewCounts[$type][$rating] = max(0, $reviewCounts[$type][$rating] - 1); |
|
59 | - } |
|
60 | - return $reviewCounts; |
|
61 | - } |
|
50 | + /** |
|
51 | + * @param string $type |
|
52 | + * @param int $rating |
|
53 | + * @return array |
|
54 | + */ |
|
55 | + public function decreaseRating(array $reviewCounts, $type, $rating) |
|
56 | + { |
|
57 | + if (isset($reviewCounts[$type][$rating])) { |
|
58 | + $reviewCounts[$type][$rating] = max(0, $reviewCounts[$type][$rating] - 1); |
|
59 | + } |
|
60 | + return $reviewCounts; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * @return array |
|
65 | - */ |
|
66 | - public function flatten(array $reviewCounts, array $args = []) |
|
67 | - { |
|
68 | - $counts = []; |
|
69 | - array_walk_recursive($reviewCounts, function ($num, $index) use (&$counts) { |
|
70 | - $counts[$index] = $num + intval(Arr::get($counts, $index, 0)); |
|
71 | - }); |
|
72 | - $min = Arr::get($args, 'min', glsr()->constant('MIN_RATING', Rating::class)); |
|
73 | - $max = Arr::get($args, 'max', glsr()->constant('MAX_RATING', Rating::class)); |
|
74 | - foreach ($counts as $index => &$num) { |
|
75 | - if (!Helper::inRange($index, $min, $max)) { |
|
76 | - $num = 0; |
|
77 | - } |
|
78 | - } |
|
79 | - return $counts; |
|
80 | - } |
|
63 | + /** |
|
64 | + * @return array |
|
65 | + */ |
|
66 | + public function flatten(array $reviewCounts, array $args = []) |
|
67 | + { |
|
68 | + $counts = []; |
|
69 | + array_walk_recursive($reviewCounts, function ($num, $index) use (&$counts) { |
|
70 | + $counts[$index] = $num + intval(Arr::get($counts, $index, 0)); |
|
71 | + }); |
|
72 | + $min = Arr::get($args, 'min', glsr()->constant('MIN_RATING', Rating::class)); |
|
73 | + $max = Arr::get($args, 'max', glsr()->constant('MAX_RATING', Rating::class)); |
|
74 | + foreach ($counts as $index => &$num) { |
|
75 | + if (!Helper::inRange($index, $min, $max)) { |
|
76 | + $num = 0; |
|
77 | + } |
|
78 | + } |
|
79 | + return $counts; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @return array |
|
84 | - */ |
|
85 | - public function getCounts(array $args = []) |
|
86 | - { |
|
87 | - $args = $this->normalizeArgs($args); |
|
88 | - $counts = $this->hasMixedAssignment($args) |
|
89 | - ? $this->buildCounts($args) // force query the database |
|
90 | - : $this->get($args); |
|
91 | - return $this->normalize($counts); |
|
92 | - } |
|
82 | + /** |
|
83 | + * @return array |
|
84 | + */ |
|
85 | + public function getCounts(array $args = []) |
|
86 | + { |
|
87 | + $args = $this->normalizeArgs($args); |
|
88 | + $counts = $this->hasMixedAssignment($args) |
|
89 | + ? $this->buildCounts($args) // force query the database |
|
90 | + : $this->get($args); |
|
91 | + return $this->normalize($counts); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * @return void |
|
96 | - */ |
|
97 | - public function increaseAll(Review $review) |
|
98 | - { |
|
99 | - glsr(GlobalCountsManager::class)->increase($review); |
|
100 | - glsr(PostCountsManager::class)->increase($review); |
|
101 | - glsr(TermCountsManager::class)->increase($review); |
|
102 | - } |
|
94 | + /** |
|
95 | + * @return void |
|
96 | + */ |
|
97 | + public function increaseAll(Review $review) |
|
98 | + { |
|
99 | + glsr(GlobalCountsManager::class)->increase($review); |
|
100 | + glsr(PostCountsManager::class)->increase($review); |
|
101 | + glsr(TermCountsManager::class)->increase($review); |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * @param string $type |
|
106 | - * @param int $rating |
|
107 | - * @return array |
|
108 | - */ |
|
109 | - public function increaseRating(array $reviewCounts, $type, $rating) |
|
110 | - { |
|
111 | - if (!array_key_exists($type, glsr()->reviewTypes)) { |
|
112 | - return $reviewCounts; |
|
113 | - } |
|
114 | - if (!array_key_exists($type, $reviewCounts)) { |
|
115 | - $reviewCounts[$type] = []; |
|
116 | - } |
|
117 | - $reviewCounts = $this->normalize($reviewCounts); |
|
118 | - $reviewCounts[$type][$rating] = intval($reviewCounts[$type][$rating]) + 1; |
|
119 | - return $reviewCounts; |
|
120 | - } |
|
104 | + /** |
|
105 | + * @param string $type |
|
106 | + * @param int $rating |
|
107 | + * @return array |
|
108 | + */ |
|
109 | + public function increaseRating(array $reviewCounts, $type, $rating) |
|
110 | + { |
|
111 | + if (!array_key_exists($type, glsr()->reviewTypes)) { |
|
112 | + return $reviewCounts; |
|
113 | + } |
|
114 | + if (!array_key_exists($type, $reviewCounts)) { |
|
115 | + $reviewCounts[$type] = []; |
|
116 | + } |
|
117 | + $reviewCounts = $this->normalize($reviewCounts); |
|
118 | + $reviewCounts[$type][$rating] = intval($reviewCounts[$type][$rating]) + 1; |
|
119 | + return $reviewCounts; |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * @return void |
|
124 | - */ |
|
125 | - public function updateAll() |
|
126 | - { |
|
127 | - glsr(GlobalCountsManager::class)->updateAll(); |
|
128 | - glsr(PostCountsManager::class)->updateAll(); |
|
129 | - glsr(TermCountsManager::class)->updateAll(); |
|
130 | - glsr(OptionManager::class)->set('last_review_count', current_time('timestamp')); |
|
131 | - } |
|
122 | + /** |
|
123 | + * @return void |
|
124 | + */ |
|
125 | + public function updateAll() |
|
126 | + { |
|
127 | + glsr(GlobalCountsManager::class)->updateAll(); |
|
128 | + glsr(PostCountsManager::class)->updateAll(); |
|
129 | + glsr(TermCountsManager::class)->updateAll(); |
|
130 | + glsr(OptionManager::class)->set('last_review_count', current_time('timestamp')); |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * @return array |
|
135 | - */ |
|
136 | - protected function combine(array $results) |
|
137 | - { |
|
138 | - if (!wp_is_numeric_array($results)) { |
|
139 | - return $results; |
|
140 | - } |
|
141 | - $mergedKeys = array_keys(array_merge(...$results)); |
|
142 | - $counts = array_fill_keys($mergedKeys, $this->generateEmptyCountsArray()); |
|
143 | - foreach ($results as $typeRatings) { |
|
144 | - foreach ($typeRatings as $type => $ratings) { |
|
145 | - foreach ($ratings as $index => $rating) { |
|
146 | - $counts[$type][$index] = intval($rating) + $counts[$type][$index]; |
|
147 | - } |
|
148 | - } |
|
149 | - } |
|
150 | - return $counts; |
|
151 | - } |
|
133 | + /** |
|
134 | + * @return array |
|
135 | + */ |
|
136 | + protected function combine(array $results) |
|
137 | + { |
|
138 | + if (!wp_is_numeric_array($results)) { |
|
139 | + return $results; |
|
140 | + } |
|
141 | + $mergedKeys = array_keys(array_merge(...$results)); |
|
142 | + $counts = array_fill_keys($mergedKeys, $this->generateEmptyCountsArray()); |
|
143 | + foreach ($results as $typeRatings) { |
|
144 | + foreach ($typeRatings as $type => $ratings) { |
|
145 | + foreach ($ratings as $index => $rating) { |
|
146 | + $counts[$type][$index] = intval($rating) + $counts[$type][$index]; |
|
147 | + } |
|
148 | + } |
|
149 | + } |
|
150 | + return $counts; |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * @return array |
|
155 | - */ |
|
156 | - protected function generateEmptyCountsArray() |
|
157 | - { |
|
158 | - return array_fill_keys(range(0, glsr()->constant('MAX_RATING', Rating::class)), 0); |
|
159 | - } |
|
153 | + /** |
|
154 | + * @return array |
|
155 | + */ |
|
156 | + protected function generateEmptyCountsArray() |
|
157 | + { |
|
158 | + return array_fill_keys(range(0, glsr()->constant('MAX_RATING', Rating::class)), 0); |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * @return array |
|
163 | - */ |
|
164 | - protected function get($args) |
|
165 | - { |
|
166 | - $results = []; |
|
167 | - foreach ($args['post_ids'] as $postId) { |
|
168 | - $results[] = glsr(PostCountsManager::class)->get($postId); |
|
169 | - } |
|
170 | - foreach ($args['term_ids'] as $termId) { |
|
171 | - $results[] = glsr(TermCountsManager::class)->get($termId); |
|
172 | - } |
|
173 | - if (empty($results)) { |
|
174 | - $results[] = glsr(GlobalCountsManager::class)->get(); |
|
175 | - } |
|
176 | - $results[] = ['local' => $this->generateEmptyCountsArray()]; // make sure there is a fallback |
|
177 | - return $this->combine($results); |
|
178 | - } |
|
161 | + /** |
|
162 | + * @return array |
|
163 | + */ |
|
164 | + protected function get($args) |
|
165 | + { |
|
166 | + $results = []; |
|
167 | + foreach ($args['post_ids'] as $postId) { |
|
168 | + $results[] = glsr(PostCountsManager::class)->get($postId); |
|
169 | + } |
|
170 | + foreach ($args['term_ids'] as $termId) { |
|
171 | + $results[] = glsr(TermCountsManager::class)->get($termId); |
|
172 | + } |
|
173 | + if (empty($results)) { |
|
174 | + $results[] = glsr(GlobalCountsManager::class)->get(); |
|
175 | + } |
|
176 | + $results[] = ['local' => $this->generateEmptyCountsArray()]; // make sure there is a fallback |
|
177 | + return $this->combine($results); |
|
178 | + } |
|
179 | 179 | |
180 | - /** |
|
181 | - * @return bool |
|
182 | - */ |
|
183 | - protected function hasMixedAssignment(array $args) |
|
184 | - { |
|
185 | - return !empty($args['post_ids']) && !empty($args['term_ids']); |
|
186 | - } |
|
180 | + /** |
|
181 | + * @return bool |
|
182 | + */ |
|
183 | + protected function hasMixedAssignment(array $args) |
|
184 | + { |
|
185 | + return !empty($args['post_ids']) && !empty($args['term_ids']); |
|
186 | + } |
|
187 | 187 | |
188 | - /** |
|
189 | - * @return array |
|
190 | - */ |
|
191 | - protected function normalize(array $reviewCounts) |
|
192 | - { |
|
193 | - foreach ($reviewCounts as &$counts) { |
|
194 | - foreach (array_keys($this->generateEmptyCountsArray()) as $index) { |
|
195 | - if (!isset($counts[$index])) { |
|
196 | - $counts[$index] = 0; |
|
197 | - } |
|
198 | - } |
|
199 | - ksort($counts); |
|
200 | - } |
|
201 | - return $reviewCounts; |
|
202 | - } |
|
188 | + /** |
|
189 | + * @return array |
|
190 | + */ |
|
191 | + protected function normalize(array $reviewCounts) |
|
192 | + { |
|
193 | + foreach ($reviewCounts as &$counts) { |
|
194 | + foreach (array_keys($this->generateEmptyCountsArray()) as $index) { |
|
195 | + if (!isset($counts[$index])) { |
|
196 | + $counts[$index] = 0; |
|
197 | + } |
|
198 | + } |
|
199 | + ksort($counts); |
|
200 | + } |
|
201 | + return $reviewCounts; |
|
202 | + } |
|
203 | 203 | |
204 | - /** |
|
205 | - * @return array |
|
206 | - */ |
|
207 | - protected function normalizeArgs(array $args) |
|
208 | - { |
|
209 | - $args = wp_parse_args(array_filter($args), [ |
|
210 | - 'post_ids' => [], |
|
211 | - 'term_ids' => [], |
|
212 | - 'type' => 'local', |
|
213 | - ]); |
|
214 | - $args['post_ids'] = glsr(Multilingual::class)->getPostIds($args['post_ids']); |
|
215 | - $args['type'] = $this->normalizeType($args['type']); |
|
216 | - return $args; |
|
217 | - } |
|
204 | + /** |
|
205 | + * @return array |
|
206 | + */ |
|
207 | + protected function normalizeArgs(array $args) |
|
208 | + { |
|
209 | + $args = wp_parse_args(array_filter($args), [ |
|
210 | + 'post_ids' => [], |
|
211 | + 'term_ids' => [], |
|
212 | + 'type' => 'local', |
|
213 | + ]); |
|
214 | + $args['post_ids'] = glsr(Multilingual::class)->getPostIds($args['post_ids']); |
|
215 | + $args['type'] = $this->normalizeType($args['type']); |
|
216 | + return $args; |
|
217 | + } |
|
218 | 218 | |
219 | - /** |
|
220 | - * @param string $type |
|
221 | - * @return string |
|
222 | - */ |
|
223 | - protected function normalizeType($type) |
|
224 | - { |
|
225 | - return empty($type) || !is_string($type) |
|
226 | - ? 'local' |
|
227 | - : $type; |
|
228 | - } |
|
219 | + /** |
|
220 | + * @param string $type |
|
221 | + * @return string |
|
222 | + */ |
|
223 | + protected function normalizeType($type) |
|
224 | + { |
|
225 | + return empty($type) || !is_string($type) |
|
226 | + ? 'local' |
|
227 | + : $type; |
|
228 | + } |
|
229 | 229 | |
230 | - /** |
|
231 | - * @param object $query |
|
232 | - * @return array |
|
233 | - */ |
|
234 | - protected function populateCountsFromQuery($query, array $counts) |
|
235 | - { |
|
236 | - foreach ($query->reviews as $review) { |
|
237 | - $type = $this->normalizeType($review->type); |
|
238 | - if (!array_key_exists($type, $counts)) { |
|
239 | - $counts[$type] = $this->generateEmptyCountsArray(); |
|
240 | - } |
|
241 | - ++$counts[$type][$review->rating]; |
|
242 | - } |
|
243 | - return $counts; |
|
244 | - } |
|
230 | + /** |
|
231 | + * @param object $query |
|
232 | + * @return array |
|
233 | + */ |
|
234 | + protected function populateCountsFromQuery($query, array $counts) |
|
235 | + { |
|
236 | + foreach ($query->reviews as $review) { |
|
237 | + $type = $this->normalizeType($review->type); |
|
238 | + if (!array_key_exists($type, $counts)) { |
|
239 | + $counts[$type] = $this->generateEmptyCountsArray(); |
|
240 | + } |
|
241 | + ++$counts[$type][$review->rating]; |
|
242 | + } |
|
243 | + return $counts; |
|
244 | + } |
|
245 | 245 | |
246 | - /** |
|
247 | - * @param int $lastPostId |
|
248 | - * @return object |
|
249 | - */ |
|
250 | - protected function queryReviews(array $args = [], $lastPostId = 0) |
|
251 | - { |
|
252 | - $reviews = glsr(SqlQueries::class)->getReviewCounts($args, $lastPostId, static::LIMIT); |
|
253 | - $hasMore = is_array($reviews) |
|
254 | - ? count($reviews) == static::LIMIT |
|
255 | - : false; |
|
256 | - return (object) [ |
|
257 | - 'has_more' => $hasMore, |
|
258 | - 'reviews' => $reviews, |
|
259 | - ]; |
|
260 | - } |
|
246 | + /** |
|
247 | + * @param int $lastPostId |
|
248 | + * @return object |
|
249 | + */ |
|
250 | + protected function queryReviews(array $args = [], $lastPostId = 0) |
|
251 | + { |
|
252 | + $reviews = glsr(SqlQueries::class)->getReviewCounts($args, $lastPostId, static::LIMIT); |
|
253 | + $hasMore = is_array($reviews) |
|
254 | + ? count($reviews) == static::LIMIT |
|
255 | + : false; |
|
256 | + return (object) [ |
|
257 | + 'has_more' => $hasMore, |
|
258 | + 'reviews' => $reviews, |
|
259 | + ]; |
|
260 | + } |
|
261 | 261 | } |
@@ -8,28 +8,28 @@ |
||
8 | 8 | |
9 | 9 | class TogglePinned |
10 | 10 | { |
11 | - /** |
|
12 | - * @return bool |
|
13 | - */ |
|
14 | - public function handle(Command $command) |
|
15 | - { |
|
16 | - if (!get_post($command->id)) { |
|
17 | - return false; |
|
18 | - } |
|
19 | - if (!glsr()->can('edit_others_posts')) { |
|
20 | - $meta = (Database::class)->get($command->id, 'pinned'); |
|
21 | - return wp_validate_boolean($meta); |
|
22 | - } |
|
23 | - if (is_null($command->pinned)) { |
|
24 | - $meta = glsr(Database::class)->get($command->id, 'pinned'); |
|
25 | - $command->pinned = !wp_validate_boolean($meta); |
|
26 | - } else { |
|
27 | - $notice = $command->pinned |
|
28 | - ? __('Review pinned.', 'site-reviews') |
|
29 | - : __('Review unpinned.', 'site-reviews'); |
|
30 | - glsr(Notice::class)->addSuccess($notice); |
|
31 | - } |
|
32 | - glsr(Database::class)->update($command->id, 'pinned', $command->pinned); |
|
33 | - return $command->pinned; |
|
34 | - } |
|
11 | + /** |
|
12 | + * @return bool |
|
13 | + */ |
|
14 | + public function handle(Command $command) |
|
15 | + { |
|
16 | + if (!get_post($command->id)) { |
|
17 | + return false; |
|
18 | + } |
|
19 | + if (!glsr()->can('edit_others_posts')) { |
|
20 | + $meta = (Database::class)->get($command->id, 'pinned'); |
|
21 | + return wp_validate_boolean($meta); |
|
22 | + } |
|
23 | + if (is_null($command->pinned)) { |
|
24 | + $meta = glsr(Database::class)->get($command->id, 'pinned'); |
|
25 | + $command->pinned = !wp_validate_boolean($meta); |
|
26 | + } else { |
|
27 | + $notice = $command->pinned |
|
28 | + ? __('Review pinned.', 'site-reviews') |
|
29 | + : __('Review unpinned.', 'site-reviews'); |
|
30 | + glsr(Notice::class)->addSuccess($notice); |
|
31 | + } |
|
32 | + glsr(Database::class)->update($command->id, 'pinned', $command->pinned); |
|
33 | + return $command->pinned; |
|
34 | + } |
|
35 | 35 | } |
@@ -6,133 +6,133 @@ |
||
6 | 6 | |
7 | 7 | class Role |
8 | 8 | { |
9 | - /** |
|
10 | - * @param string $role |
|
11 | - * @return void |
|
12 | - */ |
|
13 | - public function addCapabilities($role) |
|
14 | - { |
|
15 | - $roleCapabilities = $this->roleCapabilities(); |
|
16 | - $wpRole = get_role($role); |
|
17 | - if (empty($wpRole) || !array_key_exists($role, $roleCapabilities)) { |
|
18 | - return; |
|
19 | - } |
|
20 | - foreach ($roleCapabilities[$role] as $capability) { |
|
21 | - $wpRole->add_cap($this->normalizeCapability($capability)); |
|
22 | - } |
|
23 | - } |
|
9 | + /** |
|
10 | + * @param string $role |
|
11 | + * @return void |
|
12 | + */ |
|
13 | + public function addCapabilities($role) |
|
14 | + { |
|
15 | + $roleCapabilities = $this->roleCapabilities(); |
|
16 | + $wpRole = get_role($role); |
|
17 | + if (empty($wpRole) || !array_key_exists($role, $roleCapabilities)) { |
|
18 | + return; |
|
19 | + } |
|
20 | + foreach ($roleCapabilities[$role] as $capability) { |
|
21 | + $wpRole->add_cap($this->normalizeCapability($capability)); |
|
22 | + } |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * @param string $capability |
|
27 | - * @return bool |
|
28 | - */ |
|
29 | - public function can($capability) |
|
30 | - { |
|
31 | - return in_array($capability, $this->capabilities()) |
|
32 | - ? current_user_can($this->normalizeCapability($capability)) |
|
33 | - : current_user_can($capability); |
|
34 | - } |
|
25 | + /** |
|
26 | + * @param string $capability |
|
27 | + * @return bool |
|
28 | + */ |
|
29 | + public function can($capability) |
|
30 | + { |
|
31 | + return in_array($capability, $this->capabilities()) |
|
32 | + ? current_user_can($this->normalizeCapability($capability)) |
|
33 | + : current_user_can($capability); |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @param string $role |
|
38 | - * @return void |
|
39 | - */ |
|
40 | - public function removeCapabilities($role) |
|
41 | - { |
|
42 | - $wpRole = get_role($role); |
|
43 | - if (empty($wpRole) || 'administrator' === $role) { // do not remove from administrator role |
|
44 | - return; |
|
45 | - } |
|
46 | - foreach ($this->capabilities() as $capability) { |
|
47 | - $wpRole->remove_cap($this->normalizeCapability($capability)); |
|
48 | - } |
|
49 | - } |
|
36 | + /** |
|
37 | + * @param string $role |
|
38 | + * @return void |
|
39 | + */ |
|
40 | + public function removeCapabilities($role) |
|
41 | + { |
|
42 | + $wpRole = get_role($role); |
|
43 | + if (empty($wpRole) || 'administrator' === $role) { // do not remove from administrator role |
|
44 | + return; |
|
45 | + } |
|
46 | + foreach ($this->capabilities() as $capability) { |
|
47 | + $wpRole->remove_cap($this->normalizeCapability($capability)); |
|
48 | + } |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @return void |
|
53 | - */ |
|
54 | - public function resetAll() |
|
55 | - { |
|
56 | - $roles = array_keys(wp_roles()->roles); |
|
57 | - array_walk($roles, [$this, 'removeCapabilities']); |
|
58 | - $roles = array_keys($this->roleCapabilities()); |
|
59 | - array_walk($roles, [$this, 'addCapabilities']); |
|
60 | - } |
|
51 | + /** |
|
52 | + * @return void |
|
53 | + */ |
|
54 | + public function resetAll() |
|
55 | + { |
|
56 | + $roles = array_keys(wp_roles()->roles); |
|
57 | + array_walk($roles, [$this, 'removeCapabilities']); |
|
58 | + $roles = array_keys($this->roleCapabilities()); |
|
59 | + array_walk($roles, [$this, 'addCapabilities']); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * @return array |
|
64 | - */ |
|
65 | - protected function capabilities() |
|
66 | - { |
|
67 | - $capabilities = [ |
|
68 | - 'delete_others_posts', |
|
69 | - 'delete_post', |
|
70 | - 'delete_posts', |
|
71 | - 'delete_private_posts', |
|
72 | - 'delete_published_posts', |
|
73 | - 'edit_others_posts', |
|
74 | - 'edit_post', |
|
75 | - 'edit_posts', |
|
76 | - 'edit_private_posts', |
|
77 | - 'edit_published_posts', |
|
78 | - 'publish_posts', |
|
79 | - 'read_post', |
|
80 | - 'read_private_posts', |
|
81 | - ]; |
|
82 | - return apply_filters('site-reviews/capabilities', $capabilities); |
|
83 | - } |
|
62 | + /** |
|
63 | + * @return array |
|
64 | + */ |
|
65 | + protected function capabilities() |
|
66 | + { |
|
67 | + $capabilities = [ |
|
68 | + 'delete_others_posts', |
|
69 | + 'delete_post', |
|
70 | + 'delete_posts', |
|
71 | + 'delete_private_posts', |
|
72 | + 'delete_published_posts', |
|
73 | + 'edit_others_posts', |
|
74 | + 'edit_post', |
|
75 | + 'edit_posts', |
|
76 | + 'edit_private_posts', |
|
77 | + 'edit_published_posts', |
|
78 | + 'publish_posts', |
|
79 | + 'read_post', |
|
80 | + 'read_private_posts', |
|
81 | + ]; |
|
82 | + return apply_filters('site-reviews/capabilities', $capabilities); |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * @param string $capability |
|
87 | - * @return string |
|
88 | - */ |
|
89 | - protected function normalizeCapability($capability) |
|
90 | - { |
|
91 | - return str_replace('post', Application::POST_TYPE, $capability); |
|
92 | - } |
|
85 | + /** |
|
86 | + * @param string $capability |
|
87 | + * @return string |
|
88 | + */ |
|
89 | + protected function normalizeCapability($capability) |
|
90 | + { |
|
91 | + return str_replace('post', Application::POST_TYPE, $capability); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * @return array |
|
96 | - */ |
|
97 | - protected function roleCapabilities() |
|
98 | - { |
|
99 | - $capabilities = [ |
|
100 | - 'administrator' => [ |
|
101 | - 'delete_others_posts', |
|
102 | - 'delete_posts', |
|
103 | - 'delete_private_posts', |
|
104 | - 'delete_published_posts', |
|
105 | - 'edit_others_posts', |
|
106 | - 'edit_posts', |
|
107 | - 'edit_private_posts', |
|
108 | - 'edit_published_posts', |
|
109 | - 'publish_posts', |
|
110 | - 'read_private_posts', |
|
111 | - ], |
|
112 | - 'editor' => [ |
|
113 | - 'delete_others_posts', |
|
114 | - 'delete_posts', |
|
115 | - 'delete_private_posts', |
|
116 | - 'delete_published_posts', |
|
117 | - 'edit_others_posts', |
|
118 | - 'edit_posts', |
|
119 | - 'edit_private_posts', |
|
120 | - 'edit_published_posts', |
|
121 | - 'publish_posts', |
|
122 | - 'read_private_posts', |
|
123 | - ], |
|
124 | - 'author' => [ |
|
125 | - 'delete_posts', |
|
126 | - 'delete_published_posts', |
|
127 | - 'edit_posts', |
|
128 | - 'edit_published_posts', |
|
129 | - 'publish_posts', |
|
130 | - ], |
|
131 | - 'contributor' => [ |
|
132 | - 'delete_posts', |
|
133 | - 'edit_posts', |
|
134 | - ], |
|
135 | - ]; |
|
136 | - return apply_filters('site-reviews/capabilities/for-roles', $capabilities); |
|
137 | - } |
|
94 | + /** |
|
95 | + * @return array |
|
96 | + */ |
|
97 | + protected function roleCapabilities() |
|
98 | + { |
|
99 | + $capabilities = [ |
|
100 | + 'administrator' => [ |
|
101 | + 'delete_others_posts', |
|
102 | + 'delete_posts', |
|
103 | + 'delete_private_posts', |
|
104 | + 'delete_published_posts', |
|
105 | + 'edit_others_posts', |
|
106 | + 'edit_posts', |
|
107 | + 'edit_private_posts', |
|
108 | + 'edit_published_posts', |
|
109 | + 'publish_posts', |
|
110 | + 'read_private_posts', |
|
111 | + ], |
|
112 | + 'editor' => [ |
|
113 | + 'delete_others_posts', |
|
114 | + 'delete_posts', |
|
115 | + 'delete_private_posts', |
|
116 | + 'delete_published_posts', |
|
117 | + 'edit_others_posts', |
|
118 | + 'edit_posts', |
|
119 | + 'edit_private_posts', |
|
120 | + 'edit_published_posts', |
|
121 | + 'publish_posts', |
|
122 | + 'read_private_posts', |
|
123 | + ], |
|
124 | + 'author' => [ |
|
125 | + 'delete_posts', |
|
126 | + 'delete_published_posts', |
|
127 | + 'edit_posts', |
|
128 | + 'edit_published_posts', |
|
129 | + 'publish_posts', |
|
130 | + ], |
|
131 | + 'contributor' => [ |
|
132 | + 'delete_posts', |
|
133 | + 'edit_posts', |
|
134 | + ], |
|
135 | + ]; |
|
136 | + return apply_filters('site-reviews/capabilities/for-roles', $capabilities); |
|
137 | + } |
|
138 | 138 | } |
@@ -15,197 +15,197 @@ |
||
15 | 15 | |
16 | 16 | class MenuController extends Controller |
17 | 17 | { |
18 | - /** |
|
19 | - * @return void |
|
20 | - * @action admin_menu |
|
21 | - */ |
|
22 | - public function registerMenuCount() |
|
23 | - { |
|
24 | - global $menu, $typenow; |
|
25 | - foreach ($menu as $key => $value) { |
|
26 | - if (!isset($value[2]) || $value[2] != 'edit.php?post_type='.Application::POST_TYPE) { |
|
27 | - continue; |
|
28 | - } |
|
29 | - $postCount = wp_count_posts(Application::POST_TYPE); |
|
30 | - $pendingCount = glsr(Builder::class)->span(number_format_i18n($postCount->pending), [ |
|
31 | - 'class' => 'unapproved-count', |
|
32 | - ]); |
|
33 | - $awaitingModeration = glsr(Builder::class)->span($pendingCount, [ |
|
34 | - 'class' => 'awaiting-mod count-'.$postCount->pending, |
|
35 | - ]); |
|
36 | - $menu[$key][0].= ' '.$awaitingModeration; |
|
37 | - if (Application::POST_TYPE === $typenow) { |
|
38 | - $menu[$key][4].= ' current'; |
|
39 | - } |
|
40 | - break; |
|
41 | - } |
|
42 | - } |
|
18 | + /** |
|
19 | + * @return void |
|
20 | + * @action admin_menu |
|
21 | + */ |
|
22 | + public function registerMenuCount() |
|
23 | + { |
|
24 | + global $menu, $typenow; |
|
25 | + foreach ($menu as $key => $value) { |
|
26 | + if (!isset($value[2]) || $value[2] != 'edit.php?post_type='.Application::POST_TYPE) { |
|
27 | + continue; |
|
28 | + } |
|
29 | + $postCount = wp_count_posts(Application::POST_TYPE); |
|
30 | + $pendingCount = glsr(Builder::class)->span(number_format_i18n($postCount->pending), [ |
|
31 | + 'class' => 'unapproved-count', |
|
32 | + ]); |
|
33 | + $awaitingModeration = glsr(Builder::class)->span($pendingCount, [ |
|
34 | + 'class' => 'awaiting-mod count-'.$postCount->pending, |
|
35 | + ]); |
|
36 | + $menu[$key][0].= ' '.$awaitingModeration; |
|
37 | + if (Application::POST_TYPE === $typenow) { |
|
38 | + $menu[$key][4].= ' current'; |
|
39 | + } |
|
40 | + break; |
|
41 | + } |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * @return void |
|
46 | - * @action admin_menu |
|
47 | - */ |
|
48 | - public function registerSubMenus() |
|
49 | - { |
|
50 | - $pages = $this->parseWithFilter('submenu/pages', [ |
|
51 | - 'settings' => __('Settings', 'site-reviews'), |
|
52 | - 'tools' => __('Tools', 'site-reviews'), |
|
53 | - 'addons' => __('Add-ons', 'site-reviews'), |
|
54 | - 'documentation' => __('Help', 'site-reviews'), |
|
55 | - ]); |
|
56 | - foreach ($pages as $slug => $title) { |
|
57 | - $method = Helper::buildMethodName('render-'.$slug.'-menu'); |
|
58 | - $callback = apply_filters('site-reviews/addon/submenu/callback', [$this, $method], $slug); |
|
59 | - if (!is_callable($callback)) { |
|
60 | - continue; |
|
61 | - } |
|
62 | - add_submenu_page('edit.php?post_type='.Application::POST_TYPE, $title, $title, glsr()->getPermission($slug), $slug, $callback); |
|
63 | - } |
|
64 | - } |
|
44 | + /** |
|
45 | + * @return void |
|
46 | + * @action admin_menu |
|
47 | + */ |
|
48 | + public function registerSubMenus() |
|
49 | + { |
|
50 | + $pages = $this->parseWithFilter('submenu/pages', [ |
|
51 | + 'settings' => __('Settings', 'site-reviews'), |
|
52 | + 'tools' => __('Tools', 'site-reviews'), |
|
53 | + 'addons' => __('Add-ons', 'site-reviews'), |
|
54 | + 'documentation' => __('Help', 'site-reviews'), |
|
55 | + ]); |
|
56 | + foreach ($pages as $slug => $title) { |
|
57 | + $method = Helper::buildMethodName('render-'.$slug.'-menu'); |
|
58 | + $callback = apply_filters('site-reviews/addon/submenu/callback', [$this, $method], $slug); |
|
59 | + if (!is_callable($callback)) { |
|
60 | + continue; |
|
61 | + } |
|
62 | + add_submenu_page('edit.php?post_type='.Application::POST_TYPE, $title, $title, glsr()->getPermission($slug), $slug, $callback); |
|
63 | + } |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @return void |
|
68 | - * @see $this->registerSubMenus() |
|
69 | - * @callback add_submenu_page |
|
70 | - */ |
|
71 | - public function renderAddonsMenu() |
|
72 | - { |
|
73 | - $this->renderPage('addons', [ |
|
74 | - 'template' => glsr(Template::class), |
|
75 | - ]); |
|
76 | - } |
|
66 | + /** |
|
67 | + * @return void |
|
68 | + * @see $this->registerSubMenus() |
|
69 | + * @callback add_submenu_page |
|
70 | + */ |
|
71 | + public function renderAddonsMenu() |
|
72 | + { |
|
73 | + $this->renderPage('addons', [ |
|
74 | + 'template' => glsr(Template::class), |
|
75 | + ]); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @return void |
|
80 | - * @see $this->registerSubMenus() |
|
81 | - * @callback add_submenu_page |
|
82 | - */ |
|
83 | - public function renderDocumentationMenu() |
|
84 | - { |
|
85 | - $tabs = $this->parseWithFilter('documentation/tabs', [ |
|
86 | - 'support' => __('Support', 'site-reviews'), |
|
87 | - 'faq' => __('FAQ', 'site-reviews'), |
|
88 | - 'shortcodes' => __('Shortcodes', 'site-reviews'), |
|
89 | - 'hooks' => __('Hooks', 'site-reviews'), |
|
90 | - 'functions' => __('Functions', 'site-reviews'), |
|
91 | - 'addons' => __('Addons', 'site-reviews'), |
|
92 | - ]); |
|
93 | - $addons = apply_filters('site-reviews/addon/documentation', []); |
|
94 | - ksort($addons); |
|
95 | - if (empty($addons)) { |
|
96 | - unset($tabs['addons']); |
|
97 | - } |
|
98 | - $this->renderPage('documentation', [ |
|
99 | - 'addons' => $addons, |
|
100 | - 'tabs' => $tabs, |
|
101 | - ]); |
|
102 | - } |
|
78 | + /** |
|
79 | + * @return void |
|
80 | + * @see $this->registerSubMenus() |
|
81 | + * @callback add_submenu_page |
|
82 | + */ |
|
83 | + public function renderDocumentationMenu() |
|
84 | + { |
|
85 | + $tabs = $this->parseWithFilter('documentation/tabs', [ |
|
86 | + 'support' => __('Support', 'site-reviews'), |
|
87 | + 'faq' => __('FAQ', 'site-reviews'), |
|
88 | + 'shortcodes' => __('Shortcodes', 'site-reviews'), |
|
89 | + 'hooks' => __('Hooks', 'site-reviews'), |
|
90 | + 'functions' => __('Functions', 'site-reviews'), |
|
91 | + 'addons' => __('Addons', 'site-reviews'), |
|
92 | + ]); |
|
93 | + $addons = apply_filters('site-reviews/addon/documentation', []); |
|
94 | + ksort($addons); |
|
95 | + if (empty($addons)) { |
|
96 | + unset($tabs['addons']); |
|
97 | + } |
|
98 | + $this->renderPage('documentation', [ |
|
99 | + 'addons' => $addons, |
|
100 | + 'tabs' => $tabs, |
|
101 | + ]); |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * @return void |
|
106 | - * @see $this->registerSubMenus() |
|
107 | - * @callback add_submenu_page |
|
108 | - */ |
|
109 | - public function renderSettingsMenu() |
|
110 | - { |
|
111 | - $tabs = $this->parseWithFilter('settings/tabs', [ |
|
112 | - 'general' => __('General', 'site-reviews'), |
|
113 | - 'reviews' => __('Reviews', 'site-reviews'), |
|
114 | - 'submissions' => __('Submissions', 'site-reviews'), |
|
115 | - 'schema' => __('Schema', 'site-reviews'), |
|
116 | - 'translations' => __('Translations', 'site-reviews'), |
|
117 | - 'addons' => __('Addons', 'site-reviews'), |
|
118 | - 'licenses' => __('Licenses', 'site-reviews'), |
|
119 | - ]); |
|
120 | - if (empty(Arr::get(glsr()->defaults, 'settings.addons'))) { |
|
121 | - unset($tabs['addons']); |
|
122 | - } |
|
123 | - if (empty(Arr::get(glsr()->defaults, 'settings.licenses'))) { |
|
124 | - unset($tabs['licenses']); |
|
125 | - } |
|
126 | - $this->renderPage('settings', [ |
|
127 | - 'settings' => glsr(Settings::class), |
|
128 | - 'tabs' => $tabs, |
|
129 | - ]); |
|
130 | - } |
|
104 | + /** |
|
105 | + * @return void |
|
106 | + * @see $this->registerSubMenus() |
|
107 | + * @callback add_submenu_page |
|
108 | + */ |
|
109 | + public function renderSettingsMenu() |
|
110 | + { |
|
111 | + $tabs = $this->parseWithFilter('settings/tabs', [ |
|
112 | + 'general' => __('General', 'site-reviews'), |
|
113 | + 'reviews' => __('Reviews', 'site-reviews'), |
|
114 | + 'submissions' => __('Submissions', 'site-reviews'), |
|
115 | + 'schema' => __('Schema', 'site-reviews'), |
|
116 | + 'translations' => __('Translations', 'site-reviews'), |
|
117 | + 'addons' => __('Addons', 'site-reviews'), |
|
118 | + 'licenses' => __('Licenses', 'site-reviews'), |
|
119 | + ]); |
|
120 | + if (empty(Arr::get(glsr()->defaults, 'settings.addons'))) { |
|
121 | + unset($tabs['addons']); |
|
122 | + } |
|
123 | + if (empty(Arr::get(glsr()->defaults, 'settings.licenses'))) { |
|
124 | + unset($tabs['licenses']); |
|
125 | + } |
|
126 | + $this->renderPage('settings', [ |
|
127 | + 'settings' => glsr(Settings::class), |
|
128 | + 'tabs' => $tabs, |
|
129 | + ]); |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * @return void |
|
134 | - * @see $this->registerSubMenus() |
|
135 | - * @callback add_submenu_page |
|
136 | - */ |
|
137 | - public function renderToolsMenu() |
|
138 | - { |
|
139 | - $tabs = $this->parseWithFilter('tools/tabs', [ |
|
140 | - 'general' => __('General', 'site-reviews'), |
|
141 | - 'sync' => __('Sync Reviews', 'site-reviews'), |
|
142 | - 'console' => __('Console', 'site-reviews'), |
|
143 | - 'system-info' => __('System Info', 'site-reviews'), |
|
144 | - ]); |
|
145 | - if (!apply_filters('site-reviews/addon/sync/enable', false)) { |
|
146 | - unset($tabs['sync']); |
|
147 | - } |
|
148 | - $this->renderPage('tools', [ |
|
149 | - 'data' => [ |
|
150 | - 'context' => [ |
|
151 | - 'base_url' => admin_url('edit.php?post_type='.Application::POST_TYPE), |
|
152 | - 'console' => strval(glsr(Console::class)), |
|
153 | - 'id' => Application::ID, |
|
154 | - 'system' => strval(glsr(System::class)), |
|
155 | - ], |
|
156 | - 'services' => apply_filters('site-reviews/addon/sync/services', []), |
|
157 | - ], |
|
158 | - 'tabs' => $tabs, |
|
159 | - 'template' => glsr(Template::class), |
|
160 | - ]); |
|
161 | - } |
|
132 | + /** |
|
133 | + * @return void |
|
134 | + * @see $this->registerSubMenus() |
|
135 | + * @callback add_submenu_page |
|
136 | + */ |
|
137 | + public function renderToolsMenu() |
|
138 | + { |
|
139 | + $tabs = $this->parseWithFilter('tools/tabs', [ |
|
140 | + 'general' => __('General', 'site-reviews'), |
|
141 | + 'sync' => __('Sync Reviews', 'site-reviews'), |
|
142 | + 'console' => __('Console', 'site-reviews'), |
|
143 | + 'system-info' => __('System Info', 'site-reviews'), |
|
144 | + ]); |
|
145 | + if (!apply_filters('site-reviews/addon/sync/enable', false)) { |
|
146 | + unset($tabs['sync']); |
|
147 | + } |
|
148 | + $this->renderPage('tools', [ |
|
149 | + 'data' => [ |
|
150 | + 'context' => [ |
|
151 | + 'base_url' => admin_url('edit.php?post_type='.Application::POST_TYPE), |
|
152 | + 'console' => strval(glsr(Console::class)), |
|
153 | + 'id' => Application::ID, |
|
154 | + 'system' => strval(glsr(System::class)), |
|
155 | + ], |
|
156 | + 'services' => apply_filters('site-reviews/addon/sync/services', []), |
|
157 | + ], |
|
158 | + 'tabs' => $tabs, |
|
159 | + 'template' => glsr(Template::class), |
|
160 | + ]); |
|
161 | + } |
|
162 | 162 | |
163 | - /** |
|
164 | - * @return void |
|
165 | - * @action admin_init |
|
166 | - */ |
|
167 | - public function setCustomPermissions() |
|
168 | - { |
|
169 | - foreach (wp_roles()->roles as $role => $value) { |
|
170 | - wp_roles()->remove_cap($role, 'create_'.Application::POST_TYPE); |
|
171 | - } |
|
172 | - } |
|
163 | + /** |
|
164 | + * @return void |
|
165 | + * @action admin_init |
|
166 | + */ |
|
167 | + public function setCustomPermissions() |
|
168 | + { |
|
169 | + foreach (wp_roles()->roles as $role => $value) { |
|
170 | + wp_roles()->remove_cap($role, 'create_'.Application::POST_TYPE); |
|
171 | + } |
|
172 | + } |
|
173 | 173 | |
174 | - /** |
|
175 | - * @return string |
|
176 | - */ |
|
177 | - protected function getNotices() |
|
178 | - { |
|
179 | - return glsr(Builder::class)->div(glsr(Notice::class)->get(), [ |
|
180 | - 'id' => 'glsr-notices', |
|
181 | - ]); |
|
182 | - } |
|
174 | + /** |
|
175 | + * @return string |
|
176 | + */ |
|
177 | + protected function getNotices() |
|
178 | + { |
|
179 | + return glsr(Builder::class)->div(glsr(Notice::class)->get(), [ |
|
180 | + 'id' => 'glsr-notices', |
|
181 | + ]); |
|
182 | + } |
|
183 | 183 | |
184 | - /** |
|
185 | - * @param string $hookSuffix |
|
186 | - * @return array |
|
187 | - */ |
|
188 | - protected function parseWithFilter($hookSuffix, array $args = []) |
|
189 | - { |
|
190 | - if (Str::endsWith('/tabs', $hookSuffix)) { |
|
191 | - $page = str_replace('/tabs', '', $hookSuffix); |
|
192 | - foreach ($args as $tab => $title) { |
|
193 | - if (!glsr()->hasPermission($page, $tab)) { |
|
194 | - unset($args[$tab]); |
|
195 | - } |
|
196 | - } |
|
197 | - } |
|
198 | - return apply_filters('site-reviews/addon/'.$hookSuffix, $args); |
|
199 | - } |
|
184 | + /** |
|
185 | + * @param string $hookSuffix |
|
186 | + * @return array |
|
187 | + */ |
|
188 | + protected function parseWithFilter($hookSuffix, array $args = []) |
|
189 | + { |
|
190 | + if (Str::endsWith('/tabs', $hookSuffix)) { |
|
191 | + $page = str_replace('/tabs', '', $hookSuffix); |
|
192 | + foreach ($args as $tab => $title) { |
|
193 | + if (!glsr()->hasPermission($page, $tab)) { |
|
194 | + unset($args[$tab]); |
|
195 | + } |
|
196 | + } |
|
197 | + } |
|
198 | + return apply_filters('site-reviews/addon/'.$hookSuffix, $args); |
|
199 | + } |
|
200 | 200 | |
201 | - /** |
|
202 | - * @param string $page |
|
203 | - * @return void |
|
204 | - */ |
|
205 | - protected function renderPage($page, array $data = []) |
|
206 | - { |
|
207 | - $data['http_referer'] = (string) wp_get_referer(); |
|
208 | - $data['notices'] = $this->getNotices(); |
|
209 | - glsr()->render('pages/'.$page.'/index', $data); |
|
210 | - } |
|
201 | + /** |
|
202 | + * @param string $page |
|
203 | + * @return void |
|
204 | + */ |
|
205 | + protected function renderPage($page, array $data = []) |
|
206 | + { |
|
207 | + $data['http_referer'] = (string) wp_get_referer(); |
|
208 | + $data['notices'] = $this->getNotices(); |
|
209 | + glsr()->render('pages/'.$page.'/index', $data); |
|
210 | + } |
|
211 | 211 | } |
@@ -10,180 +10,180 @@ |
||
10 | 10 | |
11 | 11 | class Columns |
12 | 12 | { |
13 | - /** |
|
14 | - * @param int $postId |
|
15 | - * @return void|string |
|
16 | - */ |
|
17 | - public function buildColumnAssignedTo($postId) |
|
18 | - { |
|
19 | - $assignedPost = glsr(Database::class)->getAssignedToPost($postId); |
|
20 | - if ($assignedPost instanceof WP_Post && 'publish' == $assignedPost->post_status) { |
|
21 | - return glsr(Builder::class)->a(get_the_title($assignedPost->ID), [ |
|
22 | - 'href' => (string) get_the_permalink($assignedPost->ID), |
|
23 | - ]); |
|
24 | - } |
|
25 | - } |
|
13 | + /** |
|
14 | + * @param int $postId |
|
15 | + * @return void|string |
|
16 | + */ |
|
17 | + public function buildColumnAssignedTo($postId) |
|
18 | + { |
|
19 | + $assignedPost = glsr(Database::class)->getAssignedToPost($postId); |
|
20 | + if ($assignedPost instanceof WP_Post && 'publish' == $assignedPost->post_status) { |
|
21 | + return glsr(Builder::class)->a(get_the_title($assignedPost->ID), [ |
|
22 | + 'href' => (string) get_the_permalink($assignedPost->ID), |
|
23 | + ]); |
|
24 | + } |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * @param int $postId |
|
29 | - * @return void|string |
|
30 | - */ |
|
31 | - public function buildColumnEmail($postId) |
|
32 | - { |
|
33 | - if ($email = glsr(Database::class)->get($postId, 'email')) { |
|
34 | - return $email; |
|
35 | - } |
|
36 | - } |
|
27 | + /** |
|
28 | + * @param int $postId |
|
29 | + * @return void|string |
|
30 | + */ |
|
31 | + public function buildColumnEmail($postId) |
|
32 | + { |
|
33 | + if ($email = glsr(Database::class)->get($postId, 'email')) { |
|
34 | + return $email; |
|
35 | + } |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * @param int $postId |
|
40 | - * @return void|string |
|
41 | - */ |
|
42 | - public function buildColumnIpAddress($postId) |
|
43 | - { |
|
44 | - if ($ipAddress = glsr(Database::class)->get($postId, 'ip_address')) { |
|
45 | - return $ipAddress; |
|
46 | - } |
|
47 | - } |
|
38 | + /** |
|
39 | + * @param int $postId |
|
40 | + * @return void|string |
|
41 | + */ |
|
42 | + public function buildColumnIpAddress($postId) |
|
43 | + { |
|
44 | + if ($ipAddress = glsr(Database::class)->get($postId, 'ip_address')) { |
|
45 | + return $ipAddress; |
|
46 | + } |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @param int $postId |
|
51 | - * @return string |
|
52 | - */ |
|
53 | - public function buildColumnPinned($postId) |
|
54 | - { |
|
55 | - $pinned = glsr(Database::class)->get($postId, 'pinned') |
|
56 | - ? 'pinned ' |
|
57 | - : ''; |
|
58 | - if (glsr()->can('edit_others_posts')) { |
|
59 | - $pinned.= 'pin-review '; |
|
60 | - } |
|
61 | - return glsr(Builder::class)->i([ |
|
62 | - 'class' => $pinned.'dashicons dashicons-sticky', |
|
63 | - 'data-id' => $postId, |
|
64 | - ]); |
|
65 | - } |
|
49 | + /** |
|
50 | + * @param int $postId |
|
51 | + * @return string |
|
52 | + */ |
|
53 | + public function buildColumnPinned($postId) |
|
54 | + { |
|
55 | + $pinned = glsr(Database::class)->get($postId, 'pinned') |
|
56 | + ? 'pinned ' |
|
57 | + : ''; |
|
58 | + if (glsr()->can('edit_others_posts')) { |
|
59 | + $pinned.= 'pin-review '; |
|
60 | + } |
|
61 | + return glsr(Builder::class)->i([ |
|
62 | + 'class' => $pinned.'dashicons dashicons-sticky', |
|
63 | + 'data-id' => $postId, |
|
64 | + ]); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * @param int $postId |
|
69 | - * @return string |
|
70 | - */ |
|
71 | - public function buildColumnResponse($postId) |
|
72 | - { |
|
73 | - return glsr(Database::class)->get($postId, 'response') |
|
74 | - ? __('Yes', 'site-reviews') |
|
75 | - : __('No', 'site-reviews'); |
|
76 | - } |
|
67 | + /** |
|
68 | + * @param int $postId |
|
69 | + * @return string |
|
70 | + */ |
|
71 | + public function buildColumnResponse($postId) |
|
72 | + { |
|
73 | + return glsr(Database::class)->get($postId, 'response') |
|
74 | + ? __('Yes', 'site-reviews') |
|
75 | + : __('No', 'site-reviews'); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @param int $postId |
|
80 | - * @return string |
|
81 | - */ |
|
82 | - public function buildColumnReviewer($postId) |
|
83 | - { |
|
84 | - return strval(glsr(Database::class)->get($postId, 'author')); |
|
85 | - } |
|
78 | + /** |
|
79 | + * @param int $postId |
|
80 | + * @return string |
|
81 | + */ |
|
82 | + public function buildColumnReviewer($postId) |
|
83 | + { |
|
84 | + return strval(glsr(Database::class)->get($postId, 'author')); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * @param int $postId |
|
89 | - * @param int|null $rating |
|
90 | - * @return string |
|
91 | - */ |
|
92 | - public function buildColumnRating($postId) |
|
93 | - { |
|
94 | - return glsr_star_rating(intval(glsr(Database::class)->get($postId, 'rating'))); |
|
95 | - } |
|
87 | + /** |
|
88 | + * @param int $postId |
|
89 | + * @param int|null $rating |
|
90 | + * @return string |
|
91 | + */ |
|
92 | + public function buildColumnRating($postId) |
|
93 | + { |
|
94 | + return glsr_star_rating(intval(glsr(Database::class)->get($postId, 'rating'))); |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * @param int $postId |
|
99 | - * @return string |
|
100 | - */ |
|
101 | - public function buildColumnReviewType($postId) |
|
102 | - { |
|
103 | - $type = glsr(Database::class)->get($postId, 'review_type'); |
|
104 | - return array_key_exists($type, glsr()->reviewTypes) |
|
105 | - ? glsr()->reviewTypes[$type] |
|
106 | - : __('Unsupported Type', 'site-reviews'); |
|
107 | - } |
|
97 | + /** |
|
98 | + * @param int $postId |
|
99 | + * @return string |
|
100 | + */ |
|
101 | + public function buildColumnReviewType($postId) |
|
102 | + { |
|
103 | + $type = glsr(Database::class)->get($postId, 'review_type'); |
|
104 | + return array_key_exists($type, glsr()->reviewTypes) |
|
105 | + ? glsr()->reviewTypes[$type] |
|
106 | + : __('Unsupported Type', 'site-reviews'); |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * @param string $postType |
|
111 | - * @return void |
|
112 | - */ |
|
113 | - public function renderFilters($postType) |
|
114 | - { |
|
115 | - if (Application::POST_TYPE !== $postType) { |
|
116 | - return; |
|
117 | - } |
|
118 | - if (!($status = filter_input(INPUT_GET, 'post_status'))) { |
|
119 | - $status = 'publish'; |
|
120 | - } |
|
121 | - $ratings = glsr(Database::class)->getReviewsMeta('rating', $status); |
|
122 | - $types = glsr(Database::class)->getReviewsMeta('review_type', $status); |
|
123 | - $this->renderFilterRatings($ratings); |
|
124 | - $this->renderFilterTypes($types); |
|
125 | - } |
|
109 | + /** |
|
110 | + * @param string $postType |
|
111 | + * @return void |
|
112 | + */ |
|
113 | + public function renderFilters($postType) |
|
114 | + { |
|
115 | + if (Application::POST_TYPE !== $postType) { |
|
116 | + return; |
|
117 | + } |
|
118 | + if (!($status = filter_input(INPUT_GET, 'post_status'))) { |
|
119 | + $status = 'publish'; |
|
120 | + } |
|
121 | + $ratings = glsr(Database::class)->getReviewsMeta('rating', $status); |
|
122 | + $types = glsr(Database::class)->getReviewsMeta('review_type', $status); |
|
123 | + $this->renderFilterRatings($ratings); |
|
124 | + $this->renderFilterTypes($types); |
|
125 | + } |
|
126 | 126 | |
127 | - /** |
|
128 | - * @param string $column |
|
129 | - * @param int $postId |
|
130 | - * @return void |
|
131 | - */ |
|
132 | - public function renderValues($column, $postId) |
|
133 | - { |
|
134 | - $method = Helper::buildMethodName($column, 'buildColumn'); |
|
135 | - $value = method_exists($this, $method) |
|
136 | - ? call_user_func([$this, $method], $postId) |
|
137 | - : ''; |
|
138 | - $value = apply_filters('site-reviews/columns/'.$column, $value, $postId); |
|
139 | - if (0 !== $value && empty($value)) { |
|
140 | - $value = '—'; |
|
141 | - } |
|
142 | - echo $value; |
|
143 | - } |
|
127 | + /** |
|
128 | + * @param string $column |
|
129 | + * @param int $postId |
|
130 | + * @return void |
|
131 | + */ |
|
132 | + public function renderValues($column, $postId) |
|
133 | + { |
|
134 | + $method = Helper::buildMethodName($column, 'buildColumn'); |
|
135 | + $value = method_exists($this, $method) |
|
136 | + ? call_user_func([$this, $method], $postId) |
|
137 | + : ''; |
|
138 | + $value = apply_filters('site-reviews/columns/'.$column, $value, $postId); |
|
139 | + if (0 !== $value && empty($value)) { |
|
140 | + $value = '—'; |
|
141 | + } |
|
142 | + echo $value; |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * @param array $ratings |
|
147 | - * @return void |
|
148 | - */ |
|
149 | - protected function renderFilterRatings($ratings) |
|
150 | - { |
|
151 | - if (empty($ratings)) { |
|
152 | - return; |
|
153 | - } |
|
154 | - $ratings = array_flip(array_reverse($ratings)); |
|
155 | - array_walk($ratings, function (&$value, $key) { |
|
156 | - $label = _n('%s star', '%s stars', $key, 'site-reviews'); |
|
157 | - $value = sprintf($label, $key); |
|
158 | - }); |
|
159 | - echo glsr(Builder::class)->label(__('Filter by rating', 'site-reviews'), [ |
|
160 | - 'class' => 'screen-reader-text', |
|
161 | - 'for' => 'rating', |
|
162 | - ]); |
|
163 | - echo glsr(Builder::class)->select([ |
|
164 | - 'name' => 'rating', |
|
165 | - 'options' => ['' => __('All ratings', 'site-reviews')] + $ratings, |
|
166 | - 'value' => filter_input(INPUT_GET, 'rating'), |
|
167 | - ]); |
|
168 | - } |
|
145 | + /** |
|
146 | + * @param array $ratings |
|
147 | + * @return void |
|
148 | + */ |
|
149 | + protected function renderFilterRatings($ratings) |
|
150 | + { |
|
151 | + if (empty($ratings)) { |
|
152 | + return; |
|
153 | + } |
|
154 | + $ratings = array_flip(array_reverse($ratings)); |
|
155 | + array_walk($ratings, function (&$value, $key) { |
|
156 | + $label = _n('%s star', '%s stars', $key, 'site-reviews'); |
|
157 | + $value = sprintf($label, $key); |
|
158 | + }); |
|
159 | + echo glsr(Builder::class)->label(__('Filter by rating', 'site-reviews'), [ |
|
160 | + 'class' => 'screen-reader-text', |
|
161 | + 'for' => 'rating', |
|
162 | + ]); |
|
163 | + echo glsr(Builder::class)->select([ |
|
164 | + 'name' => 'rating', |
|
165 | + 'options' => ['' => __('All ratings', 'site-reviews')] + $ratings, |
|
166 | + 'value' => filter_input(INPUT_GET, 'rating'), |
|
167 | + ]); |
|
168 | + } |
|
169 | 169 | |
170 | - /** |
|
171 | - * @param array $types |
|
172 | - * @return void |
|
173 | - */ |
|
174 | - protected function renderFilterTypes($types) |
|
175 | - { |
|
176 | - if (count(glsr()->reviewTypes) < 2) { |
|
177 | - return; |
|
178 | - } |
|
179 | - echo glsr(Builder::class)->label(__('Filter by type', 'site-reviews'), [ |
|
180 | - 'class' => 'screen-reader-text', |
|
181 | - 'for' => 'review_type', |
|
182 | - ]); |
|
183 | - echo glsr(Builder::class)->select([ |
|
184 | - 'name' => 'review_type', |
|
185 | - 'options' => ['' => __('All types', 'site-reviews')] + glsr()->reviewTypes, |
|
186 | - 'value' => filter_input(INPUT_GET, 'review_type'), |
|
187 | - ]); |
|
188 | - } |
|
170 | + /** |
|
171 | + * @param array $types |
|
172 | + * @return void |
|
173 | + */ |
|
174 | + protected function renderFilterTypes($types) |
|
175 | + { |
|
176 | + if (count(glsr()->reviewTypes) < 2) { |
|
177 | + return; |
|
178 | + } |
|
179 | + echo glsr(Builder::class)->label(__('Filter by type', 'site-reviews'), [ |
|
180 | + 'class' => 'screen-reader-text', |
|
181 | + 'for' => 'review_type', |
|
182 | + ]); |
|
183 | + echo glsr(Builder::class)->select([ |
|
184 | + 'name' => 'review_type', |
|
185 | + 'options' => ['' => __('All types', 'site-reviews')] + glsr()->reviewTypes, |
|
186 | + 'value' => filter_input(INPUT_GET, 'review_type'), |
|
187 | + ]); |
|
188 | + } |
|
189 | 189 | } |