PluginProbe
LoftLoader / 2.3.2
LoftLoader v2.3.2
trunk 1.0.0 1.0.1 1.0.2 2.0.0 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.3 2.3.1 2.3.2 2.3.3 All 34 releases
loftloader / inc / class-loftloader-front.php

class-loftloader-front.php in LoftLoader 2.3.2, at inc/class-loftloader-front.php

328 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Main class for front display
4 *
5 * @package LoftLoader
6 * @link http://www.loftocean.com/
7 * @author Suihai Huang from Loft Ocean Team
8
9 * @since version 1.0
10 */
11
12 if ( ! class_exists( 'LoftLoader_Front' ) ) {
13 class LoftLoader_Front {
14 protected $defaults;
15 protected $site_header_loaded = false;
16 protected $site_footer_loaded = false;
17 protected $type; // Get the loader settings
18 public function __construct() {
19 $this->get_settings();
20 $this->init_cache();
21 if ( $this->loader_enabled() ) {
22 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
23 add_action( 'wp_head', array( $this, 'loader_custom_styles' ), 100 );
24 add_action( 'wp_footer', array( $this, 'load_inline_js' ), 99 );
25 add_filter( 'loftloader_modify_html', array( $this, 'show_loader_html' ) );
26 add_filter( 'body_class', array( $this, 'body_class' ) );
27 }
28 }
29 /**
30 * Init cache for outputing
31 */
32 public function init_cache() {
33 // Only for front view
34 if ( ! is_admin() ) {
35 add_action( 'wp_head', array( $this, 'start_cache' ) );
36 add_action( 'wp_footer', array( $this, 'modify_html' ), 99999 );
37 }
38 }
39 /**
40 * Start cache for outputing
41 */
42 public function start_cache() {
43 ob_start();
44 }
45 /**
46 * Will be called when flush cache
47 *
48 * @param string cached string
49 * @return string modified cached string
50 */
51 public function modify_html() {
52 $html = ob_get_clean();
53 if ( $this->site_header_loaded && $this->site_footer_loaded ) {
54 echo apply_filters( 'loftloader_modify_html', $html );
55 } else {
56 echo $html;
57 }
58 }
59 /**
60 * @description get the plugin settings
61 */
62 public function get_settings() {
63 global $loftloader_default_settings;
64 $this->defaults = $loftloader_default_settings;
65 do_action( 'loftloader_settings' );
66 $this->type = esc_attr( $this->get_loader_setting( 'loftloader_loader_type' ) );
67 add_action( 'wp_head', array( $this, 'set_header_loaded' ) );
68 add_action( 'wp_footer', array( $this, 'set_footer_loaded' ) );
69 }
70 /**
71 * @description enqueue the scripts and styles for front end
72 */
73 public function enqueue_scripts() {
74 $loadJSStyle = $this->get_loader_setting( 'loftloader_inline_js' );
75 if ( ! is_customize_preview() && ( 'inline' !== $loadJSStyle ) ) {
76 wp_enqueue_script( 'loftloader-lite-front-main', LOFTLOADER_URI . 'assets/js/loftloader.min.js', array(), LOFTLOADER_ASSET_VERSION, true );
77 }
78 wp_enqueue_style('loftloader-lite-animation', LOFTLOADER_URI . 'assets/css/loftloader.min.css', array(), LOFTLOADER_ASSET_VERSION);
79 }
80 /**
81 * Load inline JavaScript code if set
82 */
83 public function load_inline_js() {
84 $loadJSStyle = $this->get_loader_setting( 'loftloader_inline_js' );
85 if ( ( 'inline' === $loadJSStyle ) && ! is_customize_preview() ) { ?>
86 <script type="text/javascript">
87 ( function() {
88 function loftloader_finished() {
89 document.body.classList.add( 'loaded' );
90 }
91 var loader = document.getElementById( 'loftloader-wrapper' );
92 if ( loader ) {
93 window.addEventListener( 'load', function( e ) {
94 loftloader_finished();
95 } );
96 if ( loader.dataset && loader.dataset.showCloseTime ) {
97 var showCloseTime = parseInt( loader.dataset.showCloseTime, 10 ), maxLoadTime = false,
98 closeBtn = loader.getElementsByClassName( 'loader-close-button' );
99 if ( showCloseTime && closeBtn.length ) {
100 setTimeout( function() {
101 closeBtn[0].style.display = '';
102 }, showCloseTime );
103 closeBtn[0].addEventListener( 'click', function( e ) {
104 loftloader_finished();
105 } );
106 }
107 }
108 if ( loader.dataset.maxLoadTime ) {
109 maxLoadTime = loader.dataset.maxLoadTime;
110 maxLoadTime = parseInt( maxLoadTime, 10 );
111 if ( maxLoadTime ) {
112 setTimeout( function() {
113 loftloader_finished();
114 }, maxLoadTime );
115 }
116 }
117 }
118 } ) ();
119 </script> <?php
120 }
121 }
122 /**
123 * @description custom css for front end
124 */
125 public function loader_custom_styles() {
126 $color = esc_attr( $this->get_loader_setting( 'loftloader_loader_color' ) );
127 $bgColor = esc_attr( $this->get_loader_setting( 'loftloader_bg_color' ) );
128 $bgOpacity = intval( $this->get_loader_setting('loftloader_bg_opacity' ) ) / 100;
129
130 $styles = $this->generate_style(
131 'loftloader-lite-custom-bg-color',
132 '#loftloader-wrapper .loader-section {' . PHP_EOL . "\t" . 'background: ' . $bgColor . ';' . PHP_EOL . '}' . PHP_EOL
133 );
134 $styles .= $this->generate_style(
135 'loftloader-lite-custom-bg-opacity',
136 '#loftloader-wrapper .loader-section {' . PHP_EOL . "\t" . 'opacity: ' . $bgOpacity . ';' . PHP_EOL . '}' . PHP_EOL
137 );
138 $css = '';
139 switch ( $this->type ) {
140 case 'sun':
141 $css = '#loftloader-wrapper.pl-sun #loader {' . PHP_EOL . "\t" . 'color: ' . $color . ';' . PHP_EOL . '}' . PHP_EOL;
142 break;
143 case 'circles':
144 $css = '#loftloader-wrapper.pl-circles #loader {' . PHP_EOL . "\t" . 'color: ' . $color . ';' . PHP_EOL . '}' . PHP_EOL;
145 break;
146 case 'wave':
147 $css = '#loftloader-wrapper.pl-wave #loader {' . PHP_EOL . "\t" . 'color: ' . $color . ';' . PHP_EOL . '}' . PHP_EOL;
148 break;
149 case 'square':
150 $css = '#loftloader-wrapper.pl-square #loader span {' . PHP_EOL . "\t" . 'border: 4px solid ' . $color . ';' . PHP_EOL . '}' . PHP_EOL;
151 break;
152 case 'frame':
153 $css = '#loftloader-wrapper.pl-frame #loader {' . PHP_EOL . "\t" . 'color: ' . $color . ';' . PHP_EOL . '}' . PHP_EOL;
154 break;
155 case 'imgloading':
156 $width = absint($this->get_loader_setting('loftloader_img_width'));
157 $image = esc_url($this->get_loader_setting('loftloader_custom_img'));
158 $css = empty($width) ? '' : '#loftloader-wrapper.pl-imgloading #loader {' . PHP_EOL . "\t" . 'width: ' . $width . 'px;' . PHP_EOL . '}' . PHP_EOL;
159 $css .= '#loftloader-wrapper.pl-imgloading #loader span {' . PHP_EOL . "\t" . 'background-size: cover;' . PHP_EOL . "\t" . 'background-image: url(' . $image . ');' . PHP_EOL . '}' . PHP_EOL;
160 break;
161 case 'beating':
162 $css = '#loftloader-wrapper.pl-beating #loader {' . PHP_EOL . "\t" . 'color: ' . $color . ';' . PHP_EOL . '}' . PHP_EOL;
163 break;
164 }
165 $styles .= $this->generate_style( 'loftloader-lite-custom-loader', $css );
166 echo wp_kses( $styles, array(
167 'style' => array( 'type' => array(), 'id' => array(), 'media' => array() )
168 ) );
169 }
170 /**
171 * @description loftloader html
172 */
173 public function show_loader_html( $origin ) {
174 if ( ! empty( $origin ) ) {
175 $regexp ='/(<body[^>]*>)/i';
176 $split = preg_split( $regexp, $origin, 3, PREG_SPLIT_DELIM_CAPTURE );
177 if ( is_array( $split ) && ( 3 <= count( $split ) ) ) {
178 $image = esc_url($this->get_loader_setting('loftloader_custom_img'));
179 $ending = esc_attr($this->get_loader_setting('loftloader_bg_animation'));
180
181 $html = '<div id="loftloader-wrapper" class="pl-' . $this->type . '"' . $this->loader_attributes() . '>';
182 $html .= '<div class="loader-inner"><div id="loader">';
183
184 if ( ! empty( $image ) ) {
185 // <!-- Only image loading need the span with background -->
186 if ( in_array( $this->type, array( 'imgloading' ) ) ) {
187 $html .= $this->get_loader_type_loading_bg_image( $image );
188 }
189 if ( in_array( $this->type, array( 'frame', 'imgloading' ) ) ) {
190 $html .= sprintf(
191 '<img data-no-lazy="1" class="skip-lazy" alt="%1$s" src="%2$s">',
192 esc_attr__( 'loader image', 'loftloader-pro' ),
193 esc_url( $image )
194 );
195 }
196 }
197 $html .= in_array( $this->type, array( 'imgloading' ) ) ? '' : '<span></span>';
198 $html .= '</div></div>';
199 switch($ending){
200 case 'fade':
201 $html .= '<div class="loader-section section-fade"></div>';
202 break;
203 case 'up':
204 $html .= '<div class="loader-section section-slide-up"></div>';
205 break;
206 case 'split-v':
207 $html .= '<div class="loader-section section-up"></div>';
208 $html .= '<div class="loader-section section-down"></div>';
209 break;
210 default:
211 $html .= '<div class="loader-section section-left">';
212 $html .= '</div><div class="loader-section section-right"></div>';
213 }
214
215 if(!is_customize_preview()){
216 $close_description = $this->get_loader_setting('loftloader_show_close_tip');
217 $html .= sprintf(
218 '<div class="loader-close-button" style="display: none;"><span class="screen-reader-text">%s</span>%s</div>',
219 esc_html__('Close', 'loftloader'),
220 empty($close_description) ? '' : sprintf('<span class="close-des">%s</span>', $close_description)
221 );
222 }
223 $html .= '</div>';
224
225 return $split[0] . $split[1] . $html . implode( '', array_slice( $split, 2 ) );
226 }
227 }
228 return $origin;
229 }
230 /**
231 * Background image for loader type loading with custom image
232 *
233 * @param url image url
234 * @return string html
235 */
236 private function get_loader_type_loading_bg_image( $image ) {
237 return sprintf(
238 '<div class="imgloading-container"><span style="background-image: url(%s);"></span></div>',
239 esc_url( $image )
240 );
241 }
242 /**
243 * Helper function to add manual loader settings
244 */
245 private function loader_attributes() {
246 $attrs = '';
247 $show_close_time = $this->get_loader_setting( 'loftloader_show_close_timer' );
248 $show_close_time = number_format( $show_close_time, 0, '.', '' );
249 $attrs .= sprintf( ' data-show-close-time="%s"', esc_js( esc_attr( $show_close_time * 1000 ) ) );
250
251 $max_load_time = $this->get_loader_setting( 'loftloader_max_load_time' );
252 $max_load_time = number_format( $max_load_time, 1, '.', '' );
253 if ( ! empty( $max_load_time ) ) {
254 $attrs .= sprintf( ' data-max-load-time="%s"', esc_js( esc_attr( $max_load_time * 1000 ) ) );
255 }
256
257 return apply_filters( 'loftloader_loader_attributes', $attrs );
258 }
259 /**
260 * Helper function to test whether show loftloader
261 * @return boolean return true if loftloader enabled and display on current page, otherwise false
262 */
263 private function loader_enabled() {
264 if ( $this->test_builder() ) {
265 return false;
266 } else {
267 if ( ( $this->get_loader_setting( 'loftloader_main_switch' ) === 'on' ) ) {
268 $range = $this->get_loader_setting( 'loftloader_show_range' );
269 if ( ( $range === 'sitewide' ) || ( ( $range === 'homepage' ) && is_front_page() ) ) {
270 return true;
271 } else {
272 return false;
273 }
274 } else {
275 return apply_filters( 'loftloader_loader_enabled', false );
276 }
277 }
278 }
279 /**
280 * Helper function get setting option
281 */
282 private function get_loader_setting( $setting_id ) {
283 return apply_filters( 'loftloader_get_loader_setting', get_option( $setting_id, $this->defaults[ $setting_id ] ), $setting_id );
284 }
285 /**
286 * Helper function generate styles
287 */
288 private function generate_style( $id, $style ) {
289 return '<style id="' . $id . '">' . $style . '</style>';
290 }
291 /**
292 * Make the head loaded flag
293 */
294 public function set_header_loaded() {
295 $this->site_header_loaded = true;
296 }
297 /**
298 * Make the footer loaded flag
299 */
300 public function set_footer_loaded() {
301 $this->site_footer_loaded = true;
302 }
303 /**
304 * Not show loader in builder and theme customizer
305 */
306 protected function test_builder() {
307 if ( defined( 'ELEMENTOR_PATH' ) && isset( $_GET['elementor-preview'] ) && ! empty( sanitize_text_field( wp_unslash( $_GET['elementor-preview'] ) ) ) ) {
308 return true;
309 } else if ( class_exists( 'FLBuilderLoader' ) && isset( $_GET['fl_builder'] ) ) {
310 return true;
311 } else if ( defined( 'WPB_VC_VERSION' ) && ( ! empty( $_GET['vc_editable'] ) ) ) {
312 return true;
313 } else if ( is_customize_preview() && ( empty( $_GET['plugin'] ) || ( 'loftloader-lite' != sanitize_text_field( wp_unslash( $_GET['plugin'] ) ) ) ) ) {
314 return true;
315 }
316 return false;
317 }
318 /**
319 * Identify from loftloader lite
320 */
321 public function body_class( $class ) {
322 array_push( $class, 'loftloader-lite-enabled' );
323 return $class;
324 }
325 }
326 new LoftLoader_Front();
327 }
328