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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
<?php
/** * Class for working with ZIP archives to import * sliders with images and other attachments. * * @package LS_ImportUtil * @since 5.0.3 * @author John Gera * @copyright Copyright (c) 2013 John Gera, George Krupa, and Kreatura Media Kft. * @license http://codecanyon.net/licenses/faq Envato marketplace licenses */
class LS_ImportUtil {
// Counts the number of sliders imported. public $sliderCount = 0;
// Database ID of the lastly imported slider. public $lastImportId;
// The managed ZipArchieve instance. private $zip;
// Target folders private $uploadsDir, $targetDir, $targetURL, $tmpDir;
// Imported images private $imported = array();
// Accepts $_FILES public function __construct( $archive, $name = null, $groupName = null ) {
// Attempt to workaround memory limit & execution time issues @ini_set( 'max_execution_time', 0 ); @ini_set( 'memory_limit', '256M' );
if(empty($name)) { $name = $archive; }
// TODO: check file extension to support old import method $type = wp_check_filetype(basename($name), array( 'zip' => 'application/zip', 'json' => 'application/json' ));
// Check for ZIP if(!empty($type['ext']) && $type['ext'] == 'zip') { if(class_exists('ZipArchive')) {
// Remove previous uploads (if any) $this->cleanup();
// Extract ZIP $this->zip = new ZipArchive; if($this->zip->open($archive)) { if($this->unpack($archive)) {
// Uploaded folders $folders = glob($this->tmpDir.'/*', GLOB_ONLYDIR);
$groupId = NULL; if( ! empty( $groupName ) && count( $folders ) > 1 ) { $groupId = LS_Sliders::addGroup( $groupName ); }
foreach( $folders as $key => $dir) {
$this->imported = array();
if(!isset($_POST['skip_images'])) { $this->uploadMedia($dir); }
if(file_exists($dir.'/settings.json')) { $this->lastImportId = $this->addSlider($dir.'/settings.json', $groupId); } }
// Finishing up $this->cleanup(); return true; }
// Close ZIP $this->zip->close(); } } else { header('Location: admin.php?page=layerslider&error=1&message=exportZipError'); die(); }
// Check for JSON } elseif(!empty($type['ext']) && $type['ext'] == 'json') {
// Get decoded file data $data = file_get_contents($archive); if($decoded = base64_decode($data, true)) { if(!$parsed = json_decode($decoded, true)) { $parsed = unserialize($decoded); }
// Since v5.1.1 } else { $parsed = array(json_decode($data, true)); }
// Iterate over imported sliders if(is_array($parsed)) {
// Import sliders foreach($parsed as $item) {
// Increment the slider counter $this->sliderCount++;
// Fix for export issue in v4.6.4 if(is_string($item)) { $item = json_decode($item, true); }
$this->lastImportId = LS_Sliders::add($item['properties']['title'], $item); } } }
// Return false otherwise return false; }
public function unpack($archive) {
// Get uploads folder $uploads = wp_upload_dir();
// Check if /uploads dir is writable if(is_writable($uploads['basedir'])) {
// Get target folders $this->uploadsDir = $uploads['basedir']; $this->targetDir = $targetDir = $uploads['basedir'].'/layerslider'; $this->targetURL = $uploads['baseurl'].'/layerslider'; $this->tmpDir = $tmpDir = $uploads['basedir'].'/layerslider/tmp';
// Create necessary folders under /uploads if( ! file_exists( $targetDir ) ) { mkdir($targetDir, 0755); } if( ! file_exists( $targetDir ) ) { mkdir($targetDir, 0755); }
// Unpack archive if($this->zip->extractTo($tmpDir)) { return true; } }
return false; }
public function uploadMedia($dir = null) {
// Check provided data if(empty($dir) || !is_string($dir) || !file_exists($dir.'/uploads')) { return false; }
// Create folder if it isn't exists already $targetDir = $this->targetDir . '/' . basename($dir); if( ! file_exists( $targetDir ) ) { mkdir($targetDir, 0755); }
// Include image.php for media library upload require_once(ABSPATH.'wp-admin/includes/image.php');
// Iterate through directory foreach(glob($dir.'/uploads/*') as $filePath) {
$fileName = sanitize_file_name(basename($filePath)); $targetFile = $targetDir.'/'.$fileName; $targetURL = $this->targetURL.'/'.basename($dir).'/'.$fileName;
// Validate media $filetype = wp_check_filetype($fileName, null); if(!empty($filetype['ext']) && $filetype['ext'] != 'php') {
// New upload if( ! $attach_id = $this->attachIDForURL( $targetURL, $targetFile ) ) {
// Move item to place rename($filePath, $targetFile);
// Upload to media library $attachment = array( 'guid' => $targetFile, 'post_mime_type' => $filetype['type'], 'post_title' => preg_replace( '/\.[^.]+$/', '', $fileName), 'post_content' => '', 'post_status' => 'inherit' );
$attach_id = wp_insert_attachment($attachment, $targetFile, 37); if($attach_data = wp_generate_attachment_metadata($attach_id, $targetFile)) { wp_update_attachment_metadata($attach_id, $attach_data); }
$this->imported[$fileName] = array( 'id' => $attach_id, 'url' => $this->targetURL.'/'.basename($dir).'/'.$fileName );
// Already uploaded } else {
$this->imported[$fileName] = array( 'id' => $attach_id, 'url' => $targetURL ); } } }
return true; }
public function deleteDir($dir) { if(!file_exists($dir)) return true; if(!is_dir($dir)) return unlink($dir); foreach(scandir($dir) as $item) { if($item == '.' || $item == '..') continue; if(!$this->deleteDir($dir.DIRECTORY_SEPARATOR.$item)) return false; } return rmdir($dir); }
public function addSlider( $file, $groupId = NULL ) {
// Increment the slider counter $this->sliderCount++;
// Get slider data and title $data = json_decode(file_get_contents($file), true); $title = $data['properties']['title']; $slug = !empty($data['properties']['slug']) ? $data['properties']['slug'] : '';
// Import Google Fonts used in slider if( isset( $data['googlefonts'] ) ) { $this->addGoogleFonts( $data ); unset( $data['googlefonts'] ); }
// Slider Preview if( ! empty($data['meta']) && ! empty($data['meta']['preview']) ) { $data['meta']['previewId'] = $this->attachIDForImage($data['meta']['preview']); $data['meta']['preview'] = $this->attachURLForImage($data['meta']['preview']); }
// Slider settings if(!empty($data['properties']['backgroundimage'])) { $data['properties']['backgroundimageId'] = $this->attachIDForImage($data['properties']['backgroundimage']); $data['properties']['backgroundimage'] = $this->attachURLForImage($data['properties']['backgroundimage']); }
if(!empty($data['properties']['yourlogo'])) { $data['properties']['yourlogoId'] = $this->attachIDForImage($data['properties']['yourlogo']); $data['properties']['yourlogo'] = $this->attachURLForImage($data['properties']['yourlogo']); }
// Slides if(!empty($data['layers']) && is_array($data['layers'])) { foreach($data['layers'] as &$slide) {
if(!empty($slide['properties']['background'])) { $slide['properties']['backgroundId'] = $this->attachIDForImage($slide['properties']['background']); $slide['properties']['background'] = $this->attachURLForImage($slide['properties']['background']); }
if(!empty($slide['properties']['thumbnail'])) { $slide['properties']['thumbnailId'] = $this->attachIDForImage($slide['properties']['thumbnail']); $slide['properties']['thumbnail'] = $this->attachURLForImage($slide['properties']['thumbnail']); }
// Layers if(!empty($slide['sublayers']) && is_array($slide['sublayers'])) { foreach($slide['sublayers'] as &$layer) {
if( ! empty($layer['image']) ) { $layer['imageId'] = $this->attachIDForImage($layer['image']); $layer['image'] = $this->attachURLForImage($layer['image']); }
if( ! empty($layer['poster']) ) { $layer['posterId'] = $this->attachIDForImage($layer['poster']); $layer['poster'] = $this->attachURLForImage($layer['poster']); }
if( ! empty($layer['layerBackground']) ) { $layer['layerBackgroundId'] = $this->attachIDForImage($layer['layerBackground']); $layer['layerBackground'] = $this->attachURLForImage($layer['layerBackground']); } }} }}
// Add slider return LS_Sliders::add( $title, $data, $slug, $groupId ); }
public function addGoogleFonts( $data ) {
// Get current Google Fonts $googleFonts = get_option('ls-google-fonts', array()); $fontNames = array();
// Gather used font names foreach( $googleFonts as $item ) { $font = explode(':', $item['param']); $fontNames[ $font[0] ] = $item; }
// Merge google fonts foreach( $data['googlefonts'] as $font ) {
// If no font-weight is specified, default to regular 400 // since Google Fonts do exactly this as well. if( strpos(trim($font['param']), ':') === false ) { $font['param'] .= ':regular'; }
list($family, $weights) = explode(':', $font['param']);
// New font, just add if( ! isset($fontNames[$family]) ) { $fontNames[$family] = $font;
// Existing font, merge variants } else {
$w = array();
foreach( explode(',', $weights) as $weight ) { $w[$weight] = true; }
// If no font-weight is specified, default to regular 400 // since Google Fonts do exactly this as well. if( strpos(trim($fontNames[ $family ]['param']), ':') === false ) { $fontNames[ $family ]['param'] .= ':regular'; }
list($family, $weights) = explode(':', $fontNames[ $family ]['param']); foreach( explode(',', $weights) as $weight ) { $w[$weight] = true; }
$fontNames[ $family ] = $font; $fontNames[ $family ]['param'] = $family .':'. implode(',', array_keys($w)); } }
// Update Google Fonts $googleFonts = array(); foreach( $fontNames as $font ) { $googleFonts[] = $font; }
update_option('ls-google-fonts', $googleFonts); }
public function attachURLForImage($file = '') {
if( isset($this->imported[ basename($file) ]) ) { return $this->imported[ basename($file) ]['url']; }
return $file; }
public function attachIDForImage($file = '') {
if( isset($this->imported[ basename($file) ]) ) { return $this->imported[ basename($file) ]['id']; }
return ''; }
public function attachIDForURL( $url, $path ) {
// Attempt to retrieve the post ID from the built in // attachment_url_to_postid() WP function when available. if( function_exists('attachment_url_to_postid') ) { if( $attachID = attachment_url_to_postid( $url ) ) { return $attachID; } }
global $wpdb;
if( empty( $this->uploadsDir ) ) { $uploads = wp_upload_dir(); $this->uploadsDir = trailingslashit($uploads['basedir']); }
$imgPath = explode( parse_url( $this->uploadsDir, PHP_URL_PATH ), $path ); $attachs = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}posts WHERE guid RLIKE %s;", $imgPath[1] ) );
return ! empty( $attachs[0] ) ? $attachs[0] : 0; }
public function cleanup() { $this->deleteDir($this->tmpDir); } } ?>
|