@@ -6,54 +6,54 @@ |
||
6 | 6 | |
7 | 7 | class Blacklist |
8 | 8 | { |
9 | - /** |
|
10 | - * @return bool |
|
11 | - */ |
|
12 | - public function isBlacklisted(array $review) |
|
13 | - { |
|
14 | - $target = implode("\n", array_filter([ |
|
15 | - $review['name'], |
|
16 | - $review['content'], |
|
17 | - $review['email'], |
|
18 | - $review['ip_address'], |
|
19 | - $review['title'], |
|
20 | - ])); |
|
21 | - return (bool) apply_filters('site-reviews/blacklist/is-blacklisted', |
|
22 | - $this->check($target), |
|
23 | - $review, |
|
24 | - $target |
|
25 | - ); |
|
26 | - } |
|
9 | + /** |
|
10 | + * @return bool |
|
11 | + */ |
|
12 | + public function isBlacklisted(array $review) |
|
13 | + { |
|
14 | + $target = implode("\n", array_filter([ |
|
15 | + $review['name'], |
|
16 | + $review['content'], |
|
17 | + $review['email'], |
|
18 | + $review['ip_address'], |
|
19 | + $review['title'], |
|
20 | + ])); |
|
21 | + return (bool) apply_filters('site-reviews/blacklist/is-blacklisted', |
|
22 | + $this->check($target), |
|
23 | + $review, |
|
24 | + $target |
|
25 | + ); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * @param string $target |
|
30 | - * @return bool |
|
31 | - */ |
|
32 | - protected function check($target) |
|
33 | - { |
|
34 | - $blacklist = $this->getBlacklist(); |
|
35 | - if (empty($blacklist)) { |
|
36 | - return false; |
|
37 | - } |
|
38 | - $lines = explode("\n", $blacklist); |
|
39 | - foreach ((array) $lines as $line) { |
|
40 | - $line = trim($line); |
|
41 | - if (empty($line) || 256 < strlen($line)) { |
|
42 | - continue; |
|
43 | - } |
|
44 | - $pattern = sprintf('#%s#i', preg_quote($line, '#')); |
|
45 | - if (preg_match($pattern, $target)) { |
|
46 | - return true; |
|
47 | - } |
|
48 | - } |
|
49 | - return false; |
|
50 | - } |
|
28 | + /** |
|
29 | + * @param string $target |
|
30 | + * @return bool |
|
31 | + */ |
|
32 | + protected function check($target) |
|
33 | + { |
|
34 | + $blacklist = $this->getBlacklist(); |
|
35 | + if (empty($blacklist)) { |
|
36 | + return false; |
|
37 | + } |
|
38 | + $lines = explode("\n", $blacklist); |
|
39 | + foreach ((array) $lines as $line) { |
|
40 | + $line = trim($line); |
|
41 | + if (empty($line) || 256 < strlen($line)) { |
|
42 | + continue; |
|
43 | + } |
|
44 | + $pattern = sprintf('#%s#i', preg_quote($line, '#')); |
|
45 | + if (preg_match($pattern, $target)) { |
|
46 | + return true; |
|
47 | + } |
|
48 | + } |
|
49 | + return false; |
|
50 | + } |
|
51 | 51 | |
52 | - protected function getBlacklist() |
|
53 | - { |
|
54 | - $option = glsr(OptionManager::class)->get('settings.submissions.blacklist.integration'); |
|
55 | - return $option == 'comments' |
|
56 | - ? trim(glsr(OptionManager::class)->getWP('blacklist_keys')) |
|
57 | - : trim(glsr(OptionManager::class)->get('settings.submissions.blacklist.entries')); |
|
58 | - } |
|
52 | + protected function getBlacklist() |
|
53 | + { |
|
54 | + $option = glsr(OptionManager::class)->get('settings.submissions.blacklist.integration'); |
|
55 | + return $option == 'comments' |
|
56 | + ? trim(glsr(OptionManager::class)->getWP('blacklist_keys')) |
|
57 | + : trim(glsr(OptionManager::class)->get('settings.submissions.blacklist.entries')); |
|
58 | + } |
|
59 | 59 | } |
@@ -9,17 +9,17 @@ discard block |
||
9 | 9 | /** |
10 | 10 | * @return bool |
11 | 11 | */ |
12 | - public function isBlacklisted(array $review) |
|
12 | + public function isBlacklisted( array $review ) |
|
13 | 13 | { |
14 | - $target = implode("\n", array_filter([ |
|
14 | + $target = implode( "\n", array_filter( [ |
|
15 | 15 | $review['name'], |
16 | 16 | $review['content'], |
17 | 17 | $review['email'], |
18 | 18 | $review['ip_address'], |
19 | 19 | $review['title'], |
20 | - ])); |
|
21 | - return (bool) apply_filters('site-reviews/blacklist/is-blacklisted', |
|
22 | - $this->check($target), |
|
20 | + ] ) ); |
|
21 | + return (bool)apply_filters( 'site-reviews/blacklist/is-blacklisted', |
|
22 | + $this->check( $target ), |
|
23 | 23 | $review, |
24 | 24 | $target |
25 | 25 | ); |
@@ -29,20 +29,20 @@ discard block |
||
29 | 29 | * @param string $target |
30 | 30 | * @return bool |
31 | 31 | */ |
32 | - protected function check($target) |
|
32 | + protected function check( $target ) |
|
33 | 33 | { |
34 | 34 | $blacklist = $this->getBlacklist(); |
35 | - if (empty($blacklist)) { |
|
35 | + if( empty($blacklist) ) { |
|
36 | 36 | return false; |
37 | 37 | } |
38 | - $lines = explode("\n", $blacklist); |
|
39 | - foreach ((array) $lines as $line) { |
|
40 | - $line = trim($line); |
|
41 | - if (empty($line) || 256 < strlen($line)) { |
|
38 | + $lines = explode( "\n", $blacklist ); |
|
39 | + foreach( (array)$lines as $line ) { |
|
40 | + $line = trim( $line ); |
|
41 | + if( empty($line) || 256 < strlen( $line ) ) { |
|
42 | 42 | continue; |
43 | 43 | } |
44 | - $pattern = sprintf('#%s#i', preg_quote($line, '#')); |
|
45 | - if (preg_match($pattern, $target)) { |
|
44 | + $pattern = sprintf( '#%s#i', preg_quote( $line, '#' ) ); |
|
45 | + if( preg_match( $pattern, $target ) ) { |
|
46 | 46 | return true; |
47 | 47 | } |
48 | 48 | } |
@@ -51,9 +51,9 @@ discard block |
||
51 | 51 | |
52 | 52 | protected function getBlacklist() |
53 | 53 | { |
54 | - $option = glsr(OptionManager::class)->get('settings.submissions.blacklist.integration'); |
|
54 | + $option = glsr( OptionManager::class )->get( 'settings.submissions.blacklist.integration' ); |
|
55 | 55 | return $option == 'comments' |
56 | - ? trim(glsr(OptionManager::class)->getWP('blacklist_keys')) |
|
57 | - : trim(glsr(OptionManager::class)->get('settings.submissions.blacklist.entries')); |
|
56 | + ? trim( glsr( OptionManager::class )->getWP( 'blacklist_keys' ) ) |
|
57 | + : trim( glsr( OptionManager::class )->get( 'settings.submissions.blacklist.entries' ) ); |
|
58 | 58 | } |
59 | 59 | } |
@@ -8,164 +8,164 @@ |
||
8 | 8 | |
9 | 9 | class Notification |
10 | 10 | { |
11 | - /** |
|
12 | - * @var bool |
|
13 | - */ |
|
14 | - protected $email; |
|
11 | + /** |
|
12 | + * @var bool |
|
13 | + */ |
|
14 | + protected $email; |
|
15 | 15 | |
16 | - /** |
|
17 | - * @var Review |
|
18 | - */ |
|
19 | - protected $review; |
|
16 | + /** |
|
17 | + * @var Review |
|
18 | + */ |
|
19 | + protected $review; |
|
20 | 20 | |
21 | - /** |
|
22 | - * @var bool |
|
23 | - */ |
|
24 | - protected $slack; |
|
21 | + /** |
|
22 | + * @var bool |
|
23 | + */ |
|
24 | + protected $slack; |
|
25 | 25 | |
26 | - /** |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - protected $types; |
|
26 | + /** |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + protected $types; |
|
30 | 30 | |
31 | - public function __construct() |
|
32 | - { |
|
33 | - $types = glsr(OptionManager::class)->get('settings.general.notifications', []); |
|
34 | - $this->email = count(array_intersect(['admin', 'custom'], $types)) > 0; |
|
35 | - $this->slack = in_array('slack', $types); |
|
36 | - $this->types = $types; |
|
37 | - } |
|
31 | + public function __construct() |
|
32 | + { |
|
33 | + $types = glsr(OptionManager::class)->get('settings.general.notifications', []); |
|
34 | + $this->email = count(array_intersect(['admin', 'custom'], $types)) > 0; |
|
35 | + $this->slack = in_array('slack', $types); |
|
36 | + $this->types = $types; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @return void |
|
41 | - */ |
|
42 | - public function send(Review $review) |
|
43 | - { |
|
44 | - if (empty($this->types)) { |
|
45 | - return; |
|
46 | - } |
|
47 | - $this->review = $review; |
|
48 | - $args = [ |
|
49 | - 'link' => $this->getLink(), |
|
50 | - 'title' => $this->getTitle(), |
|
51 | - ]; |
|
52 | - $this->sendToEmail($args); |
|
53 | - $this->sendToSlack($args); |
|
54 | - } |
|
39 | + /** |
|
40 | + * @return void |
|
41 | + */ |
|
42 | + public function send(Review $review) |
|
43 | + { |
|
44 | + if (empty($this->types)) { |
|
45 | + return; |
|
46 | + } |
|
47 | + $this->review = $review; |
|
48 | + $args = [ |
|
49 | + 'link' => $this->getLink(), |
|
50 | + 'title' => $this->getTitle(), |
|
51 | + ]; |
|
52 | + $this->sendToEmail($args); |
|
53 | + $this->sendToSlack($args); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @return Email |
|
58 | - */ |
|
59 | - protected function buildEmail(array $args) |
|
60 | - { |
|
61 | - return glsr(Email::class)->compose([ |
|
62 | - 'to' => $this->getEmailAddresses(), |
|
63 | - 'subject' => $args['title'], |
|
64 | - 'template' => 'email-notification', |
|
65 | - 'template-tags' => [ |
|
66 | - 'review_author' => $this->review->author ?: __('Anonymous', 'site-reviews'), |
|
67 | - 'review_content' => $this->review->content, |
|
68 | - 'review_email' => $this->review->email, |
|
69 | - 'review_ip' => $this->review->ip_address, |
|
70 | - 'review_link' => sprintf('<a href="%1$s">%1$s</a>', $args['link']), |
|
71 | - 'review_rating' => $this->review->rating, |
|
72 | - 'review_title' => $this->review->title, |
|
73 | - ], |
|
74 | - ]); |
|
75 | - } |
|
56 | + /** |
|
57 | + * @return Email |
|
58 | + */ |
|
59 | + protected function buildEmail(array $args) |
|
60 | + { |
|
61 | + return glsr(Email::class)->compose([ |
|
62 | + 'to' => $this->getEmailAddresses(), |
|
63 | + 'subject' => $args['title'], |
|
64 | + 'template' => 'email-notification', |
|
65 | + 'template-tags' => [ |
|
66 | + 'review_author' => $this->review->author ?: __('Anonymous', 'site-reviews'), |
|
67 | + 'review_content' => $this->review->content, |
|
68 | + 'review_email' => $this->review->email, |
|
69 | + 'review_ip' => $this->review->ip_address, |
|
70 | + 'review_link' => sprintf('<a href="%1$s">%1$s</a>', $args['link']), |
|
71 | + 'review_rating' => $this->review->rating, |
|
72 | + 'review_title' => $this->review->title, |
|
73 | + ], |
|
74 | + ]); |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * @return Slack |
|
79 | - */ |
|
80 | - protected function buildSlackNotification(array $args) |
|
81 | - { |
|
82 | - return glsr(Slack::class)->compose($this->review, [ |
|
83 | - 'button_url' => $args['link'], |
|
84 | - 'fallback' => $this->buildEmail($args)->read('plaintext'), |
|
85 | - 'pretext' => $args['title'], |
|
86 | - ]); |
|
87 | - } |
|
77 | + /** |
|
78 | + * @return Slack |
|
79 | + */ |
|
80 | + protected function buildSlackNotification(array $args) |
|
81 | + { |
|
82 | + return glsr(Slack::class)->compose($this->review, [ |
|
83 | + 'button_url' => $args['link'], |
|
84 | + 'fallback' => $this->buildEmail($args)->read('plaintext'), |
|
85 | + 'pretext' => $args['title'], |
|
86 | + ]); |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * @return array |
|
91 | - */ |
|
92 | - protected function getEmailAddresses() |
|
93 | - { |
|
94 | - $emails = []; |
|
95 | - if (in_array('admin', $this->types)) { |
|
96 | - $emails[] = glsr(OptionManager::class)->getWP('admin_email'); |
|
97 | - } |
|
98 | - if (in_array('author', $this->types)) { |
|
99 | - $assignedPost = get_post(intval($this->review->assigned_to)); |
|
100 | - if ($assignedPost instanceof WP_Post) { |
|
101 | - $this->email = true; |
|
102 | - $emails[] = get_the_author_meta('user_email', intval($assignedPost->post_author)); |
|
103 | - } |
|
104 | - } |
|
105 | - if (in_array('custom', $this->types)) { |
|
106 | - $customEmails = glsr(OptionManager::class)->get('settings.general.notification_email'); |
|
107 | - $customEmails = str_replace([' ', ',', ';'], ',', $customEmails); |
|
108 | - $customEmails = explode(',', $customEmails); |
|
109 | - $emails = array_merge($emails, $customEmails); |
|
110 | - } |
|
111 | - $emails = array_filter(array_keys(array_flip($emails))); |
|
112 | - return apply_filters('site-reviews/notification/emails', $emails, $this->review); |
|
113 | - } |
|
89 | + /** |
|
90 | + * @return array |
|
91 | + */ |
|
92 | + protected function getEmailAddresses() |
|
93 | + { |
|
94 | + $emails = []; |
|
95 | + if (in_array('admin', $this->types)) { |
|
96 | + $emails[] = glsr(OptionManager::class)->getWP('admin_email'); |
|
97 | + } |
|
98 | + if (in_array('author', $this->types)) { |
|
99 | + $assignedPost = get_post(intval($this->review->assigned_to)); |
|
100 | + if ($assignedPost instanceof WP_Post) { |
|
101 | + $this->email = true; |
|
102 | + $emails[] = get_the_author_meta('user_email', intval($assignedPost->post_author)); |
|
103 | + } |
|
104 | + } |
|
105 | + if (in_array('custom', $this->types)) { |
|
106 | + $customEmails = glsr(OptionManager::class)->get('settings.general.notification_email'); |
|
107 | + $customEmails = str_replace([' ', ',', ';'], ',', $customEmails); |
|
108 | + $customEmails = explode(',', $customEmails); |
|
109 | + $emails = array_merge($emails, $customEmails); |
|
110 | + } |
|
111 | + $emails = array_filter(array_keys(array_flip($emails))); |
|
112 | + return apply_filters('site-reviews/notification/emails', $emails, $this->review); |
|
113 | + } |
|
114 | 114 | |
115 | - /** |
|
116 | - * @return string |
|
117 | - */ |
|
118 | - protected function getLink() |
|
119 | - { |
|
120 | - return admin_url('post.php?post='.$this->review->ID.'&action=edit'); |
|
121 | - } |
|
115 | + /** |
|
116 | + * @return string |
|
117 | + */ |
|
118 | + protected function getLink() |
|
119 | + { |
|
120 | + return admin_url('post.php?post='.$this->review->ID.'&action=edit'); |
|
121 | + } |
|
122 | 122 | |
123 | - /** |
|
124 | - * @return string |
|
125 | - */ |
|
126 | - protected function getTitle() |
|
127 | - { |
|
128 | - $assignedTitle = get_the_title(intval($this->review->assigned_to)); |
|
129 | - $title = _nx( |
|
130 | - 'New %s-star review', |
|
131 | - 'New %s-star review of: %s', |
|
132 | - intval(empty($assignedTitle)), |
|
133 | - 'This string differs depending on whether or not the review has been assigned to a post.', |
|
134 | - 'site-reviews' |
|
135 | - ); |
|
136 | - $title = sprintf('[%s] %s', |
|
137 | - wp_specialchars_decode(glsr(OptionManager::class)->getWP('blogname'), ENT_QUOTES), |
|
138 | - sprintf($title, $this->review->rating, $assignedTitle) |
|
139 | - ); |
|
140 | - return apply_filters('site-reviews/notification/title', $title, $this->review); |
|
141 | - } |
|
123 | + /** |
|
124 | + * @return string |
|
125 | + */ |
|
126 | + protected function getTitle() |
|
127 | + { |
|
128 | + $assignedTitle = get_the_title(intval($this->review->assigned_to)); |
|
129 | + $title = _nx( |
|
130 | + 'New %s-star review', |
|
131 | + 'New %s-star review of: %s', |
|
132 | + intval(empty($assignedTitle)), |
|
133 | + 'This string differs depending on whether or not the review has been assigned to a post.', |
|
134 | + 'site-reviews' |
|
135 | + ); |
|
136 | + $title = sprintf('[%s] %s', |
|
137 | + wp_specialchars_decode(glsr(OptionManager::class)->getWP('blogname'), ENT_QUOTES), |
|
138 | + sprintf($title, $this->review->rating, $assignedTitle) |
|
139 | + ); |
|
140 | + return apply_filters('site-reviews/notification/title', $title, $this->review); |
|
141 | + } |
|
142 | 142 | |
143 | - /** |
|
144 | - * @return void |
|
145 | - */ |
|
146 | - protected function sendToEmail(array $args) |
|
147 | - { |
|
148 | - $email = $this->buildEmail($args); |
|
149 | - if (empty($email->to)) { |
|
150 | - glsr_log()->error('Email notification was not sent (missing email address)'); |
|
151 | - return; |
|
152 | - } |
|
153 | - $email->send(); |
|
154 | - } |
|
143 | + /** |
|
144 | + * @return void |
|
145 | + */ |
|
146 | + protected function sendToEmail(array $args) |
|
147 | + { |
|
148 | + $email = $this->buildEmail($args); |
|
149 | + if (empty($email->to)) { |
|
150 | + glsr_log()->error('Email notification was not sent (missing email address)'); |
|
151 | + return; |
|
152 | + } |
|
153 | + $email->send(); |
|
154 | + } |
|
155 | 155 | |
156 | - /** |
|
157 | - * @return void |
|
158 | - */ |
|
159 | - protected function sendToSlack(array $args) |
|
160 | - { |
|
161 | - if (!$this->slack) { |
|
162 | - return; |
|
163 | - } |
|
164 | - $notification = $this->buildSlackNotification($args); |
|
165 | - $result = $notification->send(); |
|
166 | - if (is_wp_error($result)) { |
|
167 | - $notification->review = null; |
|
168 | - glsr_log()->error($result->get_error_message())->debug($notification); |
|
169 | - } |
|
170 | - } |
|
156 | + /** |
|
157 | + * @return void |
|
158 | + */ |
|
159 | + protected function sendToSlack(array $args) |
|
160 | + { |
|
161 | + if (!$this->slack) { |
|
162 | + return; |
|
163 | + } |
|
164 | + $notification = $this->buildSlackNotification($args); |
|
165 | + $result = $notification->send(); |
|
166 | + if (is_wp_error($result)) { |
|
167 | + $notification->review = null; |
|
168 | + glsr_log()->error($result->get_error_message())->debug($notification); |
|
169 | + } |
|
170 | + } |
|
171 | 171 | } |
@@ -30,18 +30,18 @@ discard block |
||
30 | 30 | |
31 | 31 | public function __construct() |
32 | 32 | { |
33 | - $types = glsr(OptionManager::class)->get('settings.general.notifications', []); |
|
34 | - $this->email = count(array_intersect(['admin', 'custom'], $types)) > 0; |
|
35 | - $this->slack = in_array('slack', $types); |
|
33 | + $types = glsr( OptionManager::class )->get( 'settings.general.notifications', [] ); |
|
34 | + $this->email = count( array_intersect( ['admin', 'custom'], $types ) ) > 0; |
|
35 | + $this->slack = in_array( 'slack', $types ); |
|
36 | 36 | $this->types = $types; |
37 | 37 | } |
38 | 38 | |
39 | 39 | /** |
40 | 40 | * @return void |
41 | 41 | */ |
42 | - public function send(Review $review) |
|
42 | + public function send( Review $review ) |
|
43 | 43 | { |
44 | - if (empty($this->types)) { |
|
44 | + if( empty($this->types) ) { |
|
45 | 45 | return; |
46 | 46 | } |
47 | 47 | $this->review = $review; |
@@ -49,41 +49,41 @@ discard block |
||
49 | 49 | 'link' => $this->getLink(), |
50 | 50 | 'title' => $this->getTitle(), |
51 | 51 | ]; |
52 | - $this->sendToEmail($args); |
|
53 | - $this->sendToSlack($args); |
|
52 | + $this->sendToEmail( $args ); |
|
53 | + $this->sendToSlack( $args ); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | /** |
57 | 57 | * @return Email |
58 | 58 | */ |
59 | - protected function buildEmail(array $args) |
|
59 | + protected function buildEmail( array $args ) |
|
60 | 60 | { |
61 | - return glsr(Email::class)->compose([ |
|
61 | + return glsr( Email::class )->compose( [ |
|
62 | 62 | 'to' => $this->getEmailAddresses(), |
63 | 63 | 'subject' => $args['title'], |
64 | 64 | 'template' => 'email-notification', |
65 | 65 | 'template-tags' => [ |
66 | - 'review_author' => $this->review->author ?: __('Anonymous', 'site-reviews'), |
|
66 | + 'review_author' => $this->review->author ?: __( 'Anonymous', 'site-reviews' ), |
|
67 | 67 | 'review_content' => $this->review->content, |
68 | 68 | 'review_email' => $this->review->email, |
69 | 69 | 'review_ip' => $this->review->ip_address, |
70 | - 'review_link' => sprintf('<a href="%1$s">%1$s</a>', $args['link']), |
|
70 | + 'review_link' => sprintf( '<a href="%1$s">%1$s</a>', $args['link'] ), |
|
71 | 71 | 'review_rating' => $this->review->rating, |
72 | 72 | 'review_title' => $this->review->title, |
73 | 73 | ], |
74 | - ]); |
|
74 | + ] ); |
|
75 | 75 | } |
76 | 76 | |
77 | 77 | /** |
78 | 78 | * @return Slack |
79 | 79 | */ |
80 | - protected function buildSlackNotification(array $args) |
|
80 | + protected function buildSlackNotification( array $args ) |
|
81 | 81 | { |
82 | - return glsr(Slack::class)->compose($this->review, [ |
|
82 | + return glsr( Slack::class )->compose( $this->review, [ |
|
83 | 83 | 'button_url' => $args['link'], |
84 | - 'fallback' => $this->buildEmail($args)->read('plaintext'), |
|
84 | + 'fallback' => $this->buildEmail( $args )->read( 'plaintext' ), |
|
85 | 85 | 'pretext' => $args['title'], |
86 | - ]); |
|
86 | + ] ); |
|
87 | 87 | } |
88 | 88 | |
89 | 89 | /** |
@@ -92,24 +92,24 @@ discard block |
||
92 | 92 | protected function getEmailAddresses() |
93 | 93 | { |
94 | 94 | $emails = []; |
95 | - if (in_array('admin', $this->types)) { |
|
96 | - $emails[] = glsr(OptionManager::class)->getWP('admin_email'); |
|
95 | + if( in_array( 'admin', $this->types ) ) { |
|
96 | + $emails[] = glsr( OptionManager::class )->getWP( 'admin_email' ); |
|
97 | 97 | } |
98 | - if (in_array('author', $this->types)) { |
|
99 | - $assignedPost = get_post(intval($this->review->assigned_to)); |
|
100 | - if ($assignedPost instanceof WP_Post) { |
|
98 | + if( in_array( 'author', $this->types ) ) { |
|
99 | + $assignedPost = get_post( intval( $this->review->assigned_to ) ); |
|
100 | + if( $assignedPost instanceof WP_Post ) { |
|
101 | 101 | $this->email = true; |
102 | - $emails[] = get_the_author_meta('user_email', intval($assignedPost->post_author)); |
|
102 | + $emails[] = get_the_author_meta( 'user_email', intval( $assignedPost->post_author ) ); |
|
103 | 103 | } |
104 | 104 | } |
105 | - if (in_array('custom', $this->types)) { |
|
106 | - $customEmails = glsr(OptionManager::class)->get('settings.general.notification_email'); |
|
107 | - $customEmails = str_replace([' ', ',', ';'], ',', $customEmails); |
|
108 | - $customEmails = explode(',', $customEmails); |
|
109 | - $emails = array_merge($emails, $customEmails); |
|
105 | + if( in_array( 'custom', $this->types ) ) { |
|
106 | + $customEmails = glsr( OptionManager::class )->get( 'settings.general.notification_email' ); |
|
107 | + $customEmails = str_replace( [' ', ',', ';'], ',', $customEmails ); |
|
108 | + $customEmails = explode( ',', $customEmails ); |
|
109 | + $emails = array_merge( $emails, $customEmails ); |
|
110 | 110 | } |
111 | - $emails = array_filter(array_keys(array_flip($emails))); |
|
112 | - return apply_filters('site-reviews/notification/emails', $emails, $this->review); |
|
111 | + $emails = array_filter( array_keys( array_flip( $emails ) ) ); |
|
112 | + return apply_filters( 'site-reviews/notification/emails', $emails, $this->review ); |
|
113 | 113 | } |
114 | 114 | |
115 | 115 | /** |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | */ |
118 | 118 | protected function getLink() |
119 | 119 | { |
120 | - return admin_url('post.php?post='.$this->review->ID.'&action=edit'); |
|
120 | + return admin_url( 'post.php?post='.$this->review->ID.'&action=edit' ); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | /** |
@@ -125,29 +125,29 @@ discard block |
||
125 | 125 | */ |
126 | 126 | protected function getTitle() |
127 | 127 | { |
128 | - $assignedTitle = get_the_title(intval($this->review->assigned_to)); |
|
128 | + $assignedTitle = get_the_title( intval( $this->review->assigned_to ) ); |
|
129 | 129 | $title = _nx( |
130 | 130 | 'New %s-star review', |
131 | 131 | 'New %s-star review of: %s', |
132 | - intval(empty($assignedTitle)), |
|
132 | + intval( empty($assignedTitle) ), |
|
133 | 133 | 'This string differs depending on whether or not the review has been assigned to a post.', |
134 | 134 | 'site-reviews' |
135 | 135 | ); |
136 | - $title = sprintf('[%s] %s', |
|
137 | - wp_specialchars_decode(glsr(OptionManager::class)->getWP('blogname'), ENT_QUOTES), |
|
138 | - sprintf($title, $this->review->rating, $assignedTitle) |
|
136 | + $title = sprintf( '[%s] %s', |
|
137 | + wp_specialchars_decode( glsr( OptionManager::class )->getWP( 'blogname' ), ENT_QUOTES ), |
|
138 | + sprintf( $title, $this->review->rating, $assignedTitle ) |
|
139 | 139 | ); |
140 | - return apply_filters('site-reviews/notification/title', $title, $this->review); |
|
140 | + return apply_filters( 'site-reviews/notification/title', $title, $this->review ); |
|
141 | 141 | } |
142 | 142 | |
143 | 143 | /** |
144 | 144 | * @return void |
145 | 145 | */ |
146 | - protected function sendToEmail(array $args) |
|
146 | + protected function sendToEmail( array $args ) |
|
147 | 147 | { |
148 | - $email = $this->buildEmail($args); |
|
149 | - if (empty($email->to)) { |
|
150 | - glsr_log()->error('Email notification was not sent (missing email address)'); |
|
148 | + $email = $this->buildEmail( $args ); |
|
149 | + if( empty($email->to) ) { |
|
150 | + glsr_log()->error( 'Email notification was not sent (missing email address)' ); |
|
151 | 151 | return; |
152 | 152 | } |
153 | 153 | $email->send(); |
@@ -156,16 +156,16 @@ discard block |
||
156 | 156 | /** |
157 | 157 | * @return void |
158 | 158 | */ |
159 | - protected function sendToSlack(array $args) |
|
159 | + protected function sendToSlack( array $args ) |
|
160 | 160 | { |
161 | - if (!$this->slack) { |
|
161 | + if( !$this->slack ) { |
|
162 | 162 | return; |
163 | 163 | } |
164 | - $notification = $this->buildSlackNotification($args); |
|
164 | + $notification = $this->buildSlackNotification( $args ); |
|
165 | 165 | $result = $notification->send(); |
166 | - if (is_wp_error($result)) { |
|
166 | + if( is_wp_error( $result ) ) { |
|
167 | 167 | $notification->review = null; |
168 | - glsr_log()->error($result->get_error_message())->debug($notification); |
|
168 | + glsr_log()->error( $result->get_error_message() )->debug( $notification ); |
|
169 | 169 | } |
170 | 170 | } |
171 | 171 | } |
@@ -7,84 +7,84 @@ |
||
7 | 7 | |
8 | 8 | class Customization |
9 | 9 | { |
10 | - /** |
|
11 | - * @return array |
|
12 | - */ |
|
13 | - public function filterEditorSettings(array $settings) |
|
14 | - { |
|
15 | - if ($this->isReviewEditable()) { |
|
16 | - $settings = [ |
|
17 | - 'media_buttons' => false, |
|
18 | - 'quicktags' => false, |
|
19 | - 'textarea_rows' => 12, |
|
20 | - 'tinymce' => false, |
|
21 | - ]; |
|
22 | - } |
|
23 | - return $settings; |
|
24 | - } |
|
10 | + /** |
|
11 | + * @return array |
|
12 | + */ |
|
13 | + public function filterEditorSettings(array $settings) |
|
14 | + { |
|
15 | + if ($this->isReviewEditable()) { |
|
16 | + $settings = [ |
|
17 | + 'media_buttons' => false, |
|
18 | + 'quicktags' => false, |
|
19 | + 'textarea_rows' => 12, |
|
20 | + 'tinymce' => false, |
|
21 | + ]; |
|
22 | + } |
|
23 | + return $settings; |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * @param string $html |
|
28 | - * @return string |
|
29 | - */ |
|
30 | - public function filterEditorTextarea($html) |
|
31 | - { |
|
32 | - if ($this->isReviewEditable()) { |
|
33 | - $html = str_replace('<textarea', '<div id="ed_toolbar"></div><textarea', $html); |
|
34 | - } |
|
35 | - return $html; |
|
36 | - } |
|
26 | + /** |
|
27 | + * @param string $html |
|
28 | + * @return string |
|
29 | + */ |
|
30 | + public function filterEditorTextarea($html) |
|
31 | + { |
|
32 | + if ($this->isReviewEditable()) { |
|
33 | + $html = str_replace('<textarea', '<div id="ed_toolbar"></div><textarea', $html); |
|
34 | + } |
|
35 | + return $html; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * @return void |
|
40 | - */ |
|
41 | - public function removeAutosave() |
|
42 | - { |
|
43 | - if (!$this->isReviewEditor() || $this->isReviewEditable()) { |
|
44 | - return; |
|
45 | - } |
|
46 | - wp_deregister_script('autosave'); |
|
47 | - } |
|
38 | + /** |
|
39 | + * @return void |
|
40 | + */ |
|
41 | + public function removeAutosave() |
|
42 | + { |
|
43 | + if (!$this->isReviewEditor() || $this->isReviewEditable()) { |
|
44 | + return; |
|
45 | + } |
|
46 | + wp_deregister_script('autosave'); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @return void |
|
51 | - */ |
|
52 | - public function removeMetaBoxes() |
|
53 | - { |
|
54 | - remove_meta_box('slugdiv', Application::POST_TYPE, 'advanced'); |
|
55 | - } |
|
49 | + /** |
|
50 | + * @return void |
|
51 | + */ |
|
52 | + public function removeMetaBoxes() |
|
53 | + { |
|
54 | + remove_meta_box('slugdiv', Application::POST_TYPE, 'advanced'); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @return void |
|
59 | - */ |
|
60 | - public function removePostTypeSupport() |
|
61 | - { |
|
62 | - if (!$this->isReviewEditor() || $this->isReviewEditable()) { |
|
63 | - return; |
|
64 | - } |
|
65 | - remove_post_type_support(Application::POST_TYPE, 'title'); |
|
66 | - remove_post_type_support(Application::POST_TYPE, 'editor'); |
|
67 | - } |
|
57 | + /** |
|
58 | + * @return void |
|
59 | + */ |
|
60 | + public function removePostTypeSupport() |
|
61 | + { |
|
62 | + if (!$this->isReviewEditor() || $this->isReviewEditable()) { |
|
63 | + return; |
|
64 | + } |
|
65 | + remove_post_type_support(Application::POST_TYPE, 'title'); |
|
66 | + remove_post_type_support(Application::POST_TYPE, 'editor'); |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * @return bool |
|
71 | - */ |
|
72 | - protected function isReviewEditable() |
|
73 | - { |
|
74 | - $postId = intval(filter_input(INPUT_GET, 'post')); |
|
75 | - return $postId > 0 |
|
76 | - && 'local' == glsr(Database::class)->get($postId, 'review_type') |
|
77 | - && $this->isReviewEditor(); |
|
78 | - } |
|
69 | + /** |
|
70 | + * @return bool |
|
71 | + */ |
|
72 | + protected function isReviewEditable() |
|
73 | + { |
|
74 | + $postId = intval(filter_input(INPUT_GET, 'post')); |
|
75 | + return $postId > 0 |
|
76 | + && 'local' == glsr(Database::class)->get($postId, 'review_type') |
|
77 | + && $this->isReviewEditor(); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * @return bool |
|
82 | - */ |
|
83 | - protected function isReviewEditor() |
|
84 | - { |
|
85 | - $screen = glsr_current_screen(); |
|
86 | - return 'post' == $screen->base |
|
87 | - && Application::POST_TYPE == $screen->id |
|
88 | - && Application::POST_TYPE == $screen->post_type; |
|
89 | - } |
|
80 | + /** |
|
81 | + * @return bool |
|
82 | + */ |
|
83 | + protected function isReviewEditor() |
|
84 | + { |
|
85 | + $screen = glsr_current_screen(); |
|
86 | + return 'post' == $screen->base |
|
87 | + && Application::POST_TYPE == $screen->id |
|
88 | + && Application::POST_TYPE == $screen->post_type; |
|
89 | + } |
|
90 | 90 | } |
@@ -10,9 +10,9 @@ discard block |
||
10 | 10 | /** |
11 | 11 | * @return array |
12 | 12 | */ |
13 | - public function filterEditorSettings(array $settings) |
|
13 | + public function filterEditorSettings( array $settings ) |
|
14 | 14 | { |
15 | - if ($this->isReviewEditable()) { |
|
15 | + if( $this->isReviewEditable() ) { |
|
16 | 16 | $settings = [ |
17 | 17 | 'media_buttons' => false, |
18 | 18 | 'quicktags' => false, |
@@ -27,10 +27,10 @@ discard block |
||
27 | 27 | * @param string $html |
28 | 28 | * @return string |
29 | 29 | */ |
30 | - public function filterEditorTextarea($html) |
|
30 | + public function filterEditorTextarea( $html ) |
|
31 | 31 | { |
32 | - if ($this->isReviewEditable()) { |
|
33 | - $html = str_replace('<textarea', '<div id="ed_toolbar"></div><textarea', $html); |
|
32 | + if( $this->isReviewEditable() ) { |
|
33 | + $html = str_replace( '<textarea', '<div id="ed_toolbar"></div><textarea', $html ); |
|
34 | 34 | } |
35 | 35 | return $html; |
36 | 36 | } |
@@ -40,10 +40,10 @@ discard block |
||
40 | 40 | */ |
41 | 41 | public function removeAutosave() |
42 | 42 | { |
43 | - if (!$this->isReviewEditor() || $this->isReviewEditable()) { |
|
43 | + if( !$this->isReviewEditor() || $this->isReviewEditable() ) { |
|
44 | 44 | return; |
45 | 45 | } |
46 | - wp_deregister_script('autosave'); |
|
46 | + wp_deregister_script( 'autosave' ); |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | /** |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | */ |
52 | 52 | public function removeMetaBoxes() |
53 | 53 | { |
54 | - remove_meta_box('slugdiv', Application::POST_TYPE, 'advanced'); |
|
54 | + remove_meta_box( 'slugdiv', Application::POST_TYPE, 'advanced' ); |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | /** |
@@ -59,11 +59,11 @@ discard block |
||
59 | 59 | */ |
60 | 60 | public function removePostTypeSupport() |
61 | 61 | { |
62 | - if (!$this->isReviewEditor() || $this->isReviewEditable()) { |
|
62 | + if( !$this->isReviewEditor() || $this->isReviewEditable() ) { |
|
63 | 63 | return; |
64 | 64 | } |
65 | - remove_post_type_support(Application::POST_TYPE, 'title'); |
|
66 | - remove_post_type_support(Application::POST_TYPE, 'editor'); |
|
65 | + remove_post_type_support( Application::POST_TYPE, 'title' ); |
|
66 | + remove_post_type_support( Application::POST_TYPE, 'editor' ); |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | /** |
@@ -71,9 +71,9 @@ discard block |
||
71 | 71 | */ |
72 | 72 | protected function isReviewEditable() |
73 | 73 | { |
74 | - $postId = intval(filter_input(INPUT_GET, 'post')); |
|
74 | + $postId = intval( filter_input( INPUT_GET, 'post' ) ); |
|
75 | 75 | return $postId > 0 |
76 | - && 'local' == glsr(Database::class)->get($postId, 'review_type') |
|
76 | + && 'local' == glsr( Database::class )->get( $postId, 'review_type' ) |
|
77 | 77 | && $this->isReviewEditor(); |
78 | 78 | } |
79 | 79 |
@@ -108,7 +108,8 @@ |
||
108 | 108 | constant($integrationClass.'::PLUGIN_NAME') |
109 | 109 | )); |
110 | 110 | return false; |
111 | - } elseif (!glsr($integrationClass)->isSupported()) { |
|
111 | + } |
|
112 | + elseif (!glsr($integrationClass)->isSupported()) { |
|
112 | 113 | glsr(Notice::class)->addError(sprintf( |
113 | 114 | __('Please update the %s plugin to v%s or greater to enable integration.', 'site-reviews'), |
114 | 115 | constant($integrationClass.'::PLUGIN_NAME'), |
@@ -9,120 +9,120 @@ |
||
9 | 9 | |
10 | 10 | class SettingsController extends Controller |
11 | 11 | { |
12 | - /** |
|
13 | - * @param mixed $input |
|
14 | - * @return array |
|
15 | - * @callback register_setting |
|
16 | - */ |
|
17 | - public function callbackRegisterSettings($input) |
|
18 | - { |
|
19 | - $settings = Arr::consolidateArray($input); |
|
20 | - if (1 === count($settings) && array_key_exists('settings', $settings)) { |
|
21 | - $options = array_replace_recursive(glsr(OptionManager::class)->all(), $input); |
|
22 | - $options = $this->sanitizeGeneral($input, $options); |
|
23 | - $options = $this->sanitizeSubmissions($input, $options); |
|
24 | - $options = $this->sanitizeTranslations($input, $options); |
|
25 | - $options = apply_filters('site-reviews/settings/callback', $options, $settings); |
|
26 | - if (filter_input(INPUT_POST, 'option_page') == Application::ID.'-settings') { |
|
27 | - glsr(Notice::class)->addSuccess(__('Settings updated.', 'site-reviews')); |
|
28 | - } |
|
29 | - return $options; |
|
30 | - } |
|
31 | - return $input; |
|
32 | - } |
|
12 | + /** |
|
13 | + * @param mixed $input |
|
14 | + * @return array |
|
15 | + * @callback register_setting |
|
16 | + */ |
|
17 | + public function callbackRegisterSettings($input) |
|
18 | + { |
|
19 | + $settings = Arr::consolidateArray($input); |
|
20 | + if (1 === count($settings) && array_key_exists('settings', $settings)) { |
|
21 | + $options = array_replace_recursive(glsr(OptionManager::class)->all(), $input); |
|
22 | + $options = $this->sanitizeGeneral($input, $options); |
|
23 | + $options = $this->sanitizeSubmissions($input, $options); |
|
24 | + $options = $this->sanitizeTranslations($input, $options); |
|
25 | + $options = apply_filters('site-reviews/settings/callback', $options, $settings); |
|
26 | + if (filter_input(INPUT_POST, 'option_page') == Application::ID.'-settings') { |
|
27 | + glsr(Notice::class)->addSuccess(__('Settings updated.', 'site-reviews')); |
|
28 | + } |
|
29 | + return $options; |
|
30 | + } |
|
31 | + return $input; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * @return void |
|
36 | - * @action admin_init |
|
37 | - */ |
|
38 | - public function registerSettings() |
|
39 | - { |
|
40 | - register_setting(Application::ID.'-settings', OptionManager::databaseKey(), [ |
|
41 | - 'sanitize_callback' => [$this, 'callbackRegisterSettings'], |
|
42 | - ]); |
|
43 | - } |
|
34 | + /** |
|
35 | + * @return void |
|
36 | + * @action admin_init |
|
37 | + */ |
|
38 | + public function registerSettings() |
|
39 | + { |
|
40 | + register_setting(Application::ID.'-settings', OptionManager::databaseKey(), [ |
|
41 | + 'sanitize_callback' => [$this, 'callbackRegisterSettings'], |
|
42 | + ]); |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @return array |
|
47 | - */ |
|
48 | - protected function sanitizeGeneral(array $input, array $options) |
|
49 | - { |
|
50 | - $key = 'settings.general'; |
|
51 | - $inputForm = Arr::get($input, $key); |
|
52 | - if (!$this->hasMultilingualIntegration(Arr::get($inputForm, 'multilingual'))) { |
|
53 | - $options = Arr::set($options, $key.'.multilingual', ''); |
|
54 | - } |
|
55 | - if ('' == trim(Arr::get($inputForm, 'notification_message'))) { |
|
56 | - $defaultValue = Arr::get(glsr()->defaults, $key.'.notification_message'); |
|
57 | - $options = Arr::set($options, $key.'.notification_message', $defaultValue); |
|
58 | - } |
|
59 | - $defaultValue = Arr::get($inputForm, 'notifications', []); |
|
60 | - $options = Arr::set($options, $key.'.notifications', $defaultValue); |
|
61 | - return $options; |
|
62 | - } |
|
45 | + /** |
|
46 | + * @return array |
|
47 | + */ |
|
48 | + protected function sanitizeGeneral(array $input, array $options) |
|
49 | + { |
|
50 | + $key = 'settings.general'; |
|
51 | + $inputForm = Arr::get($input, $key); |
|
52 | + if (!$this->hasMultilingualIntegration(Arr::get($inputForm, 'multilingual'))) { |
|
53 | + $options = Arr::set($options, $key.'.multilingual', ''); |
|
54 | + } |
|
55 | + if ('' == trim(Arr::get($inputForm, 'notification_message'))) { |
|
56 | + $defaultValue = Arr::get(glsr()->defaults, $key.'.notification_message'); |
|
57 | + $options = Arr::set($options, $key.'.notification_message', $defaultValue); |
|
58 | + } |
|
59 | + $defaultValue = Arr::get($inputForm, 'notifications', []); |
|
60 | + $options = Arr::set($options, $key.'.notifications', $defaultValue); |
|
61 | + return $options; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @return array |
|
66 | - */ |
|
67 | - protected function sanitizeSubmissions(array $input, array $options) |
|
68 | - { |
|
69 | - $key = 'settings.submissions'; |
|
70 | - $inputForm = Arr::get($input, $key); |
|
71 | - $defaultValue = isset($inputForm['required']) |
|
72 | - ? $inputForm['required'] |
|
73 | - : []; |
|
74 | - $options = Arr::set($options, $key.'.required', $defaultValue); |
|
75 | - return $options; |
|
76 | - } |
|
64 | + /** |
|
65 | + * @return array |
|
66 | + */ |
|
67 | + protected function sanitizeSubmissions(array $input, array $options) |
|
68 | + { |
|
69 | + $key = 'settings.submissions'; |
|
70 | + $inputForm = Arr::get($input, $key); |
|
71 | + $defaultValue = isset($inputForm['required']) |
|
72 | + ? $inputForm['required'] |
|
73 | + : []; |
|
74 | + $options = Arr::set($options, $key.'.required', $defaultValue); |
|
75 | + return $options; |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @return array |
|
80 | - */ |
|
81 | - protected function sanitizeTranslations(array $input, array $options) |
|
82 | - { |
|
83 | - $key = 'settings.strings'; |
|
84 | - $inputForm = Arr::consolidateArray(Arr::get($input, $key)); |
|
85 | - if (!empty($inputForm)) { |
|
86 | - $options = Arr::set($options, $key, array_values(array_filter($inputForm))); |
|
87 | - $allowedTags = [ |
|
88 | - 'a' => ['class' => [], 'href' => [], 'target' => []], |
|
89 | - 'span' => ['class' => []], |
|
90 | - ]; |
|
91 | - array_walk($options['settings']['strings'], function (&$string) use ($allowedTags) { |
|
92 | - if (isset($string['s2'])) { |
|
93 | - $string['s2'] = wp_kses($string['s2'], $allowedTags); |
|
94 | - } |
|
95 | - if (isset($string['p2'])) { |
|
96 | - $string['p2'] = wp_kses($string['p2'], $allowedTags); |
|
97 | - } |
|
98 | - }); |
|
99 | - } |
|
100 | - return $options; |
|
101 | - } |
|
78 | + /** |
|
79 | + * @return array |
|
80 | + */ |
|
81 | + protected function sanitizeTranslations(array $input, array $options) |
|
82 | + { |
|
83 | + $key = 'settings.strings'; |
|
84 | + $inputForm = Arr::consolidateArray(Arr::get($input, $key)); |
|
85 | + if (!empty($inputForm)) { |
|
86 | + $options = Arr::set($options, $key, array_values(array_filter($inputForm))); |
|
87 | + $allowedTags = [ |
|
88 | + 'a' => ['class' => [], 'href' => [], 'target' => []], |
|
89 | + 'span' => ['class' => []], |
|
90 | + ]; |
|
91 | + array_walk($options['settings']['strings'], function (&$string) use ($allowedTags) { |
|
92 | + if (isset($string['s2'])) { |
|
93 | + $string['s2'] = wp_kses($string['s2'], $allowedTags); |
|
94 | + } |
|
95 | + if (isset($string['p2'])) { |
|
96 | + $string['p2'] = wp_kses($string['p2'], $allowedTags); |
|
97 | + } |
|
98 | + }); |
|
99 | + } |
|
100 | + return $options; |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * @return bool |
|
105 | - */ |
|
106 | - protected function hasMultilingualIntegration($integration) |
|
107 | - { |
|
108 | - if (!in_array($integration, ['polylang', 'wpml'])) { |
|
109 | - return false; |
|
110 | - } |
|
111 | - $integrationClass = 'GeminiLabs\SiteReviews\Modules\\'.ucfirst($integration); |
|
112 | - if (!glsr($integrationClass)->isActive()) { |
|
113 | - glsr(Notice::class)->addError(sprintf( |
|
114 | - __('Please install/activate the %s plugin to enable integration.', 'site-reviews'), |
|
115 | - constant($integrationClass.'::PLUGIN_NAME') |
|
116 | - )); |
|
117 | - return false; |
|
118 | - } elseif (!glsr($integrationClass)->isSupported()) { |
|
119 | - glsr(Notice::class)->addError(sprintf( |
|
120 | - __('Please update the %s plugin to v%s or greater to enable integration.', 'site-reviews'), |
|
121 | - constant($integrationClass.'::PLUGIN_NAME'), |
|
122 | - constant($integrationClass.'::SUPPORTED_VERSION') |
|
123 | - )); |
|
124 | - return false; |
|
125 | - } |
|
126 | - return true; |
|
127 | - } |
|
103 | + /** |
|
104 | + * @return bool |
|
105 | + */ |
|
106 | + protected function hasMultilingualIntegration($integration) |
|
107 | + { |
|
108 | + if (!in_array($integration, ['polylang', 'wpml'])) { |
|
109 | + return false; |
|
110 | + } |
|
111 | + $integrationClass = 'GeminiLabs\SiteReviews\Modules\\'.ucfirst($integration); |
|
112 | + if (!glsr($integrationClass)->isActive()) { |
|
113 | + glsr(Notice::class)->addError(sprintf( |
|
114 | + __('Please install/activate the %s plugin to enable integration.', 'site-reviews'), |
|
115 | + constant($integrationClass.'::PLUGIN_NAME') |
|
116 | + )); |
|
117 | + return false; |
|
118 | + } elseif (!glsr($integrationClass)->isSupported()) { |
|
119 | + glsr(Notice::class)->addError(sprintf( |
|
120 | + __('Please update the %s plugin to v%s or greater to enable integration.', 'site-reviews'), |
|
121 | + constant($integrationClass.'::PLUGIN_NAME'), |
|
122 | + constant($integrationClass.'::SUPPORTED_VERSION') |
|
123 | + )); |
|
124 | + return false; |
|
125 | + } |
|
126 | + return true; |
|
127 | + } |
|
128 | 128 | } |
@@ -14,17 +14,17 @@ discard block |
||
14 | 14 | * @return array |
15 | 15 | * @callback register_setting |
16 | 16 | */ |
17 | - public function callbackRegisterSettings($input) |
|
17 | + public function callbackRegisterSettings( $input ) |
|
18 | 18 | { |
19 | - $settings = Arr::consolidateArray($input); |
|
20 | - if (1 === count($settings) && array_key_exists('settings', $settings)) { |
|
21 | - $options = array_replace_recursive(glsr(OptionManager::class)->all(), $input); |
|
22 | - $options = $this->sanitizeGeneral($input, $options); |
|
23 | - $options = $this->sanitizeSubmissions($input, $options); |
|
24 | - $options = $this->sanitizeTranslations($input, $options); |
|
25 | - $options = apply_filters('site-reviews/settings/callback', $options, $settings); |
|
26 | - if (filter_input(INPUT_POST, 'option_page') == Application::ID.'-settings') { |
|
27 | - glsr(Notice::class)->addSuccess(__('Settings updated.', 'site-reviews')); |
|
19 | + $settings = Arr::consolidateArray( $input ); |
|
20 | + if( 1 === count( $settings ) && array_key_exists( 'settings', $settings ) ) { |
|
21 | + $options = array_replace_recursive( glsr( OptionManager::class )->all(), $input ); |
|
22 | + $options = $this->sanitizeGeneral( $input, $options ); |
|
23 | + $options = $this->sanitizeSubmissions( $input, $options ); |
|
24 | + $options = $this->sanitizeTranslations( $input, $options ); |
|
25 | + $options = apply_filters( 'site-reviews/settings/callback', $options, $settings ); |
|
26 | + if( filter_input( INPUT_POST, 'option_page' ) == Application::ID.'-settings' ) { |
|
27 | + glsr( Notice::class )->addSuccess( __( 'Settings updated.', 'site-reviews' ) ); |
|
28 | 28 | } |
29 | 29 | return $options; |
30 | 30 | } |
@@ -37,63 +37,63 @@ discard block |
||
37 | 37 | */ |
38 | 38 | public function registerSettings() |
39 | 39 | { |
40 | - register_setting(Application::ID.'-settings', OptionManager::databaseKey(), [ |
|
40 | + register_setting( Application::ID.'-settings', OptionManager::databaseKey(), [ |
|
41 | 41 | 'sanitize_callback' => [$this, 'callbackRegisterSettings'], |
42 | - ]); |
|
42 | + ] ); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | /** |
46 | 46 | * @return array |
47 | 47 | */ |
48 | - protected function sanitizeGeneral(array $input, array $options) |
|
48 | + protected function sanitizeGeneral( array $input, array $options ) |
|
49 | 49 | { |
50 | 50 | $key = 'settings.general'; |
51 | - $inputForm = Arr::get($input, $key); |
|
52 | - if (!$this->hasMultilingualIntegration(Arr::get($inputForm, 'multilingual'))) { |
|
53 | - $options = Arr::set($options, $key.'.multilingual', ''); |
|
51 | + $inputForm = Arr::get( $input, $key ); |
|
52 | + if( !$this->hasMultilingualIntegration( Arr::get( $inputForm, 'multilingual' ) ) ) { |
|
53 | + $options = Arr::set( $options, $key.'.multilingual', '' ); |
|
54 | 54 | } |
55 | - if ('' == trim(Arr::get($inputForm, 'notification_message'))) { |
|
56 | - $defaultValue = Arr::get(glsr()->defaults, $key.'.notification_message'); |
|
57 | - $options = Arr::set($options, $key.'.notification_message', $defaultValue); |
|
55 | + if( '' == trim( Arr::get( $inputForm, 'notification_message' ) ) ) { |
|
56 | + $defaultValue = Arr::get( glsr()->defaults, $key.'.notification_message' ); |
|
57 | + $options = Arr::set( $options, $key.'.notification_message', $defaultValue ); |
|
58 | 58 | } |
59 | - $defaultValue = Arr::get($inputForm, 'notifications', []); |
|
60 | - $options = Arr::set($options, $key.'.notifications', $defaultValue); |
|
59 | + $defaultValue = Arr::get( $inputForm, 'notifications', [] ); |
|
60 | + $options = Arr::set( $options, $key.'.notifications', $defaultValue ); |
|
61 | 61 | return $options; |
62 | 62 | } |
63 | 63 | |
64 | 64 | /** |
65 | 65 | * @return array |
66 | 66 | */ |
67 | - protected function sanitizeSubmissions(array $input, array $options) |
|
67 | + protected function sanitizeSubmissions( array $input, array $options ) |
|
68 | 68 | { |
69 | 69 | $key = 'settings.submissions'; |
70 | - $inputForm = Arr::get($input, $key); |
|
70 | + $inputForm = Arr::get( $input, $key ); |
|
71 | 71 | $defaultValue = isset($inputForm['required']) |
72 | 72 | ? $inputForm['required'] |
73 | 73 | : []; |
74 | - $options = Arr::set($options, $key.'.required', $defaultValue); |
|
74 | + $options = Arr::set( $options, $key.'.required', $defaultValue ); |
|
75 | 75 | return $options; |
76 | 76 | } |
77 | 77 | |
78 | 78 | /** |
79 | 79 | * @return array |
80 | 80 | */ |
81 | - protected function sanitizeTranslations(array $input, array $options) |
|
81 | + protected function sanitizeTranslations( array $input, array $options ) |
|
82 | 82 | { |
83 | 83 | $key = 'settings.strings'; |
84 | - $inputForm = Arr::consolidateArray(Arr::get($input, $key)); |
|
85 | - if (!empty($inputForm)) { |
|
86 | - $options = Arr::set($options, $key, array_values(array_filter($inputForm))); |
|
84 | + $inputForm = Arr::consolidateArray( Arr::get( $input, $key ) ); |
|
85 | + if( !empty($inputForm) ) { |
|
86 | + $options = Arr::set( $options, $key, array_values( array_filter( $inputForm ) ) ); |
|
87 | 87 | $allowedTags = [ |
88 | 88 | 'a' => ['class' => [], 'href' => [], 'target' => []], |
89 | 89 | 'span' => ['class' => []], |
90 | 90 | ]; |
91 | - array_walk($options['settings']['strings'], function (&$string) use ($allowedTags) { |
|
92 | - if (isset($string['s2'])) { |
|
93 | - $string['s2'] = wp_kses($string['s2'], $allowedTags); |
|
91 | + array_walk( $options['settings']['strings'], function( &$string ) use ($allowedTags) { |
|
92 | + if( isset($string['s2']) ) { |
|
93 | + $string['s2'] = wp_kses( $string['s2'], $allowedTags ); |
|
94 | 94 | } |
95 | - if (isset($string['p2'])) { |
|
96 | - $string['p2'] = wp_kses($string['p2'], $allowedTags); |
|
95 | + if( isset($string['p2']) ) { |
|
96 | + $string['p2'] = wp_kses( $string['p2'], $allowedTags ); |
|
97 | 97 | } |
98 | 98 | }); |
99 | 99 | } |
@@ -103,24 +103,24 @@ discard block |
||
103 | 103 | /** |
104 | 104 | * @return bool |
105 | 105 | */ |
106 | - protected function hasMultilingualIntegration($integration) |
|
106 | + protected function hasMultilingualIntegration( $integration ) |
|
107 | 107 | { |
108 | - if (!in_array($integration, ['polylang', 'wpml'])) { |
|
108 | + if( !in_array( $integration, ['polylang', 'wpml'] ) ) { |
|
109 | 109 | return false; |
110 | 110 | } |
111 | - $integrationClass = 'GeminiLabs\SiteReviews\Modules\\'.ucfirst($integration); |
|
112 | - if (!glsr($integrationClass)->isActive()) { |
|
113 | - glsr(Notice::class)->addError(sprintf( |
|
114 | - __('Please install/activate the %s plugin to enable integration.', 'site-reviews'), |
|
115 | - constant($integrationClass.'::PLUGIN_NAME') |
|
116 | - )); |
|
111 | + $integrationClass = 'GeminiLabs\SiteReviews\Modules\\'.ucfirst( $integration ); |
|
112 | + if( !glsr( $integrationClass )->isActive() ) { |
|
113 | + glsr( Notice::class )->addError( sprintf( |
|
114 | + __( 'Please install/activate the %s plugin to enable integration.', 'site-reviews' ), |
|
115 | + constant( $integrationClass.'::PLUGIN_NAME' ) |
|
116 | + ) ); |
|
117 | 117 | return false; |
118 | - } elseif (!glsr($integrationClass)->isSupported()) { |
|
119 | - glsr(Notice::class)->addError(sprintf( |
|
120 | - __('Please update the %s plugin to v%s or greater to enable integration.', 'site-reviews'), |
|
121 | - constant($integrationClass.'::PLUGIN_NAME'), |
|
122 | - constant($integrationClass.'::SUPPORTED_VERSION') |
|
123 | - )); |
|
118 | + } elseif( !glsr( $integrationClass )->isSupported() ) { |
|
119 | + glsr( Notice::class )->addError( sprintf( |
|
120 | + __( 'Please update the %s plugin to v%s or greater to enable integration.', 'site-reviews' ), |
|
121 | + constant( $integrationClass.'::PLUGIN_NAME' ), |
|
122 | + constant( $integrationClass.'::SUPPORTED_VERSION' ) |
|
123 | + ) ); |
|
124 | 124 | return false; |
125 | 125 | } |
126 | 126 | return true; |
@@ -4,29 +4,29 @@ |
||
4 | 4 | |
5 | 5 | interface MultilingualContract |
6 | 6 | { |
7 | - /** |
|
8 | - * @param int|string $postId |
|
9 | - * @return \WP_Post|void|null |
|
10 | - */ |
|
11 | - public function getPost($postId); |
|
7 | + /** |
|
8 | + * @param int|string $postId |
|
9 | + * @return \WP_Post|void|null |
|
10 | + */ |
|
11 | + public function getPost($postId); |
|
12 | 12 | |
13 | - /** |
|
14 | - * @return array |
|
15 | - */ |
|
16 | - public function getPostIds(array $postIds); |
|
13 | + /** |
|
14 | + * @return array |
|
15 | + */ |
|
16 | + public function getPostIds(array $postIds); |
|
17 | 17 | |
18 | - /** |
|
19 | - * @return bool |
|
20 | - */ |
|
21 | - public function isActive(); |
|
18 | + /** |
|
19 | + * @return bool |
|
20 | + */ |
|
21 | + public function isActive(); |
|
22 | 22 | |
23 | - /** |
|
24 | - * @return bool |
|
25 | - */ |
|
26 | - public function isEnabled(); |
|
23 | + /** |
|
24 | + * @return bool |
|
25 | + */ |
|
26 | + public function isEnabled(); |
|
27 | 27 | |
28 | - /** |
|
29 | - * @return bool |
|
30 | - */ |
|
31 | - public function isSupported(); |
|
28 | + /** |
|
29 | + * @return bool |
|
30 | + */ |
|
31 | + public function isSupported(); |
|
32 | 32 | } |
@@ -8,12 +8,12 @@ |
||
8 | 8 | * @param int|string $postId |
9 | 9 | * @return \WP_Post|void|null |
10 | 10 | */ |
11 | - public function getPost($postId); |
|
11 | + public function getPost( $postId ); |
|
12 | 12 | |
13 | 13 | /** |
14 | 14 | * @return array |
15 | 15 | */ |
16 | - public function getPostIds(array $postIds); |
|
16 | + public function getPostIds( array $postIds ); |
|
17 | 17 | |
18 | 18 | /** |
19 | 19 | * @return bool |
@@ -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 | } |
@@ -13,44 +13,44 @@ discard block |
||
13 | 13 | /** |
14 | 14 | * {@inheritdoc} |
15 | 15 | */ |
16 | - public function getPost($postId) |
|
16 | + public function getPost( $postId ) |
|
17 | 17 | { |
18 | - $postId = trim($postId); |
|
19 | - if (!is_numeric($postId)) { |
|
18 | + $postId = trim( $postId ); |
|
19 | + if( !is_numeric( $postId ) ) { |
|
20 | 20 | return; |
21 | 21 | } |
22 | - if ($this->isEnabled()) { |
|
23 | - $postId = apply_filters('wpml_object_id', $postId, 'any', true); |
|
22 | + if( $this->isEnabled() ) { |
|
23 | + $postId = apply_filters( 'wpml_object_id', $postId, 'any', true ); |
|
24 | 24 | } |
25 | - return get_post(intval($postId)); |
|
25 | + return get_post( intval( $postId ) ); |
|
26 | 26 | } |
27 | 27 | |
28 | 28 | /** |
29 | 29 | * {@inheritdoc} |
30 | 30 | */ |
31 | - public function getPostIds(array $postIds) |
|
31 | + public function getPostIds( array $postIds ) |
|
32 | 32 | { |
33 | - if (!$this->isEnabled()) { |
|
33 | + if( !$this->isEnabled() ) { |
|
34 | 34 | return $postIds; |
35 | 35 | } |
36 | 36 | $newPostIds = []; |
37 | - foreach ($this->cleanIds($postIds) as $postId) { |
|
38 | - $postType = get_post_type($postId); |
|
39 | - if (!$postType) { |
|
37 | + foreach( $this->cleanIds( $postIds ) as $postId ) { |
|
38 | + $postType = get_post_type( $postId ); |
|
39 | + if( !$postType ) { |
|
40 | 40 | continue; |
41 | 41 | } |
42 | 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)) { |
|
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 | 46 | $translations = []; |
47 | 47 | } |
48 | 48 | $newPostIds = array_merge( |
49 | 49 | $newPostIds, |
50 | - array_column($translations, 'element_id') |
|
50 | + array_column( $translations, 'element_id' ) |
|
51 | 51 | ); |
52 | 52 | } |
53 | - return $this->cleanIds($newPostIds); |
|
53 | + return $this->cleanIds( $newPostIds ); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | /** |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | */ |
59 | 59 | public function isActive() |
60 | 60 | { |
61 | - return defined('ICL_SITEPRESS_VERSION'); |
|
61 | + return defined( 'ICL_SITEPRESS_VERSION' ); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | /** |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | public function isEnabled() |
68 | 68 | { |
69 | 69 | return $this->isActive() |
70 | - && 'wpml' == glsr(OptionManager::class)->get('settings.general.multilingual'); |
|
70 | + && 'wpml' == glsr( OptionManager::class )->get( 'settings.general.multilingual' ); |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | /** |
@@ -76,14 +76,14 @@ discard block |
||
76 | 76 | public function isSupported() |
77 | 77 | { |
78 | 78 | return $this->isActive() |
79 | - && version_compare(ICL_SITEPRESS_VERSION, static::SUPPORTED_VERSION, '>='); |
|
79 | + && version_compare( ICL_SITEPRESS_VERSION, static::SUPPORTED_VERSION, '>=' ); |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | /** |
83 | 83 | * @return array |
84 | 84 | */ |
85 | - protected function cleanIds(array $postIds) |
|
85 | + protected function cleanIds( array $postIds ) |
|
86 | 86 | { |
87 | - return array_filter(array_unique($postIds)); |
|
87 | + return array_filter( array_unique( $postIds ) ); |
|
88 | 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 | } |
@@ -13,37 +13,37 @@ discard block |
||
13 | 13 | /** |
14 | 14 | * {@inheritdoc} |
15 | 15 | */ |
16 | - public function getPost($postId) |
|
16 | + public function getPost( $postId ) |
|
17 | 17 | { |
18 | - $postId = trim($postId); |
|
19 | - if (!is_numeric($postId)) { |
|
18 | + $postId = trim( $postId ); |
|
19 | + if( !is_numeric( $postId ) ) { |
|
20 | 20 | return; |
21 | 21 | } |
22 | - if ($this->isEnabled()) { |
|
23 | - $polylangPostId = pll_get_post($postId, pll_get_post_language(get_the_ID())); |
|
22 | + if( $this->isEnabled() ) { |
|
23 | + $polylangPostId = pll_get_post( $postId, pll_get_post_language( get_the_ID() ) ); |
|
24 | 24 | } |
25 | - if (!empty($polylangPostId)) { |
|
25 | + if( !empty($polylangPostId) ) { |
|
26 | 26 | $postId = $polylangPostId; |
27 | 27 | } |
28 | - return get_post(intval($postId)); |
|
28 | + return get_post( intval( $postId ) ); |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | /** |
32 | 32 | * {@inheritdoc} |
33 | 33 | */ |
34 | - public function getPostIds(array $postIds) |
|
34 | + public function getPostIds( array $postIds ) |
|
35 | 35 | { |
36 | - if (!$this->isEnabled()) { |
|
36 | + if( !$this->isEnabled() ) { |
|
37 | 37 | return $postIds; |
38 | 38 | } |
39 | 39 | $newPostIds = []; |
40 | - foreach ($this->cleanIds($postIds) as $postId) { |
|
40 | + foreach( $this->cleanIds( $postIds ) as $postId ) { |
|
41 | 41 | $newPostIds = array_merge( |
42 | 42 | $newPostIds, |
43 | - array_values(pll_get_post_translations($postId)) |
|
43 | + array_values( pll_get_post_translations( $postId ) ) |
|
44 | 44 | ); |
45 | 45 | } |
46 | - return $this->cleanIds($newPostIds); |
|
46 | + return $this->cleanIds( $newPostIds ); |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | /** |
@@ -51,10 +51,10 @@ discard block |
||
51 | 51 | */ |
52 | 52 | public function isActive() |
53 | 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'); |
|
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 | 58 | } |
59 | 59 | |
60 | 60 | /** |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | public function isEnabled() |
64 | 64 | { |
65 | 65 | return $this->isActive() |
66 | - && 'polylang' == glsr(OptionManager::class)->get('settings.general.multilingual'); |
|
66 | + && 'polylang' == glsr( OptionManager::class )->get( 'settings.general.multilingual' ); |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | /** |
@@ -71,15 +71,15 @@ discard block |
||
71 | 71 | */ |
72 | 72 | public function isSupported() |
73 | 73 | { |
74 | - return defined('POLYLANG_VERSION') |
|
75 | - && version_compare(POLYLANG_VERSION, static::SUPPORTED_VERSION, '>='); |
|
74 | + return defined( 'POLYLANG_VERSION' ) |
|
75 | + && version_compare( POLYLANG_VERSION, static::SUPPORTED_VERSION, '>=' ); |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | /** |
79 | 79 | * @return array |
80 | 80 | */ |
81 | - protected function cleanIds(array $postIds) |
|
81 | + protected function cleanIds( array $postIds ) |
|
82 | 82 | { |
83 | - return array_filter(array_unique($postIds)); |
|
83 | + return array_filter( array_unique( $postIds ) ); |
|
84 | 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 | } |
@@ -45,13 +45,13 @@ discard block |
||
45 | 45 | * @param int $roundBy |
46 | 46 | * @return float |
47 | 47 | */ |
48 | - public function getAverage(array $ratingCounts, $roundBy = 1) |
|
48 | + public function getAverage( array $ratingCounts, $roundBy = 1 ) |
|
49 | 49 | { |
50 | - $average = array_sum($ratingCounts); |
|
51 | - if ($average > 0) { |
|
52 | - $average = round($this->getTotalSum($ratingCounts) / $average, intval($roundBy)); |
|
50 | + $average = array_sum( $ratingCounts ); |
|
51 | + if( $average > 0 ) { |
|
52 | + $average = round( $this->getTotalSum( $ratingCounts ) / $average, intval( $roundBy ) ); |
|
53 | 53 | } |
54 | - return floatval(apply_filters('site-reviews/rating/average', $average, $ratingCounts)); |
|
54 | + return floatval( apply_filters( 'site-reviews/rating/average', $average, $ratingCounts ) ); |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | /** |
@@ -64,50 +64,50 @@ discard block |
||
64 | 64 | * @param int $confidencePercentage |
65 | 65 | * @return int|float |
66 | 66 | */ |
67 | - public function getLowerBound(array $upDownCounts = [0, 0], $confidencePercentage = 95) |
|
67 | + public function getLowerBound( array $upDownCounts = [0, 0], $confidencePercentage = 95 ) |
|
68 | 68 | { |
69 | - $numRatings = array_sum($upDownCounts); |
|
70 | - if ($numRatings < 1) { |
|
69 | + $numRatings = array_sum( $upDownCounts ); |
|
70 | + if( $numRatings < 1 ) { |
|
71 | 71 | return 0; |
72 | 72 | } |
73 | 73 | $z = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
74 | 74 | $phat = 1 * $upDownCounts[1] / $numRatings; |
75 | - return ($phat + $z * $z / (2 * $numRatings) - $z * sqrt(($phat * (1 - $phat) + $z * $z / (4 * $numRatings)) / $numRatings)) / (1 + $z * $z / $numRatings); |
|
75 | + return ($phat + $z * $z / (2 * $numRatings) - $z * sqrt( ($phat * (1 - $phat) + $z * $z / (4 * $numRatings)) / $numRatings )) / (1 + $z * $z / $numRatings); |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | /** |
79 | 79 | * @return int|float |
80 | 80 | */ |
81 | - public function getOverallPercentage(array $ratingCounts) |
|
81 | + public function getOverallPercentage( array $ratingCounts ) |
|
82 | 82 | { |
83 | - return round($this->getAverage($ratingCounts) * 100 / glsr()->constant('MAX_RATING', __CLASS__), 2); |
|
83 | + return round( $this->getAverage( $ratingCounts ) * 100 / glsr()->constant( 'MAX_RATING', __CLASS__ ), 2 ); |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | /** |
87 | 87 | * @return array |
88 | 88 | */ |
89 | - public function getPercentages(array $ratingCounts) |
|
89 | + public function getPercentages( array $ratingCounts ) |
|
90 | 90 | { |
91 | - $total = array_sum($ratingCounts); |
|
92 | - foreach ($ratingCounts as $index => $count) { |
|
93 | - if (empty($count)) { |
|
91 | + $total = array_sum( $ratingCounts ); |
|
92 | + foreach( $ratingCounts as $index => $count ) { |
|
93 | + if( empty($count) ) { |
|
94 | 94 | continue; |
95 | 95 | } |
96 | 96 | $ratingCounts[$index] = $count / $total * 100; |
97 | 97 | } |
98 | - return $this->getRoundedPercentages($ratingCounts); |
|
98 | + return $this->getRoundedPercentages( $ratingCounts ); |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | /** |
102 | 102 | * @return float |
103 | 103 | */ |
104 | - public function getRanking(array $ratingCounts) |
|
104 | + public function getRanking( array $ratingCounts ) |
|
105 | 105 | { |
106 | - return floatval(apply_filters('site-reviews/rating/ranking', |
|
107 | - $this->getRankingUsingImdb($ratingCounts), |
|
106 | + return floatval( apply_filters( 'site-reviews/rating/ranking', |
|
107 | + $this->getRankingUsingImdb( $ratingCounts ), |
|
108 | 108 | $ratingCounts, |
109 | 109 | $this |
110 | - )); |
|
110 | + ) ); |
|
111 | 111 | } |
112 | 112 | |
113 | 113 | /** |
@@ -120,15 +120,15 @@ discard block |
||
120 | 120 | * @param int $confidencePercentage |
121 | 121 | * @return int|float |
122 | 122 | */ |
123 | - public function getRankingUsingImdb(array $ratingCounts, $confidencePercentage = 70) |
|
123 | + public function getRankingUsingImdb( array $ratingCounts, $confidencePercentage = 70 ) |
|
124 | 124 | { |
125 | - $avgRating = $this->getAverage($ratingCounts); |
|
125 | + $avgRating = $this->getAverage( $ratingCounts ); |
|
126 | 126 | // Represents a prior (your prior opinion without data) for the average star rating. A higher prior also means a higher margin for error. |
127 | 127 | // This could also be the average score of all items instead of a fixed value. |
128 | - $bayesMean = ($confidencePercentage / 100) * glsr()->constant('MAX_RATING', __CLASS__); // prior, 70% = 3.5 |
|
128 | + $bayesMean = ($confidencePercentage / 100) * glsr()->constant( 'MAX_RATING', __CLASS__ ); // prior, 70% = 3.5 |
|
129 | 129 | // Represents the number of ratings expected to begin observing a pattern that would put confidence in the prior. |
130 | 130 | $bayesMinimal = 10; // confidence |
131 | - $numOfReviews = array_sum($ratingCounts); |
|
131 | + $numOfReviews = array_sum( $ratingCounts ); |
|
132 | 132 | return $avgRating > 0 |
133 | 133 | ? (($bayesMinimal * $bayesMean) + ($avgRating * $numOfReviews)) / ($bayesMinimal + $numOfReviews) |
134 | 134 | : 0; |
@@ -144,48 +144,48 @@ discard block |
||
144 | 144 | * @param int $confidencePercentage |
145 | 145 | * @return float |
146 | 146 | */ |
147 | - public function getRankingUsingZScores(array $ratingCounts, $confidencePercentage = 90) |
|
147 | + public function getRankingUsingZScores( array $ratingCounts, $confidencePercentage = 90 ) |
|
148 | 148 | { |
149 | - $ratingCountsSum = array_sum($ratingCounts) + glsr()->constant('MAX_RATING', __CLASS__); |
|
150 | - $weight = $this->getWeight($ratingCounts, $ratingCountsSum); |
|
151 | - $weightPow2 = $this->getWeight($ratingCounts, $ratingCountsSum, true); |
|
149 | + $ratingCountsSum = array_sum( $ratingCounts ) + glsr()->constant( 'MAX_RATING', __CLASS__ ); |
|
150 | + $weight = $this->getWeight( $ratingCounts, $ratingCountsSum ); |
|
151 | + $weightPow2 = $this->getWeight( $ratingCounts, $ratingCountsSum, true ); |
|
152 | 152 | $zScore = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
153 | - return $weight - $zScore * sqrt(($weightPow2 - pow($weight, 2)) / ($ratingCountsSum + 1)); |
|
153 | + return $weight - $zScore * sqrt( ($weightPow2 - pow( $weight, 2 )) / ($ratingCountsSum + 1) ); |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | /** |
157 | 157 | * @param int $target |
158 | 158 | * @return array |
159 | 159 | */ |
160 | - protected function getRoundedPercentages(array $percentages, $totalPercent = 100) |
|
160 | + protected function getRoundedPercentages( array $percentages, $totalPercent = 100 ) |
|
161 | 161 | { |
162 | - array_walk($percentages, function (&$percent, $index) { |
|
162 | + array_walk( $percentages, function( &$percent, $index ) { |
|
163 | 163 | $percent = [ |
164 | 164 | 'index' => $index, |
165 | - 'percent' => floor($percent), |
|
166 | - 'remainder' => fmod($percent, 1), |
|
165 | + 'percent' => floor( $percent ), |
|
166 | + 'remainder' => fmod( $percent, 1 ), |
|
167 | 167 | ]; |
168 | 168 | }); |
169 | - $indexes = glsr_array_column($percentages, 'index'); |
|
170 | - $remainders = glsr_array_column($percentages, 'remainder'); |
|
171 | - array_multisort($remainders, SORT_DESC, SORT_STRING, $indexes, SORT_DESC, $percentages); |
|
169 | + $indexes = glsr_array_column( $percentages, 'index' ); |
|
170 | + $remainders = glsr_array_column( $percentages, 'remainder' ); |
|
171 | + array_multisort( $remainders, SORT_DESC, SORT_STRING, $indexes, SORT_DESC, $percentages ); |
|
172 | 172 | $i = 0; |
173 | - if (array_sum(glsr_array_column($percentages, 'percent')) > 0) { |
|
174 | - while (array_sum(glsr_array_column($percentages, 'percent')) < $totalPercent) { |
|
173 | + if( array_sum( glsr_array_column( $percentages, 'percent' ) ) > 0 ) { |
|
174 | + while( array_sum( glsr_array_column( $percentages, 'percent' ) ) < $totalPercent ) { |
|
175 | 175 | ++$percentages[$i]['percent']; |
176 | 176 | ++$i; |
177 | 177 | } |
178 | 178 | } |
179 | - array_multisort($indexes, SORT_DESC, $percentages); |
|
180 | - return array_combine($indexes, glsr_array_column($percentages, 'percent')); |
|
179 | + array_multisort( $indexes, SORT_DESC, $percentages ); |
|
180 | + return array_combine( $indexes, glsr_array_column( $percentages, 'percent' ) ); |
|
181 | 181 | } |
182 | 182 | |
183 | 183 | /** |
184 | 184 | * @return int |
185 | 185 | */ |
186 | - protected function getTotalSum(array $ratingCounts) |
|
186 | + protected function getTotalSum( array $ratingCounts ) |
|
187 | 187 | { |
188 | - return array_reduce(array_keys($ratingCounts), function ($carry, $index) use ($ratingCounts) { |
|
188 | + return array_reduce( array_keys( $ratingCounts ), function( $carry, $index ) use ($ratingCounts) { |
|
189 | 189 | return $carry + ($index * $ratingCounts[$index]); |
190 | 190 | }); |
191 | 191 | } |
@@ -195,12 +195,12 @@ discard block |
||
195 | 195 | * @param bool $powerOf2 |
196 | 196 | * @return float |
197 | 197 | */ |
198 | - protected function getWeight(array $ratingCounts, $ratingCountsSum, $powerOf2 = false) |
|
198 | + protected function getWeight( array $ratingCounts, $ratingCountsSum, $powerOf2 = false ) |
|
199 | 199 | { |
200 | - return array_reduce(array_keys($ratingCounts), |
|
201 | - function ($count, $rating) use ($ratingCounts, $ratingCountsSum, $powerOf2) { |
|
200 | + return array_reduce( array_keys( $ratingCounts ), |
|
201 | + function( $count, $rating ) use ($ratingCounts, $ratingCountsSum, $powerOf2) { |
|
202 | 202 | $ratingLevel = $powerOf2 |
203 | - ? pow($rating, 2) |
|
203 | + ? pow( $rating, 2 ) |
|
204 | 204 | : $rating; |
205 | 205 | return $count + ($ratingLevel * ($ratingCounts[$rating] + 1)) / $ratingCountsSum; |
206 | 206 | } |
@@ -7,40 +7,40 @@ |
||
7 | 7 | |
8 | 8 | class EmailDefaults extends Defaults |
9 | 9 | { |
10 | - /** |
|
11 | - * @return array |
|
12 | - */ |
|
13 | - protected function defaults() |
|
14 | - { |
|
15 | - return [ |
|
16 | - 'after' => '', |
|
17 | - 'attachments' => [], |
|
18 | - 'bcc' => '', |
|
19 | - 'before' => '', |
|
20 | - 'cc' => '', |
|
21 | - 'from' => $this->getFromName().' <'.$this->getFromEmail().'>', |
|
22 | - 'message' => '', |
|
23 | - 'reply-to' => '', |
|
24 | - 'subject' => '', |
|
25 | - 'template' => '', |
|
26 | - 'template-tags' => [], |
|
27 | - 'to' => '', |
|
28 | - ]; |
|
29 | - } |
|
10 | + /** |
|
11 | + * @return array |
|
12 | + */ |
|
13 | + protected function defaults() |
|
14 | + { |
|
15 | + return [ |
|
16 | + 'after' => '', |
|
17 | + 'attachments' => [], |
|
18 | + 'bcc' => '', |
|
19 | + 'before' => '', |
|
20 | + 'cc' => '', |
|
21 | + 'from' => $this->getFromName().' <'.$this->getFromEmail().'>', |
|
22 | + 'message' => '', |
|
23 | + 'reply-to' => '', |
|
24 | + 'subject' => '', |
|
25 | + 'template' => '', |
|
26 | + 'template-tags' => [], |
|
27 | + 'to' => '', |
|
28 | + ]; |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * @return string |
|
33 | - */ |
|
34 | - protected function getFromEmail() |
|
35 | - { |
|
36 | - return glsr(OptionManager::class)->getWP('admin_email'); |
|
37 | - } |
|
31 | + /** |
|
32 | + * @return string |
|
33 | + */ |
|
34 | + protected function getFromEmail() |
|
35 | + { |
|
36 | + return glsr(OptionManager::class)->getWP('admin_email'); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @return string |
|
41 | - */ |
|
42 | - protected function getFromName() |
|
43 | - { |
|
44 | - return wp_specialchars_decode(glsr(OptionManager::class)->getWP('blogname'), ENT_QUOTES); |
|
45 | - } |
|
39 | + /** |
|
40 | + * @return string |
|
41 | + */ |
|
42 | + protected function getFromName() |
|
43 | + { |
|
44 | + return wp_specialchars_decode(glsr(OptionManager::class)->getWP('blogname'), ENT_QUOTES); |
|
45 | + } |
|
46 | 46 | } |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | */ |
34 | 34 | protected function getFromEmail() |
35 | 35 | { |
36 | - return glsr(OptionManager::class)->getWP('admin_email'); |
|
36 | + return glsr( OptionManager::class )->getWP( 'admin_email' ); |
|
37 | 37 | } |
38 | 38 | |
39 | 39 | /** |
@@ -41,6 +41,6 @@ discard block |
||
41 | 41 | */ |
42 | 42 | protected function getFromName() |
43 | 43 | { |
44 | - return wp_specialchars_decode(glsr(OptionManager::class)->getWP('blogname'), ENT_QUOTES); |
|
44 | + return wp_specialchars_decode( glsr( OptionManager::class )->getWP( 'blogname' ), ENT_QUOTES ); |
|
45 | 45 | } |
46 | 46 | } |