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

274 lines 10.3 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->start_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 }
27 }
28 /**
29 * Start cache for outputing
30 */
31 public function start_cache() {
32 // Only for front view
33 if ( ! is_admin() ) {
34 // Start cache the output with callback function
35 ob_start( array( $this, 'modify_html' ) );
36 }
37 }
38 /**
39 * Will be called when flush cache
40 *
41 * @param string cached string
42 * @return string modified cached string
43 */
44 public function modify_html( $html ) {
45 if ( $this->site_header_loaded && $this->site_footer_loaded ) {
46 return apply_filters( 'loftloader_modify_html', $html );
47 } else {
48 return $html;
49 }
50 }
51 /**
52 * @description get the plugin settings
53 */
54 public function get_settings() {
55 global $loftloader_default_settings;
56 $this->defaults = $loftloader_default_settings;
57 do_action( 'loftloader_settings' );
58 $this->type = esc_attr( $this->get_loader_setting( 'loftloader_loader_type' ) );
59 add_action( 'wp_head', array( $this, 'set_header_loaded' ) );
60 add_action( 'wp_footer', array( $this, 'set_footer_loaded' ) );
61 }
62 /**
63 * @description enqueue the scripts and styles for front end
64 */
65 public function enqueue_scripts() {
66 $loadJSStyle = $this->get_loader_setting( 'loftloader_inline_js' );
67 if ( ! is_customize_preview() && ( 'inline' !== $loadJSStyle ) ) {
68 wp_enqueue_script( 'loftloader-lite-front-main', LOFTLOADER_URI . 'assets/js/loftloader.min.js', array( 'jquery' ), LOFTLOADER_ASSET_VERSION, true );
69 }
70 wp_enqueue_style('loftloader-lite-animation', LOFTLOADER_URI . 'assets/css/loftloader.min.css', array(), LOFTLOADER_ASSET_VERSION);
71 }
72 /**
73 * Load inline JavaScript code if set
74 */
75 public function load_inline_js() {
76 $loadJSStyle = $this->get_loader_setting( 'loftloader_inline_js' );
77 if ( ( 'inline' === $loadJSStyle ) && ! is_customize_preview() ) { ?>
78 <script type="text/javascript">
79 ( function() {
80 function loftloader_finished() {
81 document.body.classList.add( 'loaded' );
82 }
83 var loader = document.getElementById( 'loftloader-wrapper' );
84 if ( loader ) {
85 window.addEventListener( 'load', function( e ) {
86 loftloader_finished();
87 } );
88 if ( loader.dataset && loader.dataset.showCloseTime ) {
89 var showCloseTime = parseInt( loader.dataset.showCloseTime ),
90 closeBtn = loader.getElementsByClassName( 'loader-close-button' );
91 if ( showCloseTime && closeBtn.length ) {
92 setTimeout( function() {
93 closeBtn[0].style.display = '';
94 }, showCloseTime );
95 closeBtn[0].addEventListener( 'click', function( e ) {
96 loftloader_finished();
97 } );
98 }
99 }
100 }
101 } ) ();
102 </script> <?php
103 }
104 }
105 /**
106 * @description custom css for front end
107 */
108 public function loader_custom_styles() {
109 $color = esc_attr( $this->get_loader_setting( 'loftloader_loader_color' ) );
110 $bgColor = esc_attr( $this->get_loader_setting( 'loftloader_bg_color' ) );
111 $bgOpacity = intval( $this->get_loader_setting('loftloader_bg_opacity' ) ) / 100;
112
113 $styles = $this->generate_style(
114 'loftloader-lite-custom-bg-color',
115 '#loftloader-wrapper .loader-section {' . PHP_EOL . "\t" . 'background: ' . $bgColor . ';' . PHP_EOL . '}' . PHP_EOL
116 );
117 $styles .= $this->generate_style(
118 'loftloader-lite-custom-bg-opacity',
119 '#loftloader-wrapper .loader-section {' . PHP_EOL . "\t" . 'opacity: ' . $bgOpacity . ';' . PHP_EOL . '}' . PHP_EOL
120 );
121 $css = '';
122 switch ( $this->type ) {
123 case 'sun':
124 $css = '#loftloader-wrapper.pl-sun #loader {' . PHP_EOL . "\t" . 'color: ' . $color . ';' . PHP_EOL . '}' . PHP_EOL;
125 break;
126 case 'circles':
127 $css = '#loftloader-wrapper.pl-circles #loader {' . PHP_EOL . "\t" . 'color: ' . $color . ';' . PHP_EOL . '}' . PHP_EOL;
128 break;
129 case 'wave':
130 $css = '#loftloader-wrapper.pl-wave #loader {' . PHP_EOL . "\t" . 'color: ' . $color . ';' . PHP_EOL . '}' . PHP_EOL;
131 break;
132 case 'square':
133 $css = '#loftloader-wrapper.pl-square #loader span {' . PHP_EOL . "\t" . 'border: 4px solid ' . $color . ';' . PHP_EOL . '}' . PHP_EOL;
134 break;
135 case 'frame':
136 $css = '#loftloader-wrapper.pl-frame #loader {' . PHP_EOL . "\t" . 'color: ' . $color . ';' . PHP_EOL . '}' . PHP_EOL;
137 break;
138 case 'imgloading':
139 $width = absint($this->get_loader_setting('loftloader_img_width'));
140 $image = esc_url($this->get_loader_setting('loftloader_custom_img'));
141 $css = empty($width) ? '' : '#loftloader-wrapper.pl-imgloading #loader {' . PHP_EOL . "\t" . 'width: ' . $width . 'px;' . PHP_EOL . '}' . PHP_EOL;
142 $css .= '#loftloader-wrapper.pl-imgloading #loader span {' . PHP_EOL . "\t" . 'background-size: cover;' . PHP_EOL . "\t" . 'background-image: url(' . $image . ');' . PHP_EOL . '}' . PHP_EOL;
143 break;
144 case 'beating':
145 $css = '#loftloader-wrapper.pl-beating #loader {' . PHP_EOL . "\t" . 'color: ' . $color . ';' . PHP_EOL . '}' . PHP_EOL;
146 break;
147 }
148 $styles .= $this->generate_style( 'loftloader-lite-custom-loader', $css );
149 echo wp_kses( $styles, array(
150 'style' => array( 'type' => array(), 'id' => array(), 'media' => array() )
151 ) );
152 }
153 /**
154 * @description loftloader html
155 */
156 public function show_loader_html( $origin ) {
157 if ( ! empty( $origin ) ) {
158 $regexp ='/(<body[^>]*>)/i';
159 $split = preg_split( $regexp, $origin, 3, PREG_SPLIT_DELIM_CAPTURE );
160 if ( is_array( $split ) && ( 3 <= count( $split ) ) ) {
161 $image = esc_url($this->get_loader_setting('loftloader_custom_img'));
162 $ending = esc_attr($this->get_loader_setting('loftloader_bg_animation'));
163
164 $html = '<div id="loftloader-wrapper" class="pl-' . $this->type . '"' . $this->loader_attributes() . '>';
165 $html .= '<div class="loader-inner"><div id="loader">';
166
167 if ( ! empty( $image ) ) {
168 // <!-- Only image loading need the span with background -->
169 if ( in_array( $this->type, array( 'imgloading' ) ) ) {
170 $html .= $this->get_loader_type_loading_bg_image( $image );
171 }
172 if ( in_array( $this->type, array( 'frame', 'imgloading' ) ) ) {
173 $html .= sprintf( '<img alt="%1$s" src="%2$s">', esc_attr__( 'loader image', 'loftloader-pro' ), esc_url( $image ) );
174 }
175 }
176 $html .= in_array( $this->type, array( 'imgloading' ) ) ? '' : '<span></span>';
177 $html .= '</div></div>';
178 switch($ending){
179 case 'fade':
180 $html .= '<div class="loader-section section-fade"></div>';
181 break;
182 case 'up':
183 $html .= '<div class="loader-section section-slide-up"></div>';
184 break;
185 case 'split-v':
186 $html .= '<div class="loader-section section-up"></div>';
187 $html .= '<div class="loader-section section-down"></div>';
188 break;
189 default:
190 $html .= '<div class="loader-section section-left">';
191 $html .= '</div><div class="loader-section section-right"></div>';
192 }
193
194 if(!is_customize_preview()){
195 $close_description = $this->get_loader_setting('loftloader_show_close_tip');
196 $html .= sprintf(
197 '<div class="loader-close-button" style="display: none;"><span class="screen-reader-text">%s</span>%s</div>',
198 esc_html__('Close', 'loftloader'),
199 empty($close_description) ? '' : sprintf('<span class="close-des">%s</span>', $close_description)
200 );
201 }
202 $html .= '</div>';
203
204 return $split[0] . $split[1] . $html . implode( '', array_slice( $split, 2 ) );
205 }
206 }
207 return $origin;
208 }
209 /**
210 * Background image for loader type loading with custom image
211 *
212 * @param url image url
213 * @return string html
214 */
215 private function get_loader_type_loading_bg_image( $image ) {
216 return sprintf(
217 '<div class="imgloading-container"><span style="background-image: url(%s);"></span></div>',
218 esc_url( $image )
219 );
220 }
221 /**
222 * Helper function to add manual loader settings
223 */
224 private function loader_attributes() {
225 $attrs = '';
226 $show_close_time = $this->get_loader_setting( 'loftloader_show_close_timer' );
227 $show_close_time = number_format( $show_close_time, 0, '.', '' );
228 $attrs .= sprintf( ' data-show-close-time="%s"', esc_js( esc_attr( $show_close_time * 1000 ) ) );
229 return apply_filters( 'loftloader_loader_attributes', $attrs );
230 }
231 /**
232 * Helper function to test whether show loftloader
233 * @return boolean return true if loftloader enabled and display on current page, otherwise false
234 */
235 private function loader_enabled() {
236 if ( ( $this->get_loader_setting( 'loftloader_main_switch' ) === 'on' ) ) {
237 $range = $this->get_loader_setting( 'loftloader_show_range' );
238 if ( ( $range === 'sitewide' ) || ( ( $range === 'homepage' ) && is_front_page() ) ) {
239 return true;
240 } else {
241 return false;
242 }
243 } else {
244 return apply_filters( 'loftloader_loader_enabled', false );
245 }
246 }
247 /**
248 * Helper function get setting option
249 */
250 private function get_loader_setting( $setting_id ) {
251 return apply_filters( 'loftloader_get_loader_setting', get_option( $setting_id, $this->defaults[ $setting_id ] ), $setting_id );
252 }
253 /**
254 * Helper function generate styles
255 */
256 private function generate_style( $id, $style ) {
257 return '<style id="' . $id . '">' . $style . '</style>';
258 }
259 /**
260 * Make the head loaded flag
261 */
262 public function set_header_loaded() {
263 $this->site_header_loaded = true;
264 }
265 /**
266 * Make the footer loaded flag
267 */
268 public function set_footer_loaded() {
269 $this->site_footer_loaded = true;
270 }
271 }
272 new LoftLoader_Front();
273 }
274