@@ -13,288 +13,288 @@ |
||
13 | 13 | |
14 | 14 | class ValidateReview |
15 | 15 | { |
16 | - const RECAPTCHA_ENDPOINT = 'https://www.google.com/recaptcha/api/siteverify'; |
|
16 | + const RECAPTCHA_ENDPOINT = 'https://www.google.com/recaptcha/api/siteverify'; |
|
17 | 17 | |
18 | - const RECAPTCHA_DISABLED = 0; |
|
19 | - const RECAPTCHA_EMPTY = 1; |
|
20 | - const RECAPTCHA_FAILED = 2; |
|
21 | - const RECAPTCHA_INVALID = 3; |
|
22 | - const RECAPTCHA_VALID = 4; |
|
18 | + const RECAPTCHA_DISABLED = 0; |
|
19 | + const RECAPTCHA_EMPTY = 1; |
|
20 | + const RECAPTCHA_FAILED = 2; |
|
21 | + const RECAPTCHA_INVALID = 3; |
|
22 | + const RECAPTCHA_VALID = 4; |
|
23 | 23 | |
24 | - const VALIDATION_RULES = [ |
|
25 | - 'content' => 'required', |
|
26 | - 'email' => 'required|email', |
|
27 | - 'name' => 'required', |
|
28 | - 'rating' => 'required|number|between:1,5', |
|
29 | - 'terms' => 'accepted', |
|
30 | - 'title' => 'required', |
|
31 | - ]; |
|
24 | + const VALIDATION_RULES = [ |
|
25 | + 'content' => 'required', |
|
26 | + 'email' => 'required|email', |
|
27 | + 'name' => 'required', |
|
28 | + 'rating' => 'required|number|between:1,5', |
|
29 | + 'terms' => 'accepted', |
|
30 | + 'title' => 'required', |
|
31 | + ]; |
|
32 | 32 | |
33 | - /** |
|
34 | - * @var string|void |
|
35 | - */ |
|
36 | - public $error; |
|
33 | + /** |
|
34 | + * @var string|void |
|
35 | + */ |
|
36 | + public $error; |
|
37 | 37 | |
38 | - /** |
|
39 | - * @var string |
|
40 | - */ |
|
41 | - public $form_id; |
|
38 | + /** |
|
39 | + * @var string |
|
40 | + */ |
|
41 | + public $form_id; |
|
42 | 42 | |
43 | - /** |
|
44 | - * @var bool |
|
45 | - */ |
|
46 | - public $recaptchaIsUnset = false; |
|
43 | + /** |
|
44 | + * @var bool |
|
45 | + */ |
|
46 | + public $recaptchaIsUnset = false; |
|
47 | 47 | |
48 | - /** |
|
49 | - * @var array |
|
50 | - */ |
|
51 | - public $request; |
|
48 | + /** |
|
49 | + * @var array |
|
50 | + */ |
|
51 | + public $request; |
|
52 | 52 | |
53 | - /** |
|
54 | - * @var array |
|
55 | - */ |
|
56 | - protected $options; |
|
53 | + /** |
|
54 | + * @var array |
|
55 | + */ |
|
56 | + protected $options; |
|
57 | 57 | |
58 | - /** |
|
59 | - * @return static |
|
60 | - */ |
|
61 | - public function validate(array $request) |
|
62 | - { |
|
63 | - $this->form_id = $request['form_id']; |
|
64 | - $this->options = glsr(OptionManager::class)->all(); |
|
65 | - $this->request = $this->validateRequest($request); |
|
66 | - $this->validateCustom(); |
|
67 | - $this->validateHoneyPot(); |
|
68 | - $this->validateReviewLimits(); |
|
69 | - $this->validateBlacklist(); |
|
70 | - $this->validateAkismet(); |
|
71 | - $this->validateRecaptcha(); |
|
72 | - if (!empty($this->error)) { |
|
73 | - $this->setSessionValues('message', $this->error); |
|
74 | - } |
|
75 | - return $this; |
|
76 | - } |
|
58 | + /** |
|
59 | + * @return static |
|
60 | + */ |
|
61 | + public function validate(array $request) |
|
62 | + { |
|
63 | + $this->form_id = $request['form_id']; |
|
64 | + $this->options = glsr(OptionManager::class)->all(); |
|
65 | + $this->request = $this->validateRequest($request); |
|
66 | + $this->validateCustom(); |
|
67 | + $this->validateHoneyPot(); |
|
68 | + $this->validateReviewLimits(); |
|
69 | + $this->validateBlacklist(); |
|
70 | + $this->validateAkismet(); |
|
71 | + $this->validateRecaptcha(); |
|
72 | + if (!empty($this->error)) { |
|
73 | + $this->setSessionValues('message', $this->error); |
|
74 | + } |
|
75 | + return $this; |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @param string $path |
|
80 | - * @param mixed $fallback |
|
81 | - * @return mixed |
|
82 | - */ |
|
83 | - protected function getOption($path, $fallback = '') |
|
84 | - { |
|
85 | - return Arr::get($this->options, $path, $fallback); |
|
86 | - } |
|
78 | + /** |
|
79 | + * @param string $path |
|
80 | + * @param mixed $fallback |
|
81 | + * @return mixed |
|
82 | + */ |
|
83 | + protected function getOption($path, $fallback = '') |
|
84 | + { |
|
85 | + return Arr::get($this->options, $path, $fallback); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * @return int |
|
90 | - */ |
|
91 | - protected function getRecaptchaStatus() |
|
92 | - { |
|
93 | - if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
94 | - return static::RECAPTCHA_DISABLED; |
|
95 | - } |
|
96 | - if (empty($this->request['_recaptcha-token'])) { |
|
97 | - return $this->request['_counter'] < intval(apply_filters('site-reviews/recaptcha/timeout', 5)) |
|
98 | - ? static::RECAPTCHA_EMPTY |
|
99 | - : static::RECAPTCHA_FAILED; |
|
100 | - } |
|
101 | - return $this->getRecaptchaTokenStatus(); |
|
102 | - } |
|
88 | + /** |
|
89 | + * @return int |
|
90 | + */ |
|
91 | + protected function getRecaptchaStatus() |
|
92 | + { |
|
93 | + if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
94 | + return static::RECAPTCHA_DISABLED; |
|
95 | + } |
|
96 | + if (empty($this->request['_recaptcha-token'])) { |
|
97 | + return $this->request['_counter'] < intval(apply_filters('site-reviews/recaptcha/timeout', 5)) |
|
98 | + ? static::RECAPTCHA_EMPTY |
|
99 | + : static::RECAPTCHA_FAILED; |
|
100 | + } |
|
101 | + return $this->getRecaptchaTokenStatus(); |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * @return int |
|
106 | - */ |
|
107 | - protected function getRecaptchaTokenStatus() |
|
108 | - { |
|
109 | - $endpoint = add_query_arg([ |
|
110 | - 'remoteip' => Helper::getIpAddress(), |
|
111 | - 'response' => $this->request['_recaptcha-token'], |
|
112 | - 'secret' => $this->getOption('settings.submissions.recaptcha.secret'), |
|
113 | - ], static::RECAPTCHA_ENDPOINT); |
|
114 | - if (is_wp_error($response = wp_remote_get($endpoint))) { |
|
115 | - glsr_log()->error($response->get_error_message()); |
|
116 | - return static::RECAPTCHA_FAILED; |
|
117 | - } |
|
118 | - $response = json_decode(wp_remote_retrieve_body($response)); |
|
119 | - if (!empty($response->success)) { |
|
120 | - return boolval($response->success) |
|
121 | - ? static::RECAPTCHA_VALID |
|
122 | - : static::RECAPTCHA_INVALID; |
|
123 | - } |
|
124 | - foreach ($response->{'error-codes'} as $error) { |
|
125 | - glsr_log()->error('reCAPTCHA error: '.$error); |
|
126 | - } |
|
127 | - return static::RECAPTCHA_INVALID; |
|
128 | - } |
|
104 | + /** |
|
105 | + * @return int |
|
106 | + */ |
|
107 | + protected function getRecaptchaTokenStatus() |
|
108 | + { |
|
109 | + $endpoint = add_query_arg([ |
|
110 | + 'remoteip' => Helper::getIpAddress(), |
|
111 | + 'response' => $this->request['_recaptcha-token'], |
|
112 | + 'secret' => $this->getOption('settings.submissions.recaptcha.secret'), |
|
113 | + ], static::RECAPTCHA_ENDPOINT); |
|
114 | + if (is_wp_error($response = wp_remote_get($endpoint))) { |
|
115 | + glsr_log()->error($response->get_error_message()); |
|
116 | + return static::RECAPTCHA_FAILED; |
|
117 | + } |
|
118 | + $response = json_decode(wp_remote_retrieve_body($response)); |
|
119 | + if (!empty($response->success)) { |
|
120 | + return boolval($response->success) |
|
121 | + ? static::RECAPTCHA_VALID |
|
122 | + : static::RECAPTCHA_INVALID; |
|
123 | + } |
|
124 | + foreach ($response->{'error-codes'} as $error) { |
|
125 | + glsr_log()->error('reCAPTCHA error: '.$error); |
|
126 | + } |
|
127 | + return static::RECAPTCHA_INVALID; |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * @return array |
|
132 | - */ |
|
133 | - protected function getValidationRules(array $request) |
|
134 | - { |
|
135 | - $rules = array_intersect_key( |
|
136 | - apply_filters('site-reviews/validation/rules', static::VALIDATION_RULES, $request), |
|
137 | - array_flip($this->getOption('settings.submissions.required', [])) |
|
138 | - ); |
|
139 | - $excluded = explode(',', Arr::get($request, 'excluded')); |
|
140 | - return array_diff_key($rules, array_flip($excluded)); |
|
141 | - } |
|
130 | + /** |
|
131 | + * @return array |
|
132 | + */ |
|
133 | + protected function getValidationRules(array $request) |
|
134 | + { |
|
135 | + $rules = array_intersect_key( |
|
136 | + apply_filters('site-reviews/validation/rules', static::VALIDATION_RULES, $request), |
|
137 | + array_flip($this->getOption('settings.submissions.required', [])) |
|
138 | + ); |
|
139 | + $excluded = explode(',', Arr::get($request, 'excluded')); |
|
140 | + return array_diff_key($rules, array_flip($excluded)); |
|
141 | + } |
|
142 | 142 | |
143 | - /** |
|
144 | - * @return bool |
|
145 | - */ |
|
146 | - protected function isRequestValid(array $request) |
|
147 | - { |
|
148 | - $rules = $this->getValidationRules($request); |
|
149 | - $errors = glsr(Validator::class)->validate($request, $rules); |
|
150 | - if (empty($errors)) { |
|
151 | - return true; |
|
152 | - } |
|
153 | - $this->setSessionValues('errors', $errors); |
|
154 | - $this->setSessionValues('values', $request); |
|
155 | - return false; |
|
156 | - } |
|
143 | + /** |
|
144 | + * @return bool |
|
145 | + */ |
|
146 | + protected function isRequestValid(array $request) |
|
147 | + { |
|
148 | + $rules = $this->getValidationRules($request); |
|
149 | + $errors = glsr(Validator::class)->validate($request, $rules); |
|
150 | + if (empty($errors)) { |
|
151 | + return true; |
|
152 | + } |
|
153 | + $this->setSessionValues('errors', $errors); |
|
154 | + $this->setSessionValues('values', $request); |
|
155 | + return false; |
|
156 | + } |
|
157 | 157 | |
158 | - protected function setError($message, $loggedMessage = '') |
|
159 | - { |
|
160 | - $this->setSessionValues('errors', [], $loggedMessage); |
|
161 | - $this->error = $message; |
|
162 | - } |
|
158 | + protected function setError($message, $loggedMessage = '') |
|
159 | + { |
|
160 | + $this->setSessionValues('errors', [], $loggedMessage); |
|
161 | + $this->error = $message; |
|
162 | + } |
|
163 | 163 | |
164 | - /** |
|
165 | - * @param string $type |
|
166 | - * @param mixed $value |
|
167 | - * @param string $loggedMessage |
|
168 | - * @return void |
|
169 | - */ |
|
170 | - protected function setSessionValues($type, $value, $loggedMessage = '') |
|
171 | - { |
|
172 | - glsr()->sessionSet($this->form_id.$type, $value); |
|
173 | - if (!empty($loggedMessage)) { |
|
174 | - glsr_log()->warning($loggedMessage)->debug($this->request); |
|
175 | - } |
|
176 | - } |
|
164 | + /** |
|
165 | + * @param string $type |
|
166 | + * @param mixed $value |
|
167 | + * @param string $loggedMessage |
|
168 | + * @return void |
|
169 | + */ |
|
170 | + protected function setSessionValues($type, $value, $loggedMessage = '') |
|
171 | + { |
|
172 | + glsr()->sessionSet($this->form_id.$type, $value); |
|
173 | + if (!empty($loggedMessage)) { |
|
174 | + glsr_log()->warning($loggedMessage)->debug($this->request); |
|
175 | + } |
|
176 | + } |
|
177 | 177 | |
178 | - /** |
|
179 | - * @return void |
|
180 | - */ |
|
181 | - protected function validateAkismet() |
|
182 | - { |
|
183 | - if (!empty($this->error)) { |
|
184 | - return; |
|
185 | - } |
|
186 | - if (!glsr(Akismet::class)->isSpam($this->request)) { |
|
187 | - return; |
|
188 | - } |
|
189 | - $this->setError(__('This review has been flagged as possible spam and cannot be submitted.', 'site-reviews'), |
|
190 | - 'Akismet caught a spam submission (consider adding the IP address to the blacklist):' |
|
191 | - ); |
|
192 | - } |
|
178 | + /** |
|
179 | + * @return void |
|
180 | + */ |
|
181 | + protected function validateAkismet() |
|
182 | + { |
|
183 | + if (!empty($this->error)) { |
|
184 | + return; |
|
185 | + } |
|
186 | + if (!glsr(Akismet::class)->isSpam($this->request)) { |
|
187 | + return; |
|
188 | + } |
|
189 | + $this->setError(__('This review has been flagged as possible spam and cannot be submitted.', 'site-reviews'), |
|
190 | + 'Akismet caught a spam submission (consider adding the IP address to the blacklist):' |
|
191 | + ); |
|
192 | + } |
|
193 | 193 | |
194 | - /** |
|
195 | - * @return void |
|
196 | - */ |
|
197 | - protected function validateBlacklist() |
|
198 | - { |
|
199 | - if (!empty($this->error)) { |
|
200 | - return; |
|
201 | - } |
|
202 | - if (!glsr(Blacklist::class)->isBlacklisted($this->request)) { |
|
203 | - return; |
|
204 | - } |
|
205 | - $blacklistAction = $this->getOption('settings.submissions.blacklist.action'); |
|
206 | - if ('reject' != $blacklistAction) { |
|
207 | - $this->request['blacklisted'] = true; |
|
208 | - return; |
|
209 | - } |
|
210 | - $this->setError(__('Your review cannot be submitted at this time.', 'site-reviews'), |
|
211 | - 'Blacklisted submission detected:' |
|
212 | - ); |
|
213 | - } |
|
194 | + /** |
|
195 | + * @return void |
|
196 | + */ |
|
197 | + protected function validateBlacklist() |
|
198 | + { |
|
199 | + if (!empty($this->error)) { |
|
200 | + return; |
|
201 | + } |
|
202 | + if (!glsr(Blacklist::class)->isBlacklisted($this->request)) { |
|
203 | + return; |
|
204 | + } |
|
205 | + $blacklistAction = $this->getOption('settings.submissions.blacklist.action'); |
|
206 | + if ('reject' != $blacklistAction) { |
|
207 | + $this->request['blacklisted'] = true; |
|
208 | + return; |
|
209 | + } |
|
210 | + $this->setError(__('Your review cannot be submitted at this time.', 'site-reviews'), |
|
211 | + 'Blacklisted submission detected:' |
|
212 | + ); |
|
213 | + } |
|
214 | 214 | |
215 | - /** |
|
216 | - * @return void |
|
217 | - */ |
|
218 | - protected function validateCustom() |
|
219 | - { |
|
220 | - if (!empty($this->error)) { |
|
221 | - return; |
|
222 | - } |
|
223 | - $validated = apply_filters('site-reviews/validate/custom', true, $this->request); |
|
224 | - if (true === $validated) { |
|
225 | - return; |
|
226 | - } |
|
227 | - $errorMessage = is_string($validated) |
|
228 | - ? $validated |
|
229 | - : __('The review submission failed. Please notify the site administrator.', 'site-reviews'); |
|
230 | - $this->setError($errorMessage); |
|
231 | - $this->setSessionValues('values', $this->request); |
|
232 | - } |
|
215 | + /** |
|
216 | + * @return void |
|
217 | + */ |
|
218 | + protected function validateCustom() |
|
219 | + { |
|
220 | + if (!empty($this->error)) { |
|
221 | + return; |
|
222 | + } |
|
223 | + $validated = apply_filters('site-reviews/validate/custom', true, $this->request); |
|
224 | + if (true === $validated) { |
|
225 | + return; |
|
226 | + } |
|
227 | + $errorMessage = is_string($validated) |
|
228 | + ? $validated |
|
229 | + : __('The review submission failed. Please notify the site administrator.', 'site-reviews'); |
|
230 | + $this->setError($errorMessage); |
|
231 | + $this->setSessionValues('values', $this->request); |
|
232 | + } |
|
233 | 233 | |
234 | - /** |
|
235 | - * @return void |
|
236 | - */ |
|
237 | - protected function validateHoneyPot() |
|
238 | - { |
|
239 | - if (!empty($this->error)) { |
|
240 | - return; |
|
241 | - } |
|
242 | - if (empty($this->request['gotcha'])) { |
|
243 | - return; |
|
244 | - } |
|
245 | - $this->setError(__('The review submission failed. Please notify the site administrator.', 'site-reviews'), |
|
246 | - 'The Honeypot caught a bad submission:' |
|
247 | - ); |
|
248 | - } |
|
234 | + /** |
|
235 | + * @return void |
|
236 | + */ |
|
237 | + protected function validateHoneyPot() |
|
238 | + { |
|
239 | + if (!empty($this->error)) { |
|
240 | + return; |
|
241 | + } |
|
242 | + if (empty($this->request['gotcha'])) { |
|
243 | + return; |
|
244 | + } |
|
245 | + $this->setError(__('The review submission failed. Please notify the site administrator.', 'site-reviews'), |
|
246 | + 'The Honeypot caught a bad submission:' |
|
247 | + ); |
|
248 | + } |
|
249 | 249 | |
250 | - /** |
|
251 | - * @return void |
|
252 | - */ |
|
253 | - protected function validateReviewLimits() |
|
254 | - { |
|
255 | - if (!empty($this->error)) { |
|
256 | - return; |
|
257 | - } |
|
258 | - if (!glsr(ReviewLimits::class)->hasReachedLimit($this->request)) { |
|
259 | - return; |
|
260 | - } |
|
261 | - $this->setError(__('You have already submitted a review.', 'site-reviews')); |
|
262 | - } |
|
250 | + /** |
|
251 | + * @return void |
|
252 | + */ |
|
253 | + protected function validateReviewLimits() |
|
254 | + { |
|
255 | + if (!empty($this->error)) { |
|
256 | + return; |
|
257 | + } |
|
258 | + if (!glsr(ReviewLimits::class)->hasReachedLimit($this->request)) { |
|
259 | + return; |
|
260 | + } |
|
261 | + $this->setError(__('You have already submitted a review.', 'site-reviews')); |
|
262 | + } |
|
263 | 263 | |
264 | - /** |
|
265 | - * @return void |
|
266 | - */ |
|
267 | - protected function validateRecaptcha() |
|
268 | - { |
|
269 | - if (!empty($this->error)) { |
|
270 | - return; |
|
271 | - } |
|
272 | - $status = $this->getRecaptchaStatus(); |
|
273 | - if (in_array($status, [static::RECAPTCHA_DISABLED, static::RECAPTCHA_VALID])) { |
|
274 | - return; |
|
275 | - } |
|
276 | - if (static::RECAPTCHA_EMPTY === $status) { |
|
277 | - $this->setSessionValues('recaptcha', 'unset'); |
|
278 | - $this->recaptchaIsUnset = true; |
|
279 | - return; |
|
280 | - } |
|
281 | - $this->setSessionValues('recaptcha', 'reset'); |
|
282 | - $errors = [ |
|
283 | - static::RECAPTCHA_FAILED => __('The reCAPTCHA failed to load, please refresh the page and try again.', 'site-reviews'), |
|
284 | - static::RECAPTCHA_INVALID => __('The reCAPTCHA verification failed, please try again.', 'site-reviews'), |
|
285 | - ]; |
|
286 | - $this->setError($errors[$status]); |
|
287 | - } |
|
264 | + /** |
|
265 | + * @return void |
|
266 | + */ |
|
267 | + protected function validateRecaptcha() |
|
268 | + { |
|
269 | + if (!empty($this->error)) { |
|
270 | + return; |
|
271 | + } |
|
272 | + $status = $this->getRecaptchaStatus(); |
|
273 | + if (in_array($status, [static::RECAPTCHA_DISABLED, static::RECAPTCHA_VALID])) { |
|
274 | + return; |
|
275 | + } |
|
276 | + if (static::RECAPTCHA_EMPTY === $status) { |
|
277 | + $this->setSessionValues('recaptcha', 'unset'); |
|
278 | + $this->recaptchaIsUnset = true; |
|
279 | + return; |
|
280 | + } |
|
281 | + $this->setSessionValues('recaptcha', 'reset'); |
|
282 | + $errors = [ |
|
283 | + static::RECAPTCHA_FAILED => __('The reCAPTCHA failed to load, please refresh the page and try again.', 'site-reviews'), |
|
284 | + static::RECAPTCHA_INVALID => __('The reCAPTCHA verification failed, please try again.', 'site-reviews'), |
|
285 | + ]; |
|
286 | + $this->setError($errors[$status]); |
|
287 | + } |
|
288 | 288 | |
289 | - /** |
|
290 | - * @return array |
|
291 | - */ |
|
292 | - protected function validateRequest(array $request) |
|
293 | - { |
|
294 | - if (!$this->isRequestValid($request)) { |
|
295 | - $this->error = __('Please fix the submission errors.', 'site-reviews'); |
|
296 | - return $request; |
|
297 | - } |
|
298 | - return array_merge(glsr(ValidateReviewDefaults::class)->defaults(), $request); |
|
299 | - } |
|
289 | + /** |
|
290 | + * @return array |
|
291 | + */ |
|
292 | + protected function validateRequest(array $request) |
|
293 | + { |
|
294 | + if (!$this->isRequestValid($request)) { |
|
295 | + $this->error = __('Please fix the submission errors.', 'site-reviews'); |
|
296 | + return $request; |
|
297 | + } |
|
298 | + return array_merge(glsr(ValidateReviewDefaults::class)->defaults(), $request); |
|
299 | + } |
|
300 | 300 | } |
@@ -10,143 +10,143 @@ |
||
10 | 10 | |
11 | 11 | class Rebusify |
12 | 12 | { |
13 | - const API_URL = 'https://www.rebusify.com/api/rbs/'; |
|
14 | - const WEB_URL = 'https://rebusify.com/plans?ref=105'; |
|
13 | + const API_URL = 'https://www.rebusify.com/api/rbs/'; |
|
14 | + const WEB_URL = 'https://rebusify.com/plans?ref=105'; |
|
15 | 15 | |
16 | - public $message; |
|
17 | - public $response; |
|
18 | - public $success; |
|
16 | + public $message; |
|
17 | + public $response; |
|
18 | + public $success; |
|
19 | 19 | |
20 | - /** |
|
21 | - * @return mixed |
|
22 | - */ |
|
23 | - public function __get($key) |
|
24 | - { |
|
25 | - return property_exists($this, $key) |
|
26 | - ? $this->$key |
|
27 | - : Arr::get($this->response, $key, null); |
|
28 | - } |
|
20 | + /** |
|
21 | + * @return mixed |
|
22 | + */ |
|
23 | + public function __get($key) |
|
24 | + { |
|
25 | + return property_exists($this, $key) |
|
26 | + ? $this->$key |
|
27 | + : Arr::get($this->response, $key, null); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * @return self |
|
32 | - */ |
|
33 | - public function activateKey($apiKey = '', $email = '') |
|
34 | - { |
|
35 | - $this->send('api_key_activation.php', [ |
|
36 | - 'body' => [ |
|
37 | - 'apikey' => $apiKey ?: 0, |
|
38 | - 'domain' => get_home_url(), |
|
39 | - 'email' => $email ?: 0, |
|
40 | - ], |
|
41 | - ]); |
|
42 | - return $this; |
|
43 | - } |
|
30 | + /** |
|
31 | + * @return self |
|
32 | + */ |
|
33 | + public function activateKey($apiKey = '', $email = '') |
|
34 | + { |
|
35 | + $this->send('api_key_activation.php', [ |
|
36 | + 'body' => [ |
|
37 | + 'apikey' => $apiKey ?: 0, |
|
38 | + 'domain' => get_home_url(), |
|
39 | + 'email' => $email ?: 0, |
|
40 | + ], |
|
41 | + ]); |
|
42 | + return $this; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @return self |
|
47 | - */ |
|
48 | - public function reset() |
|
49 | - { |
|
50 | - $this->message = ''; |
|
51 | - $this->response = []; |
|
52 | - $this->success = false; |
|
53 | - return $this; |
|
54 | - } |
|
45 | + /** |
|
46 | + * @return self |
|
47 | + */ |
|
48 | + public function reset() |
|
49 | + { |
|
50 | + $this->message = ''; |
|
51 | + $this->response = []; |
|
52 | + $this->success = false; |
|
53 | + return $this; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @return self |
|
58 | - */ |
|
59 | - public function sendReview(Review $review) |
|
60 | - { |
|
61 | - $this->send('index.php', [ |
|
62 | - 'body' => $this->getBodyForReview($review), |
|
63 | - 'timeout' => 120, |
|
64 | - ]); |
|
65 | - return $this; |
|
66 | - } |
|
56 | + /** |
|
57 | + * @return self |
|
58 | + */ |
|
59 | + public function sendReview(Review $review) |
|
60 | + { |
|
61 | + $this->send('index.php', [ |
|
62 | + 'body' => $this->getBodyForReview($review), |
|
63 | + 'timeout' => 120, |
|
64 | + ]); |
|
65 | + return $this; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @return self |
|
70 | - */ |
|
71 | - public function sendReviewResponse(Review $review) |
|
72 | - { |
|
73 | - $this->send('fetch_customer_reply.php', [ |
|
74 | - 'body' => $this->getBodyForResponse($review), |
|
75 | - ]); |
|
76 | - return $this; |
|
77 | - } |
|
68 | + /** |
|
69 | + * @return self |
|
70 | + */ |
|
71 | + public function sendReviewResponse(Review $review) |
|
72 | + { |
|
73 | + $this->send('fetch_customer_reply.php', [ |
|
74 | + 'body' => $this->getBodyForResponse($review), |
|
75 | + ]); |
|
76 | + return $this; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @return array |
|
81 | - */ |
|
82 | - protected function getBodyForResponse(Review $review) |
|
83 | - { |
|
84 | - $rebusifyResponse = [ |
|
85 | - 'reply' => Str::truncate($review->response, 300), |
|
86 | - 'review_id' => glsr(Database::class)->get($review->ID, 'rebusify'), // this is the rebusify review ID |
|
87 | - 'review_transaction_id' => $review->review_id, |
|
88 | - 'type' => 'M', |
|
89 | - ]; |
|
90 | - return apply_filters('site-reviews/rebusify/response', $rebusifyResponse, $review); |
|
91 | - } |
|
79 | + /** |
|
80 | + * @return array |
|
81 | + */ |
|
82 | + protected function getBodyForResponse(Review $review) |
|
83 | + { |
|
84 | + $rebusifyResponse = [ |
|
85 | + 'reply' => Str::truncate($review->response, 300), |
|
86 | + 'review_id' => glsr(Database::class)->get($review->ID, 'rebusify'), // this is the rebusify review ID |
|
87 | + 'review_transaction_id' => $review->review_id, |
|
88 | + 'type' => 'M', |
|
89 | + ]; |
|
90 | + return apply_filters('site-reviews/rebusify/response', $rebusifyResponse, $review); |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - protected function getBodyForReview(Review $review) |
|
97 | - { |
|
98 | - $rebusifyReview = [ |
|
99 | - 'domain' => get_home_url(), |
|
100 | - 'firstname' => Str::truncate(Str::convertName($review->author, 'first'), 25), |
|
101 | - 'rate' => $review->rating, |
|
102 | - 'review_transaction_id' => $review->review_id, |
|
103 | - 'reviews' => Str::truncate($review->content, 280), |
|
104 | - 'title' => Str::truncate($review->title, 35), |
|
105 | - 'transaction' => Application::ID, // woocommerce field, not needed for Site Reviews |
|
106 | - ]; |
|
107 | - return apply_filters('site-reviews/rebusify/review', $rebusifyReview, $review); |
|
108 | - } |
|
93 | + /** |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + protected function getBodyForReview(Review $review) |
|
97 | + { |
|
98 | + $rebusifyReview = [ |
|
99 | + 'domain' => get_home_url(), |
|
100 | + 'firstname' => Str::truncate(Str::convertName($review->author, 'first'), 25), |
|
101 | + 'rate' => $review->rating, |
|
102 | + 'review_transaction_id' => $review->review_id, |
|
103 | + 'reviews' => Str::truncate($review->content, 280), |
|
104 | + 'title' => Str::truncate($review->title, 35), |
|
105 | + 'transaction' => Application::ID, // woocommerce field, not needed for Site Reviews |
|
106 | + ]; |
|
107 | + return apply_filters('site-reviews/rebusify/review', $rebusifyReview, $review); |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * @param \WP_Error|array $response |
|
112 | - * @return void |
|
113 | - */ |
|
114 | - protected function handleResponse($response) |
|
115 | - { |
|
116 | - if (is_wp_error($response)) { |
|
117 | - $this->message = $response->get_error_message(); |
|
118 | - } else { |
|
119 | - $responseBody = wp_remote_retrieve_body($response); |
|
120 | - $responseCode = wp_remote_retrieve_response_code($response); |
|
121 | - $responseData = (array) json_decode($responseBody, true); |
|
122 | - $this->response = array_shift($responseData); |
|
123 | - $this->message = Arr::get($this->response, 'msg'); |
|
124 | - $this->success = 'success' === Arr::get($this->response, 'result') || 'yes' === Arr::get($this->response, 'success'); // @todo remove this ugly hack! |
|
125 | - if (200 !== $responseCode) { |
|
126 | - glsr_log()->error('Bad response code ['.$responseCode.']'); |
|
127 | - } |
|
128 | - if (!$this->success) { |
|
129 | - glsr_log()->error($this->message); |
|
130 | - } |
|
131 | - } |
|
132 | - } |
|
110 | + /** |
|
111 | + * @param \WP_Error|array $response |
|
112 | + * @return void |
|
113 | + */ |
|
114 | + protected function handleResponse($response) |
|
115 | + { |
|
116 | + if (is_wp_error($response)) { |
|
117 | + $this->message = $response->get_error_message(); |
|
118 | + } else { |
|
119 | + $responseBody = wp_remote_retrieve_body($response); |
|
120 | + $responseCode = wp_remote_retrieve_response_code($response); |
|
121 | + $responseData = (array) json_decode($responseBody, true); |
|
122 | + $this->response = array_shift($responseData); |
|
123 | + $this->message = Arr::get($this->response, 'msg'); |
|
124 | + $this->success = 'success' === Arr::get($this->response, 'result') || 'yes' === Arr::get($this->response, 'success'); // @todo remove this ugly hack! |
|
125 | + if (200 !== $responseCode) { |
|
126 | + glsr_log()->error('Bad response code ['.$responseCode.']'); |
|
127 | + } |
|
128 | + if (!$this->success) { |
|
129 | + glsr_log()->error($this->message); |
|
130 | + } |
|
131 | + } |
|
132 | + } |
|
133 | 133 | |
134 | - /** |
|
135 | - * @param string $endpoint |
|
136 | - * @return void |
|
137 | - */ |
|
138 | - protected function send($endpoint, array $args = []) |
|
139 | - { |
|
140 | - $args = wp_parse_args($args, [ |
|
141 | - 'body' => null, |
|
142 | - 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'], |
|
143 | - 'redirection' => 5, |
|
144 | - 'sslverify' => false, |
|
145 | - 'timeout' => 5, |
|
146 | - ]); |
|
147 | - $this->reset(); |
|
148 | - $this->handleResponse( |
|
149 | - wp_remote_post(trailingslashit(static::API_URL).$endpoint, $args) |
|
150 | - ); |
|
151 | - } |
|
134 | + /** |
|
135 | + * @param string $endpoint |
|
136 | + * @return void |
|
137 | + */ |
|
138 | + protected function send($endpoint, array $args = []) |
|
139 | + { |
|
140 | + $args = wp_parse_args($args, [ |
|
141 | + 'body' => null, |
|
142 | + 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'], |
|
143 | + 'redirection' => 5, |
|
144 | + 'sslverify' => false, |
|
145 | + 'timeout' => 5, |
|
146 | + ]); |
|
147 | + $this->reset(); |
|
148 | + $this->handleResponse( |
|
149 | + wp_remote_post(trailingslashit(static::API_URL).$endpoint, $args) |
|
150 | + ); |
|
151 | + } |
|
152 | 152 | } |
@@ -12,370 +12,370 @@ |
||
12 | 12 | |
13 | 13 | class System |
14 | 14 | { |
15 | - const PAD = 40; |
|
15 | + const PAD = 40; |
|
16 | 16 | |
17 | - /** |
|
18 | - * @return string |
|
19 | - */ |
|
20 | - public function __toString() |
|
21 | - { |
|
22 | - return $this->get(); |
|
23 | - } |
|
17 | + /** |
|
18 | + * @return string |
|
19 | + */ |
|
20 | + public function __toString() |
|
21 | + { |
|
22 | + return $this->get(); |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * @return string |
|
27 | - */ |
|
28 | - public function get() |
|
29 | - { |
|
30 | - $details = [ |
|
31 | - 'plugin' => 'Plugin Details', |
|
32 | - 'addon' => 'Addon Details', |
|
33 | - 'browser' => 'Browser Details', |
|
34 | - 'server' => 'Server Details', |
|
35 | - 'php' => 'PHP Configuration', |
|
36 | - 'wordpress' => 'WordPress Configuration', |
|
37 | - 'mu-plugin' => 'Must-Use Plugins', |
|
38 | - 'multisite-plugin' => 'Network Active Plugins', |
|
39 | - 'active-plugin' => 'Active Plugins', |
|
40 | - 'inactive-plugin' => 'Inactive Plugins', |
|
41 | - 'setting' => 'Plugin Settings', |
|
42 | - 'reviews' => 'Review Counts', |
|
43 | - ]; |
|
44 | - $systemInfo = array_reduce(array_keys($details), function ($carry, $key) use ($details) { |
|
45 | - $methodName = Helper::buildMethodName('get-'.$key.'-details'); |
|
46 | - if (method_exists($this, $methodName) && $systemDetails = $this->$methodName()) { |
|
47 | - return $carry.$this->implode( |
|
48 | - strtoupper($details[$key]), |
|
49 | - apply_filters('site-reviews/system/'.$key, $systemDetails) |
|
50 | - ); |
|
51 | - } |
|
52 | - return $carry; |
|
53 | - }); |
|
54 | - return trim($systemInfo); |
|
55 | - } |
|
25 | + /** |
|
26 | + * @return string |
|
27 | + */ |
|
28 | + public function get() |
|
29 | + { |
|
30 | + $details = [ |
|
31 | + 'plugin' => 'Plugin Details', |
|
32 | + 'addon' => 'Addon Details', |
|
33 | + 'browser' => 'Browser Details', |
|
34 | + 'server' => 'Server Details', |
|
35 | + 'php' => 'PHP Configuration', |
|
36 | + 'wordpress' => 'WordPress Configuration', |
|
37 | + 'mu-plugin' => 'Must-Use Plugins', |
|
38 | + 'multisite-plugin' => 'Network Active Plugins', |
|
39 | + 'active-plugin' => 'Active Plugins', |
|
40 | + 'inactive-plugin' => 'Inactive Plugins', |
|
41 | + 'setting' => 'Plugin Settings', |
|
42 | + 'reviews' => 'Review Counts', |
|
43 | + ]; |
|
44 | + $systemInfo = array_reduce(array_keys($details), function ($carry, $key) use ($details) { |
|
45 | + $methodName = Helper::buildMethodName('get-'.$key.'-details'); |
|
46 | + if (method_exists($this, $methodName) && $systemDetails = $this->$methodName()) { |
|
47 | + return $carry.$this->implode( |
|
48 | + strtoupper($details[$key]), |
|
49 | + apply_filters('site-reviews/system/'.$key, $systemDetails) |
|
50 | + ); |
|
51 | + } |
|
52 | + return $carry; |
|
53 | + }); |
|
54 | + return trim($systemInfo); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @return array |
|
59 | - */ |
|
60 | - public function getActivePluginDetails() |
|
61 | - { |
|
62 | - $plugins = get_plugins(); |
|
63 | - $activePlugins = glsr(OptionManager::class)->getWP('active_plugins', [], 'array'); |
|
64 | - $inactive = array_diff_key($plugins, array_flip($activePlugins)); |
|
65 | - return $this->normalizePluginList(array_diff_key($plugins, $inactive)); |
|
66 | - } |
|
57 | + /** |
|
58 | + * @return array |
|
59 | + */ |
|
60 | + public function getActivePluginDetails() |
|
61 | + { |
|
62 | + $plugins = get_plugins(); |
|
63 | + $activePlugins = glsr(OptionManager::class)->getWP('active_plugins', [], 'array'); |
|
64 | + $inactive = array_diff_key($plugins, array_flip($activePlugins)); |
|
65 | + return $this->normalizePluginList(array_diff_key($plugins, $inactive)); |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @return array |
|
70 | - */ |
|
71 | - public function getAddonDetails() |
|
72 | - { |
|
73 | - $details = apply_filters('site-reviews/addon/system-info', []); |
|
74 | - ksort($details); |
|
75 | - return $details; |
|
76 | - } |
|
68 | + /** |
|
69 | + * @return array |
|
70 | + */ |
|
71 | + public function getAddonDetails() |
|
72 | + { |
|
73 | + $details = apply_filters('site-reviews/addon/system-info', []); |
|
74 | + ksort($details); |
|
75 | + return $details; |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @return array |
|
80 | - */ |
|
81 | - public function getBrowserDetails() |
|
82 | - { |
|
83 | - $browser = new Browser(); |
|
84 | - $name = esc_attr($browser->getName()); |
|
85 | - $userAgent = esc_attr($browser->getUserAgent()->getUserAgentString()); |
|
86 | - $version = esc_attr($browser->getVersion()); |
|
87 | - return [ |
|
88 | - 'Browser Name' => sprintf('%s %s', $name, $version), |
|
89 | - 'Browser UA' => $userAgent, |
|
90 | - ]; |
|
91 | - } |
|
78 | + /** |
|
79 | + * @return array |
|
80 | + */ |
|
81 | + public function getBrowserDetails() |
|
82 | + { |
|
83 | + $browser = new Browser(); |
|
84 | + $name = esc_attr($browser->getName()); |
|
85 | + $userAgent = esc_attr($browser->getUserAgent()->getUserAgentString()); |
|
86 | + $version = esc_attr($browser->getVersion()); |
|
87 | + return [ |
|
88 | + 'Browser Name' => sprintf('%s %s', $name, $version), |
|
89 | + 'Browser UA' => $userAgent, |
|
90 | + ]; |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - public function getInactivePluginDetails() |
|
97 | - { |
|
98 | - $activePlugins = glsr(OptionManager::class)->getWP('active_plugins', [], 'array'); |
|
99 | - $inactivePlugins = $this->normalizePluginList(array_diff_key(get_plugins(), array_flip($activePlugins))); |
|
100 | - $multisitePlugins = $this->getMultisitePluginDetails(); |
|
101 | - return empty($multisitePlugins) |
|
102 | - ? $inactivePlugins |
|
103 | - : array_diff($inactivePlugins, $multisitePlugins); |
|
104 | - } |
|
93 | + /** |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + public function getInactivePluginDetails() |
|
97 | + { |
|
98 | + $activePlugins = glsr(OptionManager::class)->getWP('active_plugins', [], 'array'); |
|
99 | + $inactivePlugins = $this->normalizePluginList(array_diff_key(get_plugins(), array_flip($activePlugins))); |
|
100 | + $multisitePlugins = $this->getMultisitePluginDetails(); |
|
101 | + return empty($multisitePlugins) |
|
102 | + ? $inactivePlugins |
|
103 | + : array_diff($inactivePlugins, $multisitePlugins); |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * @return array |
|
108 | - */ |
|
109 | - public function getMuPluginDetails() |
|
110 | - { |
|
111 | - if (empty($plugins = get_mu_plugins())) { |
|
112 | - return []; |
|
113 | - } |
|
114 | - return $this->normalizePluginList($plugins); |
|
115 | - } |
|
106 | + /** |
|
107 | + * @return array |
|
108 | + */ |
|
109 | + public function getMuPluginDetails() |
|
110 | + { |
|
111 | + if (empty($plugins = get_mu_plugins())) { |
|
112 | + return []; |
|
113 | + } |
|
114 | + return $this->normalizePluginList($plugins); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * @return array |
|
119 | - */ |
|
120 | - public function getMultisitePluginDetails() |
|
121 | - { |
|
122 | - $activePlugins = (array) get_site_option('active_sitewide_plugins', []); |
|
123 | - if (!is_multisite() || empty($activePlugins)) { |
|
124 | - return []; |
|
125 | - } |
|
126 | - return $this->normalizePluginList(array_intersect_key(get_plugins(), $activePlugins)); |
|
127 | - } |
|
117 | + /** |
|
118 | + * @return array |
|
119 | + */ |
|
120 | + public function getMultisitePluginDetails() |
|
121 | + { |
|
122 | + $activePlugins = (array) get_site_option('active_sitewide_plugins', []); |
|
123 | + if (!is_multisite() || empty($activePlugins)) { |
|
124 | + return []; |
|
125 | + } |
|
126 | + return $this->normalizePluginList(array_intersect_key(get_plugins(), $activePlugins)); |
|
127 | + } |
|
128 | 128 | |
129 | - /** |
|
130 | - * @return array |
|
131 | - */ |
|
132 | - public function getPhpDetails() |
|
133 | - { |
|
134 | - $displayErrors = ini_get('display_errors') |
|
135 | - ? 'On ('.ini_get('display_errors').')' |
|
136 | - : 'N/A'; |
|
137 | - $intlSupport = extension_loaded('intl') |
|
138 | - ? phpversion('intl') |
|
139 | - : 'false'; |
|
140 | - return [ |
|
141 | - 'cURL' => var_export(function_exists('curl_init'), true), |
|
142 | - 'Default Charset' => ini_get('default_charset'), |
|
143 | - 'Display Errors' => $displayErrors, |
|
144 | - 'fsockopen' => var_export(function_exists('fsockopen'), true), |
|
145 | - 'Intl' => $intlSupport, |
|
146 | - 'IPv6' => var_export(defined('AF_INET6'), true), |
|
147 | - 'Max Execution Time' => ini_get('max_execution_time'), |
|
148 | - 'Max Input Nesting Level' => ini_get('max_input_nesting_level'), |
|
149 | - 'Max Input Vars' => ini_get('max_input_vars'), |
|
150 | - 'Memory Limit' => ini_get('memory_limit'), |
|
151 | - 'Post Max Size' => ini_get('post_max_size'), |
|
152 | - 'Sendmail Path' => ini_get('sendmail_path'), |
|
153 | - 'Session Cookie Path' => esc_html(ini_get('session.cookie_path')), |
|
154 | - 'Session Name' => esc_html(ini_get('session.name')), |
|
155 | - 'Session Save Path' => esc_html(ini_get('session.save_path')), |
|
156 | - 'Session Use Cookies' => var_export(wp_validate_boolean(ini_get('session.use_cookies')), true), |
|
157 | - 'Session Use Only Cookies' => var_export(wp_validate_boolean(ini_get('session.use_only_cookies')), true), |
|
158 | - 'Upload Max Filesize' => ini_get('upload_max_filesize'), |
|
159 | - ]; |
|
160 | - } |
|
129 | + /** |
|
130 | + * @return array |
|
131 | + */ |
|
132 | + public function getPhpDetails() |
|
133 | + { |
|
134 | + $displayErrors = ini_get('display_errors') |
|
135 | + ? 'On ('.ini_get('display_errors').')' |
|
136 | + : 'N/A'; |
|
137 | + $intlSupport = extension_loaded('intl') |
|
138 | + ? phpversion('intl') |
|
139 | + : 'false'; |
|
140 | + return [ |
|
141 | + 'cURL' => var_export(function_exists('curl_init'), true), |
|
142 | + 'Default Charset' => ini_get('default_charset'), |
|
143 | + 'Display Errors' => $displayErrors, |
|
144 | + 'fsockopen' => var_export(function_exists('fsockopen'), true), |
|
145 | + 'Intl' => $intlSupport, |
|
146 | + 'IPv6' => var_export(defined('AF_INET6'), true), |
|
147 | + 'Max Execution Time' => ini_get('max_execution_time'), |
|
148 | + 'Max Input Nesting Level' => ini_get('max_input_nesting_level'), |
|
149 | + 'Max Input Vars' => ini_get('max_input_vars'), |
|
150 | + 'Memory Limit' => ini_get('memory_limit'), |
|
151 | + 'Post Max Size' => ini_get('post_max_size'), |
|
152 | + 'Sendmail Path' => ini_get('sendmail_path'), |
|
153 | + 'Session Cookie Path' => esc_html(ini_get('session.cookie_path')), |
|
154 | + 'Session Name' => esc_html(ini_get('session.name')), |
|
155 | + 'Session Save Path' => esc_html(ini_get('session.save_path')), |
|
156 | + 'Session Use Cookies' => var_export(wp_validate_boolean(ini_get('session.use_cookies')), true), |
|
157 | + 'Session Use Only Cookies' => var_export(wp_validate_boolean(ini_get('session.use_only_cookies')), true), |
|
158 | + 'Upload Max Filesize' => ini_get('upload_max_filesize'), |
|
159 | + ]; |
|
160 | + } |
|
161 | 161 | |
162 | - /** |
|
163 | - * @return array |
|
164 | - */ |
|
165 | - public function getReviewsDetails() |
|
166 | - { |
|
167 | - $counts = glsr(CountsManager::class)->getCounts(); |
|
168 | - $counts = Arr::flattenArray($counts); |
|
169 | - array_walk($counts, function (&$ratings) use ($counts) { |
|
170 | - if (!is_array($ratings)) { |
|
171 | - glsr_log() |
|
172 | - ->error('$ratings is not an array, possibly due to incorrectly imported reviews.') |
|
173 | - ->debug($ratings) |
|
174 | - ->debug($counts); |
|
175 | - return; |
|
176 | - } |
|
177 | - $ratings = array_sum($ratings).' ('.implode(', ', $ratings).')'; |
|
178 | - }); |
|
179 | - ksort($counts); |
|
180 | - return $counts; |
|
181 | - } |
|
162 | + /** |
|
163 | + * @return array |
|
164 | + */ |
|
165 | + public function getReviewsDetails() |
|
166 | + { |
|
167 | + $counts = glsr(CountsManager::class)->getCounts(); |
|
168 | + $counts = Arr::flattenArray($counts); |
|
169 | + array_walk($counts, function (&$ratings) use ($counts) { |
|
170 | + if (!is_array($ratings)) { |
|
171 | + glsr_log() |
|
172 | + ->error('$ratings is not an array, possibly due to incorrectly imported reviews.') |
|
173 | + ->debug($ratings) |
|
174 | + ->debug($counts); |
|
175 | + return; |
|
176 | + } |
|
177 | + $ratings = array_sum($ratings).' ('.implode(', ', $ratings).')'; |
|
178 | + }); |
|
179 | + ksort($counts); |
|
180 | + return $counts; |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * @return array |
|
185 | - */ |
|
186 | - public function getServerDetails() |
|
187 | - { |
|
188 | - global $wpdb; |
|
189 | - return [ |
|
190 | - 'Host Name' => $this->getHostName(), |
|
191 | - 'MySQL Version' => $wpdb->db_version(), |
|
192 | - 'PHP Version' => PHP_VERSION, |
|
193 | - 'Server Software' => filter_input(INPUT_SERVER, 'SERVER_SOFTWARE'), |
|
194 | - ]; |
|
195 | - } |
|
183 | + /** |
|
184 | + * @return array |
|
185 | + */ |
|
186 | + public function getServerDetails() |
|
187 | + { |
|
188 | + global $wpdb; |
|
189 | + return [ |
|
190 | + 'Host Name' => $this->getHostName(), |
|
191 | + 'MySQL Version' => $wpdb->db_version(), |
|
192 | + 'PHP Version' => PHP_VERSION, |
|
193 | + 'Server Software' => filter_input(INPUT_SERVER, 'SERVER_SOFTWARE'), |
|
194 | + ]; |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * @return array |
|
199 | - */ |
|
200 | - public function getSettingDetails() |
|
201 | - { |
|
202 | - $settings = glsr(OptionManager::class)->get('settings', []); |
|
203 | - $settings = Arr::flattenArray($settings, true); |
|
204 | - $settings = $this->purgeSensitiveData($settings); |
|
205 | - ksort($settings); |
|
206 | - $details = []; |
|
207 | - foreach ($settings as $key => $value) { |
|
208 | - if (Str::startsWith('strings', $key) && Str::endsWith('id', $key)) { |
|
209 | - continue; |
|
210 | - } |
|
211 | - $value = htmlspecialchars(trim(preg_replace('/\s\s+/', '\\n', $value)), ENT_QUOTES, 'UTF-8'); |
|
212 | - $details[$key] = $value; |
|
213 | - } |
|
214 | - return $details; |
|
215 | - } |
|
197 | + /** |
|
198 | + * @return array |
|
199 | + */ |
|
200 | + public function getSettingDetails() |
|
201 | + { |
|
202 | + $settings = glsr(OptionManager::class)->get('settings', []); |
|
203 | + $settings = Arr::flattenArray($settings, true); |
|
204 | + $settings = $this->purgeSensitiveData($settings); |
|
205 | + ksort($settings); |
|
206 | + $details = []; |
|
207 | + foreach ($settings as $key => $value) { |
|
208 | + if (Str::startsWith('strings', $key) && Str::endsWith('id', $key)) { |
|
209 | + continue; |
|
210 | + } |
|
211 | + $value = htmlspecialchars(trim(preg_replace('/\s\s+/', '\\n', $value)), ENT_QUOTES, 'UTF-8'); |
|
212 | + $details[$key] = $value; |
|
213 | + } |
|
214 | + return $details; |
|
215 | + } |
|
216 | 216 | |
217 | - /** |
|
218 | - * @return array |
|
219 | - */ |
|
220 | - public function getPluginDetails() |
|
221 | - { |
|
222 | - return [ |
|
223 | - 'Console level' => glsr(Console::class)->humanLevel(), |
|
224 | - 'Console size' => glsr(Console::class)->humanSize('0'), |
|
225 | - 'Last Rating Count' => date_i18n('Y-m-d H:i', glsr(OptionManager::class)->get('last_review_count')), |
|
226 | - 'Version (current)' => glsr()->version, |
|
227 | - 'Version (previous)' => glsr(OptionManager::class)->get('version_upgraded_from'), |
|
228 | - ]; |
|
229 | - } |
|
217 | + /** |
|
218 | + * @return array |
|
219 | + */ |
|
220 | + public function getPluginDetails() |
|
221 | + { |
|
222 | + return [ |
|
223 | + 'Console level' => glsr(Console::class)->humanLevel(), |
|
224 | + 'Console size' => glsr(Console::class)->humanSize('0'), |
|
225 | + 'Last Rating Count' => date_i18n('Y-m-d H:i', glsr(OptionManager::class)->get('last_review_count')), |
|
226 | + 'Version (current)' => glsr()->version, |
|
227 | + 'Version (previous)' => glsr(OptionManager::class)->get('version_upgraded_from'), |
|
228 | + ]; |
|
229 | + } |
|
230 | 230 | |
231 | - /** |
|
232 | - * @return array |
|
233 | - */ |
|
234 | - public function getWordpressDetails() |
|
235 | - { |
|
236 | - global $wpdb; |
|
237 | - $theme = wp_get_theme(); |
|
238 | - return [ |
|
239 | - 'Active Theme' => sprintf('%s v%s', (string) $theme->Name, (string) $theme->Version), |
|
240 | - 'Email Domain' => substr(strrchr(glsr(OptionManager::class)->getWP('admin_email'), '@'), 1), |
|
241 | - 'Home URL' => home_url(), |
|
242 | - 'Language' => get_locale(), |
|
243 | - 'Memory Limit' => WP_MEMORY_LIMIT, |
|
244 | - 'Multisite' => var_export(is_multisite(), true), |
|
245 | - 'Page For Posts ID' => glsr(OptionManager::class)->getWP('page_for_posts'), |
|
246 | - 'Page On Front ID' => glsr(OptionManager::class)->getWP('page_on_front'), |
|
247 | - 'Permalink Structure' => glsr(OptionManager::class)->getWP('permalink_structure', 'default'), |
|
248 | - 'Post Stati' => implode(', ', get_post_stati()), |
|
249 | - 'Remote Post' => glsr(Cache::class)->getRemotePostTest(), |
|
250 | - 'Show On Front' => glsr(OptionManager::class)->getWP('show_on_front'), |
|
251 | - 'Site URL' => site_url(), |
|
252 | - 'Timezone' => glsr(OptionManager::class)->getWP('timezone_string', ini_get('date.timezone').' (PHP)'), |
|
253 | - 'Version' => get_bloginfo('version'), |
|
254 | - 'WP Debug' => var_export(defined('WP_DEBUG'), true), |
|
255 | - 'WP Max Upload Size' => size_format(wp_max_upload_size()), |
|
256 | - 'WP Memory Limit' => WP_MEMORY_LIMIT, |
|
257 | - ]; |
|
258 | - } |
|
231 | + /** |
|
232 | + * @return array |
|
233 | + */ |
|
234 | + public function getWordpressDetails() |
|
235 | + { |
|
236 | + global $wpdb; |
|
237 | + $theme = wp_get_theme(); |
|
238 | + return [ |
|
239 | + 'Active Theme' => sprintf('%s v%s', (string) $theme->Name, (string) $theme->Version), |
|
240 | + 'Email Domain' => substr(strrchr(glsr(OptionManager::class)->getWP('admin_email'), '@'), 1), |
|
241 | + 'Home URL' => home_url(), |
|
242 | + 'Language' => get_locale(), |
|
243 | + 'Memory Limit' => WP_MEMORY_LIMIT, |
|
244 | + 'Multisite' => var_export(is_multisite(), true), |
|
245 | + 'Page For Posts ID' => glsr(OptionManager::class)->getWP('page_for_posts'), |
|
246 | + 'Page On Front ID' => glsr(OptionManager::class)->getWP('page_on_front'), |
|
247 | + 'Permalink Structure' => glsr(OptionManager::class)->getWP('permalink_structure', 'default'), |
|
248 | + 'Post Stati' => implode(', ', get_post_stati()), |
|
249 | + 'Remote Post' => glsr(Cache::class)->getRemotePostTest(), |
|
250 | + 'Show On Front' => glsr(OptionManager::class)->getWP('show_on_front'), |
|
251 | + 'Site URL' => site_url(), |
|
252 | + 'Timezone' => glsr(OptionManager::class)->getWP('timezone_string', ini_get('date.timezone').' (PHP)'), |
|
253 | + 'Version' => get_bloginfo('version'), |
|
254 | + 'WP Debug' => var_export(defined('WP_DEBUG'), true), |
|
255 | + 'WP Max Upload Size' => size_format(wp_max_upload_size()), |
|
256 | + 'WP Memory Limit' => WP_MEMORY_LIMIT, |
|
257 | + ]; |
|
258 | + } |
|
259 | 259 | |
260 | - /** |
|
261 | - * @return string |
|
262 | - */ |
|
263 | - protected function detectWebhostProvider() |
|
264 | - { |
|
265 | - $checks = [ |
|
266 | - '.accountservergroup.com' => 'Site5', |
|
267 | - '.gridserver.com' => 'MediaTemple Grid', |
|
268 | - '.inmotionhosting.com' => 'InMotion Hosting', |
|
269 | - '.ovh.net' => 'OVH', |
|
270 | - '.pair.com' => 'pair Networks', |
|
271 | - '.stabletransit.com' => 'Rackspace Cloud', |
|
272 | - '.stratoserver.net' => 'STRATO', |
|
273 | - '.sysfix.eu' => 'SysFix.eu Power Hosting', |
|
274 | - 'bluehost.com' => 'Bluehost', |
|
275 | - 'DH_USER' => 'DreamHost', |
|
276 | - 'Flywheel' => 'Flywheel', |
|
277 | - 'ipagemysql.com' => 'iPage', |
|
278 | - 'ipowermysql.com' => 'IPower', |
|
279 | - 'localhost:/tmp/mysql5.sock' => 'ICDSoft', |
|
280 | - 'mysqlv5' => 'NetworkSolutions', |
|
281 | - 'PAGELYBIN' => 'Pagely', |
|
282 | - 'secureserver.net' => 'GoDaddy', |
|
283 | - 'WPE_APIKEY' => 'WP Engine', |
|
284 | - ]; |
|
285 | - foreach ($checks as $key => $value) { |
|
286 | - if (!$this->isWebhostCheckValid($key)) { |
|
287 | - continue; |
|
288 | - } |
|
289 | - return $value; |
|
290 | - } |
|
291 | - return implode(',', array_filter([DB_HOST, filter_input(INPUT_SERVER, 'SERVER_NAME')])); |
|
292 | - } |
|
260 | + /** |
|
261 | + * @return string |
|
262 | + */ |
|
263 | + protected function detectWebhostProvider() |
|
264 | + { |
|
265 | + $checks = [ |
|
266 | + '.accountservergroup.com' => 'Site5', |
|
267 | + '.gridserver.com' => 'MediaTemple Grid', |
|
268 | + '.inmotionhosting.com' => 'InMotion Hosting', |
|
269 | + '.ovh.net' => 'OVH', |
|
270 | + '.pair.com' => 'pair Networks', |
|
271 | + '.stabletransit.com' => 'Rackspace Cloud', |
|
272 | + '.stratoserver.net' => 'STRATO', |
|
273 | + '.sysfix.eu' => 'SysFix.eu Power Hosting', |
|
274 | + 'bluehost.com' => 'Bluehost', |
|
275 | + 'DH_USER' => 'DreamHost', |
|
276 | + 'Flywheel' => 'Flywheel', |
|
277 | + 'ipagemysql.com' => 'iPage', |
|
278 | + 'ipowermysql.com' => 'IPower', |
|
279 | + 'localhost:/tmp/mysql5.sock' => 'ICDSoft', |
|
280 | + 'mysqlv5' => 'NetworkSolutions', |
|
281 | + 'PAGELYBIN' => 'Pagely', |
|
282 | + 'secureserver.net' => 'GoDaddy', |
|
283 | + 'WPE_APIKEY' => 'WP Engine', |
|
284 | + ]; |
|
285 | + foreach ($checks as $key => $value) { |
|
286 | + if (!$this->isWebhostCheckValid($key)) { |
|
287 | + continue; |
|
288 | + } |
|
289 | + return $value; |
|
290 | + } |
|
291 | + return implode(',', array_filter([DB_HOST, filter_input(INPUT_SERVER, 'SERVER_NAME')])); |
|
292 | + } |
|
293 | 293 | |
294 | - /** |
|
295 | - * @return string |
|
296 | - */ |
|
297 | - protected function getHostName() |
|
298 | - { |
|
299 | - return sprintf('%s (%s)', |
|
300 | - $this->detectWebhostProvider(), |
|
301 | - Helper::getIpAddress() |
|
302 | - ); |
|
303 | - } |
|
294 | + /** |
|
295 | + * @return string |
|
296 | + */ |
|
297 | + protected function getHostName() |
|
298 | + { |
|
299 | + return sprintf('%s (%s)', |
|
300 | + $this->detectWebhostProvider(), |
|
301 | + Helper::getIpAddress() |
|
302 | + ); |
|
303 | + } |
|
304 | 304 | |
305 | - /** |
|
306 | - * @return array |
|
307 | - */ |
|
308 | - protected function getWordpressPlugins() |
|
309 | - { |
|
310 | - $plugins = get_plugins(); |
|
311 | - $activePlugins = glsr(OptionManager::class)->getWP('active_plugins', [], 'array'); |
|
312 | - $inactive = $this->normalizePluginList(array_diff_key($plugins, array_flip($activePlugins))); |
|
313 | - $active = $this->normalizePluginList(array_diff_key($plugins, $inactive)); |
|
314 | - return $active + $inactive; |
|
315 | - } |
|
305 | + /** |
|
306 | + * @return array |
|
307 | + */ |
|
308 | + protected function getWordpressPlugins() |
|
309 | + { |
|
310 | + $plugins = get_plugins(); |
|
311 | + $activePlugins = glsr(OptionManager::class)->getWP('active_plugins', [], 'array'); |
|
312 | + $inactive = $this->normalizePluginList(array_diff_key($plugins, array_flip($activePlugins))); |
|
313 | + $active = $this->normalizePluginList(array_diff_key($plugins, $inactive)); |
|
314 | + return $active + $inactive; |
|
315 | + } |
|
316 | 316 | |
317 | - /** |
|
318 | - * @param string $title |
|
319 | - * @return string |
|
320 | - */ |
|
321 | - protected function implode($title, array $details) |
|
322 | - { |
|
323 | - $strings = ['['.$title.']']; |
|
324 | - $padding = max(array_map('strlen', array_keys($details))); |
|
325 | - $padding = max([$padding, static::PAD]); |
|
326 | - foreach ($details as $key => $value) { |
|
327 | - $strings[] = is_string($key) |
|
328 | - ? sprintf('%s : %s', str_pad($key, $padding, '.'), $value) |
|
329 | - : ' - '.$value; |
|
330 | - } |
|
331 | - return implode(PHP_EOL, $strings).PHP_EOL.PHP_EOL; |
|
332 | - } |
|
317 | + /** |
|
318 | + * @param string $title |
|
319 | + * @return string |
|
320 | + */ |
|
321 | + protected function implode($title, array $details) |
|
322 | + { |
|
323 | + $strings = ['['.$title.']']; |
|
324 | + $padding = max(array_map('strlen', array_keys($details))); |
|
325 | + $padding = max([$padding, static::PAD]); |
|
326 | + foreach ($details as $key => $value) { |
|
327 | + $strings[] = is_string($key) |
|
328 | + ? sprintf('%s : %s', str_pad($key, $padding, '.'), $value) |
|
329 | + : ' - '.$value; |
|
330 | + } |
|
331 | + return implode(PHP_EOL, $strings).PHP_EOL.PHP_EOL; |
|
332 | + } |
|
333 | 333 | |
334 | - /** |
|
335 | - * @param string $key |
|
336 | - * @return bool |
|
337 | - */ |
|
338 | - protected function isWebhostCheckValid($key) |
|
339 | - { |
|
340 | - return defined($key) |
|
341 | - || filter_input(INPUT_SERVER, $key) |
|
342 | - || false !== strpos(filter_input(INPUT_SERVER, 'SERVER_NAME'), $key) |
|
343 | - || false !== strpos(DB_HOST, $key) |
|
344 | - || false !== strpos(php_uname(), $key); |
|
345 | - } |
|
334 | + /** |
|
335 | + * @param string $key |
|
336 | + * @return bool |
|
337 | + */ |
|
338 | + protected function isWebhostCheckValid($key) |
|
339 | + { |
|
340 | + return defined($key) |
|
341 | + || filter_input(INPUT_SERVER, $key) |
|
342 | + || false !== strpos(filter_input(INPUT_SERVER, 'SERVER_NAME'), $key) |
|
343 | + || false !== strpos(DB_HOST, $key) |
|
344 | + || false !== strpos(php_uname(), $key); |
|
345 | + } |
|
346 | 346 | |
347 | - /** |
|
348 | - * @return array |
|
349 | - */ |
|
350 | - protected function normalizePluginList(array $plugins) |
|
351 | - { |
|
352 | - $plugins = array_map(function ($plugin) { |
|
353 | - return sprintf('%s v%s', Arr::get($plugin, 'Name'), Arr::get($plugin, 'Version')); |
|
354 | - }, $plugins); |
|
355 | - natcasesort($plugins); |
|
356 | - return array_flip($plugins); |
|
357 | - } |
|
347 | + /** |
|
348 | + * @return array |
|
349 | + */ |
|
350 | + protected function normalizePluginList(array $plugins) |
|
351 | + { |
|
352 | + $plugins = array_map(function ($plugin) { |
|
353 | + return sprintf('%s v%s', Arr::get($plugin, 'Name'), Arr::get($plugin, 'Version')); |
|
354 | + }, $plugins); |
|
355 | + natcasesort($plugins); |
|
356 | + return array_flip($plugins); |
|
357 | + } |
|
358 | 358 | |
359 | - /** |
|
360 | - * @return array |
|
361 | - */ |
|
362 | - protected function purgeSensitiveData(array $settings) |
|
363 | - { |
|
364 | - $keys = [ |
|
365 | - 'general.rebusify_serial', |
|
366 | - 'licenses.', |
|
367 | - 'submissions.recaptcha.key', |
|
368 | - 'submissions.recaptcha.secret', |
|
369 | - ]; |
|
370 | - array_walk($settings, function (&$value, $setting) use ($keys) { |
|
371 | - foreach ($keys as $key) { |
|
372 | - if (!Str::startsWith($key, $setting) || empty($value)) { |
|
373 | - continue; |
|
374 | - } |
|
375 | - $value = str_repeat('•', 13); |
|
376 | - return; |
|
377 | - } |
|
378 | - }); |
|
379 | - return $settings; |
|
380 | - } |
|
359 | + /** |
|
360 | + * @return array |
|
361 | + */ |
|
362 | + protected function purgeSensitiveData(array $settings) |
|
363 | + { |
|
364 | + $keys = [ |
|
365 | + 'general.rebusify_serial', |
|
366 | + 'licenses.', |
|
367 | + 'submissions.recaptcha.key', |
|
368 | + 'submissions.recaptcha.secret', |
|
369 | + ]; |
|
370 | + array_walk($settings, function (&$value, $setting) use ($keys) { |
|
371 | + foreach ($keys as $key) { |
|
372 | + if (!Str::startsWith($key, $setting) || empty($value)) { |
|
373 | + continue; |
|
374 | + } |
|
375 | + $value = str_repeat('•', 13); |
|
376 | + return; |
|
377 | + } |
|
378 | + }); |
|
379 | + return $settings; |
|
380 | + } |
|
381 | 381 | } |
@@ -9,433 +9,433 @@ |
||
9 | 9 | |
10 | 10 | class Console |
11 | 11 | { |
12 | - const DEBUG = 0; // Detailed debug information |
|
13 | - const INFO = 1; // Interesting events |
|
14 | - const NOTICE = 2; // Normal but significant events |
|
15 | - const WARNING = 4; // Exceptional occurrences that are not errors |
|
16 | - const ERROR = 8; // Runtime errors that do not require immediate action |
|
17 | - const CRITICAL = 16; // Critical conditions |
|
18 | - const ALERT = 32; // Action must be taken immediately |
|
19 | - const EMERGENCY = 64; // System is unusable |
|
20 | - |
|
21 | - protected $file; |
|
22 | - protected $log; |
|
23 | - protected $logOnceKey = 'glsr_log_once'; |
|
24 | - |
|
25 | - public function __construct() |
|
26 | - { |
|
27 | - $this->file = glsr()->path('console.log'); |
|
28 | - $this->log = file_exists($this->file) |
|
29 | - ? file_get_contents($this->file) |
|
30 | - : ''; |
|
31 | - $this->reset(); |
|
32 | - } |
|
33 | - |
|
34 | - /** |
|
35 | - * @return string |
|
36 | - */ |
|
37 | - public function __toString() |
|
38 | - { |
|
39 | - return $this->get(); |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Action must be taken immediately |
|
44 | - * Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up. |
|
45 | - * @param mixed $message |
|
46 | - * @param array $context |
|
47 | - * @return static |
|
48 | - */ |
|
49 | - public function alert($message, array $context = []) |
|
50 | - { |
|
51 | - return $this->log(static::ALERT, $message, $context); |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @return void |
|
56 | - */ |
|
57 | - public function clear() |
|
58 | - { |
|
59 | - $this->log = ''; |
|
60 | - file_put_contents($this->file, $this->log); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Critical conditions |
|
65 | - * Example: Application component unavailable, unexpected exception. |
|
66 | - * @param mixed $message |
|
67 | - * @param array $context |
|
68 | - * @return static |
|
69 | - */ |
|
70 | - public function critical($message, array $context = []) |
|
71 | - { |
|
72 | - return $this->log(static::CRITICAL, $message, $context); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Detailed debug information. |
|
77 | - * @param mixed $message |
|
78 | - * @param array $context |
|
79 | - * @return static |
|
80 | - */ |
|
81 | - public function debug($message, array $context = []) |
|
82 | - { |
|
83 | - return $this->log(static::DEBUG, $message, $context); |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * System is unusable. |
|
88 | - * @param mixed $message |
|
89 | - * @param array $context |
|
90 | - * @return static |
|
91 | - */ |
|
92 | - public function emergency($message, array $context = []) |
|
93 | - { |
|
94 | - return $this->log(static::EMERGENCY, $message, $context); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Runtime errors that do not require immediate action but should typically be logged and monitored. |
|
99 | - * @param mixed $message |
|
100 | - * @param array $context |
|
101 | - * @return static |
|
102 | - */ |
|
103 | - public function error($message, array $context = []) |
|
104 | - { |
|
105 | - return $this->log(static::ERROR, $message, $context); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * @return string |
|
110 | - */ |
|
111 | - public function get() |
|
112 | - { |
|
113 | - return empty($this->log) |
|
114 | - ? __('Console is empty', 'site-reviews') |
|
115 | - : $this->log; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @return int |
|
120 | - */ |
|
121 | - public function getLevel() |
|
122 | - { |
|
123 | - return intval(apply_filters('site-reviews/console/level', static::INFO)); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @return array |
|
128 | - */ |
|
129 | - public function getLevels() |
|
130 | - { |
|
131 | - $constants = (new ReflectionClass(__CLASS__))->getConstants(); |
|
132 | - return array_map('strtolower', array_flip($constants)); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * @return string |
|
137 | - */ |
|
138 | - public function humanLevel() |
|
139 | - { |
|
140 | - $level = $this->getLevel(); |
|
141 | - return sprintf('%s (%d)', strtoupper(Arr::get($this->getLevels(), $level, 'unknown')), $level); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * @param string|null $valueIfEmpty |
|
146 | - * @return string |
|
147 | - */ |
|
148 | - public function humanSize($valueIfEmpty = null) |
|
149 | - { |
|
150 | - $bytes = $this->size(); |
|
151 | - if (empty($bytes) && is_string($valueIfEmpty)) { |
|
152 | - return $valueIfEmpty; |
|
153 | - } |
|
154 | - $exponent = floor(log(max($bytes, 1), 1024)); |
|
155 | - return round($bytes / pow(1024, $exponent), 2).' '.['bytes', 'KB', 'MB', 'GB'][$exponent]; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Interesting events |
|
160 | - * Example: User logs in, SQL logs. |
|
161 | - * @param mixed $message |
|
162 | - * @param array $context |
|
163 | - * @return static |
|
164 | - */ |
|
165 | - public function info($message, array $context = []) |
|
166 | - { |
|
167 | - return $this->log(static::INFO, $message, $context); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * @param int $level |
|
172 | - * @param mixed $message |
|
173 | - * @param array $context |
|
174 | - * @param string $backtraceLine |
|
175 | - * @return static |
|
176 | - */ |
|
177 | - public function log($level, $message, $context = [], $backtraceLine = '') |
|
178 | - { |
|
179 | - if (empty($backtraceLine)) { |
|
180 | - $backtraceLine = $this->getBacktraceLine(); |
|
181 | - } |
|
182 | - if ($this->canLogEntry($level, $backtraceLine)) { |
|
183 | - $levelName = Arr::get($this->getLevels(), $level); |
|
184 | - $context = Arr::consolidateArray($context); |
|
185 | - $backtraceLine = $this->normalizeBacktraceLine($backtraceLine); |
|
186 | - $message = $this->interpolate($message, $context); |
|
187 | - $entry = $this->buildLogEntry($levelName, $message, $backtraceLine); |
|
188 | - file_put_contents($this->file, $entry.PHP_EOL, FILE_APPEND | LOCK_EX); |
|
189 | - apply_filters('console', $message, $levelName, $backtraceLine); // Show in Blackbar plugin if installed |
|
190 | - $this->reset(); |
|
191 | - } |
|
192 | - return $this; |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * @return void |
|
197 | - */ |
|
198 | - public function logOnce() |
|
199 | - { |
|
200 | - $once = Arr::consolidateArray(glsr()->{$this->logOnceKey}); |
|
201 | - $levels = $this->getLevels(); |
|
202 | - foreach ($once as $entry) { |
|
203 | - $levelName = Arr::get($entry, 'level'); |
|
204 | - if (!in_array($levelName, $levels)) { |
|
205 | - continue; |
|
206 | - } |
|
207 | - $level = Arr::get(array_flip($levels), $levelName); |
|
208 | - $message = Arr::get($entry, 'message'); |
|
209 | - $backtraceLine = Arr::get($entry, 'backtrace'); |
|
210 | - $this->log($level, $message, [], $backtraceLine); |
|
211 | - } |
|
212 | - glsr()->{$this->logOnceKey} = []; |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * Normal but significant events. |
|
217 | - * @param mixed $message |
|
218 | - * @param array $context |
|
219 | - * @return static |
|
220 | - */ |
|
221 | - public function notice($message, array $context = []) |
|
222 | - { |
|
223 | - return $this->log(static::NOTICE, $message, $context); |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * @param string $levelName |
|
228 | - * @param string $handle |
|
229 | - * @param mixed $data |
|
230 | - * @return void |
|
231 | - */ |
|
232 | - public function once($levelName, $handle, $data) |
|
233 | - { |
|
234 | - $once = Arr::consolidateArray(glsr()->{$this->logOnceKey}); |
|
235 | - $filtered = array_filter($once, function ($entry) use ($levelName, $handle) { |
|
236 | - return Arr::get($entry, 'level') == $levelName |
|
237 | - && Arr::get($entry, 'handle') == $handle; |
|
238 | - }); |
|
239 | - if (!empty($filtered)) { |
|
240 | - return; |
|
241 | - } |
|
242 | - $once[] = [ |
|
243 | - 'backtrace' => $this->getBacktraceLineFromData($data), |
|
244 | - 'handle' => $handle, |
|
245 | - 'level' => $levelName, |
|
246 | - 'message' => '[RECURRING] '.$this->getMessageFromData($data), |
|
247 | - ]; |
|
248 | - glsr()->{$this->logOnceKey} = $once; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * @return int |
|
253 | - */ |
|
254 | - public function size() |
|
255 | - { |
|
256 | - return file_exists($this->file) |
|
257 | - ? filesize($this->file) |
|
258 | - : 0; |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * Exceptional occurrences that are not errors |
|
263 | - * Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong. |
|
264 | - * @param mixed $message |
|
265 | - * @param array $context |
|
266 | - * @return static |
|
267 | - */ |
|
268 | - public function warning($message, array $context = []) |
|
269 | - { |
|
270 | - return $this->log(static::WARNING, $message, $context); |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * @param array $backtrace |
|
275 | - * @param int $index |
|
276 | - * @return string |
|
277 | - */ |
|
278 | - protected function buildBacktraceLine($backtrace, $index) |
|
279 | - { |
|
280 | - return sprintf('%s:%s', |
|
281 | - Arr::get($backtrace, $index.'.file'), // realpath |
|
282 | - Arr::get($backtrace, $index.'.line') |
|
283 | - ); |
|
284 | - } |
|
285 | - |
|
286 | - /** |
|
287 | - * @param string $levelName |
|
288 | - * @param mixed $message |
|
289 | - * @param string $backtraceLine |
|
290 | - * @return string |
|
291 | - */ |
|
292 | - protected function buildLogEntry($levelName, $message, $backtraceLine = '') |
|
293 | - { |
|
294 | - return sprintf('[%s] %s [%s] %s', |
|
295 | - current_time('mysql'), |
|
296 | - strtoupper($levelName), |
|
297 | - $backtraceLine, |
|
298 | - $message |
|
299 | - ); |
|
300 | - } |
|
301 | - |
|
302 | - /** |
|
303 | - * @param int $level |
|
304 | - * @return bool |
|
305 | - */ |
|
306 | - protected function canLogEntry($level, $backtraceLine) |
|
307 | - { |
|
308 | - $levelExists = array_key_exists($level, $this->getLevels()); |
|
309 | - if (false === strpos($backtraceLine, glsr()->path())) { |
|
310 | - return $levelExists; // ignore level restriction if triggered outside of the plugin |
|
311 | - } |
|
312 | - return $levelExists && $level >= $this->getLevel(); |
|
313 | - } |
|
314 | - |
|
315 | - /** |
|
316 | - * @return void|string |
|
317 | - */ |
|
318 | - protected function getBacktraceLine() |
|
319 | - { |
|
320 | - $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 6); |
|
321 | - $search = array_search('log', glsr_array_column($backtrace, 'function')); |
|
322 | - if (false !== $search) { |
|
323 | - $index = '{closure}' == Arr::get($backtrace, ($search + 2).'.function') |
|
324 | - ? $search + 4 |
|
325 | - : $search + 1; |
|
326 | - return $this->buildBacktraceLine($backtrace, $index); |
|
327 | - } |
|
328 | - return 'Unknown'; |
|
329 | - } |
|
330 | - |
|
331 | - /** |
|
332 | - * @param mixed $data |
|
333 | - * @return string |
|
334 | - */ |
|
335 | - protected function getBacktraceLineFromData($data) |
|
336 | - { |
|
337 | - $backtrace = $data instanceof Throwable |
|
338 | - ? $data->getTrace() |
|
339 | - : debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
|
340 | - return $this->buildBacktraceLine($backtrace, 0); |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * @param mixed $data |
|
345 | - * @return string |
|
346 | - */ |
|
347 | - protected function getMessageFromData($data) |
|
348 | - { |
|
349 | - return $data instanceof Throwable |
|
350 | - ? $this->normalizeThrowableMessage($data->getMessage()) |
|
351 | - : print_r($data, 1); |
|
352 | - } |
|
353 | - |
|
354 | - /** |
|
355 | - * Interpolates context values into the message placeholders. |
|
356 | - * @param mixed $message |
|
357 | - * @param array $context |
|
358 | - * @return string |
|
359 | - */ |
|
360 | - protected function interpolate($message, $context = []) |
|
361 | - { |
|
362 | - if ($this->isObjectOrArray($message) || !is_array($context)) { |
|
363 | - return print_r($message, true); |
|
364 | - } |
|
365 | - $replace = []; |
|
366 | - foreach ($context as $key => $value) { |
|
367 | - $replace['{'.$key.'}'] = $this->normalizeValue($value); |
|
368 | - } |
|
369 | - return strtr($message, $replace); |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * @param mixed $value |
|
374 | - * @return bool |
|
375 | - */ |
|
376 | - protected function isObjectOrArray($value) |
|
377 | - { |
|
378 | - return is_object($value) || is_array($value); |
|
379 | - } |
|
380 | - |
|
381 | - /** |
|
382 | - * @param string $backtraceLine |
|
383 | - * @return string |
|
384 | - */ |
|
385 | - protected function normalizeBacktraceLine($backtraceLine) |
|
386 | - { |
|
387 | - $search = [ |
|
388 | - glsr()->path('plugin/'), |
|
389 | - glsr()->path('plugin/', false), |
|
390 | - trailingslashit(glsr()->path()), |
|
391 | - trailingslashit(glsr()->path('', false)), |
|
392 | - WP_CONTENT_DIR, |
|
393 | - ABSPATH, |
|
394 | - ]; |
|
395 | - return str_replace(array_unique($search), '', $backtraceLine); |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * @param string $message |
|
400 | - * @return string |
|
401 | - */ |
|
402 | - protected function normalizeThrowableMessage($message) |
|
403 | - { |
|
404 | - $calledIn = strpos($message, ', called in'); |
|
405 | - return false !== $calledIn |
|
406 | - ? substr($message, 0, $calledIn) |
|
407 | - : $message; |
|
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * @param mixed $value |
|
412 | - * @return string |
|
413 | - */ |
|
414 | - protected function normalizeValue($value) |
|
415 | - { |
|
416 | - if ($value instanceof DateTime) { |
|
417 | - $value = $value->format('Y-m-d H:i:s'); |
|
418 | - } elseif ($this->isObjectOrArray($value)) { |
|
419 | - $value = json_encode($value); |
|
420 | - } |
|
421 | - return (string) $value; |
|
422 | - } |
|
423 | - |
|
424 | - /** |
|
425 | - * @return void |
|
426 | - */ |
|
427 | - protected function reset() |
|
428 | - { |
|
429 | - if ($this->size() <= pow(1024, 2) / 8) { |
|
430 | - return; |
|
431 | - } |
|
432 | - $this->clear(); |
|
433 | - file_put_contents( |
|
434 | - $this->file, |
|
435 | - $this->buildLogEntry( |
|
436 | - static::NOTICE, |
|
437 | - __('Console was automatically cleared (128 KB maximum size)', 'site-reviews') |
|
438 | - ) |
|
439 | - ); |
|
440 | - } |
|
12 | + const DEBUG = 0; // Detailed debug information |
|
13 | + const INFO = 1; // Interesting events |
|
14 | + const NOTICE = 2; // Normal but significant events |
|
15 | + const WARNING = 4; // Exceptional occurrences that are not errors |
|
16 | + const ERROR = 8; // Runtime errors that do not require immediate action |
|
17 | + const CRITICAL = 16; // Critical conditions |
|
18 | + const ALERT = 32; // Action must be taken immediately |
|
19 | + const EMERGENCY = 64; // System is unusable |
|
20 | + |
|
21 | + protected $file; |
|
22 | + protected $log; |
|
23 | + protected $logOnceKey = 'glsr_log_once'; |
|
24 | + |
|
25 | + public function __construct() |
|
26 | + { |
|
27 | + $this->file = glsr()->path('console.log'); |
|
28 | + $this->log = file_exists($this->file) |
|
29 | + ? file_get_contents($this->file) |
|
30 | + : ''; |
|
31 | + $this->reset(); |
|
32 | + } |
|
33 | + |
|
34 | + /** |
|
35 | + * @return string |
|
36 | + */ |
|
37 | + public function __toString() |
|
38 | + { |
|
39 | + return $this->get(); |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Action must be taken immediately |
|
44 | + * Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up. |
|
45 | + * @param mixed $message |
|
46 | + * @param array $context |
|
47 | + * @return static |
|
48 | + */ |
|
49 | + public function alert($message, array $context = []) |
|
50 | + { |
|
51 | + return $this->log(static::ALERT, $message, $context); |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @return void |
|
56 | + */ |
|
57 | + public function clear() |
|
58 | + { |
|
59 | + $this->log = ''; |
|
60 | + file_put_contents($this->file, $this->log); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Critical conditions |
|
65 | + * Example: Application component unavailable, unexpected exception. |
|
66 | + * @param mixed $message |
|
67 | + * @param array $context |
|
68 | + * @return static |
|
69 | + */ |
|
70 | + public function critical($message, array $context = []) |
|
71 | + { |
|
72 | + return $this->log(static::CRITICAL, $message, $context); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Detailed debug information. |
|
77 | + * @param mixed $message |
|
78 | + * @param array $context |
|
79 | + * @return static |
|
80 | + */ |
|
81 | + public function debug($message, array $context = []) |
|
82 | + { |
|
83 | + return $this->log(static::DEBUG, $message, $context); |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * System is unusable. |
|
88 | + * @param mixed $message |
|
89 | + * @param array $context |
|
90 | + * @return static |
|
91 | + */ |
|
92 | + public function emergency($message, array $context = []) |
|
93 | + { |
|
94 | + return $this->log(static::EMERGENCY, $message, $context); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Runtime errors that do not require immediate action but should typically be logged and monitored. |
|
99 | + * @param mixed $message |
|
100 | + * @param array $context |
|
101 | + * @return static |
|
102 | + */ |
|
103 | + public function error($message, array $context = []) |
|
104 | + { |
|
105 | + return $this->log(static::ERROR, $message, $context); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * @return string |
|
110 | + */ |
|
111 | + public function get() |
|
112 | + { |
|
113 | + return empty($this->log) |
|
114 | + ? __('Console is empty', 'site-reviews') |
|
115 | + : $this->log; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @return int |
|
120 | + */ |
|
121 | + public function getLevel() |
|
122 | + { |
|
123 | + return intval(apply_filters('site-reviews/console/level', static::INFO)); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @return array |
|
128 | + */ |
|
129 | + public function getLevels() |
|
130 | + { |
|
131 | + $constants = (new ReflectionClass(__CLASS__))->getConstants(); |
|
132 | + return array_map('strtolower', array_flip($constants)); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * @return string |
|
137 | + */ |
|
138 | + public function humanLevel() |
|
139 | + { |
|
140 | + $level = $this->getLevel(); |
|
141 | + return sprintf('%s (%d)', strtoupper(Arr::get($this->getLevels(), $level, 'unknown')), $level); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * @param string|null $valueIfEmpty |
|
146 | + * @return string |
|
147 | + */ |
|
148 | + public function humanSize($valueIfEmpty = null) |
|
149 | + { |
|
150 | + $bytes = $this->size(); |
|
151 | + if (empty($bytes) && is_string($valueIfEmpty)) { |
|
152 | + return $valueIfEmpty; |
|
153 | + } |
|
154 | + $exponent = floor(log(max($bytes, 1), 1024)); |
|
155 | + return round($bytes / pow(1024, $exponent), 2).' '.['bytes', 'KB', 'MB', 'GB'][$exponent]; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Interesting events |
|
160 | + * Example: User logs in, SQL logs. |
|
161 | + * @param mixed $message |
|
162 | + * @param array $context |
|
163 | + * @return static |
|
164 | + */ |
|
165 | + public function info($message, array $context = []) |
|
166 | + { |
|
167 | + return $this->log(static::INFO, $message, $context); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * @param int $level |
|
172 | + * @param mixed $message |
|
173 | + * @param array $context |
|
174 | + * @param string $backtraceLine |
|
175 | + * @return static |
|
176 | + */ |
|
177 | + public function log($level, $message, $context = [], $backtraceLine = '') |
|
178 | + { |
|
179 | + if (empty($backtraceLine)) { |
|
180 | + $backtraceLine = $this->getBacktraceLine(); |
|
181 | + } |
|
182 | + if ($this->canLogEntry($level, $backtraceLine)) { |
|
183 | + $levelName = Arr::get($this->getLevels(), $level); |
|
184 | + $context = Arr::consolidateArray($context); |
|
185 | + $backtraceLine = $this->normalizeBacktraceLine($backtraceLine); |
|
186 | + $message = $this->interpolate($message, $context); |
|
187 | + $entry = $this->buildLogEntry($levelName, $message, $backtraceLine); |
|
188 | + file_put_contents($this->file, $entry.PHP_EOL, FILE_APPEND | LOCK_EX); |
|
189 | + apply_filters('console', $message, $levelName, $backtraceLine); // Show in Blackbar plugin if installed |
|
190 | + $this->reset(); |
|
191 | + } |
|
192 | + return $this; |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * @return void |
|
197 | + */ |
|
198 | + public function logOnce() |
|
199 | + { |
|
200 | + $once = Arr::consolidateArray(glsr()->{$this->logOnceKey}); |
|
201 | + $levels = $this->getLevels(); |
|
202 | + foreach ($once as $entry) { |
|
203 | + $levelName = Arr::get($entry, 'level'); |
|
204 | + if (!in_array($levelName, $levels)) { |
|
205 | + continue; |
|
206 | + } |
|
207 | + $level = Arr::get(array_flip($levels), $levelName); |
|
208 | + $message = Arr::get($entry, 'message'); |
|
209 | + $backtraceLine = Arr::get($entry, 'backtrace'); |
|
210 | + $this->log($level, $message, [], $backtraceLine); |
|
211 | + } |
|
212 | + glsr()->{$this->logOnceKey} = []; |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * Normal but significant events. |
|
217 | + * @param mixed $message |
|
218 | + * @param array $context |
|
219 | + * @return static |
|
220 | + */ |
|
221 | + public function notice($message, array $context = []) |
|
222 | + { |
|
223 | + return $this->log(static::NOTICE, $message, $context); |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * @param string $levelName |
|
228 | + * @param string $handle |
|
229 | + * @param mixed $data |
|
230 | + * @return void |
|
231 | + */ |
|
232 | + public function once($levelName, $handle, $data) |
|
233 | + { |
|
234 | + $once = Arr::consolidateArray(glsr()->{$this->logOnceKey}); |
|
235 | + $filtered = array_filter($once, function ($entry) use ($levelName, $handle) { |
|
236 | + return Arr::get($entry, 'level') == $levelName |
|
237 | + && Arr::get($entry, 'handle') == $handle; |
|
238 | + }); |
|
239 | + if (!empty($filtered)) { |
|
240 | + return; |
|
241 | + } |
|
242 | + $once[] = [ |
|
243 | + 'backtrace' => $this->getBacktraceLineFromData($data), |
|
244 | + 'handle' => $handle, |
|
245 | + 'level' => $levelName, |
|
246 | + 'message' => '[RECURRING] '.$this->getMessageFromData($data), |
|
247 | + ]; |
|
248 | + glsr()->{$this->logOnceKey} = $once; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * @return int |
|
253 | + */ |
|
254 | + public function size() |
|
255 | + { |
|
256 | + return file_exists($this->file) |
|
257 | + ? filesize($this->file) |
|
258 | + : 0; |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * Exceptional occurrences that are not errors |
|
263 | + * Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong. |
|
264 | + * @param mixed $message |
|
265 | + * @param array $context |
|
266 | + * @return static |
|
267 | + */ |
|
268 | + public function warning($message, array $context = []) |
|
269 | + { |
|
270 | + return $this->log(static::WARNING, $message, $context); |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * @param array $backtrace |
|
275 | + * @param int $index |
|
276 | + * @return string |
|
277 | + */ |
|
278 | + protected function buildBacktraceLine($backtrace, $index) |
|
279 | + { |
|
280 | + return sprintf('%s:%s', |
|
281 | + Arr::get($backtrace, $index.'.file'), // realpath |
|
282 | + Arr::get($backtrace, $index.'.line') |
|
283 | + ); |
|
284 | + } |
|
285 | + |
|
286 | + /** |
|
287 | + * @param string $levelName |
|
288 | + * @param mixed $message |
|
289 | + * @param string $backtraceLine |
|
290 | + * @return string |
|
291 | + */ |
|
292 | + protected function buildLogEntry($levelName, $message, $backtraceLine = '') |
|
293 | + { |
|
294 | + return sprintf('[%s] %s [%s] %s', |
|
295 | + current_time('mysql'), |
|
296 | + strtoupper($levelName), |
|
297 | + $backtraceLine, |
|
298 | + $message |
|
299 | + ); |
|
300 | + } |
|
301 | + |
|
302 | + /** |
|
303 | + * @param int $level |
|
304 | + * @return bool |
|
305 | + */ |
|
306 | + protected function canLogEntry($level, $backtraceLine) |
|
307 | + { |
|
308 | + $levelExists = array_key_exists($level, $this->getLevels()); |
|
309 | + if (false === strpos($backtraceLine, glsr()->path())) { |
|
310 | + return $levelExists; // ignore level restriction if triggered outside of the plugin |
|
311 | + } |
|
312 | + return $levelExists && $level >= $this->getLevel(); |
|
313 | + } |
|
314 | + |
|
315 | + /** |
|
316 | + * @return void|string |
|
317 | + */ |
|
318 | + protected function getBacktraceLine() |
|
319 | + { |
|
320 | + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 6); |
|
321 | + $search = array_search('log', glsr_array_column($backtrace, 'function')); |
|
322 | + if (false !== $search) { |
|
323 | + $index = '{closure}' == Arr::get($backtrace, ($search + 2).'.function') |
|
324 | + ? $search + 4 |
|
325 | + : $search + 1; |
|
326 | + return $this->buildBacktraceLine($backtrace, $index); |
|
327 | + } |
|
328 | + return 'Unknown'; |
|
329 | + } |
|
330 | + |
|
331 | + /** |
|
332 | + * @param mixed $data |
|
333 | + * @return string |
|
334 | + */ |
|
335 | + protected function getBacktraceLineFromData($data) |
|
336 | + { |
|
337 | + $backtrace = $data instanceof Throwable |
|
338 | + ? $data->getTrace() |
|
339 | + : debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
|
340 | + return $this->buildBacktraceLine($backtrace, 0); |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * @param mixed $data |
|
345 | + * @return string |
|
346 | + */ |
|
347 | + protected function getMessageFromData($data) |
|
348 | + { |
|
349 | + return $data instanceof Throwable |
|
350 | + ? $this->normalizeThrowableMessage($data->getMessage()) |
|
351 | + : print_r($data, 1); |
|
352 | + } |
|
353 | + |
|
354 | + /** |
|
355 | + * Interpolates context values into the message placeholders. |
|
356 | + * @param mixed $message |
|
357 | + * @param array $context |
|
358 | + * @return string |
|
359 | + */ |
|
360 | + protected function interpolate($message, $context = []) |
|
361 | + { |
|
362 | + if ($this->isObjectOrArray($message) || !is_array($context)) { |
|
363 | + return print_r($message, true); |
|
364 | + } |
|
365 | + $replace = []; |
|
366 | + foreach ($context as $key => $value) { |
|
367 | + $replace['{'.$key.'}'] = $this->normalizeValue($value); |
|
368 | + } |
|
369 | + return strtr($message, $replace); |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * @param mixed $value |
|
374 | + * @return bool |
|
375 | + */ |
|
376 | + protected function isObjectOrArray($value) |
|
377 | + { |
|
378 | + return is_object($value) || is_array($value); |
|
379 | + } |
|
380 | + |
|
381 | + /** |
|
382 | + * @param string $backtraceLine |
|
383 | + * @return string |
|
384 | + */ |
|
385 | + protected function normalizeBacktraceLine($backtraceLine) |
|
386 | + { |
|
387 | + $search = [ |
|
388 | + glsr()->path('plugin/'), |
|
389 | + glsr()->path('plugin/', false), |
|
390 | + trailingslashit(glsr()->path()), |
|
391 | + trailingslashit(glsr()->path('', false)), |
|
392 | + WP_CONTENT_DIR, |
|
393 | + ABSPATH, |
|
394 | + ]; |
|
395 | + return str_replace(array_unique($search), '', $backtraceLine); |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * @param string $message |
|
400 | + * @return string |
|
401 | + */ |
|
402 | + protected function normalizeThrowableMessage($message) |
|
403 | + { |
|
404 | + $calledIn = strpos($message, ', called in'); |
|
405 | + return false !== $calledIn |
|
406 | + ? substr($message, 0, $calledIn) |
|
407 | + : $message; |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * @param mixed $value |
|
412 | + * @return string |
|
413 | + */ |
|
414 | + protected function normalizeValue($value) |
|
415 | + { |
|
416 | + if ($value instanceof DateTime) { |
|
417 | + $value = $value->format('Y-m-d H:i:s'); |
|
418 | + } elseif ($this->isObjectOrArray($value)) { |
|
419 | + $value = json_encode($value); |
|
420 | + } |
|
421 | + return (string) $value; |
|
422 | + } |
|
423 | + |
|
424 | + /** |
|
425 | + * @return void |
|
426 | + */ |
|
427 | + protected function reset() |
|
428 | + { |
|
429 | + if ($this->size() <= pow(1024, 2) / 8) { |
|
430 | + return; |
|
431 | + } |
|
432 | + $this->clear(); |
|
433 | + file_put_contents( |
|
434 | + $this->file, |
|
435 | + $this->buildLogEntry( |
|
436 | + static::NOTICE, |
|
437 | + __('Console was automatically cleared (128 KB maximum size)', 'site-reviews') |
|
438 | + ) |
|
439 | + ); |
|
440 | + } |
|
441 | 441 | } |
@@ -13,248 +13,248 @@ |
||
13 | 13 | |
14 | 14 | abstract class BaseType implements ArrayAccess, JsonSerializable, Type |
15 | 15 | { |
16 | - /** |
|
17 | - * @var array |
|
18 | - */ |
|
19 | - public $allowed = []; |
|
16 | + /** |
|
17 | + * @var array |
|
18 | + */ |
|
19 | + public $allowed = []; |
|
20 | 20 | |
21 | - /** |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - public $parents = []; |
|
21 | + /** |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + public $parents = []; |
|
25 | 25 | |
26 | - /** |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - protected $properties = []; |
|
26 | + /** |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + protected $properties = []; |
|
30 | 30 | |
31 | - /** |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - protected $type; |
|
31 | + /** |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + protected $type; |
|
35 | 35 | |
36 | - /** |
|
37 | - * @param string $method |
|
38 | - * @return static |
|
39 | - */ |
|
40 | - public function __call($method, array $arguments) |
|
41 | - { |
|
42 | - return $this->setProperty($method, Arr::get($arguments, 0)); |
|
43 | - } |
|
36 | + /** |
|
37 | + * @param string $method |
|
38 | + * @return static |
|
39 | + */ |
|
40 | + public function __call($method, array $arguments) |
|
41 | + { |
|
42 | + return $this->setProperty($method, Arr::get($arguments, 0)); |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param string $type |
|
47 | - */ |
|
48 | - public function __construct($type = null) |
|
49 | - { |
|
50 | - $this->type = !is_string($type) |
|
51 | - ? (new ReflectionClass($this))->getShortName() |
|
52 | - : $type; |
|
53 | - $this->setAllowedProperties(); |
|
54 | - } |
|
45 | + /** |
|
46 | + * @param string $type |
|
47 | + */ |
|
48 | + public function __construct($type = null) |
|
49 | + { |
|
50 | + $this->type = !is_string($type) |
|
51 | + ? (new ReflectionClass($this))->getShortName() |
|
52 | + : $type; |
|
53 | + $this->setAllowedProperties(); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @return string |
|
58 | - */ |
|
59 | - public function __toString() |
|
60 | - { |
|
61 | - return $this->toScript(); |
|
62 | - } |
|
56 | + /** |
|
57 | + * @return string |
|
58 | + */ |
|
59 | + public function __toString() |
|
60 | + { |
|
61 | + return $this->toScript(); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @return static |
|
66 | - */ |
|
67 | - public function addProperties(array $properties) |
|
68 | - { |
|
69 | - foreach ($properties as $property => $value) { |
|
70 | - $this->setProperty($property, $value); |
|
71 | - } |
|
72 | - return $this; |
|
73 | - } |
|
64 | + /** |
|
65 | + * @return static |
|
66 | + */ |
|
67 | + public function addProperties(array $properties) |
|
68 | + { |
|
69 | + foreach ($properties as $property => $value) { |
|
70 | + $this->setProperty($property, $value); |
|
71 | + } |
|
72 | + return $this; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * @return string |
|
77 | - */ |
|
78 | - public function getContext() |
|
79 | - { |
|
80 | - return 'https://schema.org'; |
|
81 | - } |
|
75 | + /** |
|
76 | + * @return string |
|
77 | + */ |
|
78 | + public function getContext() |
|
79 | + { |
|
80 | + return 'https://schema.org'; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * @return array |
|
85 | - */ |
|
86 | - public function getProperties() |
|
87 | - { |
|
88 | - return $this->properties; |
|
89 | - } |
|
83 | + /** |
|
84 | + * @return array |
|
85 | + */ |
|
86 | + public function getProperties() |
|
87 | + { |
|
88 | + return $this->properties; |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * @param string $property |
|
93 | - * @param mixed $default |
|
94 | - * @return mixed |
|
95 | - */ |
|
96 | - public function getProperty($property, $default = null) |
|
97 | - { |
|
98 | - return Arr::get($this->properties, $property, $default); |
|
99 | - } |
|
91 | + /** |
|
92 | + * @param string $property |
|
93 | + * @param mixed $default |
|
94 | + * @return mixed |
|
95 | + */ |
|
96 | + public function getProperty($property, $default = null) |
|
97 | + { |
|
98 | + return Arr::get($this->properties, $property, $default); |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * @return string |
|
103 | - */ |
|
104 | - public function getType() |
|
105 | - { |
|
106 | - return $this->type; |
|
107 | - } |
|
101 | + /** |
|
102 | + * @return string |
|
103 | + */ |
|
104 | + public function getType() |
|
105 | + { |
|
106 | + return $this->type; |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * @param bool $condition |
|
111 | - * @param mixed $callback |
|
112 | - * @return static |
|
113 | - */ |
|
114 | - public function doIf($condition, $callback) |
|
115 | - { |
|
116 | - if ($condition) { |
|
117 | - $callback($this); |
|
118 | - } |
|
119 | - return $this; |
|
120 | - } |
|
109 | + /** |
|
110 | + * @param bool $condition |
|
111 | + * @param mixed $callback |
|
112 | + * @return static |
|
113 | + */ |
|
114 | + public function doIf($condition, $callback) |
|
115 | + { |
|
116 | + if ($condition) { |
|
117 | + $callback($this); |
|
118 | + } |
|
119 | + return $this; |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * @return array |
|
124 | - */ |
|
125 | - public function jsonSerialize() |
|
126 | - { |
|
127 | - return $this->toArray(); |
|
128 | - } |
|
122 | + /** |
|
123 | + * @return array |
|
124 | + */ |
|
125 | + public function jsonSerialize() |
|
126 | + { |
|
127 | + return $this->toArray(); |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * @param mixed $offset |
|
132 | - * @return bool |
|
133 | - */ |
|
134 | - public function offsetExists($offset) |
|
135 | - { |
|
136 | - return array_key_exists($offset, $this->properties); |
|
137 | - } |
|
130 | + /** |
|
131 | + * @param mixed $offset |
|
132 | + * @return bool |
|
133 | + */ |
|
134 | + public function offsetExists($offset) |
|
135 | + { |
|
136 | + return array_key_exists($offset, $this->properties); |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * @param string $offset |
|
141 | - * @return mixed |
|
142 | - */ |
|
143 | - public function offsetGet($offset) |
|
144 | - { |
|
145 | - return $this->getProperty($offset); |
|
146 | - } |
|
139 | + /** |
|
140 | + * @param string $offset |
|
141 | + * @return mixed |
|
142 | + */ |
|
143 | + public function offsetGet($offset) |
|
144 | + { |
|
145 | + return $this->getProperty($offset); |
|
146 | + } |
|
147 | 147 | |
148 | - /** |
|
149 | - * @param string $offset |
|
150 | - * @param mixed $value |
|
151 | - * @return void |
|
152 | - */ |
|
153 | - public function offsetSet($offset, $value) |
|
154 | - { |
|
155 | - $this->setProperty($offset, $value); |
|
156 | - } |
|
148 | + /** |
|
149 | + * @param string $offset |
|
150 | + * @param mixed $value |
|
151 | + * @return void |
|
152 | + */ |
|
153 | + public function offsetSet($offset, $value) |
|
154 | + { |
|
155 | + $this->setProperty($offset, $value); |
|
156 | + } |
|
157 | 157 | |
158 | - /** |
|
159 | - * @param string $offset |
|
160 | - * @return void |
|
161 | - */ |
|
162 | - public function offsetUnset($offset) |
|
163 | - { |
|
164 | - unset($this->properties[$offset]); |
|
165 | - } |
|
158 | + /** |
|
159 | + * @param string $offset |
|
160 | + * @return void |
|
161 | + */ |
|
162 | + public function offsetUnset($offset) |
|
163 | + { |
|
164 | + unset($this->properties[$offset]); |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * @param string $property |
|
169 | - * @param mixed $value |
|
170 | - * @return static |
|
171 | - */ |
|
172 | - public function setProperty($property, $value) |
|
173 | - { |
|
174 | - if (!in_array($property, $this->allowed) |
|
175 | - && 'UnknownType' != (new ReflectionClass($this))->getShortName()) { |
|
176 | - glsr_log()->warning($this->getType().' does not allow the "'.$property.'" property'); |
|
177 | - return $this; |
|
178 | - } |
|
179 | - $this->properties[$property] = $value; |
|
180 | - return $this; |
|
181 | - } |
|
167 | + /** |
|
168 | + * @param string $property |
|
169 | + * @param mixed $value |
|
170 | + * @return static |
|
171 | + */ |
|
172 | + public function setProperty($property, $value) |
|
173 | + { |
|
174 | + if (!in_array($property, $this->allowed) |
|
175 | + && 'UnknownType' != (new ReflectionClass($this))->getShortName()) { |
|
176 | + glsr_log()->warning($this->getType().' does not allow the "'.$property.'" property'); |
|
177 | + return $this; |
|
178 | + } |
|
179 | + $this->properties[$property] = $value; |
|
180 | + return $this; |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * @return array |
|
185 | - */ |
|
186 | - public function toArray() |
|
187 | - { |
|
188 | - return [ |
|
189 | - '@context' => $this->getContext(), |
|
190 | - '@type' => $this->getType(), |
|
191 | - ] + $this->serializeProperty($this->getProperties()); |
|
192 | - } |
|
183 | + /** |
|
184 | + * @return array |
|
185 | + */ |
|
186 | + public function toArray() |
|
187 | + { |
|
188 | + return [ |
|
189 | + '@context' => $this->getContext(), |
|
190 | + '@type' => $this->getType(), |
|
191 | + ] + $this->serializeProperty($this->getProperties()); |
|
192 | + } |
|
193 | 193 | |
194 | - /** |
|
195 | - * @return string |
|
196 | - */ |
|
197 | - public function toScript() |
|
198 | - { |
|
199 | - return sprintf('<script type="application/ld+json">%s</script>', |
|
200 | - json_encode($this->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) |
|
201 | - ); |
|
202 | - } |
|
194 | + /** |
|
195 | + * @return string |
|
196 | + */ |
|
197 | + public function toScript() |
|
198 | + { |
|
199 | + return sprintf('<script type="application/ld+json">%s</script>', |
|
200 | + json_encode($this->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) |
|
201 | + ); |
|
202 | + } |
|
203 | 203 | |
204 | - /** |
|
205 | - * @param array|null $parents |
|
206 | - * @return array |
|
207 | - */ |
|
208 | - protected function getParents($parents = null) |
|
209 | - { |
|
210 | - if (!isset($parents)) { |
|
211 | - $parents = $this->parents; |
|
212 | - } |
|
213 | - $newParents = $parents; |
|
214 | - foreach ($parents as $parent) { |
|
215 | - $parentClass = Helper::buildClassName($parent, __NAMESPACE__); |
|
216 | - if (!class_exists($parentClass)) { |
|
217 | - continue; |
|
218 | - } |
|
219 | - $newParents = array_merge($newParents, $this->getParents((new $parentClass())->parents)); |
|
220 | - } |
|
221 | - return array_values(array_unique($newParents)); |
|
222 | - } |
|
204 | + /** |
|
205 | + * @param array|null $parents |
|
206 | + * @return array |
|
207 | + */ |
|
208 | + protected function getParents($parents = null) |
|
209 | + { |
|
210 | + if (!isset($parents)) { |
|
211 | + $parents = $this->parents; |
|
212 | + } |
|
213 | + $newParents = $parents; |
|
214 | + foreach ($parents as $parent) { |
|
215 | + $parentClass = Helper::buildClassName($parent, __NAMESPACE__); |
|
216 | + if (!class_exists($parentClass)) { |
|
217 | + continue; |
|
218 | + } |
|
219 | + $newParents = array_merge($newParents, $this->getParents((new $parentClass())->parents)); |
|
220 | + } |
|
221 | + return array_values(array_unique($newParents)); |
|
222 | + } |
|
223 | 223 | |
224 | - /** |
|
225 | - * @return void |
|
226 | - */ |
|
227 | - protected function setAllowedProperties() |
|
228 | - { |
|
229 | - $parents = $this->getParents(); |
|
230 | - foreach ($parents as $parent) { |
|
231 | - $parentClass = Helper::buildClassName($parent, __NAMESPACE__); |
|
232 | - if (!class_exists($parentClass)) { |
|
233 | - continue; |
|
234 | - } |
|
235 | - $this->allowed = array_values(array_unique(array_merge((new $parentClass())->allowed, $this->allowed))); |
|
236 | - } |
|
237 | - } |
|
224 | + /** |
|
225 | + * @return void |
|
226 | + */ |
|
227 | + protected function setAllowedProperties() |
|
228 | + { |
|
229 | + $parents = $this->getParents(); |
|
230 | + foreach ($parents as $parent) { |
|
231 | + $parentClass = Helper::buildClassName($parent, __NAMESPACE__); |
|
232 | + if (!class_exists($parentClass)) { |
|
233 | + continue; |
|
234 | + } |
|
235 | + $this->allowed = array_values(array_unique(array_merge((new $parentClass())->allowed, $this->allowed))); |
|
236 | + } |
|
237 | + } |
|
238 | 238 | |
239 | - /** |
|
240 | - * @param mixed $property |
|
241 | - * @return array|string |
|
242 | - */ |
|
243 | - protected function serializeProperty($property) |
|
244 | - { |
|
245 | - if (is_array($property)) { |
|
246 | - return array_map([$this, 'serializeProperty'], $property); |
|
247 | - } |
|
248 | - if ($property instanceof Type) { |
|
249 | - $property = $property->toArray(); |
|
250 | - unset($property['@context']); |
|
251 | - } |
|
252 | - if ($property instanceof DateTimeInterface) { |
|
253 | - $property = $property->format(DateTime::ATOM); |
|
254 | - } |
|
255 | - if (is_object($property)) { |
|
256 | - throw new InvalidProperty(); |
|
257 | - } |
|
258 | - return $property; |
|
259 | - } |
|
239 | + /** |
|
240 | + * @param mixed $property |
|
241 | + * @return array|string |
|
242 | + */ |
|
243 | + protected function serializeProperty($property) |
|
244 | + { |
|
245 | + if (is_array($property)) { |
|
246 | + return array_map([$this, 'serializeProperty'], $property); |
|
247 | + } |
|
248 | + if ($property instanceof Type) { |
|
249 | + $property = $property->toArray(); |
|
250 | + unset($property['@context']); |
|
251 | + } |
|
252 | + if ($property instanceof DateTimeInterface) { |
|
253 | + $property = $property->format(DateTime::ATOM); |
|
254 | + } |
|
255 | + if (is_object($property)) { |
|
256 | + throw new InvalidProperty(); |
|
257 | + } |
|
258 | + return $property; |
|
259 | + } |
|
260 | 260 | } |
@@ -11,239 +11,239 @@ |
||
11 | 11 | |
12 | 12 | class Settings |
13 | 13 | { |
14 | - /** |
|
15 | - * @var array |
|
16 | - */ |
|
17 | - public $settings; |
|
18 | - |
|
19 | - /** |
|
20 | - * @param string $id |
|
21 | - * @return string |
|
22 | - */ |
|
23 | - public function buildFields($id) |
|
24 | - { |
|
25 | - $this->settings = glsr(DefaultsManager::class)->settings(); |
|
26 | - $method = Helper::buildMethodName($id, 'getTemplateDataFor'); |
|
27 | - $data = !method_exists($this, $method) |
|
28 | - ? $this->getTemplateData($id) |
|
29 | - : $this->$method($id); |
|
30 | - return glsr(Template::class)->build('pages/settings/'.$id, $data); |
|
31 | - } |
|
32 | - |
|
33 | - /** |
|
34 | - * @return string |
|
35 | - */ |
|
36 | - protected function getFieldDefault(array $field) |
|
37 | - { |
|
38 | - return Arr::get($field, 'default'); |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * @return string |
|
43 | - */ |
|
44 | - protected function getFieldNameForDependsOn($path) |
|
45 | - { |
|
46 | - $fieldName = Str::convertPathToName($path, OptionManager::databaseKey()); |
|
47 | - return $this->isMultiDependency($path) |
|
48 | - ? $fieldName.'[]' |
|
49 | - : $fieldName; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @return array |
|
54 | - */ |
|
55 | - protected function getSettingFields($path) |
|
56 | - { |
|
57 | - return array_filter($this->settings, function ($key) use ($path) { |
|
58 | - return Str::startsWith($path, $key); |
|
59 | - }, ARRAY_FILTER_USE_KEY); |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * @return string |
|
64 | - */ |
|
65 | - protected function getSettingRows(array $fields) |
|
66 | - { |
|
67 | - $rows = ''; |
|
68 | - foreach ($fields as $name => $field) { |
|
69 | - $field = wp_parse_args($field, [ |
|
70 | - 'is_setting' => true, |
|
71 | - 'name' => $name, |
|
72 | - ]); |
|
73 | - $rows.= new Field($this->normalize($field)); |
|
74 | - } |
|
75 | - return $rows; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * @param string $id |
|
80 | - * @return array |
|
81 | - */ |
|
82 | - protected function getTemplateData($id) |
|
83 | - { |
|
84 | - $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
85 | - return [ |
|
86 | - 'context' => [ |
|
87 | - 'rows' => $this->getSettingRows($fields), |
|
88 | - ], |
|
89 | - ]; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * @param string $id |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - protected function getTemplateDataForAddons($id) |
|
97 | - { |
|
98 | - $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
99 | - $settings = Arr::convertDotNotationArray($fields); |
|
100 | - $settingKeys = array_keys($settings['settings']['addons']); |
|
101 | - $results = []; |
|
102 | - foreach ($settingKeys as $key) { |
|
103 | - $addonFields = array_filter($fields, function ($path) use ($key) { |
|
104 | - return Str::startsWith('settings.addons.'.$key, $path); |
|
105 | - }, ARRAY_FILTER_USE_KEY); |
|
106 | - $results[$key] = $this->getSettingRows($addonFields); |
|
107 | - } |
|
108 | - ksort($results); |
|
109 | - return [ |
|
110 | - 'settings' => $results, |
|
111 | - ]; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @param string $id |
|
116 | - * @return array |
|
117 | - */ |
|
118 | - protected function getTemplateDataForLicenses($id) |
|
119 | - { |
|
120 | - $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
121 | - ksort($fields); |
|
122 | - return [ |
|
123 | - 'context' => [ |
|
124 | - 'rows' => $this->getSettingRows($fields), |
|
125 | - ], |
|
126 | - ]; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @return array |
|
131 | - */ |
|
132 | - protected function getTemplateDataForTranslations() |
|
133 | - { |
|
134 | - $translations = glsr(Translation::class)->renderAll(); |
|
135 | - $class = empty($translations) |
|
136 | - ? 'glsr-hidden' |
|
137 | - : ''; |
|
138 | - return [ |
|
139 | - 'context' => [ |
|
140 | - 'class' => $class, |
|
141 | - 'database_key' => OptionManager::databaseKey(), |
|
142 | - 'translations' => $translations, |
|
143 | - ], |
|
144 | - ]; |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @param string $path |
|
149 | - * @param string|array $expectedValue |
|
150 | - * @return bool |
|
151 | - */ |
|
152 | - protected function isFieldHidden($path, $expectedValue) |
|
153 | - { |
|
154 | - $optionValue = glsr(OptionManager::class)->get( |
|
155 | - $path, |
|
156 | - Arr::get(glsr()->defaults, $path) |
|
157 | - ); |
|
158 | - if (is_array($expectedValue)) { |
|
159 | - return is_array($optionValue) |
|
160 | - ? 0 === count(array_intersect($optionValue, $expectedValue)) |
|
161 | - : !in_array($optionValue, $expectedValue); |
|
162 | - } |
|
163 | - return $optionValue != $expectedValue; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @return bool |
|
168 | - */ |
|
169 | - protected function isMultiDependency($path) |
|
170 | - { |
|
171 | - if (isset($this->settings[$path])) { |
|
172 | - $field = $this->settings[$path]; |
|
173 | - return ('checkbox' == $field['type'] && !empty($field['options'])) |
|
174 | - || !empty($field['multiple']); |
|
175 | - } |
|
176 | - return false; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * @return array |
|
181 | - */ |
|
182 | - protected function normalize(array $field) |
|
183 | - { |
|
184 | - $field = $this->normalizeDependsOn($field); |
|
185 | - $field = $this->normalizeLabelAndLegend($field); |
|
186 | - $field = $this->normalizeValue($field); |
|
187 | - return $field; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * @return array |
|
192 | - */ |
|
193 | - protected function normalizeDependsOn(array $field) |
|
194 | - { |
|
195 | - if (!empty($field['depends_on']) && is_array($field['depends_on'])) { |
|
196 | - $isFieldHidden = false; |
|
197 | - $conditions = []; |
|
198 | - foreach ($field['depends_on'] as $path => $value) { |
|
199 | - $conditions[] = [ |
|
200 | - 'name' => $this->getFieldNameForDependsOn($path), |
|
201 | - 'value' => $value, |
|
202 | - ]; |
|
203 | - if ($this->isFieldHidden($path, $value)) { |
|
204 | - $isFieldHidden = true; |
|
205 | - } |
|
206 | - } |
|
207 | - $field['data-depends'] = json_encode($conditions, JSON_HEX_APOS | JSON_HEX_QUOT); |
|
208 | - $field['is_hidden'] = $isFieldHidden; |
|
209 | - } |
|
210 | - return $field; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * @return array |
|
215 | - */ |
|
216 | - protected function normalizeLabelAndLegend(array $field) |
|
217 | - { |
|
218 | - if (!empty($field['label'])) { |
|
219 | - $field['legend'] = $field['label']; |
|
220 | - unset($field['label']); |
|
221 | - } else { |
|
222 | - $field['is_valid'] = false; |
|
223 | - glsr_log()->warning('Setting field is missing a label')->debug($field); |
|
224 | - } |
|
225 | - return $field; |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * @return array |
|
230 | - */ |
|
231 | - protected function normalizeValue(array $field) |
|
232 | - { |
|
233 | - if (!isset($field['value'])) { |
|
234 | - $field['value'] = glsr(OptionManager::class)->get( |
|
235 | - $field['name'], |
|
236 | - $this->getFieldDefault($field) |
|
237 | - ); |
|
238 | - } |
|
239 | - return $field; |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * @return string |
|
244 | - */ |
|
245 | - protected function normalizeSettingPath($path) |
|
246 | - { |
|
247 | - return Str::prefix('settings.', rtrim($path, '.')); |
|
248 | - } |
|
14 | + /** |
|
15 | + * @var array |
|
16 | + */ |
|
17 | + public $settings; |
|
18 | + |
|
19 | + /** |
|
20 | + * @param string $id |
|
21 | + * @return string |
|
22 | + */ |
|
23 | + public function buildFields($id) |
|
24 | + { |
|
25 | + $this->settings = glsr(DefaultsManager::class)->settings(); |
|
26 | + $method = Helper::buildMethodName($id, 'getTemplateDataFor'); |
|
27 | + $data = !method_exists($this, $method) |
|
28 | + ? $this->getTemplateData($id) |
|
29 | + : $this->$method($id); |
|
30 | + return glsr(Template::class)->build('pages/settings/'.$id, $data); |
|
31 | + } |
|
32 | + |
|
33 | + /** |
|
34 | + * @return string |
|
35 | + */ |
|
36 | + protected function getFieldDefault(array $field) |
|
37 | + { |
|
38 | + return Arr::get($field, 'default'); |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * @return string |
|
43 | + */ |
|
44 | + protected function getFieldNameForDependsOn($path) |
|
45 | + { |
|
46 | + $fieldName = Str::convertPathToName($path, OptionManager::databaseKey()); |
|
47 | + return $this->isMultiDependency($path) |
|
48 | + ? $fieldName.'[]' |
|
49 | + : $fieldName; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @return array |
|
54 | + */ |
|
55 | + protected function getSettingFields($path) |
|
56 | + { |
|
57 | + return array_filter($this->settings, function ($key) use ($path) { |
|
58 | + return Str::startsWith($path, $key); |
|
59 | + }, ARRAY_FILTER_USE_KEY); |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * @return string |
|
64 | + */ |
|
65 | + protected function getSettingRows(array $fields) |
|
66 | + { |
|
67 | + $rows = ''; |
|
68 | + foreach ($fields as $name => $field) { |
|
69 | + $field = wp_parse_args($field, [ |
|
70 | + 'is_setting' => true, |
|
71 | + 'name' => $name, |
|
72 | + ]); |
|
73 | + $rows.= new Field($this->normalize($field)); |
|
74 | + } |
|
75 | + return $rows; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * @param string $id |
|
80 | + * @return array |
|
81 | + */ |
|
82 | + protected function getTemplateData($id) |
|
83 | + { |
|
84 | + $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
85 | + return [ |
|
86 | + 'context' => [ |
|
87 | + 'rows' => $this->getSettingRows($fields), |
|
88 | + ], |
|
89 | + ]; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * @param string $id |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + protected function getTemplateDataForAddons($id) |
|
97 | + { |
|
98 | + $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
99 | + $settings = Arr::convertDotNotationArray($fields); |
|
100 | + $settingKeys = array_keys($settings['settings']['addons']); |
|
101 | + $results = []; |
|
102 | + foreach ($settingKeys as $key) { |
|
103 | + $addonFields = array_filter($fields, function ($path) use ($key) { |
|
104 | + return Str::startsWith('settings.addons.'.$key, $path); |
|
105 | + }, ARRAY_FILTER_USE_KEY); |
|
106 | + $results[$key] = $this->getSettingRows($addonFields); |
|
107 | + } |
|
108 | + ksort($results); |
|
109 | + return [ |
|
110 | + 'settings' => $results, |
|
111 | + ]; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @param string $id |
|
116 | + * @return array |
|
117 | + */ |
|
118 | + protected function getTemplateDataForLicenses($id) |
|
119 | + { |
|
120 | + $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
121 | + ksort($fields); |
|
122 | + return [ |
|
123 | + 'context' => [ |
|
124 | + 'rows' => $this->getSettingRows($fields), |
|
125 | + ], |
|
126 | + ]; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @return array |
|
131 | + */ |
|
132 | + protected function getTemplateDataForTranslations() |
|
133 | + { |
|
134 | + $translations = glsr(Translation::class)->renderAll(); |
|
135 | + $class = empty($translations) |
|
136 | + ? 'glsr-hidden' |
|
137 | + : ''; |
|
138 | + return [ |
|
139 | + 'context' => [ |
|
140 | + 'class' => $class, |
|
141 | + 'database_key' => OptionManager::databaseKey(), |
|
142 | + 'translations' => $translations, |
|
143 | + ], |
|
144 | + ]; |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @param string $path |
|
149 | + * @param string|array $expectedValue |
|
150 | + * @return bool |
|
151 | + */ |
|
152 | + protected function isFieldHidden($path, $expectedValue) |
|
153 | + { |
|
154 | + $optionValue = glsr(OptionManager::class)->get( |
|
155 | + $path, |
|
156 | + Arr::get(glsr()->defaults, $path) |
|
157 | + ); |
|
158 | + if (is_array($expectedValue)) { |
|
159 | + return is_array($optionValue) |
|
160 | + ? 0 === count(array_intersect($optionValue, $expectedValue)) |
|
161 | + : !in_array($optionValue, $expectedValue); |
|
162 | + } |
|
163 | + return $optionValue != $expectedValue; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @return bool |
|
168 | + */ |
|
169 | + protected function isMultiDependency($path) |
|
170 | + { |
|
171 | + if (isset($this->settings[$path])) { |
|
172 | + $field = $this->settings[$path]; |
|
173 | + return ('checkbox' == $field['type'] && !empty($field['options'])) |
|
174 | + || !empty($field['multiple']); |
|
175 | + } |
|
176 | + return false; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * @return array |
|
181 | + */ |
|
182 | + protected function normalize(array $field) |
|
183 | + { |
|
184 | + $field = $this->normalizeDependsOn($field); |
|
185 | + $field = $this->normalizeLabelAndLegend($field); |
|
186 | + $field = $this->normalizeValue($field); |
|
187 | + return $field; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * @return array |
|
192 | + */ |
|
193 | + protected function normalizeDependsOn(array $field) |
|
194 | + { |
|
195 | + if (!empty($field['depends_on']) && is_array($field['depends_on'])) { |
|
196 | + $isFieldHidden = false; |
|
197 | + $conditions = []; |
|
198 | + foreach ($field['depends_on'] as $path => $value) { |
|
199 | + $conditions[] = [ |
|
200 | + 'name' => $this->getFieldNameForDependsOn($path), |
|
201 | + 'value' => $value, |
|
202 | + ]; |
|
203 | + if ($this->isFieldHidden($path, $value)) { |
|
204 | + $isFieldHidden = true; |
|
205 | + } |
|
206 | + } |
|
207 | + $field['data-depends'] = json_encode($conditions, JSON_HEX_APOS | JSON_HEX_QUOT); |
|
208 | + $field['is_hidden'] = $isFieldHidden; |
|
209 | + } |
|
210 | + return $field; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * @return array |
|
215 | + */ |
|
216 | + protected function normalizeLabelAndLegend(array $field) |
|
217 | + { |
|
218 | + if (!empty($field['label'])) { |
|
219 | + $field['legend'] = $field['label']; |
|
220 | + unset($field['label']); |
|
221 | + } else { |
|
222 | + $field['is_valid'] = false; |
|
223 | + glsr_log()->warning('Setting field is missing a label')->debug($field); |
|
224 | + } |
|
225 | + return $field; |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * @return array |
|
230 | + */ |
|
231 | + protected function normalizeValue(array $field) |
|
232 | + { |
|
233 | + if (!isset($field['value'])) { |
|
234 | + $field['value'] = glsr(OptionManager::class)->get( |
|
235 | + $field['name'], |
|
236 | + $this->getFieldDefault($field) |
|
237 | + ); |
|
238 | + } |
|
239 | + return $field; |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * @return string |
|
244 | + */ |
|
245 | + protected function normalizeSettingPath($path) |
|
246 | + { |
|
247 | + return Str::prefix('settings.', rtrim($path, '.')); |
|
248 | + } |
|
249 | 249 | } |
@@ -8,135 +8,135 @@ |
||
8 | 8 | |
9 | 9 | class ReviewsHtml extends ArrayObject |
10 | 10 | { |
11 | - /** |
|
12 | - * @var array |
|
13 | - */ |
|
14 | - public $args; |
|
11 | + /** |
|
12 | + * @var array |
|
13 | + */ |
|
14 | + public $args; |
|
15 | 15 | |
16 | - /** |
|
17 | - * @var int |
|
18 | - */ |
|
19 | - public $max_num_pages; |
|
16 | + /** |
|
17 | + * @var int |
|
18 | + */ |
|
19 | + public $max_num_pages; |
|
20 | 20 | |
21 | - /** |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - public $pagination; |
|
21 | + /** |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + public $pagination; |
|
25 | 25 | |
26 | - /** |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - public $reviews; |
|
26 | + /** |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + public $reviews; |
|
30 | 30 | |
31 | - public function __construct(array $reviews, $maxPageCount, array $args) |
|
32 | - { |
|
33 | - $this->args = $args; |
|
34 | - $this->max_num_pages = $maxPageCount; |
|
35 | - $this->reviews = $reviews; |
|
36 | - $this->pagination = $this->buildPagination(); |
|
37 | - parent::__construct($reviews, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); |
|
38 | - } |
|
31 | + public function __construct(array $reviews, $maxPageCount, array $args) |
|
32 | + { |
|
33 | + $this->args = $args; |
|
34 | + $this->max_num_pages = $maxPageCount; |
|
35 | + $this->reviews = $reviews; |
|
36 | + $this->pagination = $this->buildPagination(); |
|
37 | + parent::__construct($reviews, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @return string |
|
42 | - */ |
|
43 | - public function __toString() |
|
44 | - { |
|
45 | - return glsr(Template::class)->build('templates/reviews', [ |
|
46 | - 'args' => $this->args, |
|
47 | - 'context' => [ |
|
48 | - 'assigned_to' => $this->args['assigned_to'], |
|
49 | - 'category' => $this->args['category'], |
|
50 | - 'class' => $this->getClass(), |
|
51 | - 'id' => $this->args['id'], |
|
52 | - 'pagination' => $this->getPagination(), |
|
53 | - 'reviews' => $this->getReviews(), |
|
54 | - ], |
|
55 | - ]); |
|
56 | - } |
|
40 | + /** |
|
41 | + * @return string |
|
42 | + */ |
|
43 | + public function __toString() |
|
44 | + { |
|
45 | + return glsr(Template::class)->build('templates/reviews', [ |
|
46 | + 'args' => $this->args, |
|
47 | + 'context' => [ |
|
48 | + 'assigned_to' => $this->args['assigned_to'], |
|
49 | + 'category' => $this->args['category'], |
|
50 | + 'class' => $this->getClass(), |
|
51 | + 'id' => $this->args['id'], |
|
52 | + 'pagination' => $this->getPagination(), |
|
53 | + 'reviews' => $this->getReviews(), |
|
54 | + ], |
|
55 | + ]); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * @return string |
|
60 | - */ |
|
61 | - public function getPagination() |
|
62 | - { |
|
63 | - return wp_validate_boolean($this->args['pagination']) |
|
64 | - ? $this->pagination |
|
65 | - : ''; |
|
66 | - } |
|
58 | + /** |
|
59 | + * @return string |
|
60 | + */ |
|
61 | + public function getPagination() |
|
62 | + { |
|
63 | + return wp_validate_boolean($this->args['pagination']) |
|
64 | + ? $this->pagination |
|
65 | + : ''; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @return string |
|
70 | - */ |
|
71 | - public function getReviews() |
|
72 | - { |
|
73 | - $html = empty($this->reviews) |
|
74 | - ? $this->getReviewsFallback() |
|
75 | - : implode(PHP_EOL, $this->reviews); |
|
76 | - $wrapper = '<div class="glsr-reviews">%s</div>'; |
|
77 | - $wrapper = apply_filters('site-reviews/reviews/reviews-wrapper', $wrapper); |
|
78 | - return sprintf($wrapper, $html); |
|
79 | - } |
|
68 | + /** |
|
69 | + * @return string |
|
70 | + */ |
|
71 | + public function getReviews() |
|
72 | + { |
|
73 | + $html = empty($this->reviews) |
|
74 | + ? $this->getReviewsFallback() |
|
75 | + : implode(PHP_EOL, $this->reviews); |
|
76 | + $wrapper = '<div class="glsr-reviews">%s</div>'; |
|
77 | + $wrapper = apply_filters('site-reviews/reviews/reviews-wrapper', $wrapper); |
|
78 | + return sprintf($wrapper, $html); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * @param mixed $key |
|
83 | - * @return mixed |
|
84 | - */ |
|
85 | - public function offsetGet($key) |
|
86 | - { |
|
87 | - if ('navigation' == $key) { |
|
88 | - glsr()->deprecated[] = 'The $reviewsHtml->navigation property has been been deprecated. Please use the $reviewsHtml->pagination property instead.'; |
|
89 | - return $this->pagination; |
|
90 | - } |
|
91 | - if (property_exists($this, $key)) { |
|
92 | - return $this->$key; |
|
93 | - } |
|
94 | - return array_key_exists($key, $this->reviews) |
|
95 | - ? $this->reviews[$key] |
|
96 | - : null; |
|
97 | - } |
|
81 | + /** |
|
82 | + * @param mixed $key |
|
83 | + * @return mixed |
|
84 | + */ |
|
85 | + public function offsetGet($key) |
|
86 | + { |
|
87 | + if ('navigation' == $key) { |
|
88 | + glsr()->deprecated[] = 'The $reviewsHtml->navigation property has been been deprecated. Please use the $reviewsHtml->pagination property instead.'; |
|
89 | + return $this->pagination; |
|
90 | + } |
|
91 | + if (property_exists($this, $key)) { |
|
92 | + return $this->$key; |
|
93 | + } |
|
94 | + return array_key_exists($key, $this->reviews) |
|
95 | + ? $this->reviews[$key] |
|
96 | + : null; |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * @return string |
|
101 | - */ |
|
102 | - protected function buildPagination() |
|
103 | - { |
|
104 | - $html = glsr(Partial::class)->build('pagination', [ |
|
105 | - 'baseUrl' => Arr::get($this->args, 'pagedUrl'), |
|
106 | - 'current' => Arr::get($this->args, 'paged'), |
|
107 | - 'total' => $this->max_num_pages, |
|
108 | - ]); |
|
109 | - $html.= sprintf('<glsr-pagination hidden data-atts=\'%s\'></glsr-pagination>', $this->args['json']); |
|
110 | - $wrapper = '<div class="glsr-pagination">%s</div>'; |
|
111 | - $wrapper = apply_filters('site-reviews/reviews/pagination-wrapper', $wrapper); |
|
112 | - return sprintf($wrapper, $html); |
|
113 | - } |
|
99 | + /** |
|
100 | + * @return string |
|
101 | + */ |
|
102 | + protected function buildPagination() |
|
103 | + { |
|
104 | + $html = glsr(Partial::class)->build('pagination', [ |
|
105 | + 'baseUrl' => Arr::get($this->args, 'pagedUrl'), |
|
106 | + 'current' => Arr::get($this->args, 'paged'), |
|
107 | + 'total' => $this->max_num_pages, |
|
108 | + ]); |
|
109 | + $html.= sprintf('<glsr-pagination hidden data-atts=\'%s\'></glsr-pagination>', $this->args['json']); |
|
110 | + $wrapper = '<div class="glsr-pagination">%s</div>'; |
|
111 | + $wrapper = apply_filters('site-reviews/reviews/pagination-wrapper', $wrapper); |
|
112 | + return sprintf($wrapper, $html); |
|
113 | + } |
|
114 | 114 | |
115 | - /** |
|
116 | - * @return string |
|
117 | - */ |
|
118 | - protected function getClass() |
|
119 | - { |
|
120 | - $defaults = [ |
|
121 | - 'glsr-default', |
|
122 | - ]; |
|
123 | - if ('ajax' == $this->args['pagination']) { |
|
124 | - $defaults[] = 'glsr-ajax-pagination'; |
|
125 | - } |
|
126 | - $classes = explode(' ', $this->args['class']); |
|
127 | - $classes = array_unique(array_merge($defaults, array_filter($classes))); |
|
128 | - return implode(' ', $classes); |
|
129 | - } |
|
115 | + /** |
|
116 | + * @return string |
|
117 | + */ |
|
118 | + protected function getClass() |
|
119 | + { |
|
120 | + $defaults = [ |
|
121 | + 'glsr-default', |
|
122 | + ]; |
|
123 | + if ('ajax' == $this->args['pagination']) { |
|
124 | + $defaults[] = 'glsr-ajax-pagination'; |
|
125 | + } |
|
126 | + $classes = explode(' ', $this->args['class']); |
|
127 | + $classes = array_unique(array_merge($defaults, array_filter($classes))); |
|
128 | + return implode(' ', $classes); |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * @return string |
|
133 | - */ |
|
134 | - protected function getReviewsFallback() |
|
135 | - { |
|
136 | - if (empty($this->args['fallback']) && glsr(OptionManager::class)->getBool('settings.reviews.fallback')) { |
|
137 | - $this->args['fallback'] = __('There are no reviews yet. Be the first one to write one.', 'site-reviews'); |
|
138 | - } |
|
139 | - $fallback = '<p class="glsr-no-margins">'.$this->args['fallback'].'</p>'; |
|
140 | - return apply_filters('site-reviews/reviews/fallback', $fallback, $this->args); |
|
141 | - } |
|
131 | + /** |
|
132 | + * @return string |
|
133 | + */ |
|
134 | + protected function getReviewsFallback() |
|
135 | + { |
|
136 | + if (empty($this->args['fallback']) && glsr(OptionManager::class)->getBool('settings.reviews.fallback')) { |
|
137 | + $this->args['fallback'] = __('There are no reviews yet. Be the first one to write one.', 'site-reviews'); |
|
138 | + } |
|
139 | + $fallback = '<p class="glsr-no-margins">'.$this->args['fallback'].'</p>'; |
|
140 | + return apply_filters('site-reviews/reviews/fallback', $fallback, $this->args); |
|
141 | + } |
|
142 | 142 | } |
@@ -7,73 +7,73 @@ |
||
7 | 7 | |
8 | 8 | abstract class Field |
9 | 9 | { |
10 | - /** |
|
11 | - * @var Builder |
|
12 | - */ |
|
13 | - protected $builder; |
|
10 | + /** |
|
11 | + * @var Builder |
|
12 | + */ |
|
13 | + protected $builder; |
|
14 | 14 | |
15 | - public function __construct(Builder $builder) |
|
16 | - { |
|
17 | - $this->builder = $builder; |
|
18 | - } |
|
15 | + public function __construct(Builder $builder) |
|
16 | + { |
|
17 | + $this->builder = $builder; |
|
18 | + } |
|
19 | 19 | |
20 | - /** |
|
21 | - * @return string|void |
|
22 | - */ |
|
23 | - public function build() |
|
24 | - { |
|
25 | - glsr_log()->error('Build method is not implemented for '.get_class($this)); |
|
26 | - } |
|
20 | + /** |
|
21 | + * @return string|void |
|
22 | + */ |
|
23 | + public function build() |
|
24 | + { |
|
25 | + glsr_log()->error('Build method is not implemented for '.get_class($this)); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * @return array |
|
30 | - */ |
|
31 | - public static function defaults() |
|
32 | - { |
|
33 | - return []; |
|
34 | - } |
|
28 | + /** |
|
29 | + * @return array |
|
30 | + */ |
|
31 | + public static function defaults() |
|
32 | + { |
|
33 | + return []; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @return array |
|
38 | - */ |
|
39 | - public static function merge(array $args) |
|
40 | - { |
|
41 | - $merged = array_merge( |
|
42 | - wp_parse_args($args, static::defaults()), |
|
43 | - static::required() |
|
44 | - ); |
|
45 | - $merged['class'] = implode(' ', static::mergedAttribute('class', ' ', $args)); |
|
46 | - $merged['style'] = implode(';', static::mergedAttribute('style', ';', $args)); |
|
47 | - return $merged; |
|
48 | - } |
|
36 | + /** |
|
37 | + * @return array |
|
38 | + */ |
|
39 | + public static function merge(array $args) |
|
40 | + { |
|
41 | + $merged = array_merge( |
|
42 | + wp_parse_args($args, static::defaults()), |
|
43 | + static::required() |
|
44 | + ); |
|
45 | + $merged['class'] = implode(' ', static::mergedAttribute('class', ' ', $args)); |
|
46 | + $merged['style'] = implode(';', static::mergedAttribute('style', ';', $args)); |
|
47 | + return $merged; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @param string $delimiter |
|
52 | - * @param string $key |
|
53 | - * @return array |
|
54 | - */ |
|
55 | - public static function mergedAttribute($key, $delimiter, array $args) |
|
56 | - { |
|
57 | - return array_filter(array_merge( |
|
58 | - explode($delimiter, Arr::get($args, $key)), |
|
59 | - explode($delimiter, Arr::get(static::defaults(), $key)), |
|
60 | - explode($delimiter, Arr::get(static::required(), $key)) |
|
61 | - )); |
|
62 | - } |
|
50 | + /** |
|
51 | + * @param string $delimiter |
|
52 | + * @param string $key |
|
53 | + * @return array |
|
54 | + */ |
|
55 | + public static function mergedAttribute($key, $delimiter, array $args) |
|
56 | + { |
|
57 | + return array_filter(array_merge( |
|
58 | + explode($delimiter, Arr::get($args, $key)), |
|
59 | + explode($delimiter, Arr::get(static::defaults(), $key)), |
|
60 | + explode($delimiter, Arr::get(static::required(), $key)) |
|
61 | + )); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @return array |
|
66 | - */ |
|
67 | - public static function required() |
|
68 | - { |
|
69 | - return []; |
|
70 | - } |
|
64 | + /** |
|
65 | + * @return array |
|
66 | + */ |
|
67 | + public static function required() |
|
68 | + { |
|
69 | + return []; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * @return void |
|
74 | - */ |
|
75 | - protected function mergeFieldArgs() |
|
76 | - { |
|
77 | - $this->builder->args = static::merge($this->builder->args); |
|
78 | - } |
|
72 | + /** |
|
73 | + * @return void |
|
74 | + */ |
|
75 | + protected function mergeFieldArgs() |
|
76 | + { |
|
77 | + $this->builder->args = static::merge($this->builder->args); |
|
78 | + } |
|
79 | 79 | } |
@@ -7,78 +7,78 @@ |
||
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 | - ob_start(); |
|
18 | - glsr()->render($templatePath, $data); |
|
19 | - $template = ob_get_clean(); |
|
20 | - $path = Str::removePrefix('templates/', $templatePath); |
|
21 | - $template = apply_filters('site-reviews/build/template/'.$path, $template, $data); |
|
22 | - $template = $this->interpolate($template, $data, $path); |
|
23 | - $template = apply_filters('site-reviews/rendered/template', $template, $templatePath, $data); |
|
24 | - $template = apply_filters('site-reviews/rendered/template/'.$path, $template, $data); |
|
25 | - return $template; |
|
26 | - } |
|
10 | + /** |
|
11 | + * @param string $templatePath |
|
12 | + * @return void|string |
|
13 | + */ |
|
14 | + public function build($templatePath, array $data = []) |
|
15 | + { |
|
16 | + $data = $this->normalize($data); |
|
17 | + ob_start(); |
|
18 | + glsr()->render($templatePath, $data); |
|
19 | + $template = ob_get_clean(); |
|
20 | + $path = Str::removePrefix('templates/', $templatePath); |
|
21 | + $template = apply_filters('site-reviews/build/template/'.$path, $template, $data); |
|
22 | + $template = $this->interpolate($template, $data, $path); |
|
23 | + $template = apply_filters('site-reviews/rendered/template', $template, $templatePath, $data); |
|
24 | + $template = apply_filters('site-reviews/rendered/template/'.$path, $template, $data); |
|
25 | + return $template; |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * Interpolate context values into template placeholders. |
|
30 | - * @param string $template |
|
31 | - * @param string $templatePath |
|
32 | - * @return string |
|
33 | - */ |
|
34 | - public function interpolate($template, array $data = [], $templatePath) |
|
35 | - { |
|
36 | - $context = $this->normalizeContext(Arr::get($data, 'context', [])); |
|
37 | - $context = apply_filters('site-reviews/interpolate/'.$templatePath, $context, $template, $data); |
|
38 | - foreach ($context as $key => $value) { |
|
39 | - $template = strtr( |
|
40 | - $template, |
|
41 | - array_fill_keys(['{'.$key.'}', '{{ '.$key.' }}'], $value) |
|
42 | - ); |
|
43 | - } |
|
44 | - return trim($template); |
|
45 | - } |
|
28 | + /** |
|
29 | + * Interpolate context values into template placeholders. |
|
30 | + * @param string $template |
|
31 | + * @param string $templatePath |
|
32 | + * @return string |
|
33 | + */ |
|
34 | + public function interpolate($template, array $data = [], $templatePath) |
|
35 | + { |
|
36 | + $context = $this->normalizeContext(Arr::get($data, 'context', [])); |
|
37 | + $context = apply_filters('site-reviews/interpolate/'.$templatePath, $context, $template, $data); |
|
38 | + foreach ($context as $key => $value) { |
|
39 | + $template = strtr( |
|
40 | + $template, |
|
41 | + array_fill_keys(['{'.$key.'}', '{{ '.$key.' }}'], $value) |
|
42 | + ); |
|
43 | + } |
|
44 | + return trim($template); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * @param string $templatePath |
|
49 | - * @return void|string |
|
50 | - */ |
|
51 | - public function render($templatePath, array $data = []) |
|
52 | - { |
|
53 | - echo $this->build($templatePath, $data); |
|
54 | - } |
|
47 | + /** |
|
48 | + * @param string $templatePath |
|
49 | + * @return void|string |
|
50 | + */ |
|
51 | + public function render($templatePath, array $data = []) |
|
52 | + { |
|
53 | + echo $this->build($templatePath, $data); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @return array |
|
58 | - */ |
|
59 | - protected function normalize(array $data) |
|
60 | - { |
|
61 | - $arrayKeys = ['context', 'globals']; |
|
62 | - $data = wp_parse_args($data, array_fill_keys($arrayKeys, [])); |
|
63 | - foreach ($arrayKeys as $key) { |
|
64 | - if (is_array($data[$key])) { |
|
65 | - continue; |
|
66 | - } |
|
67 | - $data[$key] = []; |
|
68 | - } |
|
69 | - return $data; |
|
70 | - } |
|
56 | + /** |
|
57 | + * @return array |
|
58 | + */ |
|
59 | + protected function normalize(array $data) |
|
60 | + { |
|
61 | + $arrayKeys = ['context', 'globals']; |
|
62 | + $data = wp_parse_args($data, array_fill_keys($arrayKeys, [])); |
|
63 | + foreach ($arrayKeys as $key) { |
|
64 | + if (is_array($data[$key])) { |
|
65 | + continue; |
|
66 | + } |
|
67 | + $data[$key] = []; |
|
68 | + } |
|
69 | + return $data; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * @return array |
|
74 | - */ |
|
75 | - protected function normalizeContext(array $context) |
|
76 | - { |
|
77 | - $context = array_filter($context, function ($value) { |
|
78 | - return !is_array($value) && !is_object($value); |
|
79 | - }); |
|
80 | - return array_map(function ($value) { |
|
81 | - return (string) $value; |
|
82 | - }, $context); |
|
83 | - } |
|
72 | + /** |
|
73 | + * @return array |
|
74 | + */ |
|
75 | + protected function normalizeContext(array $context) |
|
76 | + { |
|
77 | + $context = array_filter($context, function ($value) { |
|
78 | + return !is_array($value) && !is_object($value); |
|
79 | + }); |
|
80 | + return array_map(function ($value) { |
|
81 | + return (string) $value; |
|
82 | + }, $context); |
|
83 | + } |
|
84 | 84 | } |