PluginProbe
LoftLoader / 2.4.0
LoftLoader v2.4.0
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.4.0, at inc/class-loftloader-front.php

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