|
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\Helpers\Arr; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
|
11
|
|
|
|
|
12
|
|
|
class Style |
|
13
|
|
|
{ |
|
14
|
|
|
public array $classes; |
|
15
|
|
|
public string $style; |
|
16
|
|
|
public array $pagination; |
|
17
|
|
|
public array $validation; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct() |
|
20
|
|
|
{ |
|
21
|
|
|
$styleName = glsr_get_option('general.style', 'default'); |
|
22
|
|
|
$config = shortcode_atts( |
|
23
|
|
|
array_fill_keys(['classes', 'pagination', 'validation'], []), |
|
24
|
|
|
glsr()->config("styles/{$styleName}") |
|
25
|
|
|
); |
|
26
|
|
|
$this->style = $styleName; |
|
27
|
|
|
$this->classes = glsr(StyleClassesDefaults::class)->restrict($config['classes']); |
|
28
|
|
|
$this->pagination = glsr(PaginationDefaults::class)->restrict($config['pagination']); |
|
29
|
|
|
$this->validation = glsr(StyleValidationDefaults::class)->restrict($config['validation']); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
21 |
|
public function classes(string $key): string |
|
33
|
|
|
{ |
|
34
|
21 |
|
return $this->classes[$key] ?? ''; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
20 |
|
public function defaultClasses(string $key): string |
|
38
|
|
|
{ |
|
39
|
20 |
|
return glsr(StyleClassesDefaults::class)->defaults()[$key] ?? ''; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function defaultValidation(string $key): string |
|
43
|
|
|
{ |
|
44
|
|
|
return glsr(StyleValidationDefaults::class)->defaults()[$key] ?? ''; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
20 |
|
public function fieldElementClass(FieldContract $field): string |
|
48
|
|
|
{ |
|
49
|
20 |
|
$tag = $field->tag(); |
|
50
|
20 |
|
if (!array_key_exists($tag, $this->classes)) { |
|
51
|
|
|
return $field->class; |
|
52
|
|
|
} |
|
53
|
20 |
|
$specific = "{$tag}_{$field->type}"; |
|
54
|
20 |
|
$fallback = $this->classes[$tag] ?? ''; |
|
55
|
20 |
|
$custom = Arr::getAs('string', $this->classes, $specific, $fallback); |
|
56
|
20 |
|
return trim("{$custom} {$field->class}"); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* This allows us to override the pagination config in /config/styles instead of using a filter hook. |
|
61
|
|
|
*/ |
|
62
|
|
|
public function paginationArgs(array $args): array |
|
63
|
|
|
{ |
|
64
|
|
|
return wp_parse_args($args, $this->pagination); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function styleClasses(string $additional = ''): string |
|
68
|
|
|
{ |
|
69
|
|
|
$style = glsr()->filterString('style', "glsr-{$this->style}"); |
|
70
|
|
|
$classes = ['glsr', $style, $additional]; |
|
71
|
|
|
return glsr(Sanitizer::class)->sanitizeAttrClass(implode(' ', $classes)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function stylesheetUrl(?string $suffix = ''): string |
|
75
|
|
|
{ |
|
76
|
|
|
if ($suffix) { |
|
77
|
|
|
$format = 'assets/styles/%1$s/%2$s-%1$s.css'; |
|
78
|
|
|
$path = sprintf($format, $suffix, $this->style); |
|
79
|
|
|
$fallback = sprintf($format, $suffix, 'default'); |
|
80
|
|
|
} else { |
|
81
|
|
|
$format = 'assets/styles/%s.css'; |
|
82
|
|
|
$path = sprintf($format, $this->style); |
|
83
|
|
|
$fallback = sprintf($format, 'default'); |
|
84
|
|
|
} |
|
85
|
|
|
return file_exists(glsr()->path($path)) |
|
86
|
|
|
? glsr()->url($path) |
|
87
|
|
|
: glsr()->url($fallback); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
21 |
|
public function validation(string $key): string |
|
91
|
|
|
{ |
|
92
|
21 |
|
return $this->validation[$key] ?? ''; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
130 |
|
public function view(string $view): string |
|
96
|
|
|
{ |
|
97
|
130 |
|
static $allowed = null; |
|
98
|
130 |
|
if (null === $allowed) { |
|
99
|
|
|
$allowed = glsr()->filterArray('style/templates', [ |
|
100
|
|
|
'templates/form/field', |
|
101
|
|
|
'templates/form/response', |
|
102
|
|
|
'templates/form/submit-button', |
|
103
|
|
|
'templates/form/type-checkbox', |
|
104
|
|
|
'templates/form/type-radio', |
|
105
|
|
|
'templates/form/type-range', |
|
106
|
|
|
'templates/form/type-toggle', |
|
107
|
|
|
'templates/load-more-button', |
|
108
|
|
|
'templates/pagination', |
|
109
|
|
|
'templates/reviews-form', |
|
110
|
|
|
]); |
|
111
|
|
|
$allowed = '~('.implode('|', $allowed).')~'; |
|
112
|
|
|
} |
|
113
|
130 |
|
if (!preg_match($allowed, $view)) { |
|
114
|
109 |
|
return $view; |
|
115
|
|
|
} |
|
116
|
21 |
|
foreach ($this->possibleViews($view) as $candidate) { |
|
117
|
21 |
|
if (file_exists(glsr()->file($candidate))) { |
|
118
|
21 |
|
return Str::removePrefix($candidate, 'views/'); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
return $view; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
21 |
|
protected function possibleViews(string $view): array |
|
125
|
|
|
{ |
|
126
|
21 |
|
$filename = basename($view); |
|
127
|
21 |
|
$dirname = dirname($view).'/'; |
|
128
|
21 |
|
$shortName = strstr($filename, '_', true) ?: $filename; // before first _ or full |
|
129
|
21 |
|
$styledPath = "views/styles/{$this->style}/"; |
|
130
|
21 |
|
$candidates = [ |
|
131
|
21 |
|
$styledPath.$filename, // styled exact |
|
132
|
21 |
|
$styledPath.$shortName, // styled base |
|
133
|
21 |
|
$view, // default exact |
|
134
|
21 |
|
$dirname.$shortName, // default base |
|
135
|
21 |
|
]; |
|
136
|
21 |
|
return array_filter(glsr()->filterArray('style/views', $candidates, $view)); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|