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

253 lines 9.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->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 $html .= in_array($this->type, array('frame', 'imgloading'))
167 ? ('<span></span>' . (empty($image) ? '' : ('<img src="' . $image . '" alt="preloder">'))) : '<span></span>';
168 $html .= '</div></div>';
169 switch($ending){
170 case 'fade':
171 $html .= '<div class="loader-section section-fade"></div>';
172 break;
173 case 'up':
174 $html .= '<div class="loader-section section-slide-up"></div>';
175 break;
176 case 'split-v':
177 $html .= '<div class="loader-section section-up"></div>';
178 $html .= '<div class="loader-section section-down"></div>';
179 break;
180 default:
181 $html .= '<div class="loader-section section-left">';
182 $html .= '</div><div class="loader-section section-right"></div>';
183 }
184
185 if(!is_customize_preview()){
186 $close_description = $this->get_loader_setting('loftloader_show_close_tip');
187 $html .= sprintf(
188 '<div class="loader-close-button" style="display: none;"><span class="screen-reader-text">%s</span>%s</div>',
189 esc_html__('Close', 'loftloader'),
190 empty($close_description) ? '' : sprintf('<span class="close-des">%s</span>', $close_description)
191 );
192 }
193 $html .= '</div>';
194
195 return $split[0] . $split[1] . $html . implode( '', array_slice( $split, 2 ) );
196 }
197 }
198 return $origin;
199 }
200 /**
201 * Helper function to add manual loader settings
202 */
203 private function loader_attributes() {
204 $attrs = '';
205 $show_close_time = $this->get_loader_setting( 'loftloader_show_close_timer' );
206 $show_close_time = number_format( $show_close_time, 0, '.', '' );
207 $attrs .= sprintf( ' data-show-close-time="%s"', esc_js( esc_attr( $show_close_time * 1000 ) ) );
208 return apply_filters( 'loftloader_loader_attributes', $attrs );
209 }
210 /**
211 * Helper function to test whether show loftloader
212 * @return boolean return true if loftloader enabled and display on current page, otherwise false
213 */
214 private function loader_enabled() {
215 if ( ( $this->get_loader_setting( 'loftloader_main_switch' ) === 'on' ) ) {
216 $range = $this->get_loader_setting( 'loftloader_show_range' );
217 if ( ( $range === 'sitewide' ) || ( ( $range === 'homepage' ) && is_front_page() ) ) {
218 return true;
219 } else {
220 return false;
221 }
222 } else {
223 return apply_filters( 'loftloader_loader_enabled', false );
224 }
225 }
226 /**
227 * Helper function get setting option
228 */
229 private function get_loader_setting( $setting_id ) {
230 return apply_filters( 'loftloader_get_loader_setting', get_option( $setting_id, $this->defaults[ $setting_id ] ), $setting_id );
231 }
232 /**
233 * Helper function generate styles
234 */
235 private function generate_style( $id, $style ) {
236 return '<style id="' . $id . '">' . $style . '</style>';
237 }
238 /**
239 * Make the head loaded flag
240 */
241 public function set_header_loaded() {
242 $this->site_header_loaded = true;
243 }
244 /**
245 * Make the footer loaded flag
246 */
247 public function set_footer_loaded() {
248 $this->site_footer_loaded = true;
249 }
250 }
251 new LoftLoader_Front();
252 }
253