@@ -6,31 +6,31 @@ |
||
6 | 6 | |
7 | 7 | class Partial |
8 | 8 | { |
9 | - /** |
|
10 | - * @param string $partialPath |
|
11 | - * @return string |
|
12 | - */ |
|
13 | - public function build($partialPath, array $args = []) |
|
14 | - { |
|
15 | - $className = Helper::buildClassName($partialPath, 'Modules\Html\Partials'); |
|
16 | - $className = apply_filters('site-reviews/partial/classname', $className, $partialPath); |
|
17 | - if (!class_exists($className)) { |
|
18 | - glsr_log()->error('Partial missing: '.$className); |
|
19 | - return; |
|
20 | - } |
|
21 | - $args = apply_filters('site-reviews/partial/args/'.$partialPath, $args); |
|
22 | - $partial = glsr($className)->build($args); |
|
23 | - $partial = apply_filters('site-reviews/rendered/partial', $partial, $partialPath, $args); |
|
24 | - $partial = apply_filters('site-reviews/rendered/partial/'.$partialPath, $partial, $args); |
|
25 | - return $partial; |
|
26 | - } |
|
9 | + /** |
|
10 | + * @param string $partialPath |
|
11 | + * @return string |
|
12 | + */ |
|
13 | + public function build($partialPath, array $args = []) |
|
14 | + { |
|
15 | + $className = Helper::buildClassName($partialPath, 'Modules\Html\Partials'); |
|
16 | + $className = apply_filters('site-reviews/partial/classname', $className, $partialPath); |
|
17 | + if (!class_exists($className)) { |
|
18 | + glsr_log()->error('Partial missing: '.$className); |
|
19 | + return; |
|
20 | + } |
|
21 | + $args = apply_filters('site-reviews/partial/args/'.$partialPath, $args); |
|
22 | + $partial = glsr($className)->build($args); |
|
23 | + $partial = apply_filters('site-reviews/rendered/partial', $partial, $partialPath, $args); |
|
24 | + $partial = apply_filters('site-reviews/rendered/partial/'.$partialPath, $partial, $args); |
|
25 | + return $partial; |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * @param string $partialPath |
|
30 | - * @return void |
|
31 | - */ |
|
32 | - public function render($partialPath, array $args = []) |
|
33 | - { |
|
34 | - echo $this->build($partialPath, $args); |
|
35 | - } |
|
28 | + /** |
|
29 | + * @param string $partialPath |
|
30 | + * @return void |
|
31 | + */ |
|
32 | + public function render($partialPath, array $args = []) |
|
33 | + { |
|
34 | + echo $this->build($partialPath, $args); |
|
35 | + } |
|
36 | 36 | } |
@@ -8,137 +8,137 @@ |
||
8 | 8 | |
9 | 9 | class ReviewsHtml extends ArrayObject |
10 | 10 | { |
11 | - /** |
|
12 | - * @var array |
|
13 | - */ |
|
14 | - public $args; |
|
11 | + /** |
|
12 | + * @var array |
|
13 | + */ |
|
14 | + public $args; |
|
15 | 15 | |
16 | - /** |
|
17 | - * @var int |
|
18 | - */ |
|
19 | - public $max_num_pages; |
|
16 | + /** |
|
17 | + * @var int |
|
18 | + */ |
|
19 | + public $max_num_pages; |
|
20 | 20 | |
21 | - /** |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - public $pagination; |
|
21 | + /** |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + public $pagination; |
|
25 | 25 | |
26 | - /** |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - public $reviews; |
|
26 | + /** |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + public $reviews; |
|
30 | 30 | |
31 | - public function __construct(array $renderedReviews, $maxPageCount, array $args) |
|
32 | - { |
|
33 | - $this->args = $args; |
|
34 | - $this->max_num_pages = $maxPageCount; |
|
35 | - $this->reviews = $renderedReviews; |
|
36 | - $this->pagination = $this->buildPagination(); |
|
37 | - parent::__construct($renderedReviews, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); |
|
38 | - } |
|
31 | + public function __construct(array $renderedReviews, $maxPageCount, array $args) |
|
32 | + { |
|
33 | + $this->args = $args; |
|
34 | + $this->max_num_pages = $maxPageCount; |
|
35 | + $this->reviews = $renderedReviews; |
|
36 | + $this->pagination = $this->buildPagination(); |
|
37 | + parent::__construct($renderedReviews, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @return string |
|
42 | - */ |
|
43 | - public function __toString() |
|
44 | - { |
|
45 | - return glsr(Template::class)->build('templates/reviews', [ |
|
46 | - 'args' => $this->args, |
|
47 | - 'context' => [ |
|
48 | - 'assigned_to' => $this->args['assigned_to'], |
|
49 | - 'category' => $this->args['category'], |
|
50 | - 'class' => $this->getClass(), |
|
51 | - 'id' => $this->args['id'], |
|
52 | - 'pagination' => $this->getPagination(), |
|
53 | - 'reviews' => $this->getReviews(), |
|
54 | - ], |
|
55 | - ]); |
|
56 | - } |
|
40 | + /** |
|
41 | + * @return string |
|
42 | + */ |
|
43 | + public function __toString() |
|
44 | + { |
|
45 | + return glsr(Template::class)->build('templates/reviews', [ |
|
46 | + 'args' => $this->args, |
|
47 | + 'context' => [ |
|
48 | + 'assigned_to' => $this->args['assigned_to'], |
|
49 | + 'category' => $this->args['category'], |
|
50 | + 'class' => $this->getClass(), |
|
51 | + 'id' => $this->args['id'], |
|
52 | + 'pagination' => $this->getPagination(), |
|
53 | + 'reviews' => $this->getReviews(), |
|
54 | + ], |
|
55 | + ]); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * @return string |
|
60 | - */ |
|
61 | - public function getPagination() |
|
62 | - { |
|
63 | - return wp_validate_boolean($this->args['pagination']) |
|
64 | - ? $this->pagination |
|
65 | - : ''; |
|
66 | - } |
|
58 | + /** |
|
59 | + * @return string |
|
60 | + */ |
|
61 | + public function getPagination() |
|
62 | + { |
|
63 | + return wp_validate_boolean($this->args['pagination']) |
|
64 | + ? $this->pagination |
|
65 | + : ''; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @return string |
|
70 | - */ |
|
71 | - public function getReviews() |
|
72 | - { |
|
73 | - $html = empty($this->reviews) |
|
74 | - ? $this->getReviewsFallback() |
|
75 | - : implode(PHP_EOL, $this->reviews); |
|
76 | - return glsr(Builder::class)->div($html, [ |
|
77 | - 'class' => 'glsr-reviews-list', |
|
78 | - 'data-reviews' => '', |
|
79 | - ]); |
|
80 | - } |
|
68 | + /** |
|
69 | + * @return string |
|
70 | + */ |
|
71 | + public function getReviews() |
|
72 | + { |
|
73 | + $html = empty($this->reviews) |
|
74 | + ? $this->getReviewsFallback() |
|
75 | + : implode(PHP_EOL, $this->reviews); |
|
76 | + return glsr(Builder::class)->div($html, [ |
|
77 | + 'class' => 'glsr-reviews-list', |
|
78 | + 'data-reviews' => '', |
|
79 | + ]); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @param mixed $key |
|
84 | - * @return mixed |
|
85 | - */ |
|
86 | - public function offsetGet($key) |
|
87 | - { |
|
88 | - if ('navigation' == $key) { |
|
89 | - glsr()->deprecated[] = 'The $reviewsHtml->navigation property has been been deprecated. Please use the $reviewsHtml->pagination property instead.'; |
|
90 | - return $this->pagination; |
|
91 | - } |
|
92 | - if (array_key_exists($key, $this->reviews)) { |
|
93 | - return $this->reviews[$key]; |
|
94 | - } |
|
95 | - return property_exists($this, $key) |
|
96 | - ? $this->$key |
|
97 | - : null; |
|
98 | - } |
|
82 | + /** |
|
83 | + * @param mixed $key |
|
84 | + * @return mixed |
|
85 | + */ |
|
86 | + public function offsetGet($key) |
|
87 | + { |
|
88 | + if ('navigation' == $key) { |
|
89 | + glsr()->deprecated[] = 'The $reviewsHtml->navigation property has been been deprecated. Please use the $reviewsHtml->pagination property instead.'; |
|
90 | + return $this->pagination; |
|
91 | + } |
|
92 | + if (array_key_exists($key, $this->reviews)) { |
|
93 | + return $this->reviews[$key]; |
|
94 | + } |
|
95 | + return property_exists($this, $key) |
|
96 | + ? $this->$key |
|
97 | + : null; |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * @return string |
|
102 | - */ |
|
103 | - protected function buildPagination() |
|
104 | - { |
|
105 | - $html = glsr(Partial::class)->build('pagination', [ |
|
106 | - 'baseUrl' => Arr::get($this->args, 'pagedUrl'), |
|
107 | - 'current' => Arr::get($this->args, 'paged'), |
|
108 | - 'total' => $this->max_num_pages, |
|
109 | - ]); |
|
110 | - return glsr(Builder::class)->div($html, [ |
|
111 | - 'class' => 'glsr-pagination', |
|
112 | - 'data-atts' => $this->args['json'], |
|
113 | - 'data-pagination' => '', |
|
114 | - ]); |
|
115 | - } |
|
100 | + /** |
|
101 | + * @return string |
|
102 | + */ |
|
103 | + protected function buildPagination() |
|
104 | + { |
|
105 | + $html = glsr(Partial::class)->build('pagination', [ |
|
106 | + 'baseUrl' => Arr::get($this->args, 'pagedUrl'), |
|
107 | + 'current' => Arr::get($this->args, 'paged'), |
|
108 | + 'total' => $this->max_num_pages, |
|
109 | + ]); |
|
110 | + return glsr(Builder::class)->div($html, [ |
|
111 | + 'class' => 'glsr-pagination', |
|
112 | + 'data-atts' => $this->args['json'], |
|
113 | + 'data-pagination' => '', |
|
114 | + ]); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * @return string |
|
119 | - */ |
|
120 | - protected function getClass() |
|
121 | - { |
|
122 | - $defaults = [ |
|
123 | - 'glsr-reviews', |
|
124 | - ]; |
|
125 | - if ('ajax' == $this->args['pagination']) { |
|
126 | - $defaults[] = 'glsr-ajax-pagination'; |
|
127 | - } |
|
128 | - $classes = explode(' ', $this->args['class']); |
|
129 | - $classes = array_unique(array_merge($defaults, array_filter($classes))); |
|
130 | - return implode(' ', $classes); |
|
131 | - } |
|
117 | + /** |
|
118 | + * @return string |
|
119 | + */ |
|
120 | + protected function getClass() |
|
121 | + { |
|
122 | + $defaults = [ |
|
123 | + 'glsr-reviews', |
|
124 | + ]; |
|
125 | + if ('ajax' == $this->args['pagination']) { |
|
126 | + $defaults[] = 'glsr-ajax-pagination'; |
|
127 | + } |
|
128 | + $classes = explode(' ', $this->args['class']); |
|
129 | + $classes = array_unique(array_merge($defaults, array_filter($classes))); |
|
130 | + return implode(' ', $classes); |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * @return string |
|
135 | - */ |
|
136 | - protected function getReviewsFallback() |
|
137 | - { |
|
138 | - if (empty($this->args['fallback']) && glsr(OptionManager::class)->getBool('settings.reviews.fallback')) { |
|
139 | - $this->args['fallback'] = __('There are no reviews yet. Be the first one to write one.', 'site-reviews'); |
|
140 | - } |
|
141 | - $fallback = '<p class="glsr-no-margins">'.$this->args['fallback'].'</p>'; |
|
142 | - return apply_filters('site-reviews/reviews/fallback', $fallback, $this->args); |
|
143 | - } |
|
133 | + /** |
|
134 | + * @return string |
|
135 | + */ |
|
136 | + protected function getReviewsFallback() |
|
137 | + { |
|
138 | + if (empty($this->args['fallback']) && glsr(OptionManager::class)->getBool('settings.reviews.fallback')) { |
|
139 | + $this->args['fallback'] = __('There are no reviews yet. Be the first one to write one.', 'site-reviews'); |
|
140 | + } |
|
141 | + $fallback = '<p class="glsr-no-margins">'.$this->args['fallback'].'</p>'; |
|
142 | + return apply_filters('site-reviews/reviews/fallback', $fallback, $this->args); |
|
143 | + } |
|
144 | 144 | } |
@@ -19,343 +19,343 @@ |
||
19 | 19 | */ |
20 | 20 | class Builder |
21 | 21 | { |
22 | - const INPUT_TYPES = [ |
|
23 | - 'checkbox', 'date', 'datetime-local', 'email', 'file', 'hidden', 'image', 'month', |
|
24 | - 'number', 'password', 'radio', 'range', 'reset', 'search', 'submit', 'tel', 'text', 'time', |
|
25 | - 'url', 'week', |
|
26 | - ]; |
|
27 | - |
|
28 | - const TAGS_FORM = [ |
|
29 | - 'input', 'select', 'textarea', |
|
30 | - ]; |
|
31 | - |
|
32 | - const TAGS_SINGLE = [ |
|
33 | - 'img', |
|
34 | - ]; |
|
35 | - |
|
36 | - const TAGS_STRUCTURE = [ |
|
37 | - 'div', 'form', 'nav', 'ol', 'section', 'ul', |
|
38 | - ]; |
|
39 | - |
|
40 | - const TAGS_TEXT = [ |
|
41 | - 'a', 'button', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'label', 'li', 'option', 'p', 'pre', |
|
42 | - 'small', 'span', |
|
43 | - ]; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var array |
|
47 | - */ |
|
48 | - public $args = []; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var bool |
|
52 | - */ |
|
53 | - public $render = false; |
|
54 | - |
|
55 | - /** |
|
56 | - * @var string |
|
57 | - */ |
|
58 | - public $tag; |
|
59 | - |
|
60 | - /** |
|
61 | - * @param string $method |
|
62 | - * @param array $args |
|
63 | - * @return string|void |
|
64 | - */ |
|
65 | - public function __call($method, $args) |
|
66 | - { |
|
67 | - $instance = new static(); |
|
68 | - $instance->setTagFromMethod($method); |
|
69 | - call_user_func_array([$instance, 'normalize'], $args); |
|
70 | - $tags = array_merge(static::TAGS_FORM, static::TAGS_SINGLE, static::TAGS_STRUCTURE, static::TAGS_TEXT); |
|
71 | - do_action_ref_array('site-reviews/builder', [$instance]); |
|
72 | - $generatedTag = in_array($instance->tag, $tags) |
|
73 | - ? $instance->buildTag() |
|
74 | - : $instance->buildCustomField(); |
|
75 | - $generatedTag = apply_filters('site-reviews/builder/result', $generatedTag, $instance); |
|
76 | - if (!$this->render) { |
|
77 | - return $generatedTag; |
|
78 | - } |
|
79 | - echo $generatedTag; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @param string $property |
|
84 | - * @param mixed $value |
|
85 | - * @return void |
|
86 | - */ |
|
87 | - public function __set($property, $value) |
|
88 | - { |
|
89 | - $properties = [ |
|
90 | - 'args' => 'is_array', |
|
91 | - 'render' => 'is_bool', |
|
92 | - 'tag' => 'is_string', |
|
93 | - ]; |
|
94 | - if (array_key_exists($property, $properties) && !empty($value)) { |
|
95 | - $this->$property = $value; |
|
96 | - } |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * @return void|string |
|
101 | - */ |
|
102 | - public function getClosingTag() |
|
103 | - { |
|
104 | - if (!empty($this->tag)) { |
|
105 | - return '</'.$this->tag.'>'; |
|
106 | - } |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @return void|string |
|
111 | - */ |
|
112 | - public function getOpeningTag() |
|
113 | - { |
|
114 | - if (!empty($this->tag)) { |
|
115 | - $attributes = glsr(Attributes::class)->{$this->tag}($this->args)->toString(); |
|
116 | - return '<'.trim($this->tag.' '.$attributes).'>'; |
|
117 | - } |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @return void|string |
|
122 | - */ |
|
123 | - public function getTag() |
|
124 | - { |
|
125 | - if (in_array($this->tag, static::TAGS_SINGLE)) { |
|
126 | - return $this->getOpeningTag(); |
|
127 | - } |
|
128 | - if (!in_array($this->tag, static::TAGS_FORM)) { |
|
129 | - return $this->buildDefaultTag(); |
|
130 | - } |
|
131 | - return call_user_func([$this, 'buildForm'.ucfirst($this->tag)]).$this->buildFieldDescription(); |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * @return string |
|
136 | - */ |
|
137 | - public function raw(array $field) |
|
138 | - { |
|
139 | - unset($field['label']); |
|
140 | - return $this->{$field['type']}($field); |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * @return string|void |
|
145 | - */ |
|
146 | - protected function buildCustomField() |
|
147 | - { |
|
148 | - $className = $this->getCustomFieldClassName(); |
|
149 | - if (class_exists($className)) { |
|
150 | - return (new $className($this))->build(); |
|
151 | - } |
|
152 | - glsr_log()->error('Field class missing: '.$className); |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * @return string|void |
|
157 | - */ |
|
158 | - protected function buildDefaultTag($text = '') |
|
159 | - { |
|
160 | - if (empty($text)) { |
|
161 | - $text = $this->args['text']; |
|
162 | - } |
|
163 | - return $this->getOpeningTag().$text.$this->getClosingTag(); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @return string|void |
|
168 | - */ |
|
169 | - protected function buildFieldDescription() |
|
170 | - { |
|
171 | - if (empty($this->args['description'])) { |
|
172 | - return; |
|
173 | - } |
|
174 | - if ($this->args['is_widget']) { |
|
175 | - return $this->small($this->args['description']); |
|
176 | - } |
|
177 | - return $this->p($this->args['description'], ['class' => 'description']); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @return string|void |
|
182 | - */ |
|
183 | - protected function buildFormInput() |
|
184 | - { |
|
185 | - if (!in_array($this->args['type'], ['checkbox', 'radio'])) { |
|
186 | - if (isset($this->args['multiple'])) { |
|
187 | - $this->args['name'] .= '[]'; |
|
188 | - } |
|
189 | - return $this->buildFormLabel().$this->getOpeningTag(); |
|
190 | - } |
|
191 | - return empty($this->args['options']) |
|
192 | - ? $this->buildFormInputChoice() |
|
193 | - : $this->buildFormInputMultiChoice(); |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * @return string|void |
|
198 | - */ |
|
199 | - protected function buildFormInputChoice() |
|
200 | - { |
|
201 | - if (!empty($this->args['text'])) { |
|
202 | - $this->args['label'] = $this->args['text']; |
|
203 | - } |
|
204 | - if (!$this->args['is_public']) { |
|
205 | - return $this->buildFormLabel([ |
|
206 | - 'class' => 'glsr-'.$this->args['type'].'-label', |
|
207 | - 'text' => $this->getOpeningTag().' '.$this->args['label'].'<span></span>', |
|
208 | - ]); |
|
209 | - } |
|
210 | - return $this->getOpeningTag().$this->buildFormLabel([ |
|
211 | - 'class' => 'glsr-'.$this->args['type'].'-label', |
|
212 | - 'text' => $this->args['label'].'<span></span>', |
|
213 | - ]); |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * @return string|void |
|
218 | - */ |
|
219 | - protected function buildFormInputMultiChoice() |
|
220 | - { |
|
221 | - if ('checkbox' == $this->args['type']) { |
|
222 | - $this->args['name'] .= '[]'; |
|
223 | - } |
|
224 | - $index = 0; |
|
225 | - $options = array_reduce(array_keys($this->args['options']), function ($carry, $key) use (&$index) { |
|
226 | - return $carry.$this->li($this->{$this->args['type']}([ |
|
227 | - 'checked' => in_array($key, (array) $this->args['value']), |
|
228 | - 'id' => $this->args['id'].'-'.$index++, |
|
229 | - 'name' => $this->args['name'], |
|
230 | - 'text' => $this->args['options'][$key], |
|
231 | - 'value' => $key, |
|
232 | - ])); |
|
233 | - }); |
|
234 | - return $this->ul($options, [ |
|
235 | - 'class' => $this->args['class'], |
|
236 | - 'id' => $this->args['id'], |
|
237 | - ]); |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * @return void|string |
|
242 | - */ |
|
243 | - protected function buildFormLabel(array $customArgs = []) |
|
244 | - { |
|
245 | - if (empty($this->args['label']) || 'hidden' == $this->args['type']) { |
|
246 | - return; |
|
247 | - } |
|
248 | - return $this->label(wp_parse_args($customArgs, [ |
|
249 | - 'for' => $this->args['id'], |
|
250 | - 'is_public' => $this->args['is_public'], |
|
251 | - 'text' => $this->args['label'], |
|
252 | - 'type' => $this->args['type'], |
|
253 | - ])); |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * @return string|void |
|
258 | - */ |
|
259 | - protected function buildFormSelect() |
|
260 | - { |
|
261 | - return $this->buildFormLabel().$this->buildDefaultTag($this->buildFormSelectOptions()); |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * @return string|void |
|
266 | - */ |
|
267 | - protected function buildFormSelectOptions() |
|
268 | - { |
|
269 | - return array_reduce(array_keys($this->args['options']), function ($carry, $key) { |
|
270 | - return $carry.$this->option([ |
|
271 | - 'selected' => $this->args['value'] === (string) $key, |
|
272 | - 'text' => $this->args['options'][$key], |
|
273 | - 'value' => $key, |
|
274 | - ]); |
|
275 | - }); |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * @return string|void |
|
280 | - */ |
|
281 | - protected function buildFormTextarea() |
|
282 | - { |
|
283 | - return $this->buildFormLabel().$this->buildDefaultTag($this->args['value']); |
|
284 | - } |
|
285 | - |
|
286 | - /** |
|
287 | - * @return string|void |
|
288 | - */ |
|
289 | - protected function buildTag() |
|
290 | - { |
|
291 | - $this->mergeArgsWithRequiredDefaults(); |
|
292 | - return $this->getTag(); |
|
293 | - } |
|
294 | - |
|
295 | - /** |
|
296 | - * @return string |
|
297 | - */ |
|
298 | - protected function getCustomFieldClassName() |
|
299 | - { |
|
300 | - $classname = Helper::buildClassName($this->tag, __NAMESPACE__.'\Fields'); |
|
301 | - return apply_filters('site-reviews/builder/field/'.$this->tag, $classname); |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * @return void |
|
306 | - */ |
|
307 | - protected function mergeArgsWithRequiredDefaults() |
|
308 | - { |
|
309 | - $className = $this->getCustomFieldClassName(); |
|
310 | - if (class_exists($className)) { |
|
311 | - $this->args = $className::merge($this->args); |
|
312 | - } |
|
313 | - $this->args = glsr(BuilderDefaults::class)->merge($this->args); |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * @param string|array ...$params |
|
318 | - * @return void |
|
319 | - */ |
|
320 | - protected function normalize(...$params) |
|
321 | - { |
|
322 | - $parameter1 = Arr::get($params, 0); |
|
323 | - $parameter2 = Arr::get($params, 1); |
|
324 | - if (is_string($parameter1) || is_numeric($parameter1)) { |
|
325 | - $this->setNameOrTextAttributeForTag($parameter1); |
|
326 | - } |
|
327 | - if (is_array($parameter1)) { |
|
328 | - $this->args += $parameter1; |
|
329 | - } elseif (is_array($parameter2)) { |
|
330 | - $this->args += $parameter2; |
|
331 | - } |
|
332 | - if (!isset($this->args['is_public'])) { |
|
333 | - $this->args['is_public'] = false; |
|
334 | - } |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * @param string $value |
|
339 | - * @return void |
|
340 | - */ |
|
341 | - protected function setNameOrTextAttributeForTag($value) |
|
342 | - { |
|
343 | - $attribute = in_array($this->tag, static::TAGS_FORM) |
|
344 | - ? 'name' |
|
345 | - : 'text'; |
|
346 | - $this->args[$attribute] = $value; |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * @param string $method |
|
351 | - * @return void |
|
352 | - */ |
|
353 | - protected function setTagFromMethod($method) |
|
354 | - { |
|
355 | - $this->tag = strtolower($method); |
|
356 | - if (in_array($this->tag, static::INPUT_TYPES)) { |
|
357 | - $this->args['type'] = $this->tag; |
|
358 | - $this->tag = 'input'; |
|
359 | - } |
|
360 | - } |
|
22 | + const INPUT_TYPES = [ |
|
23 | + 'checkbox', 'date', 'datetime-local', 'email', 'file', 'hidden', 'image', 'month', |
|
24 | + 'number', 'password', 'radio', 'range', 'reset', 'search', 'submit', 'tel', 'text', 'time', |
|
25 | + 'url', 'week', |
|
26 | + ]; |
|
27 | + |
|
28 | + const TAGS_FORM = [ |
|
29 | + 'input', 'select', 'textarea', |
|
30 | + ]; |
|
31 | + |
|
32 | + const TAGS_SINGLE = [ |
|
33 | + 'img', |
|
34 | + ]; |
|
35 | + |
|
36 | + const TAGS_STRUCTURE = [ |
|
37 | + 'div', 'form', 'nav', 'ol', 'section', 'ul', |
|
38 | + ]; |
|
39 | + |
|
40 | + const TAGS_TEXT = [ |
|
41 | + 'a', 'button', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'label', 'li', 'option', 'p', 'pre', |
|
42 | + 'small', 'span', |
|
43 | + ]; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var array |
|
47 | + */ |
|
48 | + public $args = []; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var bool |
|
52 | + */ |
|
53 | + public $render = false; |
|
54 | + |
|
55 | + /** |
|
56 | + * @var string |
|
57 | + */ |
|
58 | + public $tag; |
|
59 | + |
|
60 | + /** |
|
61 | + * @param string $method |
|
62 | + * @param array $args |
|
63 | + * @return string|void |
|
64 | + */ |
|
65 | + public function __call($method, $args) |
|
66 | + { |
|
67 | + $instance = new static(); |
|
68 | + $instance->setTagFromMethod($method); |
|
69 | + call_user_func_array([$instance, 'normalize'], $args); |
|
70 | + $tags = array_merge(static::TAGS_FORM, static::TAGS_SINGLE, static::TAGS_STRUCTURE, static::TAGS_TEXT); |
|
71 | + do_action_ref_array('site-reviews/builder', [$instance]); |
|
72 | + $generatedTag = in_array($instance->tag, $tags) |
|
73 | + ? $instance->buildTag() |
|
74 | + : $instance->buildCustomField(); |
|
75 | + $generatedTag = apply_filters('site-reviews/builder/result', $generatedTag, $instance); |
|
76 | + if (!$this->render) { |
|
77 | + return $generatedTag; |
|
78 | + } |
|
79 | + echo $generatedTag; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @param string $property |
|
84 | + * @param mixed $value |
|
85 | + * @return void |
|
86 | + */ |
|
87 | + public function __set($property, $value) |
|
88 | + { |
|
89 | + $properties = [ |
|
90 | + 'args' => 'is_array', |
|
91 | + 'render' => 'is_bool', |
|
92 | + 'tag' => 'is_string', |
|
93 | + ]; |
|
94 | + if (array_key_exists($property, $properties) && !empty($value)) { |
|
95 | + $this->$property = $value; |
|
96 | + } |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * @return void|string |
|
101 | + */ |
|
102 | + public function getClosingTag() |
|
103 | + { |
|
104 | + if (!empty($this->tag)) { |
|
105 | + return '</'.$this->tag.'>'; |
|
106 | + } |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @return void|string |
|
111 | + */ |
|
112 | + public function getOpeningTag() |
|
113 | + { |
|
114 | + if (!empty($this->tag)) { |
|
115 | + $attributes = glsr(Attributes::class)->{$this->tag}($this->args)->toString(); |
|
116 | + return '<'.trim($this->tag.' '.$attributes).'>'; |
|
117 | + } |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @return void|string |
|
122 | + */ |
|
123 | + public function getTag() |
|
124 | + { |
|
125 | + if (in_array($this->tag, static::TAGS_SINGLE)) { |
|
126 | + return $this->getOpeningTag(); |
|
127 | + } |
|
128 | + if (!in_array($this->tag, static::TAGS_FORM)) { |
|
129 | + return $this->buildDefaultTag(); |
|
130 | + } |
|
131 | + return call_user_func([$this, 'buildForm'.ucfirst($this->tag)]).$this->buildFieldDescription(); |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * @return string |
|
136 | + */ |
|
137 | + public function raw(array $field) |
|
138 | + { |
|
139 | + unset($field['label']); |
|
140 | + return $this->{$field['type']}($field); |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * @return string|void |
|
145 | + */ |
|
146 | + protected function buildCustomField() |
|
147 | + { |
|
148 | + $className = $this->getCustomFieldClassName(); |
|
149 | + if (class_exists($className)) { |
|
150 | + return (new $className($this))->build(); |
|
151 | + } |
|
152 | + glsr_log()->error('Field class missing: '.$className); |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * @return string|void |
|
157 | + */ |
|
158 | + protected function buildDefaultTag($text = '') |
|
159 | + { |
|
160 | + if (empty($text)) { |
|
161 | + $text = $this->args['text']; |
|
162 | + } |
|
163 | + return $this->getOpeningTag().$text.$this->getClosingTag(); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @return string|void |
|
168 | + */ |
|
169 | + protected function buildFieldDescription() |
|
170 | + { |
|
171 | + if (empty($this->args['description'])) { |
|
172 | + return; |
|
173 | + } |
|
174 | + if ($this->args['is_widget']) { |
|
175 | + return $this->small($this->args['description']); |
|
176 | + } |
|
177 | + return $this->p($this->args['description'], ['class' => 'description']); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @return string|void |
|
182 | + */ |
|
183 | + protected function buildFormInput() |
|
184 | + { |
|
185 | + if (!in_array($this->args['type'], ['checkbox', 'radio'])) { |
|
186 | + if (isset($this->args['multiple'])) { |
|
187 | + $this->args['name'] .= '[]'; |
|
188 | + } |
|
189 | + return $this->buildFormLabel().$this->getOpeningTag(); |
|
190 | + } |
|
191 | + return empty($this->args['options']) |
|
192 | + ? $this->buildFormInputChoice() |
|
193 | + : $this->buildFormInputMultiChoice(); |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * @return string|void |
|
198 | + */ |
|
199 | + protected function buildFormInputChoice() |
|
200 | + { |
|
201 | + if (!empty($this->args['text'])) { |
|
202 | + $this->args['label'] = $this->args['text']; |
|
203 | + } |
|
204 | + if (!$this->args['is_public']) { |
|
205 | + return $this->buildFormLabel([ |
|
206 | + 'class' => 'glsr-'.$this->args['type'].'-label', |
|
207 | + 'text' => $this->getOpeningTag().' '.$this->args['label'].'<span></span>', |
|
208 | + ]); |
|
209 | + } |
|
210 | + return $this->getOpeningTag().$this->buildFormLabel([ |
|
211 | + 'class' => 'glsr-'.$this->args['type'].'-label', |
|
212 | + 'text' => $this->args['label'].'<span></span>', |
|
213 | + ]); |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * @return string|void |
|
218 | + */ |
|
219 | + protected function buildFormInputMultiChoice() |
|
220 | + { |
|
221 | + if ('checkbox' == $this->args['type']) { |
|
222 | + $this->args['name'] .= '[]'; |
|
223 | + } |
|
224 | + $index = 0; |
|
225 | + $options = array_reduce(array_keys($this->args['options']), function ($carry, $key) use (&$index) { |
|
226 | + return $carry.$this->li($this->{$this->args['type']}([ |
|
227 | + 'checked' => in_array($key, (array) $this->args['value']), |
|
228 | + 'id' => $this->args['id'].'-'.$index++, |
|
229 | + 'name' => $this->args['name'], |
|
230 | + 'text' => $this->args['options'][$key], |
|
231 | + 'value' => $key, |
|
232 | + ])); |
|
233 | + }); |
|
234 | + return $this->ul($options, [ |
|
235 | + 'class' => $this->args['class'], |
|
236 | + 'id' => $this->args['id'], |
|
237 | + ]); |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * @return void|string |
|
242 | + */ |
|
243 | + protected function buildFormLabel(array $customArgs = []) |
|
244 | + { |
|
245 | + if (empty($this->args['label']) || 'hidden' == $this->args['type']) { |
|
246 | + return; |
|
247 | + } |
|
248 | + return $this->label(wp_parse_args($customArgs, [ |
|
249 | + 'for' => $this->args['id'], |
|
250 | + 'is_public' => $this->args['is_public'], |
|
251 | + 'text' => $this->args['label'], |
|
252 | + 'type' => $this->args['type'], |
|
253 | + ])); |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * @return string|void |
|
258 | + */ |
|
259 | + protected function buildFormSelect() |
|
260 | + { |
|
261 | + return $this->buildFormLabel().$this->buildDefaultTag($this->buildFormSelectOptions()); |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * @return string|void |
|
266 | + */ |
|
267 | + protected function buildFormSelectOptions() |
|
268 | + { |
|
269 | + return array_reduce(array_keys($this->args['options']), function ($carry, $key) { |
|
270 | + return $carry.$this->option([ |
|
271 | + 'selected' => $this->args['value'] === (string) $key, |
|
272 | + 'text' => $this->args['options'][$key], |
|
273 | + 'value' => $key, |
|
274 | + ]); |
|
275 | + }); |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * @return string|void |
|
280 | + */ |
|
281 | + protected function buildFormTextarea() |
|
282 | + { |
|
283 | + return $this->buildFormLabel().$this->buildDefaultTag($this->args['value']); |
|
284 | + } |
|
285 | + |
|
286 | + /** |
|
287 | + * @return string|void |
|
288 | + */ |
|
289 | + protected function buildTag() |
|
290 | + { |
|
291 | + $this->mergeArgsWithRequiredDefaults(); |
|
292 | + return $this->getTag(); |
|
293 | + } |
|
294 | + |
|
295 | + /** |
|
296 | + * @return string |
|
297 | + */ |
|
298 | + protected function getCustomFieldClassName() |
|
299 | + { |
|
300 | + $classname = Helper::buildClassName($this->tag, __NAMESPACE__.'\Fields'); |
|
301 | + return apply_filters('site-reviews/builder/field/'.$this->tag, $classname); |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * @return void |
|
306 | + */ |
|
307 | + protected function mergeArgsWithRequiredDefaults() |
|
308 | + { |
|
309 | + $className = $this->getCustomFieldClassName(); |
|
310 | + if (class_exists($className)) { |
|
311 | + $this->args = $className::merge($this->args); |
|
312 | + } |
|
313 | + $this->args = glsr(BuilderDefaults::class)->merge($this->args); |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * @param string|array ...$params |
|
318 | + * @return void |
|
319 | + */ |
|
320 | + protected function normalize(...$params) |
|
321 | + { |
|
322 | + $parameter1 = Arr::get($params, 0); |
|
323 | + $parameter2 = Arr::get($params, 1); |
|
324 | + if (is_string($parameter1) || is_numeric($parameter1)) { |
|
325 | + $this->setNameOrTextAttributeForTag($parameter1); |
|
326 | + } |
|
327 | + if (is_array($parameter1)) { |
|
328 | + $this->args += $parameter1; |
|
329 | + } elseif (is_array($parameter2)) { |
|
330 | + $this->args += $parameter2; |
|
331 | + } |
|
332 | + if (!isset($this->args['is_public'])) { |
|
333 | + $this->args['is_public'] = false; |
|
334 | + } |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * @param string $value |
|
339 | + * @return void |
|
340 | + */ |
|
341 | + protected function setNameOrTextAttributeForTag($value) |
|
342 | + { |
|
343 | + $attribute = in_array($this->tag, static::TAGS_FORM) |
|
344 | + ? 'name' |
|
345 | + : 'text'; |
|
346 | + $this->args[$attribute] = $value; |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * @param string $method |
|
351 | + * @return void |
|
352 | + */ |
|
353 | + protected function setTagFromMethod($method) |
|
354 | + { |
|
355 | + $this->tag = strtolower($method); |
|
356 | + if (in_array($this->tag, static::INPUT_TYPES)) { |
|
357 | + $this->args['type'] = $this->tag; |
|
358 | + $this->tag = 'input'; |
|
359 | + } |
|
360 | + } |
|
361 | 361 | } |
@@ -11,239 +11,239 @@ |
||
11 | 11 | |
12 | 12 | class Settings |
13 | 13 | { |
14 | - /** |
|
15 | - * @var array |
|
16 | - */ |
|
17 | - public $settings; |
|
18 | - |
|
19 | - /** |
|
20 | - * @param string $id |
|
21 | - * @return string |
|
22 | - */ |
|
23 | - public function buildFields($id) |
|
24 | - { |
|
25 | - $this->settings = glsr(DefaultsManager::class)->settings(); |
|
26 | - $method = Helper::buildMethodName($id, 'getTemplateDataFor'); |
|
27 | - $data = !method_exists($this, $method) |
|
28 | - ? $this->getTemplateData($id) |
|
29 | - : $this->$method($id); |
|
30 | - return glsr(Template::class)->build('pages/settings/'.$id, $data); |
|
31 | - } |
|
32 | - |
|
33 | - /** |
|
34 | - * @return string |
|
35 | - */ |
|
36 | - protected function getFieldDefault(array $field) |
|
37 | - { |
|
38 | - return Arr::get($field, 'default'); |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * @return string |
|
43 | - */ |
|
44 | - protected function getFieldNameForDependsOn($path) |
|
45 | - { |
|
46 | - $fieldName = Str::convertPathToName($path, OptionManager::databaseKey()); |
|
47 | - return $this->isMultiDependency($path) |
|
48 | - ? $fieldName.'[]' |
|
49 | - : $fieldName; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @return array |
|
54 | - */ |
|
55 | - protected function getSettingFields($path) |
|
56 | - { |
|
57 | - return array_filter($this->settings, function ($key) use ($path) { |
|
58 | - return Str::startsWith($path, $key); |
|
59 | - }, ARRAY_FILTER_USE_KEY); |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * @return string |
|
64 | - */ |
|
65 | - protected function getSettingRows(array $fields) |
|
66 | - { |
|
67 | - $rows = ''; |
|
68 | - foreach ($fields as $name => $field) { |
|
69 | - $field = wp_parse_args($field, [ |
|
70 | - 'is_setting' => true, |
|
71 | - 'name' => $name, |
|
72 | - ]); |
|
73 | - $rows.= new Field($this->normalize($field)); |
|
74 | - } |
|
75 | - return $rows; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * @param string $id |
|
80 | - * @return array |
|
81 | - */ |
|
82 | - protected function getTemplateData($id) |
|
83 | - { |
|
84 | - $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
85 | - return [ |
|
86 | - 'context' => [ |
|
87 | - 'rows' => $this->getSettingRows($fields), |
|
88 | - ], |
|
89 | - ]; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * @param string $id |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - protected function getTemplateDataForAddons($id) |
|
97 | - { |
|
98 | - $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
99 | - $settings = Arr::convertFromDotNotation($fields); |
|
100 | - $settingKeys = array_keys($settings['settings']['addons']); |
|
101 | - $results = []; |
|
102 | - foreach ($settingKeys as $key) { |
|
103 | - $addonFields = array_filter($fields, function ($path) use ($key) { |
|
104 | - return Str::startsWith('settings.addons.'.$key, $path); |
|
105 | - }, ARRAY_FILTER_USE_KEY); |
|
106 | - $results[$key] = $this->getSettingRows($addonFields); |
|
107 | - } |
|
108 | - ksort($results); |
|
109 | - return [ |
|
110 | - 'settings' => $results, |
|
111 | - ]; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @param string $id |
|
116 | - * @return array |
|
117 | - */ |
|
118 | - protected function getTemplateDataForLicenses($id) |
|
119 | - { |
|
120 | - $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
121 | - ksort($fields); |
|
122 | - return [ |
|
123 | - 'context' => [ |
|
124 | - 'rows' => $this->getSettingRows($fields), |
|
125 | - ], |
|
126 | - ]; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @return array |
|
131 | - */ |
|
132 | - protected function getTemplateDataForTranslations() |
|
133 | - { |
|
134 | - $translations = glsr(Translation::class)->renderAll(); |
|
135 | - $class = empty($translations) |
|
136 | - ? 'glsr-hidden' |
|
137 | - : ''; |
|
138 | - return [ |
|
139 | - 'context' => [ |
|
140 | - 'class' => $class, |
|
141 | - 'database_key' => OptionManager::databaseKey(), |
|
142 | - 'translations' => $translations, |
|
143 | - ], |
|
144 | - ]; |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @param string $path |
|
149 | - * @param string|array $expectedValue |
|
150 | - * @return bool |
|
151 | - */ |
|
152 | - protected function isFieldHidden($path, $expectedValue) |
|
153 | - { |
|
154 | - $optionValue = glsr(OptionManager::class)->get( |
|
155 | - $path, |
|
156 | - Arr::get(glsr()->defaults, $path) |
|
157 | - ); |
|
158 | - if (is_array($expectedValue)) { |
|
159 | - return is_array($optionValue) |
|
160 | - ? 0 === count(array_intersect($optionValue, $expectedValue)) |
|
161 | - : !in_array($optionValue, $expectedValue); |
|
162 | - } |
|
163 | - return $optionValue != $expectedValue; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @return bool |
|
168 | - */ |
|
169 | - protected function isMultiDependency($path) |
|
170 | - { |
|
171 | - if (isset($this->settings[$path])) { |
|
172 | - $field = $this->settings[$path]; |
|
173 | - return ('checkbox' == $field['type'] && !empty($field['options'])) |
|
174 | - || !empty($field['multiple']); |
|
175 | - } |
|
176 | - return false; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * @return array |
|
181 | - */ |
|
182 | - protected function normalize(array $field) |
|
183 | - { |
|
184 | - $field = $this->normalizeDependsOn($field); |
|
185 | - $field = $this->normalizeLabelAndLegend($field); |
|
186 | - $field = $this->normalizeValue($field); |
|
187 | - return $field; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * @return array |
|
192 | - */ |
|
193 | - protected function normalizeDependsOn(array $field) |
|
194 | - { |
|
195 | - if (!empty($field['depends_on']) && is_array($field['depends_on'])) { |
|
196 | - $isFieldHidden = false; |
|
197 | - $conditions = []; |
|
198 | - foreach ($field['depends_on'] as $path => $value) { |
|
199 | - $conditions[] = [ |
|
200 | - 'name' => $this->getFieldNameForDependsOn($path), |
|
201 | - 'value' => $value, |
|
202 | - ]; |
|
203 | - if ($this->isFieldHidden($path, $value)) { |
|
204 | - $isFieldHidden = true; |
|
205 | - } |
|
206 | - } |
|
207 | - $field['data-depends'] = json_encode($conditions, JSON_HEX_APOS | JSON_HEX_QUOT); |
|
208 | - $field['is_hidden'] = $isFieldHidden; |
|
209 | - } |
|
210 | - return $field; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * @return array |
|
215 | - */ |
|
216 | - protected function normalizeLabelAndLegend(array $field) |
|
217 | - { |
|
218 | - if (!empty($field['label'])) { |
|
219 | - $field['legend'] = $field['label']; |
|
220 | - unset($field['label']); |
|
221 | - } else { |
|
222 | - $field['is_valid'] = false; |
|
223 | - glsr_log()->warning('Setting field is missing a label')->debug($field); |
|
224 | - } |
|
225 | - return $field; |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * @return array |
|
230 | - */ |
|
231 | - protected function normalizeValue(array $field) |
|
232 | - { |
|
233 | - if (!isset($field['value'])) { |
|
234 | - $field['value'] = glsr(OptionManager::class)->get( |
|
235 | - $field['name'], |
|
236 | - $this->getFieldDefault($field) |
|
237 | - ); |
|
238 | - } |
|
239 | - return $field; |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * @return string |
|
244 | - */ |
|
245 | - protected function normalizeSettingPath($path) |
|
246 | - { |
|
247 | - return Str::prefix('settings.', rtrim($path, '.')); |
|
248 | - } |
|
14 | + /** |
|
15 | + * @var array |
|
16 | + */ |
|
17 | + public $settings; |
|
18 | + |
|
19 | + /** |
|
20 | + * @param string $id |
|
21 | + * @return string |
|
22 | + */ |
|
23 | + public function buildFields($id) |
|
24 | + { |
|
25 | + $this->settings = glsr(DefaultsManager::class)->settings(); |
|
26 | + $method = Helper::buildMethodName($id, 'getTemplateDataFor'); |
|
27 | + $data = !method_exists($this, $method) |
|
28 | + ? $this->getTemplateData($id) |
|
29 | + : $this->$method($id); |
|
30 | + return glsr(Template::class)->build('pages/settings/'.$id, $data); |
|
31 | + } |
|
32 | + |
|
33 | + /** |
|
34 | + * @return string |
|
35 | + */ |
|
36 | + protected function getFieldDefault(array $field) |
|
37 | + { |
|
38 | + return Arr::get($field, 'default'); |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * @return string |
|
43 | + */ |
|
44 | + protected function getFieldNameForDependsOn($path) |
|
45 | + { |
|
46 | + $fieldName = Str::convertPathToName($path, OptionManager::databaseKey()); |
|
47 | + return $this->isMultiDependency($path) |
|
48 | + ? $fieldName.'[]' |
|
49 | + : $fieldName; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @return array |
|
54 | + */ |
|
55 | + protected function getSettingFields($path) |
|
56 | + { |
|
57 | + return array_filter($this->settings, function ($key) use ($path) { |
|
58 | + return Str::startsWith($path, $key); |
|
59 | + }, ARRAY_FILTER_USE_KEY); |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * @return string |
|
64 | + */ |
|
65 | + protected function getSettingRows(array $fields) |
|
66 | + { |
|
67 | + $rows = ''; |
|
68 | + foreach ($fields as $name => $field) { |
|
69 | + $field = wp_parse_args($field, [ |
|
70 | + 'is_setting' => true, |
|
71 | + 'name' => $name, |
|
72 | + ]); |
|
73 | + $rows.= new Field($this->normalize($field)); |
|
74 | + } |
|
75 | + return $rows; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * @param string $id |
|
80 | + * @return array |
|
81 | + */ |
|
82 | + protected function getTemplateData($id) |
|
83 | + { |
|
84 | + $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
85 | + return [ |
|
86 | + 'context' => [ |
|
87 | + 'rows' => $this->getSettingRows($fields), |
|
88 | + ], |
|
89 | + ]; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * @param string $id |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + protected function getTemplateDataForAddons($id) |
|
97 | + { |
|
98 | + $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
99 | + $settings = Arr::convertFromDotNotation($fields); |
|
100 | + $settingKeys = array_keys($settings['settings']['addons']); |
|
101 | + $results = []; |
|
102 | + foreach ($settingKeys as $key) { |
|
103 | + $addonFields = array_filter($fields, function ($path) use ($key) { |
|
104 | + return Str::startsWith('settings.addons.'.$key, $path); |
|
105 | + }, ARRAY_FILTER_USE_KEY); |
|
106 | + $results[$key] = $this->getSettingRows($addonFields); |
|
107 | + } |
|
108 | + ksort($results); |
|
109 | + return [ |
|
110 | + 'settings' => $results, |
|
111 | + ]; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @param string $id |
|
116 | + * @return array |
|
117 | + */ |
|
118 | + protected function getTemplateDataForLicenses($id) |
|
119 | + { |
|
120 | + $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
121 | + ksort($fields); |
|
122 | + return [ |
|
123 | + 'context' => [ |
|
124 | + 'rows' => $this->getSettingRows($fields), |
|
125 | + ], |
|
126 | + ]; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @return array |
|
131 | + */ |
|
132 | + protected function getTemplateDataForTranslations() |
|
133 | + { |
|
134 | + $translations = glsr(Translation::class)->renderAll(); |
|
135 | + $class = empty($translations) |
|
136 | + ? 'glsr-hidden' |
|
137 | + : ''; |
|
138 | + return [ |
|
139 | + 'context' => [ |
|
140 | + 'class' => $class, |
|
141 | + 'database_key' => OptionManager::databaseKey(), |
|
142 | + 'translations' => $translations, |
|
143 | + ], |
|
144 | + ]; |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @param string $path |
|
149 | + * @param string|array $expectedValue |
|
150 | + * @return bool |
|
151 | + */ |
|
152 | + protected function isFieldHidden($path, $expectedValue) |
|
153 | + { |
|
154 | + $optionValue = glsr(OptionManager::class)->get( |
|
155 | + $path, |
|
156 | + Arr::get(glsr()->defaults, $path) |
|
157 | + ); |
|
158 | + if (is_array($expectedValue)) { |
|
159 | + return is_array($optionValue) |
|
160 | + ? 0 === count(array_intersect($optionValue, $expectedValue)) |
|
161 | + : !in_array($optionValue, $expectedValue); |
|
162 | + } |
|
163 | + return $optionValue != $expectedValue; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @return bool |
|
168 | + */ |
|
169 | + protected function isMultiDependency($path) |
|
170 | + { |
|
171 | + if (isset($this->settings[$path])) { |
|
172 | + $field = $this->settings[$path]; |
|
173 | + return ('checkbox' == $field['type'] && !empty($field['options'])) |
|
174 | + || !empty($field['multiple']); |
|
175 | + } |
|
176 | + return false; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * @return array |
|
181 | + */ |
|
182 | + protected function normalize(array $field) |
|
183 | + { |
|
184 | + $field = $this->normalizeDependsOn($field); |
|
185 | + $field = $this->normalizeLabelAndLegend($field); |
|
186 | + $field = $this->normalizeValue($field); |
|
187 | + return $field; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * @return array |
|
192 | + */ |
|
193 | + protected function normalizeDependsOn(array $field) |
|
194 | + { |
|
195 | + if (!empty($field['depends_on']) && is_array($field['depends_on'])) { |
|
196 | + $isFieldHidden = false; |
|
197 | + $conditions = []; |
|
198 | + foreach ($field['depends_on'] as $path => $value) { |
|
199 | + $conditions[] = [ |
|
200 | + 'name' => $this->getFieldNameForDependsOn($path), |
|
201 | + 'value' => $value, |
|
202 | + ]; |
|
203 | + if ($this->isFieldHidden($path, $value)) { |
|
204 | + $isFieldHidden = true; |
|
205 | + } |
|
206 | + } |
|
207 | + $field['data-depends'] = json_encode($conditions, JSON_HEX_APOS | JSON_HEX_QUOT); |
|
208 | + $field['is_hidden'] = $isFieldHidden; |
|
209 | + } |
|
210 | + return $field; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * @return array |
|
215 | + */ |
|
216 | + protected function normalizeLabelAndLegend(array $field) |
|
217 | + { |
|
218 | + if (!empty($field['label'])) { |
|
219 | + $field['legend'] = $field['label']; |
|
220 | + unset($field['label']); |
|
221 | + } else { |
|
222 | + $field['is_valid'] = false; |
|
223 | + glsr_log()->warning('Setting field is missing a label')->debug($field); |
|
224 | + } |
|
225 | + return $field; |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * @return array |
|
230 | + */ |
|
231 | + protected function normalizeValue(array $field) |
|
232 | + { |
|
233 | + if (!isset($field['value'])) { |
|
234 | + $field['value'] = glsr(OptionManager::class)->get( |
|
235 | + $field['name'], |
|
236 | + $this->getFieldDefault($field) |
|
237 | + ); |
|
238 | + } |
|
239 | + return $field; |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * @return string |
|
244 | + */ |
|
245 | + protected function normalizeSettingPath($path) |
|
246 | + { |
|
247 | + return Str::prefix('settings.', rtrim($path, '.')); |
|
248 | + } |
|
249 | 249 | } |
@@ -7,292 +7,292 @@ |
||
7 | 7 | |
8 | 8 | class Attributes |
9 | 9 | { |
10 | - const ATTRIBUTES_A = [ |
|
11 | - 'download', 'href', 'hreflang', 'ping', 'referrerpolicy', 'rel', 'target', 'type', |
|
12 | - ]; |
|
13 | - |
|
14 | - const ATTRIBUTES_BUTTON = [ |
|
15 | - 'autofocus', 'disabled', 'form', 'formaction', 'formenctype', 'formmethod', |
|
16 | - 'formnovalidate', 'formtarget', 'name', 'type', 'value', |
|
17 | - ]; |
|
18 | - |
|
19 | - const ATTRIBUTES_FORM = [ |
|
20 | - 'accept', 'accept-charset', 'action', 'autocapitalize', 'autocomplete', 'enctype', 'method', |
|
21 | - 'name', 'novalidate', 'target', |
|
22 | - ]; |
|
23 | - |
|
24 | - const ATTRIBUTES_IMG = [ |
|
25 | - 'alt', 'crossorigin', 'decoding', 'height', 'ismap', 'referrerpolicy', 'sizes', 'src', |
|
26 | - 'srcset', 'width', 'usemap', |
|
27 | - ]; |
|
28 | - |
|
29 | - const ATTRIBUTES_INPUT = [ |
|
30 | - 'accept', 'autocomplete', 'autocorrect', 'autofocus', 'capture', 'checked', 'disabled', |
|
31 | - 'form', 'formaction', 'formenctype', 'formmethod', 'formnovalidate', 'formtarget', 'height', |
|
32 | - 'incremental', 'inputmode', 'list', 'max', 'maxlength', 'min', 'minlength', 'multiple', |
|
33 | - 'name', 'pattern', 'placeholder', 'readonly', 'results', 'required', 'selectionDirection', |
|
34 | - 'selectionEnd', 'selectionStart', 'size', 'spellcheck', 'src', 'step', 'tabindex', 'type', |
|
35 | - 'value', 'webkitdirectory', 'width', |
|
36 | - ]; |
|
37 | - |
|
38 | - const ATTRIBUTES_LABEL = [ |
|
39 | - 'for', |
|
40 | - ]; |
|
41 | - |
|
42 | - const ATTRIBUTES_OPTION = [ |
|
43 | - 'disabled', 'label', 'selected', 'value', |
|
44 | - ]; |
|
45 | - |
|
46 | - const ATTRIBUTES_SELECT = [ |
|
47 | - 'autofocus', 'disabled', 'form', 'multiple', 'name', 'required', 'size', |
|
48 | - ]; |
|
49 | - |
|
50 | - const ATTRIBUTES_TEXTAREA = [ |
|
51 | - 'autocapitalize', 'autocomplete', 'autofocus', 'cols', 'disabled', 'form', 'maxlength', |
|
52 | - 'minlength', 'name', 'placeholder', 'readonly', 'required', 'rows', 'spellcheck', 'wrap', |
|
53 | - ]; |
|
54 | - |
|
55 | - const BOOLEAN_ATTRIBUTES = [ |
|
56 | - 'autofocus', 'capture', 'checked', 'disabled', 'draggable', 'formnovalidate', 'hidden', |
|
57 | - 'multiple', 'novalidate', 'readonly', 'required', 'selected', 'spellcheck', |
|
58 | - 'webkitdirectory', |
|
59 | - ]; |
|
60 | - |
|
61 | - const GLOBAL_ATTRIBUTES = [ |
|
62 | - 'accesskey', 'class', 'contenteditable', 'contextmenu', 'dir', 'draggable', 'dropzone', |
|
63 | - 'hidden', 'id', 'lang', 'spellcheck', 'style', 'tabindex', 'title', |
|
64 | - ]; |
|
65 | - |
|
66 | - const GLOBAL_WILDCARD_ATTRIBUTES = [ |
|
67 | - 'aria-', 'data-', 'item', 'on', |
|
68 | - ]; |
|
69 | - |
|
70 | - const INPUT_TYPES = [ |
|
71 | - 'button', 'checkbox', 'color', 'date', 'datetime-local', 'email', 'file', 'hidden', 'image', |
|
72 | - 'month', 'number', 'password', 'radio', 'range', 'reset', 'search', 'submit', 'tel', 'text', |
|
73 | - 'time', 'url', 'week', |
|
74 | - ]; |
|
75 | - |
|
76 | - /** |
|
77 | - * @var array |
|
78 | - */ |
|
79 | - protected $attributes = []; |
|
80 | - |
|
81 | - /** |
|
82 | - * @param string $method |
|
83 | - * @param array $args |
|
84 | - * @return static |
|
85 | - */ |
|
86 | - public function __call($method, $args) |
|
87 | - { |
|
88 | - $constant = 'static::ATTRIBUTES_'.strtoupper($method); |
|
89 | - $allowedAttributeKeys = defined($constant) |
|
90 | - ? constant($constant) |
|
91 | - : []; |
|
92 | - $this->normalize(Arr::consolidate(Arr::get($args, 0)), $allowedAttributeKeys); |
|
93 | - $this->normalizeInputType($method); |
|
94 | - return $this; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @return static |
|
99 | - */ |
|
100 | - public function set(array $attributes) |
|
101 | - { |
|
102 | - $this->normalize($attributes); |
|
103 | - return $this; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @return array |
|
108 | - */ |
|
109 | - public function toArray() |
|
110 | - { |
|
111 | - return $this->attributes; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @return string |
|
116 | - */ |
|
117 | - public function toString() |
|
118 | - { |
|
119 | - $attributes = []; |
|
120 | - foreach ($this->attributes as $attribute => $value) { |
|
121 | - $quote = $this->getQuoteChar($attribute); |
|
122 | - $attributes[] = in_array($attribute, static::BOOLEAN_ATTRIBUTES) |
|
123 | - ? $attribute |
|
124 | - : $attribute.'='.$quote.implode(',', (array) $value).$quote; |
|
125 | - } |
|
126 | - return implode(' ', $attributes); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @return array |
|
131 | - */ |
|
132 | - protected function filterAttributes(array $allowedAttributeKeys) |
|
133 | - { |
|
134 | - return array_intersect_key($this->attributes, array_flip($allowedAttributeKeys)); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @return array |
|
139 | - */ |
|
140 | - protected function filterGlobalAttributes() |
|
141 | - { |
|
142 | - $globalAttributes = $this->filterAttributes(static::GLOBAL_ATTRIBUTES); |
|
143 | - $wildcards = []; |
|
144 | - foreach (static::GLOBAL_WILDCARD_ATTRIBUTES as $wildcard) { |
|
145 | - $newWildcards = array_filter($this->attributes, function ($key) use ($wildcard) { |
|
146 | - return Str::startsWith($wildcard, $key); |
|
147 | - }, ARRAY_FILTER_USE_KEY); |
|
148 | - $wildcards = array_merge($wildcards, $newWildcards); |
|
149 | - } |
|
150 | - return array_merge($globalAttributes, $wildcards); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * @return array |
|
155 | - */ |
|
156 | - protected function getPermanentAttributes() |
|
157 | - { |
|
158 | - $permanentAttributes = []; |
|
159 | - if (array_key_exists('value', $this->attributes)) { |
|
160 | - $permanentAttributes['value'] = $this->attributes['value']; |
|
161 | - } |
|
162 | - return $permanentAttributes; |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * @param string $attribute |
|
167 | - * @return string |
|
168 | - */ |
|
169 | - protected function getQuoteChar($attribute) |
|
170 | - { |
|
171 | - return Str::startsWith('data-', $attribute) |
|
172 | - ? '\'' |
|
173 | - : '"'; |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * @param string $key |
|
178 | - * @param mixed $value |
|
179 | - * @return bool |
|
180 | - */ |
|
181 | - protected function isAttributeKeyNumeric($key, $value) |
|
182 | - { |
|
183 | - return is_string($value) |
|
184 | - && is_numeric($key) |
|
185 | - && !array_key_exists($value, $this->attributes); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * @return void |
|
190 | - */ |
|
191 | - protected function normalize(array $args, array $allowedAttributeKeys = []) |
|
192 | - { |
|
193 | - $this->attributes = array_change_key_case($args, CASE_LOWER); |
|
194 | - $this->normalizeBooleanAttributes(); |
|
195 | - $this->normalizeDataAttributes(); |
|
196 | - $this->normalizeStringAttributes(); |
|
197 | - $this->removeEmptyAttributes(); |
|
198 | - $this->removeIndexedAttributes(); |
|
199 | - $this->attributes = array_merge( |
|
200 | - $this->filterGlobalAttributes(), |
|
201 | - $this->filterAttributes($allowedAttributeKeys) |
|
202 | - ); |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * @return void |
|
207 | - */ |
|
208 | - protected function normalizeBooleanAttributes() |
|
209 | - { |
|
210 | - foreach ($this->attributes as $key => $value) { |
|
211 | - if ($this->isAttributeKeyNumeric($key, $value)) { |
|
212 | - $key = $value; |
|
213 | - $value = true; |
|
214 | - } |
|
215 | - if (!in_array($key, static::BOOLEAN_ATTRIBUTES)) { |
|
216 | - continue; |
|
217 | - } |
|
218 | - $this->attributes[$key] = wp_validate_boolean($value); |
|
219 | - } |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * @return void |
|
224 | - */ |
|
225 | - protected function normalizeDataAttributes() |
|
226 | - { |
|
227 | - foreach ($this->attributes as $key => $value) { |
|
228 | - if ($this->isAttributeKeyNumeric($key, $value)) { |
|
229 | - $key = $value; |
|
230 | - $value = ''; |
|
231 | - } |
|
232 | - if (!Str::startsWith('data-', $key)) { |
|
233 | - continue; |
|
234 | - } |
|
235 | - if (is_array($value)) { |
|
236 | - $value = json_encode($value, JSON_HEX_APOS | JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
|
237 | - } |
|
238 | - $this->attributes[$key] = $value; |
|
239 | - } |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * @return void |
|
244 | - */ |
|
245 | - protected function normalizeStringAttributes() |
|
246 | - { |
|
247 | - foreach ($this->attributes as $key => $value) { |
|
248 | - if (is_string($value)) { |
|
249 | - $this->attributes[$key] = trim($value); |
|
250 | - } |
|
251 | - } |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * @param string $method |
|
256 | - * @return void |
|
257 | - */ |
|
258 | - protected function normalizeInputType($method) |
|
259 | - { |
|
260 | - if ('input' != $method) { |
|
261 | - return; |
|
262 | - } |
|
263 | - $attributes = wp_parse_args($this->attributes, ['type' => '']); |
|
264 | - if (!in_array($attributes['type'], static::INPUT_TYPES)) { |
|
265 | - $this->attributes['type'] = 'text'; |
|
266 | - } |
|
267 | - } |
|
268 | - |
|
269 | - /** |
|
270 | - * @return void |
|
271 | - */ |
|
272 | - protected function removeEmptyAttributes() |
|
273 | - { |
|
274 | - $attributes = $this->attributes; |
|
275 | - $permanentAttributes = $this->getPermanentAttributes(); |
|
276 | - foreach ($this->attributes as $key => $value) { |
|
277 | - if (in_array($key, static::BOOLEAN_ATTRIBUTES) && !$value) { |
|
278 | - unset($attributes[$key]); |
|
279 | - } |
|
280 | - if (Str::startsWith('data-', $key)) { |
|
281 | - $permanentAttributes[$key] = $value; |
|
282 | - unset($attributes[$key]); |
|
283 | - } |
|
284 | - } |
|
285 | - $this->attributes = array_merge(array_filter($attributes), $permanentAttributes); |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * @return void |
|
290 | - */ |
|
291 | - protected function removeIndexedAttributes() |
|
292 | - { |
|
293 | - $this->attributes = array_diff_key( |
|
294 | - $this->attributes, |
|
295 | - array_filter($this->attributes, 'is_numeric', ARRAY_FILTER_USE_KEY) |
|
296 | - ); |
|
297 | - } |
|
10 | + const ATTRIBUTES_A = [ |
|
11 | + 'download', 'href', 'hreflang', 'ping', 'referrerpolicy', 'rel', 'target', 'type', |
|
12 | + ]; |
|
13 | + |
|
14 | + const ATTRIBUTES_BUTTON = [ |
|
15 | + 'autofocus', 'disabled', 'form', 'formaction', 'formenctype', 'formmethod', |
|
16 | + 'formnovalidate', 'formtarget', 'name', 'type', 'value', |
|
17 | + ]; |
|
18 | + |
|
19 | + const ATTRIBUTES_FORM = [ |
|
20 | + 'accept', 'accept-charset', 'action', 'autocapitalize', 'autocomplete', 'enctype', 'method', |
|
21 | + 'name', 'novalidate', 'target', |
|
22 | + ]; |
|
23 | + |
|
24 | + const ATTRIBUTES_IMG = [ |
|
25 | + 'alt', 'crossorigin', 'decoding', 'height', 'ismap', 'referrerpolicy', 'sizes', 'src', |
|
26 | + 'srcset', 'width', 'usemap', |
|
27 | + ]; |
|
28 | + |
|
29 | + const ATTRIBUTES_INPUT = [ |
|
30 | + 'accept', 'autocomplete', 'autocorrect', 'autofocus', 'capture', 'checked', 'disabled', |
|
31 | + 'form', 'formaction', 'formenctype', 'formmethod', 'formnovalidate', 'formtarget', 'height', |
|
32 | + 'incremental', 'inputmode', 'list', 'max', 'maxlength', 'min', 'minlength', 'multiple', |
|
33 | + 'name', 'pattern', 'placeholder', 'readonly', 'results', 'required', 'selectionDirection', |
|
34 | + 'selectionEnd', 'selectionStart', 'size', 'spellcheck', 'src', 'step', 'tabindex', 'type', |
|
35 | + 'value', 'webkitdirectory', 'width', |
|
36 | + ]; |
|
37 | + |
|
38 | + const ATTRIBUTES_LABEL = [ |
|
39 | + 'for', |
|
40 | + ]; |
|
41 | + |
|
42 | + const ATTRIBUTES_OPTION = [ |
|
43 | + 'disabled', 'label', 'selected', 'value', |
|
44 | + ]; |
|
45 | + |
|
46 | + const ATTRIBUTES_SELECT = [ |
|
47 | + 'autofocus', 'disabled', 'form', 'multiple', 'name', 'required', 'size', |
|
48 | + ]; |
|
49 | + |
|
50 | + const ATTRIBUTES_TEXTAREA = [ |
|
51 | + 'autocapitalize', 'autocomplete', 'autofocus', 'cols', 'disabled', 'form', 'maxlength', |
|
52 | + 'minlength', 'name', 'placeholder', 'readonly', 'required', 'rows', 'spellcheck', 'wrap', |
|
53 | + ]; |
|
54 | + |
|
55 | + const BOOLEAN_ATTRIBUTES = [ |
|
56 | + 'autofocus', 'capture', 'checked', 'disabled', 'draggable', 'formnovalidate', 'hidden', |
|
57 | + 'multiple', 'novalidate', 'readonly', 'required', 'selected', 'spellcheck', |
|
58 | + 'webkitdirectory', |
|
59 | + ]; |
|
60 | + |
|
61 | + const GLOBAL_ATTRIBUTES = [ |
|
62 | + 'accesskey', 'class', 'contenteditable', 'contextmenu', 'dir', 'draggable', 'dropzone', |
|
63 | + 'hidden', 'id', 'lang', 'spellcheck', 'style', 'tabindex', 'title', |
|
64 | + ]; |
|
65 | + |
|
66 | + const GLOBAL_WILDCARD_ATTRIBUTES = [ |
|
67 | + 'aria-', 'data-', 'item', 'on', |
|
68 | + ]; |
|
69 | + |
|
70 | + const INPUT_TYPES = [ |
|
71 | + 'button', 'checkbox', 'color', 'date', 'datetime-local', 'email', 'file', 'hidden', 'image', |
|
72 | + 'month', 'number', 'password', 'radio', 'range', 'reset', 'search', 'submit', 'tel', 'text', |
|
73 | + 'time', 'url', 'week', |
|
74 | + ]; |
|
75 | + |
|
76 | + /** |
|
77 | + * @var array |
|
78 | + */ |
|
79 | + protected $attributes = []; |
|
80 | + |
|
81 | + /** |
|
82 | + * @param string $method |
|
83 | + * @param array $args |
|
84 | + * @return static |
|
85 | + */ |
|
86 | + public function __call($method, $args) |
|
87 | + { |
|
88 | + $constant = 'static::ATTRIBUTES_'.strtoupper($method); |
|
89 | + $allowedAttributeKeys = defined($constant) |
|
90 | + ? constant($constant) |
|
91 | + : []; |
|
92 | + $this->normalize(Arr::consolidate(Arr::get($args, 0)), $allowedAttributeKeys); |
|
93 | + $this->normalizeInputType($method); |
|
94 | + return $this; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @return static |
|
99 | + */ |
|
100 | + public function set(array $attributes) |
|
101 | + { |
|
102 | + $this->normalize($attributes); |
|
103 | + return $this; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @return array |
|
108 | + */ |
|
109 | + public function toArray() |
|
110 | + { |
|
111 | + return $this->attributes; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @return string |
|
116 | + */ |
|
117 | + public function toString() |
|
118 | + { |
|
119 | + $attributes = []; |
|
120 | + foreach ($this->attributes as $attribute => $value) { |
|
121 | + $quote = $this->getQuoteChar($attribute); |
|
122 | + $attributes[] = in_array($attribute, static::BOOLEAN_ATTRIBUTES) |
|
123 | + ? $attribute |
|
124 | + : $attribute.'='.$quote.implode(',', (array) $value).$quote; |
|
125 | + } |
|
126 | + return implode(' ', $attributes); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @return array |
|
131 | + */ |
|
132 | + protected function filterAttributes(array $allowedAttributeKeys) |
|
133 | + { |
|
134 | + return array_intersect_key($this->attributes, array_flip($allowedAttributeKeys)); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @return array |
|
139 | + */ |
|
140 | + protected function filterGlobalAttributes() |
|
141 | + { |
|
142 | + $globalAttributes = $this->filterAttributes(static::GLOBAL_ATTRIBUTES); |
|
143 | + $wildcards = []; |
|
144 | + foreach (static::GLOBAL_WILDCARD_ATTRIBUTES as $wildcard) { |
|
145 | + $newWildcards = array_filter($this->attributes, function ($key) use ($wildcard) { |
|
146 | + return Str::startsWith($wildcard, $key); |
|
147 | + }, ARRAY_FILTER_USE_KEY); |
|
148 | + $wildcards = array_merge($wildcards, $newWildcards); |
|
149 | + } |
|
150 | + return array_merge($globalAttributes, $wildcards); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * @return array |
|
155 | + */ |
|
156 | + protected function getPermanentAttributes() |
|
157 | + { |
|
158 | + $permanentAttributes = []; |
|
159 | + if (array_key_exists('value', $this->attributes)) { |
|
160 | + $permanentAttributes['value'] = $this->attributes['value']; |
|
161 | + } |
|
162 | + return $permanentAttributes; |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * @param string $attribute |
|
167 | + * @return string |
|
168 | + */ |
|
169 | + protected function getQuoteChar($attribute) |
|
170 | + { |
|
171 | + return Str::startsWith('data-', $attribute) |
|
172 | + ? '\'' |
|
173 | + : '"'; |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * @param string $key |
|
178 | + * @param mixed $value |
|
179 | + * @return bool |
|
180 | + */ |
|
181 | + protected function isAttributeKeyNumeric($key, $value) |
|
182 | + { |
|
183 | + return is_string($value) |
|
184 | + && is_numeric($key) |
|
185 | + && !array_key_exists($value, $this->attributes); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * @return void |
|
190 | + */ |
|
191 | + protected function normalize(array $args, array $allowedAttributeKeys = []) |
|
192 | + { |
|
193 | + $this->attributes = array_change_key_case($args, CASE_LOWER); |
|
194 | + $this->normalizeBooleanAttributes(); |
|
195 | + $this->normalizeDataAttributes(); |
|
196 | + $this->normalizeStringAttributes(); |
|
197 | + $this->removeEmptyAttributes(); |
|
198 | + $this->removeIndexedAttributes(); |
|
199 | + $this->attributes = array_merge( |
|
200 | + $this->filterGlobalAttributes(), |
|
201 | + $this->filterAttributes($allowedAttributeKeys) |
|
202 | + ); |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * @return void |
|
207 | + */ |
|
208 | + protected function normalizeBooleanAttributes() |
|
209 | + { |
|
210 | + foreach ($this->attributes as $key => $value) { |
|
211 | + if ($this->isAttributeKeyNumeric($key, $value)) { |
|
212 | + $key = $value; |
|
213 | + $value = true; |
|
214 | + } |
|
215 | + if (!in_array($key, static::BOOLEAN_ATTRIBUTES)) { |
|
216 | + continue; |
|
217 | + } |
|
218 | + $this->attributes[$key] = wp_validate_boolean($value); |
|
219 | + } |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * @return void |
|
224 | + */ |
|
225 | + protected function normalizeDataAttributes() |
|
226 | + { |
|
227 | + foreach ($this->attributes as $key => $value) { |
|
228 | + if ($this->isAttributeKeyNumeric($key, $value)) { |
|
229 | + $key = $value; |
|
230 | + $value = ''; |
|
231 | + } |
|
232 | + if (!Str::startsWith('data-', $key)) { |
|
233 | + continue; |
|
234 | + } |
|
235 | + if (is_array($value)) { |
|
236 | + $value = json_encode($value, JSON_HEX_APOS | JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
|
237 | + } |
|
238 | + $this->attributes[$key] = $value; |
|
239 | + } |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * @return void |
|
244 | + */ |
|
245 | + protected function normalizeStringAttributes() |
|
246 | + { |
|
247 | + foreach ($this->attributes as $key => $value) { |
|
248 | + if (is_string($value)) { |
|
249 | + $this->attributes[$key] = trim($value); |
|
250 | + } |
|
251 | + } |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * @param string $method |
|
256 | + * @return void |
|
257 | + */ |
|
258 | + protected function normalizeInputType($method) |
|
259 | + { |
|
260 | + if ('input' != $method) { |
|
261 | + return; |
|
262 | + } |
|
263 | + $attributes = wp_parse_args($this->attributes, ['type' => '']); |
|
264 | + if (!in_array($attributes['type'], static::INPUT_TYPES)) { |
|
265 | + $this->attributes['type'] = 'text'; |
|
266 | + } |
|
267 | + } |
|
268 | + |
|
269 | + /** |
|
270 | + * @return void |
|
271 | + */ |
|
272 | + protected function removeEmptyAttributes() |
|
273 | + { |
|
274 | + $attributes = $this->attributes; |
|
275 | + $permanentAttributes = $this->getPermanentAttributes(); |
|
276 | + foreach ($this->attributes as $key => $value) { |
|
277 | + if (in_array($key, static::BOOLEAN_ATTRIBUTES) && !$value) { |
|
278 | + unset($attributes[$key]); |
|
279 | + } |
|
280 | + if (Str::startsWith('data-', $key)) { |
|
281 | + $permanentAttributes[$key] = $value; |
|
282 | + unset($attributes[$key]); |
|
283 | + } |
|
284 | + } |
|
285 | + $this->attributes = array_merge(array_filter($attributes), $permanentAttributes); |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * @return void |
|
290 | + */ |
|
291 | + protected function removeIndexedAttributes() |
|
292 | + { |
|
293 | + $this->attributes = array_diff_key( |
|
294 | + $this->attributes, |
|
295 | + array_filter($this->attributes, 'is_numeric', ARRAY_FILTER_USE_KEY) |
|
296 | + ); |
|
297 | + } |
|
298 | 298 | } |
@@ -13,378 +13,378 @@ |
||
13 | 13 | |
14 | 14 | class System |
15 | 15 | { |
16 | - const PAD = 40; |
|
16 | + const PAD = 40; |
|
17 | 17 | |
18 | - /** |
|
19 | - * @return string |
|
20 | - */ |
|
21 | - public function __toString() |
|
22 | - { |
|
23 | - return $this->get(); |
|
24 | - } |
|
18 | + /** |
|
19 | + * @return string |
|
20 | + */ |
|
21 | + public function __toString() |
|
22 | + { |
|
23 | + return $this->get(); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * @return string |
|
28 | - */ |
|
29 | - public function get() |
|
30 | - { |
|
31 | - $details = [ |
|
32 | - 'plugin' => 'Plugin Details', |
|
33 | - 'addon' => 'Addon Details', |
|
34 | - 'browser' => 'Browser Details', |
|
35 | - 'server' => 'Server Details', |
|
36 | - 'php' => 'PHP Configuration', |
|
37 | - 'wordpress' => 'WordPress Configuration', |
|
38 | - 'mu-plugin' => 'Must-Use Plugins', |
|
39 | - 'multisite-plugin' => 'Network Active Plugins', |
|
40 | - 'active-plugin' => 'Active Plugins', |
|
41 | - 'inactive-plugin' => 'Inactive Plugins', |
|
42 | - 'setting' => 'Plugin Settings', |
|
43 | - 'reviews' => 'Review Counts', |
|
44 | - ]; |
|
45 | - $systemInfo = array_reduce(array_keys($details), function ($carry, $key) use ($details) { |
|
46 | - $methodName = Helper::buildMethodName('get-'.$key.'-details'); |
|
47 | - if (method_exists($this, $methodName) && $systemDetails = $this->$methodName()) { |
|
48 | - return $carry.$this->implode( |
|
49 | - strtoupper($details[$key]), |
|
50 | - apply_filters('site-reviews/system/'.$key, $systemDetails) |
|
51 | - ); |
|
52 | - } |
|
53 | - return $carry; |
|
54 | - }); |
|
55 | - return trim($systemInfo); |
|
56 | - } |
|
26 | + /** |
|
27 | + * @return string |
|
28 | + */ |
|
29 | + public function get() |
|
30 | + { |
|
31 | + $details = [ |
|
32 | + 'plugin' => 'Plugin Details', |
|
33 | + 'addon' => 'Addon Details', |
|
34 | + 'browser' => 'Browser Details', |
|
35 | + 'server' => 'Server Details', |
|
36 | + 'php' => 'PHP Configuration', |
|
37 | + 'wordpress' => 'WordPress Configuration', |
|
38 | + 'mu-plugin' => 'Must-Use Plugins', |
|
39 | + 'multisite-plugin' => 'Network Active Plugins', |
|
40 | + 'active-plugin' => 'Active Plugins', |
|
41 | + 'inactive-plugin' => 'Inactive Plugins', |
|
42 | + 'setting' => 'Plugin Settings', |
|
43 | + 'reviews' => 'Review Counts', |
|
44 | + ]; |
|
45 | + $systemInfo = array_reduce(array_keys($details), function ($carry, $key) use ($details) { |
|
46 | + $methodName = Helper::buildMethodName('get-'.$key.'-details'); |
|
47 | + if (method_exists($this, $methodName) && $systemDetails = $this->$methodName()) { |
|
48 | + return $carry.$this->implode( |
|
49 | + strtoupper($details[$key]), |
|
50 | + apply_filters('site-reviews/system/'.$key, $systemDetails) |
|
51 | + ); |
|
52 | + } |
|
53 | + return $carry; |
|
54 | + }); |
|
55 | + return trim($systemInfo); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * @return array |
|
60 | - */ |
|
61 | - public function getActivePluginDetails() |
|
62 | - { |
|
63 | - $plugins = get_plugins(); |
|
64 | - $activePlugins = glsr(OptionManager::class)->getWP('active_plugins', [], 'array'); |
|
65 | - $inactive = array_diff_key($plugins, array_flip($activePlugins)); |
|
66 | - return $this->normalizePluginList(array_diff_key($plugins, $inactive)); |
|
67 | - } |
|
58 | + /** |
|
59 | + * @return array |
|
60 | + */ |
|
61 | + public function getActivePluginDetails() |
|
62 | + { |
|
63 | + $plugins = get_plugins(); |
|
64 | + $activePlugins = glsr(OptionManager::class)->getWP('active_plugins', [], 'array'); |
|
65 | + $inactive = array_diff_key($plugins, array_flip($activePlugins)); |
|
66 | + return $this->normalizePluginList(array_diff_key($plugins, $inactive)); |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * @return array |
|
71 | - */ |
|
72 | - public function getAddonDetails() |
|
73 | - { |
|
74 | - $details = apply_filters('site-reviews/addon/system-info', []); |
|
75 | - ksort($details); |
|
76 | - return $details; |
|
77 | - } |
|
69 | + /** |
|
70 | + * @return array |
|
71 | + */ |
|
72 | + public function getAddonDetails() |
|
73 | + { |
|
74 | + $details = apply_filters('site-reviews/addon/system-info', []); |
|
75 | + ksort($details); |
|
76 | + return $details; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @return array |
|
81 | - */ |
|
82 | - public function getBrowserDetails() |
|
83 | - { |
|
84 | - $browser = new Browser(); |
|
85 | - $name = esc_attr($browser->getName()); |
|
86 | - $userAgent = esc_attr($browser->getUserAgent()->getUserAgentString()); |
|
87 | - $version = esc_attr($browser->getVersion()); |
|
88 | - return [ |
|
89 | - 'Browser Name' => sprintf('%s %s', $name, $version), |
|
90 | - 'Browser UA' => $userAgent, |
|
91 | - ]; |
|
92 | - } |
|
79 | + /** |
|
80 | + * @return array |
|
81 | + */ |
|
82 | + public function getBrowserDetails() |
|
83 | + { |
|
84 | + $browser = new Browser(); |
|
85 | + $name = esc_attr($browser->getName()); |
|
86 | + $userAgent = esc_attr($browser->getUserAgent()->getUserAgentString()); |
|
87 | + $version = esc_attr($browser->getVersion()); |
|
88 | + return [ |
|
89 | + 'Browser Name' => sprintf('%s %s', $name, $version), |
|
90 | + 'Browser UA' => $userAgent, |
|
91 | + ]; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * @return array |
|
96 | - */ |
|
97 | - public function getInactivePluginDetails() |
|
98 | - { |
|
99 | - $activePlugins = glsr(OptionManager::class)->getWP('active_plugins', [], 'array'); |
|
100 | - $inactivePlugins = $this->normalizePluginList(array_diff_key(get_plugins(), array_flip($activePlugins))); |
|
101 | - $multisitePlugins = $this->getMultisitePluginDetails(); |
|
102 | - return empty($multisitePlugins) |
|
103 | - ? $inactivePlugins |
|
104 | - : array_diff($inactivePlugins, $multisitePlugins); |
|
105 | - } |
|
94 | + /** |
|
95 | + * @return array |
|
96 | + */ |
|
97 | + public function getInactivePluginDetails() |
|
98 | + { |
|
99 | + $activePlugins = glsr(OptionManager::class)->getWP('active_plugins', [], 'array'); |
|
100 | + $inactivePlugins = $this->normalizePluginList(array_diff_key(get_plugins(), array_flip($activePlugins))); |
|
101 | + $multisitePlugins = $this->getMultisitePluginDetails(); |
|
102 | + return empty($multisitePlugins) |
|
103 | + ? $inactivePlugins |
|
104 | + : array_diff($inactivePlugins, $multisitePlugins); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * @return array |
|
109 | - */ |
|
110 | - public function getMuPluginDetails() |
|
111 | - { |
|
112 | - if (empty($plugins = get_mu_plugins())) { |
|
113 | - return []; |
|
114 | - } |
|
115 | - return $this->normalizePluginList($plugins); |
|
116 | - } |
|
107 | + /** |
|
108 | + * @return array |
|
109 | + */ |
|
110 | + public function getMuPluginDetails() |
|
111 | + { |
|
112 | + if (empty($plugins = get_mu_plugins())) { |
|
113 | + return []; |
|
114 | + } |
|
115 | + return $this->normalizePluginList($plugins); |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * @return array |
|
120 | - */ |
|
121 | - public function getMultisitePluginDetails() |
|
122 | - { |
|
123 | - $activePlugins = (array) get_site_option('active_sitewide_plugins', []); |
|
124 | - if (!is_multisite() || empty($activePlugins)) { |
|
125 | - return []; |
|
126 | - } |
|
127 | - return $this->normalizePluginList(array_intersect_key(get_plugins(), $activePlugins)); |
|
128 | - } |
|
118 | + /** |
|
119 | + * @return array |
|
120 | + */ |
|
121 | + public function getMultisitePluginDetails() |
|
122 | + { |
|
123 | + $activePlugins = (array) get_site_option('active_sitewide_plugins', []); |
|
124 | + if (!is_multisite() || empty($activePlugins)) { |
|
125 | + return []; |
|
126 | + } |
|
127 | + return $this->normalizePluginList(array_intersect_key(get_plugins(), $activePlugins)); |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * @return array |
|
132 | - */ |
|
133 | - public function getPhpDetails() |
|
134 | - { |
|
135 | - $displayErrors = $this->getINI('display_errors', null) |
|
136 | - ? 'On ('.$this->getINI('display_errors').')' |
|
137 | - : 'N/A'; |
|
138 | - $intlSupport = extension_loaded('intl') |
|
139 | - ? phpversion('intl') |
|
140 | - : 'false'; |
|
141 | - return [ |
|
142 | - 'cURL' => var_export(function_exists('curl_init'), true), |
|
143 | - 'Default Charset' => $this->getINI('default_charset'), |
|
144 | - 'Display Errors' => $displayErrors, |
|
145 | - 'fsockopen' => var_export(function_exists('fsockopen'), true), |
|
146 | - 'Intl' => $intlSupport, |
|
147 | - 'IPv6' => var_export(defined('AF_INET6'), true), |
|
148 | - 'Max Execution Time' => $this->getINI('max_execution_time'), |
|
149 | - 'Max Input Nesting Level' => $this->getINI('max_input_nesting_level'), |
|
150 | - 'Max Input Vars' => $this->getINI('max_input_vars'), |
|
151 | - 'Memory Limit' => $this->getINI('memory_limit'), |
|
152 | - 'Post Max Size' => $this->getINI('post_max_size'), |
|
153 | - 'Sendmail Path' => $this->getINI('sendmail_path'), |
|
154 | - 'Session Cookie Path' => esc_html($this->getINI('session.cookie_path')), |
|
155 | - 'Session Name' => esc_html($this->getINI('session.name')), |
|
156 | - 'Session Save Path' => esc_html($this->getINI('session.save_path')), |
|
157 | - 'Session Use Cookies' => var_export(wp_validate_boolean($this->getINI('session.use_cookies', false)), true), |
|
158 | - 'Session Use Only Cookies' => var_export(wp_validate_boolean($this->getINI('session.use_only_cookies', false)), true), |
|
159 | - 'Upload Max Filesize' => $this->getINI('upload_max_filesize'), |
|
160 | - ]; |
|
161 | - } |
|
130 | + /** |
|
131 | + * @return array |
|
132 | + */ |
|
133 | + public function getPhpDetails() |
|
134 | + { |
|
135 | + $displayErrors = $this->getINI('display_errors', null) |
|
136 | + ? 'On ('.$this->getINI('display_errors').')' |
|
137 | + : 'N/A'; |
|
138 | + $intlSupport = extension_loaded('intl') |
|
139 | + ? phpversion('intl') |
|
140 | + : 'false'; |
|
141 | + return [ |
|
142 | + 'cURL' => var_export(function_exists('curl_init'), true), |
|
143 | + 'Default Charset' => $this->getINI('default_charset'), |
|
144 | + 'Display Errors' => $displayErrors, |
|
145 | + 'fsockopen' => var_export(function_exists('fsockopen'), true), |
|
146 | + 'Intl' => $intlSupport, |
|
147 | + 'IPv6' => var_export(defined('AF_INET6'), true), |
|
148 | + 'Max Execution Time' => $this->getINI('max_execution_time'), |
|
149 | + 'Max Input Nesting Level' => $this->getINI('max_input_nesting_level'), |
|
150 | + 'Max Input Vars' => $this->getINI('max_input_vars'), |
|
151 | + 'Memory Limit' => $this->getINI('memory_limit'), |
|
152 | + 'Post Max Size' => $this->getINI('post_max_size'), |
|
153 | + 'Sendmail Path' => $this->getINI('sendmail_path'), |
|
154 | + 'Session Cookie Path' => esc_html($this->getINI('session.cookie_path')), |
|
155 | + 'Session Name' => esc_html($this->getINI('session.name')), |
|
156 | + 'Session Save Path' => esc_html($this->getINI('session.save_path')), |
|
157 | + 'Session Use Cookies' => var_export(wp_validate_boolean($this->getINI('session.use_cookies', false)), true), |
|
158 | + 'Session Use Only Cookies' => var_export(wp_validate_boolean($this->getINI('session.use_only_cookies', false)), true), |
|
159 | + 'Upload Max Filesize' => $this->getINI('upload_max_filesize'), |
|
160 | + ]; |
|
161 | + } |
|
162 | 162 | |
163 | - /** |
|
164 | - * @return array |
|
165 | - */ |
|
166 | - public function getReviewsDetails() |
|
167 | - { |
|
168 | - $counts = glsr(CountsManager::class)->getCounts(); |
|
169 | - $counts = Arr::flatten($counts); |
|
170 | - array_walk($counts, function (&$ratings) use ($counts) { |
|
171 | - if (is_array($ratings)) { |
|
172 | - $ratings = array_sum($ratings).' ('.implode(', ', $ratings).')'; |
|
173 | - return; |
|
174 | - } |
|
175 | - glsr_log() |
|
176 | - ->error('$ratings is not an array, possibly due to incorrectly imported reviews.') |
|
177 | - ->debug($ratings) |
|
178 | - ->debug($counts); |
|
179 | - }); |
|
180 | - ksort($counts); |
|
181 | - return $counts; |
|
182 | - } |
|
163 | + /** |
|
164 | + * @return array |
|
165 | + */ |
|
166 | + public function getReviewsDetails() |
|
167 | + { |
|
168 | + $counts = glsr(CountsManager::class)->getCounts(); |
|
169 | + $counts = Arr::flatten($counts); |
|
170 | + array_walk($counts, function (&$ratings) use ($counts) { |
|
171 | + if (is_array($ratings)) { |
|
172 | + $ratings = array_sum($ratings).' ('.implode(', ', $ratings).')'; |
|
173 | + return; |
|
174 | + } |
|
175 | + glsr_log() |
|
176 | + ->error('$ratings is not an array, possibly due to incorrectly imported reviews.') |
|
177 | + ->debug($ratings) |
|
178 | + ->debug($counts); |
|
179 | + }); |
|
180 | + ksort($counts); |
|
181 | + return $counts; |
|
182 | + } |
|
183 | 183 | |
184 | - /** |
|
185 | - * @return array |
|
186 | - */ |
|
187 | - public function getServerDetails() |
|
188 | - { |
|
189 | - global $wpdb; |
|
190 | - return [ |
|
191 | - 'Host Name' => $this->getHostName(), |
|
192 | - 'MySQL Version' => $wpdb->db_version(), |
|
193 | - 'PHP Version' => PHP_VERSION, |
|
194 | - 'Server Software' => filter_input(INPUT_SERVER, 'SERVER_SOFTWARE'), |
|
195 | - ]; |
|
196 | - } |
|
184 | + /** |
|
185 | + * @return array |
|
186 | + */ |
|
187 | + public function getServerDetails() |
|
188 | + { |
|
189 | + global $wpdb; |
|
190 | + return [ |
|
191 | + 'Host Name' => $this->getHostName(), |
|
192 | + 'MySQL Version' => $wpdb->db_version(), |
|
193 | + 'PHP Version' => PHP_VERSION, |
|
194 | + 'Server Software' => filter_input(INPUT_SERVER, 'SERVER_SOFTWARE'), |
|
195 | + ]; |
|
196 | + } |
|
197 | 197 | |
198 | - /** |
|
199 | - * @return array |
|
200 | - */ |
|
201 | - public function getSettingDetails() |
|
202 | - { |
|
203 | - $settings = glsr(OptionManager::class)->get('settings', []); |
|
204 | - $settings = Arr::flatten($settings, true); |
|
205 | - $settings = $this->purgeSensitiveData($settings); |
|
206 | - ksort($settings); |
|
207 | - $details = []; |
|
208 | - foreach ($settings as $key => $value) { |
|
209 | - if (Str::startsWith('strings', $key) && Str::endsWith('id', $key)) { |
|
210 | - continue; |
|
211 | - } |
|
212 | - $value = htmlspecialchars(trim(preg_replace('/\s\s+/', '\\n', $value)), ENT_QUOTES, 'UTF-8'); |
|
213 | - $details[$key] = $value; |
|
214 | - } |
|
215 | - return $details; |
|
216 | - } |
|
198 | + /** |
|
199 | + * @return array |
|
200 | + */ |
|
201 | + public function getSettingDetails() |
|
202 | + { |
|
203 | + $settings = glsr(OptionManager::class)->get('settings', []); |
|
204 | + $settings = Arr::flatten($settings, true); |
|
205 | + $settings = $this->purgeSensitiveData($settings); |
|
206 | + ksort($settings); |
|
207 | + $details = []; |
|
208 | + foreach ($settings as $key => $value) { |
|
209 | + if (Str::startsWith('strings', $key) && Str::endsWith('id', $key)) { |
|
210 | + continue; |
|
211 | + } |
|
212 | + $value = htmlspecialchars(trim(preg_replace('/\s\s+/', '\\n', $value)), ENT_QUOTES, 'UTF-8'); |
|
213 | + $details[$key] = $value; |
|
214 | + } |
|
215 | + return $details; |
|
216 | + } |
|
217 | 217 | |
218 | - /** |
|
219 | - * @return array |
|
220 | - */ |
|
221 | - public function getPluginDetails() |
|
222 | - { |
|
223 | - return [ |
|
224 | - 'Console level' => glsr(Console::class)->humanLevel(), |
|
225 | - 'Console size' => glsr(Console::class)->humanSize('0'), |
|
226 | - 'Last Migration Run' => glsr(Date::class)->localized(glsr(OptionManager::class)->get('last_migration_run'), 'unknown'), |
|
227 | - 'Last Rating Count' => glsr(Date::class)->localized(glsr(OptionManager::class)->get('last_review_count'), 'unknown'), |
|
228 | - 'Version (current)' => glsr()->version, |
|
229 | - 'Version (previous)' => glsr(OptionManager::class)->get('version_upgraded_from'), |
|
230 | - ]; |
|
231 | - } |
|
218 | + /** |
|
219 | + * @return array |
|
220 | + */ |
|
221 | + public function getPluginDetails() |
|
222 | + { |
|
223 | + return [ |
|
224 | + 'Console level' => glsr(Console::class)->humanLevel(), |
|
225 | + 'Console size' => glsr(Console::class)->humanSize('0'), |
|
226 | + 'Last Migration Run' => glsr(Date::class)->localized(glsr(OptionManager::class)->get('last_migration_run'), 'unknown'), |
|
227 | + 'Last Rating Count' => glsr(Date::class)->localized(glsr(OptionManager::class)->get('last_review_count'), 'unknown'), |
|
228 | + 'Version (current)' => glsr()->version, |
|
229 | + 'Version (previous)' => glsr(OptionManager::class)->get('version_upgraded_from'), |
|
230 | + ]; |
|
231 | + } |
|
232 | 232 | |
233 | - /** |
|
234 | - * @return array |
|
235 | - */ |
|
236 | - public function getWordpressDetails() |
|
237 | - { |
|
238 | - global $wpdb; |
|
239 | - $theme = wp_get_theme(); |
|
240 | - return [ |
|
241 | - 'Active Theme' => sprintf('%s v%s', (string) $theme->Name, (string) $theme->Version), |
|
242 | - 'Email Domain' => substr(strrchr(glsr(OptionManager::class)->getWP('admin_email'), '@'), 1), |
|
243 | - 'Home URL' => home_url(), |
|
244 | - 'Language' => get_locale(), |
|
245 | - 'Memory Limit' => WP_MEMORY_LIMIT, |
|
246 | - 'Multisite' => var_export(is_multisite(), true), |
|
247 | - 'Page For Posts ID' => glsr(OptionManager::class)->getWP('page_for_posts'), |
|
248 | - 'Page On Front ID' => glsr(OptionManager::class)->getWP('page_on_front'), |
|
249 | - 'Permalink Structure' => glsr(OptionManager::class)->getWP('permalink_structure', 'default'), |
|
250 | - 'Post Stati' => implode(', ', get_post_stati()), |
|
251 | - 'Remote Post' => glsr(Cache::class)->getRemotePostTest(), |
|
252 | - 'Show On Front' => glsr(OptionManager::class)->getWP('show_on_front'), |
|
253 | - 'Site URL' => site_url(), |
|
254 | - 'Timezone' => glsr(OptionManager::class)->getWP('timezone_string', $this->getINI('date.timezone').' (PHP)'), |
|
255 | - 'Version' => get_bloginfo('version'), |
|
256 | - 'WP Debug' => var_export(defined('WP_DEBUG'), true), |
|
257 | - 'WP Max Upload Size' => size_format(wp_max_upload_size()), |
|
258 | - 'WP Memory Limit' => WP_MEMORY_LIMIT, |
|
259 | - ]; |
|
260 | - } |
|
233 | + /** |
|
234 | + * @return array |
|
235 | + */ |
|
236 | + public function getWordpressDetails() |
|
237 | + { |
|
238 | + global $wpdb; |
|
239 | + $theme = wp_get_theme(); |
|
240 | + return [ |
|
241 | + 'Active Theme' => sprintf('%s v%s', (string) $theme->Name, (string) $theme->Version), |
|
242 | + 'Email Domain' => substr(strrchr(glsr(OptionManager::class)->getWP('admin_email'), '@'), 1), |
|
243 | + 'Home URL' => home_url(), |
|
244 | + 'Language' => get_locale(), |
|
245 | + 'Memory Limit' => WP_MEMORY_LIMIT, |
|
246 | + 'Multisite' => var_export(is_multisite(), true), |
|
247 | + 'Page For Posts ID' => glsr(OptionManager::class)->getWP('page_for_posts'), |
|
248 | + 'Page On Front ID' => glsr(OptionManager::class)->getWP('page_on_front'), |
|
249 | + 'Permalink Structure' => glsr(OptionManager::class)->getWP('permalink_structure', 'default'), |
|
250 | + 'Post Stati' => implode(', ', get_post_stati()), |
|
251 | + 'Remote Post' => glsr(Cache::class)->getRemotePostTest(), |
|
252 | + 'Show On Front' => glsr(OptionManager::class)->getWP('show_on_front'), |
|
253 | + 'Site URL' => site_url(), |
|
254 | + 'Timezone' => glsr(OptionManager::class)->getWP('timezone_string', $this->getINI('date.timezone').' (PHP)'), |
|
255 | + 'Version' => get_bloginfo('version'), |
|
256 | + 'WP Debug' => var_export(defined('WP_DEBUG'), true), |
|
257 | + 'WP Max Upload Size' => size_format(wp_max_upload_size()), |
|
258 | + 'WP Memory Limit' => WP_MEMORY_LIMIT, |
|
259 | + ]; |
|
260 | + } |
|
261 | 261 | |
262 | - /** |
|
263 | - * @return string |
|
264 | - */ |
|
265 | - protected function detectWebhostProvider() |
|
266 | - { |
|
267 | - $checks = [ |
|
268 | - '.accountservergroup.com' => 'Site5', |
|
269 | - '.gridserver.com' => 'MediaTemple Grid', |
|
270 | - '.inmotionhosting.com' => 'InMotion Hosting', |
|
271 | - '.ovh.net' => 'OVH', |
|
272 | - '.pair.com' => 'pair Networks', |
|
273 | - '.stabletransit.com' => 'Rackspace Cloud', |
|
274 | - '.stratoserver.net' => 'STRATO', |
|
275 | - '.sysfix.eu' => 'SysFix.eu Power Hosting', |
|
276 | - 'bluehost.com' => 'Bluehost', |
|
277 | - 'DH_USER' => 'DreamHost', |
|
278 | - 'Flywheel' => 'Flywheel', |
|
279 | - 'ipagemysql.com' => 'iPage', |
|
280 | - 'ipowermysql.com' => 'IPower', |
|
281 | - 'localhost:/tmp/mysql5.sock' => 'ICDSoft', |
|
282 | - 'mysqlv5' => 'NetworkSolutions', |
|
283 | - 'PAGELYBIN' => 'Pagely', |
|
284 | - 'secureserver.net' => 'GoDaddy', |
|
285 | - 'WPE_APIKEY' => 'WP Engine', |
|
286 | - ]; |
|
287 | - foreach ($checks as $key => $value) { |
|
288 | - if (!$this->isWebhostCheckValid($key)) { |
|
289 | - continue; |
|
290 | - } |
|
291 | - return $value; |
|
292 | - } |
|
293 | - return implode(',', array_filter([DB_HOST, filter_input(INPUT_SERVER, 'SERVER_NAME')])); |
|
294 | - } |
|
262 | + /** |
|
263 | + * @return string |
|
264 | + */ |
|
265 | + protected function detectWebhostProvider() |
|
266 | + { |
|
267 | + $checks = [ |
|
268 | + '.accountservergroup.com' => 'Site5', |
|
269 | + '.gridserver.com' => 'MediaTemple Grid', |
|
270 | + '.inmotionhosting.com' => 'InMotion Hosting', |
|
271 | + '.ovh.net' => 'OVH', |
|
272 | + '.pair.com' => 'pair Networks', |
|
273 | + '.stabletransit.com' => 'Rackspace Cloud', |
|
274 | + '.stratoserver.net' => 'STRATO', |
|
275 | + '.sysfix.eu' => 'SysFix.eu Power Hosting', |
|
276 | + 'bluehost.com' => 'Bluehost', |
|
277 | + 'DH_USER' => 'DreamHost', |
|
278 | + 'Flywheel' => 'Flywheel', |
|
279 | + 'ipagemysql.com' => 'iPage', |
|
280 | + 'ipowermysql.com' => 'IPower', |
|
281 | + 'localhost:/tmp/mysql5.sock' => 'ICDSoft', |
|
282 | + 'mysqlv5' => 'NetworkSolutions', |
|
283 | + 'PAGELYBIN' => 'Pagely', |
|
284 | + 'secureserver.net' => 'GoDaddy', |
|
285 | + 'WPE_APIKEY' => 'WP Engine', |
|
286 | + ]; |
|
287 | + foreach ($checks as $key => $value) { |
|
288 | + if (!$this->isWebhostCheckValid($key)) { |
|
289 | + continue; |
|
290 | + } |
|
291 | + return $value; |
|
292 | + } |
|
293 | + return implode(',', array_filter([DB_HOST, filter_input(INPUT_SERVER, 'SERVER_NAME')])); |
|
294 | + } |
|
295 | 295 | |
296 | - /** |
|
297 | - * @return string |
|
298 | - */ |
|
299 | - protected function getHostName() |
|
300 | - { |
|
301 | - return sprintf('%s (%s)', |
|
302 | - $this->detectWebhostProvider(), |
|
303 | - Helper::getIpAddress() |
|
304 | - ); |
|
305 | - } |
|
296 | + /** |
|
297 | + * @return string |
|
298 | + */ |
|
299 | + protected function getHostName() |
|
300 | + { |
|
301 | + return sprintf('%s (%s)', |
|
302 | + $this->detectWebhostProvider(), |
|
303 | + Helper::getIpAddress() |
|
304 | + ); |
|
305 | + } |
|
306 | 306 | |
307 | - protected function getINI($name, $disabledValue = 'ini_get() is disabled.') |
|
308 | - { |
|
309 | - return function_exists('ini_get') |
|
310 | - ? ini_get($name) |
|
311 | - : $disabledValue; |
|
312 | - } |
|
307 | + protected function getINI($name, $disabledValue = 'ini_get() is disabled.') |
|
308 | + { |
|
309 | + return function_exists('ini_get') |
|
310 | + ? ini_get($name) |
|
311 | + : $disabledValue; |
|
312 | + } |
|
313 | 313 | |
314 | - /** |
|
315 | - * @return array |
|
316 | - */ |
|
317 | - protected function getWordpressPlugins() |
|
318 | - { |
|
319 | - $plugins = get_plugins(); |
|
320 | - $activePlugins = glsr(OptionManager::class)->getWP('active_plugins', [], 'array'); |
|
321 | - $inactive = $this->normalizePluginList(array_diff_key($plugins, array_flip($activePlugins))); |
|
322 | - $active = $this->normalizePluginList(array_diff_key($plugins, $inactive)); |
|
323 | - return $active + $inactive; |
|
324 | - } |
|
314 | + /** |
|
315 | + * @return array |
|
316 | + */ |
|
317 | + protected function getWordpressPlugins() |
|
318 | + { |
|
319 | + $plugins = get_plugins(); |
|
320 | + $activePlugins = glsr(OptionManager::class)->getWP('active_plugins', [], 'array'); |
|
321 | + $inactive = $this->normalizePluginList(array_diff_key($plugins, array_flip($activePlugins))); |
|
322 | + $active = $this->normalizePluginList(array_diff_key($plugins, $inactive)); |
|
323 | + return $active + $inactive; |
|
324 | + } |
|
325 | 325 | |
326 | - /** |
|
327 | - * @param string $title |
|
328 | - * @return string |
|
329 | - */ |
|
330 | - protected function implode($title, array $details) |
|
331 | - { |
|
332 | - $strings = ['['.$title.']']; |
|
333 | - $padding = max(array_map('strlen', array_keys($details))); |
|
334 | - $padding = max([$padding, static::PAD]); |
|
335 | - foreach ($details as $key => $value) { |
|
336 | - $strings[] = is_string($key) |
|
337 | - ? sprintf('%s : %s', str_pad($key, $padding, '.'), $value) |
|
338 | - : ' - '.$value; |
|
339 | - } |
|
340 | - return implode(PHP_EOL, $strings).PHP_EOL.PHP_EOL; |
|
341 | - } |
|
326 | + /** |
|
327 | + * @param string $title |
|
328 | + * @return string |
|
329 | + */ |
|
330 | + protected function implode($title, array $details) |
|
331 | + { |
|
332 | + $strings = ['['.$title.']']; |
|
333 | + $padding = max(array_map('strlen', array_keys($details))); |
|
334 | + $padding = max([$padding, static::PAD]); |
|
335 | + foreach ($details as $key => $value) { |
|
336 | + $strings[] = is_string($key) |
|
337 | + ? sprintf('%s : %s', str_pad($key, $padding, '.'), $value) |
|
338 | + : ' - '.$value; |
|
339 | + } |
|
340 | + return implode(PHP_EOL, $strings).PHP_EOL.PHP_EOL; |
|
341 | + } |
|
342 | 342 | |
343 | - /** |
|
344 | - * @param string $key |
|
345 | - * @return bool |
|
346 | - */ |
|
347 | - protected function isWebhostCheckValid($key) |
|
348 | - { |
|
349 | - return defined($key) |
|
350 | - || filter_input(INPUT_SERVER, $key) |
|
351 | - || Str::contains(filter_input(INPUT_SERVER, 'SERVER_NAME'), $key) |
|
352 | - || Str::contains(DB_HOST, $key) |
|
353 | - || Str::contains(php_uname(), $key); |
|
354 | - } |
|
343 | + /** |
|
344 | + * @param string $key |
|
345 | + * @return bool |
|
346 | + */ |
|
347 | + protected function isWebhostCheckValid($key) |
|
348 | + { |
|
349 | + return defined($key) |
|
350 | + || filter_input(INPUT_SERVER, $key) |
|
351 | + || Str::contains(filter_input(INPUT_SERVER, 'SERVER_NAME'), $key) |
|
352 | + || Str::contains(DB_HOST, $key) |
|
353 | + || Str::contains(php_uname(), $key); |
|
354 | + } |
|
355 | 355 | |
356 | - /** |
|
357 | - * @return array |
|
358 | - */ |
|
359 | - protected function normalizePluginList(array $plugins) |
|
360 | - { |
|
361 | - $plugins = array_map(function ($plugin) { |
|
362 | - return sprintf('%s v%s', Arr::get($plugin, 'Name'), Arr::get($plugin, 'Version')); |
|
363 | - }, $plugins); |
|
364 | - natcasesort($plugins); |
|
365 | - return array_flip($plugins); |
|
366 | - } |
|
356 | + /** |
|
357 | + * @return array |
|
358 | + */ |
|
359 | + protected function normalizePluginList(array $plugins) |
|
360 | + { |
|
361 | + $plugins = array_map(function ($plugin) { |
|
362 | + return sprintf('%s v%s', Arr::get($plugin, 'Name'), Arr::get($plugin, 'Version')); |
|
363 | + }, $plugins); |
|
364 | + natcasesort($plugins); |
|
365 | + return array_flip($plugins); |
|
366 | + } |
|
367 | 367 | |
368 | - /** |
|
369 | - * @return array |
|
370 | - */ |
|
371 | - protected function purgeSensitiveData(array $settings) |
|
372 | - { |
|
373 | - $keys = [ |
|
374 | - 'general.trustalyze_serial', |
|
375 | - 'licenses.', |
|
376 | - 'submissions.recaptcha.key', |
|
377 | - 'submissions.recaptcha.secret', |
|
378 | - ]; |
|
379 | - array_walk($settings, function (&$value, $setting) use ($keys) { |
|
380 | - foreach ($keys as $key) { |
|
381 | - if (!Str::startsWith($key, $setting) || empty($value)) { |
|
382 | - continue; |
|
383 | - } |
|
384 | - $value = str_repeat('•', 13); |
|
385 | - return; |
|
386 | - } |
|
387 | - }); |
|
388 | - return $settings; |
|
389 | - } |
|
368 | + /** |
|
369 | + * @return array |
|
370 | + */ |
|
371 | + protected function purgeSensitiveData(array $settings) |
|
372 | + { |
|
373 | + $keys = [ |
|
374 | + 'general.trustalyze_serial', |
|
375 | + 'licenses.', |
|
376 | + 'submissions.recaptcha.key', |
|
377 | + 'submissions.recaptcha.secret', |
|
378 | + ]; |
|
379 | + array_walk($settings, function (&$value, $setting) use ($keys) { |
|
380 | + foreach ($keys as $key) { |
|
381 | + if (!Str::startsWith($key, $setting) || empty($value)) { |
|
382 | + continue; |
|
383 | + } |
|
384 | + $value = str_repeat('•', 13); |
|
385 | + return; |
|
386 | + } |
|
387 | + }); |
|
388 | + return $settings; |
|
389 | + } |
|
390 | 390 | } |
@@ -7,160 +7,160 @@ |
||
7 | 7 | |
8 | 8 | class Migrate_3_0_0 |
9 | 9 | { |
10 | - const MAPPED_SETTINGS = [ |
|
11 | - 'settings.general.notification' => 'settings.general.notifications', // array |
|
12 | - 'settings.general.notification_email' => 'settings.general.notification_email', |
|
13 | - 'settings.general.notification_message' => 'settings.general.notification_message', |
|
14 | - 'settings.general.require.approval' => 'settings.general.require.approval', |
|
15 | - 'settings.general.require.login' => 'settings.general.require.login', |
|
16 | - 'settings.general.require.login_register' => 'settings.general.require.login_register', |
|
17 | - 'settings.general.webhook_url' => 'settings.general.notification_slack', |
|
18 | - 'settings.reviews-form.akismet' => 'settings.submissions.akismet', |
|
19 | - 'settings.reviews-form.blacklist.action' => 'settings.submissions.blacklist.action', |
|
20 | - 'settings.reviews-form.blacklist.entries' => 'settings.submissions.blacklist.entries', |
|
21 | - 'settings.reviews-form.recaptcha.integration' => 'settings.submissions.recaptcha.integration', |
|
22 | - 'settings.reviews-form.recaptcha.key' => 'settings.submissions.recaptcha.key', |
|
23 | - 'settings.reviews-form.recaptcha.position' => 'settings.submissions.recaptcha.position', |
|
24 | - 'settings.reviews-form.recaptcha.secret' => 'settings.submissions.recaptcha.secret', |
|
25 | - 'settings.reviews-form.required' => 'settings.submissions.required', // array |
|
26 | - 'settings.reviews.assigned_links.enabled' => 'settings.reviews.assigned_links', |
|
27 | - 'settings.reviews.avatars.enabled' => 'settings.reviews.avatars', |
|
28 | - 'settings.reviews.date.custom' => 'settings.reviews.date.custom', |
|
29 | - 'settings.reviews.date.format' => 'settings.reviews.date.format', |
|
30 | - 'settings.reviews.excerpt.enabled' => 'settings.reviews.excerpts', |
|
31 | - 'settings.reviews.excerpt.length' => 'settings.reviews.excerpts_length', |
|
32 | - 'settings.reviews.schema.address' => 'settings.schema.address', |
|
33 | - 'settings.reviews.schema.description.custom' => 'settings.schema.description.custom', |
|
34 | - 'settings.reviews.schema.description.default' => 'settings.schema.description.default', |
|
35 | - 'settings.reviews.schema.highprice' => 'settings.schema.highprice', |
|
36 | - 'settings.reviews.schema.image.custom' => 'settings.schema.image.custom', |
|
37 | - 'settings.reviews.schema.image.default' => 'settings.schema.image.default', |
|
38 | - 'settings.reviews.schema.lowprice' => 'settings.schema.lowprice', |
|
39 | - 'settings.reviews.schema.name.custom' => 'settings.schema.name.custom', |
|
40 | - 'settings.reviews.schema.name.default' => 'settings.schema.name.default', |
|
41 | - 'settings.reviews.schema.pricecurrency' => 'settings.schema.pricecurrency', |
|
42 | - 'settings.reviews.schema.pricerange' => 'settings.schema.pricerange', |
|
43 | - 'settings.reviews.schema.telephone' => 'settings.schema.telephone', |
|
44 | - 'settings.reviews.schema.type.custom' => 'settings.schema.type.custom', |
|
45 | - 'settings.reviews.schema.type.default' => 'settings.schema.type.default', |
|
46 | - 'settings.reviews.schema.url.custom' => 'settings.schema.url.custom', |
|
47 | - 'settings.reviews.schema.url.default' => 'settings.schema.url.default', |
|
48 | - 'version' => 'version_upgraded_from', |
|
49 | - ]; |
|
10 | + const MAPPED_SETTINGS = [ |
|
11 | + 'settings.general.notification' => 'settings.general.notifications', // array |
|
12 | + 'settings.general.notification_email' => 'settings.general.notification_email', |
|
13 | + 'settings.general.notification_message' => 'settings.general.notification_message', |
|
14 | + 'settings.general.require.approval' => 'settings.general.require.approval', |
|
15 | + 'settings.general.require.login' => 'settings.general.require.login', |
|
16 | + 'settings.general.require.login_register' => 'settings.general.require.login_register', |
|
17 | + 'settings.general.webhook_url' => 'settings.general.notification_slack', |
|
18 | + 'settings.reviews-form.akismet' => 'settings.submissions.akismet', |
|
19 | + 'settings.reviews-form.blacklist.action' => 'settings.submissions.blacklist.action', |
|
20 | + 'settings.reviews-form.blacklist.entries' => 'settings.submissions.blacklist.entries', |
|
21 | + 'settings.reviews-form.recaptcha.integration' => 'settings.submissions.recaptcha.integration', |
|
22 | + 'settings.reviews-form.recaptcha.key' => 'settings.submissions.recaptcha.key', |
|
23 | + 'settings.reviews-form.recaptcha.position' => 'settings.submissions.recaptcha.position', |
|
24 | + 'settings.reviews-form.recaptcha.secret' => 'settings.submissions.recaptcha.secret', |
|
25 | + 'settings.reviews-form.required' => 'settings.submissions.required', // array |
|
26 | + 'settings.reviews.assigned_links.enabled' => 'settings.reviews.assigned_links', |
|
27 | + 'settings.reviews.avatars.enabled' => 'settings.reviews.avatars', |
|
28 | + 'settings.reviews.date.custom' => 'settings.reviews.date.custom', |
|
29 | + 'settings.reviews.date.format' => 'settings.reviews.date.format', |
|
30 | + 'settings.reviews.excerpt.enabled' => 'settings.reviews.excerpts', |
|
31 | + 'settings.reviews.excerpt.length' => 'settings.reviews.excerpts_length', |
|
32 | + 'settings.reviews.schema.address' => 'settings.schema.address', |
|
33 | + 'settings.reviews.schema.description.custom' => 'settings.schema.description.custom', |
|
34 | + 'settings.reviews.schema.description.default' => 'settings.schema.description.default', |
|
35 | + 'settings.reviews.schema.highprice' => 'settings.schema.highprice', |
|
36 | + 'settings.reviews.schema.image.custom' => 'settings.schema.image.custom', |
|
37 | + 'settings.reviews.schema.image.default' => 'settings.schema.image.default', |
|
38 | + 'settings.reviews.schema.lowprice' => 'settings.schema.lowprice', |
|
39 | + 'settings.reviews.schema.name.custom' => 'settings.schema.name.custom', |
|
40 | + 'settings.reviews.schema.name.default' => 'settings.schema.name.default', |
|
41 | + 'settings.reviews.schema.pricecurrency' => 'settings.schema.pricecurrency', |
|
42 | + 'settings.reviews.schema.pricerange' => 'settings.schema.pricerange', |
|
43 | + 'settings.reviews.schema.telephone' => 'settings.schema.telephone', |
|
44 | + 'settings.reviews.schema.type.custom' => 'settings.schema.type.custom', |
|
45 | + 'settings.reviews.schema.type.default' => 'settings.schema.type.default', |
|
46 | + 'settings.reviews.schema.url.custom' => 'settings.schema.url.custom', |
|
47 | + 'settings.reviews.schema.url.default' => 'settings.schema.url.default', |
|
48 | + 'version' => 'version_upgraded_from', |
|
49 | + ]; |
|
50 | 50 | |
51 | - /** |
|
52 | - * @var array |
|
53 | - */ |
|
54 | - protected $newSettings; |
|
51 | + /** |
|
52 | + * @var array |
|
53 | + */ |
|
54 | + protected $newSettings; |
|
55 | 55 | |
56 | - /** |
|
57 | - * @var array |
|
58 | - */ |
|
59 | - protected $oldSettings; |
|
56 | + /** |
|
57 | + * @var array |
|
58 | + */ |
|
59 | + protected $oldSettings; |
|
60 | 60 | |
61 | - /** |
|
62 | - * @return void |
|
63 | - */ |
|
64 | - public function migrateSettings() |
|
65 | - { |
|
66 | - $this->newSettings = $this->getNewSettings(); |
|
67 | - $this->oldSettings = $this->getOldSettings(); |
|
68 | - if (empty($this->oldSettings) || empty($this->newSettings)) { |
|
69 | - return; |
|
70 | - } |
|
71 | - foreach (static::MAPPED_SETTINGS as $old => $new) { |
|
72 | - if (empty($this->oldSettings[$old])) { |
|
73 | - continue; |
|
74 | - } |
|
75 | - $this->newSettings[$new] = $this->oldSettings[$old]; |
|
76 | - } |
|
77 | - $this->migrateNotificationSettings(); |
|
78 | - $this->migrateRecaptchaSettings(); |
|
79 | - $this->migrateRequiredSettings(); |
|
80 | - $oldSettings = Arr::convertFromDotNotation($this->oldSettings); |
|
81 | - $newSettings = Arr::convertFromDotNotation($this->newSettings); |
|
82 | - if (isset($oldSettings['settings']['strings']) && is_array($oldSettings['settings']['strings'])) { |
|
83 | - $newSettings['settings']['strings'] = $oldSettings['settings']['strings']; |
|
84 | - } |
|
85 | - update_option(OptionManager::databaseKey(3), $newSettings); |
|
86 | - } |
|
61 | + /** |
|
62 | + * @return void |
|
63 | + */ |
|
64 | + public function migrateSettings() |
|
65 | + { |
|
66 | + $this->newSettings = $this->getNewSettings(); |
|
67 | + $this->oldSettings = $this->getOldSettings(); |
|
68 | + if (empty($this->oldSettings) || empty($this->newSettings)) { |
|
69 | + return; |
|
70 | + } |
|
71 | + foreach (static::MAPPED_SETTINGS as $old => $new) { |
|
72 | + if (empty($this->oldSettings[$old])) { |
|
73 | + continue; |
|
74 | + } |
|
75 | + $this->newSettings[$new] = $this->oldSettings[$old]; |
|
76 | + } |
|
77 | + $this->migrateNotificationSettings(); |
|
78 | + $this->migrateRecaptchaSettings(); |
|
79 | + $this->migrateRequiredSettings(); |
|
80 | + $oldSettings = Arr::convertFromDotNotation($this->oldSettings); |
|
81 | + $newSettings = Arr::convertFromDotNotation($this->newSettings); |
|
82 | + if (isset($oldSettings['settings']['strings']) && is_array($oldSettings['settings']['strings'])) { |
|
83 | + $newSettings['settings']['strings'] = $oldSettings['settings']['strings']; |
|
84 | + } |
|
85 | + update_option(OptionManager::databaseKey(3), $newSettings); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * @return void |
|
90 | - */ |
|
91 | - public function run() |
|
92 | - { |
|
93 | - $this->migrateSettings(); |
|
94 | - } |
|
88 | + /** |
|
89 | + * @return void |
|
90 | + */ |
|
91 | + public function run() |
|
92 | + { |
|
93 | + $this->migrateSettings(); |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * @return array |
|
98 | - */ |
|
99 | - protected function getNewSettings() |
|
100 | - { |
|
101 | - return Arr::flatten(Arr::consolidate(OptionManager::databaseKey(3))); |
|
102 | - } |
|
96 | + /** |
|
97 | + * @return array |
|
98 | + */ |
|
99 | + protected function getNewSettings() |
|
100 | + { |
|
101 | + return Arr::flatten(Arr::consolidate(OptionManager::databaseKey(3))); |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * @return array |
|
106 | - */ |
|
107 | - protected function getOldSettings() |
|
108 | - { |
|
109 | - $defaults = array_fill_keys(array_keys(static::MAPPED_SETTINGS), ''); |
|
110 | - $settings = Arr::consolidate(get_option(OptionManager::databaseKey(2))); |
|
111 | - $settings = Arr::flatten($settings); |
|
112 | - return !empty($settings) |
|
113 | - ? wp_parse_args($settings, $defaults) |
|
114 | - : []; |
|
115 | - } |
|
104 | + /** |
|
105 | + * @return array |
|
106 | + */ |
|
107 | + protected function getOldSettings() |
|
108 | + { |
|
109 | + $defaults = array_fill_keys(array_keys(static::MAPPED_SETTINGS), ''); |
|
110 | + $settings = Arr::consolidate(get_option(OptionManager::databaseKey(2))); |
|
111 | + $settings = Arr::flatten($settings); |
|
112 | + return !empty($settings) |
|
113 | + ? wp_parse_args($settings, $defaults) |
|
114 | + : []; |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * @return void |
|
119 | - */ |
|
120 | - protected function migrateNotificationSettings() |
|
121 | - { |
|
122 | - $notifications = [ |
|
123 | - 'custom' => 'custom', |
|
124 | - 'default' => 'admin', |
|
125 | - 'webhook' => 'slack', |
|
126 | - ]; |
|
127 | - $this->newSettings['settings.general.notifications'] = []; |
|
128 | - foreach ($notifications as $old => $new) { |
|
129 | - if ($this->oldSettings['settings.general.notification'] != $old) { |
|
130 | - continue; |
|
131 | - } |
|
132 | - $this->newSettings['settings.general.notifications'][] = $new; |
|
133 | - } |
|
134 | - } |
|
117 | + /** |
|
118 | + * @return void |
|
119 | + */ |
|
120 | + protected function migrateNotificationSettings() |
|
121 | + { |
|
122 | + $notifications = [ |
|
123 | + 'custom' => 'custom', |
|
124 | + 'default' => 'admin', |
|
125 | + 'webhook' => 'slack', |
|
126 | + ]; |
|
127 | + $this->newSettings['settings.general.notifications'] = []; |
|
128 | + foreach ($notifications as $old => $new) { |
|
129 | + if ($this->oldSettings['settings.general.notification'] != $old) { |
|
130 | + continue; |
|
131 | + } |
|
132 | + $this->newSettings['settings.general.notifications'][] = $new; |
|
133 | + } |
|
134 | + } |
|
135 | 135 | |
136 | - /** |
|
137 | - * @return void |
|
138 | - */ |
|
139 | - protected function migrateRecaptchaSettings() |
|
140 | - { |
|
141 | - $recaptcha = [ |
|
142 | - 'BadgePosition' => $this->oldSettings['settings.reviews-form.recaptcha.position'], |
|
143 | - 'SecretKey' => $this->oldSettings['settings.reviews-form.recaptcha.secret'], |
|
144 | - 'SiteKey' => $this->oldSettings['settings.reviews-form.recaptcha.key'], |
|
145 | - ]; |
|
146 | - if (in_array($this->oldSettings['settings.reviews-form.recaptcha.integration'], ['custom', 'invisible-recaptcha'])) { |
|
147 | - $this->newSettings['settings.submissions.recaptcha.integration'] = 'all'; |
|
148 | - } |
|
149 | - if ('invisible-recaptcha' == $this->oldSettings['settings.reviews-form.recaptcha.integration']) { |
|
150 | - $recaptcha = wp_parse_args((array) get_site_option('ic-settings', [], false), $recaptcha); |
|
151 | - } |
|
152 | - $this->newSettings['settings.submissions.recaptcha.key'] = $recaptcha['SiteKey']; |
|
153 | - $this->newSettings['settings.submissions.recaptcha.secret'] = $recaptcha['SecretKey']; |
|
154 | - $this->newSettings['settings.submissions.recaptcha.position'] = $recaptcha['BadgePosition']; |
|
155 | - } |
|
136 | + /** |
|
137 | + * @return void |
|
138 | + */ |
|
139 | + protected function migrateRecaptchaSettings() |
|
140 | + { |
|
141 | + $recaptcha = [ |
|
142 | + 'BadgePosition' => $this->oldSettings['settings.reviews-form.recaptcha.position'], |
|
143 | + 'SecretKey' => $this->oldSettings['settings.reviews-form.recaptcha.secret'], |
|
144 | + 'SiteKey' => $this->oldSettings['settings.reviews-form.recaptcha.key'], |
|
145 | + ]; |
|
146 | + if (in_array($this->oldSettings['settings.reviews-form.recaptcha.integration'], ['custom', 'invisible-recaptcha'])) { |
|
147 | + $this->newSettings['settings.submissions.recaptcha.integration'] = 'all'; |
|
148 | + } |
|
149 | + if ('invisible-recaptcha' == $this->oldSettings['settings.reviews-form.recaptcha.integration']) { |
|
150 | + $recaptcha = wp_parse_args((array) get_site_option('ic-settings', [], false), $recaptcha); |
|
151 | + } |
|
152 | + $this->newSettings['settings.submissions.recaptcha.key'] = $recaptcha['SiteKey']; |
|
153 | + $this->newSettings['settings.submissions.recaptcha.secret'] = $recaptcha['SecretKey']; |
|
154 | + $this->newSettings['settings.submissions.recaptcha.position'] = $recaptcha['BadgePosition']; |
|
155 | + } |
|
156 | 156 | |
157 | - /** |
|
158 | - * @return void |
|
159 | - */ |
|
160 | - protected function migrateRequiredSettings() |
|
161 | - { |
|
162 | - $this->newSettings['settings.submissions.required'] = array_filter((array) $this->oldSettings['settings.reviews-form.required']); |
|
163 | - $this->newSettings['settings.submissions.required'][] = 'rating'; |
|
164 | - $this->newSettings['settings.submissions.required'][] = 'terms'; |
|
165 | - } |
|
157 | + /** |
|
158 | + * @return void |
|
159 | + */ |
|
160 | + protected function migrateRequiredSettings() |
|
161 | + { |
|
162 | + $this->newSettings['settings.submissions.required'] = array_filter((array) $this->oldSettings['settings.reviews-form.required']); |
|
163 | + $this->newSettings['settings.submissions.required'][] = 'rating'; |
|
164 | + $this->newSettings['settings.submissions.required'][] = 'terms'; |
|
165 | + } |
|
166 | 166 | } |
@@ -8,84 +8,84 @@ |
||
8 | 8 | |
9 | 9 | class Migrate_4_6_0 |
10 | 10 | { |
11 | - /** |
|
12 | - * @return void |
|
13 | - */ |
|
14 | - public function migrateSidebarWidgets() |
|
15 | - { |
|
16 | - $sidebars = get_option('sidebars_widgets'); |
|
17 | - if ($this->widgetsExist($sidebars)) { |
|
18 | - $sidebars = $this->updateWidgetNames($sidebars); |
|
19 | - update_option('sidebars_widgets', $sidebars); |
|
20 | - } |
|
21 | - } |
|
11 | + /** |
|
12 | + * @return void |
|
13 | + */ |
|
14 | + public function migrateSidebarWidgets() |
|
15 | + { |
|
16 | + $sidebars = get_option('sidebars_widgets'); |
|
17 | + if ($this->widgetsExist($sidebars)) { |
|
18 | + $sidebars = $this->updateWidgetNames($sidebars); |
|
19 | + update_option('sidebars_widgets', $sidebars); |
|
20 | + } |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * @return void |
|
25 | - */ |
|
26 | - public function migrateThemeModWidgets() |
|
27 | - { |
|
28 | - $themes = $this->queryThemeMods(); |
|
29 | - foreach ($themes as $theme) { |
|
30 | - $themeMod = get_option($theme); |
|
31 | - $sidebars = Arr::get($themeMod, 'sidebars_widgets.data'); |
|
32 | - if ($this->widgetsExist($sidebars)) { |
|
33 | - $themeMod['sidebars_widgets']['data'] = $this->updateWidgetNames($sidebars); |
|
34 | - update_option($theme, $themeMod); |
|
35 | - } |
|
36 | - } |
|
37 | - } |
|
23 | + /** |
|
24 | + * @return void |
|
25 | + */ |
|
26 | + public function migrateThemeModWidgets() |
|
27 | + { |
|
28 | + $themes = $this->queryThemeMods(); |
|
29 | + foreach ($themes as $theme) { |
|
30 | + $themeMod = get_option($theme); |
|
31 | + $sidebars = Arr::get($themeMod, 'sidebars_widgets.data'); |
|
32 | + if ($this->widgetsExist($sidebars)) { |
|
33 | + $themeMod['sidebars_widgets']['data'] = $this->updateWidgetNames($sidebars); |
|
34 | + update_option($theme, $themeMod); |
|
35 | + } |
|
36 | + } |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @return void |
|
41 | - */ |
|
42 | - public function run() |
|
43 | - { |
|
44 | - $this->migrateSidebarWidgets(); |
|
45 | - $this->migrateThemeModWidgets(); |
|
46 | - } |
|
39 | + /** |
|
40 | + * @return void |
|
41 | + */ |
|
42 | + public function run() |
|
43 | + { |
|
44 | + $this->migrateSidebarWidgets(); |
|
45 | + $this->migrateThemeModWidgets(); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @return array |
|
50 | - */ |
|
51 | - protected function queryThemeMods() |
|
52 | - { |
|
53 | - global $wpdb; |
|
54 | - return $wpdb->get_col(" |
|
48 | + /** |
|
49 | + * @return array |
|
50 | + */ |
|
51 | + protected function queryThemeMods() |
|
52 | + { |
|
53 | + global $wpdb; |
|
54 | + return $wpdb->get_col(" |
|
55 | 55 | SELECT option_name |
56 | 56 | FROM {$wpdb->options} |
57 | 57 | WHERE option_name LIKE '%theme_mods_%' |
58 | 58 | "); |
59 | - } |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * @param array $sidebars |
|
63 | - * @return array |
|
64 | - */ |
|
65 | - protected function updateWidgetNames(array $sidebars) |
|
66 | - { |
|
67 | - array_walk($sidebars, function(&$widgets) { |
|
68 | - array_walk($widgets, function(&$widget) { |
|
69 | - if (Str::startsWith(Application::ID.'_', $widget)) { |
|
70 | - $widget = Str::replaceFirst(Application::ID.'_', Application::PREFIX, $widget); |
|
71 | - } |
|
72 | - }); |
|
73 | - }); |
|
74 | - return $sidebars; |
|
75 | - } |
|
61 | + /** |
|
62 | + * @param array $sidebars |
|
63 | + * @return array |
|
64 | + */ |
|
65 | + protected function updateWidgetNames(array $sidebars) |
|
66 | + { |
|
67 | + array_walk($sidebars, function(&$widgets) { |
|
68 | + array_walk($widgets, function(&$widget) { |
|
69 | + if (Str::startsWith(Application::ID.'_', $widget)) { |
|
70 | + $widget = Str::replaceFirst(Application::ID.'_', Application::PREFIX, $widget); |
|
71 | + } |
|
72 | + }); |
|
73 | + }); |
|
74 | + return $sidebars; |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * @param array $sidebars |
|
79 | - * @return bool |
|
80 | - */ |
|
81 | - protected function widgetsExist($sidebars) |
|
82 | - { |
|
83 | - $widgets = call_user_func_array('array_merge', array_filter(Arr::consolidate($sidebars), 'is_array')); |
|
84 | - foreach ($widgets as $widget) { |
|
85 | - if (Str::startsWith(Application::ID.'_', $widget)) { |
|
86 | - return true; |
|
87 | - } |
|
88 | - } |
|
89 | - return false; |
|
90 | - } |
|
77 | + /** |
|
78 | + * @param array $sidebars |
|
79 | + * @return bool |
|
80 | + */ |
|
81 | + protected function widgetsExist($sidebars) |
|
82 | + { |
|
83 | + $widgets = call_user_func_array('array_merge', array_filter(Arr::consolidate($sidebars), 'is_array')); |
|
84 | + foreach ($widgets as $widget) { |
|
85 | + if (Str::startsWith(Application::ID.'_', $widget)) { |
|
86 | + return true; |
|
87 | + } |
|
88 | + } |
|
89 | + return false; |
|
90 | + } |
|
91 | 91 | } |
@@ -12,144 +12,144 @@ |
||
12 | 12 | |
13 | 13 | class Style |
14 | 14 | { |
15 | - /** |
|
16 | - * @var array |
|
17 | - */ |
|
18 | - public $fields; |
|
15 | + /** |
|
16 | + * @var array |
|
17 | + */ |
|
18 | + public $fields; |
|
19 | 19 | |
20 | - /** |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - public $style; |
|
20 | + /** |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + public $style; |
|
24 | 24 | |
25 | - /** |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - public $pagination; |
|
25 | + /** |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + public $pagination; |
|
29 | 29 | |
30 | - /** |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - public $validation; |
|
30 | + /** |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + public $validation; |
|
34 | 34 | |
35 | - public function __construct() |
|
36 | - { |
|
37 | - $this->style = glsr(OptionManager::class)->get('settings.general.style', 'default'); |
|
38 | - $this->setConfig(); |
|
39 | - } |
|
35 | + public function __construct() |
|
36 | + { |
|
37 | + $this->style = glsr(OptionManager::class)->get('settings.general.style', 'default'); |
|
38 | + $this->setConfig(); |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * @param string $view |
|
43 | - * @return string |
|
44 | - */ |
|
45 | - public function filterView($view) |
|
46 | - { |
|
47 | - $styledViews = apply_filters('site-reviews/style/views', [ |
|
48 | - 'templates/form/field', |
|
49 | - 'templates/form/response', |
|
50 | - 'templates/form/submit-button', |
|
51 | - 'templates/reviews-form', |
|
52 | - ]); |
|
53 | - if (!preg_match('('.implode('|', $styledViews).')', $view)) { |
|
54 | - return $view; |
|
55 | - } |
|
56 | - $views = $this->generatePossibleViews($view); |
|
57 | - foreach ($views as $possibleView) { |
|
58 | - if (!file_exists(glsr()->file($possibleView))) { |
|
59 | - continue; |
|
60 | - } |
|
61 | - return Str::removePrefix('views/', $possibleView); |
|
62 | - } |
|
63 | - return $view; |
|
64 | - } |
|
41 | + /** |
|
42 | + * @param string $view |
|
43 | + * @return string |
|
44 | + */ |
|
45 | + public function filterView($view) |
|
46 | + { |
|
47 | + $styledViews = apply_filters('site-reviews/style/views', [ |
|
48 | + 'templates/form/field', |
|
49 | + 'templates/form/response', |
|
50 | + 'templates/form/submit-button', |
|
51 | + 'templates/reviews-form', |
|
52 | + ]); |
|
53 | + if (!preg_match('('.implode('|', $styledViews).')', $view)) { |
|
54 | + return $view; |
|
55 | + } |
|
56 | + $views = $this->generatePossibleViews($view); |
|
57 | + foreach ($views as $possibleView) { |
|
58 | + if (!file_exists(glsr()->file($possibleView))) { |
|
59 | + continue; |
|
60 | + } |
|
61 | + return Str::removePrefix('views/', $possibleView); |
|
62 | + } |
|
63 | + return $view; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @return string |
|
68 | - */ |
|
69 | - public function get() |
|
70 | - { |
|
71 | - return apply_filters('site-reviews/style', $this->style); |
|
72 | - } |
|
66 | + /** |
|
67 | + * @return string |
|
68 | + */ |
|
69 | + public function get() |
|
70 | + { |
|
71 | + return apply_filters('site-reviews/style', $this->style); |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * @return array |
|
76 | - */ |
|
77 | - public function setConfig() |
|
78 | - { |
|
79 | - $config = shortcode_atts( |
|
80 | - array_fill_keys(['fields', 'pagination', 'validation'], []), |
|
81 | - glsr()->config('styles/'.$this->style) |
|
82 | - ); |
|
83 | - $this->fields = glsr(StyleFieldsDefaults::class)->restrict($config['fields']); |
|
84 | - $this->pagination = glsr(PaginationDefaults::class)->restrict($config['pagination']); |
|
85 | - $this->validation = glsr(StyleValidationDefaults::class)->restrict($config['validation']); |
|
86 | - } |
|
74 | + /** |
|
75 | + * @return array |
|
76 | + */ |
|
77 | + public function setConfig() |
|
78 | + { |
|
79 | + $config = shortcode_atts( |
|
80 | + array_fill_keys(['fields', 'pagination', 'validation'], []), |
|
81 | + glsr()->config('styles/'.$this->style) |
|
82 | + ); |
|
83 | + $this->fields = glsr(StyleFieldsDefaults::class)->restrict($config['fields']); |
|
84 | + $this->pagination = glsr(PaginationDefaults::class)->restrict($config['pagination']); |
|
85 | + $this->validation = glsr(StyleValidationDefaults::class)->restrict($config['validation']); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * @return void |
|
90 | - */ |
|
91 | - public function modifyField(Builder $instance) |
|
92 | - { |
|
93 | - if (!$this->isPublicInstance($instance) || empty(array_filter($this->fields))) { |
|
94 | - return; |
|
95 | - } |
|
96 | - call_user_func_array([$this, 'customize'], [$instance]); |
|
97 | - } |
|
88 | + /** |
|
89 | + * @return void |
|
90 | + */ |
|
91 | + public function modifyField(Builder $instance) |
|
92 | + { |
|
93 | + if (!$this->isPublicInstance($instance) || empty(array_filter($this->fields))) { |
|
94 | + return; |
|
95 | + } |
|
96 | + call_user_func_array([$this, 'customize'], [$instance]); |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * @return array |
|
101 | - */ |
|
102 | - public function paginationArgs(array $args) |
|
103 | - { |
|
104 | - return wp_parse_args($args, $this->pagination); |
|
105 | - } |
|
99 | + /** |
|
100 | + * @return array |
|
101 | + */ |
|
102 | + public function paginationArgs(array $args) |
|
103 | + { |
|
104 | + return wp_parse_args($args, $this->pagination); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * @return void |
|
109 | - */ |
|
110 | - protected function customize(Builder $instance) |
|
111 | - { |
|
112 | - if (!array_key_exists($instance->tag, $this->fields)) { |
|
113 | - return; |
|
114 | - } |
|
115 | - $args = wp_parse_args($instance->args, array_fill_keys(['class', 'type'], '')); |
|
116 | - $key = $instance->tag.'_'.$args['type']; |
|
117 | - $classes = Arr::get($this->fields, $key, Arr::get($this->fields, $instance->tag)); |
|
118 | - $instance->args['class'] = trim($args['class'].' '.$classes); |
|
119 | - do_action_ref_array('site-reviews/customize/'.$this->style, [$instance]); |
|
120 | - } |
|
107 | + /** |
|
108 | + * @return void |
|
109 | + */ |
|
110 | + protected function customize(Builder $instance) |
|
111 | + { |
|
112 | + if (!array_key_exists($instance->tag, $this->fields)) { |
|
113 | + return; |
|
114 | + } |
|
115 | + $args = wp_parse_args($instance->args, array_fill_keys(['class', 'type'], '')); |
|
116 | + $key = $instance->tag.'_'.$args['type']; |
|
117 | + $classes = Arr::get($this->fields, $key, Arr::get($this->fields, $instance->tag)); |
|
118 | + $instance->args['class'] = trim($args['class'].' '.$classes); |
|
119 | + do_action_ref_array('site-reviews/customize/'.$this->style, [$instance]); |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * @param string $view |
|
124 | - * @return array |
|
125 | - */ |
|
126 | - protected function generatePossibleViews($view) |
|
127 | - { |
|
128 | - $basename = basename($view); |
|
129 | - $basepath = rtrim($view, $basename); |
|
130 | - $customPath = 'views/partials/styles/'.$this->style.'/'; |
|
131 | - $parts = explode('_', $basename); |
|
132 | - $views = [ |
|
133 | - $customPath.$basename, |
|
134 | - $customPath.$parts[0], |
|
135 | - $view, |
|
136 | - $basepath.$parts[0], |
|
137 | - ]; |
|
138 | - return array_filter($views); |
|
139 | - } |
|
122 | + /** |
|
123 | + * @param string $view |
|
124 | + * @return array |
|
125 | + */ |
|
126 | + protected function generatePossibleViews($view) |
|
127 | + { |
|
128 | + $basename = basename($view); |
|
129 | + $basepath = rtrim($view, $basename); |
|
130 | + $customPath = 'views/partials/styles/'.$this->style.'/'; |
|
131 | + $parts = explode('_', $basename); |
|
132 | + $views = [ |
|
133 | + $customPath.$basename, |
|
134 | + $customPath.$parts[0], |
|
135 | + $view, |
|
136 | + $basepath.$parts[0], |
|
137 | + ]; |
|
138 | + return array_filter($views); |
|
139 | + } |
|
140 | 140 | |
141 | - /** |
|
142 | - * @return bool |
|
143 | - */ |
|
144 | - protected function isPublicInstance(Builder $instance) |
|
145 | - { |
|
146 | - $args = wp_parse_args($instance->args, [ |
|
147 | - 'is_public' => false, |
|
148 | - 'is_raw' => false, |
|
149 | - ]); |
|
150 | - if (is_admin() || !$args['is_public'] || $args['is_raw']) { |
|
151 | - return false; |
|
152 | - } |
|
153 | - return true; |
|
154 | - } |
|
141 | + /** |
|
142 | + * @return bool |
|
143 | + */ |
|
144 | + protected function isPublicInstance(Builder $instance) |
|
145 | + { |
|
146 | + $args = wp_parse_args($instance->args, [ |
|
147 | + 'is_public' => false, |
|
148 | + 'is_raw' => false, |
|
149 | + ]); |
|
150 | + if (is_admin() || !$args['is_public'] || $args['is_raw']) { |
|
151 | + return false; |
|
152 | + } |
|
153 | + return true; |
|
154 | + } |
|
155 | 155 | } |