|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Contracts\FieldContract; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Defaults\PaginationDefaults; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Defaults\StyleClassesDefaults; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Defaults\StyleValidationDefaults; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
|
11
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @method string classes(string $key) |
|
15
|
|
|
* @method string defaultClasses(string $key) |
|
16
|
|
|
* @method string defaultValidation(string $key) |
|
17
|
|
|
* @method string validation(string $key) |
|
18
|
|
|
*/ |
|
19
|
|
|
class Style |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The properties that are accessible. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $accessible = [ |
|
27
|
|
|
'classes', 'style', 'pagination', 'validation', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The methods that are callable. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $callable = [ |
|
36
|
|
|
'classes', 'validation', |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $classes; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $style; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $pagination; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var array |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $validation; |
|
58
|
|
|
|
|
59
|
21 |
|
public function __call($method, $args) |
|
60
|
|
|
{ |
|
61
|
21 |
|
$property = strtolower(Str::removePrefix($method, 'default')); |
|
62
|
21 |
|
if (!in_array($property, $this->callable)) { |
|
63
|
|
|
return; |
|
64
|
|
|
} |
|
65
|
21 |
|
$key = Arr::get($args, 0); |
|
66
|
21 |
|
if (str_starts_with($method, 'default')) { |
|
67
|
20 |
|
$className = Helper::buildClassName(['style', $property, 'defaults'], 'Defaults'); |
|
68
|
20 |
|
return glsr()->args(glsr($className)->defaults())->$key; |
|
69
|
|
|
} |
|
70
|
21 |
|
return glsr()->args($this->__get($property))->$key; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
21 |
|
public function __get($property) |
|
74
|
|
|
{ |
|
75
|
21 |
|
if (!in_array($property, $this->accessible)) { |
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
21 |
|
if (!isset($this->$property)) { |
|
79
|
1 |
|
$style = glsr_get_option('general.style', 'default'); |
|
80
|
1 |
|
$config = shortcode_atts(array_fill_keys(['classes', 'pagination', 'validation'], []), |
|
81
|
1 |
|
glsr()->config("styles/{$style}") |
|
82
|
1 |
|
); |
|
83
|
1 |
|
$this->classes = glsr(StyleClassesDefaults::class)->restrict($config['classes']); |
|
84
|
1 |
|
$this->pagination = glsr(PaginationDefaults::class)->restrict($config['pagination']); |
|
85
|
1 |
|
$this->style = $style; |
|
86
|
1 |
|
$this->validation = glsr(StyleValidationDefaults::class)->restrict($config['validation']); |
|
87
|
|
|
} |
|
88
|
21 |
|
return $this->$property; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
20 |
|
public function fieldClass(FieldContract $field): string |
|
92
|
|
|
{ |
|
93
|
20 |
|
if (!array_key_exists($field->tag(), $this->__get('classes'))) { |
|
94
|
|
|
return $field->class; |
|
95
|
|
|
} |
|
96
|
20 |
|
$key = "{$field->tag()}_{$field->type}"; |
|
97
|
20 |
|
$fallback = Arr::get($this->classes, $field->tag()); |
|
98
|
20 |
|
$class = Arr::getAs('string', $this->classes, $key, $fallback); |
|
99
|
20 |
|
return trim("{$class} {$field->class}"); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* This allows us to override the pagination config in /config/styles instead of using a filter hook. |
|
104
|
|
|
*/ |
|
105
|
|
|
public function paginationArgs(array $args): array |
|
106
|
|
|
{ |
|
107
|
|
|
return wp_parse_args($args, $this->__get('pagination')); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function styleClasses(string $additional = ''): string |
|
111
|
|
|
{ |
|
112
|
|
|
$style = $this->__get('style'); |
|
113
|
|
|
$classes = ['glsr']; |
|
114
|
|
|
$classes[] = glsr()->filterString('style', "glsr-{$style}"); |
|
115
|
|
|
$classes[] = $additional; |
|
116
|
|
|
$attribute = implode(' ', $classes); |
|
117
|
|
|
return glsr(Sanitizer::class)->sanitizeAttrClass($attribute); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function stylesheetUrl(?string $suffix = ''): string |
|
121
|
|
|
{ |
|
122
|
|
|
if ($suffix) { |
|
123
|
|
|
$string = 'assets/styles/%1$s/%2$s-%1$s.css'; |
|
124
|
|
|
$path = sprintf($string, $suffix, $this->__get('style')); |
|
125
|
|
|
return file_exists(glsr()->path($path)) |
|
126
|
|
|
? glsr()->url($path) |
|
127
|
|
|
: glsr()->url(sprintf($string, $suffix, 'default')); |
|
128
|
|
|
} |
|
129
|
|
|
$string = 'assets/styles/%s.css'; |
|
130
|
|
|
$path = sprintf($string, $this->__get('style')); |
|
131
|
|
|
return file_exists(glsr()->path($path)) |
|
132
|
|
|
? glsr()->url($path) |
|
133
|
|
|
: glsr()->url(sprintf($string, 'default')); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
121 |
|
public function view(string $view): string |
|
137
|
|
|
{ |
|
138
|
121 |
|
$templates = [ |
|
139
|
121 |
|
'templates/form/field', |
|
140
|
121 |
|
'templates/form/response', |
|
141
|
121 |
|
'templates/form/submit-button', |
|
142
|
121 |
|
'templates/form/type-checkbox', |
|
143
|
121 |
|
'templates/form/type-radio', |
|
144
|
121 |
|
'templates/form/type-range', |
|
145
|
121 |
|
'templates/form/type-toggle', |
|
146
|
121 |
|
'templates/load-more-button', |
|
147
|
121 |
|
'templates/pagination', |
|
148
|
121 |
|
'templates/reviews-form', |
|
149
|
121 |
|
]; |
|
150
|
121 |
|
$templates = glsr()->filterArray('style/templates', $templates); |
|
151
|
121 |
|
if (!preg_match('('.implode('|', $templates).')', $view)) { |
|
152
|
100 |
|
return $view; |
|
153
|
|
|
} |
|
154
|
21 |
|
$views = $this->generatePossibleViews($view); |
|
155
|
21 |
|
foreach ($views as $possibleView) { |
|
156
|
21 |
|
if (file_exists(glsr()->file($possibleView))) { |
|
157
|
21 |
|
return Str::removePrefix($possibleView, 'views/'); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
return $view; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
21 |
|
protected function generatePossibleViews(string $view): array |
|
164
|
|
|
{ |
|
165
|
21 |
|
$basename = basename($view); |
|
166
|
21 |
|
$basepath = rtrim($view, $basename); |
|
167
|
21 |
|
$style = $this->__get('style'); |
|
168
|
21 |
|
$customPath = "views/styles/{$style}/"; |
|
169
|
21 |
|
$parts = explode('_', $basename); |
|
170
|
21 |
|
$views = [ |
|
171
|
21 |
|
$customPath.$basename, // styled view |
|
172
|
21 |
|
$customPath.$parts[0], // styled view (base) |
|
173
|
21 |
|
$view, // default view |
|
174
|
21 |
|
$basepath.$parts[0], // default view (base) |
|
175
|
21 |
|
]; |
|
176
|
21 |
|
$views = glsr()->filterArray('style/views', $views, $view); |
|
177
|
21 |
|
return array_filter($views); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|