@@ -10,437 +10,437 @@ |
||
10 | 10 | |
11 | 11 | class Console |
12 | 12 | { |
13 | - const DEBUG = 0; // Detailed debug information |
|
14 | - const INFO = 1; // Interesting events |
|
15 | - const NOTICE = 2; // Normal but significant events |
|
16 | - const WARNING = 4; // Exceptional occurrences that are not errors |
|
17 | - const ERROR = 8; // Runtime errors that do not require immediate action |
|
18 | - const CRITICAL = 16; // Critical conditions |
|
19 | - const ALERT = 32; // Action must be taken immediately |
|
20 | - const EMERGENCY = 64; // System is unusable |
|
21 | - |
|
22 | - protected $file; |
|
23 | - protected $log; |
|
24 | - protected $logOnceKey = 'glsr_log_once'; |
|
25 | - |
|
26 | - public function __construct() |
|
27 | - { |
|
28 | - $this->file = glsr()->path('console.log'); |
|
29 | - $this->log = file_exists($this->file) |
|
30 | - ? file_get_contents($this->file) |
|
31 | - : ''; |
|
32 | - $this->reset(); |
|
33 | - } |
|
34 | - |
|
35 | - /** |
|
36 | - * @return string |
|
37 | - */ |
|
38 | - public function __toString() |
|
39 | - { |
|
40 | - return $this->get(); |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Action must be taken immediately |
|
45 | - * Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up. |
|
46 | - * @param mixed $message |
|
47 | - * @param array $context |
|
48 | - * @return static |
|
49 | - */ |
|
50 | - public function alert($message, array $context = []) |
|
51 | - { |
|
52 | - return $this->log(static::ALERT, $message, $context); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @return void |
|
57 | - */ |
|
58 | - public function clear() |
|
59 | - { |
|
60 | - $this->log = ''; |
|
61 | - file_put_contents($this->file, $this->log); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Critical conditions |
|
66 | - * Example: Application component unavailable, unexpected exception. |
|
67 | - * @param mixed $message |
|
68 | - * @param array $context |
|
69 | - * @return static |
|
70 | - */ |
|
71 | - public function critical($message, array $context = []) |
|
72 | - { |
|
73 | - return $this->log(static::CRITICAL, $message, $context); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Detailed debug information. |
|
78 | - * @param mixed $message |
|
79 | - * @param array $context |
|
80 | - * @return static |
|
81 | - */ |
|
82 | - public function debug($message, array $context = []) |
|
83 | - { |
|
84 | - return $this->log(static::DEBUG, $message, $context); |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * System is unusable. |
|
89 | - * @param mixed $message |
|
90 | - * @param array $context |
|
91 | - * @return static |
|
92 | - */ |
|
93 | - public function emergency($message, array $context = []) |
|
94 | - { |
|
95 | - return $this->log(static::EMERGENCY, $message, $context); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Runtime errors that do not require immediate action but should typically be logged and monitored. |
|
100 | - * @param mixed $message |
|
101 | - * @param array $context |
|
102 | - * @return static |
|
103 | - */ |
|
104 | - public function error($message, array $context = []) |
|
105 | - { |
|
106 | - return $this->log(static::ERROR, $message, $context); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @return string |
|
111 | - */ |
|
112 | - public function get() |
|
113 | - { |
|
114 | - return empty($this->log) |
|
115 | - ? __('Console is empty', 'site-reviews') |
|
116 | - : $this->log; |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * @return int |
|
121 | - */ |
|
122 | - public function getLevel() |
|
123 | - { |
|
124 | - return intval(apply_filters('site-reviews/console/level', static::INFO)); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * @return array |
|
129 | - */ |
|
130 | - public function getLevels() |
|
131 | - { |
|
132 | - $constants = (new ReflectionClass(__CLASS__))->getConstants(); |
|
133 | - return array_map('strtolower', array_flip($constants)); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * @return string |
|
138 | - */ |
|
139 | - public function humanLevel() |
|
140 | - { |
|
141 | - $level = $this->getLevel(); |
|
142 | - return sprintf('%s (%d)', strtoupper(Arr::get($this->getLevels(), $level, 'unknown')), $level); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @param string|null $valueIfEmpty |
|
147 | - * @return string |
|
148 | - */ |
|
149 | - public function humanSize($valueIfEmpty = null) |
|
150 | - { |
|
151 | - $bytes = $this->size(); |
|
152 | - if (empty($bytes) && is_string($valueIfEmpty)) { |
|
153 | - return $valueIfEmpty; |
|
154 | - } |
|
155 | - $exponent = floor(log(max($bytes, 1), 1024)); |
|
156 | - return round($bytes / pow(1024, $exponent), 2).' '.['bytes', 'KB', 'MB', 'GB'][$exponent]; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Interesting events |
|
161 | - * Example: User logs in, SQL logs. |
|
162 | - * @param mixed $message |
|
163 | - * @param array $context |
|
164 | - * @return static |
|
165 | - */ |
|
166 | - public function info($message, array $context = []) |
|
167 | - { |
|
168 | - return $this->log(static::INFO, $message, $context); |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * @param int $level |
|
173 | - * @param mixed $message |
|
174 | - * @param array $context |
|
175 | - * @param string $backtraceLine |
|
176 | - * @return static |
|
177 | - */ |
|
178 | - public function log($level, $message, $context = [], $backtraceLine = '') |
|
179 | - { |
|
180 | - if (empty($backtraceLine)) { |
|
181 | - $backtraceLine = $this->getBacktraceLine(); |
|
182 | - } |
|
183 | - if ($this->canLogEntry($level, $backtraceLine)) { |
|
184 | - $levelName = Arr::get($this->getLevels(), $level); |
|
185 | - $context = Arr::consolidate($context); |
|
186 | - $backtraceLine = $this->normalizeBacktraceLine($backtraceLine); |
|
187 | - $message = $this->interpolate($message, $context); |
|
188 | - $entry = $this->buildLogEntry($levelName, $message, $backtraceLine); |
|
189 | - file_put_contents($this->file, $entry.PHP_EOL, FILE_APPEND | LOCK_EX); |
|
190 | - apply_filters('console', $message, $levelName, $backtraceLine); // Show in Blackbar plugin if installed |
|
191 | - $this->reset(); |
|
192 | - } |
|
193 | - return $this; |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * @return void |
|
198 | - */ |
|
199 | - public function logOnce() |
|
200 | - { |
|
201 | - $once = Arr::consolidate(glsr()->{$this->logOnceKey}); |
|
202 | - $levels = $this->getLevels(); |
|
203 | - foreach ($once as $entry) { |
|
204 | - $levelName = Arr::get($entry, 'level'); |
|
205 | - if (!in_array($levelName, $levels)) { |
|
206 | - continue; |
|
207 | - } |
|
208 | - $level = Arr::get(array_flip($levels), $levelName); |
|
209 | - $message = Arr::get($entry, 'message'); |
|
210 | - $backtraceLine = Arr::get($entry, 'backtrace'); |
|
211 | - $this->log($level, $message, [], $backtraceLine); |
|
212 | - } |
|
213 | - glsr()->{$this->logOnceKey} = []; |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * Normal but significant events. |
|
218 | - * @param mixed $message |
|
219 | - * @param array $context |
|
220 | - * @return static |
|
221 | - */ |
|
222 | - public function notice($message, array $context = []) |
|
223 | - { |
|
224 | - return $this->log(static::NOTICE, $message, $context); |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * @param string $levelName |
|
229 | - * @param string $handle |
|
230 | - * @param mixed $data |
|
231 | - * @return void |
|
232 | - */ |
|
233 | - public function once($levelName, $handle, $data) |
|
234 | - { |
|
235 | - $once = Arr::consolidate(glsr()->{$this->logOnceKey}); |
|
236 | - $filtered = array_filter($once, function ($entry) use ($levelName, $handle) { |
|
237 | - return Arr::get($entry, 'level') == $levelName |
|
238 | - && Arr::get($entry, 'handle') == $handle; |
|
239 | - }); |
|
240 | - if (!empty($filtered)) { |
|
241 | - return; |
|
242 | - } |
|
243 | - $once[] = [ |
|
244 | - 'backtrace' => $this->getBacktraceLineFromData($data), |
|
245 | - 'handle' => $handle, |
|
246 | - 'level' => $levelName, |
|
247 | - 'message' => '[RECURRING] '.$this->getMessageFromData($data), |
|
248 | - ]; |
|
249 | - glsr()->{$this->logOnceKey} = $once; |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * @return int |
|
254 | - */ |
|
255 | - public function size() |
|
256 | - { |
|
257 | - return file_exists($this->file) |
|
258 | - ? filesize($this->file) |
|
259 | - : 0; |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * Exceptional occurrences that are not errors |
|
264 | - * Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong. |
|
265 | - * @param mixed $message |
|
266 | - * @param array $context |
|
267 | - * @return static |
|
268 | - */ |
|
269 | - public function warning($message, array $context = []) |
|
270 | - { |
|
271 | - return $this->log(static::WARNING, $message, $context); |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * @param array $backtrace |
|
276 | - * @param int $index |
|
277 | - * @return string |
|
278 | - */ |
|
279 | - protected function buildBacktraceLine($backtrace, $index) |
|
280 | - { |
|
281 | - return sprintf('%s:%s', |
|
282 | - Arr::get($backtrace, $index.'.file'), // realpath |
|
283 | - Arr::get($backtrace, $index.'.line') |
|
284 | - ); |
|
285 | - } |
|
286 | - |
|
287 | - /** |
|
288 | - * @param string $levelName |
|
289 | - * @param mixed $message |
|
290 | - * @param string $backtraceLine |
|
291 | - * @return string |
|
292 | - */ |
|
293 | - protected function buildLogEntry($levelName, $message, $backtraceLine = '') |
|
294 | - { |
|
295 | - return sprintf('[%s] %s [%s] %s', |
|
296 | - current_time('mysql'), |
|
297 | - strtoupper($levelName), |
|
298 | - $backtraceLine, |
|
299 | - $message |
|
300 | - ); |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * @param int $level |
|
305 | - * @return bool |
|
306 | - */ |
|
307 | - protected function canLogEntry($level, $backtraceLine) |
|
308 | - { |
|
309 | - $levelExists = array_key_exists($level, $this->getLevels()); |
|
310 | - if (!Str::contains($backtraceLine, glsr()->path())) { |
|
311 | - return $levelExists; // ignore level restriction if triggered outside of the plugin |
|
312 | - } |
|
313 | - return $levelExists && $level >= $this->getLevel(); |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * @return void|string |
|
318 | - */ |
|
319 | - protected function getBacktraceLine() |
|
320 | - { |
|
321 | - $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 6); |
|
322 | - $search = array_search('glsr_log', glsr_array_column($backtrace, 'function')); |
|
323 | - if (false !== $search) { |
|
324 | - return $this->buildBacktraceLine($backtrace, (int) $search); |
|
325 | - } |
|
326 | - $search = array_search('log', glsr_array_column($backtrace, 'function')); |
|
327 | - if (false !== $search) { |
|
328 | - $index = '{closure}' == Arr::get($backtrace, ($search + 2).'.function') |
|
329 | - ? $search + 4 |
|
330 | - : $search + 1; |
|
331 | - return $this->buildBacktraceLine($backtrace, $index); |
|
332 | - } |
|
333 | - return 'Unknown'; |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * @param mixed $data |
|
338 | - * @return string |
|
339 | - */ |
|
340 | - protected function getBacktraceLineFromData($data) |
|
341 | - { |
|
342 | - $backtrace = $data instanceof Throwable |
|
343 | - ? $data->getTrace() |
|
344 | - : debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
|
345 | - return $this->buildBacktraceLine($backtrace, 0); |
|
346 | - } |
|
347 | - |
|
348 | - /** |
|
349 | - * @param mixed $data |
|
350 | - * @return string |
|
351 | - */ |
|
352 | - protected function getMessageFromData($data) |
|
353 | - { |
|
354 | - return $data instanceof Throwable |
|
355 | - ? $this->normalizeThrowableMessage($data->getMessage()) |
|
356 | - : print_r($data, 1); |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * Interpolates context values into the message placeholders. |
|
361 | - * @param mixed $message |
|
362 | - * @param array $context |
|
363 | - * @return string |
|
364 | - */ |
|
365 | - protected function interpolate($message, $context = []) |
|
366 | - { |
|
367 | - if ($this->isObjectOrArray($message) || !is_array($context)) { |
|
368 | - return print_r($message, true); |
|
369 | - } |
|
370 | - $replace = []; |
|
371 | - foreach ($context as $key => $value) { |
|
372 | - $replace['{'.$key.'}'] = $this->normalizeValue($value); |
|
373 | - } |
|
374 | - return strtr($message, $replace); |
|
375 | - } |
|
376 | - |
|
377 | - /** |
|
378 | - * @param mixed $value |
|
379 | - * @return bool |
|
380 | - */ |
|
381 | - protected function isObjectOrArray($value) |
|
382 | - { |
|
383 | - return is_object($value) || is_array($value); |
|
384 | - } |
|
385 | - |
|
386 | - /** |
|
387 | - * @param string $backtraceLine |
|
388 | - * @return string |
|
389 | - */ |
|
390 | - protected function normalizeBacktraceLine($backtraceLine) |
|
391 | - { |
|
392 | - $search = [ |
|
393 | - glsr()->path('plugin/'), |
|
394 | - glsr()->path('plugin/', false), |
|
395 | - trailingslashit(glsr()->path()), |
|
396 | - trailingslashit(glsr()->path('', false)), |
|
397 | - WP_CONTENT_DIR, |
|
398 | - ABSPATH, |
|
399 | - ]; |
|
400 | - return str_replace(array_unique($search), '', $backtraceLine); |
|
401 | - } |
|
402 | - |
|
403 | - /** |
|
404 | - * @param string $message |
|
405 | - * @return string |
|
406 | - */ |
|
407 | - protected function normalizeThrowableMessage($message) |
|
408 | - { |
|
409 | - $calledIn = strpos($message, ', called in'); |
|
410 | - return false !== $calledIn |
|
411 | - ? substr($message, 0, $calledIn) |
|
412 | - : $message; |
|
413 | - } |
|
414 | - |
|
415 | - /** |
|
416 | - * @param mixed $value |
|
417 | - * @return string |
|
418 | - */ |
|
419 | - protected function normalizeValue($value) |
|
420 | - { |
|
421 | - if ($value instanceof DateTime) { |
|
422 | - $value = $value->format('Y-m-d H:i:s'); |
|
423 | - } elseif ($this->isObjectOrArray($value)) { |
|
424 | - $value = json_encode($value); |
|
425 | - } |
|
426 | - return (string) $value; |
|
427 | - } |
|
428 | - |
|
429 | - /** |
|
430 | - * @return void |
|
431 | - */ |
|
432 | - protected function reset() |
|
433 | - { |
|
434 | - if ($this->size() <= pow(1024, 2) / 8) { |
|
435 | - return; |
|
436 | - } |
|
437 | - $this->clear(); |
|
438 | - file_put_contents( |
|
439 | - $this->file, |
|
440 | - $this->buildLogEntry( |
|
441 | - static::NOTICE, |
|
442 | - __('Console was automatically cleared (128 KB maximum size)', 'site-reviews') |
|
443 | - ) |
|
444 | - ); |
|
445 | - } |
|
13 | + const DEBUG = 0; // Detailed debug information |
|
14 | + const INFO = 1; // Interesting events |
|
15 | + const NOTICE = 2; // Normal but significant events |
|
16 | + const WARNING = 4; // Exceptional occurrences that are not errors |
|
17 | + const ERROR = 8; // Runtime errors that do not require immediate action |
|
18 | + const CRITICAL = 16; // Critical conditions |
|
19 | + const ALERT = 32; // Action must be taken immediately |
|
20 | + const EMERGENCY = 64; // System is unusable |
|
21 | + |
|
22 | + protected $file; |
|
23 | + protected $log; |
|
24 | + protected $logOnceKey = 'glsr_log_once'; |
|
25 | + |
|
26 | + public function __construct() |
|
27 | + { |
|
28 | + $this->file = glsr()->path('console.log'); |
|
29 | + $this->log = file_exists($this->file) |
|
30 | + ? file_get_contents($this->file) |
|
31 | + : ''; |
|
32 | + $this->reset(); |
|
33 | + } |
|
34 | + |
|
35 | + /** |
|
36 | + * @return string |
|
37 | + */ |
|
38 | + public function __toString() |
|
39 | + { |
|
40 | + return $this->get(); |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Action must be taken immediately |
|
45 | + * Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up. |
|
46 | + * @param mixed $message |
|
47 | + * @param array $context |
|
48 | + * @return static |
|
49 | + */ |
|
50 | + public function alert($message, array $context = []) |
|
51 | + { |
|
52 | + return $this->log(static::ALERT, $message, $context); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @return void |
|
57 | + */ |
|
58 | + public function clear() |
|
59 | + { |
|
60 | + $this->log = ''; |
|
61 | + file_put_contents($this->file, $this->log); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Critical conditions |
|
66 | + * Example: Application component unavailable, unexpected exception. |
|
67 | + * @param mixed $message |
|
68 | + * @param array $context |
|
69 | + * @return static |
|
70 | + */ |
|
71 | + public function critical($message, array $context = []) |
|
72 | + { |
|
73 | + return $this->log(static::CRITICAL, $message, $context); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Detailed debug information. |
|
78 | + * @param mixed $message |
|
79 | + * @param array $context |
|
80 | + * @return static |
|
81 | + */ |
|
82 | + public function debug($message, array $context = []) |
|
83 | + { |
|
84 | + return $this->log(static::DEBUG, $message, $context); |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * System is unusable. |
|
89 | + * @param mixed $message |
|
90 | + * @param array $context |
|
91 | + * @return static |
|
92 | + */ |
|
93 | + public function emergency($message, array $context = []) |
|
94 | + { |
|
95 | + return $this->log(static::EMERGENCY, $message, $context); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Runtime errors that do not require immediate action but should typically be logged and monitored. |
|
100 | + * @param mixed $message |
|
101 | + * @param array $context |
|
102 | + * @return static |
|
103 | + */ |
|
104 | + public function error($message, array $context = []) |
|
105 | + { |
|
106 | + return $this->log(static::ERROR, $message, $context); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @return string |
|
111 | + */ |
|
112 | + public function get() |
|
113 | + { |
|
114 | + return empty($this->log) |
|
115 | + ? __('Console is empty', 'site-reviews') |
|
116 | + : $this->log; |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * @return int |
|
121 | + */ |
|
122 | + public function getLevel() |
|
123 | + { |
|
124 | + return intval(apply_filters('site-reviews/console/level', static::INFO)); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * @return array |
|
129 | + */ |
|
130 | + public function getLevels() |
|
131 | + { |
|
132 | + $constants = (new ReflectionClass(__CLASS__))->getConstants(); |
|
133 | + return array_map('strtolower', array_flip($constants)); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * @return string |
|
138 | + */ |
|
139 | + public function humanLevel() |
|
140 | + { |
|
141 | + $level = $this->getLevel(); |
|
142 | + return sprintf('%s (%d)', strtoupper(Arr::get($this->getLevels(), $level, 'unknown')), $level); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @param string|null $valueIfEmpty |
|
147 | + * @return string |
|
148 | + */ |
|
149 | + public function humanSize($valueIfEmpty = null) |
|
150 | + { |
|
151 | + $bytes = $this->size(); |
|
152 | + if (empty($bytes) && is_string($valueIfEmpty)) { |
|
153 | + return $valueIfEmpty; |
|
154 | + } |
|
155 | + $exponent = floor(log(max($bytes, 1), 1024)); |
|
156 | + return round($bytes / pow(1024, $exponent), 2).' '.['bytes', 'KB', 'MB', 'GB'][$exponent]; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Interesting events |
|
161 | + * Example: User logs in, SQL logs. |
|
162 | + * @param mixed $message |
|
163 | + * @param array $context |
|
164 | + * @return static |
|
165 | + */ |
|
166 | + public function info($message, array $context = []) |
|
167 | + { |
|
168 | + return $this->log(static::INFO, $message, $context); |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * @param int $level |
|
173 | + * @param mixed $message |
|
174 | + * @param array $context |
|
175 | + * @param string $backtraceLine |
|
176 | + * @return static |
|
177 | + */ |
|
178 | + public function log($level, $message, $context = [], $backtraceLine = '') |
|
179 | + { |
|
180 | + if (empty($backtraceLine)) { |
|
181 | + $backtraceLine = $this->getBacktraceLine(); |
|
182 | + } |
|
183 | + if ($this->canLogEntry($level, $backtraceLine)) { |
|
184 | + $levelName = Arr::get($this->getLevels(), $level); |
|
185 | + $context = Arr::consolidate($context); |
|
186 | + $backtraceLine = $this->normalizeBacktraceLine($backtraceLine); |
|
187 | + $message = $this->interpolate($message, $context); |
|
188 | + $entry = $this->buildLogEntry($levelName, $message, $backtraceLine); |
|
189 | + file_put_contents($this->file, $entry.PHP_EOL, FILE_APPEND | LOCK_EX); |
|
190 | + apply_filters('console', $message, $levelName, $backtraceLine); // Show in Blackbar plugin if installed |
|
191 | + $this->reset(); |
|
192 | + } |
|
193 | + return $this; |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * @return void |
|
198 | + */ |
|
199 | + public function logOnce() |
|
200 | + { |
|
201 | + $once = Arr::consolidate(glsr()->{$this->logOnceKey}); |
|
202 | + $levels = $this->getLevels(); |
|
203 | + foreach ($once as $entry) { |
|
204 | + $levelName = Arr::get($entry, 'level'); |
|
205 | + if (!in_array($levelName, $levels)) { |
|
206 | + continue; |
|
207 | + } |
|
208 | + $level = Arr::get(array_flip($levels), $levelName); |
|
209 | + $message = Arr::get($entry, 'message'); |
|
210 | + $backtraceLine = Arr::get($entry, 'backtrace'); |
|
211 | + $this->log($level, $message, [], $backtraceLine); |
|
212 | + } |
|
213 | + glsr()->{$this->logOnceKey} = []; |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * Normal but significant events. |
|
218 | + * @param mixed $message |
|
219 | + * @param array $context |
|
220 | + * @return static |
|
221 | + */ |
|
222 | + public function notice($message, array $context = []) |
|
223 | + { |
|
224 | + return $this->log(static::NOTICE, $message, $context); |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * @param string $levelName |
|
229 | + * @param string $handle |
|
230 | + * @param mixed $data |
|
231 | + * @return void |
|
232 | + */ |
|
233 | + public function once($levelName, $handle, $data) |
|
234 | + { |
|
235 | + $once = Arr::consolidate(glsr()->{$this->logOnceKey}); |
|
236 | + $filtered = array_filter($once, function ($entry) use ($levelName, $handle) { |
|
237 | + return Arr::get($entry, 'level') == $levelName |
|
238 | + && Arr::get($entry, 'handle') == $handle; |
|
239 | + }); |
|
240 | + if (!empty($filtered)) { |
|
241 | + return; |
|
242 | + } |
|
243 | + $once[] = [ |
|
244 | + 'backtrace' => $this->getBacktraceLineFromData($data), |
|
245 | + 'handle' => $handle, |
|
246 | + 'level' => $levelName, |
|
247 | + 'message' => '[RECURRING] '.$this->getMessageFromData($data), |
|
248 | + ]; |
|
249 | + glsr()->{$this->logOnceKey} = $once; |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * @return int |
|
254 | + */ |
|
255 | + public function size() |
|
256 | + { |
|
257 | + return file_exists($this->file) |
|
258 | + ? filesize($this->file) |
|
259 | + : 0; |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * Exceptional occurrences that are not errors |
|
264 | + * Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong. |
|
265 | + * @param mixed $message |
|
266 | + * @param array $context |
|
267 | + * @return static |
|
268 | + */ |
|
269 | + public function warning($message, array $context = []) |
|
270 | + { |
|
271 | + return $this->log(static::WARNING, $message, $context); |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * @param array $backtrace |
|
276 | + * @param int $index |
|
277 | + * @return string |
|
278 | + */ |
|
279 | + protected function buildBacktraceLine($backtrace, $index) |
|
280 | + { |
|
281 | + return sprintf('%s:%s', |
|
282 | + Arr::get($backtrace, $index.'.file'), // realpath |
|
283 | + Arr::get($backtrace, $index.'.line') |
|
284 | + ); |
|
285 | + } |
|
286 | + |
|
287 | + /** |
|
288 | + * @param string $levelName |
|
289 | + * @param mixed $message |
|
290 | + * @param string $backtraceLine |
|
291 | + * @return string |
|
292 | + */ |
|
293 | + protected function buildLogEntry($levelName, $message, $backtraceLine = '') |
|
294 | + { |
|
295 | + return sprintf('[%s] %s [%s] %s', |
|
296 | + current_time('mysql'), |
|
297 | + strtoupper($levelName), |
|
298 | + $backtraceLine, |
|
299 | + $message |
|
300 | + ); |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * @param int $level |
|
305 | + * @return bool |
|
306 | + */ |
|
307 | + protected function canLogEntry($level, $backtraceLine) |
|
308 | + { |
|
309 | + $levelExists = array_key_exists($level, $this->getLevels()); |
|
310 | + if (!Str::contains($backtraceLine, glsr()->path())) { |
|
311 | + return $levelExists; // ignore level restriction if triggered outside of the plugin |
|
312 | + } |
|
313 | + return $levelExists && $level >= $this->getLevel(); |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * @return void|string |
|
318 | + */ |
|
319 | + protected function getBacktraceLine() |
|
320 | + { |
|
321 | + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 6); |
|
322 | + $search = array_search('glsr_log', glsr_array_column($backtrace, 'function')); |
|
323 | + if (false !== $search) { |
|
324 | + return $this->buildBacktraceLine($backtrace, (int) $search); |
|
325 | + } |
|
326 | + $search = array_search('log', glsr_array_column($backtrace, 'function')); |
|
327 | + if (false !== $search) { |
|
328 | + $index = '{closure}' == Arr::get($backtrace, ($search + 2).'.function') |
|
329 | + ? $search + 4 |
|
330 | + : $search + 1; |
|
331 | + return $this->buildBacktraceLine($backtrace, $index); |
|
332 | + } |
|
333 | + return 'Unknown'; |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * @param mixed $data |
|
338 | + * @return string |
|
339 | + */ |
|
340 | + protected function getBacktraceLineFromData($data) |
|
341 | + { |
|
342 | + $backtrace = $data instanceof Throwable |
|
343 | + ? $data->getTrace() |
|
344 | + : debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
|
345 | + return $this->buildBacktraceLine($backtrace, 0); |
|
346 | + } |
|
347 | + |
|
348 | + /** |
|
349 | + * @param mixed $data |
|
350 | + * @return string |
|
351 | + */ |
|
352 | + protected function getMessageFromData($data) |
|
353 | + { |
|
354 | + return $data instanceof Throwable |
|
355 | + ? $this->normalizeThrowableMessage($data->getMessage()) |
|
356 | + : print_r($data, 1); |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * Interpolates context values into the message placeholders. |
|
361 | + * @param mixed $message |
|
362 | + * @param array $context |
|
363 | + * @return string |
|
364 | + */ |
|
365 | + protected function interpolate($message, $context = []) |
|
366 | + { |
|
367 | + if ($this->isObjectOrArray($message) || !is_array($context)) { |
|
368 | + return print_r($message, true); |
|
369 | + } |
|
370 | + $replace = []; |
|
371 | + foreach ($context as $key => $value) { |
|
372 | + $replace['{'.$key.'}'] = $this->normalizeValue($value); |
|
373 | + } |
|
374 | + return strtr($message, $replace); |
|
375 | + } |
|
376 | + |
|
377 | + /** |
|
378 | + * @param mixed $value |
|
379 | + * @return bool |
|
380 | + */ |
|
381 | + protected function isObjectOrArray($value) |
|
382 | + { |
|
383 | + return is_object($value) || is_array($value); |
|
384 | + } |
|
385 | + |
|
386 | + /** |
|
387 | + * @param string $backtraceLine |
|
388 | + * @return string |
|
389 | + */ |
|
390 | + protected function normalizeBacktraceLine($backtraceLine) |
|
391 | + { |
|
392 | + $search = [ |
|
393 | + glsr()->path('plugin/'), |
|
394 | + glsr()->path('plugin/', false), |
|
395 | + trailingslashit(glsr()->path()), |
|
396 | + trailingslashit(glsr()->path('', false)), |
|
397 | + WP_CONTENT_DIR, |
|
398 | + ABSPATH, |
|
399 | + ]; |
|
400 | + return str_replace(array_unique($search), '', $backtraceLine); |
|
401 | + } |
|
402 | + |
|
403 | + /** |
|
404 | + * @param string $message |
|
405 | + * @return string |
|
406 | + */ |
|
407 | + protected function normalizeThrowableMessage($message) |
|
408 | + { |
|
409 | + $calledIn = strpos($message, ', called in'); |
|
410 | + return false !== $calledIn |
|
411 | + ? substr($message, 0, $calledIn) |
|
412 | + : $message; |
|
413 | + } |
|
414 | + |
|
415 | + /** |
|
416 | + * @param mixed $value |
|
417 | + * @return string |
|
418 | + */ |
|
419 | + protected function normalizeValue($value) |
|
420 | + { |
|
421 | + if ($value instanceof DateTime) { |
|
422 | + $value = $value->format('Y-m-d H:i:s'); |
|
423 | + } elseif ($this->isObjectOrArray($value)) { |
|
424 | + $value = json_encode($value); |
|
425 | + } |
|
426 | + return (string) $value; |
|
427 | + } |
|
428 | + |
|
429 | + /** |
|
430 | + * @return void |
|
431 | + */ |
|
432 | + protected function reset() |
|
433 | + { |
|
434 | + if ($this->size() <= pow(1024, 2) / 8) { |
|
435 | + return; |
|
436 | + } |
|
437 | + $this->clear(); |
|
438 | + file_put_contents( |
|
439 | + $this->file, |
|
440 | + $this->buildLogEntry( |
|
441 | + static::NOTICE, |
|
442 | + __('Console was automatically cleared (128 KB maximum size)', 'site-reviews') |
|
443 | + ) |
|
444 | + ); |
|
445 | + } |
|
446 | 446 | } |
@@ -18,357 +18,357 @@ |
||
18 | 18 | */ |
19 | 19 | final class Application extends Container |
20 | 20 | { |
21 | - const CAPABILITY = 'edit_others_posts'; |
|
22 | - const CRON_EVENT = 'site-reviews/schedule/session/purge'; |
|
23 | - const ID = 'site-reviews'; |
|
24 | - const PAGED_QUERY_VAR = 'reviews-page'; |
|
25 | - const POST_TYPE = 'site-review'; |
|
26 | - const PREFIX = 'glsr_'; |
|
27 | - const TAXONOMY = 'site-review-category'; |
|
21 | + const CAPABILITY = 'edit_others_posts'; |
|
22 | + const CRON_EVENT = 'site-reviews/schedule/session/purge'; |
|
23 | + const ID = 'site-reviews'; |
|
24 | + const PAGED_QUERY_VAR = 'reviews-page'; |
|
25 | + const POST_TYPE = 'site-review'; |
|
26 | + const PREFIX = 'glsr_'; |
|
27 | + const TAXONOMY = 'site-review-category'; |
|
28 | 28 | |
29 | - public $addons = []; |
|
30 | - public $defaults; |
|
31 | - public $deprecated = []; |
|
32 | - public $file; |
|
33 | - public $languages; |
|
34 | - public $mceShortcodes = []; //defined elsewhere |
|
35 | - public $name; |
|
36 | - public $postTypeColumns = []; // defined elsewhere |
|
37 | - public $reviewTypes; |
|
38 | - public $schemas = []; //defined elsewhere |
|
39 | - public $version; |
|
29 | + public $addons = []; |
|
30 | + public $defaults; |
|
31 | + public $deprecated = []; |
|
32 | + public $file; |
|
33 | + public $languages; |
|
34 | + public $mceShortcodes = []; //defined elsewhere |
|
35 | + public $name; |
|
36 | + public $postTypeColumns = []; // defined elsewhere |
|
37 | + public $reviewTypes; |
|
38 | + public $schemas = []; //defined elsewhere |
|
39 | + public $version; |
|
40 | 40 | |
41 | - public function __construct() |
|
42 | - { |
|
43 | - static::$instance = $this; |
|
44 | - $this->file = str_replace('plugin/Application', static::ID, (new ReflectionClass($this))->getFileName()); |
|
45 | - $plugin = get_file_data($this->file, [ |
|
46 | - 'languages' => 'Domain Path', |
|
47 | - 'name' => 'Plugin Name', |
|
48 | - 'version' => 'Version', |
|
49 | - ], 'plugin'); |
|
50 | - array_walk($plugin, function ($value, $key) { |
|
51 | - $this->$key = $value; |
|
52 | - }); |
|
53 | - } |
|
41 | + public function __construct() |
|
42 | + { |
|
43 | + static::$instance = $this; |
|
44 | + $this->file = str_replace('plugin/Application', static::ID, (new ReflectionClass($this))->getFileName()); |
|
45 | + $plugin = get_file_data($this->file, [ |
|
46 | + 'languages' => 'Domain Path', |
|
47 | + 'name' => 'Plugin Name', |
|
48 | + 'version' => 'Version', |
|
49 | + ], 'plugin'); |
|
50 | + array_walk($plugin, function ($value, $key) { |
|
51 | + $this->$key = $value; |
|
52 | + }); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @return void |
|
57 | - */ |
|
58 | - public function activate() |
|
59 | - { |
|
60 | - $this->scheduleCronJob(); |
|
61 | - add_option(static::ID.'activated', true); |
|
62 | - } |
|
55 | + /** |
|
56 | + * @return void |
|
57 | + */ |
|
58 | + public function activate() |
|
59 | + { |
|
60 | + $this->scheduleCronJob(); |
|
61 | + add_option(static::ID.'activated', true); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @param string $view |
|
66 | - * @return string |
|
67 | - */ |
|
68 | - public function build($view, array $data = []) |
|
69 | - { |
|
70 | - ob_start(); |
|
71 | - $this->render($view, $data); |
|
72 | - return ob_get_clean(); |
|
73 | - } |
|
64 | + /** |
|
65 | + * @param string $view |
|
66 | + * @return string |
|
67 | + */ |
|
68 | + public function build($view, array $data = []) |
|
69 | + { |
|
70 | + ob_start(); |
|
71 | + $this->render($view, $data); |
|
72 | + return ob_get_clean(); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * @param string $capability |
|
77 | - * @return bool |
|
78 | - */ |
|
79 | - public function can($capability) |
|
80 | - { |
|
81 | - return $this->make(Role::class)->can($capability); |
|
82 | - } |
|
75 | + /** |
|
76 | + * @param string $capability |
|
77 | + * @return bool |
|
78 | + */ |
|
79 | + public function can($capability) |
|
80 | + { |
|
81 | + return $this->make(Role::class)->can($capability); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * @return void |
|
86 | - */ |
|
87 | - public function catchFatalError() |
|
88 | - { |
|
89 | - $error = error_get_last(); |
|
90 | - if (E_ERROR !== $error['type'] || !Str::contains($error['message'], $this->path())) { |
|
91 | - return; |
|
92 | - } |
|
93 | - glsr_log()->error($error['message']); |
|
94 | - } |
|
84 | + /** |
|
85 | + * @return void |
|
86 | + */ |
|
87 | + public function catchFatalError() |
|
88 | + { |
|
89 | + $error = error_get_last(); |
|
90 | + if (E_ERROR !== $error['type'] || !Str::contains($error['message'], $this->path())) { |
|
91 | + return; |
|
92 | + } |
|
93 | + glsr_log()->error($error['message']); |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * @param string $name |
|
98 | - * @return array |
|
99 | - */ |
|
100 | - public function config($name) |
|
101 | - { |
|
102 | - $path = apply_filters('site-reviews/config', 'config/'.$name.'.php'); |
|
103 | - $configFile = $this->path($path); |
|
104 | - $config = file_exists($configFile) |
|
105 | - ? include $configFile |
|
106 | - : []; |
|
107 | - return apply_filters('site-reviews/config/'.$name, $config); |
|
108 | - } |
|
96 | + /** |
|
97 | + * @param string $name |
|
98 | + * @return array |
|
99 | + */ |
|
100 | + public function config($name) |
|
101 | + { |
|
102 | + $path = apply_filters('site-reviews/config', 'config/'.$name.'.php'); |
|
103 | + $configFile = $this->path($path); |
|
104 | + $config = file_exists($configFile) |
|
105 | + ? include $configFile |
|
106 | + : []; |
|
107 | + return apply_filters('site-reviews/config/'.$name, $config); |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * @param string $property |
|
112 | - * @return string |
|
113 | - */ |
|
114 | - public function constant($property, $className = 'static') |
|
115 | - { |
|
116 | - $constant = $className.'::'.$property; |
|
117 | - return defined($constant) |
|
118 | - ? apply_filters('site-reviews/const/'.$property, constant($constant)) |
|
119 | - : ''; |
|
120 | - } |
|
110 | + /** |
|
111 | + * @param string $property |
|
112 | + * @return string |
|
113 | + */ |
|
114 | + public function constant($property, $className = 'static') |
|
115 | + { |
|
116 | + $constant = $className.'::'.$property; |
|
117 | + return defined($constant) |
|
118 | + ? apply_filters('site-reviews/const/'.$property, constant($constant)) |
|
119 | + : ''; |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * @return void |
|
124 | - */ |
|
125 | - public function deactivate() |
|
126 | - { |
|
127 | - $this->unscheduleCronJob(); |
|
128 | - } |
|
122 | + /** |
|
123 | + * @return void |
|
124 | + */ |
|
125 | + public function deactivate() |
|
126 | + { |
|
127 | + $this->unscheduleCronJob(); |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * @param string $view |
|
132 | - * @return void|string |
|
133 | - */ |
|
134 | - public function file($view) |
|
135 | - { |
|
136 | - $view.= '.php'; |
|
137 | - $filePaths = []; |
|
138 | - if (Str::startsWith('templates/', $view)) { |
|
139 | - $filePaths[] = $this->themePath(Str::removePrefix('templates/', $view)); |
|
140 | - } |
|
141 | - $filePaths[] = $this->path($view); |
|
142 | - $filePaths[] = $this->path('views/'.$view); |
|
143 | - foreach ($filePaths as $file) { |
|
144 | - if (!file_exists($file)) { |
|
145 | - continue; |
|
146 | - } |
|
147 | - return $file; |
|
148 | - } |
|
149 | - } |
|
130 | + /** |
|
131 | + * @param string $view |
|
132 | + * @return void|string |
|
133 | + */ |
|
134 | + public function file($view) |
|
135 | + { |
|
136 | + $view.= '.php'; |
|
137 | + $filePaths = []; |
|
138 | + if (Str::startsWith('templates/', $view)) { |
|
139 | + $filePaths[] = $this->themePath(Str::removePrefix('templates/', $view)); |
|
140 | + } |
|
141 | + $filePaths[] = $this->path($view); |
|
142 | + $filePaths[] = $this->path('views/'.$view); |
|
143 | + foreach ($filePaths as $file) { |
|
144 | + if (!file_exists($file)) { |
|
145 | + continue; |
|
146 | + } |
|
147 | + return $file; |
|
148 | + } |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * @return array |
|
153 | - */ |
|
154 | - public function getDefaults() |
|
155 | - { |
|
156 | - if (empty($this->defaults)) { |
|
157 | - $this->defaults = $this->make(DefaultsManager::class)->get(); |
|
158 | - } |
|
159 | - return apply_filters('site-reviews/get/defaults', $this->defaults); |
|
160 | - } |
|
151 | + /** |
|
152 | + * @return array |
|
153 | + */ |
|
154 | + public function getDefaults() |
|
155 | + { |
|
156 | + if (empty($this->defaults)) { |
|
157 | + $this->defaults = $this->make(DefaultsManager::class)->get(); |
|
158 | + } |
|
159 | + return apply_filters('site-reviews/get/defaults', $this->defaults); |
|
160 | + } |
|
161 | 161 | |
162 | - /** |
|
163 | - * @param string $page |
|
164 | - * @param string $tab |
|
165 | - * @return string |
|
166 | - */ |
|
167 | - public function getPermission($page = '', $tab = 'index') |
|
168 | - { |
|
169 | - $fallback = 'edit_posts'; |
|
170 | - $permissions = [ |
|
171 | - 'addons' => 'install_plugins', |
|
172 | - 'documentation' => [ |
|
173 | - 'faq' => 'edit_others_posts', |
|
174 | - 'functions' => 'manage_options', |
|
175 | - 'hooks' => 'edit_others_posts', |
|
176 | - 'index' => 'edit_posts', |
|
177 | - 'support' => 'edit_others_posts', |
|
178 | - ], |
|
179 | - 'settings' => 'manage_options', |
|
180 | - 'tools' => [ |
|
181 | - 'console' => 'edit_others_posts', |
|
182 | - 'general' => 'edit_others_posts', |
|
183 | - 'index' => 'edit_others_posts', |
|
184 | - 'sync' => 'manage_options', |
|
185 | - 'system-info' => 'edit_others_posts', |
|
186 | - ] |
|
187 | - ]; |
|
188 | - $permission = Arr::get($permissions, $page, $fallback); |
|
189 | - if (is_array($permission)) { |
|
190 | - $permission = Arr::get($permission, $tab, $fallback); |
|
191 | - } |
|
192 | - return empty($permission) || !is_string($permission) |
|
193 | - ? $fallback |
|
194 | - : $permission; |
|
195 | - } |
|
162 | + /** |
|
163 | + * @param string $page |
|
164 | + * @param string $tab |
|
165 | + * @return string |
|
166 | + */ |
|
167 | + public function getPermission($page = '', $tab = 'index') |
|
168 | + { |
|
169 | + $fallback = 'edit_posts'; |
|
170 | + $permissions = [ |
|
171 | + 'addons' => 'install_plugins', |
|
172 | + 'documentation' => [ |
|
173 | + 'faq' => 'edit_others_posts', |
|
174 | + 'functions' => 'manage_options', |
|
175 | + 'hooks' => 'edit_others_posts', |
|
176 | + 'index' => 'edit_posts', |
|
177 | + 'support' => 'edit_others_posts', |
|
178 | + ], |
|
179 | + 'settings' => 'manage_options', |
|
180 | + 'tools' => [ |
|
181 | + 'console' => 'edit_others_posts', |
|
182 | + 'general' => 'edit_others_posts', |
|
183 | + 'index' => 'edit_others_posts', |
|
184 | + 'sync' => 'manage_options', |
|
185 | + 'system-info' => 'edit_others_posts', |
|
186 | + ] |
|
187 | + ]; |
|
188 | + $permission = Arr::get($permissions, $page, $fallback); |
|
189 | + if (is_array($permission)) { |
|
190 | + $permission = Arr::get($permission, $tab, $fallback); |
|
191 | + } |
|
192 | + return empty($permission) || !is_string($permission) |
|
193 | + ? $fallback |
|
194 | + : $permission; |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * @param string $page |
|
199 | - * @param string $tab |
|
200 | - * @return bool |
|
201 | - */ |
|
202 | - public function hasPermission($page = '', $tab = 'index') |
|
203 | - { |
|
204 | - $isAdmin = $this->isAdmin(); |
|
205 | - return !$isAdmin || ($isAdmin && $this->can($this->getPermission($page, $tab))); |
|
206 | - } |
|
197 | + /** |
|
198 | + * @param string $page |
|
199 | + * @param string $tab |
|
200 | + * @return bool |
|
201 | + */ |
|
202 | + public function hasPermission($page = '', $tab = 'index') |
|
203 | + { |
|
204 | + $isAdmin = $this->isAdmin(); |
|
205 | + return !$isAdmin || ($isAdmin && $this->can($this->getPermission($page, $tab))); |
|
206 | + } |
|
207 | 207 | |
208 | - /** |
|
209 | - * @return void |
|
210 | - */ |
|
211 | - public function init() |
|
212 | - { |
|
213 | - $this->make(Actions::class)->run(); |
|
214 | - $this->make(Filters::class)->run(); |
|
215 | - } |
|
208 | + /** |
|
209 | + * @return void |
|
210 | + */ |
|
211 | + public function init() |
|
212 | + { |
|
213 | + $this->make(Actions::class)->run(); |
|
214 | + $this->make(Filters::class)->run(); |
|
215 | + } |
|
216 | 216 | |
217 | - /** |
|
218 | - * @return bool |
|
219 | - */ |
|
220 | - public function isAdmin() |
|
221 | - { |
|
222 | - return is_admin() && !wp_doing_ajax(); |
|
223 | - } |
|
217 | + /** |
|
218 | + * @return bool |
|
219 | + */ |
|
220 | + public function isAdmin() |
|
221 | + { |
|
222 | + return is_admin() && !wp_doing_ajax(); |
|
223 | + } |
|
224 | 224 | |
225 | - /** |
|
226 | - * @param string $file |
|
227 | - * @return string |
|
228 | - */ |
|
229 | - public function path($file = '', $realpath = true) |
|
230 | - { |
|
231 | - $path = plugin_dir_path($this->file); |
|
232 | - if (!$realpath) { |
|
233 | - $path = trailingslashit(WP_PLUGIN_DIR).basename(dirname($this->file)); |
|
234 | - } |
|
235 | - $path = trailingslashit($path).ltrim(trim($file), '/'); |
|
236 | - return apply_filters('site-reviews/path', $path, $file); |
|
237 | - } |
|
225 | + /** |
|
226 | + * @param string $file |
|
227 | + * @return string |
|
228 | + */ |
|
229 | + public function path($file = '', $realpath = true) |
|
230 | + { |
|
231 | + $path = plugin_dir_path($this->file); |
|
232 | + if (!$realpath) { |
|
233 | + $path = trailingslashit(WP_PLUGIN_DIR).basename(dirname($this->file)); |
|
234 | + } |
|
235 | + $path = trailingslashit($path).ltrim(trim($file), '/'); |
|
236 | + return apply_filters('site-reviews/path', $path, $file); |
|
237 | + } |
|
238 | 238 | |
239 | - /** |
|
240 | - * @param object $addon |
|
241 | - * @return void |
|
242 | - */ |
|
243 | - public function register($addon) |
|
244 | - { |
|
245 | - try { |
|
246 | - $reflection = new \ReflectionClass($addon); |
|
247 | - if ($id = $reflection->getConstant('ID')) { |
|
248 | - $this->addons[] = $id; |
|
249 | - $this->bind($id, $addon); |
|
250 | - $addon->init(); |
|
251 | - } |
|
252 | - } catch(\ReflectionException $e) { |
|
253 | - glsr_log()->error('Attempted to register an invalid addon.'); |
|
254 | - } |
|
255 | - } |
|
239 | + /** |
|
240 | + * @param object $addon |
|
241 | + * @return void |
|
242 | + */ |
|
243 | + public function register($addon) |
|
244 | + { |
|
245 | + try { |
|
246 | + $reflection = new \ReflectionClass($addon); |
|
247 | + if ($id = $reflection->getConstant('ID')) { |
|
248 | + $this->addons[] = $id; |
|
249 | + $this->bind($id, $addon); |
|
250 | + $addon->init(); |
|
251 | + } |
|
252 | + } catch(\ReflectionException $e) { |
|
253 | + glsr_log()->error('Attempted to register an invalid addon.'); |
|
254 | + } |
|
255 | + } |
|
256 | 256 | |
257 | - /** |
|
258 | - * @return void |
|
259 | - */ |
|
260 | - public function registerAddons() |
|
261 | - { |
|
262 | - do_action('site-reviews/addon/register', $this); |
|
263 | - } |
|
257 | + /** |
|
258 | + * @return void |
|
259 | + */ |
|
260 | + public function registerAddons() |
|
261 | + { |
|
262 | + do_action('site-reviews/addon/register', $this); |
|
263 | + } |
|
264 | 264 | |
265 | - /** |
|
266 | - * @return void |
|
267 | - */ |
|
268 | - public function registerLanguages() |
|
269 | - { |
|
270 | - load_plugin_textdomain(static::ID, false, |
|
271 | - trailingslashit(plugin_basename($this->path()).'/'.$this->languages) |
|
272 | - ); |
|
273 | - } |
|
265 | + /** |
|
266 | + * @return void |
|
267 | + */ |
|
268 | + public function registerLanguages() |
|
269 | + { |
|
270 | + load_plugin_textdomain(static::ID, false, |
|
271 | + trailingslashit(plugin_basename($this->path()).'/'.$this->languages) |
|
272 | + ); |
|
273 | + } |
|
274 | 274 | |
275 | - /** |
|
276 | - * @return void |
|
277 | - */ |
|
278 | - public function registerReviewTypes() |
|
279 | - { |
|
280 | - $types = apply_filters('site-reviews/addon/types', []); |
|
281 | - $this->reviewTypes = wp_parse_args($types, [ |
|
282 | - 'local' => __('Local', 'site-reviews'), |
|
283 | - ]); |
|
284 | - } |
|
275 | + /** |
|
276 | + * @return void |
|
277 | + */ |
|
278 | + public function registerReviewTypes() |
|
279 | + { |
|
280 | + $types = apply_filters('site-reviews/addon/types', []); |
|
281 | + $this->reviewTypes = wp_parse_args($types, [ |
|
282 | + 'local' => __('Local', 'site-reviews'), |
|
283 | + ]); |
|
284 | + } |
|
285 | 285 | |
286 | - /** |
|
287 | - * @param string $view |
|
288 | - * @return void |
|
289 | - */ |
|
290 | - public function render($view, array $data = []) |
|
291 | - { |
|
292 | - $view = apply_filters('site-reviews/render/view', $view, $data); |
|
293 | - $file = apply_filters('site-reviews/views/file', $this->file($view), $view, $data); |
|
294 | - if (!file_exists($file)) { |
|
295 | - glsr_log()->error(sprintf('File not found: (%s) %s', $view, $file)); |
|
296 | - return; |
|
297 | - } |
|
298 | - $data = apply_filters('site-reviews/views/data', $data, $view); |
|
299 | - extract($data); |
|
300 | - include $file; |
|
301 | - } |
|
286 | + /** |
|
287 | + * @param string $view |
|
288 | + * @return void |
|
289 | + */ |
|
290 | + public function render($view, array $data = []) |
|
291 | + { |
|
292 | + $view = apply_filters('site-reviews/render/view', $view, $data); |
|
293 | + $file = apply_filters('site-reviews/views/file', $this->file($view), $view, $data); |
|
294 | + if (!file_exists($file)) { |
|
295 | + glsr_log()->error(sprintf('File not found: (%s) %s', $view, $file)); |
|
296 | + return; |
|
297 | + } |
|
298 | + $data = apply_filters('site-reviews/views/data', $data, $view); |
|
299 | + extract($data); |
|
300 | + include $file; |
|
301 | + } |
|
302 | 302 | |
303 | - /** |
|
304 | - * @return void |
|
305 | - */ |
|
306 | - public function scheduleCronJob() |
|
307 | - { |
|
308 | - if (false === wp_next_scheduled(static::CRON_EVENT)) { |
|
309 | - wp_schedule_event(time(), 'twicedaily', static::CRON_EVENT); |
|
310 | - } |
|
311 | - } |
|
303 | + /** |
|
304 | + * @return void |
|
305 | + */ |
|
306 | + public function scheduleCronJob() |
|
307 | + { |
|
308 | + if (false === wp_next_scheduled(static::CRON_EVENT)) { |
|
309 | + wp_schedule_event(time(), 'twicedaily', static::CRON_EVENT); |
|
310 | + } |
|
311 | + } |
|
312 | 312 | |
313 | - /** |
|
314 | - * @return void |
|
315 | - */ |
|
316 | - public function setDefaults() |
|
317 | - { |
|
318 | - if (get_option(static::ID.'activated')) { |
|
319 | - $this->make(DefaultsManager::class)->set(); |
|
320 | - delete_option(static::ID.'activated'); |
|
321 | - } |
|
322 | - } |
|
313 | + /** |
|
314 | + * @return void |
|
315 | + */ |
|
316 | + public function setDefaults() |
|
317 | + { |
|
318 | + if (get_option(static::ID.'activated')) { |
|
319 | + $this->make(DefaultsManager::class)->set(); |
|
320 | + delete_option(static::ID.'activated'); |
|
321 | + } |
|
322 | + } |
|
323 | 323 | |
324 | - /** |
|
325 | - * @param string $file |
|
326 | - * @return string |
|
327 | - */ |
|
328 | - public function themePath($file = '') |
|
329 | - { |
|
330 | - return get_stylesheet_directory().'/'.static::ID.'/'.ltrim(trim($file), '/'); |
|
331 | - } |
|
324 | + /** |
|
325 | + * @param string $file |
|
326 | + * @return string |
|
327 | + */ |
|
328 | + public function themePath($file = '') |
|
329 | + { |
|
330 | + return get_stylesheet_directory().'/'.static::ID.'/'.ltrim(trim($file), '/'); |
|
331 | + } |
|
332 | 332 | |
333 | - /** |
|
334 | - * @return void |
|
335 | - */ |
|
336 | - public function unscheduleCronJob() |
|
337 | - { |
|
338 | - wp_unschedule_event(intval(wp_next_scheduled(static::CRON_EVENT)), static::CRON_EVENT); |
|
339 | - } |
|
333 | + /** |
|
334 | + * @return void |
|
335 | + */ |
|
336 | + public function unscheduleCronJob() |
|
337 | + { |
|
338 | + wp_unschedule_event(intval(wp_next_scheduled(static::CRON_EVENT)), static::CRON_EVENT); |
|
339 | + } |
|
340 | 340 | |
341 | - /** |
|
342 | - * @param string $path |
|
343 | - * @return string |
|
344 | - */ |
|
345 | - public function url($path = '') |
|
346 | - { |
|
347 | - $url = esc_url(plugin_dir_url($this->file).ltrim(trim($path), '/')); |
|
348 | - return apply_filters('site-reviews/url', $url, $path); |
|
349 | - } |
|
341 | + /** |
|
342 | + * @param string $path |
|
343 | + * @return string |
|
344 | + */ |
|
345 | + public function url($path = '') |
|
346 | + { |
|
347 | + $url = esc_url(plugin_dir_url($this->file).ltrim(trim($path), '/')); |
|
348 | + return apply_filters('site-reviews/url', $url, $path); |
|
349 | + } |
|
350 | 350 | |
351 | - /** |
|
352 | - * @param string $versionLevel |
|
353 | - * @return string |
|
354 | - */ |
|
355 | - public function version($versionLevel = '') |
|
356 | - { |
|
357 | - $pattern = '/^v?(\d{1,5})(\.\d++)?(\.\d++)?(.+)?$/i'; |
|
358 | - preg_match($pattern, $this->version, $matches); |
|
359 | - switch ($versionLevel) { |
|
360 | - case 'major': |
|
361 | - $version = Arr::get($matches, 1); |
|
362 | - break; |
|
363 | - case 'minor': |
|
364 | - $version = Arr::get($matches, 1).Arr::get($matches, 2); |
|
365 | - break; |
|
366 | - case 'patch': |
|
367 | - $version = Arr::get($matches, 1).Arr::get($matches, 2).Arr::get($matches, 3); |
|
368 | - break; |
|
369 | - } |
|
370 | - return empty($version) |
|
371 | - ? $this->version |
|
372 | - : $version; |
|
373 | - } |
|
351 | + /** |
|
352 | + * @param string $versionLevel |
|
353 | + * @return string |
|
354 | + */ |
|
355 | + public function version($versionLevel = '') |
|
356 | + { |
|
357 | + $pattern = '/^v?(\d{1,5})(\.\d++)?(\.\d++)?(.+)?$/i'; |
|
358 | + preg_match($pattern, $this->version, $matches); |
|
359 | + switch ($versionLevel) { |
|
360 | + case 'major': |
|
361 | + $version = Arr::get($matches, 1); |
|
362 | + break; |
|
363 | + case 'minor': |
|
364 | + $version = Arr::get($matches, 1).Arr::get($matches, 2); |
|
365 | + break; |
|
366 | + case 'patch': |
|
367 | + $version = Arr::get($matches, 1).Arr::get($matches, 2).Arr::get($matches, 3); |
|
368 | + break; |
|
369 | + } |
|
370 | + return empty($version) |
|
371 | + ? $this->version |
|
372 | + : $version; |
|
373 | + } |
|
374 | 374 | } |
@@ -8,18 +8,18 @@ |
||
8 | 8 | |
9 | 9 | class RegisterWidgets |
10 | 10 | { |
11 | - /** |
|
12 | - * @return void |
|
13 | - */ |
|
14 | - public function handle(Command $command) |
|
15 | - { |
|
16 | - foreach ($command->widgets as $baseId => $args) { |
|
17 | - $widgetClass = Helper::buildClassName($baseId.'-widget', 'Widgets'); |
|
18 | - if (!class_exists($widgetClass)) { |
|
19 | - glsr_log()->error(sprintf('Widget class missing (%s)', $widgetClass)); |
|
20 | - continue; |
|
21 | - } |
|
22 | - register_widget(new $widgetClass(Application::PREFIX.$baseId, $args['name'], $args)); |
|
23 | - } |
|
24 | - } |
|
11 | + /** |
|
12 | + * @return void |
|
13 | + */ |
|
14 | + public function handle(Command $command) |
|
15 | + { |
|
16 | + foreach ($command->widgets as $baseId => $args) { |
|
17 | + $widgetClass = Helper::buildClassName($baseId.'-widget', 'Widgets'); |
|
18 | + if (!class_exists($widgetClass)) { |
|
19 | + glsr_log()->error(sprintf('Widget class missing (%s)', $widgetClass)); |
|
20 | + continue; |
|
21 | + } |
|
22 | + register_widget(new $widgetClass(Application::PREFIX.$baseId, $args['name'], $args)); |
|
23 | + } |
|
24 | + } |
|
25 | 25 | } |
@@ -10,158 +10,158 @@ |
||
10 | 10 | |
11 | 11 | class EnqueueAdminAssets |
12 | 12 | { |
13 | - /** |
|
14 | - * @var array |
|
15 | - */ |
|
16 | - protected $pointers; |
|
13 | + /** |
|
14 | + * @var array |
|
15 | + */ |
|
16 | + protected $pointers; |
|
17 | 17 | |
18 | - /** |
|
19 | - * @return void |
|
20 | - */ |
|
21 | - public function handle(Command $command) |
|
22 | - { |
|
23 | - $this->generatePointers($command->pointers); |
|
24 | - $this->enqueueAssets(); |
|
25 | - $this->localizeAssets(); |
|
26 | - } |
|
18 | + /** |
|
19 | + * @return void |
|
20 | + */ |
|
21 | + public function handle(Command $command) |
|
22 | + { |
|
23 | + $this->generatePointers($command->pointers); |
|
24 | + $this->enqueueAssets(); |
|
25 | + $this->localizeAssets(); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * @return void |
|
30 | - */ |
|
31 | - public function enqueueAssets() |
|
32 | - { |
|
33 | - if (!$this->isCurrentScreen()) { |
|
34 | - return; |
|
35 | - } |
|
36 | - wp_enqueue_style( |
|
37 | - Application::ID.'/admin', |
|
38 | - glsr()->url('assets/styles/'.Application::ID.'-admin.css'), |
|
39 | - [], |
|
40 | - glsr()->version |
|
41 | - ); |
|
42 | - wp_enqueue_script( |
|
43 | - Application::ID.'/admin', |
|
44 | - glsr()->url('assets/scripts/'.Application::ID.'-admin.js'), |
|
45 | - $this->getDependencies(), |
|
46 | - glsr()->version, |
|
47 | - true |
|
48 | - ); |
|
49 | - if (!empty($this->pointers)) { |
|
50 | - wp_enqueue_style('wp-pointer'); |
|
51 | - wp_enqueue_script('wp-pointer'); |
|
52 | - } |
|
53 | - } |
|
28 | + /** |
|
29 | + * @return void |
|
30 | + */ |
|
31 | + public function enqueueAssets() |
|
32 | + { |
|
33 | + if (!$this->isCurrentScreen()) { |
|
34 | + return; |
|
35 | + } |
|
36 | + wp_enqueue_style( |
|
37 | + Application::ID.'/admin', |
|
38 | + glsr()->url('assets/styles/'.Application::ID.'-admin.css'), |
|
39 | + [], |
|
40 | + glsr()->version |
|
41 | + ); |
|
42 | + wp_enqueue_script( |
|
43 | + Application::ID.'/admin', |
|
44 | + glsr()->url('assets/scripts/'.Application::ID.'-admin.js'), |
|
45 | + $this->getDependencies(), |
|
46 | + glsr()->version, |
|
47 | + true |
|
48 | + ); |
|
49 | + if (!empty($this->pointers)) { |
|
50 | + wp_enqueue_style('wp-pointer'); |
|
51 | + wp_enqueue_script('wp-pointer'); |
|
52 | + } |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @return void |
|
57 | - */ |
|
58 | - public function localizeAssets() |
|
59 | - { |
|
60 | - $variables = [ |
|
61 | - 'action' => Application::PREFIX.'action', |
|
62 | - 'addons' => [], |
|
63 | - 'ajaxurl' => admin_url('admin-ajax.php'), |
|
64 | - 'hideoptions' => [ |
|
65 | - 'site_reviews' => glsr(SiteReviewsShortcode::class)->getHideOptions(), |
|
66 | - 'site_reviews_form' => glsr(SiteReviewsFormShortcode::class)->getHideOptions(), |
|
67 | - 'site_reviews_summary' => glsr(SiteReviewsSummaryShortcode::class)->getHideOptions(), |
|
68 | - ], |
|
69 | - 'nameprefix' => Application::ID, |
|
70 | - 'nonce' => [ |
|
71 | - 'change-status' => wp_create_nonce('change-status'), |
|
72 | - 'clear-console' => wp_create_nonce('clear-console'), |
|
73 | - 'count-reviews' => wp_create_nonce('count-reviews'), |
|
74 | - 'fetch-console' => wp_create_nonce('fetch-console'), |
|
75 | - 'mce-shortcode' => wp_create_nonce('mce-shortcode'), |
|
76 | - 'sync-reviews' => wp_create_nonce('sync-reviews'), |
|
77 | - 'toggle-pinned' => wp_create_nonce('toggle-pinned'), |
|
78 | - ], |
|
79 | - 'pointers' => $this->pointers, |
|
80 | - 'shortcodes' => [], |
|
81 | - 'tinymce' => [ |
|
82 | - 'glsr_shortcode' => glsr()->url('assets/scripts/mce-plugin.js'), |
|
83 | - ], |
|
84 | - ]; |
|
85 | - if (user_can_richedit()) { |
|
86 | - $variables['shortcodes'] = $this->localizeShortcodes(); |
|
87 | - } |
|
88 | - $variables = apply_filters('site-reviews/enqueue/admin/localize', $variables); |
|
89 | - wp_localize_script(Application::ID.'/admin', 'GLSR', $variables); |
|
90 | - } |
|
55 | + /** |
|
56 | + * @return void |
|
57 | + */ |
|
58 | + public function localizeAssets() |
|
59 | + { |
|
60 | + $variables = [ |
|
61 | + 'action' => Application::PREFIX.'action', |
|
62 | + 'addons' => [], |
|
63 | + 'ajaxurl' => admin_url('admin-ajax.php'), |
|
64 | + 'hideoptions' => [ |
|
65 | + 'site_reviews' => glsr(SiteReviewsShortcode::class)->getHideOptions(), |
|
66 | + 'site_reviews_form' => glsr(SiteReviewsFormShortcode::class)->getHideOptions(), |
|
67 | + 'site_reviews_summary' => glsr(SiteReviewsSummaryShortcode::class)->getHideOptions(), |
|
68 | + ], |
|
69 | + 'nameprefix' => Application::ID, |
|
70 | + 'nonce' => [ |
|
71 | + 'change-status' => wp_create_nonce('change-status'), |
|
72 | + 'clear-console' => wp_create_nonce('clear-console'), |
|
73 | + 'count-reviews' => wp_create_nonce('count-reviews'), |
|
74 | + 'fetch-console' => wp_create_nonce('fetch-console'), |
|
75 | + 'mce-shortcode' => wp_create_nonce('mce-shortcode'), |
|
76 | + 'sync-reviews' => wp_create_nonce('sync-reviews'), |
|
77 | + 'toggle-pinned' => wp_create_nonce('toggle-pinned'), |
|
78 | + ], |
|
79 | + 'pointers' => $this->pointers, |
|
80 | + 'shortcodes' => [], |
|
81 | + 'tinymce' => [ |
|
82 | + 'glsr_shortcode' => glsr()->url('assets/scripts/mce-plugin.js'), |
|
83 | + ], |
|
84 | + ]; |
|
85 | + if (user_can_richedit()) { |
|
86 | + $variables['shortcodes'] = $this->localizeShortcodes(); |
|
87 | + } |
|
88 | + $variables = apply_filters('site-reviews/enqueue/admin/localize', $variables); |
|
89 | + wp_localize_script(Application::ID.'/admin', 'GLSR', $variables); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * @return array |
|
94 | - */ |
|
95 | - protected function getDependencies() |
|
96 | - { |
|
97 | - $dependencies = apply_filters('site-reviews/enqueue/admin/dependencies', []); |
|
98 | - $dependencies = array_merge($dependencies, [ |
|
99 | - 'jquery', 'jquery-ui-sortable', 'underscore', 'wp-util', |
|
100 | - ]); |
|
101 | - return $dependencies; |
|
102 | - } |
|
92 | + /** |
|
93 | + * @return array |
|
94 | + */ |
|
95 | + protected function getDependencies() |
|
96 | + { |
|
97 | + $dependencies = apply_filters('site-reviews/enqueue/admin/dependencies', []); |
|
98 | + $dependencies = array_merge($dependencies, [ |
|
99 | + 'jquery', 'jquery-ui-sortable', 'underscore', 'wp-util', |
|
100 | + ]); |
|
101 | + return $dependencies; |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * @return array |
|
106 | - */ |
|
107 | - protected function generatePointer(array $pointer) |
|
108 | - { |
|
109 | - return [ |
|
110 | - 'id' => $pointer['id'], |
|
111 | - 'options' => [ |
|
112 | - 'content' => '<h3>'.$pointer['title'].'</h3><p>'.$pointer['content'].'</p>', |
|
113 | - 'position' => $pointer['position'], |
|
114 | - ], |
|
115 | - 'screen' => $pointer['screen'], |
|
116 | - 'target' => $pointer['target'], |
|
117 | - ]; |
|
118 | - } |
|
104 | + /** |
|
105 | + * @return array |
|
106 | + */ |
|
107 | + protected function generatePointer(array $pointer) |
|
108 | + { |
|
109 | + return [ |
|
110 | + 'id' => $pointer['id'], |
|
111 | + 'options' => [ |
|
112 | + 'content' => '<h3>'.$pointer['title'].'</h3><p>'.$pointer['content'].'</p>', |
|
113 | + 'position' => $pointer['position'], |
|
114 | + ], |
|
115 | + 'screen' => $pointer['screen'], |
|
116 | + 'target' => $pointer['target'], |
|
117 | + ]; |
|
118 | + } |
|
119 | 119 | |
120 | - /** |
|
121 | - * @return void |
|
122 | - */ |
|
123 | - protected function generatePointers(array $pointers) |
|
124 | - { |
|
125 | - $dismissedPointers = get_user_meta(get_current_user_id(), 'dismissed_wp_pointers', true); |
|
126 | - $dismissedPointers = explode(',', (string) $dismissedPointers); |
|
127 | - $generatedPointers = []; |
|
128 | - foreach ($pointers as $pointer) { |
|
129 | - if ($pointer['screen'] != glsr_current_screen()->id) { |
|
130 | - continue; |
|
131 | - } |
|
132 | - if (in_array($pointer['id'], $dismissedPointers)) { |
|
133 | - continue; |
|
134 | - } |
|
135 | - $generatedPointers[] = $this->generatePointer($pointer); |
|
136 | - } |
|
137 | - $this->pointers = $generatedPointers; |
|
138 | - } |
|
120 | + /** |
|
121 | + * @return void |
|
122 | + */ |
|
123 | + protected function generatePointers(array $pointers) |
|
124 | + { |
|
125 | + $dismissedPointers = get_user_meta(get_current_user_id(), 'dismissed_wp_pointers', true); |
|
126 | + $dismissedPointers = explode(',', (string) $dismissedPointers); |
|
127 | + $generatedPointers = []; |
|
128 | + foreach ($pointers as $pointer) { |
|
129 | + if ($pointer['screen'] != glsr_current_screen()->id) { |
|
130 | + continue; |
|
131 | + } |
|
132 | + if (in_array($pointer['id'], $dismissedPointers)) { |
|
133 | + continue; |
|
134 | + } |
|
135 | + $generatedPointers[] = $this->generatePointer($pointer); |
|
136 | + } |
|
137 | + $this->pointers = $generatedPointers; |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * @return bool |
|
142 | - */ |
|
143 | - protected function isCurrentScreen() |
|
144 | - { |
|
145 | - $screen = glsr_current_screen(); |
|
146 | - return Application::POST_TYPE == $screen->post_type |
|
147 | - || 'dashboard' == $screen->id |
|
148 | - || 'plugins_page_'.Application::ID == $screen->id |
|
149 | - || 'post' == $screen->base |
|
150 | - || 'widgets' == $screen->id; |
|
151 | - } |
|
140 | + /** |
|
141 | + * @return bool |
|
142 | + */ |
|
143 | + protected function isCurrentScreen() |
|
144 | + { |
|
145 | + $screen = glsr_current_screen(); |
|
146 | + return Application::POST_TYPE == $screen->post_type |
|
147 | + || 'dashboard' == $screen->id |
|
148 | + || 'plugins_page_'.Application::ID == $screen->id |
|
149 | + || 'post' == $screen->base |
|
150 | + || 'widgets' == $screen->id; |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * @return array |
|
155 | - */ |
|
156 | - protected function localizeShortcodes() |
|
157 | - { |
|
158 | - $variables = []; |
|
159 | - foreach (glsr()->mceShortcodes as $tag => $args) { |
|
160 | - if (empty($args['required'])) { |
|
161 | - continue; |
|
162 | - } |
|
163 | - $variables[$tag] = $args['required']; |
|
164 | - } |
|
165 | - return $variables; |
|
166 | - } |
|
153 | + /** |
|
154 | + * @return array |
|
155 | + */ |
|
156 | + protected function localizeShortcodes() |
|
157 | + { |
|
158 | + $variables = []; |
|
159 | + foreach (glsr()->mceShortcodes as $tag => $args) { |
|
160 | + if (empty($args['required'])) { |
|
161 | + continue; |
|
162 | + } |
|
163 | + $variables[$tag] = $args['required']; |
|
164 | + } |
|
165 | + return $variables; |
|
166 | + } |
|
167 | 167 | } |
@@ -7,18 +7,18 @@ |
||
7 | 7 | |
8 | 8 | class RegisterShortcodes |
9 | 9 | { |
10 | - /** |
|
11 | - * @return void |
|
12 | - */ |
|
13 | - public function handle(Command $command) |
|
14 | - { |
|
15 | - foreach ($command->shortcodes as $shortcode) { |
|
16 | - $shortcodeClass = Helper::buildClassName($shortcode.'-shortcode', 'Shortcodes'); |
|
17 | - if (!class_exists($shortcodeClass)) { |
|
18 | - glsr_log()->error(sprintf('Shortcode class missing (%s)', $shortcodeClass)); |
|
19 | - continue; |
|
20 | - } |
|
21 | - add_shortcode($shortcode, [glsr($shortcodeClass), 'buildShortcode']); |
|
22 | - } |
|
23 | - } |
|
10 | + /** |
|
11 | + * @return void |
|
12 | + */ |
|
13 | + public function handle(Command $command) |
|
14 | + { |
|
15 | + foreach ($command->shortcodes as $shortcode) { |
|
16 | + $shortcodeClass = Helper::buildClassName($shortcode.'-shortcode', 'Shortcodes'); |
|
17 | + if (!class_exists($shortcodeClass)) { |
|
18 | + glsr_log()->error(sprintf('Shortcode class missing (%s)', $shortcodeClass)); |
|
19 | + continue; |
|
20 | + } |
|
21 | + add_shortcode($shortcode, [glsr($shortcodeClass), 'buildShortcode']); |
|
22 | + } |
|
23 | + } |
|
24 | 24 | } |
@@ -7,22 +7,22 @@ |
||
7 | 7 | |
8 | 8 | class RegisterTinymcePopups |
9 | 9 | { |
10 | - /** |
|
11 | - * @return void |
|
12 | - */ |
|
13 | - public function handle(Command $command) |
|
14 | - { |
|
15 | - foreach ($command->popups as $slug => $label) { |
|
16 | - $buttonClass = Helper::buildClassName($slug.'-popup', 'Shortcodes'); |
|
17 | - if (!class_exists($buttonClass)) { |
|
18 | - glsr_log()->error(sprintf('Tinymce Popup class missing (%s)', $buttonClass)); |
|
19 | - continue; |
|
20 | - } |
|
21 | - $shortcode = glsr($buttonClass)->register($slug, [ |
|
22 | - 'label' => $label, |
|
23 | - 'title' => $label, |
|
24 | - ]); |
|
25 | - glsr()->mceShortcodes[$slug] = $shortcode->properties; |
|
26 | - } |
|
27 | - } |
|
10 | + /** |
|
11 | + * @return void |
|
12 | + */ |
|
13 | + public function handle(Command $command) |
|
14 | + { |
|
15 | + foreach ($command->popups as $slug => $label) { |
|
16 | + $buttonClass = Helper::buildClassName($slug.'-popup', 'Shortcodes'); |
|
17 | + if (!class_exists($buttonClass)) { |
|
18 | + glsr_log()->error(sprintf('Tinymce Popup class missing (%s)', $buttonClass)); |
|
19 | + continue; |
|
20 | + } |
|
21 | + $shortcode = glsr($buttonClass)->register($slug, [ |
|
22 | + 'label' => $label, |
|
23 | + 'title' => $label, |
|
24 | + ]); |
|
25 | + glsr()->mceShortcodes[$slug] = $shortcode->properties; |
|
26 | + } |
|
27 | + } |
|
28 | 28 | } |
@@ -12,173 +12,173 @@ |
||
12 | 12 | |
13 | 13 | class Review implements \ArrayAccess |
14 | 14 | { |
15 | - public $assigned_to; |
|
16 | - public $author; |
|
17 | - public $avatar; |
|
18 | - public $content; |
|
19 | - public $custom; |
|
20 | - public $date; |
|
21 | - public $email; |
|
22 | - public $ID; |
|
23 | - public $ip_address; |
|
24 | - public $modified; |
|
25 | - public $pinned; |
|
26 | - public $rating; |
|
27 | - public $response; |
|
28 | - public $review_id; |
|
29 | - public $review_type; |
|
30 | - public $status; |
|
31 | - public $term_ids; |
|
32 | - public $title; |
|
33 | - public $url; |
|
34 | - public $user_id; |
|
15 | + public $assigned_to; |
|
16 | + public $author; |
|
17 | + public $avatar; |
|
18 | + public $content; |
|
19 | + public $custom; |
|
20 | + public $date; |
|
21 | + public $email; |
|
22 | + public $ID; |
|
23 | + public $ip_address; |
|
24 | + public $modified; |
|
25 | + public $pinned; |
|
26 | + public $rating; |
|
27 | + public $response; |
|
28 | + public $review_id; |
|
29 | + public $review_type; |
|
30 | + public $status; |
|
31 | + public $term_ids; |
|
32 | + public $title; |
|
33 | + public $url; |
|
34 | + public $user_id; |
|
35 | 35 | |
36 | - public function __construct(WP_Post $post) |
|
37 | - { |
|
38 | - if (Application::POST_TYPE != $post->post_type) { |
|
39 | - return; |
|
40 | - } |
|
41 | - $this->content = $post->post_content; |
|
42 | - $this->date = $post->post_date; |
|
43 | - $this->ID = intval($post->ID); |
|
44 | - $this->status = $post->post_status; |
|
45 | - $this->title = $post->post_title; |
|
46 | - $this->user_id = intval($post->post_author); |
|
47 | - $this->setProperties($post); |
|
48 | - $this->setTermIds($post); |
|
49 | - } |
|
36 | + public function __construct(WP_Post $post) |
|
37 | + { |
|
38 | + if (Application::POST_TYPE != $post->post_type) { |
|
39 | + return; |
|
40 | + } |
|
41 | + $this->content = $post->post_content; |
|
42 | + $this->date = $post->post_date; |
|
43 | + $this->ID = intval($post->ID); |
|
44 | + $this->status = $post->post_status; |
|
45 | + $this->title = $post->post_title; |
|
46 | + $this->user_id = intval($post->post_author); |
|
47 | + $this->setProperties($post); |
|
48 | + $this->setTermIds($post); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @return mixed |
|
53 | - */ |
|
54 | - public function __get($key) |
|
55 | - { |
|
56 | - return $this->offsetGet($key); |
|
57 | - } |
|
51 | + /** |
|
52 | + * @return mixed |
|
53 | + */ |
|
54 | + public function __get($key) |
|
55 | + { |
|
56 | + return $this->offsetGet($key); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * @return string |
|
61 | - */ |
|
62 | - public function __toString() |
|
63 | - { |
|
64 | - return (string) $this->build(); |
|
65 | - } |
|
59 | + /** |
|
60 | + * @return string |
|
61 | + */ |
|
62 | + public function __toString() |
|
63 | + { |
|
64 | + return (string) $this->build(); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * @return ReviewHtml |
|
69 | - */ |
|
70 | - public function build(array $args = []) |
|
71 | - { |
|
72 | - if (empty($this->ID)) { |
|
73 | - return new ReviewHtml($this); |
|
74 | - } |
|
75 | - $partial = glsr(SiteReviewsPartial::class); |
|
76 | - $partial->args = glsr(SiteReviewsDefaults::class)->merge($args); |
|
77 | - $partial->options = Arr::flatten(glsr(OptionManager::class)->all()); |
|
78 | - return $partial->buildReview($this); |
|
79 | - } |
|
67 | + /** |
|
68 | + * @return ReviewHtml |
|
69 | + */ |
|
70 | + public function build(array $args = []) |
|
71 | + { |
|
72 | + if (empty($this->ID)) { |
|
73 | + return new ReviewHtml($this); |
|
74 | + } |
|
75 | + $partial = glsr(SiteReviewsPartial::class); |
|
76 | + $partial->args = glsr(SiteReviewsDefaults::class)->merge($args); |
|
77 | + $partial->options = Arr::flatten(glsr(OptionManager::class)->all()); |
|
78 | + return $partial->buildReview($this); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * @param mixed $key |
|
83 | - * @return bool |
|
84 | - */ |
|
85 | - public function offsetExists($key) |
|
86 | - { |
|
87 | - return property_exists($this, $key) || array_key_exists($key, (array) $this->custom); |
|
88 | - } |
|
81 | + /** |
|
82 | + * @param mixed $key |
|
83 | + * @return bool |
|
84 | + */ |
|
85 | + public function offsetExists($key) |
|
86 | + { |
|
87 | + return property_exists($this, $key) || array_key_exists($key, (array) $this->custom); |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * @param mixed $key |
|
92 | - * @return mixed |
|
93 | - */ |
|
94 | - public function offsetGet($key) |
|
95 | - { |
|
96 | - return property_exists($this, $key) |
|
97 | - ? $this->$key |
|
98 | - : Arr::get($this->custom, $key, null); |
|
99 | - } |
|
90 | + /** |
|
91 | + * @param mixed $key |
|
92 | + * @return mixed |
|
93 | + */ |
|
94 | + public function offsetGet($key) |
|
95 | + { |
|
96 | + return property_exists($this, $key) |
|
97 | + ? $this->$key |
|
98 | + : Arr::get($this->custom, $key, null); |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * @param mixed $key |
|
103 | - * @param mixed $value |
|
104 | - * @return void |
|
105 | - */ |
|
106 | - public function offsetSet($key, $value) |
|
107 | - { |
|
108 | - if (property_exists($this, $key)) { |
|
109 | - $this->$key = $value; |
|
110 | - return; |
|
111 | - } |
|
112 | - if (!is_array($this->custom)) { |
|
113 | - $this->custom = array_filter((array) $this->custom); |
|
114 | - } |
|
115 | - $this->custom[$key] = $value; |
|
116 | - } |
|
101 | + /** |
|
102 | + * @param mixed $key |
|
103 | + * @param mixed $value |
|
104 | + * @return void |
|
105 | + */ |
|
106 | + public function offsetSet($key, $value) |
|
107 | + { |
|
108 | + if (property_exists($this, $key)) { |
|
109 | + $this->$key = $value; |
|
110 | + return; |
|
111 | + } |
|
112 | + if (!is_array($this->custom)) { |
|
113 | + $this->custom = array_filter((array) $this->custom); |
|
114 | + } |
|
115 | + $this->custom[$key] = $value; |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * @param mixed $key |
|
120 | - * @return void |
|
121 | - */ |
|
122 | - public function offsetUnset($key) |
|
123 | - { |
|
124 | - $this->offsetSet($key, null); |
|
125 | - } |
|
118 | + /** |
|
119 | + * @param mixed $key |
|
120 | + * @return void |
|
121 | + */ |
|
122 | + public function offsetUnset($key) |
|
123 | + { |
|
124 | + $this->offsetSet($key, null); |
|
125 | + } |
|
126 | 126 | |
127 | - /** |
|
128 | - * @return void |
|
129 | - */ |
|
130 | - public function render() |
|
131 | - { |
|
132 | - echo $this->build(); |
|
133 | - } |
|
127 | + /** |
|
128 | + * @return void |
|
129 | + */ |
|
130 | + public function render() |
|
131 | + { |
|
132 | + echo $this->build(); |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * @return bool |
|
137 | - */ |
|
138 | - protected function isModified(array $properties) |
|
139 | - { |
|
140 | - return $this->date != $properties['date'] |
|
141 | - || $this->content != $properties['content'] |
|
142 | - || $this->title != $properties['title']; |
|
143 | - } |
|
135 | + /** |
|
136 | + * @return bool |
|
137 | + */ |
|
138 | + protected function isModified(array $properties) |
|
139 | + { |
|
140 | + return $this->date != $properties['date'] |
|
141 | + || $this->content != $properties['content'] |
|
142 | + || $this->title != $properties['title']; |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * @return void |
|
147 | - */ |
|
148 | - protected function setProperties(WP_Post $post) |
|
149 | - { |
|
150 | - $defaults = [ |
|
151 | - 'author' => __('Anonymous', 'site-reviews'), |
|
152 | - 'date' => '', |
|
153 | - 'review_id' => '', |
|
154 | - 'review_type' => 'local', |
|
155 | - ]; |
|
156 | - $meta = array_filter( |
|
157 | - array_map('array_shift', array_filter((array) get_post_meta($post->ID))), |
|
158 | - 'strlen' |
|
159 | - ); |
|
160 | - $meta = array_merge($defaults, Arr::unprefixKeys($meta)); |
|
161 | - $properties = glsr(CreateReviewDefaults::class)->restrict(array_merge($defaults, $meta)); |
|
162 | - $this->modified = $this->isModified($properties); |
|
163 | - array_walk($properties, function ($value, $key) { |
|
164 | - if (!property_exists($this, $key) || isset($this->$key)) { |
|
165 | - return; |
|
166 | - } |
|
167 | - $this->$key = maybe_unserialize($value); |
|
168 | - }); |
|
169 | - } |
|
145 | + /** |
|
146 | + * @return void |
|
147 | + */ |
|
148 | + protected function setProperties(WP_Post $post) |
|
149 | + { |
|
150 | + $defaults = [ |
|
151 | + 'author' => __('Anonymous', 'site-reviews'), |
|
152 | + 'date' => '', |
|
153 | + 'review_id' => '', |
|
154 | + 'review_type' => 'local', |
|
155 | + ]; |
|
156 | + $meta = array_filter( |
|
157 | + array_map('array_shift', array_filter((array) get_post_meta($post->ID))), |
|
158 | + 'strlen' |
|
159 | + ); |
|
160 | + $meta = array_merge($defaults, Arr::unprefixKeys($meta)); |
|
161 | + $properties = glsr(CreateReviewDefaults::class)->restrict(array_merge($defaults, $meta)); |
|
162 | + $this->modified = $this->isModified($properties); |
|
163 | + array_walk($properties, function ($value, $key) { |
|
164 | + if (!property_exists($this, $key) || isset($this->$key)) { |
|
165 | + return; |
|
166 | + } |
|
167 | + $this->$key = maybe_unserialize($value); |
|
168 | + }); |
|
169 | + } |
|
170 | 170 | |
171 | - /** |
|
172 | - * @return void |
|
173 | - */ |
|
174 | - protected function setTermIds(WP_Post $post) |
|
175 | - { |
|
176 | - $this->term_ids = []; |
|
177 | - if (!is_array($terms = get_the_terms($post, Application::TAXONOMY))) { |
|
178 | - return; |
|
179 | - } |
|
180 | - foreach ($terms as $term) { |
|
181 | - $this->term_ids[] = $term->term_id; |
|
182 | - } |
|
183 | - } |
|
171 | + /** |
|
172 | + * @return void |
|
173 | + */ |
|
174 | + protected function setTermIds(WP_Post $post) |
|
175 | + { |
|
176 | + $this->term_ids = []; |
|
177 | + if (!is_array($terms = get_the_terms($post, Application::TAXONOMY))) { |
|
178 | + return; |
|
179 | + } |
|
180 | + foreach ($terms as $term) { |
|
181 | + $this->term_ids[] = $term->term_id; |
|
182 | + } |
|
183 | + } |
|
184 | 184 | } |
@@ -12,190 +12,190 @@ |
||
12 | 12 | |
13 | 13 | class QueryBuilder |
14 | 14 | { |
15 | - /** |
|
16 | - * Build a WP_Query meta_query/tax_query. |
|
17 | - * @return array |
|
18 | - */ |
|
19 | - public function buildQuery(array $keys = [], array $values = []) |
|
20 | - { |
|
21 | - $queries = []; |
|
22 | - foreach ($keys as $key) { |
|
23 | - if (!array_key_exists($key, $values)) { |
|
24 | - continue; |
|
25 | - } |
|
26 | - $methodName = Helper::buildMethodName($key, __FUNCTION__); |
|
27 | - if (!method_exists($this, $methodName)) { |
|
28 | - continue; |
|
29 | - } |
|
30 | - $query = call_user_func([$this, $methodName], $values[$key]); |
|
31 | - if (is_array($query)) { |
|
32 | - $queries[] = $query; |
|
33 | - } |
|
34 | - } |
|
35 | - return $queries; |
|
36 | - } |
|
15 | + /** |
|
16 | + * Build a WP_Query meta_query/tax_query. |
|
17 | + * @return array |
|
18 | + */ |
|
19 | + public function buildQuery(array $keys = [], array $values = []) |
|
20 | + { |
|
21 | + $queries = []; |
|
22 | + foreach ($keys as $key) { |
|
23 | + if (!array_key_exists($key, $values)) { |
|
24 | + continue; |
|
25 | + } |
|
26 | + $methodName = Helper::buildMethodName($key, __FUNCTION__); |
|
27 | + if (!method_exists($this, $methodName)) { |
|
28 | + continue; |
|
29 | + } |
|
30 | + $query = call_user_func([$this, $methodName], $values[$key]); |
|
31 | + if (is_array($query)) { |
|
32 | + $queries[] = $query; |
|
33 | + } |
|
34 | + } |
|
35 | + return $queries; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - public function buildSqlLines(array $values, array $conditions) |
|
42 | - { |
|
43 | - $string = ''; |
|
44 | - $values = array_filter($values); |
|
45 | - foreach ($conditions as $key => $value) { |
|
46 | - if (!isset($values[$key])) { |
|
47 | - continue; |
|
48 | - } |
|
49 | - $values[$key] = implode(',', (array) $values[$key]); |
|
50 | - $string.= Str::contains($value, '%s') |
|
51 | - ? sprintf($value, strval($values[$key])) |
|
52 | - : $value; |
|
53 | - } |
|
54 | - return $string; |
|
55 | - } |
|
38 | + /** |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + public function buildSqlLines(array $values, array $conditions) |
|
42 | + { |
|
43 | + $string = ''; |
|
44 | + $values = array_filter($values); |
|
45 | + foreach ($conditions as $key => $value) { |
|
46 | + if (!isset($values[$key])) { |
|
47 | + continue; |
|
48 | + } |
|
49 | + $values[$key] = implode(',', (array) $values[$key]); |
|
50 | + $string.= Str::contains($value, '%s') |
|
51 | + ? sprintf($value, strval($values[$key])) |
|
52 | + : $value; |
|
53 | + } |
|
54 | + return $string; |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * Build a SQL 'OR' string from an array. |
|
59 | - * @param string|array $values |
|
60 | - * @param string $sprintfFormat |
|
61 | - * @return string |
|
62 | - */ |
|
63 | - public function buildSqlOr($values, $sprintfFormat) |
|
64 | - { |
|
65 | - if (!is_array($values)) { |
|
66 | - $values = explode(',', $values); |
|
67 | - } |
|
68 | - $values = array_filter(array_map('trim', (array) $values)); |
|
69 | - $values = array_map(function ($value) use ($sprintfFormat) { |
|
70 | - return sprintf($sprintfFormat, $value); |
|
71 | - }, $values); |
|
72 | - return implode(' OR ', $values); |
|
73 | - } |
|
57 | + /** |
|
58 | + * Build a SQL 'OR' string from an array. |
|
59 | + * @param string|array $values |
|
60 | + * @param string $sprintfFormat |
|
61 | + * @return string |
|
62 | + */ |
|
63 | + public function buildSqlOr($values, $sprintfFormat) |
|
64 | + { |
|
65 | + if (!is_array($values)) { |
|
66 | + $values = explode(',', $values); |
|
67 | + } |
|
68 | + $values = array_filter(array_map('trim', (array) $values)); |
|
69 | + $values = array_map(function ($value) use ($sprintfFormat) { |
|
70 | + return sprintf($sprintfFormat, $value); |
|
71 | + }, $values); |
|
72 | + return implode(' OR ', $values); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Search SQL filter for matching against post title only. |
|
77 | - * @see http://wordpress.stackexchange.com/a/11826/1685 |
|
78 | - * @param string $search |
|
79 | - * @return string |
|
80 | - * @filter posts_search |
|
81 | - */ |
|
82 | - public function filterSearchByTitle($search, WP_Query $query) |
|
83 | - { |
|
84 | - if (empty($search) || empty($query->get('search_terms'))) { |
|
85 | - return $search; |
|
86 | - } |
|
87 | - global $wpdb; |
|
88 | - $n = empty($query->get('exact')) |
|
89 | - ? '%' |
|
90 | - : ''; |
|
91 | - $search = []; |
|
92 | - foreach ((array) $query->get('search_terms') as $term) { |
|
93 | - $search[] = $wpdb->prepare("{$wpdb->posts}.post_title LIKE %s", $n.$wpdb->esc_like($term).$n); |
|
94 | - } |
|
95 | - if (!is_user_logged_in()) { |
|
96 | - $search[] = "{$wpdb->posts}.post_password = ''"; |
|
97 | - } |
|
98 | - return ' AND '.implode(' AND ', $search); |
|
99 | - } |
|
75 | + /** |
|
76 | + * Search SQL filter for matching against post title only. |
|
77 | + * @see http://wordpress.stackexchange.com/a/11826/1685 |
|
78 | + * @param string $search |
|
79 | + * @return string |
|
80 | + * @filter posts_search |
|
81 | + */ |
|
82 | + public function filterSearchByTitle($search, WP_Query $query) |
|
83 | + { |
|
84 | + if (empty($search) || empty($query->get('search_terms'))) { |
|
85 | + return $search; |
|
86 | + } |
|
87 | + global $wpdb; |
|
88 | + $n = empty($query->get('exact')) |
|
89 | + ? '%' |
|
90 | + : ''; |
|
91 | + $search = []; |
|
92 | + foreach ((array) $query->get('search_terms') as $term) { |
|
93 | + $search[] = $wpdb->prepare("{$wpdb->posts}.post_title LIKE %s", $n.$wpdb->esc_like($term).$n); |
|
94 | + } |
|
95 | + if (!is_user_logged_in()) { |
|
96 | + $search[] = "{$wpdb->posts}.post_password = ''"; |
|
97 | + } |
|
98 | + return ' AND '.implode(' AND ', $search); |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Get the current page number from the global query. |
|
103 | - * @param bool $isEnabled |
|
104 | - * @return int |
|
105 | - */ |
|
106 | - public function getPaged($isEnabled = true) |
|
107 | - { |
|
108 | - return $isEnabled |
|
109 | - ? max(1, intval(filter_input(INPUT_GET, glsr()->constant('PAGED_QUERY_VAR')))) |
|
110 | - : 1; |
|
111 | - } |
|
101 | + /** |
|
102 | + * Get the current page number from the global query. |
|
103 | + * @param bool $isEnabled |
|
104 | + * @return int |
|
105 | + */ |
|
106 | + public function getPaged($isEnabled = true) |
|
107 | + { |
|
108 | + return $isEnabled |
|
109 | + ? max(1, intval(filter_input(INPUT_GET, glsr()->constant('PAGED_QUERY_VAR')))) |
|
110 | + : 1; |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * @param string $value |
|
115 | - * @return void|array |
|
116 | - */ |
|
117 | - protected function buildQueryAssignedTo($value) |
|
118 | - { |
|
119 | - if (!empty($value)) { |
|
120 | - $postIds = Arr::convertFromString($value, 'is_numeric'); |
|
121 | - return [ |
|
122 | - 'compare' => 'IN', |
|
123 | - 'key' => '_assigned_to', |
|
124 | - 'value' => glsr(Multilingual::class)->getPostIds($postIds), |
|
125 | - ]; |
|
126 | - } |
|
127 | - } |
|
113 | + /** |
|
114 | + * @param string $value |
|
115 | + * @return void|array |
|
116 | + */ |
|
117 | + protected function buildQueryAssignedTo($value) |
|
118 | + { |
|
119 | + if (!empty($value)) { |
|
120 | + $postIds = Arr::convertFromString($value, 'is_numeric'); |
|
121 | + return [ |
|
122 | + 'compare' => 'IN', |
|
123 | + 'key' => '_assigned_to', |
|
124 | + 'value' => glsr(Multilingual::class)->getPostIds($postIds), |
|
125 | + ]; |
|
126 | + } |
|
127 | + } |
|
128 | 128 | |
129 | - /** |
|
130 | - * @param array $value |
|
131 | - * @return void|array |
|
132 | - */ |
|
133 | - protected function buildQueryCategory($value) |
|
134 | - { |
|
135 | - if (!empty($value)) { |
|
136 | - return [ |
|
137 | - 'field' => 'term_id', |
|
138 | - 'taxonomy' => Application::TAXONOMY, |
|
139 | - 'terms' => $value, |
|
140 | - ]; |
|
141 | - } |
|
142 | - } |
|
129 | + /** |
|
130 | + * @param array $value |
|
131 | + * @return void|array |
|
132 | + */ |
|
133 | + protected function buildQueryCategory($value) |
|
134 | + { |
|
135 | + if (!empty($value)) { |
|
136 | + return [ |
|
137 | + 'field' => 'term_id', |
|
138 | + 'taxonomy' => Application::TAXONOMY, |
|
139 | + 'terms' => $value, |
|
140 | + ]; |
|
141 | + } |
|
142 | + } |
|
143 | 143 | |
144 | - /** |
|
145 | - * @param string $value |
|
146 | - * @return void|array |
|
147 | - */ |
|
148 | - protected function buildQueryEmail($value) |
|
149 | - { |
|
150 | - if (!empty($value)) { |
|
151 | - return [ |
|
152 | - 'key' => '_email', |
|
153 | - 'value' => $value, |
|
154 | - ]; |
|
155 | - } |
|
156 | - } |
|
144 | + /** |
|
145 | + * @param string $value |
|
146 | + * @return void|array |
|
147 | + */ |
|
148 | + protected function buildQueryEmail($value) |
|
149 | + { |
|
150 | + if (!empty($value)) { |
|
151 | + return [ |
|
152 | + 'key' => '_email', |
|
153 | + 'value' => $value, |
|
154 | + ]; |
|
155 | + } |
|
156 | + } |
|
157 | 157 | |
158 | - /** |
|
159 | - * @param string $value |
|
160 | - * @return void|array |
|
161 | - */ |
|
162 | - protected function buildQueryIpAddress($value) |
|
163 | - { |
|
164 | - if (!empty($value)) { |
|
165 | - return [ |
|
166 | - 'key' => '_ip_address', |
|
167 | - 'value' => $value, |
|
168 | - ]; |
|
169 | - } |
|
170 | - } |
|
158 | + /** |
|
159 | + * @param string $value |
|
160 | + * @return void|array |
|
161 | + */ |
|
162 | + protected function buildQueryIpAddress($value) |
|
163 | + { |
|
164 | + if (!empty($value)) { |
|
165 | + return [ |
|
166 | + 'key' => '_ip_address', |
|
167 | + 'value' => $value, |
|
168 | + ]; |
|
169 | + } |
|
170 | + } |
|
171 | 171 | |
172 | - /** |
|
173 | - * @param string $value |
|
174 | - * @return void|array |
|
175 | - */ |
|
176 | - protected function buildQueryRating($value) |
|
177 | - { |
|
178 | - if (is_numeric($value) |
|
179 | - && in_array(intval($value), range(1, glsr()->constant('MAX_RATING', Rating::class)))) { |
|
180 | - return [ |
|
181 | - 'compare' => '>=', |
|
182 | - 'key' => '_rating', |
|
183 | - 'value' => $value, |
|
184 | - ]; |
|
185 | - } |
|
186 | - } |
|
172 | + /** |
|
173 | + * @param string $value |
|
174 | + * @return void|array |
|
175 | + */ |
|
176 | + protected function buildQueryRating($value) |
|
177 | + { |
|
178 | + if (is_numeric($value) |
|
179 | + && in_array(intval($value), range(1, glsr()->constant('MAX_RATING', Rating::class)))) { |
|
180 | + return [ |
|
181 | + 'compare' => '>=', |
|
182 | + 'key' => '_rating', |
|
183 | + 'value' => $value, |
|
184 | + ]; |
|
185 | + } |
|
186 | + } |
|
187 | 187 | |
188 | - /** |
|
189 | - * @param string $value |
|
190 | - * @return void|array |
|
191 | - */ |
|
192 | - protected function buildQueryType($value) |
|
193 | - { |
|
194 | - if (!in_array($value, ['', 'all'])) { |
|
195 | - return [ |
|
196 | - 'key' => '_review_type', |
|
197 | - 'value' => $value, |
|
198 | - ]; |
|
199 | - } |
|
200 | - } |
|
188 | + /** |
|
189 | + * @param string $value |
|
190 | + * @return void|array |
|
191 | + */ |
|
192 | + protected function buildQueryType($value) |
|
193 | + { |
|
194 | + if (!in_array($value, ['', 'all'])) { |
|
195 | + return [ |
|
196 | + 'key' => '_review_type', |
|
197 | + 'value' => $value, |
|
198 | + ]; |
|
199 | + } |
|
200 | + } |
|
201 | 201 | } |
@@ -16,204 +16,204 @@ |
||
16 | 16 | |
17 | 17 | class ReviewManager |
18 | 18 | { |
19 | - /** |
|
20 | - * @return false|Review |
|
21 | - */ |
|
22 | - public function create(CreateReview $command) |
|
23 | - { |
|
24 | - $reviewValues = glsr(CreateReviewDefaults::class)->restrict((array) $command); |
|
25 | - $reviewValues = apply_filters('site-reviews/create/review-values', $reviewValues, $command); |
|
26 | - $reviewValues = Arr::prefixKeys($reviewValues); |
|
27 | - unset($reviewValues['json']); // @todo remove the need for this |
|
28 | - $postValues = [ |
|
29 | - 'comment_status' => 'closed', |
|
30 | - 'meta_input' => $reviewValues, |
|
31 | - 'ping_status' => 'closed', |
|
32 | - 'post_content' => $reviewValues['_content'], |
|
33 | - 'post_date' => $reviewValues['_date'], |
|
34 | - 'post_date_gmt' => get_gmt_from_date($reviewValues['_date']), |
|
35 | - 'post_name' => $reviewValues['_review_type'].'-'.$reviewValues['_review_id'], |
|
36 | - 'post_status' => $this->getNewPostStatus($reviewValues, $command->blacklisted), |
|
37 | - 'post_title' => $reviewValues['_title'], |
|
38 | - 'post_type' => Application::POST_TYPE, |
|
39 | - ]; |
|
40 | - $postId = wp_insert_post($postValues, true); |
|
41 | - if (is_wp_error($postId)) { |
|
42 | - glsr_log()->error($postId->get_error_message())->debug($postValues); |
|
43 | - return false; |
|
44 | - } |
|
45 | - $this->setTerms($postId, $command->category); |
|
46 | - $review = $this->single(get_post($postId)); |
|
47 | - do_action('site-reviews/review/created', $review, $command); |
|
48 | - return $review; |
|
49 | - } |
|
19 | + /** |
|
20 | + * @return false|Review |
|
21 | + */ |
|
22 | + public function create(CreateReview $command) |
|
23 | + { |
|
24 | + $reviewValues = glsr(CreateReviewDefaults::class)->restrict((array) $command); |
|
25 | + $reviewValues = apply_filters('site-reviews/create/review-values', $reviewValues, $command); |
|
26 | + $reviewValues = Arr::prefixKeys($reviewValues); |
|
27 | + unset($reviewValues['json']); // @todo remove the need for this |
|
28 | + $postValues = [ |
|
29 | + 'comment_status' => 'closed', |
|
30 | + 'meta_input' => $reviewValues, |
|
31 | + 'ping_status' => 'closed', |
|
32 | + 'post_content' => $reviewValues['_content'], |
|
33 | + 'post_date' => $reviewValues['_date'], |
|
34 | + 'post_date_gmt' => get_gmt_from_date($reviewValues['_date']), |
|
35 | + 'post_name' => $reviewValues['_review_type'].'-'.$reviewValues['_review_id'], |
|
36 | + 'post_status' => $this->getNewPostStatus($reviewValues, $command->blacklisted), |
|
37 | + 'post_title' => $reviewValues['_title'], |
|
38 | + 'post_type' => Application::POST_TYPE, |
|
39 | + ]; |
|
40 | + $postId = wp_insert_post($postValues, true); |
|
41 | + if (is_wp_error($postId)) { |
|
42 | + glsr_log()->error($postId->get_error_message())->debug($postValues); |
|
43 | + return false; |
|
44 | + } |
|
45 | + $this->setTerms($postId, $command->category); |
|
46 | + $review = $this->single(get_post($postId)); |
|
47 | + do_action('site-reviews/review/created', $review, $command); |
|
48 | + return $review; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @param string $metaReviewId |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function delete($metaReviewId) |
|
56 | - { |
|
57 | - if ($postId = $this->getPostId($metaReviewId)) { |
|
58 | - wp_delete_post($postId, true); |
|
59 | - } |
|
60 | - } |
|
51 | + /** |
|
52 | + * @param string $metaReviewId |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function delete($metaReviewId) |
|
56 | + { |
|
57 | + if ($postId = $this->getPostId($metaReviewId)) { |
|
58 | + wp_delete_post($postId, true); |
|
59 | + } |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * @return object |
|
64 | - */ |
|
65 | - public function get(array $args = []) |
|
66 | - { |
|
67 | - $args = glsr(ReviewsDefaults::class)->merge($args); |
|
68 | - $metaQuery = glsr(QueryBuilder::class)->buildQuery( |
|
69 | - ['assigned_to', 'email', 'ip_address', 'type', 'rating'], |
|
70 | - $args |
|
71 | - ); |
|
72 | - $taxQuery = glsr(QueryBuilder::class)->buildQuery( |
|
73 | - ['category'], |
|
74 | - ['category' => $this->normalizeTermIds($args['category'])] |
|
75 | - ); |
|
76 | - $paged = glsr(QueryBuilder::class)->getPaged( |
|
77 | - wp_validate_boolean($args['pagination']) |
|
78 | - ); |
|
79 | - $parameters = [ |
|
80 | - 'meta_key' => '_pinned', |
|
81 | - 'meta_query' => $metaQuery, |
|
82 | - 'offset' => $args['offset'], |
|
83 | - 'order' => $args['order'], |
|
84 | - 'orderby' => 'meta_value '.$args['orderby'], |
|
85 | - 'paged' => Arr::get($args, 'paged', $paged), |
|
86 | - 'post__in' => $args['post__in'], |
|
87 | - 'post__not_in' => $args['post__not_in'], |
|
88 | - 'post_status' => 'publish', |
|
89 | - 'post_type' => Application::POST_TYPE, |
|
90 | - 'posts_per_page' => $args['per_page'], |
|
91 | - 'tax_query' => $taxQuery, |
|
92 | - ]; |
|
93 | - $parameters = apply_filters('site-reviews/get/reviews/query', $parameters, $args); |
|
94 | - $query = new WP_Query($parameters); |
|
95 | - $results = array_map([$this, 'single'], $query->posts); |
|
96 | - $reviews = new Reviews($results, $query->max_num_pages, $args); |
|
97 | - return apply_filters('site-reviews/get/reviews', $reviews, $query); |
|
98 | - } |
|
62 | + /** |
|
63 | + * @return object |
|
64 | + */ |
|
65 | + public function get(array $args = []) |
|
66 | + { |
|
67 | + $args = glsr(ReviewsDefaults::class)->merge($args); |
|
68 | + $metaQuery = glsr(QueryBuilder::class)->buildQuery( |
|
69 | + ['assigned_to', 'email', 'ip_address', 'type', 'rating'], |
|
70 | + $args |
|
71 | + ); |
|
72 | + $taxQuery = glsr(QueryBuilder::class)->buildQuery( |
|
73 | + ['category'], |
|
74 | + ['category' => $this->normalizeTermIds($args['category'])] |
|
75 | + ); |
|
76 | + $paged = glsr(QueryBuilder::class)->getPaged( |
|
77 | + wp_validate_boolean($args['pagination']) |
|
78 | + ); |
|
79 | + $parameters = [ |
|
80 | + 'meta_key' => '_pinned', |
|
81 | + 'meta_query' => $metaQuery, |
|
82 | + 'offset' => $args['offset'], |
|
83 | + 'order' => $args['order'], |
|
84 | + 'orderby' => 'meta_value '.$args['orderby'], |
|
85 | + 'paged' => Arr::get($args, 'paged', $paged), |
|
86 | + 'post__in' => $args['post__in'], |
|
87 | + 'post__not_in' => $args['post__not_in'], |
|
88 | + 'post_status' => 'publish', |
|
89 | + 'post_type' => Application::POST_TYPE, |
|
90 | + 'posts_per_page' => $args['per_page'], |
|
91 | + 'tax_query' => $taxQuery, |
|
92 | + ]; |
|
93 | + $parameters = apply_filters('site-reviews/get/reviews/query', $parameters, $args); |
|
94 | + $query = new WP_Query($parameters); |
|
95 | + $results = array_map([$this, 'single'], $query->posts); |
|
96 | + $reviews = new Reviews($results, $query->max_num_pages, $args); |
|
97 | + return apply_filters('site-reviews/get/reviews', $reviews, $query); |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * @param string $metaReviewId |
|
102 | - * @return int |
|
103 | - */ |
|
104 | - public function getPostId($metaReviewId) |
|
105 | - { |
|
106 | - return glsr(SqlQueries::class)->getPostIdFromReviewId($metaReviewId); |
|
107 | - } |
|
100 | + /** |
|
101 | + * @param string $metaReviewId |
|
102 | + * @return int |
|
103 | + */ |
|
104 | + public function getPostId($metaReviewId) |
|
105 | + { |
|
106 | + return glsr(SqlQueries::class)->getPostIdFromReviewId($metaReviewId); |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * @return array |
|
111 | - */ |
|
112 | - public function getRatingCounts(array $args = []) |
|
113 | - { |
|
114 | - $args = glsr(SiteReviewsSummaryDefaults::class)->filter($args); |
|
115 | - $counts = glsr(CountsManager::class)->getCounts([ |
|
116 | - 'post_ids' => Arr::convertFromString($args['assigned_to']), |
|
117 | - 'term_ids' => $this->normalizeTermIds($args['category']), |
|
118 | - 'type' => $args['type'], |
|
119 | - ]); |
|
120 | - return glsr(CountsManager::class)->flatten($counts, [ |
|
121 | - 'min' => $args['rating'], |
|
122 | - ]); |
|
123 | - } |
|
109 | + /** |
|
110 | + * @return array |
|
111 | + */ |
|
112 | + public function getRatingCounts(array $args = []) |
|
113 | + { |
|
114 | + $args = glsr(SiteReviewsSummaryDefaults::class)->filter($args); |
|
115 | + $counts = glsr(CountsManager::class)->getCounts([ |
|
116 | + 'post_ids' => Arr::convertFromString($args['assigned_to']), |
|
117 | + 'term_ids' => $this->normalizeTermIds($args['category']), |
|
118 | + 'type' => $args['type'], |
|
119 | + ]); |
|
120 | + return glsr(CountsManager::class)->flatten($counts, [ |
|
121 | + 'min' => $args['rating'], |
|
122 | + ]); |
|
123 | + } |
|
124 | 124 | |
125 | - /** |
|
126 | - * @param string $commaSeparatedTermIds |
|
127 | - * @return array |
|
128 | - */ |
|
129 | - public function normalizeTermIds($commaSeparatedTermIds) |
|
130 | - { |
|
131 | - $termIds = glsr_array_column($this->normalizeTerms($commaSeparatedTermIds), 'term_id'); |
|
132 | - return array_unique(array_map('intval', $termIds)); |
|
133 | - } |
|
125 | + /** |
|
126 | + * @param string $commaSeparatedTermIds |
|
127 | + * @return array |
|
128 | + */ |
|
129 | + public function normalizeTermIds($commaSeparatedTermIds) |
|
130 | + { |
|
131 | + $termIds = glsr_array_column($this->normalizeTerms($commaSeparatedTermIds), 'term_id'); |
|
132 | + return array_unique(array_map('intval', $termIds)); |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * @param string $commaSeparatedTermIds |
|
137 | - * @return array |
|
138 | - */ |
|
139 | - public function normalizeTerms($commaSeparatedTermIds) |
|
140 | - { |
|
141 | - $terms = []; |
|
142 | - $termIds = Arr::convertFromString($commaSeparatedTermIds); |
|
143 | - foreach ($termIds as $termId) { |
|
144 | - if (is_numeric($termId)) { |
|
145 | - $termId = intval($termId); |
|
146 | - } |
|
147 | - $term = term_exists($termId, Application::TAXONOMY); |
|
148 | - if (!isset($term['term_id'])) { |
|
149 | - continue; |
|
150 | - } |
|
151 | - $terms[] = $term; |
|
152 | - } |
|
153 | - return $terms; |
|
154 | - } |
|
135 | + /** |
|
136 | + * @param string $commaSeparatedTermIds |
|
137 | + * @return array |
|
138 | + */ |
|
139 | + public function normalizeTerms($commaSeparatedTermIds) |
|
140 | + { |
|
141 | + $terms = []; |
|
142 | + $termIds = Arr::convertFromString($commaSeparatedTermIds); |
|
143 | + foreach ($termIds as $termId) { |
|
144 | + if (is_numeric($termId)) { |
|
145 | + $termId = intval($termId); |
|
146 | + } |
|
147 | + $term = term_exists($termId, Application::TAXONOMY); |
|
148 | + if (!isset($term['term_id'])) { |
|
149 | + continue; |
|
150 | + } |
|
151 | + $terms[] = $term; |
|
152 | + } |
|
153 | + return $terms; |
|
154 | + } |
|
155 | 155 | |
156 | - /** |
|
157 | - * @param int $postId |
|
158 | - * @return void |
|
159 | - */ |
|
160 | - public function revert($postId) |
|
161 | - { |
|
162 | - if (Application::POST_TYPE != get_post_field('post_type', $postId)) { |
|
163 | - return; |
|
164 | - } |
|
165 | - delete_post_meta($postId, '_edit_last'); |
|
166 | - $result = wp_update_post([ |
|
167 | - 'ID' => $postId, |
|
168 | - 'post_content' => glsr(Database::class)->get($postId, 'content'), |
|
169 | - 'post_date' => glsr(Database::class)->get($postId, 'date'), |
|
170 | - 'post_title' => glsr(Database::class)->get($postId, 'title'), |
|
171 | - ]); |
|
172 | - if (is_wp_error($result)) { |
|
173 | - glsr_log()->error($result->get_error_message()); |
|
174 | - return; |
|
175 | - } |
|
176 | - do_action('site-reviews/review/reverted', glsr_get_review($postId)); |
|
177 | - } |
|
156 | + /** |
|
157 | + * @param int $postId |
|
158 | + * @return void |
|
159 | + */ |
|
160 | + public function revert($postId) |
|
161 | + { |
|
162 | + if (Application::POST_TYPE != get_post_field('post_type', $postId)) { |
|
163 | + return; |
|
164 | + } |
|
165 | + delete_post_meta($postId, '_edit_last'); |
|
166 | + $result = wp_update_post([ |
|
167 | + 'ID' => $postId, |
|
168 | + 'post_content' => glsr(Database::class)->get($postId, 'content'), |
|
169 | + 'post_date' => glsr(Database::class)->get($postId, 'date'), |
|
170 | + 'post_title' => glsr(Database::class)->get($postId, 'title'), |
|
171 | + ]); |
|
172 | + if (is_wp_error($result)) { |
|
173 | + glsr_log()->error($result->get_error_message()); |
|
174 | + return; |
|
175 | + } |
|
176 | + do_action('site-reviews/review/reverted', glsr_get_review($postId)); |
|
177 | + } |
|
178 | 178 | |
179 | - /** |
|
180 | - * @return Review |
|
181 | - */ |
|
182 | - public function single(WP_Post $post) |
|
183 | - { |
|
184 | - if (Application::POST_TYPE != $post->post_type) { |
|
185 | - $post = new WP_Post((object) []); |
|
186 | - } |
|
187 | - $review = new Review($post); |
|
188 | - return apply_filters('site-reviews/get/review', $review, $post); |
|
189 | - } |
|
179 | + /** |
|
180 | + * @return Review |
|
181 | + */ |
|
182 | + public function single(WP_Post $post) |
|
183 | + { |
|
184 | + if (Application::POST_TYPE != $post->post_type) { |
|
185 | + $post = new WP_Post((object) []); |
|
186 | + } |
|
187 | + $review = new Review($post); |
|
188 | + return apply_filters('site-reviews/get/review', $review, $post); |
|
189 | + } |
|
190 | 190 | |
191 | - /** |
|
192 | - * @param bool $isBlacklisted |
|
193 | - * @return string |
|
194 | - */ |
|
195 | - protected function getNewPostStatus(array $reviewValues, $isBlacklisted) |
|
196 | - { |
|
197 | - $requireApproval = glsr(OptionManager::class)->getBool('settings.general.require.approval'); |
|
198 | - return 'local' == $reviewValues['_review_type'] && ($requireApproval || $isBlacklisted) |
|
199 | - ? 'pending' |
|
200 | - : 'publish'; |
|
201 | - } |
|
191 | + /** |
|
192 | + * @param bool $isBlacklisted |
|
193 | + * @return string |
|
194 | + */ |
|
195 | + protected function getNewPostStatus(array $reviewValues, $isBlacklisted) |
|
196 | + { |
|
197 | + $requireApproval = glsr(OptionManager::class)->getBool('settings.general.require.approval'); |
|
198 | + return 'local' == $reviewValues['_review_type'] && ($requireApproval || $isBlacklisted) |
|
199 | + ? 'pending' |
|
200 | + : 'publish'; |
|
201 | + } |
|
202 | 202 | |
203 | - /** |
|
204 | - * @param int $postId |
|
205 | - * @param string $termIds |
|
206 | - * @return void |
|
207 | - */ |
|
208 | - protected function setTerms($postId, $termIds) |
|
209 | - { |
|
210 | - $termIds = $this->normalizeTermIds($termIds); |
|
211 | - if (empty($termIds)) { |
|
212 | - return; |
|
213 | - } |
|
214 | - $termTaxonomyIds = wp_set_object_terms($postId, $termIds, Application::TAXONOMY); |
|
215 | - if (is_wp_error($termTaxonomyIds)) { |
|
216 | - glsr_log()->error($termTaxonomyIds->get_error_message()); |
|
217 | - } |
|
218 | - } |
|
203 | + /** |
|
204 | + * @param int $postId |
|
205 | + * @param string $termIds |
|
206 | + * @return void |
|
207 | + */ |
|
208 | + protected function setTerms($postId, $termIds) |
|
209 | + { |
|
210 | + $termIds = $this->normalizeTermIds($termIds); |
|
211 | + if (empty($termIds)) { |
|
212 | + return; |
|
213 | + } |
|
214 | + $termTaxonomyIds = wp_set_object_terms($postId, $termIds, Application::TAXONOMY); |
|
215 | + if (is_wp_error($termTaxonomyIds)) { |
|
216 | + glsr_log()->error($termTaxonomyIds->get_error_message()); |
|
217 | + } |
|
218 | + } |
|
219 | 219 | } |