@@ -10,52 +10,52 @@ |
||
10 | 10 | |
11 | 11 | class Pagination implements PartialContract |
12 | 12 | { |
13 | - /** |
|
14 | - * @var array |
|
15 | - */ |
|
16 | - protected $args; |
|
13 | + /** |
|
14 | + * @var array |
|
15 | + */ |
|
16 | + protected $args; |
|
17 | 17 | |
18 | - /** |
|
19 | - * {@inheritdoc} |
|
20 | - */ |
|
21 | - public function build(array $args = []) |
|
22 | - { |
|
23 | - $this->args = $this->normalize($args); |
|
24 | - if ($this->args['total'] < 2) { |
|
25 | - return ''; |
|
26 | - } |
|
27 | - return glsr(Template::class)->build('templates/pagination', [ |
|
28 | - 'context' => [ |
|
29 | - 'links' => apply_filters('site-reviews/paginate_links', $this->buildLinks(), $this->args), |
|
30 | - 'loader' => '<div class="glsr-loader"></div>', |
|
31 | - 'screen_reader_text' => __('Site Reviews navigation', 'site-reviews'), |
|
32 | - ], |
|
33 | - ]); |
|
34 | - } |
|
18 | + /** |
|
19 | + * {@inheritdoc} |
|
20 | + */ |
|
21 | + public function build(array $args = []) |
|
22 | + { |
|
23 | + $this->args = $this->normalize($args); |
|
24 | + if ($this->args['total'] < 2) { |
|
25 | + return ''; |
|
26 | + } |
|
27 | + return glsr(Template::class)->build('templates/pagination', [ |
|
28 | + 'context' => [ |
|
29 | + 'links' => apply_filters('site-reviews/paginate_links', $this->buildLinks(), $this->args), |
|
30 | + 'loader' => '<div class="glsr-loader"></div>', |
|
31 | + 'screen_reader_text' => __('Site Reviews navigation', 'site-reviews'), |
|
32 | + ], |
|
33 | + ]); |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @return string |
|
38 | - */ |
|
39 | - protected function buildLinks() |
|
40 | - { |
|
41 | - $args = glsr(Style::class)->paginationArgs($this->args); |
|
42 | - if ('array' == $args['type']) { |
|
43 | - $args['type'] = 'plain'; |
|
44 | - } |
|
45 | - return paginate_links($args); |
|
46 | - } |
|
36 | + /** |
|
37 | + * @return string |
|
38 | + */ |
|
39 | + protected function buildLinks() |
|
40 | + { |
|
41 | + $args = glsr(Style::class)->paginationArgs($this->args); |
|
42 | + if ('array' == $args['type']) { |
|
43 | + $args['type'] = 'plain'; |
|
44 | + } |
|
45 | + return paginate_links($args); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @return array |
|
50 | - */ |
|
51 | - protected function normalize(array $args) |
|
52 | - { |
|
53 | - if ($baseUrl = Arr::get($args, 'baseUrl')) { |
|
54 | - $args['base'] = $baseUrl.'%_%'; |
|
55 | - } |
|
56 | - return wp_parse_args(array_filter($args), [ |
|
57 | - 'current' => glsr(QueryBuilder::class)->getPaged(), |
|
58 | - 'total' => 1, |
|
59 | - ]); |
|
60 | - } |
|
48 | + /** |
|
49 | + * @return array |
|
50 | + */ |
|
51 | + protected function normalize(array $args) |
|
52 | + { |
|
53 | + if ($baseUrl = Arr::get($args, 'baseUrl')) { |
|
54 | + $args['base'] = $baseUrl.'%_%'; |
|
55 | + } |
|
56 | + return wp_parse_args(array_filter($args), [ |
|
57 | + 'current' => glsr(QueryBuilder::class)->getPaged(), |
|
58 | + 'total' => 1, |
|
59 | + ]); |
|
60 | + } |
|
61 | 61 | } |
@@ -11,197 +11,197 @@ |
||
11 | 11 | |
12 | 12 | class SiteReviewsSummary implements PartialContract |
13 | 13 | { |
14 | - /** |
|
15 | - * @var array |
|
16 | - */ |
|
17 | - protected $args; |
|
18 | - |
|
19 | - /** |
|
20 | - * @var float |
|
21 | - */ |
|
22 | - protected $averageRating; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var array |
|
26 | - */ |
|
27 | - protected $ratingCounts; |
|
28 | - |
|
29 | - /** |
|
30 | - * {@inheritdoc} |
|
31 | - */ |
|
32 | - public function build(array $args = []) |
|
33 | - { |
|
34 | - $this->args = $args; |
|
35 | - $this->ratingCounts = glsr(ReviewManager::class)->getRatingCounts($args); |
|
36 | - if (!array_sum($this->ratingCounts) && $this->isHidden('if_empty')) { |
|
37 | - return ''; |
|
38 | - } |
|
39 | - $this->averageRating = glsr(Rating::class)->getAverage($this->ratingCounts); |
|
40 | - $this->generateSchema(); |
|
41 | - return glsr(Template::class)->build('templates/reviews-summary', [ |
|
42 | - 'context' => [ |
|
43 | - 'assigned_to' => $this->args['assigned_to'], |
|
44 | - 'category' => $this->args['category'], |
|
45 | - 'class' => $this->getClass(), |
|
46 | - 'id' => $this->args['id'], |
|
47 | - 'percentages' => $this->buildPercentage(), |
|
48 | - 'rating' => $this->buildRating(), |
|
49 | - 'stars' => $this->buildStars(), |
|
50 | - 'text' => $this->buildText(), |
|
51 | - ], |
|
52 | - ]); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @return void|string |
|
57 | - */ |
|
58 | - protected function buildPercentage() |
|
59 | - { |
|
60 | - if ($this->isHidden('bars')) { |
|
61 | - return; |
|
62 | - } |
|
63 | - $percentages = preg_filter('/$/', '%', glsr(Rating::class)->getPercentages($this->ratingCounts)); |
|
64 | - $bars = array_reduce(range(glsr()->constant('MAX_RATING', Rating::class), 1), function ($carry, $level) use ($percentages) { |
|
65 | - $label = $this->buildPercentageLabel($this->args['labels'][$level]); |
|
66 | - $background = $this->buildPercentageBackground($percentages[$level]); |
|
67 | - $count = apply_filters('site-reviews/summary/counts', |
|
68 | - $percentages[$level], |
|
69 | - $this->ratingCounts[$level] |
|
70 | - ); |
|
71 | - $percent = $this->buildPercentageCount($count); |
|
72 | - $value = $label.$background.$percent; |
|
73 | - $value = apply_filters('site-reviews/summary/wrap/bar', $value, $this->args, [ |
|
74 | - 'percent' => wp_strip_all_tags($count, true), |
|
75 | - 'rating' => $level, |
|
76 | - ]); |
|
77 | - return $carry.glsr(Builder::class)->div($value, [ |
|
78 | - 'class' => 'glsr-bar', |
|
79 | - ]); |
|
80 | - }); |
|
81 | - return $this->wrap('percentage', $bars); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * @param string $percent |
|
86 | - * @return string |
|
87 | - */ |
|
88 | - protected function buildPercentageBackground($percent) |
|
89 | - { |
|
90 | - $backgroundPercent = glsr(Builder::class)->span([ |
|
91 | - 'class' => 'glsr-bar-background-percent', |
|
92 | - 'style' => 'width:'.$percent, |
|
93 | - ]); |
|
94 | - return '<span class="glsr-bar-background">'.$backgroundPercent.'</span>'; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @param string $count |
|
99 | - * @return string |
|
100 | - */ |
|
101 | - protected function buildPercentageCount($count) |
|
102 | - { |
|
103 | - return '<span class="glsr-bar-percent">'.$count.'</span>'; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @param string $label |
|
108 | - * @return string |
|
109 | - */ |
|
110 | - protected function buildPercentageLabel($label) |
|
111 | - { |
|
112 | - return '<span class="glsr-bar-label">'.$label.'</span>'; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * @return void|string |
|
117 | - */ |
|
118 | - protected function buildRating() |
|
119 | - { |
|
120 | - if ($this->isHidden('rating')) { |
|
121 | - return; |
|
122 | - } |
|
123 | - return $this->wrap('rating', '<span>'.$this->averageRating.'</span>'); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @return void|string |
|
128 | - */ |
|
129 | - protected function buildStars() |
|
130 | - { |
|
131 | - if ($this->isHidden('stars')) { |
|
132 | - return; |
|
133 | - } |
|
134 | - $stars = glsr_star_rating($this->averageRating); |
|
135 | - return $this->wrap('stars', $stars); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * @return void|string |
|
140 | - */ |
|
141 | - protected function buildText() |
|
142 | - { |
|
143 | - if ($this->isHidden('summary')) { |
|
144 | - return; |
|
145 | - } |
|
146 | - $count = intval(array_sum($this->ratingCounts)); |
|
147 | - if (empty($this->args['text'])) { |
|
148 | - // @todo document this change |
|
149 | - $this->args['text'] = _nx( |
|
150 | - '{rating} out of {max} stars (based on {num} review)', |
|
151 | - '{rating} out of {max} stars (based on {num} reviews)', |
|
152 | - $count, |
|
153 | - 'Do not translate {rating}, {max}, and {num}, they are template tags.', |
|
154 | - 'site-reviews' |
|
155 | - ); |
|
156 | - } |
|
157 | - $summary = str_replace( |
|
158 | - ['{rating}', '{max}', '{num}'], |
|
159 | - [$this->averageRating, glsr()->constant('MAX_RATING', Rating::class), $count], |
|
160 | - $this->args['text'] |
|
161 | - ); |
|
162 | - return $this->wrap('text', '<span>'.$summary.'</span>'); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * @return void |
|
167 | - */ |
|
168 | - protected function generateSchema() |
|
169 | - { |
|
170 | - if (!wp_validate_boolean($this->args['schema'])) { |
|
171 | - return; |
|
172 | - } |
|
173 | - glsr(Schema::class)->store( |
|
174 | - glsr(Schema::class)->buildSummary($this->args) |
|
175 | - ); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * @return string |
|
180 | - */ |
|
181 | - protected function getClass() |
|
182 | - { |
|
183 | - return trim('glsr-summary '.$this->args['class']); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * @param string $key |
|
188 | - * @return bool |
|
189 | - */ |
|
190 | - protected function isHidden($key) |
|
191 | - { |
|
192 | - return in_array($key, $this->args['hide']); |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * @param string $key |
|
197 | - * @param string $value |
|
198 | - * @return string |
|
199 | - */ |
|
200 | - protected function wrap($key, $value) |
|
201 | - { |
|
202 | - $value = apply_filters('site-reviews/summary/wrap/'.$key, $value, $this->args); |
|
203 | - return glsr(Builder::class)->div($value, [ |
|
204 | - 'class' => 'glsr-summary-'.$key, |
|
205 | - ]); |
|
206 | - } |
|
14 | + /** |
|
15 | + * @var array |
|
16 | + */ |
|
17 | + protected $args; |
|
18 | + |
|
19 | + /** |
|
20 | + * @var float |
|
21 | + */ |
|
22 | + protected $averageRating; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var array |
|
26 | + */ |
|
27 | + protected $ratingCounts; |
|
28 | + |
|
29 | + /** |
|
30 | + * {@inheritdoc} |
|
31 | + */ |
|
32 | + public function build(array $args = []) |
|
33 | + { |
|
34 | + $this->args = $args; |
|
35 | + $this->ratingCounts = glsr(ReviewManager::class)->getRatingCounts($args); |
|
36 | + if (!array_sum($this->ratingCounts) && $this->isHidden('if_empty')) { |
|
37 | + return ''; |
|
38 | + } |
|
39 | + $this->averageRating = glsr(Rating::class)->getAverage($this->ratingCounts); |
|
40 | + $this->generateSchema(); |
|
41 | + return glsr(Template::class)->build('templates/reviews-summary', [ |
|
42 | + 'context' => [ |
|
43 | + 'assigned_to' => $this->args['assigned_to'], |
|
44 | + 'category' => $this->args['category'], |
|
45 | + 'class' => $this->getClass(), |
|
46 | + 'id' => $this->args['id'], |
|
47 | + 'percentages' => $this->buildPercentage(), |
|
48 | + 'rating' => $this->buildRating(), |
|
49 | + 'stars' => $this->buildStars(), |
|
50 | + 'text' => $this->buildText(), |
|
51 | + ], |
|
52 | + ]); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @return void|string |
|
57 | + */ |
|
58 | + protected function buildPercentage() |
|
59 | + { |
|
60 | + if ($this->isHidden('bars')) { |
|
61 | + return; |
|
62 | + } |
|
63 | + $percentages = preg_filter('/$/', '%', glsr(Rating::class)->getPercentages($this->ratingCounts)); |
|
64 | + $bars = array_reduce(range(glsr()->constant('MAX_RATING', Rating::class), 1), function ($carry, $level) use ($percentages) { |
|
65 | + $label = $this->buildPercentageLabel($this->args['labels'][$level]); |
|
66 | + $background = $this->buildPercentageBackground($percentages[$level]); |
|
67 | + $count = apply_filters('site-reviews/summary/counts', |
|
68 | + $percentages[$level], |
|
69 | + $this->ratingCounts[$level] |
|
70 | + ); |
|
71 | + $percent = $this->buildPercentageCount($count); |
|
72 | + $value = $label.$background.$percent; |
|
73 | + $value = apply_filters('site-reviews/summary/wrap/bar', $value, $this->args, [ |
|
74 | + 'percent' => wp_strip_all_tags($count, true), |
|
75 | + 'rating' => $level, |
|
76 | + ]); |
|
77 | + return $carry.glsr(Builder::class)->div($value, [ |
|
78 | + 'class' => 'glsr-bar', |
|
79 | + ]); |
|
80 | + }); |
|
81 | + return $this->wrap('percentage', $bars); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * @param string $percent |
|
86 | + * @return string |
|
87 | + */ |
|
88 | + protected function buildPercentageBackground($percent) |
|
89 | + { |
|
90 | + $backgroundPercent = glsr(Builder::class)->span([ |
|
91 | + 'class' => 'glsr-bar-background-percent', |
|
92 | + 'style' => 'width:'.$percent, |
|
93 | + ]); |
|
94 | + return '<span class="glsr-bar-background">'.$backgroundPercent.'</span>'; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @param string $count |
|
99 | + * @return string |
|
100 | + */ |
|
101 | + protected function buildPercentageCount($count) |
|
102 | + { |
|
103 | + return '<span class="glsr-bar-percent">'.$count.'</span>'; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @param string $label |
|
108 | + * @return string |
|
109 | + */ |
|
110 | + protected function buildPercentageLabel($label) |
|
111 | + { |
|
112 | + return '<span class="glsr-bar-label">'.$label.'</span>'; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * @return void|string |
|
117 | + */ |
|
118 | + protected function buildRating() |
|
119 | + { |
|
120 | + if ($this->isHidden('rating')) { |
|
121 | + return; |
|
122 | + } |
|
123 | + return $this->wrap('rating', '<span>'.$this->averageRating.'</span>'); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @return void|string |
|
128 | + */ |
|
129 | + protected function buildStars() |
|
130 | + { |
|
131 | + if ($this->isHidden('stars')) { |
|
132 | + return; |
|
133 | + } |
|
134 | + $stars = glsr_star_rating($this->averageRating); |
|
135 | + return $this->wrap('stars', $stars); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * @return void|string |
|
140 | + */ |
|
141 | + protected function buildText() |
|
142 | + { |
|
143 | + if ($this->isHidden('summary')) { |
|
144 | + return; |
|
145 | + } |
|
146 | + $count = intval(array_sum($this->ratingCounts)); |
|
147 | + if (empty($this->args['text'])) { |
|
148 | + // @todo document this change |
|
149 | + $this->args['text'] = _nx( |
|
150 | + '{rating} out of {max} stars (based on {num} review)', |
|
151 | + '{rating} out of {max} stars (based on {num} reviews)', |
|
152 | + $count, |
|
153 | + 'Do not translate {rating}, {max}, and {num}, they are template tags.', |
|
154 | + 'site-reviews' |
|
155 | + ); |
|
156 | + } |
|
157 | + $summary = str_replace( |
|
158 | + ['{rating}', '{max}', '{num}'], |
|
159 | + [$this->averageRating, glsr()->constant('MAX_RATING', Rating::class), $count], |
|
160 | + $this->args['text'] |
|
161 | + ); |
|
162 | + return $this->wrap('text', '<span>'.$summary.'</span>'); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * @return void |
|
167 | + */ |
|
168 | + protected function generateSchema() |
|
169 | + { |
|
170 | + if (!wp_validate_boolean($this->args['schema'])) { |
|
171 | + return; |
|
172 | + } |
|
173 | + glsr(Schema::class)->store( |
|
174 | + glsr(Schema::class)->buildSummary($this->args) |
|
175 | + ); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * @return string |
|
180 | + */ |
|
181 | + protected function getClass() |
|
182 | + { |
|
183 | + return trim('glsr-summary '.$this->args['class']); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * @param string $key |
|
188 | + * @return bool |
|
189 | + */ |
|
190 | + protected function isHidden($key) |
|
191 | + { |
|
192 | + return in_array($key, $this->args['hide']); |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * @param string $key |
|
197 | + * @param string $value |
|
198 | + * @return string |
|
199 | + */ |
|
200 | + protected function wrap($key, $value) |
|
201 | + { |
|
202 | + $value = apply_filters('site-reviews/summary/wrap/'.$key, $value, $this->args); |
|
203 | + return glsr(Builder::class)->div($value, [ |
|
204 | + 'class' => 'glsr-summary-'.$key, |
|
205 | + ]); |
|
206 | + } |
|
207 | 207 | } |
@@ -8,54 +8,54 @@ |
||
8 | 8 | |
9 | 9 | class StarRating implements PartialContract |
10 | 10 | { |
11 | - protected $prefix; |
|
12 | - protected $rating; |
|
11 | + protected $prefix; |
|
12 | + protected $rating; |
|
13 | 13 | |
14 | - /** |
|
15 | - * {@inheritdoc} |
|
16 | - */ |
|
17 | - public function build(array $args = []) |
|
18 | - { |
|
19 | - $this->setProperties($args); |
|
20 | - $fullStars = intval(floor($this->rating)); |
|
21 | - $halfStars = intval(ceil($this->rating - $fullStars)); |
|
22 | - $emptyStars = max(0, glsr()->constant('MAX_RATING', Rating::class) - $fullStars - $halfStars); |
|
23 | - return glsr(Template::class)->build('templates/rating/stars', [ |
|
24 | - 'context' => [ |
|
25 | - 'empty_stars' => $this->getTemplate('empty-star', $emptyStars), |
|
26 | - 'full_stars' => $this->getTemplate('full-star', $fullStars), |
|
27 | - 'half_stars' => $this->getTemplate('half-star', $halfStars), |
|
28 | - 'prefix' => $this->prefix, |
|
29 | - 'title' => sprintf(__('%s rating', 'site-reviews'), number_format_i18n($this->rating, 1)), |
|
30 | - ], |
|
31 | - ]); |
|
32 | - } |
|
14 | + /** |
|
15 | + * {@inheritdoc} |
|
16 | + */ |
|
17 | + public function build(array $args = []) |
|
18 | + { |
|
19 | + $this->setProperties($args); |
|
20 | + $fullStars = intval(floor($this->rating)); |
|
21 | + $halfStars = intval(ceil($this->rating - $fullStars)); |
|
22 | + $emptyStars = max(0, glsr()->constant('MAX_RATING', Rating::class) - $fullStars - $halfStars); |
|
23 | + return glsr(Template::class)->build('templates/rating/stars', [ |
|
24 | + 'context' => [ |
|
25 | + 'empty_stars' => $this->getTemplate('empty-star', $emptyStars), |
|
26 | + 'full_stars' => $this->getTemplate('full-star', $fullStars), |
|
27 | + 'half_stars' => $this->getTemplate('half-star', $halfStars), |
|
28 | + 'prefix' => $this->prefix, |
|
29 | + 'title' => sprintf(__('%s rating', 'site-reviews'), number_format_i18n($this->rating, 1)), |
|
30 | + ], |
|
31 | + ]); |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * @param string $templateName |
|
36 | - * @param int $timesRepeated |
|
37 | - * @return string |
|
38 | - */ |
|
39 | - protected function getTemplate($templateName, $timesRepeated) |
|
40 | - { |
|
41 | - $template = glsr(Template::class)->build('templates/rating/'.$templateName, [ |
|
42 | - 'context' => [ |
|
43 | - 'prefix' => $this->prefix, |
|
44 | - ], |
|
45 | - ]); |
|
46 | - return str_repeat($template, $timesRepeated); |
|
47 | - } |
|
34 | + /** |
|
35 | + * @param string $templateName |
|
36 | + * @param int $timesRepeated |
|
37 | + * @return string |
|
38 | + */ |
|
39 | + protected function getTemplate($templateName, $timesRepeated) |
|
40 | + { |
|
41 | + $template = glsr(Template::class)->build('templates/rating/'.$templateName, [ |
|
42 | + 'context' => [ |
|
43 | + 'prefix' => $this->prefix, |
|
44 | + ], |
|
45 | + ]); |
|
46 | + return str_repeat($template, $timesRepeated); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @return array |
|
51 | - */ |
|
52 | - protected function setProperties(array $args) |
|
53 | - { |
|
54 | - $args = wp_parse_args($args, [ |
|
55 | - 'prefix' => glsr()->isAdmin() ? '' : 'glsr-', |
|
56 | - 'rating' => 0, |
|
57 | - ]); |
|
58 | - $this->prefix = $args['prefix']; |
|
59 | - $this->rating = (float) str_replace(',', '.', $args['rating']); |
|
60 | - } |
|
49 | + /** |
|
50 | + * @return array |
|
51 | + */ |
|
52 | + protected function setProperties(array $args) |
|
53 | + { |
|
54 | + $args = wp_parse_args($args, [ |
|
55 | + 'prefix' => glsr()->isAdmin() ? '' : 'glsr-', |
|
56 | + 'rating' => 0, |
|
57 | + ]); |
|
58 | + $this->prefix = $args['prefix']; |
|
59 | + $this->rating = (float) str_replace(',', '.', $args['rating']); |
|
60 | + } |
|
61 | 61 | } |
@@ -21,384 +21,384 @@ |
||
21 | 21 | |
22 | 22 | class SiteReviews |
23 | 23 | { |
24 | - /** |
|
25 | - * @var array |
|
26 | - */ |
|
27 | - public $args; |
|
24 | + /** |
|
25 | + * @var array |
|
26 | + */ |
|
27 | + public $args; |
|
28 | 28 | |
29 | - /** |
|
30 | - * @var Review |
|
31 | - */ |
|
32 | - public $current; |
|
29 | + /** |
|
30 | + * @var Review |
|
31 | + */ |
|
32 | + public $current; |
|
33 | 33 | |
34 | - /** |
|
35 | - * @var array |
|
36 | - */ |
|
37 | - public $options; |
|
34 | + /** |
|
35 | + * @var array |
|
36 | + */ |
|
37 | + public $options; |
|
38 | 38 | |
39 | - /** |
|
40 | - * @var Reviews |
|
41 | - */ |
|
42 | - protected $reviews; |
|
39 | + /** |
|
40 | + * @var Reviews |
|
41 | + */ |
|
42 | + protected $reviews; |
|
43 | 43 | |
44 | - /** |
|
45 | - * @param Reviews|null $reviews |
|
46 | - * @return ReviewsHtml |
|
47 | - */ |
|
48 | - public function build(array $args = [], $reviews = null) |
|
49 | - { |
|
50 | - $this->args = glsr(SiteReviewsDefaults::class)->merge($args); |
|
51 | - $this->options = Arr::flatten(glsr(OptionManager::class)->all()); |
|
52 | - $this->reviews = $reviews instanceof Reviews |
|
53 | - ? $reviews |
|
54 | - : glsr(ReviewManager::class)->get($this->args); |
|
55 | - $this->generateSchema(); |
|
56 | - return $this->buildReviews(); |
|
57 | - } |
|
44 | + /** |
|
45 | + * @param Reviews|null $reviews |
|
46 | + * @return ReviewsHtml |
|
47 | + */ |
|
48 | + public function build(array $args = [], $reviews = null) |
|
49 | + { |
|
50 | + $this->args = glsr(SiteReviewsDefaults::class)->merge($args); |
|
51 | + $this->options = Arr::flatten(glsr(OptionManager::class)->all()); |
|
52 | + $this->reviews = $reviews instanceof Reviews |
|
53 | + ? $reviews |
|
54 | + : glsr(ReviewManager::class)->get($this->args); |
|
55 | + $this->generateSchema(); |
|
56 | + return $this->buildReviews(); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * @return ReviewHtml |
|
61 | - */ |
|
62 | - public function buildReview(Review $review) |
|
63 | - { |
|
64 | - $review = apply_filters('site-reviews/review/build/before', $review); |
|
65 | - $this->current = $review; |
|
66 | - $renderedFields = []; |
|
67 | - foreach ($review as $key => $value) { |
|
68 | - $method = Helper::buildMethodName($key, 'buildOption'); |
|
69 | - $field = method_exists($this, $method) |
|
70 | - ? $this->$method($key, $value) |
|
71 | - : false; |
|
72 | - $field = apply_filters('site-reviews/review/build/'.$key, $field, $value, $review, $this); |
|
73 | - if (false === $field) { |
|
74 | - continue; |
|
75 | - } |
|
76 | - $renderedFields[$key] = $field; |
|
77 | - } |
|
78 | - $this->wrap($renderedFields, $review); |
|
79 | - $renderedFields = apply_filters('site-reviews/review/build/after', $renderedFields, $review, $this); |
|
80 | - $this->current = null; |
|
81 | - return new ReviewHtml($review, (array) $renderedFields); |
|
82 | - } |
|
59 | + /** |
|
60 | + * @return ReviewHtml |
|
61 | + */ |
|
62 | + public function buildReview(Review $review) |
|
63 | + { |
|
64 | + $review = apply_filters('site-reviews/review/build/before', $review); |
|
65 | + $this->current = $review; |
|
66 | + $renderedFields = []; |
|
67 | + foreach ($review as $key => $value) { |
|
68 | + $method = Helper::buildMethodName($key, 'buildOption'); |
|
69 | + $field = method_exists($this, $method) |
|
70 | + ? $this->$method($key, $value) |
|
71 | + : false; |
|
72 | + $field = apply_filters('site-reviews/review/build/'.$key, $field, $value, $review, $this); |
|
73 | + if (false === $field) { |
|
74 | + continue; |
|
75 | + } |
|
76 | + $renderedFields[$key] = $field; |
|
77 | + } |
|
78 | + $this->wrap($renderedFields, $review); |
|
79 | + $renderedFields = apply_filters('site-reviews/review/build/after', $renderedFields, $review, $this); |
|
80 | + $this->current = null; |
|
81 | + return new ReviewHtml($review, (array) $renderedFields); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * @return ReviewsHtml |
|
86 | - */ |
|
87 | - public function buildReviews() |
|
88 | - { |
|
89 | - $renderedReviews = []; |
|
90 | - foreach ($this->reviews as $index => $review) { |
|
91 | - $renderedReviews[] = $this->buildReview($review); |
|
92 | - } |
|
93 | - return new ReviewsHtml($renderedReviews, $this->reviews->max_num_pages, $this->args); |
|
94 | - } |
|
84 | + /** |
|
85 | + * @return ReviewsHtml |
|
86 | + */ |
|
87 | + public function buildReviews() |
|
88 | + { |
|
89 | + $renderedReviews = []; |
|
90 | + foreach ($this->reviews as $index => $review) { |
|
91 | + $renderedReviews[] = $this->buildReview($review); |
|
92 | + } |
|
93 | + return new ReviewsHtml($renderedReviews, $this->reviews->max_num_pages, $this->args); |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * @return void |
|
98 | - */ |
|
99 | - public function generateSchema() |
|
100 | - { |
|
101 | - if (!wp_validate_boolean($this->args['schema'])) { |
|
102 | - return; |
|
103 | - } |
|
104 | - glsr(Schema::class)->store( |
|
105 | - glsr(Schema::class)->build($this->args) |
|
106 | - ); |
|
107 | - } |
|
96 | + /** |
|
97 | + * @return void |
|
98 | + */ |
|
99 | + public function generateSchema() |
|
100 | + { |
|
101 | + if (!wp_validate_boolean($this->args['schema'])) { |
|
102 | + return; |
|
103 | + } |
|
104 | + glsr(Schema::class)->store( |
|
105 | + glsr(Schema::class)->build($this->args) |
|
106 | + ); |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * @param string $text |
|
111 | - * @return string |
|
112 | - */ |
|
113 | - public function getExcerpt($text) |
|
114 | - { |
|
115 | - $limit = intval($this->getOption('settings.reviews.excerpts_length', 55)); |
|
116 | - $split = extension_loaded('intl') |
|
117 | - ? $this->getExcerptIntlSplit($text, $limit) |
|
118 | - : $this->getExcerptSplit($text, $limit); |
|
119 | - $hiddenText = substr($text, $split); |
|
120 | - if (!empty($hiddenText)) { |
|
121 | - $showMore = glsr(Builder::class)->span($hiddenText, [ |
|
122 | - 'class' => 'glsr-hidden glsr-hidden-text', |
|
123 | - 'data-show-less' => __('Show less', 'site-reviews'), |
|
124 | - 'data-show-more' => __('Show more', 'site-reviews'), |
|
125 | - ]); |
|
126 | - $text = ltrim(substr($text, 0, $split)).$showMore; |
|
127 | - } |
|
128 | - return $text; |
|
129 | - } |
|
109 | + /** |
|
110 | + * @param string $text |
|
111 | + * @return string |
|
112 | + */ |
|
113 | + public function getExcerpt($text) |
|
114 | + { |
|
115 | + $limit = intval($this->getOption('settings.reviews.excerpts_length', 55)); |
|
116 | + $split = extension_loaded('intl') |
|
117 | + ? $this->getExcerptIntlSplit($text, $limit) |
|
118 | + : $this->getExcerptSplit($text, $limit); |
|
119 | + $hiddenText = substr($text, $split); |
|
120 | + if (!empty($hiddenText)) { |
|
121 | + $showMore = glsr(Builder::class)->span($hiddenText, [ |
|
122 | + 'class' => 'glsr-hidden glsr-hidden-text', |
|
123 | + 'data-show-less' => __('Show less', 'site-reviews'), |
|
124 | + 'data-show-more' => __('Show more', 'site-reviews'), |
|
125 | + ]); |
|
126 | + $text = ltrim(substr($text, 0, $split)).$showMore; |
|
127 | + } |
|
128 | + return $text; |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * @param string $key |
|
133 | - * @param string $path |
|
134 | - * @return bool |
|
135 | - */ |
|
136 | - public function isHidden($key, $path = '') |
|
137 | - { |
|
138 | - $isOptionEnabled = !empty($path) |
|
139 | - ? $this->isOptionEnabled($path) |
|
140 | - : true; |
|
141 | - return in_array($key, $this->args['hide']) || !$isOptionEnabled; |
|
142 | - } |
|
131 | + /** |
|
132 | + * @param string $key |
|
133 | + * @param string $path |
|
134 | + * @return bool |
|
135 | + */ |
|
136 | + public function isHidden($key, $path = '') |
|
137 | + { |
|
138 | + $isOptionEnabled = !empty($path) |
|
139 | + ? $this->isOptionEnabled($path) |
|
140 | + : true; |
|
141 | + return in_array($key, $this->args['hide']) || !$isOptionEnabled; |
|
142 | + } |
|
143 | 143 | |
144 | - /** |
|
145 | - * @param string $key |
|
146 | - * @param string $value |
|
147 | - * @return bool |
|
148 | - */ |
|
149 | - public function isHiddenOrEmpty($key, $value) |
|
150 | - { |
|
151 | - return $this->isHidden($key) || empty($value); |
|
152 | - } |
|
144 | + /** |
|
145 | + * @param string $key |
|
146 | + * @param string $value |
|
147 | + * @return bool |
|
148 | + */ |
|
149 | + public function isHiddenOrEmpty($key, $value) |
|
150 | + { |
|
151 | + return $this->isHidden($key) || empty($value); |
|
152 | + } |
|
153 | 153 | |
154 | - /** |
|
155 | - * @param string $text |
|
156 | - * @return string |
|
157 | - */ |
|
158 | - public function normalizeText($text) |
|
159 | - { |
|
160 | - $text = wp_kses($text, wp_kses_allowed_html()); |
|
161 | - $text = convert_smilies(strip_shortcodes($text)); |
|
162 | - $text = str_replace(']]>', ']]>', $text); |
|
163 | - $text = preg_replace('/(\R){2,}/', '$1', $text); |
|
164 | - if ($this->isOptionEnabled('settings.reviews.excerpts')) { |
|
165 | - $text = $this->getExcerpt($text); |
|
166 | - } |
|
167 | - return wptexturize(nl2br($text)); |
|
168 | - } |
|
154 | + /** |
|
155 | + * @param string $text |
|
156 | + * @return string |
|
157 | + */ |
|
158 | + public function normalizeText($text) |
|
159 | + { |
|
160 | + $text = wp_kses($text, wp_kses_allowed_html()); |
|
161 | + $text = convert_smilies(strip_shortcodes($text)); |
|
162 | + $text = str_replace(']]>', ']]>', $text); |
|
163 | + $text = preg_replace('/(\R){2,}/', '$1', $text); |
|
164 | + if ($this->isOptionEnabled('settings.reviews.excerpts')) { |
|
165 | + $text = $this->getExcerpt($text); |
|
166 | + } |
|
167 | + return wptexturize(nl2br($text)); |
|
168 | + } |
|
169 | 169 | |
170 | - /** |
|
171 | - * @param string $key |
|
172 | - * @param string $value |
|
173 | - * @return void|string |
|
174 | - */ |
|
175 | - protected function buildOptionAssignedTo($key, $value) |
|
176 | - { |
|
177 | - if ($this->isHidden($key, 'settings.reviews.assigned_links')) { |
|
178 | - return; |
|
179 | - } |
|
180 | - $post = get_post(glsr(Multilingual::class)->getPostId($value)); |
|
181 | - if (empty($post->ID)) { |
|
182 | - return; |
|
183 | - } |
|
184 | - $permalink = glsr(Builder::class)->a(get_the_title($post->ID), [ |
|
185 | - 'href' => get_the_permalink($post->ID), |
|
186 | - ]); |
|
187 | - $assignedTo = sprintf(__('Review of %s', 'site-reviews'), $permalink); |
|
188 | - return '<span>'.$assignedTo.'</span>'; |
|
189 | - } |
|
170 | + /** |
|
171 | + * @param string $key |
|
172 | + * @param string $value |
|
173 | + * @return void|string |
|
174 | + */ |
|
175 | + protected function buildOptionAssignedTo($key, $value) |
|
176 | + { |
|
177 | + if ($this->isHidden($key, 'settings.reviews.assigned_links')) { |
|
178 | + return; |
|
179 | + } |
|
180 | + $post = get_post(glsr(Multilingual::class)->getPostId($value)); |
|
181 | + if (empty($post->ID)) { |
|
182 | + return; |
|
183 | + } |
|
184 | + $permalink = glsr(Builder::class)->a(get_the_title($post->ID), [ |
|
185 | + 'href' => get_the_permalink($post->ID), |
|
186 | + ]); |
|
187 | + $assignedTo = sprintf(__('Review of %s', 'site-reviews'), $permalink); |
|
188 | + return '<span>'.$assignedTo.'</span>'; |
|
189 | + } |
|
190 | 190 | |
191 | - /** |
|
192 | - * @param string $key |
|
193 | - * @param string $value |
|
194 | - * @return void|string |
|
195 | - */ |
|
196 | - protected function buildOptionAuthor($key, $value) |
|
197 | - { |
|
198 | - if (!$this->isHidden($key)) { |
|
199 | - $name = Str::convertName( |
|
200 | - $value, |
|
201 | - glsr_get_option('reviews.name.format'), |
|
202 | - glsr_get_option('reviews.name.initial') |
|
203 | - ); |
|
204 | - return '<span>'.$name.'</span>'; |
|
205 | - } |
|
206 | - } |
|
191 | + /** |
|
192 | + * @param string $key |
|
193 | + * @param string $value |
|
194 | + * @return void|string |
|
195 | + */ |
|
196 | + protected function buildOptionAuthor($key, $value) |
|
197 | + { |
|
198 | + if (!$this->isHidden($key)) { |
|
199 | + $name = Str::convertName( |
|
200 | + $value, |
|
201 | + glsr_get_option('reviews.name.format'), |
|
202 | + glsr_get_option('reviews.name.initial') |
|
203 | + ); |
|
204 | + return '<span>'.$name.'</span>'; |
|
205 | + } |
|
206 | + } |
|
207 | 207 | |
208 | - /** |
|
209 | - * @param string $key |
|
210 | - * @param string $value |
|
211 | - * @return void|string |
|
212 | - */ |
|
213 | - protected function buildOptionAvatar($key, $value) |
|
214 | - { |
|
215 | - if ($this->isHidden($key, 'settings.reviews.avatars')) { |
|
216 | - return; |
|
217 | - } |
|
218 | - $size = $this->getOption('settings.reviews.avatars_size', 40); |
|
219 | - return glsr(Builder::class)->img([ |
|
220 | - 'height' => $size, |
|
221 | - 'src' => $this->generateAvatar($value), |
|
222 | - 'style' => sprintf('width:%1$spx; height:%1$spx;', $size), |
|
223 | - 'width' => $size, |
|
224 | - ]); |
|
225 | - } |
|
208 | + /** |
|
209 | + * @param string $key |
|
210 | + * @param string $value |
|
211 | + * @return void|string |
|
212 | + */ |
|
213 | + protected function buildOptionAvatar($key, $value) |
|
214 | + { |
|
215 | + if ($this->isHidden($key, 'settings.reviews.avatars')) { |
|
216 | + return; |
|
217 | + } |
|
218 | + $size = $this->getOption('settings.reviews.avatars_size', 40); |
|
219 | + return glsr(Builder::class)->img([ |
|
220 | + 'height' => $size, |
|
221 | + 'src' => $this->generateAvatar($value), |
|
222 | + 'style' => sprintf('width:%1$spx; height:%1$spx;', $size), |
|
223 | + 'width' => $size, |
|
224 | + ]); |
|
225 | + } |
|
226 | 226 | |
227 | - /** |
|
228 | - * @param string $key |
|
229 | - * @param string $value |
|
230 | - * @return void|string |
|
231 | - */ |
|
232 | - protected function buildOptionContent($key, $value) |
|
233 | - { |
|
234 | - $text = $this->normalizeText($value); |
|
235 | - if (!$this->isHiddenOrEmpty($key, $text)) { |
|
236 | - return '<p>'.$text.'</p>'; |
|
237 | - } |
|
238 | - } |
|
227 | + /** |
|
228 | + * @param string $key |
|
229 | + * @param string $value |
|
230 | + * @return void|string |
|
231 | + */ |
|
232 | + protected function buildOptionContent($key, $value) |
|
233 | + { |
|
234 | + $text = $this->normalizeText($value); |
|
235 | + if (!$this->isHiddenOrEmpty($key, $text)) { |
|
236 | + return '<p>'.$text.'</p>'; |
|
237 | + } |
|
238 | + } |
|
239 | 239 | |
240 | - /** |
|
241 | - * @param string $key |
|
242 | - * @param string $value |
|
243 | - * @return void|string |
|
244 | - */ |
|
245 | - protected function buildOptionDate($key, $value) |
|
246 | - { |
|
247 | - if ($this->isHidden($key)) { |
|
248 | - return; |
|
249 | - } |
|
250 | - $dateFormat = $this->getOption('settings.reviews.date.format', 'default'); |
|
251 | - if ('relative' == $dateFormat) { |
|
252 | - $date = glsr(Date::class)->relative($value); |
|
253 | - } else { |
|
254 | - $format = 'custom' == $dateFormat |
|
255 | - ? $this->getOption('settings.reviews.date.custom', 'M j, Y') |
|
256 | - : glsr(OptionManager::class)->getWP('date_format', 'F j, Y'); |
|
257 | - $date = date_i18n($format, strtotime($value)); |
|
258 | - } |
|
259 | - return '<span>'.$date.'</span>'; |
|
260 | - } |
|
240 | + /** |
|
241 | + * @param string $key |
|
242 | + * @param string $value |
|
243 | + * @return void|string |
|
244 | + */ |
|
245 | + protected function buildOptionDate($key, $value) |
|
246 | + { |
|
247 | + if ($this->isHidden($key)) { |
|
248 | + return; |
|
249 | + } |
|
250 | + $dateFormat = $this->getOption('settings.reviews.date.format', 'default'); |
|
251 | + if ('relative' == $dateFormat) { |
|
252 | + $date = glsr(Date::class)->relative($value); |
|
253 | + } else { |
|
254 | + $format = 'custom' == $dateFormat |
|
255 | + ? $this->getOption('settings.reviews.date.custom', 'M j, Y') |
|
256 | + : glsr(OptionManager::class)->getWP('date_format', 'F j, Y'); |
|
257 | + $date = date_i18n($format, strtotime($value)); |
|
258 | + } |
|
259 | + return '<span>'.$date.'</span>'; |
|
260 | + } |
|
261 | 261 | |
262 | - /** |
|
263 | - * @param string $key |
|
264 | - * @param string $value |
|
265 | - * @return void|string |
|
266 | - */ |
|
267 | - protected function buildOptionRating($key, $value) |
|
268 | - { |
|
269 | - if (!$this->isHiddenOrEmpty($key, $value)) { |
|
270 | - return glsr_star_rating($value); |
|
271 | - } |
|
272 | - } |
|
262 | + /** |
|
263 | + * @param string $key |
|
264 | + * @param string $value |
|
265 | + * @return void|string |
|
266 | + */ |
|
267 | + protected function buildOptionRating($key, $value) |
|
268 | + { |
|
269 | + if (!$this->isHiddenOrEmpty($key, $value)) { |
|
270 | + return glsr_star_rating($value); |
|
271 | + } |
|
272 | + } |
|
273 | 273 | |
274 | - /** |
|
275 | - * @param string $key |
|
276 | - * @param string $value |
|
277 | - * @return void|string |
|
278 | - */ |
|
279 | - protected function buildOptionResponse($key, $value) |
|
280 | - { |
|
281 | - if ($this->isHiddenOrEmpty($key, $value)) { |
|
282 | - return; |
|
283 | - } |
|
284 | - $title = sprintf(__('Response from %s', 'site-reviews'), get_bloginfo('name')); |
|
285 | - $text = $this->normalizeText($value); |
|
286 | - $text = '<p><strong>'.$title.'</strong></p><p>'.$text.'</p>'; |
|
287 | - $response = glsr(Builder::class)->div($text, ['class' => 'glsr-review-response-inner']); |
|
288 | - $background = glsr(Builder::class)->div(['class' => 'glsr-review-response-background']); |
|
289 | - return $response.$background; |
|
290 | - } |
|
274 | + /** |
|
275 | + * @param string $key |
|
276 | + * @param string $value |
|
277 | + * @return void|string |
|
278 | + */ |
|
279 | + protected function buildOptionResponse($key, $value) |
|
280 | + { |
|
281 | + if ($this->isHiddenOrEmpty($key, $value)) { |
|
282 | + return; |
|
283 | + } |
|
284 | + $title = sprintf(__('Response from %s', 'site-reviews'), get_bloginfo('name')); |
|
285 | + $text = $this->normalizeText($value); |
|
286 | + $text = '<p><strong>'.$title.'</strong></p><p>'.$text.'</p>'; |
|
287 | + $response = glsr(Builder::class)->div($text, ['class' => 'glsr-review-response-inner']); |
|
288 | + $background = glsr(Builder::class)->div(['class' => 'glsr-review-response-background']); |
|
289 | + return $response.$background; |
|
290 | + } |
|
291 | 291 | |
292 | - /** |
|
293 | - * @param string $key |
|
294 | - * @param string $value |
|
295 | - * @return void|string |
|
296 | - */ |
|
297 | - protected function buildOptionTitle($key, $value) |
|
298 | - { |
|
299 | - if ($this->isHidden($key)) { |
|
300 | - return; |
|
301 | - } |
|
302 | - if (empty($value)) { |
|
303 | - $value = __('No Title', 'site-reviews'); |
|
304 | - } |
|
305 | - return '<h3>'.$value.'</h3>'; |
|
306 | - } |
|
292 | + /** |
|
293 | + * @param string $key |
|
294 | + * @param string $value |
|
295 | + * @return void|string |
|
296 | + */ |
|
297 | + protected function buildOptionTitle($key, $value) |
|
298 | + { |
|
299 | + if ($this->isHidden($key)) { |
|
300 | + return; |
|
301 | + } |
|
302 | + if (empty($value)) { |
|
303 | + $value = __('No Title', 'site-reviews'); |
|
304 | + } |
|
305 | + return '<h3>'.$value.'</h3>'; |
|
306 | + } |
|
307 | 307 | |
308 | - /** |
|
309 | - * @param string $avatarUrl |
|
310 | - * @return string |
|
311 | - */ |
|
312 | - protected function generateAvatar($avatarUrl) |
|
313 | - { |
|
314 | - if (!$this->isOptionEnabled('settings.reviews.avatars_regenerate') || 'local' != $this->current->review_type) { |
|
315 | - return $avatarUrl; |
|
316 | - } |
|
317 | - if ($this->current->user_id) { |
|
318 | - $authorIdOrEmail = get_the_author_meta('ID', $this->current->user_id); |
|
319 | - } |
|
320 | - if (empty($authorIdOrEmail)) { |
|
321 | - $authorIdOrEmail = $this->current->email; |
|
322 | - } |
|
323 | - if ($newAvatar = get_avatar_url($authorIdOrEmail)) { |
|
324 | - return $newAvatar; |
|
325 | - } |
|
326 | - return $avatarUrl; |
|
327 | - } |
|
308 | + /** |
|
309 | + * @param string $avatarUrl |
|
310 | + * @return string |
|
311 | + */ |
|
312 | + protected function generateAvatar($avatarUrl) |
|
313 | + { |
|
314 | + if (!$this->isOptionEnabled('settings.reviews.avatars_regenerate') || 'local' != $this->current->review_type) { |
|
315 | + return $avatarUrl; |
|
316 | + } |
|
317 | + if ($this->current->user_id) { |
|
318 | + $authorIdOrEmail = get_the_author_meta('ID', $this->current->user_id); |
|
319 | + } |
|
320 | + if (empty($authorIdOrEmail)) { |
|
321 | + $authorIdOrEmail = $this->current->email; |
|
322 | + } |
|
323 | + if ($newAvatar = get_avatar_url($authorIdOrEmail)) { |
|
324 | + return $newAvatar; |
|
325 | + } |
|
326 | + return $avatarUrl; |
|
327 | + } |
|
328 | 328 | |
329 | - /** |
|
330 | - * @param string $text |
|
331 | - * @param int $limit |
|
332 | - * @return int |
|
333 | - */ |
|
334 | - protected function getExcerptIntlSplit($text, $limit) |
|
335 | - { |
|
336 | - $words = IntlRuleBasedBreakIterator::createWordInstance(''); |
|
337 | - $words->setText($text); |
|
338 | - $count = 0; |
|
339 | - foreach ($words as $offset) { |
|
340 | - if (IntlRuleBasedBreakIterator::WORD_NONE === $words->getRuleStatus()) { |
|
341 | - continue; |
|
342 | - } |
|
343 | - ++$count; |
|
344 | - if ($count != $limit) { |
|
345 | - continue; |
|
346 | - } |
|
347 | - return $offset; |
|
348 | - } |
|
349 | - return strlen($text); |
|
350 | - } |
|
329 | + /** |
|
330 | + * @param string $text |
|
331 | + * @param int $limit |
|
332 | + * @return int |
|
333 | + */ |
|
334 | + protected function getExcerptIntlSplit($text, $limit) |
|
335 | + { |
|
336 | + $words = IntlRuleBasedBreakIterator::createWordInstance(''); |
|
337 | + $words->setText($text); |
|
338 | + $count = 0; |
|
339 | + foreach ($words as $offset) { |
|
340 | + if (IntlRuleBasedBreakIterator::WORD_NONE === $words->getRuleStatus()) { |
|
341 | + continue; |
|
342 | + } |
|
343 | + ++$count; |
|
344 | + if ($count != $limit) { |
|
345 | + continue; |
|
346 | + } |
|
347 | + return $offset; |
|
348 | + } |
|
349 | + return strlen($text); |
|
350 | + } |
|
351 | 351 | |
352 | - /** |
|
353 | - * @param string $text |
|
354 | - * @param int $limit |
|
355 | - * @return int |
|
356 | - */ |
|
357 | - protected function getExcerptSplit($text, $limit) |
|
358 | - { |
|
359 | - if (str_word_count($text, 0) > $limit) { |
|
360 | - $words = array_keys(str_word_count($text, 2)); |
|
361 | - return $words[$limit]; |
|
362 | - } |
|
363 | - return strlen($text); |
|
364 | - } |
|
352 | + /** |
|
353 | + * @param string $text |
|
354 | + * @param int $limit |
|
355 | + * @return int |
|
356 | + */ |
|
357 | + protected function getExcerptSplit($text, $limit) |
|
358 | + { |
|
359 | + if (str_word_count($text, 0) > $limit) { |
|
360 | + $words = array_keys(str_word_count($text, 2)); |
|
361 | + return $words[$limit]; |
|
362 | + } |
|
363 | + return strlen($text); |
|
364 | + } |
|
365 | 365 | |
366 | - /** |
|
367 | - * @param string $path |
|
368 | - * @param mixed $fallback |
|
369 | - * @return mixed |
|
370 | - */ |
|
371 | - protected function getOption($path, $fallback = '') |
|
372 | - { |
|
373 | - if (array_key_exists($path, $this->options)) { |
|
374 | - return $this->options[$path]; |
|
375 | - } |
|
376 | - return $fallback; |
|
377 | - } |
|
366 | + /** |
|
367 | + * @param string $path |
|
368 | + * @param mixed $fallback |
|
369 | + * @return mixed |
|
370 | + */ |
|
371 | + protected function getOption($path, $fallback = '') |
|
372 | + { |
|
373 | + if (array_key_exists($path, $this->options)) { |
|
374 | + return $this->options[$path]; |
|
375 | + } |
|
376 | + return $fallback; |
|
377 | + } |
|
378 | 378 | |
379 | - /** |
|
380 | - * @param string $path |
|
381 | - * @return bool |
|
382 | - */ |
|
383 | - protected function isOptionEnabled($path) |
|
384 | - { |
|
385 | - return 'yes' == $this->getOption($path); |
|
386 | - } |
|
379 | + /** |
|
380 | + * @param string $path |
|
381 | + * @return bool |
|
382 | + */ |
|
383 | + protected function isOptionEnabled($path) |
|
384 | + { |
|
385 | + return 'yes' == $this->getOption($path); |
|
386 | + } |
|
387 | 387 | |
388 | - /** |
|
389 | - * @return void |
|
390 | - */ |
|
391 | - protected function wrap(array &$renderedFields, Review $review) |
|
392 | - { |
|
393 | - $renderedFields = apply_filters('site-reviews/review/wrap', $renderedFields, $review, $this); |
|
394 | - array_walk($renderedFields, function (&$value, $key) use ($review) { |
|
395 | - $value = apply_filters('site-reviews/review/wrap/'.$key, $value, $review); |
|
396 | - if (empty($value)) { |
|
397 | - return; |
|
398 | - } |
|
399 | - $value = glsr(Builder::class)->div($value, [ |
|
400 | - 'class' => 'glsr-review-'.$key, |
|
401 | - ]); |
|
402 | - }); |
|
403 | - } |
|
388 | + /** |
|
389 | + * @return void |
|
390 | + */ |
|
391 | + protected function wrap(array &$renderedFields, Review $review) |
|
392 | + { |
|
393 | + $renderedFields = apply_filters('site-reviews/review/wrap', $renderedFields, $review, $this); |
|
394 | + array_walk($renderedFields, function (&$value, $key) use ($review) { |
|
395 | + $value = apply_filters('site-reviews/review/wrap/'.$key, $value, $review); |
|
396 | + if (empty($value)) { |
|
397 | + return; |
|
398 | + } |
|
399 | + $value = glsr(Builder::class)->div($value, [ |
|
400 | + 'class' => 'glsr-review-'.$key, |
|
401 | + ]); |
|
402 | + }); |
|
403 | + } |
|
404 | 404 | } |
@@ -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 | } |