1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Application; |
6
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
7
|
|
|
use GeminiLabs\SiteReviews\Defaults\PaginationDefaults; |
8
|
|
|
use GeminiLabs\SiteReviews\Defaults\StyleFieldsDefaults; |
9
|
|
|
use GeminiLabs\SiteReviews\Defaults\StyleValidationDefaults; |
10
|
|
|
use GeminiLabs\SiteReviews\Helper; |
11
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
12
|
|
|
|
13
|
|
|
class Style |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
public $fields; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
public $style; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
public $pagination; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
public $validation; |
34
|
|
|
|
35
|
7 |
|
public function __construct() |
36
|
|
|
{ |
37
|
7 |
|
$this->style = glsr( OptionManager::class )->get( 'settings.general.style', 'default' ); |
38
|
7 |
|
$this->setConfig(); |
39
|
7 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $view |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
7 |
|
public function filterView( $view ) |
46
|
|
|
{ |
47
|
|
|
$styledViews = [ |
48
|
7 |
|
'templates/form/field', |
49
|
|
|
'templates/form/response', |
50
|
|
|
'templates/form/submit-button', |
51
|
|
|
'templates/reviews-form', |
52
|
|
|
]; |
53
|
7 |
|
if( !preg_match( '('.implode( '|', $styledViews ).')', $view )) { |
54
|
7 |
|
return $view; |
55
|
|
|
} |
56
|
|
|
$views = $this->generatePossibleViews( $view ); |
57
|
|
|
foreach( $views as $possibleView ) { |
58
|
|
|
if( !file_exists( glsr()->file( $possibleView )))continue; |
59
|
|
|
return glsr( Helper::class )->removePrefix( 'views/', $possibleView ); |
60
|
|
|
} |
61
|
|
|
return $view; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function get() |
68
|
|
|
{ |
69
|
|
|
return apply_filters( 'site-reviews/style', $this->style ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
7 |
|
public function setConfig() |
76
|
|
|
{ |
77
|
7 |
|
$config = shortcode_atts( |
78
|
7 |
|
array_fill_keys( ['fields', 'pagination', 'validation'], [] ), |
79
|
7 |
|
glsr()->config( 'styles/'.$this->style ) |
80
|
|
|
); |
81
|
7 |
|
$this->fields = glsr( StyleFieldsDefaults::class )->restrict( $config['fields'] ); |
82
|
7 |
|
$this->pagination = glsr( PaginationDefaults::class )->merge( $config['pagination'] ); |
83
|
7 |
|
$this->validation = glsr( StyleValidationDefaults::class )->restrict( $config['validation'] ); |
84
|
7 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
public function modifyField( Builder $instance ) |
90
|
|
|
{ |
91
|
|
|
if( !$this->isPublicInstance( $instance ) || empty( array_filter( $this->fields )))return; |
92
|
|
|
call_user_func_array( [$this, 'customize'], [&$instance] ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
public function paginationArgs( array $args ) |
99
|
|
|
{ |
100
|
|
|
return wp_parse_args( $args, $this->pagination ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
protected function customize( Builder $instance ) |
107
|
|
|
{ |
108
|
|
|
if( !array_key_exists( $instance->tag, $this->fields ))return; |
109
|
|
|
$args = wp_parse_args( $instance->args, array_fill_keys( ['class', 'type'], '' )); |
110
|
|
|
$key = $instance->tag.'_'.$args['type']; |
111
|
|
|
$classes = !isset( $this->fields[$key] ) |
112
|
|
|
? $this->fields[$instance->tag] |
113
|
|
|
: $this->fields[$key]; |
114
|
|
|
$instance->args['class'] = trim( $args['class'].' '.$classes ); |
115
|
|
|
do_action_ref_array( 'site-reviews/customize/'.$this->style, [&$instance] ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $view |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
protected function generatePossibleViews( $view ) |
123
|
|
|
{ |
124
|
|
|
$basename = basename( $view ); |
125
|
|
|
$basepath = rtrim( $view, $basename ); |
126
|
|
|
$customPath = 'views/partials/styles/'.$this->style.'/'; |
127
|
|
|
$parts = explode( '_', $basename ); |
128
|
|
|
$views = [ |
129
|
|
|
$customPath.$basename, |
130
|
|
|
$customPath.$parts[0], |
131
|
|
|
$view, |
132
|
|
|
$basepath.$parts[0], |
133
|
|
|
]; |
134
|
|
|
return array_filter( $views ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
protected function isPublicInstance( Builder $instance ) |
141
|
|
|
{ |
142
|
|
|
$args = wp_parse_args( $instance->args, [ |
143
|
|
|
'is_public' => false, |
144
|
|
|
'is_raw' => false, |
145
|
|
|
]); |
146
|
|
|
if( is_admin() || !$args['is_public'] || $args['is_raw'] ) { |
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
return true; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|