@@ -9,55 +9,55 @@ |
||
9 | 9 | |
10 | 10 | class Pagination implements PartialContract |
11 | 11 | { |
12 | - /** |
|
13 | - * @var array |
|
14 | - */ |
|
15 | - protected $args; |
|
12 | + /** |
|
13 | + * @var array |
|
14 | + */ |
|
15 | + protected $args; |
|
16 | 16 | |
17 | - /** |
|
18 | - * @return void|string |
|
19 | - */ |
|
20 | - public function build(array $args = []) |
|
21 | - { |
|
22 | - $this->args = $this->normalize($args); |
|
23 | - if ($this->args['total'] < 2) { |
|
24 | - return; |
|
25 | - } |
|
26 | - return glsr(Template::class)->build('templates/pagination', [ |
|
27 | - 'context' => [ |
|
28 | - 'links' => apply_filters('site-reviews/paginate_links', $this->buildLinks(), $this->args), |
|
29 | - 'loader' => '<div class="glsr-loader"></div>', |
|
30 | - 'screen_reader_text' => __('Site Reviews navigation', 'site-reviews'), |
|
31 | - ], |
|
32 | - ]); |
|
33 | - } |
|
17 | + /** |
|
18 | + * @return void|string |
|
19 | + */ |
|
20 | + public function build(array $args = []) |
|
21 | + { |
|
22 | + $this->args = $this->normalize($args); |
|
23 | + if ($this->args['total'] < 2) { |
|
24 | + return; |
|
25 | + } |
|
26 | + return glsr(Template::class)->build('templates/pagination', [ |
|
27 | + 'context' => [ |
|
28 | + 'links' => apply_filters('site-reviews/paginate_links', $this->buildLinks(), $this->args), |
|
29 | + 'loader' => '<div class="glsr-loader"></div>', |
|
30 | + 'screen_reader_text' => __('Site Reviews navigation', 'site-reviews'), |
|
31 | + ], |
|
32 | + ]); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * @return string |
|
37 | - */ |
|
38 | - protected function buildLinks() |
|
39 | - { |
|
40 | - $args = glsr(Style::class)->paginationArgs($this->args); |
|
41 | - if (is_front_page()) { |
|
42 | - unset($args['format']); |
|
43 | - } |
|
44 | - if ('array' == $args['type']) { |
|
45 | - $args['type'] = 'plain'; |
|
46 | - } |
|
47 | - return paginate_links($args); |
|
48 | - } |
|
35 | + /** |
|
36 | + * @return string |
|
37 | + */ |
|
38 | + protected function buildLinks() |
|
39 | + { |
|
40 | + $args = glsr(Style::class)->paginationArgs($this->args); |
|
41 | + if (is_front_page()) { |
|
42 | + unset($args['format']); |
|
43 | + } |
|
44 | + if ('array' == $args['type']) { |
|
45 | + $args['type'] = 'plain'; |
|
46 | + } |
|
47 | + return paginate_links($args); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @return array |
|
52 | - */ |
|
53 | - protected function normalize(array $args) |
|
54 | - { |
|
55 | - if ($baseUrl = glsr_get($args, 'baseUrl')) { |
|
56 | - $args['base'] = $baseUrl.'%_%'; |
|
57 | - } |
|
58 | - return wp_parse_args(array_filter($args), [ |
|
59 | - 'current' => glsr(QueryBuilder::class)->getPaged(), |
|
60 | - 'total' => 1, |
|
61 | - ]); |
|
62 | - } |
|
50 | + /** |
|
51 | + * @return array |
|
52 | + */ |
|
53 | + protected function normalize(array $args) |
|
54 | + { |
|
55 | + if ($baseUrl = glsr_get($args, 'baseUrl')) { |
|
56 | + $args['base'] = $baseUrl.'%_%'; |
|
57 | + } |
|
58 | + return wp_parse_args(array_filter($args), [ |
|
59 | + 'current' => glsr(QueryBuilder::class)->getPaged(), |
|
60 | + 'total' => 1, |
|
61 | + ]); |
|
62 | + } |
|
63 | 63 | } |
@@ -17,19 +17,19 @@ discard block |
||
17 | 17 | /** |
18 | 18 | * @return void|string |
19 | 19 | */ |
20 | - public function build(array $args = []) |
|
20 | + public function build( array $args = [] ) |
|
21 | 21 | { |
22 | - $this->args = $this->normalize($args); |
|
23 | - if ($this->args['total'] < 2) { |
|
22 | + $this->args = $this->normalize( $args ); |
|
23 | + if( $this->args['total'] < 2 ) { |
|
24 | 24 | return; |
25 | 25 | } |
26 | - return glsr(Template::class)->build('templates/pagination', [ |
|
26 | + return glsr( Template::class )->build( 'templates/pagination', [ |
|
27 | 27 | 'context' => [ |
28 | - 'links' => apply_filters('site-reviews/paginate_links', $this->buildLinks(), $this->args), |
|
28 | + 'links' => apply_filters( 'site-reviews/paginate_links', $this->buildLinks(), $this->args ), |
|
29 | 29 | 'loader' => '<div class="glsr-loader"></div>', |
30 | - 'screen_reader_text' => __('Site Reviews navigation', 'site-reviews'), |
|
30 | + 'screen_reader_text' => __( 'Site Reviews navigation', 'site-reviews' ), |
|
31 | 31 | ], |
32 | - ]); |
|
32 | + ] ); |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | /** |
@@ -37,27 +37,27 @@ discard block |
||
37 | 37 | */ |
38 | 38 | protected function buildLinks() |
39 | 39 | { |
40 | - $args = glsr(Style::class)->paginationArgs($this->args); |
|
41 | - if (is_front_page()) { |
|
40 | + $args = glsr( Style::class )->paginationArgs( $this->args ); |
|
41 | + if( is_front_page() ) { |
|
42 | 42 | unset($args['format']); |
43 | 43 | } |
44 | - if ('array' == $args['type']) { |
|
44 | + if( 'array' == $args['type'] ) { |
|
45 | 45 | $args['type'] = 'plain'; |
46 | 46 | } |
47 | - return paginate_links($args); |
|
47 | + return paginate_links( $args ); |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | /** |
51 | 51 | * @return array |
52 | 52 | */ |
53 | - protected function normalize(array $args) |
|
53 | + protected function normalize( array $args ) |
|
54 | 54 | { |
55 | - if ($baseUrl = glsr_get($args, 'baseUrl')) { |
|
55 | + if( $baseUrl = glsr_get( $args, 'baseUrl' ) ) { |
|
56 | 56 | $args['base'] = $baseUrl.'%_%'; |
57 | 57 | } |
58 | - return wp_parse_args(array_filter($args), [ |
|
59 | - 'current' => glsr(QueryBuilder::class)->getPaged(), |
|
58 | + return wp_parse_args( array_filter( $args ), [ |
|
59 | + 'current' => glsr( QueryBuilder::class )->getPaged(), |
|
60 | 60 | 'total' => 1, |
61 | - ]); |
|
61 | + ] ); |
|
62 | 62 | } |
63 | 63 | } |
@@ -7,48 +7,48 @@ |
||
7 | 7 | |
8 | 8 | class ReviewHtml extends ArrayObject |
9 | 9 | { |
10 | - /** |
|
11 | - * @var Review |
|
12 | - */ |
|
13 | - public $review; |
|
10 | + /** |
|
11 | + * @var Review |
|
12 | + */ |
|
13 | + public $review; |
|
14 | 14 | |
15 | - /** |
|
16 | - * @var array |
|
17 | - */ |
|
18 | - public $values; |
|
15 | + /** |
|
16 | + * @var array |
|
17 | + */ |
|
18 | + public $values; |
|
19 | 19 | |
20 | - public function __construct(Review $review, array $values = []) |
|
21 | - { |
|
22 | - $this->review = $review; |
|
23 | - $this->values = $values; |
|
24 | - parent::__construct($values, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); |
|
25 | - } |
|
20 | + public function __construct(Review $review, array $values = []) |
|
21 | + { |
|
22 | + $this->review = $review; |
|
23 | + $this->values = $values; |
|
24 | + parent::__construct($values, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * @return string|void |
|
29 | - */ |
|
30 | - public function __toString() |
|
31 | - { |
|
32 | - if (empty($this->values)) { |
|
33 | - return; |
|
34 | - } |
|
35 | - return glsr(Template::class)->build('templates/review', [ |
|
36 | - 'context' => $this->values, |
|
37 | - 'review' => $this->review, |
|
38 | - ]); |
|
39 | - } |
|
27 | + /** |
|
28 | + * @return string|void |
|
29 | + */ |
|
30 | + public function __toString() |
|
31 | + { |
|
32 | + if (empty($this->values)) { |
|
33 | + return; |
|
34 | + } |
|
35 | + return glsr(Template::class)->build('templates/review', [ |
|
36 | + 'context' => $this->values, |
|
37 | + 'review' => $this->review, |
|
38 | + ]); |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * @param mixed $key |
|
43 | - * @return mixed |
|
44 | - */ |
|
45 | - public function offsetGet($key) |
|
46 | - { |
|
47 | - if (property_exists($this, $key)) { |
|
48 | - return $this->{$key}; |
|
49 | - } |
|
50 | - return array_key_exists($key, $this->values) |
|
51 | - ? $this->values[$key] |
|
52 | - : null; |
|
53 | - } |
|
41 | + /** |
|
42 | + * @param mixed $key |
|
43 | + * @return mixed |
|
44 | + */ |
|
45 | + public function offsetGet($key) |
|
46 | + { |
|
47 | + if (property_exists($this, $key)) { |
|
48 | + return $this->{$key}; |
|
49 | + } |
|
50 | + return array_key_exists($key, $this->values) |
|
51 | + ? $this->values[$key] |
|
52 | + : null; |
|
53 | + } |
|
54 | 54 | } |
@@ -17,11 +17,11 @@ discard block |
||
17 | 17 | */ |
18 | 18 | public $values; |
19 | 19 | |
20 | - public function __construct(Review $review, array $values = []) |
|
20 | + public function __construct( Review $review, array $values = [] ) |
|
21 | 21 | { |
22 | 22 | $this->review = $review; |
23 | 23 | $this->values = $values; |
24 | - parent::__construct($values, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); |
|
24 | + parent::__construct( $values, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS ); |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | /** |
@@ -29,25 +29,25 @@ discard block |
||
29 | 29 | */ |
30 | 30 | public function __toString() |
31 | 31 | { |
32 | - if (empty($this->values)) { |
|
32 | + if( empty($this->values) ) { |
|
33 | 33 | return; |
34 | 34 | } |
35 | - return glsr(Template::class)->build('templates/review', [ |
|
35 | + return glsr( Template::class )->build( 'templates/review', [ |
|
36 | 36 | 'context' => $this->values, |
37 | 37 | 'review' => $this->review, |
38 | - ]); |
|
38 | + ] ); |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | /** |
42 | 42 | * @param mixed $key |
43 | 43 | * @return mixed |
44 | 44 | */ |
45 | - public function offsetGet($key) |
|
45 | + public function offsetGet( $key ) |
|
46 | 46 | { |
47 | - if (property_exists($this, $key)) { |
|
47 | + if( property_exists( $this, $key ) ) { |
|
48 | 48 | return $this->{$key}; |
49 | 49 | } |
50 | - return array_key_exists($key, $this->values) |
|
50 | + return array_key_exists( $key, $this->values ) |
|
51 | 51 | ? $this->values[$key] |
52 | 52 | : null; |
53 | 53 | } |
@@ -8,237 +8,237 @@ |
||
8 | 8 | |
9 | 9 | class Field |
10 | 10 | { |
11 | - /** |
|
12 | - * @var array |
|
13 | - */ |
|
14 | - public $field; |
|
15 | - |
|
16 | - public function __construct(array $field = []) |
|
17 | - { |
|
18 | - $this->field = wp_parse_args($field, [ |
|
19 | - 'errors' => false, |
|
20 | - 'is_hidden' => false, |
|
21 | - 'is_multi' => false, |
|
22 | - 'is_public' => false, |
|
23 | - 'is_raw' => false, |
|
24 | - 'is_setting' => false, |
|
25 | - 'is_valid' => true, |
|
26 | - 'is_widget' => false, |
|
27 | - 'path' => '', |
|
28 | - ]); |
|
29 | - $this->normalize(); |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * @return string |
|
34 | - */ |
|
35 | - public function __toString() |
|
36 | - { |
|
37 | - return (string) $this->build(); |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * @return void|string |
|
42 | - */ |
|
43 | - public function build() |
|
44 | - { |
|
45 | - if (!$this->field['is_valid']) { |
|
46 | - return; |
|
47 | - } |
|
48 | - if ($this->field['is_raw']) { |
|
49 | - return glsr(Builder::class)->{$this->field['type']}($this->field); |
|
50 | - } |
|
51 | - if (!$this->field['is_setting']) { |
|
52 | - return $this->buildField(); |
|
53 | - } |
|
54 | - if (!$this->field['is_multi']) { |
|
55 | - return $this->buildSettingField(); |
|
56 | - } |
|
57 | - return $this->buildSettingMultiField(); |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * @return void |
|
62 | - */ |
|
63 | - public function render() |
|
64 | - { |
|
65 | - echo $this->build(); |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * @return string |
|
70 | - */ |
|
71 | - protected function buildField() |
|
72 | - { |
|
73 | - $field = glsr(Template::class)->build('templates/form/field_'.$this->field['type'], [ |
|
74 | - 'context' => [ |
|
75 | - 'class' => $this->getFieldClass(), |
|
76 | - 'errors' => $this->getFieldErrors(), |
|
77 | - 'field' => glsr(Builder::class)->raw($this->field), |
|
78 | - 'label' => glsr(Builder::class)->label([ |
|
79 | - 'class' => 'glsr-'.$this->field['type'].'-label', |
|
80 | - 'for' => $this->field['id'], |
|
81 | - 'is_public' => $this->field['is_public'], |
|
82 | - 'text' => $this->field['label'].'<span></span>', |
|
83 | - 'type' => $this->field['type'], |
|
84 | - ]), |
|
85 | - ], |
|
86 | - ]); |
|
87 | - return apply_filters('site-reviews/rendered/field', $field, $this->field['type'], $this->field); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @return string |
|
92 | - */ |
|
93 | - protected function buildSettingField() |
|
94 | - { |
|
95 | - return glsr(Template::class)->build('partials/form/table-row', [ |
|
96 | - 'context' => [ |
|
97 | - 'class' => $this->getFieldClass(), |
|
98 | - 'field' => glsr(Builder::class)->{$this->field['type']}($this->field), |
|
99 | - 'label' => glsr(Builder::class)->label($this->field['legend'], ['for' => $this->field['id']]), |
|
100 | - ], |
|
101 | - ]); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * @return string |
|
106 | - */ |
|
107 | - protected function buildSettingMultiField() |
|
108 | - { |
|
109 | - $dependsOn = $this->getFieldDependsOn(); |
|
110 | - unset($this->field['data-depends']); |
|
111 | - return glsr(Template::class)->build('partials/form/table-row-multiple', [ |
|
112 | - 'context' => [ |
|
113 | - 'class' => $this->getFieldClass(), |
|
114 | - 'depends_on' => $dependsOn, |
|
115 | - 'field' => glsr(Builder::class)->{$this->field['type']}($this->field), |
|
116 | - 'label' => glsr(Builder::class)->label($this->field['legend'], ['for' => $this->field['id']]), |
|
117 | - 'legend' => $this->field['legend'], |
|
118 | - ], |
|
119 | - ]); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * @return string |
|
124 | - */ |
|
125 | - protected function getFieldClass() |
|
126 | - { |
|
127 | - $classes = []; |
|
128 | - if (!empty($this->field['errors'])) { |
|
129 | - $classes[] = 'glsr-has-error'; |
|
130 | - } |
|
131 | - if ($this->field['is_hidden']) { |
|
132 | - $classes[] = 'hidden'; |
|
133 | - } |
|
134 | - if (!empty($this->field['required'])) { |
|
135 | - $classes[] = 'glsr-required'; |
|
136 | - } |
|
137 | - $classes = apply_filters('site-reviews/rendered/field/classes', $classes, $this->field); |
|
138 | - return implode(' ', $classes); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @return string |
|
143 | - */ |
|
144 | - protected function getFieldDependsOn() |
|
145 | - { |
|
146 | - return !empty($this->field['data-depends']) |
|
147 | - ? $this->field['data-depends'] |
|
148 | - : ''; |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * @return void|string |
|
153 | - */ |
|
154 | - protected function getFieldErrors() |
|
155 | - { |
|
156 | - if (empty($this->field['errors']) || !is_array($this->field['errors'])) { |
|
157 | - return; |
|
158 | - } |
|
159 | - $errors = array_reduce($this->field['errors'], function ($carry, $error) { |
|
160 | - return $carry.glsr(Builder::class)->span($error, ['class' => 'glsr-field-error']); |
|
161 | - }); |
|
162 | - return glsr(Template::class)->build('templates/form/field-errors', [ |
|
163 | - 'context' => [ |
|
164 | - 'errors' => $errors, |
|
165 | - ], |
|
166 | - ]); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * @return string |
|
171 | - */ |
|
172 | - protected function getFieldPrefix() |
|
173 | - { |
|
174 | - return $this->field['is_setting'] |
|
175 | - ? OptionManager::databaseKey() |
|
176 | - : Application::ID; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * @return bool |
|
181 | - */ |
|
182 | - protected function isFieldValid() |
|
183 | - { |
|
184 | - $missingValues = []; |
|
185 | - $requiredValues = [ |
|
186 | - 'name', 'type', |
|
187 | - ]; |
|
188 | - foreach ($requiredValues as $value) { |
|
189 | - if (isset($this->field[$value])) { |
|
190 | - continue; |
|
191 | - } |
|
192 | - $missingValues[] = $value; |
|
193 | - $this->field['is_valid'] = false; |
|
194 | - } |
|
195 | - if (!empty($missingValues)) { |
|
196 | - glsr_log() |
|
197 | - ->warning('Field is missing: '.implode(', ', $missingValues)) |
|
198 | - ->debug($this->field); |
|
199 | - } |
|
200 | - return $this->field['is_valid']; |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * @return void |
|
205 | - */ |
|
206 | - protected function normalize() |
|
207 | - { |
|
208 | - if (!$this->isFieldValid()) { |
|
209 | - return; |
|
210 | - } |
|
211 | - $this->field['path'] = $this->field['name']; |
|
212 | - $className = glsr(Helper::class)->buildClassName($this->field['type'], __NAMESPACE__.'\Fields'); |
|
213 | - if (class_exists($className)) { |
|
214 | - $this->field = $className::merge($this->field); |
|
215 | - } |
|
216 | - $this->normalizeFieldId(); |
|
217 | - $this->normalizeFieldName(); |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * @return void |
|
222 | - */ |
|
223 | - protected function normalizeFieldId() |
|
224 | - { |
|
225 | - if (isset($this->field['id']) || $this->field['is_raw']) { |
|
226 | - return; |
|
227 | - } |
|
228 | - $this->field['id'] = glsr(Helper::class)->convertPathToId( |
|
229 | - $this->field['path'], |
|
230 | - $this->getFieldPrefix() |
|
231 | - ); |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * @return void |
|
236 | - */ |
|
237 | - protected function normalizeFieldName() |
|
238 | - { |
|
239 | - $this->field['name'] = glsr(Helper::class)->convertPathToName( |
|
240 | - $this->field['path'], |
|
241 | - $this->getFieldPrefix() |
|
242 | - ); |
|
243 | - } |
|
11 | + /** |
|
12 | + * @var array |
|
13 | + */ |
|
14 | + public $field; |
|
15 | + |
|
16 | + public function __construct(array $field = []) |
|
17 | + { |
|
18 | + $this->field = wp_parse_args($field, [ |
|
19 | + 'errors' => false, |
|
20 | + 'is_hidden' => false, |
|
21 | + 'is_multi' => false, |
|
22 | + 'is_public' => false, |
|
23 | + 'is_raw' => false, |
|
24 | + 'is_setting' => false, |
|
25 | + 'is_valid' => true, |
|
26 | + 'is_widget' => false, |
|
27 | + 'path' => '', |
|
28 | + ]); |
|
29 | + $this->normalize(); |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * @return string |
|
34 | + */ |
|
35 | + public function __toString() |
|
36 | + { |
|
37 | + return (string) $this->build(); |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * @return void|string |
|
42 | + */ |
|
43 | + public function build() |
|
44 | + { |
|
45 | + if (!$this->field['is_valid']) { |
|
46 | + return; |
|
47 | + } |
|
48 | + if ($this->field['is_raw']) { |
|
49 | + return glsr(Builder::class)->{$this->field['type']}($this->field); |
|
50 | + } |
|
51 | + if (!$this->field['is_setting']) { |
|
52 | + return $this->buildField(); |
|
53 | + } |
|
54 | + if (!$this->field['is_multi']) { |
|
55 | + return $this->buildSettingField(); |
|
56 | + } |
|
57 | + return $this->buildSettingMultiField(); |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * @return void |
|
62 | + */ |
|
63 | + public function render() |
|
64 | + { |
|
65 | + echo $this->build(); |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * @return string |
|
70 | + */ |
|
71 | + protected function buildField() |
|
72 | + { |
|
73 | + $field = glsr(Template::class)->build('templates/form/field_'.$this->field['type'], [ |
|
74 | + 'context' => [ |
|
75 | + 'class' => $this->getFieldClass(), |
|
76 | + 'errors' => $this->getFieldErrors(), |
|
77 | + 'field' => glsr(Builder::class)->raw($this->field), |
|
78 | + 'label' => glsr(Builder::class)->label([ |
|
79 | + 'class' => 'glsr-'.$this->field['type'].'-label', |
|
80 | + 'for' => $this->field['id'], |
|
81 | + 'is_public' => $this->field['is_public'], |
|
82 | + 'text' => $this->field['label'].'<span></span>', |
|
83 | + 'type' => $this->field['type'], |
|
84 | + ]), |
|
85 | + ], |
|
86 | + ]); |
|
87 | + return apply_filters('site-reviews/rendered/field', $field, $this->field['type'], $this->field); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @return string |
|
92 | + */ |
|
93 | + protected function buildSettingField() |
|
94 | + { |
|
95 | + return glsr(Template::class)->build('partials/form/table-row', [ |
|
96 | + 'context' => [ |
|
97 | + 'class' => $this->getFieldClass(), |
|
98 | + 'field' => glsr(Builder::class)->{$this->field['type']}($this->field), |
|
99 | + 'label' => glsr(Builder::class)->label($this->field['legend'], ['for' => $this->field['id']]), |
|
100 | + ], |
|
101 | + ]); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * @return string |
|
106 | + */ |
|
107 | + protected function buildSettingMultiField() |
|
108 | + { |
|
109 | + $dependsOn = $this->getFieldDependsOn(); |
|
110 | + unset($this->field['data-depends']); |
|
111 | + return glsr(Template::class)->build('partials/form/table-row-multiple', [ |
|
112 | + 'context' => [ |
|
113 | + 'class' => $this->getFieldClass(), |
|
114 | + 'depends_on' => $dependsOn, |
|
115 | + 'field' => glsr(Builder::class)->{$this->field['type']}($this->field), |
|
116 | + 'label' => glsr(Builder::class)->label($this->field['legend'], ['for' => $this->field['id']]), |
|
117 | + 'legend' => $this->field['legend'], |
|
118 | + ], |
|
119 | + ]); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * @return string |
|
124 | + */ |
|
125 | + protected function getFieldClass() |
|
126 | + { |
|
127 | + $classes = []; |
|
128 | + if (!empty($this->field['errors'])) { |
|
129 | + $classes[] = 'glsr-has-error'; |
|
130 | + } |
|
131 | + if ($this->field['is_hidden']) { |
|
132 | + $classes[] = 'hidden'; |
|
133 | + } |
|
134 | + if (!empty($this->field['required'])) { |
|
135 | + $classes[] = 'glsr-required'; |
|
136 | + } |
|
137 | + $classes = apply_filters('site-reviews/rendered/field/classes', $classes, $this->field); |
|
138 | + return implode(' ', $classes); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @return string |
|
143 | + */ |
|
144 | + protected function getFieldDependsOn() |
|
145 | + { |
|
146 | + return !empty($this->field['data-depends']) |
|
147 | + ? $this->field['data-depends'] |
|
148 | + : ''; |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * @return void|string |
|
153 | + */ |
|
154 | + protected function getFieldErrors() |
|
155 | + { |
|
156 | + if (empty($this->field['errors']) || !is_array($this->field['errors'])) { |
|
157 | + return; |
|
158 | + } |
|
159 | + $errors = array_reduce($this->field['errors'], function ($carry, $error) { |
|
160 | + return $carry.glsr(Builder::class)->span($error, ['class' => 'glsr-field-error']); |
|
161 | + }); |
|
162 | + return glsr(Template::class)->build('templates/form/field-errors', [ |
|
163 | + 'context' => [ |
|
164 | + 'errors' => $errors, |
|
165 | + ], |
|
166 | + ]); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * @return string |
|
171 | + */ |
|
172 | + protected function getFieldPrefix() |
|
173 | + { |
|
174 | + return $this->field['is_setting'] |
|
175 | + ? OptionManager::databaseKey() |
|
176 | + : Application::ID; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * @return bool |
|
181 | + */ |
|
182 | + protected function isFieldValid() |
|
183 | + { |
|
184 | + $missingValues = []; |
|
185 | + $requiredValues = [ |
|
186 | + 'name', 'type', |
|
187 | + ]; |
|
188 | + foreach ($requiredValues as $value) { |
|
189 | + if (isset($this->field[$value])) { |
|
190 | + continue; |
|
191 | + } |
|
192 | + $missingValues[] = $value; |
|
193 | + $this->field['is_valid'] = false; |
|
194 | + } |
|
195 | + if (!empty($missingValues)) { |
|
196 | + glsr_log() |
|
197 | + ->warning('Field is missing: '.implode(', ', $missingValues)) |
|
198 | + ->debug($this->field); |
|
199 | + } |
|
200 | + return $this->field['is_valid']; |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * @return void |
|
205 | + */ |
|
206 | + protected function normalize() |
|
207 | + { |
|
208 | + if (!$this->isFieldValid()) { |
|
209 | + return; |
|
210 | + } |
|
211 | + $this->field['path'] = $this->field['name']; |
|
212 | + $className = glsr(Helper::class)->buildClassName($this->field['type'], __NAMESPACE__.'\Fields'); |
|
213 | + if (class_exists($className)) { |
|
214 | + $this->field = $className::merge($this->field); |
|
215 | + } |
|
216 | + $this->normalizeFieldId(); |
|
217 | + $this->normalizeFieldName(); |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * @return void |
|
222 | + */ |
|
223 | + protected function normalizeFieldId() |
|
224 | + { |
|
225 | + if (isset($this->field['id']) || $this->field['is_raw']) { |
|
226 | + return; |
|
227 | + } |
|
228 | + $this->field['id'] = glsr(Helper::class)->convertPathToId( |
|
229 | + $this->field['path'], |
|
230 | + $this->getFieldPrefix() |
|
231 | + ); |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * @return void |
|
236 | + */ |
|
237 | + protected function normalizeFieldName() |
|
238 | + { |
|
239 | + $this->field['name'] = glsr(Helper::class)->convertPathToName( |
|
240 | + $this->field['path'], |
|
241 | + $this->getFieldPrefix() |
|
242 | + ); |
|
243 | + } |
|
244 | 244 | } |
@@ -13,9 +13,9 @@ discard block |
||
13 | 13 | */ |
14 | 14 | public $field; |
15 | 15 | |
16 | - public function __construct(array $field = []) |
|
16 | + public function __construct( array $field = [] ) |
|
17 | 17 | { |
18 | - $this->field = wp_parse_args($field, [ |
|
18 | + $this->field = wp_parse_args( $field, [ |
|
19 | 19 | 'errors' => false, |
20 | 20 | 'is_hidden' => false, |
21 | 21 | 'is_multi' => false, |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | 'is_valid' => true, |
26 | 26 | 'is_widget' => false, |
27 | 27 | 'path' => '', |
28 | - ]); |
|
28 | + ] ); |
|
29 | 29 | $this->normalize(); |
30 | 30 | } |
31 | 31 | |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | */ |
35 | 35 | public function __toString() |
36 | 36 | { |
37 | - return (string) $this->build(); |
|
37 | + return (string)$this->build(); |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | /** |
@@ -42,16 +42,16 @@ discard block |
||
42 | 42 | */ |
43 | 43 | public function build() |
44 | 44 | { |
45 | - if (!$this->field['is_valid']) { |
|
45 | + if( !$this->field['is_valid'] ) { |
|
46 | 46 | return; |
47 | 47 | } |
48 | - if ($this->field['is_raw']) { |
|
49 | - return glsr(Builder::class)->{$this->field['type']}($this->field); |
|
48 | + if( $this->field['is_raw'] ) { |
|
49 | + return glsr( Builder::class )->{$this->field['type']}($this->field); |
|
50 | 50 | } |
51 | - if (!$this->field['is_setting']) { |
|
51 | + if( !$this->field['is_setting'] ) { |
|
52 | 52 | return $this->buildField(); |
53 | 53 | } |
54 | - if (!$this->field['is_multi']) { |
|
54 | + if( !$this->field['is_multi'] ) { |
|
55 | 55 | return $this->buildSettingField(); |
56 | 56 | } |
57 | 57 | return $this->buildSettingMultiField(); |
@@ -70,21 +70,21 @@ discard block |
||
70 | 70 | */ |
71 | 71 | protected function buildField() |
72 | 72 | { |
73 | - $field = glsr(Template::class)->build('templates/form/field_'.$this->field['type'], [ |
|
73 | + $field = glsr( Template::class )->build( 'templates/form/field_'.$this->field['type'], [ |
|
74 | 74 | 'context' => [ |
75 | 75 | 'class' => $this->getFieldClass(), |
76 | 76 | 'errors' => $this->getFieldErrors(), |
77 | - 'field' => glsr(Builder::class)->raw($this->field), |
|
78 | - 'label' => glsr(Builder::class)->label([ |
|
77 | + 'field' => glsr( Builder::class )->raw( $this->field ), |
|
78 | + 'label' => glsr( Builder::class )->label( [ |
|
79 | 79 | 'class' => 'glsr-'.$this->field['type'].'-label', |
80 | 80 | 'for' => $this->field['id'], |
81 | 81 | 'is_public' => $this->field['is_public'], |
82 | 82 | 'text' => $this->field['label'].'<span></span>', |
83 | 83 | 'type' => $this->field['type'], |
84 | - ]), |
|
84 | + ] ), |
|
85 | 85 | ], |
86 | - ]); |
|
87 | - return apply_filters('site-reviews/rendered/field', $field, $this->field['type'], $this->field); |
|
86 | + ] ); |
|
87 | + return apply_filters( 'site-reviews/rendered/field', $field, $this->field['type'], $this->field ); |
|
88 | 88 | } |
89 | 89 | |
90 | 90 | /** |
@@ -92,13 +92,13 @@ discard block |
||
92 | 92 | */ |
93 | 93 | protected function buildSettingField() |
94 | 94 | { |
95 | - return glsr(Template::class)->build('partials/form/table-row', [ |
|
95 | + return glsr( Template::class )->build( 'partials/form/table-row', [ |
|
96 | 96 | 'context' => [ |
97 | 97 | 'class' => $this->getFieldClass(), |
98 | - 'field' => glsr(Builder::class)->{$this->field['type']}($this->field), |
|
99 | - 'label' => glsr(Builder::class)->label($this->field['legend'], ['for' => $this->field['id']]), |
|
98 | + 'field' => glsr( Builder::class )->{$this->field['type']}($this->field), |
|
99 | + 'label' => glsr( Builder::class )->label( $this->field['legend'], ['for' => $this->field['id']] ), |
|
100 | 100 | ], |
101 | - ]); |
|
101 | + ] ); |
|
102 | 102 | } |
103 | 103 | |
104 | 104 | /** |
@@ -108,15 +108,15 @@ discard block |
||
108 | 108 | { |
109 | 109 | $dependsOn = $this->getFieldDependsOn(); |
110 | 110 | unset($this->field['data-depends']); |
111 | - return glsr(Template::class)->build('partials/form/table-row-multiple', [ |
|
111 | + return glsr( Template::class )->build( 'partials/form/table-row-multiple', [ |
|
112 | 112 | 'context' => [ |
113 | 113 | 'class' => $this->getFieldClass(), |
114 | 114 | 'depends_on' => $dependsOn, |
115 | - 'field' => glsr(Builder::class)->{$this->field['type']}($this->field), |
|
116 | - 'label' => glsr(Builder::class)->label($this->field['legend'], ['for' => $this->field['id']]), |
|
115 | + 'field' => glsr( Builder::class )->{$this->field['type']}($this->field), |
|
116 | + 'label' => glsr( Builder::class )->label( $this->field['legend'], ['for' => $this->field['id']] ), |
|
117 | 117 | 'legend' => $this->field['legend'], |
118 | 118 | ], |
119 | - ]); |
|
119 | + ] ); |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | /** |
@@ -125,17 +125,17 @@ discard block |
||
125 | 125 | protected function getFieldClass() |
126 | 126 | { |
127 | 127 | $classes = []; |
128 | - if (!empty($this->field['errors'])) { |
|
128 | + if( !empty($this->field['errors']) ) { |
|
129 | 129 | $classes[] = 'glsr-has-error'; |
130 | 130 | } |
131 | - if ($this->field['is_hidden']) { |
|
131 | + if( $this->field['is_hidden'] ) { |
|
132 | 132 | $classes[] = 'hidden'; |
133 | 133 | } |
134 | - if (!empty($this->field['required'])) { |
|
134 | + if( !empty($this->field['required']) ) { |
|
135 | 135 | $classes[] = 'glsr-required'; |
136 | 136 | } |
137 | - $classes = apply_filters('site-reviews/rendered/field/classes', $classes, $this->field); |
|
138 | - return implode(' ', $classes); |
|
137 | + $classes = apply_filters( 'site-reviews/rendered/field/classes', $classes, $this->field ); |
|
138 | + return implode( ' ', $classes ); |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | /** |
@@ -153,17 +153,17 @@ discard block |
||
153 | 153 | */ |
154 | 154 | protected function getFieldErrors() |
155 | 155 | { |
156 | - if (empty($this->field['errors']) || !is_array($this->field['errors'])) { |
|
156 | + if( empty($this->field['errors']) || !is_array( $this->field['errors'] ) ) { |
|
157 | 157 | return; |
158 | 158 | } |
159 | - $errors = array_reduce($this->field['errors'], function ($carry, $error) { |
|
160 | - return $carry.glsr(Builder::class)->span($error, ['class' => 'glsr-field-error']); |
|
159 | + $errors = array_reduce( $this->field['errors'], function( $carry, $error ) { |
|
160 | + return $carry.glsr( Builder::class )->span( $error, ['class' => 'glsr-field-error'] ); |
|
161 | 161 | }); |
162 | - return glsr(Template::class)->build('templates/form/field-errors', [ |
|
162 | + return glsr( Template::class )->build( 'templates/form/field-errors', [ |
|
163 | 163 | 'context' => [ |
164 | 164 | 'errors' => $errors, |
165 | 165 | ], |
166 | - ]); |
|
166 | + ] ); |
|
167 | 167 | } |
168 | 168 | |
169 | 169 | /** |
@@ -185,17 +185,17 @@ discard block |
||
185 | 185 | $requiredValues = [ |
186 | 186 | 'name', 'type', |
187 | 187 | ]; |
188 | - foreach ($requiredValues as $value) { |
|
189 | - if (isset($this->field[$value])) { |
|
188 | + foreach( $requiredValues as $value ) { |
|
189 | + if( isset($this->field[$value]) ) { |
|
190 | 190 | continue; |
191 | 191 | } |
192 | 192 | $missingValues[] = $value; |
193 | 193 | $this->field['is_valid'] = false; |
194 | 194 | } |
195 | - if (!empty($missingValues)) { |
|
195 | + if( !empty($missingValues) ) { |
|
196 | 196 | glsr_log() |
197 | - ->warning('Field is missing: '.implode(', ', $missingValues)) |
|
198 | - ->debug($this->field); |
|
197 | + ->warning( 'Field is missing: '.implode( ', ', $missingValues ) ) |
|
198 | + ->debug( $this->field ); |
|
199 | 199 | } |
200 | 200 | return $this->field['is_valid']; |
201 | 201 | } |
@@ -205,13 +205,13 @@ discard block |
||
205 | 205 | */ |
206 | 206 | protected function normalize() |
207 | 207 | { |
208 | - if (!$this->isFieldValid()) { |
|
208 | + if( !$this->isFieldValid() ) { |
|
209 | 209 | return; |
210 | 210 | } |
211 | 211 | $this->field['path'] = $this->field['name']; |
212 | - $className = glsr(Helper::class)->buildClassName($this->field['type'], __NAMESPACE__.'\Fields'); |
|
213 | - if (class_exists($className)) { |
|
214 | - $this->field = $className::merge($this->field); |
|
212 | + $className = glsr( Helper::class )->buildClassName( $this->field['type'], __NAMESPACE__.'\Fields' ); |
|
213 | + if( class_exists( $className ) ) { |
|
214 | + $this->field = $className::merge( $this->field ); |
|
215 | 215 | } |
216 | 216 | $this->normalizeFieldId(); |
217 | 217 | $this->normalizeFieldName(); |
@@ -222,10 +222,10 @@ discard block |
||
222 | 222 | */ |
223 | 223 | protected function normalizeFieldId() |
224 | 224 | { |
225 | - if (isset($this->field['id']) || $this->field['is_raw']) { |
|
225 | + if( isset($this->field['id']) || $this->field['is_raw'] ) { |
|
226 | 226 | return; |
227 | 227 | } |
228 | - $this->field['id'] = glsr(Helper::class)->convertPathToId( |
|
228 | + $this->field['id'] = glsr( Helper::class )->convertPathToId( |
|
229 | 229 | $this->field['path'], |
230 | 230 | $this->getFieldPrefix() |
231 | 231 | ); |
@@ -236,7 +236,7 @@ discard block |
||
236 | 236 | */ |
237 | 237 | protected function normalizeFieldName() |
238 | 238 | { |
239 | - $this->field['name'] = glsr(Helper::class)->convertPathToName( |
|
239 | + $this->field['name'] = glsr( Helper::class )->convertPathToName( |
|
240 | 240 | $this->field['path'], |
241 | 241 | $this->getFieldPrefix() |
242 | 242 | ); |
@@ -6,78 +6,78 @@ |
||
6 | 6 | |
7 | 7 | class Template |
8 | 8 | { |
9 | - /** |
|
10 | - * @param string $templatePath |
|
11 | - * @return void|string |
|
12 | - */ |
|
13 | - public function build($templatePath, array $data = []) |
|
14 | - { |
|
15 | - $data = $this->normalize($data); |
|
16 | - ob_start(); |
|
17 | - glsr()->render($templatePath, $data); |
|
18 | - $template = ob_get_clean(); |
|
19 | - $path = glsr(Helper::class)->removePrefix('templates/', $templatePath); |
|
20 | - $template = apply_filters('site-reviews/build/template/'.$path, $template, $data); |
|
21 | - $template = $this->interpolate($template, $data['context'], $path); |
|
22 | - $template = apply_filters('site-reviews/rendered/template', $template, $templatePath, $data); |
|
23 | - $template = apply_filters('site-reviews/rendered/template/'.$path, $template, $data); |
|
24 | - return $template; |
|
25 | - } |
|
9 | + /** |
|
10 | + * @param string $templatePath |
|
11 | + * @return void|string |
|
12 | + */ |
|
13 | + public function build($templatePath, array $data = []) |
|
14 | + { |
|
15 | + $data = $this->normalize($data); |
|
16 | + ob_start(); |
|
17 | + glsr()->render($templatePath, $data); |
|
18 | + $template = ob_get_clean(); |
|
19 | + $path = glsr(Helper::class)->removePrefix('templates/', $templatePath); |
|
20 | + $template = apply_filters('site-reviews/build/template/'.$path, $template, $data); |
|
21 | + $template = $this->interpolate($template, $data['context'], $path); |
|
22 | + $template = apply_filters('site-reviews/rendered/template', $template, $templatePath, $data); |
|
23 | + $template = apply_filters('site-reviews/rendered/template/'.$path, $template, $data); |
|
24 | + return $template; |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * Interpolate context values into template placeholders. |
|
29 | - * @param string $template |
|
30 | - * @param string $templatePath |
|
31 | - * @return string |
|
32 | - */ |
|
33 | - public function interpolate($template, array $context = [], $templatePath) |
|
34 | - { |
|
35 | - $context = $this->normalizeContext($context); |
|
36 | - $context = apply_filters('site-reviews/interpolate/'.$templatePath, $context, $template); |
|
37 | - foreach ($context as $key => $value) { |
|
38 | - $template = strtr( |
|
39 | - $template, |
|
40 | - array_fill_keys(['{'.$key.'}', '{{ '.$key.' }}'], $value) |
|
41 | - ); |
|
42 | - } |
|
43 | - return trim($template); |
|
44 | - } |
|
27 | + /** |
|
28 | + * Interpolate context values into template placeholders. |
|
29 | + * @param string $template |
|
30 | + * @param string $templatePath |
|
31 | + * @return string |
|
32 | + */ |
|
33 | + public function interpolate($template, array $context = [], $templatePath) |
|
34 | + { |
|
35 | + $context = $this->normalizeContext($context); |
|
36 | + $context = apply_filters('site-reviews/interpolate/'.$templatePath, $context, $template); |
|
37 | + foreach ($context as $key => $value) { |
|
38 | + $template = strtr( |
|
39 | + $template, |
|
40 | + array_fill_keys(['{'.$key.'}', '{{ '.$key.' }}'], $value) |
|
41 | + ); |
|
42 | + } |
|
43 | + return trim($template); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * @param string $templatePath |
|
48 | - * @return void|string |
|
49 | - */ |
|
50 | - public function render($templatePath, array $data = []) |
|
51 | - { |
|
52 | - echo $this->build($templatePath, $data); |
|
53 | - } |
|
46 | + /** |
|
47 | + * @param string $templatePath |
|
48 | + * @return void|string |
|
49 | + */ |
|
50 | + public function render($templatePath, array $data = []) |
|
51 | + { |
|
52 | + echo $this->build($templatePath, $data); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @return array |
|
57 | - */ |
|
58 | - protected function normalize(array $data) |
|
59 | - { |
|
60 | - $arrayKeys = ['context', 'globals']; |
|
61 | - $data = wp_parse_args($data, array_fill_keys($arrayKeys, [])); |
|
62 | - foreach ($arrayKeys as $key) { |
|
63 | - if (is_array($data[$key])) { |
|
64 | - continue; |
|
65 | - } |
|
66 | - $data[$key] = []; |
|
67 | - } |
|
68 | - return $data; |
|
69 | - } |
|
55 | + /** |
|
56 | + * @return array |
|
57 | + */ |
|
58 | + protected function normalize(array $data) |
|
59 | + { |
|
60 | + $arrayKeys = ['context', 'globals']; |
|
61 | + $data = wp_parse_args($data, array_fill_keys($arrayKeys, [])); |
|
62 | + foreach ($arrayKeys as $key) { |
|
63 | + if (is_array($data[$key])) { |
|
64 | + continue; |
|
65 | + } |
|
66 | + $data[$key] = []; |
|
67 | + } |
|
68 | + return $data; |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * @return array |
|
73 | - */ |
|
74 | - protected function normalizeContext(array $context) |
|
75 | - { |
|
76 | - $context = array_filter($context, function ($value) { |
|
77 | - return !is_array($value) && !is_object($value); |
|
78 | - }); |
|
79 | - return array_map(function ($value) { |
|
80 | - return (string) $value; |
|
81 | - }, $context); |
|
82 | - } |
|
71 | + /** |
|
72 | + * @return array |
|
73 | + */ |
|
74 | + protected function normalizeContext(array $context) |
|
75 | + { |
|
76 | + $context = array_filter($context, function ($value) { |
|
77 | + return !is_array($value) && !is_object($value); |
|
78 | + }); |
|
79 | + return array_map(function ($value) { |
|
80 | + return (string) $value; |
|
81 | + }, $context); |
|
82 | + } |
|
83 | 83 | } |
@@ -10,17 +10,17 @@ discard block |
||
10 | 10 | * @param string $templatePath |
11 | 11 | * @return void|string |
12 | 12 | */ |
13 | - public function build($templatePath, array $data = []) |
|
13 | + public function build( $templatePath, array $data = [] ) |
|
14 | 14 | { |
15 | - $data = $this->normalize($data); |
|
15 | + $data = $this->normalize( $data ); |
|
16 | 16 | ob_start(); |
17 | - glsr()->render($templatePath, $data); |
|
17 | + glsr()->render( $templatePath, $data ); |
|
18 | 18 | $template = ob_get_clean(); |
19 | - $path = glsr(Helper::class)->removePrefix('templates/', $templatePath); |
|
20 | - $template = apply_filters('site-reviews/build/template/'.$path, $template, $data); |
|
21 | - $template = $this->interpolate($template, $data['context'], $path); |
|
22 | - $template = apply_filters('site-reviews/rendered/template', $template, $templatePath, $data); |
|
23 | - $template = apply_filters('site-reviews/rendered/template/'.$path, $template, $data); |
|
19 | + $path = glsr( Helper::class )->removePrefix( 'templates/', $templatePath ); |
|
20 | + $template = apply_filters( 'site-reviews/build/template/'.$path, $template, $data ); |
|
21 | + $template = $this->interpolate( $template, $data['context'], $path ); |
|
22 | + $template = apply_filters( 'site-reviews/rendered/template', $template, $templatePath, $data ); |
|
23 | + $template = apply_filters( 'site-reviews/rendered/template/'.$path, $template, $data ); |
|
24 | 24 | return $template; |
25 | 25 | } |
26 | 26 | |
@@ -30,37 +30,37 @@ discard block |
||
30 | 30 | * @param string $templatePath |
31 | 31 | * @return string |
32 | 32 | */ |
33 | - public function interpolate($template, array $context = [], $templatePath) |
|
33 | + public function interpolate( $template, array $context = [], $templatePath ) |
|
34 | 34 | { |
35 | - $context = $this->normalizeContext($context); |
|
36 | - $context = apply_filters('site-reviews/interpolate/'.$templatePath, $context, $template); |
|
37 | - foreach ($context as $key => $value) { |
|
35 | + $context = $this->normalizeContext( $context ); |
|
36 | + $context = apply_filters( 'site-reviews/interpolate/'.$templatePath, $context, $template ); |
|
37 | + foreach( $context as $key => $value ) { |
|
38 | 38 | $template = strtr( |
39 | 39 | $template, |
40 | - array_fill_keys(['{'.$key.'}', '{{ '.$key.' }}'], $value) |
|
40 | + array_fill_keys( ['{'.$key.'}', '{{ '.$key.' }}'], $value ) |
|
41 | 41 | ); |
42 | 42 | } |
43 | - return trim($template); |
|
43 | + return trim( $template ); |
|
44 | 44 | } |
45 | 45 | |
46 | 46 | /** |
47 | 47 | * @param string $templatePath |
48 | 48 | * @return void|string |
49 | 49 | */ |
50 | - public function render($templatePath, array $data = []) |
|
50 | + public function render( $templatePath, array $data = [] ) |
|
51 | 51 | { |
52 | - echo $this->build($templatePath, $data); |
|
52 | + echo $this->build( $templatePath, $data ); |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | /** |
56 | 56 | * @return array |
57 | 57 | */ |
58 | - protected function normalize(array $data) |
|
58 | + protected function normalize( array $data ) |
|
59 | 59 | { |
60 | 60 | $arrayKeys = ['context', 'globals']; |
61 | - $data = wp_parse_args($data, array_fill_keys($arrayKeys, [])); |
|
62 | - foreach ($arrayKeys as $key) { |
|
63 | - if (is_array($data[$key])) { |
|
61 | + $data = wp_parse_args( $data, array_fill_keys( $arrayKeys, [] ) ); |
|
62 | + foreach( $arrayKeys as $key ) { |
|
63 | + if( is_array( $data[$key] ) ) { |
|
64 | 64 | continue; |
65 | 65 | } |
66 | 66 | $data[$key] = []; |
@@ -71,13 +71,13 @@ discard block |
||
71 | 71 | /** |
72 | 72 | * @return array |
73 | 73 | */ |
74 | - protected function normalizeContext(array $context) |
|
74 | + protected function normalizeContext( array $context ) |
|
75 | 75 | { |
76 | - $context = array_filter($context, function ($value) { |
|
77 | - return !is_array($value) && !is_object($value); |
|
76 | + $context = array_filter( $context, function( $value ) { |
|
77 | + return !is_array( $value ) && !is_object( $value ); |
|
78 | 78 | }); |
79 | - return array_map(function ($value) { |
|
80 | - return (string) $value; |
|
81 | - }, $context); |
|
79 | + return array_map( function( $value ) { |
|
80 | + return (string)$value; |
|
81 | + }, $context ); |
|
82 | 82 | } |
83 | 83 | } |
@@ -11,144 +11,144 @@ |
||
11 | 11 | |
12 | 12 | class Style |
13 | 13 | { |
14 | - /** |
|
15 | - * @var array |
|
16 | - */ |
|
17 | - public $fields; |
|
14 | + /** |
|
15 | + * @var array |
|
16 | + */ |
|
17 | + public $fields; |
|
18 | 18 | |
19 | - /** |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - public $style; |
|
19 | + /** |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + public $style; |
|
23 | 23 | |
24 | - /** |
|
25 | - * @var array |
|
26 | - */ |
|
27 | - public $pagination; |
|
24 | + /** |
|
25 | + * @var array |
|
26 | + */ |
|
27 | + public $pagination; |
|
28 | 28 | |
29 | - /** |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - public $validation; |
|
29 | + /** |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + public $validation; |
|
33 | 33 | |
34 | - public function __construct() |
|
35 | - { |
|
36 | - $this->style = glsr(OptionManager::class)->get('settings.general.style', 'default'); |
|
37 | - $this->setConfig(); |
|
38 | - } |
|
34 | + public function __construct() |
|
35 | + { |
|
36 | + $this->style = glsr(OptionManager::class)->get('settings.general.style', 'default'); |
|
37 | + $this->setConfig(); |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @param string $view |
|
42 | - * @return string |
|
43 | - */ |
|
44 | - public function filterView($view) |
|
45 | - { |
|
46 | - $styledViews = [ |
|
47 | - 'templates/form/field', |
|
48 | - 'templates/form/response', |
|
49 | - 'templates/form/submit-button', |
|
50 | - 'templates/reviews-form', |
|
51 | - ]; |
|
52 | - if (!preg_match('('.implode('|', $styledViews).')', $view)) { |
|
53 | - return $view; |
|
54 | - } |
|
55 | - $views = $this->generatePossibleViews($view); |
|
56 | - foreach ($views as $possibleView) { |
|
57 | - if (!file_exists(glsr()->file($possibleView))) { |
|
58 | - continue; |
|
59 | - } |
|
60 | - return glsr(Helper::class)->removePrefix('views/', $possibleView); |
|
61 | - } |
|
62 | - return $view; |
|
63 | - } |
|
40 | + /** |
|
41 | + * @param string $view |
|
42 | + * @return string |
|
43 | + */ |
|
44 | + public function filterView($view) |
|
45 | + { |
|
46 | + $styledViews = [ |
|
47 | + 'templates/form/field', |
|
48 | + 'templates/form/response', |
|
49 | + 'templates/form/submit-button', |
|
50 | + 'templates/reviews-form', |
|
51 | + ]; |
|
52 | + if (!preg_match('('.implode('|', $styledViews).')', $view)) { |
|
53 | + return $view; |
|
54 | + } |
|
55 | + $views = $this->generatePossibleViews($view); |
|
56 | + foreach ($views as $possibleView) { |
|
57 | + if (!file_exists(glsr()->file($possibleView))) { |
|
58 | + continue; |
|
59 | + } |
|
60 | + return glsr(Helper::class)->removePrefix('views/', $possibleView); |
|
61 | + } |
|
62 | + return $view; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * @return string |
|
67 | - */ |
|
68 | - public function get() |
|
69 | - { |
|
70 | - return apply_filters('site-reviews/style', $this->style); |
|
71 | - } |
|
65 | + /** |
|
66 | + * @return string |
|
67 | + */ |
|
68 | + public function get() |
|
69 | + { |
|
70 | + return apply_filters('site-reviews/style', $this->style); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * @return array |
|
75 | - */ |
|
76 | - public function setConfig() |
|
77 | - { |
|
78 | - $config = shortcode_atts( |
|
79 | - array_fill_keys(['fields', 'pagination', 'validation'], []), |
|
80 | - glsr()->config('styles/'.$this->style) |
|
81 | - ); |
|
82 | - $this->fields = glsr(StyleFieldsDefaults::class)->restrict($config['fields']); |
|
83 | - $this->pagination = glsr(PaginationDefaults::class)->restrict($config['pagination']); |
|
84 | - $this->validation = glsr(StyleValidationDefaults::class)->restrict($config['validation']); |
|
85 | - } |
|
73 | + /** |
|
74 | + * @return array |
|
75 | + */ |
|
76 | + public function setConfig() |
|
77 | + { |
|
78 | + $config = shortcode_atts( |
|
79 | + array_fill_keys(['fields', 'pagination', 'validation'], []), |
|
80 | + glsr()->config('styles/'.$this->style) |
|
81 | + ); |
|
82 | + $this->fields = glsr(StyleFieldsDefaults::class)->restrict($config['fields']); |
|
83 | + $this->pagination = glsr(PaginationDefaults::class)->restrict($config['pagination']); |
|
84 | + $this->validation = glsr(StyleValidationDefaults::class)->restrict($config['validation']); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * @return void |
|
89 | - */ |
|
90 | - public function modifyField(Builder $instance) |
|
91 | - { |
|
92 | - if (!$this->isPublicInstance($instance) || empty(array_filter($this->fields))) { |
|
93 | - return; |
|
94 | - } |
|
95 | - call_user_func_array([$this, 'customize'], [$instance]); |
|
96 | - } |
|
87 | + /** |
|
88 | + * @return void |
|
89 | + */ |
|
90 | + public function modifyField(Builder $instance) |
|
91 | + { |
|
92 | + if (!$this->isPublicInstance($instance) || empty(array_filter($this->fields))) { |
|
93 | + return; |
|
94 | + } |
|
95 | + call_user_func_array([$this, 'customize'], [$instance]); |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * @return array |
|
100 | - */ |
|
101 | - public function paginationArgs(array $args) |
|
102 | - { |
|
103 | - return wp_parse_args($args, $this->pagination); |
|
104 | - } |
|
98 | + /** |
|
99 | + * @return array |
|
100 | + */ |
|
101 | + public function paginationArgs(array $args) |
|
102 | + { |
|
103 | + return wp_parse_args($args, $this->pagination); |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * @return void |
|
108 | - */ |
|
109 | - protected function customize(Builder $instance) |
|
110 | - { |
|
111 | - if (!array_key_exists($instance->tag, $this->fields)) { |
|
112 | - return; |
|
113 | - } |
|
114 | - $args = wp_parse_args($instance->args, array_fill_keys(['class', 'type'], '')); |
|
115 | - $key = $instance->tag.'_'.$args['type']; |
|
116 | - $classes = glsr_get($this->fields, $key, glsr_get($this->fields, $instance->tag)); |
|
117 | - $instance->args['class'] = trim($args['class'].' '.$classes); |
|
118 | - do_action_ref_array('site-reviews/customize/'.$this->style, [$instance]); |
|
119 | - } |
|
106 | + /** |
|
107 | + * @return void |
|
108 | + */ |
|
109 | + protected function customize(Builder $instance) |
|
110 | + { |
|
111 | + if (!array_key_exists($instance->tag, $this->fields)) { |
|
112 | + return; |
|
113 | + } |
|
114 | + $args = wp_parse_args($instance->args, array_fill_keys(['class', 'type'], '')); |
|
115 | + $key = $instance->tag.'_'.$args['type']; |
|
116 | + $classes = glsr_get($this->fields, $key, glsr_get($this->fields, $instance->tag)); |
|
117 | + $instance->args['class'] = trim($args['class'].' '.$classes); |
|
118 | + do_action_ref_array('site-reviews/customize/'.$this->style, [$instance]); |
|
119 | + } |
|
120 | 120 | |
121 | - /** |
|
122 | - * @param string $view |
|
123 | - * @return array |
|
124 | - */ |
|
125 | - protected function generatePossibleViews($view) |
|
126 | - { |
|
127 | - $basename = basename($view); |
|
128 | - $basepath = rtrim($view, $basename); |
|
129 | - $customPath = 'views/partials/styles/'.$this->style.'/'; |
|
130 | - $parts = explode('_', $basename); |
|
131 | - $views = [ |
|
132 | - $customPath.$basename, |
|
133 | - $customPath.$parts[0], |
|
134 | - $view, |
|
135 | - $basepath.$parts[0], |
|
136 | - ]; |
|
137 | - return array_filter($views); |
|
138 | - } |
|
121 | + /** |
|
122 | + * @param string $view |
|
123 | + * @return array |
|
124 | + */ |
|
125 | + protected function generatePossibleViews($view) |
|
126 | + { |
|
127 | + $basename = basename($view); |
|
128 | + $basepath = rtrim($view, $basename); |
|
129 | + $customPath = 'views/partials/styles/'.$this->style.'/'; |
|
130 | + $parts = explode('_', $basename); |
|
131 | + $views = [ |
|
132 | + $customPath.$basename, |
|
133 | + $customPath.$parts[0], |
|
134 | + $view, |
|
135 | + $basepath.$parts[0], |
|
136 | + ]; |
|
137 | + return array_filter($views); |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * @return bool |
|
142 | - */ |
|
143 | - protected function isPublicInstance(Builder $instance) |
|
144 | - { |
|
145 | - $args = wp_parse_args($instance->args, [ |
|
146 | - 'is_public' => false, |
|
147 | - 'is_raw' => false, |
|
148 | - ]); |
|
149 | - if (is_admin() || !$args['is_public'] || $args['is_raw']) { |
|
150 | - return false; |
|
151 | - } |
|
152 | - return true; |
|
153 | - } |
|
140 | + /** |
|
141 | + * @return bool |
|
142 | + */ |
|
143 | + protected function isPublicInstance(Builder $instance) |
|
144 | + { |
|
145 | + $args = wp_parse_args($instance->args, [ |
|
146 | + 'is_public' => false, |
|
147 | + 'is_raw' => false, |
|
148 | + ]); |
|
149 | + if (is_admin() || !$args['is_public'] || $args['is_raw']) { |
|
150 | + return false; |
|
151 | + } |
|
152 | + return true; |
|
153 | + } |
|
154 | 154 | } |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | |
34 | 34 | public function __construct() |
35 | 35 | { |
36 | - $this->style = glsr(OptionManager::class)->get('settings.general.style', 'default'); |
|
36 | + $this->style = glsr( OptionManager::class )->get( 'settings.general.style', 'default' ); |
|
37 | 37 | $this->setConfig(); |
38 | 38 | } |
39 | 39 | |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | * @param string $view |
42 | 42 | * @return string |
43 | 43 | */ |
44 | - public function filterView($view) |
|
44 | + public function filterView( $view ) |
|
45 | 45 | { |
46 | 46 | $styledViews = [ |
47 | 47 | 'templates/form/field', |
@@ -49,15 +49,15 @@ discard block |
||
49 | 49 | 'templates/form/submit-button', |
50 | 50 | 'templates/reviews-form', |
51 | 51 | ]; |
52 | - if (!preg_match('('.implode('|', $styledViews).')', $view)) { |
|
52 | + if( !preg_match( '('.implode( '|', $styledViews ).')', $view ) ) { |
|
53 | 53 | return $view; |
54 | 54 | } |
55 | - $views = $this->generatePossibleViews($view); |
|
56 | - foreach ($views as $possibleView) { |
|
57 | - if (!file_exists(glsr()->file($possibleView))) { |
|
55 | + $views = $this->generatePossibleViews( $view ); |
|
56 | + foreach( $views as $possibleView ) { |
|
57 | + if( !file_exists( glsr()->file( $possibleView ) ) ) { |
|
58 | 58 | continue; |
59 | 59 | } |
60 | - return glsr(Helper::class)->removePrefix('views/', $possibleView); |
|
60 | + return glsr( Helper::class )->removePrefix( 'views/', $possibleView ); |
|
61 | 61 | } |
62 | 62 | return $view; |
63 | 63 | } |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | */ |
68 | 68 | public function get() |
69 | 69 | { |
70 | - return apply_filters('site-reviews/style', $this->style); |
|
70 | + return apply_filters( 'site-reviews/style', $this->style ); |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | /** |
@@ -76,77 +76,77 @@ discard block |
||
76 | 76 | public function setConfig() |
77 | 77 | { |
78 | 78 | $config = shortcode_atts( |
79 | - array_fill_keys(['fields', 'pagination', 'validation'], []), |
|
80 | - glsr()->config('styles/'.$this->style) |
|
79 | + array_fill_keys( ['fields', 'pagination', 'validation'], [] ), |
|
80 | + glsr()->config( 'styles/'.$this->style ) |
|
81 | 81 | ); |
82 | - $this->fields = glsr(StyleFieldsDefaults::class)->restrict($config['fields']); |
|
83 | - $this->pagination = glsr(PaginationDefaults::class)->restrict($config['pagination']); |
|
84 | - $this->validation = glsr(StyleValidationDefaults::class)->restrict($config['validation']); |
|
82 | + $this->fields = glsr( StyleFieldsDefaults::class )->restrict( $config['fields'] ); |
|
83 | + $this->pagination = glsr( PaginationDefaults::class )->restrict( $config['pagination'] ); |
|
84 | + $this->validation = glsr( StyleValidationDefaults::class )->restrict( $config['validation'] ); |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | /** |
88 | 88 | * @return void |
89 | 89 | */ |
90 | - public function modifyField(Builder $instance) |
|
90 | + public function modifyField( Builder $instance ) |
|
91 | 91 | { |
92 | - if (!$this->isPublicInstance($instance) || empty(array_filter($this->fields))) { |
|
92 | + if( !$this->isPublicInstance( $instance ) || empty(array_filter( $this->fields )) ) { |
|
93 | 93 | return; |
94 | 94 | } |
95 | - call_user_func_array([$this, 'customize'], [$instance]); |
|
95 | + call_user_func_array( [$this, 'customize'], [$instance] ); |
|
96 | 96 | } |
97 | 97 | |
98 | 98 | /** |
99 | 99 | * @return array |
100 | 100 | */ |
101 | - public function paginationArgs(array $args) |
|
101 | + public function paginationArgs( array $args ) |
|
102 | 102 | { |
103 | - return wp_parse_args($args, $this->pagination); |
|
103 | + return wp_parse_args( $args, $this->pagination ); |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | /** |
107 | 107 | * @return void |
108 | 108 | */ |
109 | - protected function customize(Builder $instance) |
|
109 | + protected function customize( Builder $instance ) |
|
110 | 110 | { |
111 | - if (!array_key_exists($instance->tag, $this->fields)) { |
|
111 | + if( !array_key_exists( $instance->tag, $this->fields ) ) { |
|
112 | 112 | return; |
113 | 113 | } |
114 | - $args = wp_parse_args($instance->args, array_fill_keys(['class', 'type'], '')); |
|
114 | + $args = wp_parse_args( $instance->args, array_fill_keys( ['class', 'type'], '' ) ); |
|
115 | 115 | $key = $instance->tag.'_'.$args['type']; |
116 | - $classes = glsr_get($this->fields, $key, glsr_get($this->fields, $instance->tag)); |
|
117 | - $instance->args['class'] = trim($args['class'].' '.$classes); |
|
118 | - do_action_ref_array('site-reviews/customize/'.$this->style, [$instance]); |
|
116 | + $classes = glsr_get( $this->fields, $key, glsr_get( $this->fields, $instance->tag ) ); |
|
117 | + $instance->args['class'] = trim( $args['class'].' '.$classes ); |
|
118 | + do_action_ref_array( 'site-reviews/customize/'.$this->style, [$instance] ); |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | /** |
122 | 122 | * @param string $view |
123 | 123 | * @return array |
124 | 124 | */ |
125 | - protected function generatePossibleViews($view) |
|
125 | + protected function generatePossibleViews( $view ) |
|
126 | 126 | { |
127 | - $basename = basename($view); |
|
128 | - $basepath = rtrim($view, $basename); |
|
127 | + $basename = basename( $view ); |
|
128 | + $basepath = rtrim( $view, $basename ); |
|
129 | 129 | $customPath = 'views/partials/styles/'.$this->style.'/'; |
130 | - $parts = explode('_', $basename); |
|
130 | + $parts = explode( '_', $basename ); |
|
131 | 131 | $views = [ |
132 | 132 | $customPath.$basename, |
133 | 133 | $customPath.$parts[0], |
134 | 134 | $view, |
135 | 135 | $basepath.$parts[0], |
136 | 136 | ]; |
137 | - return array_filter($views); |
|
137 | + return array_filter( $views ); |
|
138 | 138 | } |
139 | 139 | |
140 | 140 | /** |
141 | 141 | * @return bool |
142 | 142 | */ |
143 | - protected function isPublicInstance(Builder $instance) |
|
143 | + protected function isPublicInstance( Builder $instance ) |
|
144 | 144 | { |
145 | - $args = wp_parse_args($instance->args, [ |
|
145 | + $args = wp_parse_args( $instance->args, [ |
|
146 | 146 | 'is_public' => false, |
147 | 147 | 'is_raw' => false, |
148 | - ]); |
|
149 | - if (is_admin() || !$args['is_public'] || $args['is_raw']) { |
|
148 | + ] ); |
|
149 | + if( is_admin() || !$args['is_public'] || $args['is_raw'] ) { |
|
150 | 150 | return false; |
151 | 151 | } |
152 | 152 | return true; |
@@ -73,7 +73,8 @@ discard block |
||
73 | 73 | foreach ($potEntries as $key => $entry) { |
74 | 74 | $entries[html_entity_decode($key, ENT_COMPAT, 'UTF-8')] = $entry; |
75 | 75 | } |
76 | - } catch (Exception $e) { |
|
76 | + } |
|
77 | + catch (Exception $e) { |
|
77 | 78 | glsr_log()->error($e->getMessage()); |
78 | 79 | } |
79 | 80 | return $entries; |
@@ -196,7 +197,8 @@ discard block |
||
196 | 197 | if (in_array($needle, [$single, $plural])) { |
197 | 198 | $this->results[$key] = $entry; |
198 | 199 | } |
199 | - } elseif (false !== strpos(sprintf('%s %s', $single, $plural), $needle)) { |
|
200 | + } |
|
201 | + elseif (false !== strpos(sprintf('%s %s', $single, $plural), $needle)) { |
|
200 | 202 | $this->results[$key] = $entry; |
201 | 203 | } |
202 | 204 | } |
@@ -242,7 +244,8 @@ discard block |
||
242 | 244 | foreach ($keys as $key) { |
243 | 245 | try { |
244 | 246 | $entry = $this->normalizeEntryString($entry, $key); |
245 | - } catch (\TypeError $error) { |
|
247 | + } |
|
248 | + catch (\TypeError $error) { |
|
246 | 249 | glsr_log()->once('error', 'Translation/normalize', $error); |
247 | 250 | glsr_log()->once('debug', 'Translation/normalize', $entry); |
248 | 251 | } |
@@ -10,272 +10,272 @@ |
||
10 | 10 | |
11 | 11 | class Translation |
12 | 12 | { |
13 | - const SEARCH_THRESHOLD = 3; |
|
13 | + const SEARCH_THRESHOLD = 3; |
|
14 | 14 | |
15 | - /** |
|
16 | - * @var array |
|
17 | - */ |
|
18 | - protected $entries; |
|
15 | + /** |
|
16 | + * @var array |
|
17 | + */ |
|
18 | + protected $entries; |
|
19 | 19 | |
20 | - /** |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - protected $results; |
|
20 | + /** |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + protected $results; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Returns all saved custom translations with translation context. |
|
27 | - * @return array |
|
28 | - */ |
|
29 | - public function all() |
|
30 | - { |
|
31 | - $translations = $this->translations(); |
|
32 | - $entries = $this->filter($translations, $this->entries())->results(); |
|
33 | - array_walk($translations, function (&$entry) use ($entries) { |
|
34 | - $entry['desc'] = array_key_exists($entry['id'], $entries) |
|
35 | - ? $this->getEntryString($entries[$entry['id']], 'msgctxt') |
|
36 | - : ''; |
|
37 | - }); |
|
38 | - return $translations; |
|
39 | - } |
|
25 | + /** |
|
26 | + * Returns all saved custom translations with translation context. |
|
27 | + * @return array |
|
28 | + */ |
|
29 | + public function all() |
|
30 | + { |
|
31 | + $translations = $this->translations(); |
|
32 | + $entries = $this->filter($translations, $this->entries())->results(); |
|
33 | + array_walk($translations, function (&$entry) use ($entries) { |
|
34 | + $entry['desc'] = array_key_exists($entry['id'], $entries) |
|
35 | + ? $this->getEntryString($entries[$entry['id']], 'msgctxt') |
|
36 | + : ''; |
|
37 | + }); |
|
38 | + return $translations; |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * @return array |
|
43 | - */ |
|
44 | - public function entries() |
|
45 | - { |
|
46 | - if (!isset($this->entries)) { |
|
47 | - $potFile = glsr()->path(glsr()->languages.'/'.Application::ID.'.pot'); |
|
48 | - $entries = $this->extractEntriesFromPotFile($potFile); |
|
49 | - $entries = apply_filters('site-reviews/translation/entries', $entries); |
|
50 | - $this->entries = $entries; |
|
51 | - } |
|
52 | - return $this->entries; |
|
53 | - } |
|
41 | + /** |
|
42 | + * @return array |
|
43 | + */ |
|
44 | + public function entries() |
|
45 | + { |
|
46 | + if (!isset($this->entries)) { |
|
47 | + $potFile = glsr()->path(glsr()->languages.'/'.Application::ID.'.pot'); |
|
48 | + $entries = $this->extractEntriesFromPotFile($potFile); |
|
49 | + $entries = apply_filters('site-reviews/translation/entries', $entries); |
|
50 | + $this->entries = $entries; |
|
51 | + } |
|
52 | + return $this->entries; |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @param array|null $entriesToExclude |
|
57 | - * @param array|null $entries |
|
58 | - * @return static |
|
59 | - */ |
|
60 | - public function exclude($entriesToExclude = null, $entries = null) |
|
61 | - { |
|
62 | - return $this->filter($entriesToExclude, $entries, false); |
|
63 | - } |
|
55 | + /** |
|
56 | + * @param array|null $entriesToExclude |
|
57 | + * @param array|null $entries |
|
58 | + * @return static |
|
59 | + */ |
|
60 | + public function exclude($entriesToExclude = null, $entries = null) |
|
61 | + { |
|
62 | + return $this->filter($entriesToExclude, $entries, false); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * @param string $potFile |
|
67 | - * @return array |
|
68 | - */ |
|
69 | - public function extractEntriesFromPotFile($potFile, array $entries = []) |
|
70 | - { |
|
71 | - try { |
|
72 | - $potEntries = $this->normalize(Parser::parseFile($potFile)->getEntries()); |
|
73 | - foreach ($potEntries as $key => $entry) { |
|
74 | - $entries[html_entity_decode($key, ENT_COMPAT, 'UTF-8')] = $entry; |
|
75 | - } |
|
76 | - } catch (Exception $e) { |
|
77 | - glsr_log()->error($e->getMessage()); |
|
78 | - } |
|
79 | - return $entries; |
|
80 | - } |
|
65 | + /** |
|
66 | + * @param string $potFile |
|
67 | + * @return array |
|
68 | + */ |
|
69 | + public function extractEntriesFromPotFile($potFile, array $entries = []) |
|
70 | + { |
|
71 | + try { |
|
72 | + $potEntries = $this->normalize(Parser::parseFile($potFile)->getEntries()); |
|
73 | + foreach ($potEntries as $key => $entry) { |
|
74 | + $entries[html_entity_decode($key, ENT_COMPAT, 'UTF-8')] = $entry; |
|
75 | + } |
|
76 | + } catch (Exception $e) { |
|
77 | + glsr_log()->error($e->getMessage()); |
|
78 | + } |
|
79 | + return $entries; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @param array|null $filterWith |
|
84 | - * @param array|null $entries |
|
85 | - * @param bool $intersect |
|
86 | - * @return static |
|
87 | - */ |
|
88 | - public function filter($filterWith = null, $entries = null, $intersect = true) |
|
89 | - { |
|
90 | - if (!is_array($entries)) { |
|
91 | - $entries = $this->results; |
|
92 | - } |
|
93 | - if (!is_array($filterWith)) { |
|
94 | - $filterWith = $this->translations(); |
|
95 | - } |
|
96 | - $keys = array_flip(glsr_array_column($filterWith, 'id')); |
|
97 | - $this->results = $intersect |
|
98 | - ? array_intersect_key($entries, $keys) |
|
99 | - : array_diff_key($entries, $keys); |
|
100 | - return $this; |
|
101 | - } |
|
82 | + /** |
|
83 | + * @param array|null $filterWith |
|
84 | + * @param array|null $entries |
|
85 | + * @param bool $intersect |
|
86 | + * @return static |
|
87 | + */ |
|
88 | + public function filter($filterWith = null, $entries = null, $intersect = true) |
|
89 | + { |
|
90 | + if (!is_array($entries)) { |
|
91 | + $entries = $this->results; |
|
92 | + } |
|
93 | + if (!is_array($filterWith)) { |
|
94 | + $filterWith = $this->translations(); |
|
95 | + } |
|
96 | + $keys = array_flip(glsr_array_column($filterWith, 'id')); |
|
97 | + $this->results = $intersect |
|
98 | + ? array_intersect_key($entries, $keys) |
|
99 | + : array_diff_key($entries, $keys); |
|
100 | + return $this; |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * @param string $template |
|
105 | - * @return string |
|
106 | - */ |
|
107 | - public function render($template, array $entry) |
|
108 | - { |
|
109 | - $data = array_combine( |
|
110 | - array_map(function ($key) { return 'data.'.$key; }, array_keys($entry)), |
|
111 | - $entry |
|
112 | - ); |
|
113 | - $data['data.class'] = $data['data.error'] = ''; |
|
114 | - if (false === array_search($entry['s1'], glsr_array_column($this->entries(), 'msgid'))) { |
|
115 | - $data['data.class'] = 'is-invalid'; |
|
116 | - $data['data.error'] = __('This custom translation is no longer valid as the original text has been changed or removed.', 'site-reviews'); |
|
117 | - } |
|
118 | - return glsr(Template::class)->build('partials/translations/'.$template, [ |
|
119 | - 'context' => $data, |
|
120 | - ]); |
|
121 | - } |
|
103 | + /** |
|
104 | + * @param string $template |
|
105 | + * @return string |
|
106 | + */ |
|
107 | + public function render($template, array $entry) |
|
108 | + { |
|
109 | + $data = array_combine( |
|
110 | + array_map(function ($key) { return 'data.'.$key; }, array_keys($entry)), |
|
111 | + $entry |
|
112 | + ); |
|
113 | + $data['data.class'] = $data['data.error'] = ''; |
|
114 | + if (false === array_search($entry['s1'], glsr_array_column($this->entries(), 'msgid'))) { |
|
115 | + $data['data.class'] = 'is-invalid'; |
|
116 | + $data['data.error'] = __('This custom translation is no longer valid as the original text has been changed or removed.', 'site-reviews'); |
|
117 | + } |
|
118 | + return glsr(Template::class)->build('partials/translations/'.$template, [ |
|
119 | + 'context' => $data, |
|
120 | + ]); |
|
121 | + } |
|
122 | 122 | |
123 | - /** |
|
124 | - * Returns a rendered string of all saved custom translations with translation context. |
|
125 | - * @return string |
|
126 | - */ |
|
127 | - public function renderAll() |
|
128 | - { |
|
129 | - $rendered = ''; |
|
130 | - foreach ($this->all() as $index => $entry) { |
|
131 | - $entry['index'] = $index; |
|
132 | - $entry['prefix'] = OptionManager::databaseKey(); |
|
133 | - $rendered.= $this->render($entry['type'], $entry); |
|
134 | - } |
|
135 | - return $rendered; |
|
136 | - } |
|
123 | + /** |
|
124 | + * Returns a rendered string of all saved custom translations with translation context. |
|
125 | + * @return string |
|
126 | + */ |
|
127 | + public function renderAll() |
|
128 | + { |
|
129 | + $rendered = ''; |
|
130 | + foreach ($this->all() as $index => $entry) { |
|
131 | + $entry['index'] = $index; |
|
132 | + $entry['prefix'] = OptionManager::databaseKey(); |
|
133 | + $rendered.= $this->render($entry['type'], $entry); |
|
134 | + } |
|
135 | + return $rendered; |
|
136 | + } |
|
137 | 137 | |
138 | - /** |
|
139 | - * @param bool $resetAfterRender |
|
140 | - * @return string |
|
141 | - */ |
|
142 | - public function renderResults($resetAfterRender = true) |
|
143 | - { |
|
144 | - $rendered = ''; |
|
145 | - foreach ($this->results as $id => $entry) { |
|
146 | - $data = [ |
|
147 | - 'desc' => $this->getEntryString($entry, 'msgctxt'), |
|
148 | - 'id' => $id, |
|
149 | - 'p1' => $this->getEntryString($entry, 'msgid_plural'), |
|
150 | - 's1' => $this->getEntryString($entry, 'msgid'), |
|
151 | - ]; |
|
152 | - $text = !empty($data['p1']) |
|
153 | - ? sprintf('%s | %s', $data['s1'], $data['p1']) |
|
154 | - : $data['s1']; |
|
155 | - $rendered.= $this->render('result', [ |
|
156 | - 'entry' => json_encode($data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), |
|
157 | - 'text' => wp_strip_all_tags($text), |
|
158 | - ]); |
|
159 | - } |
|
160 | - if ($resetAfterRender) { |
|
161 | - $this->reset(); |
|
162 | - } |
|
163 | - return $rendered; |
|
164 | - } |
|
138 | + /** |
|
139 | + * @param bool $resetAfterRender |
|
140 | + * @return string |
|
141 | + */ |
|
142 | + public function renderResults($resetAfterRender = true) |
|
143 | + { |
|
144 | + $rendered = ''; |
|
145 | + foreach ($this->results as $id => $entry) { |
|
146 | + $data = [ |
|
147 | + 'desc' => $this->getEntryString($entry, 'msgctxt'), |
|
148 | + 'id' => $id, |
|
149 | + 'p1' => $this->getEntryString($entry, 'msgid_plural'), |
|
150 | + 's1' => $this->getEntryString($entry, 'msgid'), |
|
151 | + ]; |
|
152 | + $text = !empty($data['p1']) |
|
153 | + ? sprintf('%s | %s', $data['s1'], $data['p1']) |
|
154 | + : $data['s1']; |
|
155 | + $rendered.= $this->render('result', [ |
|
156 | + 'entry' => json_encode($data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), |
|
157 | + 'text' => wp_strip_all_tags($text), |
|
158 | + ]); |
|
159 | + } |
|
160 | + if ($resetAfterRender) { |
|
161 | + $this->reset(); |
|
162 | + } |
|
163 | + return $rendered; |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * @return void |
|
168 | - */ |
|
169 | - public function reset() |
|
170 | - { |
|
171 | - $this->results = []; |
|
172 | - } |
|
166 | + /** |
|
167 | + * @return void |
|
168 | + */ |
|
169 | + public function reset() |
|
170 | + { |
|
171 | + $this->results = []; |
|
172 | + } |
|
173 | 173 | |
174 | - /** |
|
175 | - * @return array |
|
176 | - */ |
|
177 | - public function results() |
|
178 | - { |
|
179 | - $results = $this->results; |
|
180 | - $this->reset(); |
|
181 | - return $results; |
|
182 | - } |
|
174 | + /** |
|
175 | + * @return array |
|
176 | + */ |
|
177 | + public function results() |
|
178 | + { |
|
179 | + $results = $this->results; |
|
180 | + $this->reset(); |
|
181 | + return $results; |
|
182 | + } |
|
183 | 183 | |
184 | - /** |
|
185 | - * @param string $needle |
|
186 | - * @return static |
|
187 | - */ |
|
188 | - public function search($needle = '') |
|
189 | - { |
|
190 | - $this->reset(); |
|
191 | - $needle = trim(strtolower($needle)); |
|
192 | - foreach ($this->entries() as $key => $entry) { |
|
193 | - $single = strtolower($this->getEntryString($entry, 'msgid')); |
|
194 | - $plural = strtolower($this->getEntryString($entry, 'msgid_plural')); |
|
195 | - if (strlen($needle) < static::SEARCH_THRESHOLD) { |
|
196 | - if (in_array($needle, [$single, $plural])) { |
|
197 | - $this->results[$key] = $entry; |
|
198 | - } |
|
199 | - } elseif (false !== strpos(sprintf('%s %s', $single, $plural), $needle)) { |
|
200 | - $this->results[$key] = $entry; |
|
201 | - } |
|
202 | - } |
|
203 | - return $this; |
|
204 | - } |
|
184 | + /** |
|
185 | + * @param string $needle |
|
186 | + * @return static |
|
187 | + */ |
|
188 | + public function search($needle = '') |
|
189 | + { |
|
190 | + $this->reset(); |
|
191 | + $needle = trim(strtolower($needle)); |
|
192 | + foreach ($this->entries() as $key => $entry) { |
|
193 | + $single = strtolower($this->getEntryString($entry, 'msgid')); |
|
194 | + $plural = strtolower($this->getEntryString($entry, 'msgid_plural')); |
|
195 | + if (strlen($needle) < static::SEARCH_THRESHOLD) { |
|
196 | + if (in_array($needle, [$single, $plural])) { |
|
197 | + $this->results[$key] = $entry; |
|
198 | + } |
|
199 | + } elseif (false !== strpos(sprintf('%s %s', $single, $plural), $needle)) { |
|
200 | + $this->results[$key] = $entry; |
|
201 | + } |
|
202 | + } |
|
203 | + return $this; |
|
204 | + } |
|
205 | 205 | |
206 | - /** |
|
207 | - * Store the translations to avoid unnecessary loops. |
|
208 | - * @return array |
|
209 | - */ |
|
210 | - public function translations() |
|
211 | - { |
|
212 | - static $translations; |
|
213 | - if (empty($translations)) { |
|
214 | - $settings = glsr(OptionManager::class)->get('settings'); |
|
215 | - $translations = isset($settings['strings']) |
|
216 | - ? $this->normalizeSettings((array) $settings['strings']) |
|
217 | - : []; |
|
218 | - } |
|
219 | - return $translations; |
|
220 | - } |
|
206 | + /** |
|
207 | + * Store the translations to avoid unnecessary loops. |
|
208 | + * @return array |
|
209 | + */ |
|
210 | + public function translations() |
|
211 | + { |
|
212 | + static $translations; |
|
213 | + if (empty($translations)) { |
|
214 | + $settings = glsr(OptionManager::class)->get('settings'); |
|
215 | + $translations = isset($settings['strings']) |
|
216 | + ? $this->normalizeSettings((array) $settings['strings']) |
|
217 | + : []; |
|
218 | + } |
|
219 | + return $translations; |
|
220 | + } |
|
221 | 221 | |
222 | - /** |
|
223 | - * @param string $key |
|
224 | - * @return string |
|
225 | - */ |
|
226 | - protected function getEntryString(array $entry, $key) |
|
227 | - { |
|
228 | - return isset($entry[$key]) |
|
229 | - ? implode('', (array) $entry[$key]) |
|
230 | - : ''; |
|
231 | - } |
|
222 | + /** |
|
223 | + * @param string $key |
|
224 | + * @return string |
|
225 | + */ |
|
226 | + protected function getEntryString(array $entry, $key) |
|
227 | + { |
|
228 | + return isset($entry[$key]) |
|
229 | + ? implode('', (array) $entry[$key]) |
|
230 | + : ''; |
|
231 | + } |
|
232 | 232 | |
233 | - /** |
|
234 | - * @return array |
|
235 | - */ |
|
236 | - protected function normalize(array $entries) |
|
237 | - { |
|
238 | - $keys = [ |
|
239 | - 'msgctxt', 'msgid', 'msgid_plural', 'msgstr', 'msgstr[0]', 'msgstr[1]', |
|
240 | - ]; |
|
241 | - array_walk($entries, function (&$entry) use ($keys) { |
|
242 | - foreach ($keys as $key) { |
|
243 | - try { |
|
244 | - $entry = $this->normalizeEntryString($entry, $key); |
|
245 | - } catch (\TypeError $error) { |
|
246 | - glsr_log()->once('error', 'Translation/normalize', $error); |
|
247 | - glsr_log()->once('debug', 'Translation/normalize', $entry); |
|
248 | - } |
|
249 | - } |
|
250 | - }); |
|
251 | - return $entries; |
|
252 | - } |
|
233 | + /** |
|
234 | + * @return array |
|
235 | + */ |
|
236 | + protected function normalize(array $entries) |
|
237 | + { |
|
238 | + $keys = [ |
|
239 | + 'msgctxt', 'msgid', 'msgid_plural', 'msgstr', 'msgstr[0]', 'msgstr[1]', |
|
240 | + ]; |
|
241 | + array_walk($entries, function (&$entry) use ($keys) { |
|
242 | + foreach ($keys as $key) { |
|
243 | + try { |
|
244 | + $entry = $this->normalizeEntryString($entry, $key); |
|
245 | + } catch (\TypeError $error) { |
|
246 | + glsr_log()->once('error', 'Translation/normalize', $error); |
|
247 | + glsr_log()->once('debug', 'Translation/normalize', $entry); |
|
248 | + } |
|
249 | + } |
|
250 | + }); |
|
251 | + return $entries; |
|
252 | + } |
|
253 | 253 | |
254 | - /** |
|
255 | - * @param string $key |
|
256 | - * @return array |
|
257 | - */ |
|
258 | - protected function normalizeEntryString(array $entry, $key) |
|
259 | - { |
|
260 | - if (isset($entry[$key])) { |
|
261 | - $entry[$key] = $this->getEntryString($entry, $key); |
|
262 | - } |
|
263 | - return $entry; |
|
264 | - } |
|
254 | + /** |
|
255 | + * @param string $key |
|
256 | + * @return array |
|
257 | + */ |
|
258 | + protected function normalizeEntryString(array $entry, $key) |
|
259 | + { |
|
260 | + if (isset($entry[$key])) { |
|
261 | + $entry[$key] = $this->getEntryString($entry, $key); |
|
262 | + } |
|
263 | + return $entry; |
|
264 | + } |
|
265 | 265 | |
266 | - /** |
|
267 | - * @return array |
|
268 | - */ |
|
269 | - protected function normalizeSettings(array $strings) |
|
270 | - { |
|
271 | - $defaultString = array_fill_keys(['id', 's1', 's2', 'p1', 'p2'], ''); |
|
272 | - $strings = array_filter($strings, 'is_array'); |
|
273 | - foreach ($strings as &$string) { |
|
274 | - $string['type'] = isset($string['p1']) ? 'plural' : 'single'; |
|
275 | - $string = wp_parse_args($string, $defaultString); |
|
276 | - } |
|
277 | - return array_filter($strings, function ($string) { |
|
278 | - return !empty($string['id']); |
|
279 | - }); |
|
280 | - } |
|
266 | + /** |
|
267 | + * @return array |
|
268 | + */ |
|
269 | + protected function normalizeSettings(array $strings) |
|
270 | + { |
|
271 | + $defaultString = array_fill_keys(['id', 's1', 's2', 'p1', 'p2'], ''); |
|
272 | + $strings = array_filter($strings, 'is_array'); |
|
273 | + foreach ($strings as &$string) { |
|
274 | + $string['type'] = isset($string['p1']) ? 'plural' : 'single'; |
|
275 | + $string = wp_parse_args($string, $defaultString); |
|
276 | + } |
|
277 | + return array_filter($strings, function ($string) { |
|
278 | + return !empty($string['id']); |
|
279 | + }); |
|
280 | + } |
|
281 | 281 | } |
@@ -29,10 +29,10 @@ discard block |
||
29 | 29 | public function all() |
30 | 30 | { |
31 | 31 | $translations = $this->translations(); |
32 | - $entries = $this->filter($translations, $this->entries())->results(); |
|
33 | - array_walk($translations, function (&$entry) use ($entries) { |
|
34 | - $entry['desc'] = array_key_exists($entry['id'], $entries) |
|
35 | - ? $this->getEntryString($entries[$entry['id']], 'msgctxt') |
|
32 | + $entries = $this->filter( $translations, $this->entries() )->results(); |
|
33 | + array_walk( $translations, function( &$entry ) use ($entries) { |
|
34 | + $entry['desc'] = array_key_exists( $entry['id'], $entries ) |
|
35 | + ? $this->getEntryString( $entries[$entry['id']], 'msgctxt' ) |
|
36 | 36 | : ''; |
37 | 37 | }); |
38 | 38 | return $translations; |
@@ -43,10 +43,10 @@ discard block |
||
43 | 43 | */ |
44 | 44 | public function entries() |
45 | 45 | { |
46 | - if (!isset($this->entries)) { |
|
47 | - $potFile = glsr()->path(glsr()->languages.'/'.Application::ID.'.pot'); |
|
48 | - $entries = $this->extractEntriesFromPotFile($potFile); |
|
49 | - $entries = apply_filters('site-reviews/translation/entries', $entries); |
|
46 | + if( !isset($this->entries) ) { |
|
47 | + $potFile = glsr()->path( glsr()->languages.'/'.Application::ID.'.pot' ); |
|
48 | + $entries = $this->extractEntriesFromPotFile( $potFile ); |
|
49 | + $entries = apply_filters( 'site-reviews/translation/entries', $entries ); |
|
50 | 50 | $this->entries = $entries; |
51 | 51 | } |
52 | 52 | return $this->entries; |
@@ -57,24 +57,24 @@ discard block |
||
57 | 57 | * @param array|null $entries |
58 | 58 | * @return static |
59 | 59 | */ |
60 | - public function exclude($entriesToExclude = null, $entries = null) |
|
60 | + public function exclude( $entriesToExclude = null, $entries = null ) |
|
61 | 61 | { |
62 | - return $this->filter($entriesToExclude, $entries, false); |
|
62 | + return $this->filter( $entriesToExclude, $entries, false ); |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | /** |
66 | 66 | * @param string $potFile |
67 | 67 | * @return array |
68 | 68 | */ |
69 | - public function extractEntriesFromPotFile($potFile, array $entries = []) |
|
69 | + public function extractEntriesFromPotFile( $potFile, array $entries = [] ) |
|
70 | 70 | { |
71 | 71 | try { |
72 | - $potEntries = $this->normalize(Parser::parseFile($potFile)->getEntries()); |
|
73 | - foreach ($potEntries as $key => $entry) { |
|
74 | - $entries[html_entity_decode($key, ENT_COMPAT, 'UTF-8')] = $entry; |
|
72 | + $potEntries = $this->normalize( Parser::parseFile( $potFile )->getEntries() ); |
|
73 | + foreach( $potEntries as $key => $entry ) { |
|
74 | + $entries[html_entity_decode( $key, ENT_COMPAT, 'UTF-8' )] = $entry; |
|
75 | 75 | } |
76 | - } catch (Exception $e) { |
|
77 | - glsr_log()->error($e->getMessage()); |
|
76 | + } catch( Exception $e ) { |
|
77 | + glsr_log()->error( $e->getMessage() ); |
|
78 | 78 | } |
79 | 79 | return $entries; |
80 | 80 | } |
@@ -85,18 +85,18 @@ discard block |
||
85 | 85 | * @param bool $intersect |
86 | 86 | * @return static |
87 | 87 | */ |
88 | - public function filter($filterWith = null, $entries = null, $intersect = true) |
|
88 | + public function filter( $filterWith = null, $entries = null, $intersect = true ) |
|
89 | 89 | { |
90 | - if (!is_array($entries)) { |
|
90 | + if( !is_array( $entries ) ) { |
|
91 | 91 | $entries = $this->results; |
92 | 92 | } |
93 | - if (!is_array($filterWith)) { |
|
93 | + if( !is_array( $filterWith ) ) { |
|
94 | 94 | $filterWith = $this->translations(); |
95 | 95 | } |
96 | - $keys = array_flip(glsr_array_column($filterWith, 'id')); |
|
96 | + $keys = array_flip( glsr_array_column( $filterWith, 'id' ) ); |
|
97 | 97 | $this->results = $intersect |
98 | - ? array_intersect_key($entries, $keys) |
|
99 | - : array_diff_key($entries, $keys); |
|
98 | + ? array_intersect_key( $entries, $keys ) |
|
99 | + : array_diff_key( $entries, $keys ); |
|
100 | 100 | return $this; |
101 | 101 | } |
102 | 102 | |
@@ -104,20 +104,20 @@ discard block |
||
104 | 104 | * @param string $template |
105 | 105 | * @return string |
106 | 106 | */ |
107 | - public function render($template, array $entry) |
|
107 | + public function render( $template, array $entry ) |
|
108 | 108 | { |
109 | 109 | $data = array_combine( |
110 | - array_map(function ($key) { return 'data.'.$key; }, array_keys($entry)), |
|
110 | + array_map( function( $key ) { return 'data.'.$key; }, array_keys( $entry ) ), |
|
111 | 111 | $entry |
112 | 112 | ); |
113 | 113 | $data['data.class'] = $data['data.error'] = ''; |
114 | - if (false === array_search($entry['s1'], glsr_array_column($this->entries(), 'msgid'))) { |
|
114 | + if( false === array_search( $entry['s1'], glsr_array_column( $this->entries(), 'msgid' ) ) ) { |
|
115 | 115 | $data['data.class'] = 'is-invalid'; |
116 | - $data['data.error'] = __('This custom translation is no longer valid as the original text has been changed or removed.', 'site-reviews'); |
|
116 | + $data['data.error'] = __( 'This custom translation is no longer valid as the original text has been changed or removed.', 'site-reviews' ); |
|
117 | 117 | } |
118 | - return glsr(Template::class)->build('partials/translations/'.$template, [ |
|
118 | + return glsr( Template::class )->build( 'partials/translations/'.$template, [ |
|
119 | 119 | 'context' => $data, |
120 | - ]); |
|
120 | + ] ); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | /** |
@@ -127,10 +127,10 @@ discard block |
||
127 | 127 | public function renderAll() |
128 | 128 | { |
129 | 129 | $rendered = ''; |
130 | - foreach ($this->all() as $index => $entry) { |
|
130 | + foreach( $this->all() as $index => $entry ) { |
|
131 | 131 | $entry['index'] = $index; |
132 | 132 | $entry['prefix'] = OptionManager::databaseKey(); |
133 | - $rendered.= $this->render($entry['type'], $entry); |
|
133 | + $rendered .= $this->render( $entry['type'], $entry ); |
|
134 | 134 | } |
135 | 135 | return $rendered; |
136 | 136 | } |
@@ -139,25 +139,25 @@ discard block |
||
139 | 139 | * @param bool $resetAfterRender |
140 | 140 | * @return string |
141 | 141 | */ |
142 | - public function renderResults($resetAfterRender = true) |
|
142 | + public function renderResults( $resetAfterRender = true ) |
|
143 | 143 | { |
144 | 144 | $rendered = ''; |
145 | - foreach ($this->results as $id => $entry) { |
|
145 | + foreach( $this->results as $id => $entry ) { |
|
146 | 146 | $data = [ |
147 | - 'desc' => $this->getEntryString($entry, 'msgctxt'), |
|
147 | + 'desc' => $this->getEntryString( $entry, 'msgctxt' ), |
|
148 | 148 | 'id' => $id, |
149 | - 'p1' => $this->getEntryString($entry, 'msgid_plural'), |
|
150 | - 's1' => $this->getEntryString($entry, 'msgid'), |
|
149 | + 'p1' => $this->getEntryString( $entry, 'msgid_plural' ), |
|
150 | + 's1' => $this->getEntryString( $entry, 'msgid' ), |
|
151 | 151 | ]; |
152 | 152 | $text = !empty($data['p1']) |
153 | - ? sprintf('%s | %s', $data['s1'], $data['p1']) |
|
153 | + ? sprintf( '%s | %s', $data['s1'], $data['p1'] ) |
|
154 | 154 | : $data['s1']; |
155 | - $rendered.= $this->render('result', [ |
|
156 | - 'entry' => json_encode($data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), |
|
157 | - 'text' => wp_strip_all_tags($text), |
|
158 | - ]); |
|
155 | + $rendered .= $this->render( 'result', [ |
|
156 | + 'entry' => json_encode( $data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ), |
|
157 | + 'text' => wp_strip_all_tags( $text ), |
|
158 | + ] ); |
|
159 | 159 | } |
160 | - if ($resetAfterRender) { |
|
160 | + if( $resetAfterRender ) { |
|
161 | 161 | $this->reset(); |
162 | 162 | } |
163 | 163 | return $rendered; |
@@ -185,18 +185,18 @@ discard block |
||
185 | 185 | * @param string $needle |
186 | 186 | * @return static |
187 | 187 | */ |
188 | - public function search($needle = '') |
|
188 | + public function search( $needle = '' ) |
|
189 | 189 | { |
190 | 190 | $this->reset(); |
191 | - $needle = trim(strtolower($needle)); |
|
192 | - foreach ($this->entries() as $key => $entry) { |
|
193 | - $single = strtolower($this->getEntryString($entry, 'msgid')); |
|
194 | - $plural = strtolower($this->getEntryString($entry, 'msgid_plural')); |
|
195 | - if (strlen($needle) < static::SEARCH_THRESHOLD) { |
|
196 | - if (in_array($needle, [$single, $plural])) { |
|
191 | + $needle = trim( strtolower( $needle ) ); |
|
192 | + foreach( $this->entries() as $key => $entry ) { |
|
193 | + $single = strtolower( $this->getEntryString( $entry, 'msgid' ) ); |
|
194 | + $plural = strtolower( $this->getEntryString( $entry, 'msgid_plural' ) ); |
|
195 | + if( strlen( $needle ) < static::SEARCH_THRESHOLD ) { |
|
196 | + if( in_array( $needle, [$single, $plural] ) ) { |
|
197 | 197 | $this->results[$key] = $entry; |
198 | 198 | } |
199 | - } elseif (false !== strpos(sprintf('%s %s', $single, $plural), $needle)) { |
|
199 | + } elseif( false !== strpos( sprintf( '%s %s', $single, $plural ), $needle ) ) { |
|
200 | 200 | $this->results[$key] = $entry; |
201 | 201 | } |
202 | 202 | } |
@@ -210,10 +210,10 @@ discard block |
||
210 | 210 | public function translations() |
211 | 211 | { |
212 | 212 | static $translations; |
213 | - if (empty($translations)) { |
|
214 | - $settings = glsr(OptionManager::class)->get('settings'); |
|
213 | + if( empty($translations) ) { |
|
214 | + $settings = glsr( OptionManager::class )->get( 'settings' ); |
|
215 | 215 | $translations = isset($settings['strings']) |
216 | - ? $this->normalizeSettings((array) $settings['strings']) |
|
216 | + ? $this->normalizeSettings( (array)$settings['strings'] ) |
|
217 | 217 | : []; |
218 | 218 | } |
219 | 219 | return $translations; |
@@ -223,28 +223,28 @@ discard block |
||
223 | 223 | * @param string $key |
224 | 224 | * @return string |
225 | 225 | */ |
226 | - protected function getEntryString(array $entry, $key) |
|
226 | + protected function getEntryString( array $entry, $key ) |
|
227 | 227 | { |
228 | 228 | return isset($entry[$key]) |
229 | - ? implode('', (array) $entry[$key]) |
|
229 | + ? implode( '', (array)$entry[$key] ) |
|
230 | 230 | : ''; |
231 | 231 | } |
232 | 232 | |
233 | 233 | /** |
234 | 234 | * @return array |
235 | 235 | */ |
236 | - protected function normalize(array $entries) |
|
236 | + protected function normalize( array $entries ) |
|
237 | 237 | { |
238 | 238 | $keys = [ |
239 | 239 | 'msgctxt', 'msgid', 'msgid_plural', 'msgstr', 'msgstr[0]', 'msgstr[1]', |
240 | 240 | ]; |
241 | - array_walk($entries, function (&$entry) use ($keys) { |
|
242 | - foreach ($keys as $key) { |
|
241 | + array_walk( $entries, function( &$entry ) use ($keys) { |
|
242 | + foreach( $keys as $key ) { |
|
243 | 243 | try { |
244 | - $entry = $this->normalizeEntryString($entry, $key); |
|
245 | - } catch (\TypeError $error) { |
|
246 | - glsr_log()->once('error', 'Translation/normalize', $error); |
|
247 | - glsr_log()->once('debug', 'Translation/normalize', $entry); |
|
244 | + $entry = $this->normalizeEntryString( $entry, $key ); |
|
245 | + } catch( \TypeError $error ) { |
|
246 | + glsr_log()->once( 'error', 'Translation/normalize', $error ); |
|
247 | + glsr_log()->once( 'debug', 'Translation/normalize', $entry ); |
|
248 | 248 | } |
249 | 249 | } |
250 | 250 | }); |
@@ -255,10 +255,10 @@ discard block |
||
255 | 255 | * @param string $key |
256 | 256 | * @return array |
257 | 257 | */ |
258 | - protected function normalizeEntryString(array $entry, $key) |
|
258 | + protected function normalizeEntryString( array $entry, $key ) |
|
259 | 259 | { |
260 | - if (isset($entry[$key])) { |
|
261 | - $entry[$key] = $this->getEntryString($entry, $key); |
|
260 | + if( isset($entry[$key]) ) { |
|
261 | + $entry[$key] = $this->getEntryString( $entry, $key ); |
|
262 | 262 | } |
263 | 263 | return $entry; |
264 | 264 | } |
@@ -266,15 +266,15 @@ discard block |
||
266 | 266 | /** |
267 | 267 | * @return array |
268 | 268 | */ |
269 | - protected function normalizeSettings(array $strings) |
|
269 | + protected function normalizeSettings( array $strings ) |
|
270 | 270 | { |
271 | - $defaultString = array_fill_keys(['id', 's1', 's2', 'p1', 'p2'], ''); |
|
272 | - $strings = array_filter($strings, 'is_array'); |
|
273 | - foreach ($strings as &$string) { |
|
271 | + $defaultString = array_fill_keys( ['id', 's1', 's2', 'p1', 'p2'], '' ); |
|
272 | + $strings = array_filter( $strings, 'is_array' ); |
|
273 | + foreach( $strings as &$string ) { |
|
274 | 274 | $string['type'] = isset($string['p1']) ? 'plural' : 'single'; |
275 | - $string = wp_parse_args($string, $defaultString); |
|
275 | + $string = wp_parse_args( $string, $defaultString ); |
|
276 | 276 | } |
277 | - return array_filter($strings, function ($string) { |
|
277 | + return array_filter( $strings, function( $string ) { |
|
278 | 278 | return !empty($string['id']); |
279 | 279 | }); |
280 | 280 | } |
@@ -9,433 +9,433 @@ |
||
9 | 9 | |
10 | 10 | class Console |
11 | 11 | { |
12 | - const DEBUG = 0; // Detailed debug information |
|
13 | - const INFO = 1; // Interesting events |
|
14 | - const NOTICE = 2; // Normal but significant events |
|
15 | - const WARNING = 4; // Exceptional occurrences that are not errors |
|
16 | - const ERROR = 8; // Runtime errors that do not require immediate action |
|
17 | - const CRITICAL = 16; // Critical conditions |
|
18 | - const ALERT = 32; // Action must be taken immediately |
|
19 | - const EMERGENCY = 64; // System is unusable |
|
20 | - |
|
21 | - protected $file; |
|
22 | - protected $log; |
|
23 | - protected $logOnceKey = 'glsr_log_once'; |
|
24 | - |
|
25 | - public function __construct() |
|
26 | - { |
|
27 | - $this->file = glsr()->path('console.log'); |
|
28 | - $this->log = file_exists($this->file) |
|
29 | - ? file_get_contents($this->file) |
|
30 | - : ''; |
|
31 | - $this->reset(); |
|
32 | - } |
|
33 | - |
|
34 | - /** |
|
35 | - * @return string |
|
36 | - */ |
|
37 | - public function __toString() |
|
38 | - { |
|
39 | - return $this->get(); |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Action must be taken immediately |
|
44 | - * Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up. |
|
45 | - * @param mixed $message |
|
46 | - * @param array $context |
|
47 | - * @return static |
|
48 | - */ |
|
49 | - public function alert($message, array $context = []) |
|
50 | - { |
|
51 | - return $this->log(static::ALERT, $message, $context); |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @return void |
|
56 | - */ |
|
57 | - public function clear() |
|
58 | - { |
|
59 | - $this->log = ''; |
|
60 | - file_put_contents($this->file, $this->log); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Critical conditions |
|
65 | - * Example: Application component unavailable, unexpected exception. |
|
66 | - * @param mixed $message |
|
67 | - * @param array $context |
|
68 | - * @return static |
|
69 | - */ |
|
70 | - public function critical($message, array $context = []) |
|
71 | - { |
|
72 | - return $this->log(static::CRITICAL, $message, $context); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Detailed debug information. |
|
77 | - * @param mixed $message |
|
78 | - * @param array $context |
|
79 | - * @return static |
|
80 | - */ |
|
81 | - public function debug($message, array $context = []) |
|
82 | - { |
|
83 | - return $this->log(static::DEBUG, $message, $context); |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * System is unusable. |
|
88 | - * @param mixed $message |
|
89 | - * @param array $context |
|
90 | - * @return static |
|
91 | - */ |
|
92 | - public function emergency($message, array $context = []) |
|
93 | - { |
|
94 | - return $this->log(static::EMERGENCY, $message, $context); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Runtime errors that do not require immediate action but should typically be logged and monitored. |
|
99 | - * @param mixed $message |
|
100 | - * @param array $context |
|
101 | - * @return static |
|
102 | - */ |
|
103 | - public function error($message, array $context = []) |
|
104 | - { |
|
105 | - return $this->log(static::ERROR, $message, $context); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * @return string |
|
110 | - */ |
|
111 | - public function get() |
|
112 | - { |
|
113 | - return empty($this->log) |
|
114 | - ? __('Console is empty', 'site-reviews') |
|
115 | - : $this->log; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @return int |
|
120 | - */ |
|
121 | - public function getLevel() |
|
122 | - { |
|
123 | - return intval(apply_filters('site-reviews/console/level', static::INFO)); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @return array |
|
128 | - */ |
|
129 | - public function getLevels() |
|
130 | - { |
|
131 | - $constants = (new ReflectionClass(__CLASS__))->getConstants(); |
|
132 | - return array_map('strtolower', array_flip($constants)); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * @return string |
|
137 | - */ |
|
138 | - public function humanLevel() |
|
139 | - { |
|
140 | - $level = $this->getLevel(); |
|
141 | - return sprintf('%s (%d)', strtoupper(glsr_get($this->getLevels(), $level, 'unknown')), $level); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * @param string|null $valueIfEmpty |
|
146 | - * @return string |
|
147 | - */ |
|
148 | - public function humanSize($valueIfEmpty = null) |
|
149 | - { |
|
150 | - $bytes = $this->size(); |
|
151 | - if (empty($bytes) && is_string($valueIfEmpty)) { |
|
152 | - return $valueIfEmpty; |
|
153 | - } |
|
154 | - $exponent = floor(log(max($bytes, 1), 1024)); |
|
155 | - return round($bytes / pow(1024, $exponent), 2).' '.['bytes', 'KB', 'MB', 'GB'][$exponent]; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Interesting events |
|
160 | - * Example: User logs in, SQL logs. |
|
161 | - * @param mixed $message |
|
162 | - * @param array $context |
|
163 | - * @return static |
|
164 | - */ |
|
165 | - public function info($message, array $context = []) |
|
166 | - { |
|
167 | - return $this->log(static::INFO, $message, $context); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * @param int $level |
|
172 | - * @param mixed $message |
|
173 | - * @param array $context |
|
174 | - * @param string $backtraceLine |
|
175 | - * @return static |
|
176 | - */ |
|
177 | - public function log($level, $message, $context = [], $backtraceLine = '') |
|
178 | - { |
|
179 | - if (empty($backtraceLine)) { |
|
180 | - $backtraceLine = $this->getBacktraceLine(); |
|
181 | - } |
|
182 | - if ($this->canLogEntry($level, $backtraceLine)) { |
|
183 | - $levelName = glsr_get($this->getLevels(), $level); |
|
184 | - $context = glsr(Helper::class)->consolidateArray($context); |
|
185 | - $backtraceLine = $this->normalizeBacktraceLine($backtraceLine); |
|
186 | - $message = $this->interpolate($message, $context); |
|
187 | - $entry = $this->buildLogEntry($levelName, $message, $backtraceLine); |
|
188 | - file_put_contents($this->file, $entry.PHP_EOL, FILE_APPEND | LOCK_EX); |
|
189 | - apply_filters('console', $message, $levelName, $backtraceLine); // Show in Blackbar plugin if installed |
|
190 | - $this->reset(); |
|
191 | - } |
|
192 | - return $this; |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * @return void |
|
197 | - */ |
|
198 | - public function logOnce() |
|
199 | - { |
|
200 | - $once = glsr(Helper::class)->consolidateArray(glsr()->{$this->logOnceKey}); |
|
201 | - $levels = $this->getLevels(); |
|
202 | - foreach ($once as $entry) { |
|
203 | - $levelName = glsr_get($entry, 'level'); |
|
204 | - if (!in_array($levelName, $levels)) { |
|
205 | - continue; |
|
206 | - } |
|
207 | - $level = glsr_get(array_flip($levels), $levelName); |
|
208 | - $message = glsr_get($entry, 'message'); |
|
209 | - $backtraceLine = glsr_get($entry, 'backtrace'); |
|
210 | - $this->log($level, $message, [], $backtraceLine); |
|
211 | - } |
|
212 | - glsr()->{$this->logOnceKey} = []; |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * Normal but significant events. |
|
217 | - * @param mixed $message |
|
218 | - * @param array $context |
|
219 | - * @return static |
|
220 | - */ |
|
221 | - public function notice($message, array $context = []) |
|
222 | - { |
|
223 | - return $this->log(static::NOTICE, $message, $context); |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * @param string $levelName |
|
228 | - * @param string $handle |
|
229 | - * @param mixed $data |
|
230 | - * @return void |
|
231 | - */ |
|
232 | - public function once($levelName, $handle, $data) |
|
233 | - { |
|
234 | - $once = glsr(Helper::class)->consolidateArray(glsr()->{$this->logOnceKey}); |
|
235 | - $filtered = array_filter($once, function ($entry) use ($levelName, $handle) { |
|
236 | - return glsr_get($entry, 'level') == $levelName |
|
237 | - && glsr_get($entry, 'handle') == $handle; |
|
238 | - }); |
|
239 | - if (!empty($filtered)) { |
|
240 | - return; |
|
241 | - } |
|
242 | - $once[] = [ |
|
243 | - 'backtrace' => $this->getBacktraceLineFromData($data), |
|
244 | - 'handle' => $handle, |
|
245 | - 'level' => $levelName, |
|
246 | - 'message' => '[RECURRING] '.$this->getMessageFromData($data), |
|
247 | - ]; |
|
248 | - glsr()->{$this->logOnceKey} = $once; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * @return int |
|
253 | - */ |
|
254 | - public function size() |
|
255 | - { |
|
256 | - return file_exists($this->file) |
|
257 | - ? filesize($this->file) |
|
258 | - : 0; |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * Exceptional occurrences that are not errors |
|
263 | - * Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong. |
|
264 | - * @param mixed $message |
|
265 | - * @param array $context |
|
266 | - * @return static |
|
267 | - */ |
|
268 | - public function warning($message, array $context = []) |
|
269 | - { |
|
270 | - return $this->log(static::WARNING, $message, $context); |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * @param array $backtrace |
|
275 | - * @param int $index |
|
276 | - * @return string |
|
277 | - */ |
|
278 | - protected function buildBacktraceLine($backtrace, $index) |
|
279 | - { |
|
280 | - return sprintf('%s:%s', |
|
281 | - glsr_get($backtrace, $index.'.file'), // realpath |
|
282 | - glsr_get($backtrace, $index.'.line') |
|
283 | - ); |
|
284 | - } |
|
285 | - |
|
286 | - /** |
|
287 | - * @param string $levelName |
|
288 | - * @param mixed $message |
|
289 | - * @param string $backtraceLine |
|
290 | - * @return string |
|
291 | - */ |
|
292 | - protected function buildLogEntry($levelName, $message, $backtraceLine = '') |
|
293 | - { |
|
294 | - return sprintf('[%s] %s [%s] %s', |
|
295 | - current_time('mysql'), |
|
296 | - strtoupper($levelName), |
|
297 | - $backtraceLine, |
|
298 | - $message |
|
299 | - ); |
|
300 | - } |
|
301 | - |
|
302 | - /** |
|
303 | - * @param int $level |
|
304 | - * @return bool |
|
305 | - */ |
|
306 | - protected function canLogEntry($level, $backtraceLine) |
|
307 | - { |
|
308 | - $levelExists = array_key_exists($level, $this->getLevels()); |
|
309 | - if (false === strpos($backtraceLine, glsr()->path())) { |
|
310 | - return $levelExists; // ignore level restriction if triggered outside of the plugin |
|
311 | - } |
|
312 | - return $levelExists && $level >= $this->getLevel(); |
|
313 | - } |
|
314 | - |
|
315 | - /** |
|
316 | - * @return void|string |
|
317 | - */ |
|
318 | - protected function getBacktraceLine() |
|
319 | - { |
|
320 | - $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 6); |
|
321 | - $search = array_search('log', glsr_array_column($backtrace, 'function')); |
|
322 | - if (false !== $search) { |
|
323 | - $index = '{closure}' == glsr_get($backtrace, ($search + 2).'.function') |
|
324 | - ? $search + 4 |
|
325 | - : $search + 1; |
|
326 | - return $this->buildBacktraceLine($backtrace, $index); |
|
327 | - } |
|
328 | - return 'Unknown'; |
|
329 | - } |
|
330 | - |
|
331 | - /** |
|
332 | - * @param mixed $data |
|
333 | - * @return string |
|
334 | - */ |
|
335 | - protected function getBacktraceLineFromData($data) |
|
336 | - { |
|
337 | - $backtrace = $data instanceof Throwable |
|
338 | - ? $data->getTrace() |
|
339 | - : debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
|
340 | - return $this->buildBacktraceLine($backtrace, 0); |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * @param mixed $data |
|
345 | - * @return string |
|
346 | - */ |
|
347 | - protected function getMessageFromData($data) |
|
348 | - { |
|
349 | - return $data instanceof Throwable |
|
350 | - ? $this->normalizeThrowableMessage($data->getMessage()) |
|
351 | - : print_r($data, 1); |
|
352 | - } |
|
353 | - |
|
354 | - /** |
|
355 | - * Interpolates context values into the message placeholders. |
|
356 | - * @param mixed $message |
|
357 | - * @param array $context |
|
358 | - * @return string |
|
359 | - */ |
|
360 | - protected function interpolate($message, $context = []) |
|
361 | - { |
|
362 | - if ($this->isObjectOrArray($message) || !is_array($context)) { |
|
363 | - return print_r($message, true); |
|
364 | - } |
|
365 | - $replace = []; |
|
366 | - foreach ($context as $key => $value) { |
|
367 | - $replace['{'.$key.'}'] = $this->normalizeValue($value); |
|
368 | - } |
|
369 | - return strtr($message, $replace); |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * @param mixed $value |
|
374 | - * @return bool |
|
375 | - */ |
|
376 | - protected function isObjectOrArray($value) |
|
377 | - { |
|
378 | - return is_object($value) || is_array($value); |
|
379 | - } |
|
380 | - |
|
381 | - /** |
|
382 | - * @param string $backtraceLine |
|
383 | - * @return string |
|
384 | - */ |
|
385 | - protected function normalizeBacktraceLine($backtraceLine) |
|
386 | - { |
|
387 | - $search = [ |
|
388 | - glsr()->path('plugin/'), |
|
389 | - glsr()->path('plugin/', false), |
|
390 | - trailingslashit(glsr()->path()), |
|
391 | - trailingslashit(glsr()->path('', false)), |
|
392 | - WP_CONTENT_DIR, |
|
393 | - ABSPATH, |
|
394 | - ]; |
|
395 | - return str_replace(array_unique($search), '', $backtraceLine); |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * @param string $message |
|
400 | - * @return string |
|
401 | - */ |
|
402 | - protected function normalizeThrowableMessage($message) |
|
403 | - { |
|
404 | - $calledIn = strpos($message, ', called in'); |
|
405 | - return false !== $calledIn |
|
406 | - ? substr($message, 0, $calledIn) |
|
407 | - : $message; |
|
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * @param mixed $value |
|
412 | - * @return string |
|
413 | - */ |
|
414 | - protected function normalizeValue($value) |
|
415 | - { |
|
416 | - if ($value instanceof DateTime) { |
|
417 | - $value = $value->format('Y-m-d H:i:s'); |
|
418 | - } elseif ($this->isObjectOrArray($value)) { |
|
419 | - $value = json_encode($value); |
|
420 | - } |
|
421 | - return (string) $value; |
|
422 | - } |
|
423 | - |
|
424 | - /** |
|
425 | - * @return void |
|
426 | - */ |
|
427 | - protected function reset() |
|
428 | - { |
|
429 | - if ($this->size() <= pow(1024, 2) / 8) { |
|
430 | - return; |
|
431 | - } |
|
432 | - $this->clear(); |
|
433 | - file_put_contents( |
|
434 | - $this->file, |
|
435 | - $this->buildLogEntry( |
|
436 | - static::NOTICE, |
|
437 | - __('Console was automatically cleared (128 KB maximum size)', 'site-reviews') |
|
438 | - ) |
|
439 | - ); |
|
440 | - } |
|
12 | + const DEBUG = 0; // Detailed debug information |
|
13 | + const INFO = 1; // Interesting events |
|
14 | + const NOTICE = 2; // Normal but significant events |
|
15 | + const WARNING = 4; // Exceptional occurrences that are not errors |
|
16 | + const ERROR = 8; // Runtime errors that do not require immediate action |
|
17 | + const CRITICAL = 16; // Critical conditions |
|
18 | + const ALERT = 32; // Action must be taken immediately |
|
19 | + const EMERGENCY = 64; // System is unusable |
|
20 | + |
|
21 | + protected $file; |
|
22 | + protected $log; |
|
23 | + protected $logOnceKey = 'glsr_log_once'; |
|
24 | + |
|
25 | + public function __construct() |
|
26 | + { |
|
27 | + $this->file = glsr()->path('console.log'); |
|
28 | + $this->log = file_exists($this->file) |
|
29 | + ? file_get_contents($this->file) |
|
30 | + : ''; |
|
31 | + $this->reset(); |
|
32 | + } |
|
33 | + |
|
34 | + /** |
|
35 | + * @return string |
|
36 | + */ |
|
37 | + public function __toString() |
|
38 | + { |
|
39 | + return $this->get(); |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Action must be taken immediately |
|
44 | + * Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up. |
|
45 | + * @param mixed $message |
|
46 | + * @param array $context |
|
47 | + * @return static |
|
48 | + */ |
|
49 | + public function alert($message, array $context = []) |
|
50 | + { |
|
51 | + return $this->log(static::ALERT, $message, $context); |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @return void |
|
56 | + */ |
|
57 | + public function clear() |
|
58 | + { |
|
59 | + $this->log = ''; |
|
60 | + file_put_contents($this->file, $this->log); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Critical conditions |
|
65 | + * Example: Application component unavailable, unexpected exception. |
|
66 | + * @param mixed $message |
|
67 | + * @param array $context |
|
68 | + * @return static |
|
69 | + */ |
|
70 | + public function critical($message, array $context = []) |
|
71 | + { |
|
72 | + return $this->log(static::CRITICAL, $message, $context); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Detailed debug information. |
|
77 | + * @param mixed $message |
|
78 | + * @param array $context |
|
79 | + * @return static |
|
80 | + */ |
|
81 | + public function debug($message, array $context = []) |
|
82 | + { |
|
83 | + return $this->log(static::DEBUG, $message, $context); |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * System is unusable. |
|
88 | + * @param mixed $message |
|
89 | + * @param array $context |
|
90 | + * @return static |
|
91 | + */ |
|
92 | + public function emergency($message, array $context = []) |
|
93 | + { |
|
94 | + return $this->log(static::EMERGENCY, $message, $context); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Runtime errors that do not require immediate action but should typically be logged and monitored. |
|
99 | + * @param mixed $message |
|
100 | + * @param array $context |
|
101 | + * @return static |
|
102 | + */ |
|
103 | + public function error($message, array $context = []) |
|
104 | + { |
|
105 | + return $this->log(static::ERROR, $message, $context); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * @return string |
|
110 | + */ |
|
111 | + public function get() |
|
112 | + { |
|
113 | + return empty($this->log) |
|
114 | + ? __('Console is empty', 'site-reviews') |
|
115 | + : $this->log; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @return int |
|
120 | + */ |
|
121 | + public function getLevel() |
|
122 | + { |
|
123 | + return intval(apply_filters('site-reviews/console/level', static::INFO)); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @return array |
|
128 | + */ |
|
129 | + public function getLevels() |
|
130 | + { |
|
131 | + $constants = (new ReflectionClass(__CLASS__))->getConstants(); |
|
132 | + return array_map('strtolower', array_flip($constants)); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * @return string |
|
137 | + */ |
|
138 | + public function humanLevel() |
|
139 | + { |
|
140 | + $level = $this->getLevel(); |
|
141 | + return sprintf('%s (%d)', strtoupper(glsr_get($this->getLevels(), $level, 'unknown')), $level); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * @param string|null $valueIfEmpty |
|
146 | + * @return string |
|
147 | + */ |
|
148 | + public function humanSize($valueIfEmpty = null) |
|
149 | + { |
|
150 | + $bytes = $this->size(); |
|
151 | + if (empty($bytes) && is_string($valueIfEmpty)) { |
|
152 | + return $valueIfEmpty; |
|
153 | + } |
|
154 | + $exponent = floor(log(max($bytes, 1), 1024)); |
|
155 | + return round($bytes / pow(1024, $exponent), 2).' '.['bytes', 'KB', 'MB', 'GB'][$exponent]; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Interesting events |
|
160 | + * Example: User logs in, SQL logs. |
|
161 | + * @param mixed $message |
|
162 | + * @param array $context |
|
163 | + * @return static |
|
164 | + */ |
|
165 | + public function info($message, array $context = []) |
|
166 | + { |
|
167 | + return $this->log(static::INFO, $message, $context); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * @param int $level |
|
172 | + * @param mixed $message |
|
173 | + * @param array $context |
|
174 | + * @param string $backtraceLine |
|
175 | + * @return static |
|
176 | + */ |
|
177 | + public function log($level, $message, $context = [], $backtraceLine = '') |
|
178 | + { |
|
179 | + if (empty($backtraceLine)) { |
|
180 | + $backtraceLine = $this->getBacktraceLine(); |
|
181 | + } |
|
182 | + if ($this->canLogEntry($level, $backtraceLine)) { |
|
183 | + $levelName = glsr_get($this->getLevels(), $level); |
|
184 | + $context = glsr(Helper::class)->consolidateArray($context); |
|
185 | + $backtraceLine = $this->normalizeBacktraceLine($backtraceLine); |
|
186 | + $message = $this->interpolate($message, $context); |
|
187 | + $entry = $this->buildLogEntry($levelName, $message, $backtraceLine); |
|
188 | + file_put_contents($this->file, $entry.PHP_EOL, FILE_APPEND | LOCK_EX); |
|
189 | + apply_filters('console', $message, $levelName, $backtraceLine); // Show in Blackbar plugin if installed |
|
190 | + $this->reset(); |
|
191 | + } |
|
192 | + return $this; |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * @return void |
|
197 | + */ |
|
198 | + public function logOnce() |
|
199 | + { |
|
200 | + $once = glsr(Helper::class)->consolidateArray(glsr()->{$this->logOnceKey}); |
|
201 | + $levels = $this->getLevels(); |
|
202 | + foreach ($once as $entry) { |
|
203 | + $levelName = glsr_get($entry, 'level'); |
|
204 | + if (!in_array($levelName, $levels)) { |
|
205 | + continue; |
|
206 | + } |
|
207 | + $level = glsr_get(array_flip($levels), $levelName); |
|
208 | + $message = glsr_get($entry, 'message'); |
|
209 | + $backtraceLine = glsr_get($entry, 'backtrace'); |
|
210 | + $this->log($level, $message, [], $backtraceLine); |
|
211 | + } |
|
212 | + glsr()->{$this->logOnceKey} = []; |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * Normal but significant events. |
|
217 | + * @param mixed $message |
|
218 | + * @param array $context |
|
219 | + * @return static |
|
220 | + */ |
|
221 | + public function notice($message, array $context = []) |
|
222 | + { |
|
223 | + return $this->log(static::NOTICE, $message, $context); |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * @param string $levelName |
|
228 | + * @param string $handle |
|
229 | + * @param mixed $data |
|
230 | + * @return void |
|
231 | + */ |
|
232 | + public function once($levelName, $handle, $data) |
|
233 | + { |
|
234 | + $once = glsr(Helper::class)->consolidateArray(glsr()->{$this->logOnceKey}); |
|
235 | + $filtered = array_filter($once, function ($entry) use ($levelName, $handle) { |
|
236 | + return glsr_get($entry, 'level') == $levelName |
|
237 | + && glsr_get($entry, 'handle') == $handle; |
|
238 | + }); |
|
239 | + if (!empty($filtered)) { |
|
240 | + return; |
|
241 | + } |
|
242 | + $once[] = [ |
|
243 | + 'backtrace' => $this->getBacktraceLineFromData($data), |
|
244 | + 'handle' => $handle, |
|
245 | + 'level' => $levelName, |
|
246 | + 'message' => '[RECURRING] '.$this->getMessageFromData($data), |
|
247 | + ]; |
|
248 | + glsr()->{$this->logOnceKey} = $once; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * @return int |
|
253 | + */ |
|
254 | + public function size() |
|
255 | + { |
|
256 | + return file_exists($this->file) |
|
257 | + ? filesize($this->file) |
|
258 | + : 0; |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * Exceptional occurrences that are not errors |
|
263 | + * Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong. |
|
264 | + * @param mixed $message |
|
265 | + * @param array $context |
|
266 | + * @return static |
|
267 | + */ |
|
268 | + public function warning($message, array $context = []) |
|
269 | + { |
|
270 | + return $this->log(static::WARNING, $message, $context); |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * @param array $backtrace |
|
275 | + * @param int $index |
|
276 | + * @return string |
|
277 | + */ |
|
278 | + protected function buildBacktraceLine($backtrace, $index) |
|
279 | + { |
|
280 | + return sprintf('%s:%s', |
|
281 | + glsr_get($backtrace, $index.'.file'), // realpath |
|
282 | + glsr_get($backtrace, $index.'.line') |
|
283 | + ); |
|
284 | + } |
|
285 | + |
|
286 | + /** |
|
287 | + * @param string $levelName |
|
288 | + * @param mixed $message |
|
289 | + * @param string $backtraceLine |
|
290 | + * @return string |
|
291 | + */ |
|
292 | + protected function buildLogEntry($levelName, $message, $backtraceLine = '') |
|
293 | + { |
|
294 | + return sprintf('[%s] %s [%s] %s', |
|
295 | + current_time('mysql'), |
|
296 | + strtoupper($levelName), |
|
297 | + $backtraceLine, |
|
298 | + $message |
|
299 | + ); |
|
300 | + } |
|
301 | + |
|
302 | + /** |
|
303 | + * @param int $level |
|
304 | + * @return bool |
|
305 | + */ |
|
306 | + protected function canLogEntry($level, $backtraceLine) |
|
307 | + { |
|
308 | + $levelExists = array_key_exists($level, $this->getLevels()); |
|
309 | + if (false === strpos($backtraceLine, glsr()->path())) { |
|
310 | + return $levelExists; // ignore level restriction if triggered outside of the plugin |
|
311 | + } |
|
312 | + return $levelExists && $level >= $this->getLevel(); |
|
313 | + } |
|
314 | + |
|
315 | + /** |
|
316 | + * @return void|string |
|
317 | + */ |
|
318 | + protected function getBacktraceLine() |
|
319 | + { |
|
320 | + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 6); |
|
321 | + $search = array_search('log', glsr_array_column($backtrace, 'function')); |
|
322 | + if (false !== $search) { |
|
323 | + $index = '{closure}' == glsr_get($backtrace, ($search + 2).'.function') |
|
324 | + ? $search + 4 |
|
325 | + : $search + 1; |
|
326 | + return $this->buildBacktraceLine($backtrace, $index); |
|
327 | + } |
|
328 | + return 'Unknown'; |
|
329 | + } |
|
330 | + |
|
331 | + /** |
|
332 | + * @param mixed $data |
|
333 | + * @return string |
|
334 | + */ |
|
335 | + protected function getBacktraceLineFromData($data) |
|
336 | + { |
|
337 | + $backtrace = $data instanceof Throwable |
|
338 | + ? $data->getTrace() |
|
339 | + : debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
|
340 | + return $this->buildBacktraceLine($backtrace, 0); |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * @param mixed $data |
|
345 | + * @return string |
|
346 | + */ |
|
347 | + protected function getMessageFromData($data) |
|
348 | + { |
|
349 | + return $data instanceof Throwable |
|
350 | + ? $this->normalizeThrowableMessage($data->getMessage()) |
|
351 | + : print_r($data, 1); |
|
352 | + } |
|
353 | + |
|
354 | + /** |
|
355 | + * Interpolates context values into the message placeholders. |
|
356 | + * @param mixed $message |
|
357 | + * @param array $context |
|
358 | + * @return string |
|
359 | + */ |
|
360 | + protected function interpolate($message, $context = []) |
|
361 | + { |
|
362 | + if ($this->isObjectOrArray($message) || !is_array($context)) { |
|
363 | + return print_r($message, true); |
|
364 | + } |
|
365 | + $replace = []; |
|
366 | + foreach ($context as $key => $value) { |
|
367 | + $replace['{'.$key.'}'] = $this->normalizeValue($value); |
|
368 | + } |
|
369 | + return strtr($message, $replace); |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * @param mixed $value |
|
374 | + * @return bool |
|
375 | + */ |
|
376 | + protected function isObjectOrArray($value) |
|
377 | + { |
|
378 | + return is_object($value) || is_array($value); |
|
379 | + } |
|
380 | + |
|
381 | + /** |
|
382 | + * @param string $backtraceLine |
|
383 | + * @return string |
|
384 | + */ |
|
385 | + protected function normalizeBacktraceLine($backtraceLine) |
|
386 | + { |
|
387 | + $search = [ |
|
388 | + glsr()->path('plugin/'), |
|
389 | + glsr()->path('plugin/', false), |
|
390 | + trailingslashit(glsr()->path()), |
|
391 | + trailingslashit(glsr()->path('', false)), |
|
392 | + WP_CONTENT_DIR, |
|
393 | + ABSPATH, |
|
394 | + ]; |
|
395 | + return str_replace(array_unique($search), '', $backtraceLine); |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * @param string $message |
|
400 | + * @return string |
|
401 | + */ |
|
402 | + protected function normalizeThrowableMessage($message) |
|
403 | + { |
|
404 | + $calledIn = strpos($message, ', called in'); |
|
405 | + return false !== $calledIn |
|
406 | + ? substr($message, 0, $calledIn) |
|
407 | + : $message; |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * @param mixed $value |
|
412 | + * @return string |
|
413 | + */ |
|
414 | + protected function normalizeValue($value) |
|
415 | + { |
|
416 | + if ($value instanceof DateTime) { |
|
417 | + $value = $value->format('Y-m-d H:i:s'); |
|
418 | + } elseif ($this->isObjectOrArray($value)) { |
|
419 | + $value = json_encode($value); |
|
420 | + } |
|
421 | + return (string) $value; |
|
422 | + } |
|
423 | + |
|
424 | + /** |
|
425 | + * @return void |
|
426 | + */ |
|
427 | + protected function reset() |
|
428 | + { |
|
429 | + if ($this->size() <= pow(1024, 2) / 8) { |
|
430 | + return; |
|
431 | + } |
|
432 | + $this->clear(); |
|
433 | + file_put_contents( |
|
434 | + $this->file, |
|
435 | + $this->buildLogEntry( |
|
436 | + static::NOTICE, |
|
437 | + __('Console was automatically cleared (128 KB maximum size)', 'site-reviews') |
|
438 | + ) |
|
439 | + ); |
|
440 | + } |
|
441 | 441 | } |
@@ -9,13 +9,13 @@ discard block |
||
9 | 9 | |
10 | 10 | class Console |
11 | 11 | { |
12 | - const DEBUG = 0; // Detailed debug information |
|
13 | - const INFO = 1; // Interesting events |
|
14 | - const NOTICE = 2; // Normal but significant events |
|
15 | - const WARNING = 4; // Exceptional occurrences that are not errors |
|
16 | - const ERROR = 8; // Runtime errors that do not require immediate action |
|
17 | - const CRITICAL = 16; // Critical conditions |
|
18 | - const ALERT = 32; // Action must be taken immediately |
|
12 | + const DEBUG = 0; // Detailed debug information |
|
13 | + const INFO = 1; // Interesting events |
|
14 | + const NOTICE = 2; // Normal but significant events |
|
15 | + const WARNING = 4; // Exceptional occurrences that are not errors |
|
16 | + const ERROR = 8; // Runtime errors that do not require immediate action |
|
17 | + const CRITICAL = 16; // Critical conditions |
|
18 | + const ALERT = 32; // Action must be taken immediately |
|
19 | 19 | const EMERGENCY = 64; // System is unusable |
20 | 20 | |
21 | 21 | protected $file; |
@@ -24,9 +24,9 @@ discard block |
||
24 | 24 | |
25 | 25 | public function __construct() |
26 | 26 | { |
27 | - $this->file = glsr()->path('console.log'); |
|
28 | - $this->log = file_exists($this->file) |
|
29 | - ? file_get_contents($this->file) |
|
27 | + $this->file = glsr()->path( 'console.log' ); |
|
28 | + $this->log = file_exists( $this->file ) |
|
29 | + ? file_get_contents( $this->file ) |
|
30 | 30 | : ''; |
31 | 31 | $this->reset(); |
32 | 32 | } |
@@ -46,9 +46,9 @@ discard block |
||
46 | 46 | * @param array $context |
47 | 47 | * @return static |
48 | 48 | */ |
49 | - public function alert($message, array $context = []) |
|
49 | + public function alert( $message, array $context = [] ) |
|
50 | 50 | { |
51 | - return $this->log(static::ALERT, $message, $context); |
|
51 | + return $this->log( static::ALERT, $message, $context ); |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | /** |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | public function clear() |
58 | 58 | { |
59 | 59 | $this->log = ''; |
60 | - file_put_contents($this->file, $this->log); |
|
60 | + file_put_contents( $this->file, $this->log ); |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | /** |
@@ -67,9 +67,9 @@ discard block |
||
67 | 67 | * @param array $context |
68 | 68 | * @return static |
69 | 69 | */ |
70 | - public function critical($message, array $context = []) |
|
70 | + public function critical( $message, array $context = [] ) |
|
71 | 71 | { |
72 | - return $this->log(static::CRITICAL, $message, $context); |
|
72 | + return $this->log( static::CRITICAL, $message, $context ); |
|
73 | 73 | } |
74 | 74 | |
75 | 75 | /** |
@@ -78,9 +78,9 @@ discard block |
||
78 | 78 | * @param array $context |
79 | 79 | * @return static |
80 | 80 | */ |
81 | - public function debug($message, array $context = []) |
|
81 | + public function debug( $message, array $context = [] ) |
|
82 | 82 | { |
83 | - return $this->log(static::DEBUG, $message, $context); |
|
83 | + return $this->log( static::DEBUG, $message, $context ); |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | /** |
@@ -89,9 +89,9 @@ discard block |
||
89 | 89 | * @param array $context |
90 | 90 | * @return static |
91 | 91 | */ |
92 | - public function emergency($message, array $context = []) |
|
92 | + public function emergency( $message, array $context = [] ) |
|
93 | 93 | { |
94 | - return $this->log(static::EMERGENCY, $message, $context); |
|
94 | + return $this->log( static::EMERGENCY, $message, $context ); |
|
95 | 95 | } |
96 | 96 | |
97 | 97 | /** |
@@ -100,9 +100,9 @@ discard block |
||
100 | 100 | * @param array $context |
101 | 101 | * @return static |
102 | 102 | */ |
103 | - public function error($message, array $context = []) |
|
103 | + public function error( $message, array $context = [] ) |
|
104 | 104 | { |
105 | - return $this->log(static::ERROR, $message, $context); |
|
105 | + return $this->log( static::ERROR, $message, $context ); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | /** |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | public function get() |
112 | 112 | { |
113 | 113 | return empty($this->log) |
114 | - ? __('Console is empty', 'site-reviews') |
|
114 | + ? __( 'Console is empty', 'site-reviews' ) |
|
115 | 115 | : $this->log; |
116 | 116 | } |
117 | 117 | |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | */ |
121 | 121 | public function getLevel() |
122 | 122 | { |
123 | - return intval(apply_filters('site-reviews/console/level', static::INFO)); |
|
123 | + return intval( apply_filters( 'site-reviews/console/level', static::INFO ) ); |
|
124 | 124 | } |
125 | 125 | |
126 | 126 | /** |
@@ -128,8 +128,8 @@ discard block |
||
128 | 128 | */ |
129 | 129 | public function getLevels() |
130 | 130 | { |
131 | - $constants = (new ReflectionClass(__CLASS__))->getConstants(); |
|
132 | - return array_map('strtolower', array_flip($constants)); |
|
131 | + $constants = (new ReflectionClass( __CLASS__ ))->getConstants(); |
|
132 | + return array_map( 'strtolower', array_flip( $constants ) ); |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | /** |
@@ -138,21 +138,21 @@ discard block |
||
138 | 138 | public function humanLevel() |
139 | 139 | { |
140 | 140 | $level = $this->getLevel(); |
141 | - return sprintf('%s (%d)', strtoupper(glsr_get($this->getLevels(), $level, 'unknown')), $level); |
|
141 | + return sprintf( '%s (%d)', strtoupper( glsr_get( $this->getLevels(), $level, 'unknown' ) ), $level ); |
|
142 | 142 | } |
143 | 143 | |
144 | 144 | /** |
145 | 145 | * @param string|null $valueIfEmpty |
146 | 146 | * @return string |
147 | 147 | */ |
148 | - public function humanSize($valueIfEmpty = null) |
|
148 | + public function humanSize( $valueIfEmpty = null ) |
|
149 | 149 | { |
150 | 150 | $bytes = $this->size(); |
151 | - if (empty($bytes) && is_string($valueIfEmpty)) { |
|
151 | + if( empty($bytes) && is_string( $valueIfEmpty ) ) { |
|
152 | 152 | return $valueIfEmpty; |
153 | 153 | } |
154 | - $exponent = floor(log(max($bytes, 1), 1024)); |
|
155 | - return round($bytes / pow(1024, $exponent), 2).' '.['bytes', 'KB', 'MB', 'GB'][$exponent]; |
|
154 | + $exponent = floor( log( max( $bytes, 1 ), 1024 ) ); |
|
155 | + return round( $bytes / pow( 1024, $exponent ), 2 ).' '.['bytes', 'KB', 'MB', 'GB'][$exponent]; |
|
156 | 156 | } |
157 | 157 | |
158 | 158 | /** |
@@ -162,9 +162,9 @@ discard block |
||
162 | 162 | * @param array $context |
163 | 163 | * @return static |
164 | 164 | */ |
165 | - public function info($message, array $context = []) |
|
165 | + public function info( $message, array $context = [] ) |
|
166 | 166 | { |
167 | - return $this->log(static::INFO, $message, $context); |
|
167 | + return $this->log( static::INFO, $message, $context ); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | /** |
@@ -174,19 +174,19 @@ discard block |
||
174 | 174 | * @param string $backtraceLine |
175 | 175 | * @return static |
176 | 176 | */ |
177 | - public function log($level, $message, $context = [], $backtraceLine = '') |
|
177 | + public function log( $level, $message, $context = [], $backtraceLine = '' ) |
|
178 | 178 | { |
179 | - if (empty($backtraceLine)) { |
|
179 | + if( empty($backtraceLine) ) { |
|
180 | 180 | $backtraceLine = $this->getBacktraceLine(); |
181 | 181 | } |
182 | - if ($this->canLogEntry($level, $backtraceLine)) { |
|
183 | - $levelName = glsr_get($this->getLevels(), $level); |
|
184 | - $context = glsr(Helper::class)->consolidateArray($context); |
|
185 | - $backtraceLine = $this->normalizeBacktraceLine($backtraceLine); |
|
186 | - $message = $this->interpolate($message, $context); |
|
187 | - $entry = $this->buildLogEntry($levelName, $message, $backtraceLine); |
|
188 | - file_put_contents($this->file, $entry.PHP_EOL, FILE_APPEND | LOCK_EX); |
|
189 | - apply_filters('console', $message, $levelName, $backtraceLine); // Show in Blackbar plugin if installed |
|
182 | + if( $this->canLogEntry( $level, $backtraceLine ) ) { |
|
183 | + $levelName = glsr_get( $this->getLevels(), $level ); |
|
184 | + $context = glsr( Helper::class )->consolidateArray( $context ); |
|
185 | + $backtraceLine = $this->normalizeBacktraceLine( $backtraceLine ); |
|
186 | + $message = $this->interpolate( $message, $context ); |
|
187 | + $entry = $this->buildLogEntry( $levelName, $message, $backtraceLine ); |
|
188 | + file_put_contents( $this->file, $entry.PHP_EOL, FILE_APPEND | LOCK_EX ); |
|
189 | + apply_filters( 'console', $message, $levelName, $backtraceLine ); // Show in Blackbar plugin if installed |
|
190 | 190 | $this->reset(); |
191 | 191 | } |
192 | 192 | return $this; |
@@ -197,17 +197,17 @@ discard block |
||
197 | 197 | */ |
198 | 198 | public function logOnce() |
199 | 199 | { |
200 | - $once = glsr(Helper::class)->consolidateArray(glsr()->{$this->logOnceKey}); |
|
200 | + $once = glsr( Helper::class )->consolidateArray( glsr()->{$this->logOnceKey}); |
|
201 | 201 | $levels = $this->getLevels(); |
202 | - foreach ($once as $entry) { |
|
203 | - $levelName = glsr_get($entry, 'level'); |
|
204 | - if (!in_array($levelName, $levels)) { |
|
202 | + foreach( $once as $entry ) { |
|
203 | + $levelName = glsr_get( $entry, 'level' ); |
|
204 | + if( !in_array( $levelName, $levels ) ) { |
|
205 | 205 | continue; |
206 | 206 | } |
207 | - $level = glsr_get(array_flip($levels), $levelName); |
|
208 | - $message = glsr_get($entry, 'message'); |
|
209 | - $backtraceLine = glsr_get($entry, 'backtrace'); |
|
210 | - $this->log($level, $message, [], $backtraceLine); |
|
207 | + $level = glsr_get( array_flip( $levels ), $levelName ); |
|
208 | + $message = glsr_get( $entry, 'message' ); |
|
209 | + $backtraceLine = glsr_get( $entry, 'backtrace' ); |
|
210 | + $this->log( $level, $message, [], $backtraceLine ); |
|
211 | 211 | } |
212 | 212 | glsr()->{$this->logOnceKey} = []; |
213 | 213 | } |
@@ -218,9 +218,9 @@ discard block |
||
218 | 218 | * @param array $context |
219 | 219 | * @return static |
220 | 220 | */ |
221 | - public function notice($message, array $context = []) |
|
221 | + public function notice( $message, array $context = [] ) |
|
222 | 222 | { |
223 | - return $this->log(static::NOTICE, $message, $context); |
|
223 | + return $this->log( static::NOTICE, $message, $context ); |
|
224 | 224 | } |
225 | 225 | |
226 | 226 | /** |
@@ -229,21 +229,21 @@ discard block |
||
229 | 229 | * @param mixed $data |
230 | 230 | * @return void |
231 | 231 | */ |
232 | - public function once($levelName, $handle, $data) |
|
232 | + public function once( $levelName, $handle, $data ) |
|
233 | 233 | { |
234 | - $once = glsr(Helper::class)->consolidateArray(glsr()->{$this->logOnceKey}); |
|
235 | - $filtered = array_filter($once, function ($entry) use ($levelName, $handle) { |
|
236 | - return glsr_get($entry, 'level') == $levelName |
|
237 | - && glsr_get($entry, 'handle') == $handle; |
|
234 | + $once = glsr( Helper::class )->consolidateArray( glsr()->{$this->logOnceKey}); |
|
235 | + $filtered = array_filter( $once, function( $entry ) use ($levelName, $handle) { |
|
236 | + return glsr_get( $entry, 'level' ) == $levelName |
|
237 | + && glsr_get( $entry, 'handle' ) == $handle; |
|
238 | 238 | }); |
239 | - if (!empty($filtered)) { |
|
239 | + if( !empty($filtered) ) { |
|
240 | 240 | return; |
241 | 241 | } |
242 | 242 | $once[] = [ |
243 | - 'backtrace' => $this->getBacktraceLineFromData($data), |
|
243 | + 'backtrace' => $this->getBacktraceLineFromData( $data ), |
|
244 | 244 | 'handle' => $handle, |
245 | 245 | 'level' => $levelName, |
246 | - 'message' => '[RECURRING] '.$this->getMessageFromData($data), |
|
246 | + 'message' => '[RECURRING] '.$this->getMessageFromData( $data ), |
|
247 | 247 | ]; |
248 | 248 | glsr()->{$this->logOnceKey} = $once; |
249 | 249 | } |
@@ -253,8 +253,8 @@ discard block |
||
253 | 253 | */ |
254 | 254 | public function size() |
255 | 255 | { |
256 | - return file_exists($this->file) |
|
257 | - ? filesize($this->file) |
|
256 | + return file_exists( $this->file ) |
|
257 | + ? filesize( $this->file ) |
|
258 | 258 | : 0; |
259 | 259 | } |
260 | 260 | |
@@ -265,9 +265,9 @@ discard block |
||
265 | 265 | * @param array $context |
266 | 266 | * @return static |
267 | 267 | */ |
268 | - public function warning($message, array $context = []) |
|
268 | + public function warning( $message, array $context = [] ) |
|
269 | 269 | { |
270 | - return $this->log(static::WARNING, $message, $context); |
|
270 | + return $this->log( static::WARNING, $message, $context ); |
|
271 | 271 | } |
272 | 272 | |
273 | 273 | /** |
@@ -275,11 +275,11 @@ discard block |
||
275 | 275 | * @param int $index |
276 | 276 | * @return string |
277 | 277 | */ |
278 | - protected function buildBacktraceLine($backtrace, $index) |
|
278 | + protected function buildBacktraceLine( $backtrace, $index ) |
|
279 | 279 | { |
280 | - return sprintf('%s:%s', |
|
281 | - glsr_get($backtrace, $index.'.file'), // realpath |
|
282 | - glsr_get($backtrace, $index.'.line') |
|
280 | + return sprintf( '%s:%s', |
|
281 | + glsr_get( $backtrace, $index.'.file' ), // realpath |
|
282 | + glsr_get( $backtrace, $index.'.line' ) |
|
283 | 283 | ); |
284 | 284 | } |
285 | 285 | |
@@ -289,11 +289,11 @@ discard block |
||
289 | 289 | * @param string $backtraceLine |
290 | 290 | * @return string |
291 | 291 | */ |
292 | - protected function buildLogEntry($levelName, $message, $backtraceLine = '') |
|
292 | + protected function buildLogEntry( $levelName, $message, $backtraceLine = '' ) |
|
293 | 293 | { |
294 | - return sprintf('[%s] %s [%s] %s', |
|
295 | - current_time('mysql'), |
|
296 | - strtoupper($levelName), |
|
294 | + return sprintf( '[%s] %s [%s] %s', |
|
295 | + current_time( 'mysql' ), |
|
296 | + strtoupper( $levelName ), |
|
297 | 297 | $backtraceLine, |
298 | 298 | $message |
299 | 299 | ); |
@@ -303,10 +303,10 @@ discard block |
||
303 | 303 | * @param int $level |
304 | 304 | * @return bool |
305 | 305 | */ |
306 | - protected function canLogEntry($level, $backtraceLine) |
|
306 | + protected function canLogEntry( $level, $backtraceLine ) |
|
307 | 307 | { |
308 | - $levelExists = array_key_exists($level, $this->getLevels()); |
|
309 | - if (false === strpos($backtraceLine, glsr()->path())) { |
|
308 | + $levelExists = array_key_exists( $level, $this->getLevels() ); |
|
309 | + if( false === strpos( $backtraceLine, glsr()->path() ) ) { |
|
310 | 310 | return $levelExists; // ignore level restriction if triggered outside of the plugin |
311 | 311 | } |
312 | 312 | return $levelExists && $level >= $this->getLevel(); |
@@ -317,13 +317,13 @@ discard block |
||
317 | 317 | */ |
318 | 318 | protected function getBacktraceLine() |
319 | 319 | { |
320 | - $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 6); |
|
321 | - $search = array_search('log', glsr_array_column($backtrace, 'function')); |
|
322 | - if (false !== $search) { |
|
323 | - $index = '{closure}' == glsr_get($backtrace, ($search + 2).'.function') |
|
320 | + $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 6 ); |
|
321 | + $search = array_search( 'log', glsr_array_column( $backtrace, 'function' ) ); |
|
322 | + if( false !== $search ) { |
|
323 | + $index = '{closure}' == glsr_get( $backtrace, ($search + 2).'.function' ) |
|
324 | 324 | ? $search + 4 |
325 | 325 | : $search + 1; |
326 | - return $this->buildBacktraceLine($backtrace, $index); |
|
326 | + return $this->buildBacktraceLine( $backtrace, $index ); |
|
327 | 327 | } |
328 | 328 | return 'Unknown'; |
329 | 329 | } |
@@ -332,23 +332,23 @@ discard block |
||
332 | 332 | * @param mixed $data |
333 | 333 | * @return string |
334 | 334 | */ |
335 | - protected function getBacktraceLineFromData($data) |
|
335 | + protected function getBacktraceLineFromData( $data ) |
|
336 | 336 | { |
337 | 337 | $backtrace = $data instanceof Throwable |
338 | 338 | ? $data->getTrace() |
339 | - : debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
|
340 | - return $this->buildBacktraceLine($backtrace, 0); |
|
339 | + : debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 1 ); |
|
340 | + return $this->buildBacktraceLine( $backtrace, 0 ); |
|
341 | 341 | } |
342 | 342 | |
343 | 343 | /** |
344 | 344 | * @param mixed $data |
345 | 345 | * @return string |
346 | 346 | */ |
347 | - protected function getMessageFromData($data) |
|
347 | + protected function getMessageFromData( $data ) |
|
348 | 348 | { |
349 | 349 | return $data instanceof Throwable |
350 | - ? $this->normalizeThrowableMessage($data->getMessage()) |
|
351 | - : print_r($data, 1); |
|
350 | + ? $this->normalizeThrowableMessage( $data->getMessage() ) |
|
351 | + : print_r( $data, 1 ); |
|
352 | 352 | } |
353 | 353 | |
354 | 354 | /** |
@@ -357,53 +357,53 @@ discard block |
||
357 | 357 | * @param array $context |
358 | 358 | * @return string |
359 | 359 | */ |
360 | - protected function interpolate($message, $context = []) |
|
360 | + protected function interpolate( $message, $context = [] ) |
|
361 | 361 | { |
362 | - if ($this->isObjectOrArray($message) || !is_array($context)) { |
|
363 | - return print_r($message, true); |
|
362 | + if( $this->isObjectOrArray( $message ) || !is_array( $context ) ) { |
|
363 | + return print_r( $message, true ); |
|
364 | 364 | } |
365 | 365 | $replace = []; |
366 | - foreach ($context as $key => $value) { |
|
367 | - $replace['{'.$key.'}'] = $this->normalizeValue($value); |
|
366 | + foreach( $context as $key => $value ) { |
|
367 | + $replace['{'.$key.'}'] = $this->normalizeValue( $value ); |
|
368 | 368 | } |
369 | - return strtr($message, $replace); |
|
369 | + return strtr( $message, $replace ); |
|
370 | 370 | } |
371 | 371 | |
372 | 372 | /** |
373 | 373 | * @param mixed $value |
374 | 374 | * @return bool |
375 | 375 | */ |
376 | - protected function isObjectOrArray($value) |
|
376 | + protected function isObjectOrArray( $value ) |
|
377 | 377 | { |
378 | - return is_object($value) || is_array($value); |
|
378 | + return is_object( $value ) || is_array( $value ); |
|
379 | 379 | } |
380 | 380 | |
381 | 381 | /** |
382 | 382 | * @param string $backtraceLine |
383 | 383 | * @return string |
384 | 384 | */ |
385 | - protected function normalizeBacktraceLine($backtraceLine) |
|
385 | + protected function normalizeBacktraceLine( $backtraceLine ) |
|
386 | 386 | { |
387 | 387 | $search = [ |
388 | - glsr()->path('plugin/'), |
|
389 | - glsr()->path('plugin/', false), |
|
390 | - trailingslashit(glsr()->path()), |
|
391 | - trailingslashit(glsr()->path('', false)), |
|
388 | + glsr()->path( 'plugin/' ), |
|
389 | + glsr()->path( 'plugin/', false ), |
|
390 | + trailingslashit( glsr()->path() ), |
|
391 | + trailingslashit( glsr()->path( '', false ) ), |
|
392 | 392 | WP_CONTENT_DIR, |
393 | 393 | ABSPATH, |
394 | 394 | ]; |
395 | - return str_replace(array_unique($search), '', $backtraceLine); |
|
395 | + return str_replace( array_unique( $search ), '', $backtraceLine ); |
|
396 | 396 | } |
397 | 397 | |
398 | 398 | /** |
399 | 399 | * @param string $message |
400 | 400 | * @return string |
401 | 401 | */ |
402 | - protected function normalizeThrowableMessage($message) |
|
402 | + protected function normalizeThrowableMessage( $message ) |
|
403 | 403 | { |
404 | - $calledIn = strpos($message, ', called in'); |
|
404 | + $calledIn = strpos( $message, ', called in' ); |
|
405 | 405 | return false !== $calledIn |
406 | - ? substr($message, 0, $calledIn) |
|
406 | + ? substr( $message, 0, $calledIn ) |
|
407 | 407 | : $message; |
408 | 408 | } |
409 | 409 | |
@@ -411,14 +411,14 @@ discard block |
||
411 | 411 | * @param mixed $value |
412 | 412 | * @return string |
413 | 413 | */ |
414 | - protected function normalizeValue($value) |
|
414 | + protected function normalizeValue( $value ) |
|
415 | 415 | { |
416 | - if ($value instanceof DateTime) { |
|
417 | - $value = $value->format('Y-m-d H:i:s'); |
|
418 | - } elseif ($this->isObjectOrArray($value)) { |
|
419 | - $value = json_encode($value); |
|
416 | + if( $value instanceof DateTime ) { |
|
417 | + $value = $value->format( 'Y-m-d H:i:s' ); |
|
418 | + } elseif( $this->isObjectOrArray( $value ) ) { |
|
419 | + $value = json_encode( $value ); |
|
420 | 420 | } |
421 | - return (string) $value; |
|
421 | + return (string)$value; |
|
422 | 422 | } |
423 | 423 | |
424 | 424 | /** |
@@ -426,7 +426,7 @@ discard block |
||
426 | 426 | */ |
427 | 427 | protected function reset() |
428 | 428 | { |
429 | - if ($this->size() <= pow(1024, 2) / 8) { |
|
429 | + if( $this->size() <= pow( 1024, 2 ) / 8 ) { |
|
430 | 430 | return; |
431 | 431 | } |
432 | 432 | $this->clear(); |
@@ -434,7 +434,7 @@ discard block |
||
434 | 434 | $this->file, |
435 | 435 | $this->buildLogEntry( |
436 | 436 | static::NOTICE, |
437 | - __('Console was automatically cleared (128 KB maximum size)', 'site-reviews') |
|
437 | + __( 'Console was automatically cleared (128 KB maximum size)', 'site-reviews' ) |
|
438 | 438 | ) |
439 | 439 | ); |
440 | 440 | } |
@@ -415,7 +415,8 @@ |
||
415 | 415 | { |
416 | 416 | if ($value instanceof DateTime) { |
417 | 417 | $value = $value->format('Y-m-d H:i:s'); |
418 | - } elseif ($this->isObjectOrArray($value)) { |
|
418 | + } |
|
419 | + elseif ($this->isObjectOrArray($value)) { |
|
419 | 420 | $value = json_encode($value); |
420 | 421 | } |
421 | 422 | return (string) $value; |
@@ -19,12 +19,12 @@ |
||
19 | 19 | */ |
20 | 20 | class Thing extends BaseType |
21 | 21 | { |
22 | - /** |
|
23 | - * @var array |
|
24 | - * @see http://schema.org/{property_name} |
|
25 | - */ |
|
26 | - public $allowed = [ |
|
27 | - 'additionalType', 'alternateName', 'description', 'disambiguatingDescription', 'identifier', |
|
28 | - 'image', 'mainEntityOfPage', 'name', 'potentialAction', 'sameAs', 'url', |
|
29 | - ]; |
|
22 | + /** |
|
23 | + * @var array |
|
24 | + * @see http://schema.org/{property_name} |
|
25 | + */ |
|
26 | + public $allowed = [ |
|
27 | + 'additionalType', 'alternateName', 'description', 'disambiguatingDescription', 'identifier', |
|
28 | + 'image', 'mainEntityOfPage', 'name', 'potentialAction', 'sameAs', 'url', |
|
29 | + ]; |
|
30 | 30 | } |
@@ -4,19 +4,19 @@ |
||
4 | 4 | |
5 | 5 | class UnknownType extends BaseType |
6 | 6 | { |
7 | - /** |
|
8 | - * @var array |
|
9 | - * @see http://schema.org/{property_name} |
|
10 | - */ |
|
11 | - public $allowed = [ |
|
12 | - 'aggregateRating', |
|
13 | - ]; |
|
7 | + /** |
|
8 | + * @var array |
|
9 | + * @see http://schema.org/{property_name} |
|
10 | + */ |
|
11 | + public $allowed = [ |
|
12 | + 'aggregateRating', |
|
13 | + ]; |
|
14 | 14 | |
15 | - /** |
|
16 | - * @var array |
|
17 | - * @see http://schema.org/{property_name} |
|
18 | - */ |
|
19 | - public $parents = [ |
|
20 | - 'Thing', |
|
21 | - ]; |
|
15 | + /** |
|
16 | + * @var array |
|
17 | + * @see http://schema.org/{property_name} |
|
18 | + */ |
|
19 | + public $parents = [ |
|
20 | + 'Thing', |
|
21 | + ]; |
|
22 | 22 | } |