class-wpml-ls-template.php
5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
class WPML_LS_Template extends WPML_Templates_Factory {
const FILENAME = 'template.twig';
/* @var array $template */
private $template;
/* @var array $model */
private $model;
/* @var string $prefix */
private $prefix = 'wpml-ls-';
/**
* WPML_Language_Switcher_Menu constructor.
*
* @param array $template_data
* @param array $template_model
*/
public function __construct( $template_data, $template_model = array() ) {
$this->template = $this->format_data($template_data);
$this->template['js'] = self::remove_non_minified_duplicates( $this->template['js'] );
$this->template['css'] = self::remove_non_minified_duplicates( $this->template['css'] );
if ( array_key_exists( 'template_string', $this->template ) ) {
$this->template_string = $this->template['template_string'];
}
$this->model = $template_model;
parent::__construct();
}
/**
* Make sure some elements are of array type
*
* @param array $template_data
*
* @return array
*/
private function format_data( $template_data ) {
foreach ( array( 'path', 'js', 'css' ) as $k ) {
$template_data[ $k ] = isset( $template_data[ $k ] ) ? $template_data[ $k ] : array();
$template_data[ $k ] = is_array( $template_data[ $k ] ) ? $template_data[ $k ] : array( $template_data[ $k ] );
}
return $template_data;
}
/**
* @param array $model
*/
public function set_model( $model ) {
$this->model = is_array( $model ) ? $model : array( $model );
}
/**
* @return string
* @throws \WPML\Core\Twig\Error\LoaderError
* @throws \WPML\Core\Twig\Error\RuntimeError
* @throws \WPML\Core\Twig\Error\SyntaxError
*/
public function get_html() {
$ret = '';
if ( $this->template_paths || $this->template_string ) {
$ret = parent::get_view();
}
return $ret;
}
/**
* @param bool $with_version
*
* @return array
*/
public function get_styles( $with_version = false ) {
$styles = $with_version
? array_map( array( $this, 'add_resource_version' ), $this->template['css'] )
: $this->template['css'];
return array_values( $styles );
}
/**
* @return bool
*/
public function has_styles() {
return ! empty( $this->template['css'] );
}
/**
* @param bool $with_version
*
* @return array
*/
public function get_scripts( $with_version = false ) {
$scripts = $with_version
? array_map( array( $this, 'add_resource_version' ), $this->template['js'] )
: $this->template['js'];
return array_values( $scripts );
}
/**
* @param string $url
*
* @return string
*/
private function add_resource_version( $url ) {
return $url . '?ver=' . $this->get_version();
}
/**
* @param int $index
*
* @return string
*/
public function get_resource_handler( $index ) {
$slug = isset( $this->template['slug'] ) ? $this->template['slug'] : '';
$prefix = $this->is_core() ? '' : $this->prefix;
return $prefix . $slug . '-' . $index;
}
/**
* @return mixed|string|bool
*/
public function get_inline_style_handler() {
$count = count( $this->template['css'] );
return $count > 0 ? $this->get_resource_handler( $count - 1 ) : null;
}
/**
* @return string
*/
public function get_version() {
return $this->template['version'];
}
protected function init_template_base_dir() {
$this->template_paths = $this->template['path'];
}
/**
* @return string Template filename
*/
public function get_template() {
$template = self::FILENAME;
if ( isset( $this->template_string ) ) {
$template = $this->template_string;
} elseif ( array_key_exists( 'filename', $this->template ) ) {
$template = $this->template['filename'];
}
return $template;
}
/**
* @return array
*/
public function get_model() {
return $this->model;
}
/**
* @return array
*/
public function get_template_data() {
return $this->template;
}
/**
* @param array $template
*/
public function set_template_data( $template ) {
$this->template = $template;
}
/**
* return bool
*/
public function is_core() {
return isset( $this->template['is_core'] ) ? (bool) $this->template['is_core'] : false;
}
/**
* @return array
*/
public function supported_slot_types() {
return isset( $this->template['for'] ) ? $this->template['for'] : array();
}
/**
* @return array
*/
public function force_settings() {
return isset( $this->template['force_settings'] ) ? $this->template['force_settings'] : array();
}
public function is_path_valid() {
$valid = true;
$this->template_paths = apply_filters( 'wpml_ls_template_paths', $this->template_paths );
foreach ( $this->template_paths as $path ) {
if ( ! file_exists( $path ) ) {
$valid = false;
break;
}
}
return $valid;
}
/**
* @param string $template_string
*/
public function set_template_string( $template_string ) {
if ( method_exists( $this, 'is_string_template' ) ) {
$this->template_string = $template_string;
}
}
/**
* If an asset has a minified and a non-minified version,
* we remove the non-minified version.
*
* @param array $assets
*
* @return array
*/
public static function remove_non_minified_duplicates( array $assets ) {
$hasMinifiedVersion = function( $url ) use ( $assets ) {
$extension = pathinfo( $url, PATHINFO_EXTENSION );
$basename = pathinfo( $url, PATHINFO_BASENAME );
$filename = pathinfo( $url, PATHINFO_FILENAME );
$url_start = substr( $url, 0, strlen( $url ) - strlen( $basename ) );
$minified_file = $filename . '.min.' . $extension;
if ( in_array( $url_start . $minified_file, $assets, true ) ) {
return true;
}
return false;
};
return wpml_collect( $assets )
->reject( $hasMinifiedVersion )
->toArray();
}
}