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