@@ -4,15 +4,15 @@ |
||
4 | 4 | |
5 | 5 | class SiteReviewsFormShortcode extends Shortcode |
6 | 6 | { |
7 | - protected function hideOptions() |
|
8 | - { |
|
9 | - return [ |
|
10 | - 'rating' => __('Hide the rating field', 'site-reviews'), |
|
11 | - 'title' => __('Hide the title field', 'site-reviews'), |
|
12 | - 'content' => __('Hide the review field', 'site-reviews'), |
|
13 | - 'name' => __('Hide the name field', 'site-reviews'), |
|
14 | - 'email' => __('Hide the email field', 'site-reviews'), |
|
15 | - 'terms' => __('Hide the terms field', 'site-reviews'), |
|
16 | - ]; |
|
17 | - } |
|
7 | + protected function hideOptions() |
|
8 | + { |
|
9 | + return [ |
|
10 | + 'rating' => __('Hide the rating field', 'site-reviews'), |
|
11 | + 'title' => __('Hide the title field', 'site-reviews'), |
|
12 | + 'content' => __('Hide the review field', 'site-reviews'), |
|
13 | + 'name' => __('Hide the name field', 'site-reviews'), |
|
14 | + 'email' => __('Hide the email field', 'site-reviews'), |
|
15 | + 'terms' => __('Hide the terms field', 'site-reviews'), |
|
16 | + ]; |
|
17 | + } |
|
18 | 18 | } |
@@ -12,238 +12,238 @@ |
||
12 | 12 | |
13 | 13 | abstract class Shortcode implements ShortcodeContract |
14 | 14 | { |
15 | - /** |
|
16 | - * @var string |
|
17 | - */ |
|
18 | - protected $partialName; |
|
19 | - |
|
20 | - /** |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - protected $shortcodeName; |
|
24 | - |
|
25 | - public function __construct() |
|
26 | - { |
|
27 | - $this->partialName = $this->getShortcodePartialName(); |
|
28 | - $this->shortcodeName = $this->getShortcodeName(); |
|
29 | - } |
|
30 | - |
|
31 | - /** |
|
32 | - * @param string|array $atts |
|
33 | - * @param string $type |
|
34 | - * @return string |
|
35 | - */ |
|
36 | - public function build($atts, array $args = [], $type = 'shortcode') |
|
37 | - { |
|
38 | - $args = $this->normalizeArgs($args, $type); |
|
39 | - $atts = $this->normalizeAtts($atts, $type); |
|
40 | - $partial = glsr(Partial::class)->build($this->partialName, $atts); |
|
41 | - $title = !empty($atts['title']) |
|
42 | - ? $args['before_title'].$atts['title'].$args['after_title'] |
|
43 | - : ''; |
|
44 | - $debug = sprintf('<glsr-%1$s hidden data-atts=\'%2$s\'></glsr-%1$s>', $type, $atts['json']); |
|
45 | - return $args['before_widget'].$title.$partial.$debug.$args['after_widget']; |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * @param string|array $atts |
|
50 | - * @return string |
|
51 | - */ |
|
52 | - public function buildShortcode($atts = []) |
|
53 | - { |
|
54 | - return $this->build($atts); |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @return array |
|
59 | - */ |
|
60 | - public function getDefaults($atts) |
|
61 | - { |
|
62 | - return glsr($this->getShortcodeDefaultsClassName())->restrict(wp_parse_args($atts)); |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * @return array |
|
67 | - */ |
|
68 | - public function getHideOptions() |
|
69 | - { |
|
70 | - $options = $this->hideOptions(); |
|
71 | - return apply_filters('site-reviews/shortcode/hide-options', $options, $this->shortcodeName); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * @return string |
|
76 | - */ |
|
77 | - public function getShortcodeClassName($replace = '', $search = 'Shortcode') |
|
78 | - { |
|
79 | - return str_replace($search, $replace, (new ReflectionClass($this))->getShortName()); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @return string |
|
84 | - */ |
|
85 | - public function getShortcodeDefaultsClassName() |
|
86 | - { |
|
87 | - return Helper::buildClassName( |
|
88 | - $this->getShortcodeClassName('Defaults'), |
|
89 | - 'Defaults' |
|
90 | - ); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @return string |
|
95 | - */ |
|
96 | - public function getShortcodeName() |
|
97 | - { |
|
98 | - return Str::snakeCase($this->getShortcodeClassName()); |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @return string |
|
103 | - */ |
|
104 | - public function getShortcodePartialName() |
|
105 | - { |
|
106 | - return Str::dashCase($this->getShortcodeClassName()); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @param array|string $args |
|
111 | - * @param string $type |
|
112 | - * @return array |
|
113 | - */ |
|
114 | - public function normalizeArgs($args, $type = 'shortcode') |
|
115 | - { |
|
116 | - $args = wp_parse_args($args, [ |
|
117 | - 'before_widget' => '<div class="glsr-'.$type.' '.$type.'-'.$this->partialName.'">', |
|
118 | - 'after_widget' => '</div>', |
|
119 | - 'before_title' => '<h3 class="glsr-'.$type.'-title">', |
|
120 | - 'after_title' => '</h3>', |
|
121 | - ]); |
|
122 | - return apply_filters('site-reviews/shortcode/args', $args, $type, $this->partialName); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @param array|string $atts |
|
127 | - * @param string $type |
|
128 | - * @return array |
|
129 | - */ |
|
130 | - public function normalizeAtts($atts, $type = 'shortcode') |
|
131 | - { |
|
132 | - $atts = apply_filters('site-reviews/shortcode/atts', $atts, $type, $this->partialName); |
|
133 | - $atts = $this->getDefaults($atts); |
|
134 | - array_walk($atts, function (&$value, $key) { |
|
135 | - $methodName = Helper::buildMethodName($key, 'normalize'); |
|
136 | - if (!method_exists($this, $methodName)) { |
|
137 | - return; |
|
138 | - } |
|
139 | - $value = $this->$methodName($value); |
|
140 | - }); |
|
141 | - $this->setId($atts); |
|
142 | - return $atts; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @return array |
|
147 | - */ |
|
148 | - abstract protected function hideOptions(); |
|
149 | - |
|
150 | - /** |
|
151 | - * @param string $postId |
|
152 | - * @return int|string |
|
153 | - */ |
|
154 | - protected function normalizeAssignedTo($postId) |
|
155 | - { |
|
156 | - if ('parent_id' == $postId) { |
|
157 | - $postId = intval(wp_get_post_parent_id(intval(get_the_ID()))); |
|
158 | - } elseif ('post_id' == $postId) { |
|
159 | - $postId = intval(get_the_ID()); |
|
160 | - } |
|
161 | - return $postId; |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @param string $postId |
|
166 | - * @return int|string |
|
167 | - */ |
|
168 | - protected function normalizeAssignTo($postId) |
|
169 | - { |
|
170 | - return $this->normalizeAssignedTo($postId); |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @param string|array $hide |
|
175 | - * @return array |
|
176 | - */ |
|
177 | - protected function normalizeHide($hide) |
|
178 | - { |
|
179 | - if (is_string($hide)) { |
|
180 | - $hide = explode(',', $hide); |
|
181 | - } |
|
182 | - $hideKeys = array_keys($this->getHideOptions()); |
|
183 | - return array_filter(array_map('trim', $hide), function ($value) use ($hideKeys) { |
|
184 | - return in_array($value, $hideKeys); |
|
185 | - }); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * @param string $id |
|
190 | - * @return string |
|
191 | - */ |
|
192 | - protected function normalizeId($id) |
|
193 | - { |
|
194 | - return sanitize_title($id); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * @param string $labels |
|
199 | - * @return array |
|
200 | - */ |
|
201 | - protected function normalizeLabels($labels) |
|
202 | - { |
|
203 | - $defaults = [ |
|
204 | - __('Excellent', 'site-reviews'), |
|
205 | - __('Very good', 'site-reviews'), |
|
206 | - __('Average', 'site-reviews'), |
|
207 | - __('Poor', 'site-reviews'), |
|
208 | - __('Terrible', 'site-reviews'), |
|
209 | - ]; |
|
210 | - $maxRating = (int) glsr()->constant('MAX_RATING', Rating::class); |
|
211 | - $defaults = array_pad(array_slice($defaults, 0, $maxRating), $maxRating, ''); |
|
212 | - $labels = array_map('trim', explode(',', $labels)); |
|
213 | - foreach ($defaults as $i => $label) { |
|
214 | - if (empty($labels[$i])) { |
|
215 | - continue; |
|
216 | - } |
|
217 | - $defaults[$i] = $labels[$i]; |
|
218 | - } |
|
219 | - return array_combine(range($maxRating, 1), $defaults); |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * @param string $schema |
|
224 | - * @return bool |
|
225 | - */ |
|
226 | - protected function normalizeSchema($schema) |
|
227 | - { |
|
228 | - return wp_validate_boolean($schema); |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * @param string $text |
|
233 | - * @return string |
|
234 | - */ |
|
235 | - protected function normalizeText($text) |
|
236 | - { |
|
237 | - return trim($text); |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * @return void |
|
242 | - */ |
|
243 | - protected function setId(array &$atts) |
|
244 | - { |
|
245 | - if (empty($atts['id'])) { |
|
246 | - $atts['id'] = Application::PREFIX.substr(md5(serialize($atts)), 0, 8); |
|
247 | - } |
|
248 | - } |
|
15 | + /** |
|
16 | + * @var string |
|
17 | + */ |
|
18 | + protected $partialName; |
|
19 | + |
|
20 | + /** |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + protected $shortcodeName; |
|
24 | + |
|
25 | + public function __construct() |
|
26 | + { |
|
27 | + $this->partialName = $this->getShortcodePartialName(); |
|
28 | + $this->shortcodeName = $this->getShortcodeName(); |
|
29 | + } |
|
30 | + |
|
31 | + /** |
|
32 | + * @param string|array $atts |
|
33 | + * @param string $type |
|
34 | + * @return string |
|
35 | + */ |
|
36 | + public function build($atts, array $args = [], $type = 'shortcode') |
|
37 | + { |
|
38 | + $args = $this->normalizeArgs($args, $type); |
|
39 | + $atts = $this->normalizeAtts($atts, $type); |
|
40 | + $partial = glsr(Partial::class)->build($this->partialName, $atts); |
|
41 | + $title = !empty($atts['title']) |
|
42 | + ? $args['before_title'].$atts['title'].$args['after_title'] |
|
43 | + : ''; |
|
44 | + $debug = sprintf('<glsr-%1$s hidden data-atts=\'%2$s\'></glsr-%1$s>', $type, $atts['json']); |
|
45 | + return $args['before_widget'].$title.$partial.$debug.$args['after_widget']; |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * @param string|array $atts |
|
50 | + * @return string |
|
51 | + */ |
|
52 | + public function buildShortcode($atts = []) |
|
53 | + { |
|
54 | + return $this->build($atts); |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @return array |
|
59 | + */ |
|
60 | + public function getDefaults($atts) |
|
61 | + { |
|
62 | + return glsr($this->getShortcodeDefaultsClassName())->restrict(wp_parse_args($atts)); |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * @return array |
|
67 | + */ |
|
68 | + public function getHideOptions() |
|
69 | + { |
|
70 | + $options = $this->hideOptions(); |
|
71 | + return apply_filters('site-reviews/shortcode/hide-options', $options, $this->shortcodeName); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * @return string |
|
76 | + */ |
|
77 | + public function getShortcodeClassName($replace = '', $search = 'Shortcode') |
|
78 | + { |
|
79 | + return str_replace($search, $replace, (new ReflectionClass($this))->getShortName()); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @return string |
|
84 | + */ |
|
85 | + public function getShortcodeDefaultsClassName() |
|
86 | + { |
|
87 | + return Helper::buildClassName( |
|
88 | + $this->getShortcodeClassName('Defaults'), |
|
89 | + 'Defaults' |
|
90 | + ); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @return string |
|
95 | + */ |
|
96 | + public function getShortcodeName() |
|
97 | + { |
|
98 | + return Str::snakeCase($this->getShortcodeClassName()); |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @return string |
|
103 | + */ |
|
104 | + public function getShortcodePartialName() |
|
105 | + { |
|
106 | + return Str::dashCase($this->getShortcodeClassName()); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @param array|string $args |
|
111 | + * @param string $type |
|
112 | + * @return array |
|
113 | + */ |
|
114 | + public function normalizeArgs($args, $type = 'shortcode') |
|
115 | + { |
|
116 | + $args = wp_parse_args($args, [ |
|
117 | + 'before_widget' => '<div class="glsr-'.$type.' '.$type.'-'.$this->partialName.'">', |
|
118 | + 'after_widget' => '</div>', |
|
119 | + 'before_title' => '<h3 class="glsr-'.$type.'-title">', |
|
120 | + 'after_title' => '</h3>', |
|
121 | + ]); |
|
122 | + return apply_filters('site-reviews/shortcode/args', $args, $type, $this->partialName); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @param array|string $atts |
|
127 | + * @param string $type |
|
128 | + * @return array |
|
129 | + */ |
|
130 | + public function normalizeAtts($atts, $type = 'shortcode') |
|
131 | + { |
|
132 | + $atts = apply_filters('site-reviews/shortcode/atts', $atts, $type, $this->partialName); |
|
133 | + $atts = $this->getDefaults($atts); |
|
134 | + array_walk($atts, function (&$value, $key) { |
|
135 | + $methodName = Helper::buildMethodName($key, 'normalize'); |
|
136 | + if (!method_exists($this, $methodName)) { |
|
137 | + return; |
|
138 | + } |
|
139 | + $value = $this->$methodName($value); |
|
140 | + }); |
|
141 | + $this->setId($atts); |
|
142 | + return $atts; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @return array |
|
147 | + */ |
|
148 | + abstract protected function hideOptions(); |
|
149 | + |
|
150 | + /** |
|
151 | + * @param string $postId |
|
152 | + * @return int|string |
|
153 | + */ |
|
154 | + protected function normalizeAssignedTo($postId) |
|
155 | + { |
|
156 | + if ('parent_id' == $postId) { |
|
157 | + $postId = intval(wp_get_post_parent_id(intval(get_the_ID()))); |
|
158 | + } elseif ('post_id' == $postId) { |
|
159 | + $postId = intval(get_the_ID()); |
|
160 | + } |
|
161 | + return $postId; |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @param string $postId |
|
166 | + * @return int|string |
|
167 | + */ |
|
168 | + protected function normalizeAssignTo($postId) |
|
169 | + { |
|
170 | + return $this->normalizeAssignedTo($postId); |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @param string|array $hide |
|
175 | + * @return array |
|
176 | + */ |
|
177 | + protected function normalizeHide($hide) |
|
178 | + { |
|
179 | + if (is_string($hide)) { |
|
180 | + $hide = explode(',', $hide); |
|
181 | + } |
|
182 | + $hideKeys = array_keys($this->getHideOptions()); |
|
183 | + return array_filter(array_map('trim', $hide), function ($value) use ($hideKeys) { |
|
184 | + return in_array($value, $hideKeys); |
|
185 | + }); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * @param string $id |
|
190 | + * @return string |
|
191 | + */ |
|
192 | + protected function normalizeId($id) |
|
193 | + { |
|
194 | + return sanitize_title($id); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * @param string $labels |
|
199 | + * @return array |
|
200 | + */ |
|
201 | + protected function normalizeLabels($labels) |
|
202 | + { |
|
203 | + $defaults = [ |
|
204 | + __('Excellent', 'site-reviews'), |
|
205 | + __('Very good', 'site-reviews'), |
|
206 | + __('Average', 'site-reviews'), |
|
207 | + __('Poor', 'site-reviews'), |
|
208 | + __('Terrible', 'site-reviews'), |
|
209 | + ]; |
|
210 | + $maxRating = (int) glsr()->constant('MAX_RATING', Rating::class); |
|
211 | + $defaults = array_pad(array_slice($defaults, 0, $maxRating), $maxRating, ''); |
|
212 | + $labels = array_map('trim', explode(',', $labels)); |
|
213 | + foreach ($defaults as $i => $label) { |
|
214 | + if (empty($labels[$i])) { |
|
215 | + continue; |
|
216 | + } |
|
217 | + $defaults[$i] = $labels[$i]; |
|
218 | + } |
|
219 | + return array_combine(range($maxRating, 1), $defaults); |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * @param string $schema |
|
224 | + * @return bool |
|
225 | + */ |
|
226 | + protected function normalizeSchema($schema) |
|
227 | + { |
|
228 | + return wp_validate_boolean($schema); |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * @param string $text |
|
233 | + * @return string |
|
234 | + */ |
|
235 | + protected function normalizeText($text) |
|
236 | + { |
|
237 | + return trim($text); |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * @return void |
|
242 | + */ |
|
243 | + protected function setId(array &$atts) |
|
244 | + { |
|
245 | + if (empty($atts['id'])) { |
|
246 | + $atts['id'] = Application::PREFIX.substr(md5(serialize($atts)), 0, 8); |
|
247 | + } |
|
248 | + } |
|
249 | 249 | } |
@@ -10,197 +10,197 @@ |
||
10 | 10 | |
11 | 11 | class SiteReviewsSummary |
12 | 12 | { |
13 | - /** |
|
14 | - * @var array |
|
15 | - */ |
|
16 | - protected $args; |
|
17 | - |
|
18 | - /** |
|
19 | - * @var float |
|
20 | - */ |
|
21 | - protected $averageRating; |
|
22 | - |
|
23 | - /** |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - protected $ratingCounts; |
|
27 | - |
|
28 | - /** |
|
29 | - * @return void|string |
|
30 | - */ |
|
31 | - public function build(array $args = []) |
|
32 | - { |
|
33 | - $this->args = $args; |
|
34 | - $this->ratingCounts = glsr(ReviewManager::class)->getRatingCounts($args); |
|
35 | - if (!array_sum($this->ratingCounts) && $this->isHidden('if_empty')) { |
|
36 | - return; |
|
37 | - } |
|
38 | - $this->averageRating = glsr(Rating::class)->getAverage($this->ratingCounts); |
|
39 | - $this->generateSchema(); |
|
40 | - return glsr(Template::class)->build('templates/reviews-summary', [ |
|
41 | - 'context' => [ |
|
42 | - 'assigned_to' => $this->args['assigned_to'], |
|
43 | - 'category' => $this->args['category'], |
|
44 | - 'class' => $this->getClass(), |
|
45 | - 'id' => $this->args['id'], |
|
46 | - 'percentages' => $this->buildPercentage(), |
|
47 | - 'rating' => $this->buildRating(), |
|
48 | - 'stars' => $this->buildStars(), |
|
49 | - 'text' => $this->buildText(), |
|
50 | - ], |
|
51 | - ]); |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @return void|string |
|
56 | - */ |
|
57 | - protected function buildPercentage() |
|
58 | - { |
|
59 | - if ($this->isHidden('bars')) { |
|
60 | - return; |
|
61 | - } |
|
62 | - $percentages = preg_filter('/$/', '%', glsr(Rating::class)->getPercentages($this->ratingCounts)); |
|
63 | - $bars = array_reduce(range(glsr()->constant('MAX_RATING', Rating::class), 1), function ($carry, $level) use ($percentages) { |
|
64 | - $label = $this->buildPercentageLabel($this->args['labels'][$level]); |
|
65 | - $background = $this->buildPercentageBackground($percentages[$level]); |
|
66 | - $count = apply_filters('site-reviews/summary/counts', |
|
67 | - $percentages[$level], |
|
68 | - $this->ratingCounts[$level] |
|
69 | - ); |
|
70 | - $percent = $this->buildPercentageCount($count); |
|
71 | - $value = $label.$background.$percent; |
|
72 | - $value = apply_filters('site-reviews/summary/wrap/bar', $value, $this->args, [ |
|
73 | - 'percent' => wp_strip_all_tags($count, true), |
|
74 | - 'rating' => $level, |
|
75 | - ]); |
|
76 | - return $carry.glsr(Builder::class)->div($value, [ |
|
77 | - 'class' => 'glsr-bar', |
|
78 | - ]); |
|
79 | - }); |
|
80 | - return $this->wrap('percentage', $bars); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * @param string $percent |
|
85 | - * @return string |
|
86 | - */ |
|
87 | - protected function buildPercentageBackground($percent) |
|
88 | - { |
|
89 | - $backgroundPercent = glsr(Builder::class)->span([ |
|
90 | - 'class' => 'glsr-bar-background-percent', |
|
91 | - 'style' => 'width:'.$percent, |
|
92 | - ]); |
|
93 | - return '<span class="glsr-bar-background">'.$backgroundPercent.'</span>'; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @param string $count |
|
98 | - * @return string |
|
99 | - */ |
|
100 | - protected function buildPercentageCount($count) |
|
101 | - { |
|
102 | - return '<span class="glsr-bar-percent">'.$count.'</span>'; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @param string $label |
|
107 | - * @return string |
|
108 | - */ |
|
109 | - protected function buildPercentageLabel($label) |
|
110 | - { |
|
111 | - return '<span class="glsr-bar-label">'.$label.'</span>'; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @return void|string |
|
116 | - */ |
|
117 | - protected function buildRating() |
|
118 | - { |
|
119 | - if ($this->isHidden('rating')) { |
|
120 | - return; |
|
121 | - } |
|
122 | - return $this->wrap('rating', '<span>'.$this->averageRating.'</span>'); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @return void|string |
|
127 | - */ |
|
128 | - protected function buildStars() |
|
129 | - { |
|
130 | - if ($this->isHidden('stars')) { |
|
131 | - return; |
|
132 | - } |
|
133 | - $stars = glsr_star_rating($this->averageRating); |
|
134 | - return $this->wrap('stars', $stars); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @return void|string |
|
139 | - */ |
|
140 | - protected function buildText() |
|
141 | - { |
|
142 | - if ($this->isHidden('summary')) { |
|
143 | - return; |
|
144 | - } |
|
145 | - $count = intval(array_sum($this->ratingCounts)); |
|
146 | - if (empty($this->args['text'])) { |
|
147 | - // @todo document this change |
|
148 | - $this->args['text'] = _nx( |
|
149 | - '{rating} out of {max} stars (based on {num} review)', |
|
150 | - '{rating} out of {max} stars (based on {num} reviews)', |
|
151 | - $count, |
|
152 | - 'Do not translate {rating}, {max}, and {num}, they are template tags.', |
|
153 | - 'site-reviews' |
|
154 | - ); |
|
155 | - } |
|
156 | - $summary = str_replace( |
|
157 | - ['{rating}', '{max}', '{num}'], |
|
158 | - [$this->averageRating, glsr()->constant('MAX_RATING', Rating::class), $count], |
|
159 | - $this->args['text'] |
|
160 | - ); |
|
161 | - return $this->wrap('text', '<span>'.$summary.'</span>'); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @return void |
|
166 | - */ |
|
167 | - protected function generateSchema() |
|
168 | - { |
|
169 | - if (!wp_validate_boolean($this->args['schema'])) { |
|
170 | - return; |
|
171 | - } |
|
172 | - glsr(Schema::class)->store( |
|
173 | - glsr(Schema::class)->buildSummary($this->args) |
|
174 | - ); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * @return string |
|
179 | - */ |
|
180 | - protected function getClass() |
|
181 | - { |
|
182 | - return trim('glsr-summary glsr-default '.$this->args['class']); |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * @param string $key |
|
187 | - * @return bool |
|
188 | - */ |
|
189 | - protected function isHidden($key) |
|
190 | - { |
|
191 | - return in_array($key, $this->args['hide']); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * @param string $key |
|
196 | - * @param string $value |
|
197 | - * @return string |
|
198 | - */ |
|
199 | - protected function wrap($key, $value) |
|
200 | - { |
|
201 | - $value = apply_filters('site-reviews/summary/wrap/'.$key, $value, $this->args); |
|
202 | - return glsr(Builder::class)->div($value, [ |
|
203 | - 'class' => 'glsr-summary-'.$key, |
|
204 | - ]); |
|
205 | - } |
|
13 | + /** |
|
14 | + * @var array |
|
15 | + */ |
|
16 | + protected $args; |
|
17 | + |
|
18 | + /** |
|
19 | + * @var float |
|
20 | + */ |
|
21 | + protected $averageRating; |
|
22 | + |
|
23 | + /** |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + protected $ratingCounts; |
|
27 | + |
|
28 | + /** |
|
29 | + * @return void|string |
|
30 | + */ |
|
31 | + public function build(array $args = []) |
|
32 | + { |
|
33 | + $this->args = $args; |
|
34 | + $this->ratingCounts = glsr(ReviewManager::class)->getRatingCounts($args); |
|
35 | + if (!array_sum($this->ratingCounts) && $this->isHidden('if_empty')) { |
|
36 | + return; |
|
37 | + } |
|
38 | + $this->averageRating = glsr(Rating::class)->getAverage($this->ratingCounts); |
|
39 | + $this->generateSchema(); |
|
40 | + return glsr(Template::class)->build('templates/reviews-summary', [ |
|
41 | + 'context' => [ |
|
42 | + 'assigned_to' => $this->args['assigned_to'], |
|
43 | + 'category' => $this->args['category'], |
|
44 | + 'class' => $this->getClass(), |
|
45 | + 'id' => $this->args['id'], |
|
46 | + 'percentages' => $this->buildPercentage(), |
|
47 | + 'rating' => $this->buildRating(), |
|
48 | + 'stars' => $this->buildStars(), |
|
49 | + 'text' => $this->buildText(), |
|
50 | + ], |
|
51 | + ]); |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @return void|string |
|
56 | + */ |
|
57 | + protected function buildPercentage() |
|
58 | + { |
|
59 | + if ($this->isHidden('bars')) { |
|
60 | + return; |
|
61 | + } |
|
62 | + $percentages = preg_filter('/$/', '%', glsr(Rating::class)->getPercentages($this->ratingCounts)); |
|
63 | + $bars = array_reduce(range(glsr()->constant('MAX_RATING', Rating::class), 1), function ($carry, $level) use ($percentages) { |
|
64 | + $label = $this->buildPercentageLabel($this->args['labels'][$level]); |
|
65 | + $background = $this->buildPercentageBackground($percentages[$level]); |
|
66 | + $count = apply_filters('site-reviews/summary/counts', |
|
67 | + $percentages[$level], |
|
68 | + $this->ratingCounts[$level] |
|
69 | + ); |
|
70 | + $percent = $this->buildPercentageCount($count); |
|
71 | + $value = $label.$background.$percent; |
|
72 | + $value = apply_filters('site-reviews/summary/wrap/bar', $value, $this->args, [ |
|
73 | + 'percent' => wp_strip_all_tags($count, true), |
|
74 | + 'rating' => $level, |
|
75 | + ]); |
|
76 | + return $carry.glsr(Builder::class)->div($value, [ |
|
77 | + 'class' => 'glsr-bar', |
|
78 | + ]); |
|
79 | + }); |
|
80 | + return $this->wrap('percentage', $bars); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * @param string $percent |
|
85 | + * @return string |
|
86 | + */ |
|
87 | + protected function buildPercentageBackground($percent) |
|
88 | + { |
|
89 | + $backgroundPercent = glsr(Builder::class)->span([ |
|
90 | + 'class' => 'glsr-bar-background-percent', |
|
91 | + 'style' => 'width:'.$percent, |
|
92 | + ]); |
|
93 | + return '<span class="glsr-bar-background">'.$backgroundPercent.'</span>'; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @param string $count |
|
98 | + * @return string |
|
99 | + */ |
|
100 | + protected function buildPercentageCount($count) |
|
101 | + { |
|
102 | + return '<span class="glsr-bar-percent">'.$count.'</span>'; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @param string $label |
|
107 | + * @return string |
|
108 | + */ |
|
109 | + protected function buildPercentageLabel($label) |
|
110 | + { |
|
111 | + return '<span class="glsr-bar-label">'.$label.'</span>'; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @return void|string |
|
116 | + */ |
|
117 | + protected function buildRating() |
|
118 | + { |
|
119 | + if ($this->isHidden('rating')) { |
|
120 | + return; |
|
121 | + } |
|
122 | + return $this->wrap('rating', '<span>'.$this->averageRating.'</span>'); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @return void|string |
|
127 | + */ |
|
128 | + protected function buildStars() |
|
129 | + { |
|
130 | + if ($this->isHidden('stars')) { |
|
131 | + return; |
|
132 | + } |
|
133 | + $stars = glsr_star_rating($this->averageRating); |
|
134 | + return $this->wrap('stars', $stars); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @return void|string |
|
139 | + */ |
|
140 | + protected function buildText() |
|
141 | + { |
|
142 | + if ($this->isHidden('summary')) { |
|
143 | + return; |
|
144 | + } |
|
145 | + $count = intval(array_sum($this->ratingCounts)); |
|
146 | + if (empty($this->args['text'])) { |
|
147 | + // @todo document this change |
|
148 | + $this->args['text'] = _nx( |
|
149 | + '{rating} out of {max} stars (based on {num} review)', |
|
150 | + '{rating} out of {max} stars (based on {num} reviews)', |
|
151 | + $count, |
|
152 | + 'Do not translate {rating}, {max}, and {num}, they are template tags.', |
|
153 | + 'site-reviews' |
|
154 | + ); |
|
155 | + } |
|
156 | + $summary = str_replace( |
|
157 | + ['{rating}', '{max}', '{num}'], |
|
158 | + [$this->averageRating, glsr()->constant('MAX_RATING', Rating::class), $count], |
|
159 | + $this->args['text'] |
|
160 | + ); |
|
161 | + return $this->wrap('text', '<span>'.$summary.'</span>'); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @return void |
|
166 | + */ |
|
167 | + protected function generateSchema() |
|
168 | + { |
|
169 | + if (!wp_validate_boolean($this->args['schema'])) { |
|
170 | + return; |
|
171 | + } |
|
172 | + glsr(Schema::class)->store( |
|
173 | + glsr(Schema::class)->buildSummary($this->args) |
|
174 | + ); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * @return string |
|
179 | + */ |
|
180 | + protected function getClass() |
|
181 | + { |
|
182 | + return trim('glsr-summary glsr-default '.$this->args['class']); |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * @param string $key |
|
187 | + * @return bool |
|
188 | + */ |
|
189 | + protected function isHidden($key) |
|
190 | + { |
|
191 | + return in_array($key, $this->args['hide']); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * @param string $key |
|
196 | + * @param string $value |
|
197 | + * @return string |
|
198 | + */ |
|
199 | + protected function wrap($key, $value) |
|
200 | + { |
|
201 | + $value = apply_filters('site-reviews/summary/wrap/'.$key, $value, $this->args); |
|
202 | + return glsr(Builder::class)->div($value, [ |
|
203 | + 'class' => 'glsr-summary-'.$key, |
|
204 | + ]); |
|
205 | + } |
|
206 | 206 | } |
@@ -7,86 +7,86 @@ |
||
7 | 7 | |
8 | 8 | class Template |
9 | 9 | { |
10 | - /** |
|
11 | - * @param string $templatePath |
|
12 | - * @return void|string |
|
13 | - */ |
|
14 | - public function build($templatePath, array $data = []) |
|
15 | - { |
|
16 | - $data = $this->normalize($data); |
|
17 | - $path = Str::removePrefix('templates/', $templatePath); |
|
18 | - $template = glsr()->build($templatePath, $data); |
|
19 | - $template = apply_filters('site-reviews/build/template/'.$path, $template, $data); |
|
20 | - $template = $this->interpolate($template, $data, $path); |
|
21 | - $template = apply_filters('site-reviews/rendered/template', $template, $templatePath, $data); |
|
22 | - $template = apply_filters('site-reviews/rendered/template/'.$path, $template, $data); |
|
23 | - return $template; |
|
24 | - } |
|
10 | + /** |
|
11 | + * @param string $templatePath |
|
12 | + * @return void|string |
|
13 | + */ |
|
14 | + public function build($templatePath, array $data = []) |
|
15 | + { |
|
16 | + $data = $this->normalize($data); |
|
17 | + $path = Str::removePrefix('templates/', $templatePath); |
|
18 | + $template = glsr()->build($templatePath, $data); |
|
19 | + $template = apply_filters('site-reviews/build/template/'.$path, $template, $data); |
|
20 | + $template = $this->interpolate($template, $data, $path); |
|
21 | + $template = apply_filters('site-reviews/rendered/template', $template, $templatePath, $data); |
|
22 | + $template = apply_filters('site-reviews/rendered/template/'.$path, $template, $data); |
|
23 | + return $template; |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Interpolate context values into template placeholders. |
|
28 | - * @param string $template |
|
29 | - * @param string $templatePath |
|
30 | - * @return string |
|
31 | - */ |
|
32 | - public function interpolate($template, array $data = [], $templatePath) |
|
33 | - { |
|
34 | - $context = $this->normalizeContext(Arr::get($data, 'context', [])); |
|
35 | - $context = apply_filters('site-reviews/interpolate/'.$templatePath, $context, $template, $data); |
|
36 | - return $this->interpolateContext($template, $context); |
|
37 | - } |
|
26 | + /** |
|
27 | + * Interpolate context values into template placeholders. |
|
28 | + * @param string $template |
|
29 | + * @param string $templatePath |
|
30 | + * @return string |
|
31 | + */ |
|
32 | + public function interpolate($template, array $data = [], $templatePath) |
|
33 | + { |
|
34 | + $context = $this->normalizeContext(Arr::get($data, 'context', [])); |
|
35 | + $context = apply_filters('site-reviews/interpolate/'.$templatePath, $context, $template, $data); |
|
36 | + return $this->interpolateContext($template, $context); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Interpolate context values into template placeholders. |
|
41 | - * @param string $text |
|
42 | - * @return string |
|
43 | - */ |
|
44 | - public function interpolateContext($text, array $context = []) |
|
45 | - { |
|
46 | - foreach ($context as $key => $value) { |
|
47 | - $text = strtr( |
|
48 | - $text, |
|
49 | - array_fill_keys(['{'.$key.'}', '{{ '.$key.' }}'], $value) |
|
50 | - ); |
|
51 | - } |
|
52 | - return trim($text); |
|
53 | - } |
|
39 | + /** |
|
40 | + * Interpolate context values into template placeholders. |
|
41 | + * @param string $text |
|
42 | + * @return string |
|
43 | + */ |
|
44 | + public function interpolateContext($text, array $context = []) |
|
45 | + { |
|
46 | + foreach ($context as $key => $value) { |
|
47 | + $text = strtr( |
|
48 | + $text, |
|
49 | + array_fill_keys(['{'.$key.'}', '{{ '.$key.' }}'], $value) |
|
50 | + ); |
|
51 | + } |
|
52 | + return trim($text); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @param string $templatePath |
|
57 | - * @return void|string |
|
58 | - */ |
|
59 | - public function render($templatePath, array $data = []) |
|
60 | - { |
|
61 | - echo $this->build($templatePath, $data); |
|
62 | - } |
|
55 | + /** |
|
56 | + * @param string $templatePath |
|
57 | + * @return void|string |
|
58 | + */ |
|
59 | + public function render($templatePath, array $data = []) |
|
60 | + { |
|
61 | + echo $this->build($templatePath, $data); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @return array |
|
66 | - */ |
|
67 | - protected function normalize(array $data) |
|
68 | - { |
|
69 | - $arrayKeys = ['context', 'globals']; |
|
70 | - $data = wp_parse_args($data, array_fill_keys($arrayKeys, [])); |
|
71 | - foreach ($arrayKeys as $key) { |
|
72 | - if (is_array($data[$key])) { |
|
73 | - continue; |
|
74 | - } |
|
75 | - $data[$key] = []; |
|
76 | - } |
|
77 | - return $data; |
|
78 | - } |
|
64 | + /** |
|
65 | + * @return array |
|
66 | + */ |
|
67 | + protected function normalize(array $data) |
|
68 | + { |
|
69 | + $arrayKeys = ['context', 'globals']; |
|
70 | + $data = wp_parse_args($data, array_fill_keys($arrayKeys, [])); |
|
71 | + foreach ($arrayKeys as $key) { |
|
72 | + if (is_array($data[$key])) { |
|
73 | + continue; |
|
74 | + } |
|
75 | + $data[$key] = []; |
|
76 | + } |
|
77 | + return $data; |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * @return array |
|
82 | - */ |
|
83 | - protected function normalizeContext(array $context) |
|
84 | - { |
|
85 | - $context = array_filter($context, function ($value) { |
|
86 | - return !is_array($value) && !is_object($value); |
|
87 | - }); |
|
88 | - return array_map(function ($value) { |
|
89 | - return (string) $value; |
|
90 | - }, $context); |
|
91 | - } |
|
80 | + /** |
|
81 | + * @return array |
|
82 | + */ |
|
83 | + protected function normalizeContext(array $context) |
|
84 | + { |
|
85 | + $context = array_filter($context, function ($value) { |
|
86 | + return !is_array($value) && !is_object($value); |
|
87 | + }); |
|
88 | + return array_map(function ($value) { |
|
89 | + return (string) $value; |
|
90 | + }, $context); |
|
91 | + } |
|
92 | 92 | } |
@@ -4,215 +4,215 @@ |
||
4 | 4 | |
5 | 5 | class Arr |
6 | 6 | { |
7 | - /** |
|
8 | - * @return bool |
|
9 | - */ |
|
10 | - public static function compareArrays(array $arr1, array $arr2) |
|
11 | - { |
|
12 | - sort($arr1); |
|
13 | - sort($arr2); |
|
14 | - return $arr1 == $arr2; |
|
15 | - } |
|
16 | - |
|
17 | - /** |
|
18 | - * @param mixed $array |
|
19 | - * @return array |
|
20 | - */ |
|
21 | - public static function consolidateArray($array) |
|
22 | - { |
|
23 | - return is_array($array) || is_object($array) |
|
24 | - ? (array) $array |
|
25 | - : []; |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * @return array |
|
30 | - */ |
|
31 | - public static function convertDotNotationArray(array $array) |
|
32 | - { |
|
33 | - $results = []; |
|
34 | - foreach ($array as $path => $value) { |
|
35 | - $results = static::set($results, $path, $value); |
|
36 | - } |
|
37 | - return $results; |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * @param string $string |
|
42 | - * @param mixed $callback |
|
43 | - * @return array |
|
44 | - */ |
|
45 | - public static function convertStringToArray($string, $callback = null) |
|
46 | - { |
|
47 | - $array = array_map('trim', explode(',', $string)); |
|
48 | - return $callback |
|
49 | - ? array_filter($array, $callback) |
|
50 | - : array_filter($array); |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * @param bool $flattenValue |
|
55 | - * @param string $prefix |
|
56 | - * @return array |
|
57 | - */ |
|
58 | - public static function flattenArray(array $array, $flattenValue = false, $prefix = '') |
|
59 | - { |
|
60 | - $result = []; |
|
61 | - foreach ($array as $key => $value) { |
|
62 | - $newKey = ltrim($prefix.'.'.$key, '.'); |
|
63 | - if (static::isIndexedFlatArray($value)) { |
|
64 | - if ($flattenValue) { |
|
65 | - $value = '['.implode(', ', $value).']'; |
|
66 | - } |
|
67 | - } elseif (is_array($value)) { |
|
68 | - $result = array_merge($result, static::flattenArray($value, $flattenValue, $newKey)); |
|
69 | - continue; |
|
70 | - } |
|
71 | - $result[$newKey] = $value; |
|
72 | - } |
|
73 | - return $result; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Get a value from an array of values using a dot-notation path as reference. |
|
78 | - * @param mixed $data |
|
79 | - * @param string $path |
|
80 | - * @param mixed $fallback |
|
81 | - * @return mixed |
|
82 | - */ |
|
83 | - public static function get($data, $path = '', $fallback = '') |
|
84 | - { |
|
85 | - $data = static::consolidateArray($data); |
|
86 | - $keys = explode('.', $path); |
|
87 | - foreach ($keys as $key) { |
|
88 | - if (!isset($data[$key])) { |
|
89 | - return $fallback; |
|
90 | - } |
|
91 | - $data = $data[$key]; |
|
92 | - } |
|
93 | - return $data; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @param string $key |
|
98 | - * @return array |
|
99 | - */ |
|
100 | - public static function insertAfter($key, array $array, array $insert) |
|
101 | - { |
|
102 | - return static::insertInArray($array, $insert, $key, 'after'); |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @param string $key |
|
107 | - * @return array |
|
108 | - */ |
|
109 | - public static function insertBefore($key, array $array, array $insert) |
|
110 | - { |
|
111 | - return static::insertInArray($array, $insert, $key, 'before'); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @param string $key |
|
116 | - * @param string $position |
|
117 | - * @return array |
|
118 | - */ |
|
119 | - public static function insertInArray(array $array, array $insert, $key, $position = 'before') |
|
120 | - { |
|
121 | - $keyPosition = intval(array_search($key, array_keys($array))); |
|
122 | - if ('after' == $position) { |
|
123 | - ++$keyPosition; |
|
124 | - } |
|
125 | - if (false !== $keyPosition) { |
|
126 | - $result = array_slice($array, 0, $keyPosition); |
|
127 | - $result = array_merge($result, $insert); |
|
128 | - return array_merge($result, array_slice($array, $keyPosition)); |
|
129 | - } |
|
130 | - return array_merge($array, $insert); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * @param mixed $array |
|
135 | - * @return bool |
|
136 | - */ |
|
137 | - public static function isIndexedFlatArray($array) |
|
138 | - { |
|
139 | - if (!is_array($array) || array_filter($array, 'is_array')) { |
|
140 | - return false; |
|
141 | - } |
|
142 | - return wp_is_numeric_array($array); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @param bool $prefixed |
|
147 | - * @return array |
|
148 | - */ |
|
149 | - public static function prefixArrayKeys(array $values, $prefixed = true) |
|
150 | - { |
|
151 | - $trim = '_'; |
|
152 | - $prefix = $prefixed |
|
153 | - ? $trim |
|
154 | - : ''; |
|
155 | - $prefixed = []; |
|
156 | - foreach ($values as $key => $value) { |
|
157 | - $key = trim($key); |
|
158 | - if (0 === strpos($key, $trim)) { |
|
159 | - $key = substr($key, strlen($trim)); |
|
160 | - } |
|
161 | - $prefixed[$prefix.$key] = $value; |
|
162 | - } |
|
163 | - return $prefixed; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @return array |
|
168 | - */ |
|
169 | - public static function removeEmptyArrayValues(array $array) |
|
170 | - { |
|
171 | - $result = []; |
|
172 | - foreach ($array as $key => $value) { |
|
173 | - if (!$value) { |
|
174 | - continue; |
|
175 | - } |
|
176 | - $result[$key] = is_array($value) |
|
177 | - ? static::removeEmptyArrayValues($value) |
|
178 | - : $value; |
|
179 | - } |
|
180 | - return $result; |
|
181 | - } |
|
182 | - |
|
183 | - |
|
184 | - /** |
|
185 | - * Set a value to an array of values using a dot-notation path as reference. |
|
186 | - * @param string $path |
|
187 | - * @param mixed $value |
|
188 | - * @return array |
|
189 | - */ |
|
190 | - public static function set(array $data, $path, $value) |
|
191 | - { |
|
192 | - $token = strtok($path, '.'); |
|
193 | - $ref = &$data; |
|
194 | - while (false !== $token) { |
|
195 | - $ref = static::consolidateArray($ref); |
|
196 | - $ref = &$ref[$token]; |
|
197 | - $token = strtok('.'); |
|
198 | - } |
|
199 | - $ref = $value; |
|
200 | - return $data; |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * @return array |
|
205 | - */ |
|
206 | - public static function unique(array $values) |
|
207 | - { |
|
208 | - return array_filter(array_unique($values)); |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * @return array |
|
213 | - */ |
|
214 | - public static function unprefixArrayKeys(array $values) |
|
215 | - { |
|
216 | - return static::prefixArrayKeys($values, false); |
|
217 | - } |
|
7 | + /** |
|
8 | + * @return bool |
|
9 | + */ |
|
10 | + public static function compareArrays(array $arr1, array $arr2) |
|
11 | + { |
|
12 | + sort($arr1); |
|
13 | + sort($arr2); |
|
14 | + return $arr1 == $arr2; |
|
15 | + } |
|
16 | + |
|
17 | + /** |
|
18 | + * @param mixed $array |
|
19 | + * @return array |
|
20 | + */ |
|
21 | + public static function consolidateArray($array) |
|
22 | + { |
|
23 | + return is_array($array) || is_object($array) |
|
24 | + ? (array) $array |
|
25 | + : []; |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * @return array |
|
30 | + */ |
|
31 | + public static function convertDotNotationArray(array $array) |
|
32 | + { |
|
33 | + $results = []; |
|
34 | + foreach ($array as $path => $value) { |
|
35 | + $results = static::set($results, $path, $value); |
|
36 | + } |
|
37 | + return $results; |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * @param string $string |
|
42 | + * @param mixed $callback |
|
43 | + * @return array |
|
44 | + */ |
|
45 | + public static function convertStringToArray($string, $callback = null) |
|
46 | + { |
|
47 | + $array = array_map('trim', explode(',', $string)); |
|
48 | + return $callback |
|
49 | + ? array_filter($array, $callback) |
|
50 | + : array_filter($array); |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * @param bool $flattenValue |
|
55 | + * @param string $prefix |
|
56 | + * @return array |
|
57 | + */ |
|
58 | + public static function flattenArray(array $array, $flattenValue = false, $prefix = '') |
|
59 | + { |
|
60 | + $result = []; |
|
61 | + foreach ($array as $key => $value) { |
|
62 | + $newKey = ltrim($prefix.'.'.$key, '.'); |
|
63 | + if (static::isIndexedFlatArray($value)) { |
|
64 | + if ($flattenValue) { |
|
65 | + $value = '['.implode(', ', $value).']'; |
|
66 | + } |
|
67 | + } elseif (is_array($value)) { |
|
68 | + $result = array_merge($result, static::flattenArray($value, $flattenValue, $newKey)); |
|
69 | + continue; |
|
70 | + } |
|
71 | + $result[$newKey] = $value; |
|
72 | + } |
|
73 | + return $result; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Get a value from an array of values using a dot-notation path as reference. |
|
78 | + * @param mixed $data |
|
79 | + * @param string $path |
|
80 | + * @param mixed $fallback |
|
81 | + * @return mixed |
|
82 | + */ |
|
83 | + public static function get($data, $path = '', $fallback = '') |
|
84 | + { |
|
85 | + $data = static::consolidateArray($data); |
|
86 | + $keys = explode('.', $path); |
|
87 | + foreach ($keys as $key) { |
|
88 | + if (!isset($data[$key])) { |
|
89 | + return $fallback; |
|
90 | + } |
|
91 | + $data = $data[$key]; |
|
92 | + } |
|
93 | + return $data; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @param string $key |
|
98 | + * @return array |
|
99 | + */ |
|
100 | + public static function insertAfter($key, array $array, array $insert) |
|
101 | + { |
|
102 | + return static::insertInArray($array, $insert, $key, 'after'); |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @param string $key |
|
107 | + * @return array |
|
108 | + */ |
|
109 | + public static function insertBefore($key, array $array, array $insert) |
|
110 | + { |
|
111 | + return static::insertInArray($array, $insert, $key, 'before'); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @param string $key |
|
116 | + * @param string $position |
|
117 | + * @return array |
|
118 | + */ |
|
119 | + public static function insertInArray(array $array, array $insert, $key, $position = 'before') |
|
120 | + { |
|
121 | + $keyPosition = intval(array_search($key, array_keys($array))); |
|
122 | + if ('after' == $position) { |
|
123 | + ++$keyPosition; |
|
124 | + } |
|
125 | + if (false !== $keyPosition) { |
|
126 | + $result = array_slice($array, 0, $keyPosition); |
|
127 | + $result = array_merge($result, $insert); |
|
128 | + return array_merge($result, array_slice($array, $keyPosition)); |
|
129 | + } |
|
130 | + return array_merge($array, $insert); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * @param mixed $array |
|
135 | + * @return bool |
|
136 | + */ |
|
137 | + public static function isIndexedFlatArray($array) |
|
138 | + { |
|
139 | + if (!is_array($array) || array_filter($array, 'is_array')) { |
|
140 | + return false; |
|
141 | + } |
|
142 | + return wp_is_numeric_array($array); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @param bool $prefixed |
|
147 | + * @return array |
|
148 | + */ |
|
149 | + public static function prefixArrayKeys(array $values, $prefixed = true) |
|
150 | + { |
|
151 | + $trim = '_'; |
|
152 | + $prefix = $prefixed |
|
153 | + ? $trim |
|
154 | + : ''; |
|
155 | + $prefixed = []; |
|
156 | + foreach ($values as $key => $value) { |
|
157 | + $key = trim($key); |
|
158 | + if (0 === strpos($key, $trim)) { |
|
159 | + $key = substr($key, strlen($trim)); |
|
160 | + } |
|
161 | + $prefixed[$prefix.$key] = $value; |
|
162 | + } |
|
163 | + return $prefixed; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @return array |
|
168 | + */ |
|
169 | + public static function removeEmptyArrayValues(array $array) |
|
170 | + { |
|
171 | + $result = []; |
|
172 | + foreach ($array as $key => $value) { |
|
173 | + if (!$value) { |
|
174 | + continue; |
|
175 | + } |
|
176 | + $result[$key] = is_array($value) |
|
177 | + ? static::removeEmptyArrayValues($value) |
|
178 | + : $value; |
|
179 | + } |
|
180 | + return $result; |
|
181 | + } |
|
182 | + |
|
183 | + |
|
184 | + /** |
|
185 | + * Set a value to an array of values using a dot-notation path as reference. |
|
186 | + * @param string $path |
|
187 | + * @param mixed $value |
|
188 | + * @return array |
|
189 | + */ |
|
190 | + public static function set(array $data, $path, $value) |
|
191 | + { |
|
192 | + $token = strtok($path, '.'); |
|
193 | + $ref = &$data; |
|
194 | + while (false !== $token) { |
|
195 | + $ref = static::consolidateArray($ref); |
|
196 | + $ref = &$ref[$token]; |
|
197 | + $token = strtok('.'); |
|
198 | + } |
|
199 | + $ref = $value; |
|
200 | + return $data; |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * @return array |
|
205 | + */ |
|
206 | + public static function unique(array $values) |
|
207 | + { |
|
208 | + return array_filter(array_unique($values)); |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * @return array |
|
213 | + */ |
|
214 | + public static function unprefixArrayKeys(array $values) |
|
215 | + { |
|
216 | + return static::prefixArrayKeys($values, false); |
|
217 | + } |
|
218 | 218 | } |
@@ -9,149 +9,149 @@ |
||
9 | 9 | |
10 | 10 | class EnqueuePublicAssets |
11 | 11 | { |
12 | - /** |
|
13 | - * @return void |
|
14 | - */ |
|
15 | - public function handle() |
|
16 | - { |
|
17 | - $this->enqueueAssets(); |
|
18 | - $this->enqueuePolyfillService(); |
|
19 | - $this->enqueueRecaptchaScript(); |
|
20 | - $this->inlineScript(); |
|
21 | - $this->inlineStyles(); |
|
22 | - } |
|
12 | + /** |
|
13 | + * @return void |
|
14 | + */ |
|
15 | + public function handle() |
|
16 | + { |
|
17 | + $this->enqueueAssets(); |
|
18 | + $this->enqueuePolyfillService(); |
|
19 | + $this->enqueueRecaptchaScript(); |
|
20 | + $this->inlineScript(); |
|
21 | + $this->inlineStyles(); |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * @return void |
|
26 | - */ |
|
27 | - public function enqueueAssets() |
|
28 | - { |
|
29 | - if (apply_filters('site-reviews/assets/css', true)) { |
|
30 | - wp_enqueue_style( |
|
31 | - Application::ID, |
|
32 | - $this->getStylesheet(), |
|
33 | - [], |
|
34 | - glsr()->version |
|
35 | - ); |
|
36 | - } |
|
37 | - if (apply_filters('site-reviews/assets/js', true)) { |
|
38 | - $dependencies = apply_filters('site-reviews/assets/polyfill', true) |
|
39 | - ? [Application::ID.'/polyfill'] |
|
40 | - : []; |
|
41 | - $dependencies = apply_filters('site-reviews/enqueue/public/dependencies', $dependencies); |
|
42 | - wp_enqueue_script( |
|
43 | - Application::ID, |
|
44 | - glsr()->url('assets/scripts/'.Application::ID.'.js'), |
|
45 | - $dependencies, |
|
46 | - glsr()->version, |
|
47 | - true |
|
48 | - ); |
|
49 | - } |
|
50 | - } |
|
24 | + /** |
|
25 | + * @return void |
|
26 | + */ |
|
27 | + public function enqueueAssets() |
|
28 | + { |
|
29 | + if (apply_filters('site-reviews/assets/css', true)) { |
|
30 | + wp_enqueue_style( |
|
31 | + Application::ID, |
|
32 | + $this->getStylesheet(), |
|
33 | + [], |
|
34 | + glsr()->version |
|
35 | + ); |
|
36 | + } |
|
37 | + if (apply_filters('site-reviews/assets/js', true)) { |
|
38 | + $dependencies = apply_filters('site-reviews/assets/polyfill', true) |
|
39 | + ? [Application::ID.'/polyfill'] |
|
40 | + : []; |
|
41 | + $dependencies = apply_filters('site-reviews/enqueue/public/dependencies', $dependencies); |
|
42 | + wp_enqueue_script( |
|
43 | + Application::ID, |
|
44 | + glsr()->url('assets/scripts/'.Application::ID.'.js'), |
|
45 | + $dependencies, |
|
46 | + glsr()->version, |
|
47 | + true |
|
48 | + ); |
|
49 | + } |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function enqueuePolyfillService() |
|
56 | - { |
|
57 | - if (!apply_filters('site-reviews/assets/polyfill', true)) { |
|
58 | - return; |
|
59 | - } |
|
60 | - wp_enqueue_script(Application::ID.'/polyfill', add_query_arg([ |
|
61 | - 'features' => 'Array.prototype.findIndex,CustomEvent,Element.prototype.closest,Element.prototype.dataset,Event,XMLHttpRequest,MutationObserver', |
|
62 | - 'flags' => 'gated', |
|
63 | - ], 'https://polyfill.io/v3/polyfill.min.js')); |
|
64 | - } |
|
52 | + /** |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function enqueuePolyfillService() |
|
56 | + { |
|
57 | + if (!apply_filters('site-reviews/assets/polyfill', true)) { |
|
58 | + return; |
|
59 | + } |
|
60 | + wp_enqueue_script(Application::ID.'/polyfill', add_query_arg([ |
|
61 | + 'features' => 'Array.prototype.findIndex,CustomEvent,Element.prototype.closest,Element.prototype.dataset,Event,XMLHttpRequest,MutationObserver', |
|
62 | + 'flags' => 'gated', |
|
63 | + ], 'https://polyfill.io/v3/polyfill.min.js')); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @return void |
|
68 | - */ |
|
69 | - public function enqueueRecaptchaScript() |
|
70 | - { |
|
71 | - // wpforms-recaptcha |
|
72 | - // google-recaptcha |
|
73 | - // nf-google-recaptcha |
|
74 | - if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
75 | - return; |
|
76 | - } |
|
77 | - $language = apply_filters('site-reviews/recaptcha/language', get_locale()); |
|
78 | - wp_enqueue_script(Application::ID.'/google-recaptcha', add_query_arg([ |
|
79 | - 'hl' => $language, |
|
80 | - 'render' => 'explicit', |
|
81 | - ], 'https://www.google.com/recaptcha/api.js')); |
|
82 | - } |
|
66 | + /** |
|
67 | + * @return void |
|
68 | + */ |
|
69 | + public function enqueueRecaptchaScript() |
|
70 | + { |
|
71 | + // wpforms-recaptcha |
|
72 | + // google-recaptcha |
|
73 | + // nf-google-recaptcha |
|
74 | + if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
75 | + return; |
|
76 | + } |
|
77 | + $language = apply_filters('site-reviews/recaptcha/language', get_locale()); |
|
78 | + wp_enqueue_script(Application::ID.'/google-recaptcha', add_query_arg([ |
|
79 | + 'hl' => $language, |
|
80 | + 'render' => 'explicit', |
|
81 | + ], 'https://www.google.com/recaptcha/api.js')); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * @return void |
|
86 | - */ |
|
87 | - public function inlineScript() |
|
88 | - { |
|
89 | - $variables = [ |
|
90 | - 'action' => Application::PREFIX.'action', |
|
91 | - 'ajaxpagination' => $this->getFixedSelectorsForPagination(), |
|
92 | - 'ajaxurl' => admin_url('admin-ajax.php'), |
|
93 | - 'nameprefix' => Application::ID, |
|
94 | - 'urlparameter' => glsr(OptionManager::class)->getBool('settings.reviews.pagination.url_parameter'), |
|
95 | - 'validationconfig' => glsr(Style::class)->validation, |
|
96 | - 'validationstrings' => glsr(ValidationStringsDefaults::class)->defaults(), |
|
97 | - ]; |
|
98 | - $variables = apply_filters('site-reviews/enqueue/public/localize', $variables); |
|
99 | - wp_add_inline_script(Application::ID, $this->buildInlineScript($variables), 'before'); |
|
100 | - } |
|
84 | + /** |
|
85 | + * @return void |
|
86 | + */ |
|
87 | + public function inlineScript() |
|
88 | + { |
|
89 | + $variables = [ |
|
90 | + 'action' => Application::PREFIX.'action', |
|
91 | + 'ajaxpagination' => $this->getFixedSelectorsForPagination(), |
|
92 | + 'ajaxurl' => admin_url('admin-ajax.php'), |
|
93 | + 'nameprefix' => Application::ID, |
|
94 | + 'urlparameter' => glsr(OptionManager::class)->getBool('settings.reviews.pagination.url_parameter'), |
|
95 | + 'validationconfig' => glsr(Style::class)->validation, |
|
96 | + 'validationstrings' => glsr(ValidationStringsDefaults::class)->defaults(), |
|
97 | + ]; |
|
98 | + $variables = apply_filters('site-reviews/enqueue/public/localize', $variables); |
|
99 | + wp_add_inline_script(Application::ID, $this->buildInlineScript($variables), 'before'); |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * @return void |
|
104 | - */ |
|
105 | - public function inlineStyles() |
|
106 | - { |
|
107 | - $inlineStylesheetPath = glsr()->path('assets/styles/inline-styles.css'); |
|
108 | - if (!apply_filters('site-reviews/assets/css', true)) { |
|
109 | - return; |
|
110 | - } |
|
111 | - if (!file_exists($inlineStylesheetPath)) { |
|
112 | - glsr_log()->error('Inline stylesheet is missing: '.$inlineStylesheetPath); |
|
113 | - return; |
|
114 | - } |
|
115 | - $inlineStylesheetValues = glsr()->config('inline-styles'); |
|
116 | - $stylesheet = str_replace( |
|
117 | - array_keys($inlineStylesheetValues), |
|
118 | - array_values($inlineStylesheetValues), |
|
119 | - file_get_contents($inlineStylesheetPath) |
|
120 | - ); |
|
121 | - wp_add_inline_style(Application::ID, $stylesheet); |
|
122 | - } |
|
102 | + /** |
|
103 | + * @return void |
|
104 | + */ |
|
105 | + public function inlineStyles() |
|
106 | + { |
|
107 | + $inlineStylesheetPath = glsr()->path('assets/styles/inline-styles.css'); |
|
108 | + if (!apply_filters('site-reviews/assets/css', true)) { |
|
109 | + return; |
|
110 | + } |
|
111 | + if (!file_exists($inlineStylesheetPath)) { |
|
112 | + glsr_log()->error('Inline stylesheet is missing: '.$inlineStylesheetPath); |
|
113 | + return; |
|
114 | + } |
|
115 | + $inlineStylesheetValues = glsr()->config('inline-styles'); |
|
116 | + $stylesheet = str_replace( |
|
117 | + array_keys($inlineStylesheetValues), |
|
118 | + array_values($inlineStylesheetValues), |
|
119 | + file_get_contents($inlineStylesheetPath) |
|
120 | + ); |
|
121 | + wp_add_inline_style(Application::ID, $stylesheet); |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * @return string |
|
126 | - */ |
|
127 | - protected function buildInlineScript(array $variables) |
|
128 | - { |
|
129 | - $script = 'window.hasOwnProperty("GLSR")||(window.GLSR={});'; |
|
130 | - foreach ($variables as $key => $value) { |
|
131 | - $script.= sprintf('GLSR.%s=%s;', $key, json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); |
|
132 | - } |
|
133 | - $pattern = '/\"([^ \-\"]+)\"(:[{\[\"])/'; // removes unnecessary quotes surrounding object keys |
|
134 | - $optimizedScript = preg_replace($pattern, '$1$2', $script); |
|
135 | - return apply_filters('site-reviews/enqueue/public/inline-script', $optimizedScript, $script, $variables); |
|
136 | - } |
|
124 | + /** |
|
125 | + * @return string |
|
126 | + */ |
|
127 | + protected function buildInlineScript(array $variables) |
|
128 | + { |
|
129 | + $script = 'window.hasOwnProperty("GLSR")||(window.GLSR={});'; |
|
130 | + foreach ($variables as $key => $value) { |
|
131 | + $script.= sprintf('GLSR.%s=%s;', $key, json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); |
|
132 | + } |
|
133 | + $pattern = '/\"([^ \-\"]+)\"(:[{\[\"])/'; // removes unnecessary quotes surrounding object keys |
|
134 | + $optimizedScript = preg_replace($pattern, '$1$2', $script); |
|
135 | + return apply_filters('site-reviews/enqueue/public/inline-script', $optimizedScript, $script, $variables); |
|
136 | + } |
|
137 | 137 | |
138 | - /** |
|
139 | - * @return array |
|
140 | - */ |
|
141 | - protected function getFixedSelectorsForPagination() |
|
142 | - { |
|
143 | - $selectors = ['#wpadminbar', '.site-navigation-fixed']; |
|
144 | - return apply_filters('site-reviews/enqueue/public/localize/ajax-pagination', $selectors); |
|
145 | - } |
|
138 | + /** |
|
139 | + * @return array |
|
140 | + */ |
|
141 | + protected function getFixedSelectorsForPagination() |
|
142 | + { |
|
143 | + $selectors = ['#wpadminbar', '.site-navigation-fixed']; |
|
144 | + return apply_filters('site-reviews/enqueue/public/localize/ajax-pagination', $selectors); |
|
145 | + } |
|
146 | 146 | |
147 | - /** |
|
148 | - * @return string |
|
149 | - */ |
|
150 | - protected function getStylesheet() |
|
151 | - { |
|
152 | - $currentStyle = glsr(Style::class)->style; |
|
153 | - return file_exists(glsr()->path('assets/styles/custom/'.$currentStyle.'.css')) |
|
154 | - ? glsr()->url('assets/styles/custom/'.$currentStyle.'.css') |
|
155 | - : glsr()->url('assets/styles/'.Application::ID.'.css'); |
|
156 | - } |
|
147 | + /** |
|
148 | + * @return string |
|
149 | + */ |
|
150 | + protected function getStylesheet() |
|
151 | + { |
|
152 | + $currentStyle = glsr(Style::class)->style; |
|
153 | + return file_exists(glsr()->path('assets/styles/custom/'.$currentStyle.'.css')) |
|
154 | + ? glsr()->url('assets/styles/custom/'.$currentStyle.'.css') |
|
155 | + : glsr()->url('assets/styles/'.Application::ID.'.css'); |
|
156 | + } |
|
157 | 157 | } |
@@ -1,16 +1,16 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | return [ |
4 | - 'fields' => [ |
|
5 | - 'input' => 'input', |
|
6 | - 'input_checkbox' => 'input', |
|
7 | - 'input_radio' => 'input', |
|
8 | - 'label' => 'et_pb_contact_form_label', |
|
9 | - 'select' => 'et_pb_contact_select input', |
|
10 | - 'textarea' => 'et_pb_contact_message input', |
|
11 | - ], |
|
12 | - 'validation' => [ |
|
13 | - 'field_error_class' => 'et_contact_error', |
|
14 | - 'input_error_class' => 'et_contact_error', |
|
15 | - ], |
|
4 | + 'fields' => [ |
|
5 | + 'input' => 'input', |
|
6 | + 'input_checkbox' => 'input', |
|
7 | + 'input_radio' => 'input', |
|
8 | + 'label' => 'et_pb_contact_form_label', |
|
9 | + 'select' => 'et_pb_contact_select input', |
|
10 | + 'textarea' => 'et_pb_contact_message input', |
|
11 | + ], |
|
12 | + 'validation' => [ |
|
13 | + 'field_error_class' => 'et_contact_error', |
|
14 | + 'input_error_class' => 'et_contact_error', |
|
15 | + ], |
|
16 | 16 | ]; |
@@ -12,163 +12,163 @@ |
||
12 | 12 | |
13 | 13 | class SettingsController extends Controller |
14 | 14 | { |
15 | - /** |
|
16 | - * @param mixed $input |
|
17 | - * @return array |
|
18 | - * @callback register_setting |
|
19 | - */ |
|
20 | - public function callbackRegisterSettings($input) |
|
21 | - { |
|
22 | - $settings = Arr::consolidateArray($input); |
|
23 | - if (1 === count($settings) && array_key_exists('settings', $settings)) { |
|
24 | - $options = array_replace_recursive(glsr(OptionManager::class)->all(), $input); |
|
25 | - $options = $this->sanitizeGeneral($input, $options); |
|
26 | - $options = $this->sanitizeLicenses($input, $options); |
|
27 | - $options = $this->sanitizeSubmissions($input, $options); |
|
28 | - $options = $this->sanitizeTranslations($input, $options); |
|
29 | - $options = apply_filters('site-reviews/settings/callback', $options, $settings); |
|
30 | - if (filter_input(INPUT_POST, 'option_page') == Application::ID.'-settings') { |
|
31 | - glsr(Notice::class)->addSuccess(__('Settings updated.', 'site-reviews')); |
|
32 | - } |
|
33 | - return $options; |
|
34 | - } |
|
35 | - return $input; |
|
36 | - } |
|
15 | + /** |
|
16 | + * @param mixed $input |
|
17 | + * @return array |
|
18 | + * @callback register_setting |
|
19 | + */ |
|
20 | + public function callbackRegisterSettings($input) |
|
21 | + { |
|
22 | + $settings = Arr::consolidateArray($input); |
|
23 | + if (1 === count($settings) && array_key_exists('settings', $settings)) { |
|
24 | + $options = array_replace_recursive(glsr(OptionManager::class)->all(), $input); |
|
25 | + $options = $this->sanitizeGeneral($input, $options); |
|
26 | + $options = $this->sanitizeLicenses($input, $options); |
|
27 | + $options = $this->sanitizeSubmissions($input, $options); |
|
28 | + $options = $this->sanitizeTranslations($input, $options); |
|
29 | + $options = apply_filters('site-reviews/settings/callback', $options, $settings); |
|
30 | + if (filter_input(INPUT_POST, 'option_page') == Application::ID.'-settings') { |
|
31 | + glsr(Notice::class)->addSuccess(__('Settings updated.', 'site-reviews')); |
|
32 | + } |
|
33 | + return $options; |
|
34 | + } |
|
35 | + return $input; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * @return void |
|
40 | - * @action admin_init |
|
41 | - */ |
|
42 | - public function registerSettings() |
|
43 | - { |
|
44 | - register_setting(Application::ID.'-settings', OptionManager::databaseKey(), [ |
|
45 | - 'sanitize_callback' => [$this, 'callbackRegisterSettings'], |
|
46 | - ]); |
|
47 | - } |
|
38 | + /** |
|
39 | + * @return void |
|
40 | + * @action admin_init |
|
41 | + */ |
|
42 | + public function registerSettings() |
|
43 | + { |
|
44 | + register_setting(Application::ID.'-settings', OptionManager::databaseKey(), [ |
|
45 | + 'sanitize_callback' => [$this, 'callbackRegisterSettings'], |
|
46 | + ]); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @return array |
|
51 | - */ |
|
52 | - protected function sanitizeGeneral(array $input, array $options) |
|
53 | - { |
|
54 | - $key = 'settings.general'; |
|
55 | - $inputForm = Arr::get($input, $key); |
|
56 | - if (!$this->hasMultilingualIntegration(Arr::get($inputForm, 'multilingual'))) { |
|
57 | - $options = Arr::set($options, $key.'.multilingual', ''); |
|
58 | - } |
|
59 | - if ('' == trim(Arr::get($inputForm, 'notification_message'))) { |
|
60 | - $defaultValue = Arr::get(glsr()->defaults, $key.'.notification_message'); |
|
61 | - $options = Arr::set($options, $key.'.notification_message', $defaultValue); |
|
62 | - } |
|
63 | - $defaultValue = Arr::get($inputForm, 'notifications', []); |
|
64 | - $options = Arr::set($options, $key.'.notifications', $defaultValue); |
|
65 | - return $options; |
|
66 | - } |
|
49 | + /** |
|
50 | + * @return array |
|
51 | + */ |
|
52 | + protected function sanitizeGeneral(array $input, array $options) |
|
53 | + { |
|
54 | + $key = 'settings.general'; |
|
55 | + $inputForm = Arr::get($input, $key); |
|
56 | + if (!$this->hasMultilingualIntegration(Arr::get($inputForm, 'multilingual'))) { |
|
57 | + $options = Arr::set($options, $key.'.multilingual', ''); |
|
58 | + } |
|
59 | + if ('' == trim(Arr::get($inputForm, 'notification_message'))) { |
|
60 | + $defaultValue = Arr::get(glsr()->defaults, $key.'.notification_message'); |
|
61 | + $options = Arr::set($options, $key.'.notification_message', $defaultValue); |
|
62 | + } |
|
63 | + $defaultValue = Arr::get($inputForm, 'notifications', []); |
|
64 | + $options = Arr::set($options, $key.'.notifications', $defaultValue); |
|
65 | + return $options; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @return array |
|
70 | - */ |
|
71 | - protected function sanitizeLicenses(array $input, array $options) |
|
72 | - { |
|
73 | - $key = 'settings.licenses'; |
|
74 | - $licenses = Arr::consolidateArray(Arr::get($input, $key)); |
|
75 | - foreach ($licenses as $slug => &$license) { |
|
76 | - if (empty($license)) { |
|
77 | - continue; |
|
78 | - } |
|
79 | - $license = $this->verifyLicense($license, $slug); |
|
80 | - } |
|
81 | - $options = Arr::set($options, $key, $licenses); |
|
82 | - return $options; |
|
83 | - } |
|
68 | + /** |
|
69 | + * @return array |
|
70 | + */ |
|
71 | + protected function sanitizeLicenses(array $input, array $options) |
|
72 | + { |
|
73 | + $key = 'settings.licenses'; |
|
74 | + $licenses = Arr::consolidateArray(Arr::get($input, $key)); |
|
75 | + foreach ($licenses as $slug => &$license) { |
|
76 | + if (empty($license)) { |
|
77 | + continue; |
|
78 | + } |
|
79 | + $license = $this->verifyLicense($license, $slug); |
|
80 | + } |
|
81 | + $options = Arr::set($options, $key, $licenses); |
|
82 | + return $options; |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * @return array |
|
87 | - */ |
|
88 | - protected function sanitizeSubmissions(array $input, array $options) |
|
89 | - { |
|
90 | - $key = 'settings.submissions'; |
|
91 | - $inputForm = Arr::get($input, $key); |
|
92 | - $defaultValue = isset($inputForm['required']) |
|
93 | - ? $inputForm['required'] |
|
94 | - : []; |
|
95 | - $options = Arr::set($options, $key.'.required', $defaultValue); |
|
96 | - return $options; |
|
97 | - } |
|
85 | + /** |
|
86 | + * @return array |
|
87 | + */ |
|
88 | + protected function sanitizeSubmissions(array $input, array $options) |
|
89 | + { |
|
90 | + $key = 'settings.submissions'; |
|
91 | + $inputForm = Arr::get($input, $key); |
|
92 | + $defaultValue = isset($inputForm['required']) |
|
93 | + ? $inputForm['required'] |
|
94 | + : []; |
|
95 | + $options = Arr::set($options, $key.'.required', $defaultValue); |
|
96 | + return $options; |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * @return array |
|
101 | - */ |
|
102 | - protected function sanitizeTranslations(array $input, array $options) |
|
103 | - { |
|
104 | - $key = 'settings.strings'; |
|
105 | - $inputForm = Arr::consolidateArray(Arr::get($input, $key)); |
|
106 | - if (!empty($inputForm)) { |
|
107 | - $options = Arr::set($options, $key, array_values(array_filter($inputForm))); |
|
108 | - $allowedTags = [ |
|
109 | - 'a' => ['class' => [], 'href' => [], 'target' => []], |
|
110 | - 'span' => ['class' => []], |
|
111 | - ]; |
|
112 | - array_walk($options['settings']['strings'], function (&$string) use ($allowedTags) { |
|
113 | - if (isset($string['s2'])) { |
|
114 | - $string['s2'] = wp_kses($string['s2'], $allowedTags); |
|
115 | - } |
|
116 | - if (isset($string['p2'])) { |
|
117 | - $string['p2'] = wp_kses($string['p2'], $allowedTags); |
|
118 | - } |
|
119 | - }); |
|
120 | - } |
|
121 | - return $options; |
|
122 | - } |
|
99 | + /** |
|
100 | + * @return array |
|
101 | + */ |
|
102 | + protected function sanitizeTranslations(array $input, array $options) |
|
103 | + { |
|
104 | + $key = 'settings.strings'; |
|
105 | + $inputForm = Arr::consolidateArray(Arr::get($input, $key)); |
|
106 | + if (!empty($inputForm)) { |
|
107 | + $options = Arr::set($options, $key, array_values(array_filter($inputForm))); |
|
108 | + $allowedTags = [ |
|
109 | + 'a' => ['class' => [], 'href' => [], 'target' => []], |
|
110 | + 'span' => ['class' => []], |
|
111 | + ]; |
|
112 | + array_walk($options['settings']['strings'], function (&$string) use ($allowedTags) { |
|
113 | + if (isset($string['s2'])) { |
|
114 | + $string['s2'] = wp_kses($string['s2'], $allowedTags); |
|
115 | + } |
|
116 | + if (isset($string['p2'])) { |
|
117 | + $string['p2'] = wp_kses($string['p2'], $allowedTags); |
|
118 | + } |
|
119 | + }); |
|
120 | + } |
|
121 | + return $options; |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * @param string $integrationSlug |
|
126 | - * @return bool |
|
127 | - */ |
|
128 | - protected function hasMultilingualIntegration($integrationSlug) |
|
129 | - { |
|
130 | - $integration = glsr(Multilingual::class)->getIntegration($integrationSlug); |
|
131 | - if (!$integration) { |
|
132 | - return false; |
|
133 | - } |
|
134 | - if (!$integration->isActive()) { |
|
135 | - glsr(Notice::class)->addError(sprintf( |
|
136 | - __('Please install/activate the %s plugin to enable integration.', 'site-reviews'), |
|
137 | - $integration->pluginName |
|
138 | - )); |
|
139 | - return false; |
|
140 | - } elseif (!$integration->isSupported()) { |
|
141 | - glsr(Notice::class)->addError(sprintf( |
|
142 | - __('Please update the %s plugin to v%s or greater to enable integration.', 'site-reviews'), |
|
143 | - $integration->pluginName, |
|
144 | - $integration->supportedVersion |
|
145 | - )); |
|
146 | - return false; |
|
147 | - } |
|
148 | - return true; |
|
149 | - } |
|
124 | + /** |
|
125 | + * @param string $integrationSlug |
|
126 | + * @return bool |
|
127 | + */ |
|
128 | + protected function hasMultilingualIntegration($integrationSlug) |
|
129 | + { |
|
130 | + $integration = glsr(Multilingual::class)->getIntegration($integrationSlug); |
|
131 | + if (!$integration) { |
|
132 | + return false; |
|
133 | + } |
|
134 | + if (!$integration->isActive()) { |
|
135 | + glsr(Notice::class)->addError(sprintf( |
|
136 | + __('Please install/activate the %s plugin to enable integration.', 'site-reviews'), |
|
137 | + $integration->pluginName |
|
138 | + )); |
|
139 | + return false; |
|
140 | + } elseif (!$integration->isSupported()) { |
|
141 | + glsr(Notice::class)->addError(sprintf( |
|
142 | + __('Please update the %s plugin to v%s or greater to enable integration.', 'site-reviews'), |
|
143 | + $integration->pluginName, |
|
144 | + $integration->supportedVersion |
|
145 | + )); |
|
146 | + return false; |
|
147 | + } |
|
148 | + return true; |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * @param string $license |
|
153 | - * @param string $slug |
|
154 | - * @return string |
|
155 | - */ |
|
156 | - protected function verifyLicense($license, $slug) |
|
157 | - { |
|
158 | - try { |
|
159 | - $addon = glsr($slug); |
|
160 | - $updater = new Updater($addon->update_url, $addon->file, [ |
|
161 | - 'license' => $license, |
|
162 | - 'testedTo' => $addon->testedTo, |
|
163 | - ]); |
|
164 | - if (!$updater->isLicenseValid()) { |
|
165 | - throw new Exception('Invalid license: '.$license.' ('.$addon->id.')'); |
|
166 | - } |
|
167 | - } catch (Exception $e) { |
|
168 | - $license = ''; |
|
169 | - glsr_log()->debug($e->getMessage()); |
|
170 | - glsr(Notice::class)->addError(__('A license you entered was invalid.', 'site-reviews')); |
|
171 | - } |
|
172 | - return $license; |
|
173 | - } |
|
151 | + /** |
|
152 | + * @param string $license |
|
153 | + * @param string $slug |
|
154 | + * @return string |
|
155 | + */ |
|
156 | + protected function verifyLicense($license, $slug) |
|
157 | + { |
|
158 | + try { |
|
159 | + $addon = glsr($slug); |
|
160 | + $updater = new Updater($addon->update_url, $addon->file, [ |
|
161 | + 'license' => $license, |
|
162 | + 'testedTo' => $addon->testedTo, |
|
163 | + ]); |
|
164 | + if (!$updater->isLicenseValid()) { |
|
165 | + throw new Exception('Invalid license: '.$license.' ('.$addon->id.')'); |
|
166 | + } |
|
167 | + } catch (Exception $e) { |
|
168 | + $license = ''; |
|
169 | + glsr_log()->debug($e->getMessage()); |
|
170 | + glsr(Notice::class)->addError(__('A license you entered was invalid.', 'site-reviews')); |
|
171 | + } |
|
172 | + return $license; |
|
173 | + } |
|
174 | 174 | } |
@@ -9,205 +9,205 @@ |
||
9 | 9 | |
10 | 10 | class Email |
11 | 11 | { |
12 | - /** |
|
13 | - * @var array |
|
14 | - */ |
|
15 | - public $attachments; |
|
16 | - |
|
17 | - /** |
|
18 | - * @var array |
|
19 | - */ |
|
20 | - public $email; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var array |
|
24 | - */ |
|
25 | - public $headers; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - public $message; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var string |
|
34 | - */ |
|
35 | - public $subject; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var string|array |
|
39 | - */ |
|
40 | - public $to; |
|
41 | - |
|
42 | - /** |
|
43 | - * @return Email |
|
44 | - */ |
|
45 | - public function compose(array $email) |
|
46 | - { |
|
47 | - $this->normalize($email); |
|
48 | - $this->attachments = $this->email['attachments']; |
|
49 | - $this->headers = $this->buildHeaders(); |
|
50 | - $this->message = $this->buildHtmlMessage(); |
|
51 | - $this->subject = $this->email['subject']; |
|
52 | - $this->to = $this->email['to']; |
|
53 | - add_action('phpmailer_init', [$this, 'buildPlainTextMessage']); |
|
54 | - return $this; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @param string $format |
|
59 | - * @return string|null |
|
60 | - */ |
|
61 | - public function read($format = '') |
|
62 | - { |
|
63 | - if ('plaintext' == $format) { |
|
64 | - $message = $this->stripHtmlTags($this->message); |
|
65 | - return apply_filters('site-reviews/email/message', $message, 'text', $this); |
|
66 | - } |
|
67 | - return $this->message; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @return void|bool |
|
72 | - */ |
|
73 | - public function send() |
|
74 | - { |
|
75 | - if (!$this->message || !$this->subject || !$this->to) { |
|
76 | - return; |
|
77 | - } |
|
78 | - add_action('wp_mail_failed', [$this, 'logMailError']); |
|
79 | - $sent = wp_mail( |
|
80 | - $this->to, |
|
81 | - $this->subject, |
|
82 | - $this->message, |
|
83 | - $this->headers, |
|
84 | - $this->attachments |
|
85 | - ); |
|
86 | - remove_action('wp_mail_failed', [$this, 'logMailError']); |
|
87 | - $this->reset(); |
|
88 | - return $sent; |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * @return void |
|
93 | - * @action phpmailer_init |
|
94 | - */ |
|
95 | - public function buildPlainTextMessage(PHPMailer $phpmailer) |
|
96 | - { |
|
97 | - if (empty($this->email)) { |
|
98 | - return; |
|
99 | - } |
|
100 | - if ('text/plain' === $phpmailer->ContentType || !empty($phpmailer->AltBody)) { |
|
101 | - return; |
|
102 | - } |
|
103 | - $message = $this->stripHtmlTags($phpmailer->Body); |
|
104 | - $phpmailer->AltBody = apply_filters('site-reviews/email/message', $message, 'text', $this); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * @return array |
|
109 | - */ |
|
110 | - protected function buildHeaders() |
|
111 | - { |
|
112 | - $allowed = [ |
|
113 | - 'bcc', 'cc', 'from', 'reply-to', |
|
114 | - ]; |
|
115 | - $headers = array_intersect_key($this->email, array_flip($allowed)); |
|
116 | - $headers = array_filter($headers); |
|
117 | - foreach ($headers as $key => $value) { |
|
118 | - unset($headers[$key]); |
|
119 | - $headers[] = $key.': '.$value; |
|
120 | - } |
|
121 | - $headers[] = 'Content-Type: text/html'; |
|
122 | - return apply_filters('site-reviews/email/headers', $headers, $this); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @return string |
|
127 | - */ |
|
128 | - protected function buildHtmlMessage() |
|
129 | - { |
|
130 | - $template = trim(glsr(OptionManager::class)->get('settings.general.notification_message')); |
|
131 | - if (!empty($template)) { |
|
132 | - $message = glsr(Template::class)->interpolate( |
|
133 | - $template, |
|
134 | - ['context' => $this->email['template-tags']], |
|
135 | - $this->email['template'] |
|
136 | - ); |
|
137 | - } elseif ($this->email['template']) { |
|
138 | - $message = glsr(Template::class)->build('templates/'.$this->email['template'], [ |
|
139 | - 'context' => $this->email['template-tags'], |
|
140 | - ]); |
|
141 | - } |
|
142 | - if (!isset($message)) { |
|
143 | - $message = $this->email['message']; |
|
144 | - } |
|
145 | - $message = $this->email['before'].$message.$this->email['after']; |
|
146 | - $message = strip_shortcodes($message); |
|
147 | - $message = wptexturize($message); |
|
148 | - $message = wpautop($message); |
|
149 | - $message = str_replace('<> ', '', $message); |
|
150 | - $message = str_replace(']]>', ']]>', $message); |
|
151 | - $message = glsr(Template::class)->build('partials/email/index', [ |
|
152 | - 'context' => ['message' => $message], |
|
153 | - ]); |
|
154 | - return apply_filters('site-reviews/email/message', stripslashes($message), 'html', $this); |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * @param \WP_Error $error |
|
159 | - * @return void |
|
160 | - */ |
|
161 | - protected function logMailError($error) |
|
162 | - { |
|
163 | - glsr_log()->error('Email was not sent (wp_mail failed)') |
|
164 | - ->debug($this) |
|
165 | - ->debug($error); |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * @return void |
|
170 | - */ |
|
171 | - protected function normalize(array $email = []) |
|
172 | - { |
|
173 | - $email = shortcode_atts(glsr(EmailDefaults::class)->defaults(), $email); |
|
174 | - if (empty($email['reply-to'])) { |
|
175 | - $email['reply-to'] = $email['from']; |
|
176 | - } |
|
177 | - $this->email = apply_filters('site-reviews/email/compose', $email, $this); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @return void |
|
182 | - */ |
|
183 | - protected function reset() |
|
184 | - { |
|
185 | - $this->attachments = []; |
|
186 | - $this->email = []; |
|
187 | - $this->headers = []; |
|
188 | - $this->message = null; |
|
189 | - $this->subject = null; |
|
190 | - $this->to = null; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * @return string |
|
195 | - */ |
|
196 | - protected function stripHtmlTags($string) |
|
197 | - { |
|
198 | - // remove invisible elements |
|
199 | - $string = preg_replace('@<(embed|head|noembed|noscript|object|script|style)[^>]*?>.*?</\\1>@siu', '', $string); |
|
200 | - // replace certain elements with a line-break |
|
201 | - $string = preg_replace('@</(div|h[1-9]|p|pre|tr)@iu', "\r\n\$0", $string); |
|
202 | - // replace other elements with a space |
|
203 | - $string = preg_replace('@</(td|th)@iu', ' $0', $string); |
|
204 | - // add a placeholder for plain-text bullets to list elements |
|
205 | - $string = preg_replace('@<(li)[^>]*?>@siu', '$0-o-^-o-', $string); |
|
206 | - // strip all remaining HTML tags |
|
207 | - $string = wp_strip_all_tags($string); |
|
208 | - $string = wp_specialchars_decode($string, ENT_QUOTES); |
|
209 | - $string = preg_replace('/\v(?:[\v\h]+){2,}/u', "\r\n\r\n", $string); |
|
210 | - $string = str_replace('-o-^-o-', ' - ', $string); |
|
211 | - return html_entity_decode($string, ENT_QUOTES, 'UTF-8'); |
|
212 | - } |
|
12 | + /** |
|
13 | + * @var array |
|
14 | + */ |
|
15 | + public $attachments; |
|
16 | + |
|
17 | + /** |
|
18 | + * @var array |
|
19 | + */ |
|
20 | + public $email; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var array |
|
24 | + */ |
|
25 | + public $headers; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + public $message; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var string |
|
34 | + */ |
|
35 | + public $subject; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var string|array |
|
39 | + */ |
|
40 | + public $to; |
|
41 | + |
|
42 | + /** |
|
43 | + * @return Email |
|
44 | + */ |
|
45 | + public function compose(array $email) |
|
46 | + { |
|
47 | + $this->normalize($email); |
|
48 | + $this->attachments = $this->email['attachments']; |
|
49 | + $this->headers = $this->buildHeaders(); |
|
50 | + $this->message = $this->buildHtmlMessage(); |
|
51 | + $this->subject = $this->email['subject']; |
|
52 | + $this->to = $this->email['to']; |
|
53 | + add_action('phpmailer_init', [$this, 'buildPlainTextMessage']); |
|
54 | + return $this; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @param string $format |
|
59 | + * @return string|null |
|
60 | + */ |
|
61 | + public function read($format = '') |
|
62 | + { |
|
63 | + if ('plaintext' == $format) { |
|
64 | + $message = $this->stripHtmlTags($this->message); |
|
65 | + return apply_filters('site-reviews/email/message', $message, 'text', $this); |
|
66 | + } |
|
67 | + return $this->message; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @return void|bool |
|
72 | + */ |
|
73 | + public function send() |
|
74 | + { |
|
75 | + if (!$this->message || !$this->subject || !$this->to) { |
|
76 | + return; |
|
77 | + } |
|
78 | + add_action('wp_mail_failed', [$this, 'logMailError']); |
|
79 | + $sent = wp_mail( |
|
80 | + $this->to, |
|
81 | + $this->subject, |
|
82 | + $this->message, |
|
83 | + $this->headers, |
|
84 | + $this->attachments |
|
85 | + ); |
|
86 | + remove_action('wp_mail_failed', [$this, 'logMailError']); |
|
87 | + $this->reset(); |
|
88 | + return $sent; |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * @return void |
|
93 | + * @action phpmailer_init |
|
94 | + */ |
|
95 | + public function buildPlainTextMessage(PHPMailer $phpmailer) |
|
96 | + { |
|
97 | + if (empty($this->email)) { |
|
98 | + return; |
|
99 | + } |
|
100 | + if ('text/plain' === $phpmailer->ContentType || !empty($phpmailer->AltBody)) { |
|
101 | + return; |
|
102 | + } |
|
103 | + $message = $this->stripHtmlTags($phpmailer->Body); |
|
104 | + $phpmailer->AltBody = apply_filters('site-reviews/email/message', $message, 'text', $this); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * @return array |
|
109 | + */ |
|
110 | + protected function buildHeaders() |
|
111 | + { |
|
112 | + $allowed = [ |
|
113 | + 'bcc', 'cc', 'from', 'reply-to', |
|
114 | + ]; |
|
115 | + $headers = array_intersect_key($this->email, array_flip($allowed)); |
|
116 | + $headers = array_filter($headers); |
|
117 | + foreach ($headers as $key => $value) { |
|
118 | + unset($headers[$key]); |
|
119 | + $headers[] = $key.': '.$value; |
|
120 | + } |
|
121 | + $headers[] = 'Content-Type: text/html'; |
|
122 | + return apply_filters('site-reviews/email/headers', $headers, $this); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @return string |
|
127 | + */ |
|
128 | + protected function buildHtmlMessage() |
|
129 | + { |
|
130 | + $template = trim(glsr(OptionManager::class)->get('settings.general.notification_message')); |
|
131 | + if (!empty($template)) { |
|
132 | + $message = glsr(Template::class)->interpolate( |
|
133 | + $template, |
|
134 | + ['context' => $this->email['template-tags']], |
|
135 | + $this->email['template'] |
|
136 | + ); |
|
137 | + } elseif ($this->email['template']) { |
|
138 | + $message = glsr(Template::class)->build('templates/'.$this->email['template'], [ |
|
139 | + 'context' => $this->email['template-tags'], |
|
140 | + ]); |
|
141 | + } |
|
142 | + if (!isset($message)) { |
|
143 | + $message = $this->email['message']; |
|
144 | + } |
|
145 | + $message = $this->email['before'].$message.$this->email['after']; |
|
146 | + $message = strip_shortcodes($message); |
|
147 | + $message = wptexturize($message); |
|
148 | + $message = wpautop($message); |
|
149 | + $message = str_replace('<> ', '', $message); |
|
150 | + $message = str_replace(']]>', ']]>', $message); |
|
151 | + $message = glsr(Template::class)->build('partials/email/index', [ |
|
152 | + 'context' => ['message' => $message], |
|
153 | + ]); |
|
154 | + return apply_filters('site-reviews/email/message', stripslashes($message), 'html', $this); |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * @param \WP_Error $error |
|
159 | + * @return void |
|
160 | + */ |
|
161 | + protected function logMailError($error) |
|
162 | + { |
|
163 | + glsr_log()->error('Email was not sent (wp_mail failed)') |
|
164 | + ->debug($this) |
|
165 | + ->debug($error); |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * @return void |
|
170 | + */ |
|
171 | + protected function normalize(array $email = []) |
|
172 | + { |
|
173 | + $email = shortcode_atts(glsr(EmailDefaults::class)->defaults(), $email); |
|
174 | + if (empty($email['reply-to'])) { |
|
175 | + $email['reply-to'] = $email['from']; |
|
176 | + } |
|
177 | + $this->email = apply_filters('site-reviews/email/compose', $email, $this); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @return void |
|
182 | + */ |
|
183 | + protected function reset() |
|
184 | + { |
|
185 | + $this->attachments = []; |
|
186 | + $this->email = []; |
|
187 | + $this->headers = []; |
|
188 | + $this->message = null; |
|
189 | + $this->subject = null; |
|
190 | + $this->to = null; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * @return string |
|
195 | + */ |
|
196 | + protected function stripHtmlTags($string) |
|
197 | + { |
|
198 | + // remove invisible elements |
|
199 | + $string = preg_replace('@<(embed|head|noembed|noscript|object|script|style)[^>]*?>.*?</\\1>@siu', '', $string); |
|
200 | + // replace certain elements with a line-break |
|
201 | + $string = preg_replace('@</(div|h[1-9]|p|pre|tr)@iu', "\r\n\$0", $string); |
|
202 | + // replace other elements with a space |
|
203 | + $string = preg_replace('@</(td|th)@iu', ' $0', $string); |
|
204 | + // add a placeholder for plain-text bullets to list elements |
|
205 | + $string = preg_replace('@<(li)[^>]*?>@siu', '$0-o-^-o-', $string); |
|
206 | + // strip all remaining HTML tags |
|
207 | + $string = wp_strip_all_tags($string); |
|
208 | + $string = wp_specialchars_decode($string, ENT_QUOTES); |
|
209 | + $string = preg_replace('/\v(?:[\v\h]+){2,}/u', "\r\n\r\n", $string); |
|
210 | + $string = str_replace('-o-^-o-', ' - ', $string); |
|
211 | + return html_entity_decode($string, ENT_QUOTES, 'UTF-8'); |
|
212 | + } |
|
213 | 213 | } |