PageRenderTime 29ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/LayerSlider/wp/shortcodes.php

https://gitlab.com/webkod3r/tripolis
PHP | 242 lines | 118 code | 60 blank | 64 comment | 30 complexity | 2b4dc35ad3e1acd00b176720b51fb44f MD5 | raw file
  1. <?php
  2. function layerslider($id = 0, $filters = '') {
  3. echo LS_Shortcode::handleShortcode(array('id' => $id, 'filters' => $filters));
  4. }
  5. class LS_Shortcode {
  6. // List of already included sliders on page.
  7. // Using to identify duplicates and give them
  8. // a unique slider ID to avoid issues with caching.
  9. public static $slidersOnPage = array();
  10. private function __contruct() {}
  11. /**
  12. * Registers the LayerSlider shortcode.
  13. *
  14. * @since 5.3.3
  15. * @access public
  16. * @return void
  17. */
  18. public static function registerShortcode() {
  19. if(!shortcode_exists('layerslider')) {
  20. add_shortcode('layerslider', array(__CLASS__, 'handleShortcode'));
  21. }
  22. }
  23. /**
  24. * Handles the shortcode workflow to display the
  25. * appropriate content.
  26. *
  27. * @since 5.3.3
  28. * @access public
  29. * @param array $atts Shortcode attributes
  30. * @return bool True on successful validation, false otherwise
  31. */
  32. public static function handleShortcode($atts = array()) {
  33. if(self::validateFilters($atts)) {
  34. if($slider = self::validateShortcode($atts)) {
  35. return self::processShortcode($slider);
  36. } else {
  37. $data = '<div style="margin: 10px auto; padding: 10px; border: 2px solid red; border-radius: 5px;">';
  38. $data.= '<strong style="display: block; font-size: 18px;">'.__('LayerSlider encountered a problem while it tried to show your slider.', 'LayerSlider').'</strong>';
  39. $data.= __("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");
  40. $data.= '</div>';
  41. return $data;
  42. }
  43. }
  44. }
  45. /**
  46. * Validates the provided shortcode filters (if any).
  47. *
  48. * @since 5.3.3
  49. * @access public
  50. * @param array $atts Shortcode attributes
  51. * @return bool True on successful validation, false otherwise
  52. */
  53. public static function validateFilters($atts = array()) {
  54. // Bail out early and pass the validation
  55. // if there aren't filters provided
  56. if(empty($atts['filters'])) {
  57. return true;
  58. }
  59. // Gather data needed for filters
  60. $pages = explode(',', $atts['filters']);
  61. $currSlug = basename(get_permalink());
  62. $currPageID = (string) get_the_ID();
  63. foreach($pages as $page) {
  64. if(($page == 'homepage' && is_front_page())
  65. || $currPageID == $page
  66. || $currSlug == $page
  67. || in_category($page)
  68. ) {
  69. return true;
  70. }
  71. }
  72. // No filters matched,
  73. // return false
  74. return false;
  75. }
  76. /**
  77. * Validates the shortcode parameters and checks
  78. * the references slider.
  79. *
  80. * @since 5.3.3
  81. * @access public
  82. * @param array $atts Shortcode attributes
  83. * @return bool True on successful validation, false otherwise
  84. */
  85. public static function validateShortcode($atts = array()) {
  86. // Has ID attribute
  87. if(!empty($atts['id'])) {
  88. // Attempt to retrieve the pre-generated markup
  89. // set via the Transients API
  90. if(get_option('ls_use_cache', true)) {
  91. if($markup = get_transient('ls-slider-data-'.intval($atts['id']))) {
  92. $markup['id'] = intval($atts['id']);
  93. $markup['_cached'] = true;
  94. return $markup;
  95. }
  96. }
  97. // Slider exists and isn't deleted
  98. $slider = LS_Sliders::find($atts['id']);
  99. if(!empty($slider) || $slider['flag_deleted'] != '1') {
  100. return $slider;
  101. }
  102. }
  103. return false;
  104. }
  105. public static function processShortcode($slider) {
  106. // Slider ID
  107. $sID = 'layerslider_'.$slider['id'];
  108. // Include init code in the footer?
  109. $condsc = get_option('ls_conditional_script_loading', false) ? true : false;
  110. $footer = get_option('ls_include_at_footer', false) ? true : false;
  111. $footer = $condsc ? true : $footer;
  112. // Check if the returned data is a string,
  113. // indicating that it's a pre-generated
  114. // slider markup retrieved via Transients
  115. if(!empty($slider['_cached'])) { $output = $slider;}
  116. else {
  117. $output = self::generateSliderMarkup($slider);
  118. set_transient('ls-slider-data-'.$slider['id'], $output, HOUR_IN_SECONDS * 6);
  119. }
  120. // Replace slider ID to avoid issues with enabled caching when
  121. // adding the same slider to a page in multiple times
  122. if(array_key_exists($slider['id'], self::$slidersOnPage)) {
  123. $sliderCount = ++self::$slidersOnPage[ $slider['id'] ];
  124. $output['init'] = str_replace($sID, $sID.'_'.$sliderCount, $output['init']);
  125. $output['container'] = str_replace($sID, $sID.'_'.$sliderCount, $output['container']);
  126. } else {
  127. // Add current slider ID to identify duplicates later on
  128. // and give them a unique slider ID to avoid issues with caching.
  129. self::$slidersOnPage[ $slider['id'] ] = 1;
  130. }
  131. // Unify the whole markup after any potential string replacement
  132. $output['markup'] = $output['container'].$output['markup'];
  133. // Filter to override the printed HTML markup
  134. if(has_filter('layerslider_slider_markup')) {
  135. $lsMarkup = apply_filters('layerslider_slider_markup', $lsMarkup);
  136. }
  137. if($footer) {
  138. $GLOBALS['lsSliderInit'][] = $output['init'];
  139. return $output['markup'];
  140. } else {
  141. return $output['init'].$output['markup'];
  142. }
  143. }
  144. public static function generateSliderMarkup($slider = null) {
  145. // Bail out early if no params received
  146. if(!$slider) { return array('init' => '', 'container' => '', 'markup' => ''); }
  147. // Slider and markup data
  148. $id = $slider['id'];
  149. $sliderID = 'layerslider_'.$id;
  150. $slides = $slider['data'];
  151. // Store generated output
  152. $lsInit = ''; $lsContainer = ''; $lsMarkup = '';
  153. // Include slider file
  154. if(is_array($slides)) {
  155. // Get phpQuery
  156. if(!class_exists('phpQuery')) {
  157. libxml_use_internal_errors(true);
  158. include LS_ROOT_PATH.'/helpers/phpQuery.php';
  159. }
  160. include LS_ROOT_PATH.'/config/defaults.php';
  161. include LS_ROOT_PATH.'/includes/slider_markup_init.php';
  162. include LS_ROOT_PATH.'/includes/slider_markup_html.php';
  163. $lsInit = implode('', $lsInit);
  164. $lsContainer = implode('', $lsContainer);
  165. $lsMarkup = implode('', $lsMarkup);
  166. }
  167. // Concatenate output
  168. if(get_option('ls_concatenate_output', false)) {
  169. $lsInit = trim(preg_replace('/\s+/u', ' ', $lsInit));
  170. $lsContainer = trim(preg_replace('/\s+/u', ' ', $lsContainer));
  171. $lsMarkup = trim(preg_replace('/\s+/u', ' ', $lsMarkup));
  172. }
  173. // Bug fix in v5.4.0: Use self closing tag for <source>
  174. $lsMarkup = str_replace('></source>', ' />', $lsMarkup);
  175. // Return formatted data
  176. return array(
  177. 'init' => $lsInit,
  178. 'container' => $lsContainer,
  179. 'markup' => $lsMarkup
  180. );
  181. }
  182. }