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
434
435
436
437
438
439
440
441
442
|
<?php
$GLOBALS['lsLoadPlugins'] = array(); $GLOBALS['lsLoadFonts'] = array();
function layerslider( $id = 0, $filters = '', $options = array() ) { echo LS_Shortcode::handleShortcode( array_merge( array('id' => $id, 'filters' => $filters), $options) ); }
class LS_Shortcode {
// List of already included sliders on page. // Using to identify duplicates and give them // a unique slider ID to avoid issues with caching. public static $slidersOnPage = array();
private function __construct() {}
/** * Registers the LayerSlider shortcode. * * @since 5.3.3 * @access public * @return void */
public static function registerShortcode() { if(!shortcode_exists('layerslider')) { add_shortcode('layerslider', array(__CLASS__, 'handleShortcode')); } }
/** * Handles the shortcode workflow to display the * appropriate content. * * @since 5.3.3 * @access public * @param array $atts Shortcode attributes * @return bool True on successful validation, false otherwise */
public static function handleShortcode( $atts = array() ) {
if(self::validateFilters($atts)) {
$output = ''; $item = self::validateShortcode( $atts );
// Show error messages (if any) if( ! empty( $item['error'] ) ) {
// Bail out early if the visitor has no permission to see error messages if( ! current_user_can(get_option('layerslider_custom_capability', 'manage_options')) ) { return ''; }
// Prevent showing errors for Popups if( ! empty($atts['popup']) || ! empty( $item['data']['flag_popup'] ) ) { return ''; }
$output .= $item['error']; }
if( $item['data'] ) { $output .= self::processShortcode( $item['data'], $atts ); }
return $output; } }
/** * Validates the provided shortcode filters (if any). * * @since 5.3.3 * @access public * @param array $atts Shortcode attributes * @return bool True on successful validation, false otherwise */
public static function validateFilters($atts = array()) {
// Bail out early and pass the validation // if there aren't filters provided if(empty($atts['filters'])) { return true; }
// Gather data needed for filters $pages = explode(',', $atts['filters']); $currSlug = basename(get_permalink()); $currPageID = (string) get_the_ID();
foreach($pages as $page) {
if(($page == 'homepage' && is_front_page()) || $currPageID == $page || $currSlug == $page || in_category($page) ) { return true; } }
// No filters matched, // return false return false; }
/** * Validates the shortcode parameters and checks * the references slider. * * @since 5.3.3 * @access public * @param array $atts Shortcode attributes * @return bool True on successful validation, false otherwise */
public static function validateShortcode($atts = array()) {
$error = false; $slider = false;
// Has ID attribute if( ! empty( $atts['id'] ) ) {
$sliderID = $atts['id']; $slider = self::cacheForSlider( $sliderID );
if( empty( $slider ) ) { $slider = LS_Sliders::find( $sliderID );
// Second attempt to retrieve cache (if any) // based on the actual slider ID instead of alias if( $cache = self::cacheForSlider( $slider['id'] ) ) { $slider = $cache; } }
// ERROR: No slider with ID was found if( empty( $slider ) ) { $error = self::generateErrorMarkup( __('The slider cannot be found', 'LayerSlider'), null );
// ERROR: The slider is not published } elseif( (int)$slider['flag_hidden'] ) { $error = self::generateErrorMarkup( __('Unpublished slider', 'LayerSlider'), sprintf(__('The slider you’ve inserted here is yet to be published, thus it won’t be displayed to your visitors. You can publish it by enabling the appropriate option in %sSlider Settings -> Publish%s. ', 'LayerSlider'), '<a href="'.admin_url('admin.php?page=layerslider&action=edit&id='.(int)$slider['id'].'&showsettings=1#publish').'" target="_blank">', '</a>.'), 'dashicons-hidden' );
// ERROR: The slider was removed } elseif( (int)$slider['flag_deleted'] ) { $error = self::generateErrorMarkup( __('Removed slider', 'LayerSlider'), sprintf(__('The slider you’ve inserted here was removed in the meantime, thus it won’t be displayed to your visitors. This slider is still recoverable on the admin interface. You can enable listing removed sliders with the Screen Options -> Removed sliders option, then choose the Restore option for the corresponding item to reinstate this slider, or just click %shere%s.', 'LayerSlider'), '<a href="'.admin_url('admin.php').wp_nonce_url('?page=layerslider&action=restore&id='.$slider['id'].'&ref='.urlencode(get_permalink()), 'restore_'.$slider['id']).'">', '</a>'), 'dashicons-trash' );
// ERROR: Scheduled sliders } else {
$tz = date_default_timezone_get(); $siteTz = get_option('timezone_string', 'UTC'); $siteTz = $siteTz ? $siteTz : 'UTC'; date_default_timezone_set( $siteTz );
if( ! empty($slider['schedule_start']) && (int) $slider['schedule_start'] > time() ) { $error = self::generateErrorMarkup( sprintf(__('This slider is scheduled to display on %s', 'LayerSlider'), date_i18n(get_option('date_format').' @ '.get_option('time_format'), (int) $slider['schedule_start']) ), '', 'dashicons-calendar-alt', 'scheduled' ); } elseif( ! empty($slider['schedule_end']) && (int) $slider['schedule_end'] < time() ) { $error = self::generateErrorMarkup( sprintf(__('This slider was scheduled to hide on %s ','LayerSlider'), date_i18n(get_option('date_format').' @ '.get_option('time_format'), (int) $slider['schedule_end']) ), sprintf(__('Due to scheduling, this slider is no longer visible to your visitors. If you wish to reinstate this slider, just remove the schedule in %sSlider Settings -> Publish%s.', 'LayerSlider'), '<a href="'.admin_url('admin.php?page=layerslider&action=edit&id='.(int)$slider['id'].'&showsettings=1#publish').'" target="_blank">', '</a>'), 'dashicons-no-alt', 'dead' ); }
date_default_timezone_set( $tz ); }
// ERROR: No slider ID was provided } else { $error = self::generateErrorMarkup(); }
return array( 'error' => $error, 'data' => $slider ); }
public static function cacheForSlider( $sliderID ) {
// Exclude administrators to avoid serving a copy // where notifications and other items may not be present. if( current_user_can( get_option('layerslider_custom_capability', 'manage_options') ) ) { return false; }
// Attempt to retrieve the pre-generated markup // set via the Transients API if caching is enabled. if( get_option('ls_use_cache', true) ) {
if( $slider = get_transient('ls-slider-data-'.$sliderID) ) { $slider['id'] = $sliderID; $slider['_cached'] = true;
return $slider; } }
return false; }
public static function processShortcode( $slider, $embed = array() ) {
// Slider ID $sID = 'layerslider_'.$slider['id'];
// Include init code in the footer? $condsc = get_option('ls_conditional_script_loading', false) ? true : false; $footer = get_option('ls_include_at_footer', false) ? true : false; $footer = $condsc ? true : $footer;
// Check for the '_cached' key in data, // indicating that it's a pre-generated // slider markup retrieved via Transients if( ! empty( $slider['_cached'] ) ) { $output = $slider;
// No cached copy, generate new markup. // Make sure to include some database related // data, since we rely on those to display // notifications for admins. } else {
$output = self::generateSliderMarkup( $slider, $embed );
$output['id'] = $slider['id']; $output['schedule_start'] = $slider['schedule_start']; $output['schedule_end'] = $slider['schedule_end']; $output['flag_hidden'] = $slider['flag_hidden']; $output['flag_deleted'] = $slider['flag_deleted'];
// Save generated markup if caching is enabled, except for // administrators to avoid serving a copy where notifications // and other items may be present. $capability = get_option('layerslider_custom_capability', 'manage_options'); $permission = current_user_can( $capability ); if( get_option('ls_use_cache', true) && ! $permission ) { set_transient('ls-slider-data-'.$slider['id'], $output, HOUR_IN_SECONDS * 6); } }
// Replace slider ID to avoid issues with enabled caching when // adding the same slider to a page in multiple times if(array_key_exists($slider['id'], self::$slidersOnPage)) { $sliderCount = ++self::$slidersOnPage[ $slider['id'] ]; $output['init'] = str_replace($sID, $sID.'_'.$sliderCount, $output['init']); $output['container'] = str_replace($sID, $sID.'_'.$sliderCount, $output['container']);
$sID = $sID.'_'.$sliderCount;
} else {
// Add current slider ID to identify duplicates later on // and give them a unique slider ID to avoid issues with caching. self::$slidersOnPage[ $slider['id'] ] = 1; }
// Override firstSlide if it is specified in embed params if( ! empty( $embed['firstslide'] ) ) { $output['init'] = str_replace('[firstSlide]', $embed['firstslide'], $output['init']); }
// Filter to override the printed JavaScript init code if( has_filter('layerslider_slider_init') ) { $output['init'] = apply_filters('layerslider_slider_init', $output['init'], $slider, $sID ); }
// Unify the whole markup after any potential string replacement $output['markup'] = $output['container'].$output['markup'];
// Filter to override the printed HTML markup if( has_filter('layerslider_slider_markup') ) { $output['markup'] = apply_filters('layerslider_slider_markup', $output['markup'], $slider, $sID); }
// Plugins if( ! empty( $output['plugins'] ) ) { $GLOBALS['lsLoadPlugins'] = array_merge($GLOBALS['lsLoadPlugins'], $output['plugins']); }
// Fonts if( ! empty( $output['fonts'] ) ) { $GLOBALS['lsLoadFonts'] = array_merge($GLOBALS['lsLoadFonts'], $output['fonts']); }
if( $footer ) { $GLOBALS['lsSliderInit'][] = $output['init']; return $output['markup']; } else { $output['init'] = '<script type="text/javascript">'.$output['init'].'</script>'; return $output['init'].$output['markup']; } }
public static function generateSliderMarkup( $slider = null, $embed = array() ) {
// Bail out early if no params received or using Popup on unactivated sites if( ! $slider || ( (int)$slider['flag_popup'] && ! LS_Config::isActivatedSite() ) ) { return array('init' => '', 'container' => '', 'markup' => ''); }
// Slider and markup data $id = $slider['id']; $sliderID = 'layerslider_'.$id; $slides = $slider['data'];
// Store generated output $lsInit = array(); $lsContainer = array(); $lsMarkup = array(); $lsPlugins = array(); $lsFonts = array();
// Include slider file if(is_array($slides)) {
// Get phpQuery if( ! defined('LS_phpQuery') ) { libxml_use_internal_errors(true); include LS_ROOT_PATH.'/helpers/phpQuery.php'; }
$GLOBALS['lsPremiumNotice'] = array();
include LS_ROOT_PATH.'/config/defaults.php'; include LS_ROOT_PATH.'/includes/slider_markup_setup.php'; include LS_ROOT_PATH.'/includes/slider_markup_html.php'; include LS_ROOT_PATH.'/includes/slider_markup_init.php';
// Admin notice when using premium features on non-activated sites if( ! empty( $GLOBALS['lsPremiumNotice'] ) ) { array_unshift($lsContainer, self::generateErrorMarkup( __('Premium features is available for preview purposes only.', 'LayerSlider'), sprintf(__('We’ve detected that you’re using premium features in this slider, but you have not yet activated your copy of LayerSlider. Premium features in your sliders will not be available for your visitors without activation. %sClick here to learn more%s. Detected features: %s', 'LayerSlider'), '<a href="https://layerslider.kreaturamedia.com/documentation/#activation" target="_blank">', '</a>', implode(', ', $GLOBALS['lsPremiumNotice'])), 'dashicons-star-filled', 'info' )); }
$lsInit = implode('', $lsInit); $lsContainer = implode('', $lsContainer); $lsMarkup = implode('', $lsMarkup); }
// Concatenate output if( get_option('ls_concatenate_output', false) ) { $lsInit = trim(preg_replace('/\s+/u', ' ', $lsInit)); $lsContainer = trim(preg_replace('/\s+/u', ' ', $lsContainer)); $lsMarkup = trim(preg_replace('/\s+/u', ' ', $lsMarkup)); }
// Bug fix in v5.4.0: Use self closing tag for <source> $lsMarkup = str_replace('></source>', ' />', $lsMarkup);
// Return formatted data return array( 'init' => $lsInit, 'container' => $lsContainer, 'markup' => $lsMarkup, 'plugins' => array_unique( $lsPlugins ), 'fonts' => array_unique( $lsFonts ) ); }
public static function generateErrorMarkup( $title = null, $description = null, $logo = 'dashicons-warning', $customClass = '' ) {
if( ! $title ) { $title = __('LayerSlider encountered a problem while it tried to show your slider.', 'LayerSlider'); }
if( is_null($description) ) { $description = __('Please make sure that you’ve used the right shortcode or method to insert the slider, and check if the corresponding slider exists and it wasn’t deleted previously.', 'LayerSlider'); }
if( $description ) { $description .= '<br><br>'; }
$logo = $logo ? '<i class="lswp-notification-logo dashicons '.$logo.'"></i>' : ''; $notice = __('Only you and other administrators can see this to take appropriate actions if necessary.', 'LayerSlider');
$classes = array('error', 'info', 'scheduled', 'dead'); if( ! empty($customClass) && ! in_array($customClass, $classes) ) { $customClass = ''; }
return '<div class="clearfix lswp-notification '.$customClass.'"> '.$logo.' <strong>'.$title.'</strong> <span>'.$description.'</span> <small> <i class="dashicons dashicons-lock"></i> '.$notice.' </small> </div>'; } }
|