1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Application; |
6
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
7
|
|
|
use GeminiLabs\SiteReviews\Helper; |
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
9
|
|
|
|
10
|
|
|
class Style |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
public $style; |
16
|
|
|
|
17
|
7 |
|
public function __construct() |
18
|
|
|
{ |
19
|
7 |
|
$this->style = glsr( OptionManager::class )->get( 'settings.submissions.style', 'default' ); |
20
|
7 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $view |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
7 |
|
public function filterView( $view ) |
27
|
|
|
{ |
28
|
|
|
$styledViews = [ |
29
|
7 |
|
'templates/form/field', |
30
|
|
|
'templates/form/response', |
31
|
|
|
'templates/form/submit-button', |
32
|
|
|
'templates/reviews-form', |
33
|
|
|
]; |
34
|
7 |
|
if( !preg_match( '('.implode( '|', $styledViews ).')', $view )) { |
35
|
7 |
|
return $view; |
36
|
|
|
} |
37
|
|
|
$views = $this->generatePossibleViews( $view ); |
38
|
|
|
foreach( $views as $possibleView ) { |
39
|
|
|
if( !file_exists( glsr()->path( 'views/'.$possibleView.'.php' )))continue; |
40
|
|
|
return $possibleView; |
41
|
|
|
} |
42
|
|
|
return $view; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function get() |
49
|
|
|
{ |
50
|
|
|
return $this->style; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
public function modifyField( Builder $instance ) |
57
|
|
|
{ |
58
|
|
|
$styles = glsr()->config( 'styles/'.$this->style ); |
59
|
|
|
if( !$this->isPublicInstance( $instance ) || empty( $styles ))return; |
60
|
|
|
call_user_func_array( [$this, 'customize'], [&$instance, $this->normalizeStyles( $styles )] ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
protected function customize( Builder $instance, array $styles ) |
67
|
|
|
{ |
68
|
|
|
$args = wp_parse_args( $instance->args, array_fill_keys( ['class', 'type'], '' )); |
69
|
|
|
$key = $instance->tag.'_'.$args['type']; |
70
|
|
|
$classes = !isset( $styles[$key] ) |
71
|
|
|
? $styles[$instance->tag] |
72
|
|
|
: $styles[$key]; |
73
|
|
|
$instance->args['class'] = trim( $args['class'].' '.$classes ); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $view |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
protected function generatePossibleViews( $view ) |
81
|
|
|
{ |
82
|
|
|
$basename = basename( $view ); |
83
|
|
|
$basepath = rtrim( $view, $basename ); |
84
|
|
|
$customPath = 'partials/styles/'.$this->style.'/'; |
85
|
|
|
$parts = explode( '_', $basename ); |
86
|
|
|
$views = [ |
87
|
|
|
$customPath.$basename, |
88
|
|
|
$customPath.$parts[0], |
89
|
|
|
$view, |
90
|
|
|
$basepath.$parts[0], |
91
|
|
|
]; |
92
|
|
|
return array_filter( $views ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
protected function isPublicInstance( Builder $instance ) |
99
|
|
|
{ |
100
|
|
|
$args = wp_parse_args( $instance->args, [ |
101
|
|
|
'is_public' => false, |
102
|
|
|
'is_raw' => false, |
103
|
|
|
]); |
104
|
|
|
if( is_admin() || !$args['is_public'] || $args['is_raw'] ) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param |
112
|
|
|
* @return |
113
|
|
|
*/ |
|
|
|
|
114
|
|
|
protected function normalizeStyles( array $styles ) |
115
|
|
|
{ |
116
|
|
|
$keys = [ |
117
|
|
|
'div', 'div_checkbox', 'div_radio', 'input', 'input_checkbox', 'input_radio', 'label', |
118
|
|
|
'label_checkbox', 'label_radio', 'select', 'textarea', |
119
|
|
|
]; |
120
|
|
|
return wp_parse_args( $styles, array_fill_keys( $keys, '' )); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|