Facebook
From Innocent Treeshrew, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 124
  1. <?php
  2.  
  3. // Prevent direct file access
  4. defined( 'LS_ROOT_FILE' ) || exit;
  5.  
  6. $GLOBALS['lsLoadPlugins']       = array();
  7. $GLOBALS['lsLoadFonts']         = array();
  8.  
  9. function layerslider( $id = 0, $filters = '', $options = array() ) {
  10.  
  11.         $attrs = array_merge(
  12.                 array(
  13.                         'id' => $id,
  14.                         'filters' => $filters
  15.                 ),
  16.                 $options
  17.         );
  18.  
  19.         echo LS_Shortcode::handleShortcode( $attrs );
  20. }
  21.  
  22.  
  23. class LS_Shortcode {
  24.  
  25.         // List of already included sliders on page.
  26.         // Using to identify duplicates and give them
  27.         // a unique slider ID to avoid issues with caching.
  28.         public static $slidersOnPage = array();
  29.  
  30.         private function __construct() {}
  31.  
  32.  
  33.         /**
  34.          * Registers the LayerSlider shortcode.
  35.          *
  36.          * @since 5.3.3
  37.          * @access public
  38.          * @return void
  39.          */
  40.  
  41.         public static function registerShortcode() {
  42.                 if(!shortcode_exists('layerslider')) {
  43.                         add_shortcode('layerslider', array(__CLASS__, 'handleShortcode'));
  44.                 }
  45.         }
  46.  
  47.  
  48.  
  49.  
  50.         /**
  51.          * Handles the shortcode workflow to display the
  52.          * appropriate content.
  53.          *
  54.          * @since 5.3.3
  55.          * @access public
  56.          * @param array $atts Shortcode attributes
  57.          * @return bool True on successful validation, false otherwise
  58.          */
  59.  
  60.         public static function handleShortcode( $atts = array() ) {
  61.  
  62.                 if(self::validateFilters($atts)) {
  63.  
  64.                         $output = '';
  65.                         $item = self::validateShortcode( $atts );
  66.  
  67.                         // Show error messages (if any)
  68.                         if( ! empty( $item['error'] ) ) {
  69.  
  70.                                 // Bail out early if the visitor has no permission to see error messages
  71.                                 if( ! current_user_can(get_option('layerslider_custom_capability', 'manage_options')) ) {
  72.                                         return '';
  73.                                 }
  74.  
  75.                                 // Prevent showing errors for Popups
  76.                                 if( ! empty($atts['popup']) || ! empty( $item['data']['flag_popup'] ) ) {
  77.                                         return '';
  78.                                 }
  79.  
  80.  
  81.                                 $output .= $item['error'];
  82.                         }
  83.  
  84.  
  85.                         if( $item['data'] ) {
  86.                                 $output .= self::processShortcode( $item['data'], $atts );
  87.                         }
  88.  
  89.                         return $output;
  90.                 }
  91.         }
  92.  
  93.  
  94.  
  95.  
  96.         /**
  97.          * Validates the provided shortcode filters (if any).
  98.          *
  99.          * @since 5.3.3
  100.          * @access public
  101.          * @param array $atts Shortcode attributes
  102.          * @return bool True on successful validation, false otherwise
  103.          */
  104.  
  105.         public static function validateFilters($atts = array()) {
  106.  
  107.                 // Bail out early and pass the validation
  108.                 // if there aren't filters provided
  109.                 if(empty($atts['filters'])) {
  110.                         return true;
  111.                 }
  112.  
  113.                 // Gather data needed for filters
  114.                 $pages = explode(',', $atts['filters']);
  115.                 $currSlug = basename(get_permalink());
  116.                 $currPageID = (string) get_the_ID();
  117.  
  118.                 foreach($pages as $page) {
  119.  
  120.                         if(($page == 'homepage' && is_front_page())
  121.                                 || $currPageID == $page
  122.                                 || $currSlug == $page
  123.                                 || in_category($page)
  124.                         ) {
  125.                                 return true;
  126.                         }
  127.                 }
  128.  
  129.                 // No filters matched,
  130.                 // return false
  131.                 return false;
  132.         }
  133.  
  134.  
  135.  
  136.         /**
  137.          * Validates the shortcode parameters and checks
  138.          * the references slider.
  139.          *
  140.          * @since 5.3.3
  141.          * @access public
  142.          * @param array $atts Shortcode attributes
  143.          * @return bool True on successful validation, false otherwise
  144.          */
  145.  
  146.         public static function validateShortcode($atts = array()) {
  147.  
  148.                 $error = false;
  149.                 $slider = false;
  150.  
  151.                 // Has ID attribute
  152.                 if( ! empty( $atts['id'] ) ) {
  153.  
  154.                         $sliderID       = $atts['id'];
  155.                         $slider         = self::cacheForSlider( $sliderID );
  156.  
  157.                         if( empty( $slider ) ) {
  158.                                 $slider = LS_Sliders::find( $sliderID );
  159.  
  160.                                 // Second attempt to retrieve cache (if any)
  161.                                 // based on the actual slider ID instead of alias
  162.                                 if( $cache = self::cacheForSlider( $slider['id'] ) ) {
  163.                                         $slider = $cache;
  164.                                 }
  165.                         }
  166.  
  167.                         // ERROR: No slider with ID was found
  168.                         if( empty( $slider ) ) {
  169.                                 $error = self::generateErrorMarkup(
  170.                                         __('The slider cannot be found', 'LayerSlider'),
  171.                                         null
  172.                                 );
  173.  
  174.                         // ERROR: The slider is not published
  175.                         } elseif( (int)$slider['flag_hidden'] ) {
  176.                                 $error = self::generateErrorMarkup(
  177.                                         __('Unpublished slider', 'LayerSlider'),
  178.                                         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>.'),
  179.                                         'dashicons-hidden'
  180.                                 );
  181.  
  182.                         // ERROR: The slider was removed
  183.                         } elseif( (int)$slider['flag_deleted'] ) {
  184.                                 $error = self::generateErrorMarkup(
  185.                                         __('Removed slider', 'LayerSlider'),
  186.                                         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="'.wp_nonce_url( admin_url('admin.php?page=layerslider&action=restore&id='.$slider['id'].'&ref='.urlencode(get_permalink()) ), 'restore_'.$slider['id']).'">', '</a>'),
  187.                                         'dashicons-trash'
  188.                                 );
  189.  
  190.                         // ERROR: Scheduled sliders
  191.                         } else {
  192.  
  193.                                 if( ! empty($slider['schedule_start']) && (int) $slider['schedule_start'] > time() ) {
  194.                                         $error = self::generateErrorMarkup(
  195.                                                 sprintf(__('This slider is scheduled to display on %s', 'LayerSlider'), ls_date(get_option('date_format') .' '. get_option('time_format'), $slider['schedule_start']) ),
  196.                                                 '', 'dashicons-calendar-alt', 'scheduled'
  197.                                         );
  198.                                 } elseif( ! empty($slider['schedule_end']) && (int) $slider['schedule_end'] < time() ) {
  199.                                         $error = self::generateErrorMarkup(
  200.                                                 sprintf(__('This slider was scheduled to hide on %s ','LayerSlider'), ls_date(get_option('date_format') .' '. get_option('time_format'), $slider['schedule_end']) ),
  201.                                                 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>'),
  202.                                                 'dashicons-no-alt', 'dead'
  203.                                         );
  204.                                 }
  205.  
  206.                         }
  207.  
  208.                 // ERROR: No slider ID was provided
  209.                 } else {
  210.                         $error = self::generateErrorMarkup();
  211.                 }
  212.  
  213.                 return array(
  214.                         'error' => $error,
  215.                         'data' => $slider
  216.                 );
  217.         }
  218.  
  219.  
  220.  
  221.         public static function cacheForSlider( $sliderID ) {
  222.  
  223.                 // Exclude administrators to avoid serving a copy
  224.                 // where notifications and other items may not be present.
  225.                 if( current_user_can( get_option('layerslider_custom_capability', 'manage_options') ) ) {
  226.                         return false;
  227.                 }
  228.  
  229.                 // Attempt to retrieve the pre-generated markup
  230.                 // set via the Transients API if caching is enabled.
  231.                 if( get_option('ls_use_cache', true) ) {
  232.  
  233.                         if( $slider = get_transient('ls-slider-data-'.$sliderID) ) {
  234.                                 $slider['id'] = $sliderID;
  235.                                 $slider['_cached'] = true;
  236.  
  237.                                 return $slider;
  238.                         }
  239.                 }
  240.  
  241.                 return false;
  242.         }
  243.  
  244.  
  245.         public static function getUniqueID( ) {
  246.                 return base_convert( rand( 1000000000, PHP_INT_MAX ), 10, 36 );
  247.         }
  248.  
  249.  
  250.         public static function processShortcode( $slider, $embed = array() ) {
  251.  
  252.                 // Slider ID
  253.                 $sID = 'layerslider_'.$slider['id'];
  254.                 $uID = $sID .'_'. self::getUniqueID();
  255.  
  256.                 // Include init code in the footer?
  257.                 $condsc = get_option( 'ls_conditional_script_loading', false );
  258.                 $condsc = apply_filters( 'ls_conditional_script_loading', $condsc );
  259.  
  260.                 $footer = get_option( 'ls_include_at_footer', false );
  261.                 $footer = apply_filters( 'ls_include_at_footer', $footer );
  262.  
  263.                 $footer = $condsc ? true : $footer;
  264.  
  265.                 // Check for the '_cached' key in data,
  266.                 // indicating that it's a pre-generated
  267.                 // slider markup retrieved via Transients
  268.                 if( ! empty( $slider['_cached'] ) ) {
  269.                         $output = $slider;
  270.  
  271.                 // No cached copy, generate new markup.
  272.                 // Make sure to include some database related
  273.                 // data, since we rely on those to display
  274.                 // notifications for admins.
  275.                 } else {
  276.  
  277.                         $output = self::generateSliderMarkup( $slider, $embed );
  278.  
  279.                         $output['id']                           = $slider['id'];
  280.                         $output['schedule_start']       = $slider['schedule_start'];
  281.                         $output['schedule_end']         = $slider['schedule_end'];
  282.                         $output['flag_hidden']          = $slider['flag_hidden'];
  283.                         $output['flag_deleted']         = $slider['flag_deleted'];
  284.  
  285.  
  286.                         // Save generated markup if caching is enabled, except for
  287.                         // administrators to avoid serving a copy where notifications
  288.                         // and other items may be present.
  289.                         $capability = get_option('layerslider_custom_capability', 'manage_options');
  290.                         $permission = current_user_can( $capability );
  291.                         if( get_option('ls_use_cache', true) && ! $permission ) {
  292.                                 set_transient('ls-slider-data-'.$slider['id'], $output, HOUR_IN_SECONDS * 6);
  293.                         }
  294.                 }
  295.  
  296.                 // Replace slider ID to avoid issues with enabled caching when
  297.                 // adding the same slider to a page in multiple times
  298.                 $output['init'] = str_replace( $sID, $uID, $output['init'] );
  299.                 $output['container'] = str_replace( $sID, $uID, $output['container'] );
  300.                 $sID = $uID;
  301.  
  302.                 // Override firstSlide if it is specified in embed params
  303.                 if( ! empty( $embed['firstslide'] ) ) {
  304.                         $output['init'] = str_replace('[firstSlide]', $embed['firstslide'], $output['init']);
  305.                 }
  306.  
  307.                 // Filter to override the printed JavaScript init code
  308.                 if( has_filter('layerslider_slider_init') ) {
  309.                         $output['init'] = apply_filters('layerslider_slider_init', $output['init'], $slider, $sID );
  310.                 }
  311.  
  312.                 // Unify the whole markup after any potential string replacement
  313.                 $output['markup'] = $output['container'].$output['markup'];
  314.  
  315.                 // Filter to override the printed HTML markup
  316.                 if( has_filter('layerslider_slider_markup') ) {
  317.                         $output['markup'] = apply_filters('layerslider_slider_markup', $output['markup'], $slider, $sID);
  318.                 }
  319.  
  320.                 // Plugins
  321.                 if( ! empty( $output['plugins'] ) ) {
  322.                         $GLOBALS['lsLoadPlugins'] = array_merge($GLOBALS['lsLoadPlugins'], $output['plugins']);
  323.                 }
  324.  
  325.                 // Fonts
  326.                 if( ! empty( $output['fonts'] ) ) {
  327.                         $GLOBALS['lsLoadFonts'] = array_merge($GLOBALS['lsLoadFonts'], $output['fonts']);
  328.                 }
  329.  
  330.  
  331.                 if( $footer ) {
  332.                         $GLOBALS['lsSliderInit'][] = $output['init'];
  333.                         return $output['markup'];
  334.                 } else {
  335.                         $output['init'] = '<script type="text/javascript">'.$output['init'].'</script>';
  336.                         return $output['init'].$output['markup'];
  337.                 }
  338.         }
  339.  
  340.  
  341.  
  342.         public static function generateSliderMarkup( $slider = null, $embed = array() ) {
  343.  
  344.                 // Bail out early if no params received or using Popup on unactivated sites
  345.                 if( ! $slider || ( (int)$slider['flag_popup'] && ! LS_Config::isActivatedSite() ) ) {
  346.                         return array('init' => '', 'container' => '', 'markup' => '');
  347.                 }
  348.  
  349.                 // Slider and markup data
  350.                 $id                     = $slider['id'];
  351.                 $sliderID               = 'layerslider_'.$id;
  352.                 $slides                 = $slider['data'];
  353.  
  354.                 // Store generated output
  355.                 $lsInit                 = array();
  356.                 $lsContainer    = array();
  357.                 $lsMarkup               = array();
  358.                 $lsPlugins              = array();
  359.                 $lsFonts                = array();
  360.  
  361.                 // Include slider file
  362.                 if(is_array($slides)) {
  363.  
  364.                         // Get phpQuery
  365.                         if( ! defined('LS_phpQuery') ) {
  366.                                 libxml_use_internal_errors(true);
  367.                                 include LS_ROOT_PATH.'/helpers/phpQuery.php';
  368.                         }
  369.  
  370.                         $GLOBALS['lsPremiumNotice'] = array();
  371.  
  372.                         include LS_ROOT_PATH.'/config/defaults.php';
  373.                         include LS_ROOT_PATH.'/includes/slider_markup_setup.php';
  374.                         include LS_ROOT_PATH.'/includes/slider_markup_html.php';
  375.                         include LS_ROOT_PATH.'/includes/slider_markup_init.php';
  376.  
  377.                         // Admin notice when using premium features on non-activated sites
  378.                         if( ! empty( $GLOBALS['lsPremiumNotice'] ) ) {
  379.                                 array_unshift($lsContainer, self::generateErrorMarkup(
  380.                                         __('Premium features is available for preview purposes only.', 'LayerSlider'),
  381.                                         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'])),
  382.                                         'dashicons-star-filled', 'info'
  383.                                 ));
  384.                         }
  385.  
  386.  
  387.  
  388.                         $lsInit                 = implode('', $lsInit);
  389.                         $lsContainer    = implode('', $lsContainer);
  390.                         $lsMarkup               = implode('', $lsMarkup);
  391.                 }
  392.  
  393.                 // Concatenate output
  394.                 if( get_option('ls_concatenate_output', false) ) {
  395.                         $lsInit = trim(preg_replace('/s+/u', ' ', $lsInit));
  396.                         $lsContainer = trim(preg_replace('/s+/u', ' ', $lsContainer));
  397.                         $lsMarkup = trim(preg_replace('/s+/u', ' ', $lsMarkup));
  398.                 }
  399.  
  400.                 // Bug fix in v5.4.0: Use self closing tag for <source>
  401.                 $lsMarkup = str_replace('></source>', ' />', $lsMarkup);
  402.  
  403.                 // Return formatted data
  404.                 return array(
  405.                         'init'          => $lsInit,
  406.                         'container' => $lsContainer,
  407.                         'markup'        => $lsMarkup,
  408.                         'plugins'       => array_unique( $lsPlugins ),
  409.                         'fonts'         => array_unique( $lsFonts )
  410.                 );
  411.         }
  412.  
  413.  
  414.         public static function generateErrorMarkup( $title = null, $description = null, $logo = 'dashicons-warning', $customClass = '' ) {
  415.  
  416.                 if( ! $title ) {
  417.                         $title = __('LayerSlider encountered a problem while it tried to show your slider.', 'LayerSlider');
  418.                 }
  419.  
  420.                 if( is_null($description) ) {
  421.                         $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');
  422.                 }
  423.  
  424.                 if( $description ) {
  425.                         $description .= '<br><br>';
  426.                 }
  427.  
  428.                 $logo = $logo ? '<i class="lswp-notification-logo dashicons '.$logo.'"></i>' : '';
  429.                 $notice = __('Only you and other administrators can see this to take appropriate actions if necessary.', 'LayerSlider');
  430.  
  431.                 $classes = array('error', 'info', 'scheduled', 'dead');
  432.                 if( ! empty($customClass) && ! in_array($customClass, $classes) ) {
  433.                         $customClass = '';
  434.                 }
  435.  
  436.  
  437.                 return '<div class="clearfix lswp-notification '.$customClass.'">
  438.                                         '.$logo.'
  439.                                         <strong>'.$title.'</strong>
  440.                                         <span>'.$description.'</span>
  441.                                         <small>
  442.                                                 <i class="dashicons dashicons-lock"></i>
  443.                                                 '.$notice.'
  444.                                         </small>
  445.                                 </div>';
  446.         }
  447. }