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

177 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Not allowed by directly accessing.
3 if(!defined('ABSPATH')){
4 die('Access not allowed!');
5 }
6
7 /**
8 * Main class for front display
9 *
10 * @package LoftLoader
11 * @link http://www.loftocean.com/
12 * @author Suihai Huang from Loft Ocean Team
13
14 * @since version 1.0
15 */
16
17 if(!class_exists('LoftLoader_Front')){
18 class LoftLoader_Front{
19 private $defaults;
20 private $type; // Get the loader settings
21 public function __construct(){
22 $this->get_settings();
23 if($this->loader_enabled()){
24 add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
25 add_action('wp_head', array($this, 'loader_custom_styles'), 100);
26 add_action('wp_footer', array($this, 'show_loader_html'));
27 }
28 }
29 /**
30 * @description get the plugin settings
31 */
32 public function get_settings(){
33 global $loftloader_default_settings;
34 $this->defaults = $loftloader_default_settings;
35 do_action('loftloader_settings');
36 $this->type = esc_attr($this->get_loader_setting('loftloader_loader_type'));
37 }
38 /**
39 * @description enqueue the scripts and styles for front end
40 */
41 public function enqueue_scripts(){
42 is_customize_preview() ? '' : wp_enqueue_script('loftloader-lite-front-main', LOFTLOADER_URI . 'assets/js/loftloader.min.js', array('jquery'), LOFTLOADER_ASSET_VERSION, true);
43 wp_enqueue_style('loftloader-lite-animation', LOFTLOADER_URI . 'assets/css/loftloader.min.css', array(), LOFTLOADER_ASSET_VERSION);
44 }
45 /**
46 * @description custom css for front end
47 */
48 public function loader_custom_styles(){
49 $color = esc_attr($this->get_loader_setting('loftloader_loader_color'));
50 $bgColor = esc_attr($this->get_loader_setting('loftloader_bg_color'));
51 $bgOpacity = intval($this->get_loader_setting('loftloader_bg_opacity')) / 100;
52
53 $styles = $this->generate_style('loftloader-lite-custom-bg-color', '#loftloader-wrapper .loader-section {' . PHP_EOL . "\t" . 'background: ' . $bgColor . ';' . PHP_EOL . '}' . PHP_EOL);
54 $styles .= $this->generate_style('loftloader-lite-custom-bg-opacity', '#loftloader-wrapper .loader-section {' . PHP_EOL . "\t" . 'opacity: ' . $bgOpacity . ';' . PHP_EOL . '}' . PHP_EOL);
55 $css = '';
56 switch($this->type){
57 case 'sun':
58 $css = '#loftloader-wrapper.pl-sun #loader {' . PHP_EOL . "\t" . 'color: ' . $color . ';' . PHP_EOL . '}' . PHP_EOL;
59 break;
60 case 'circles':
61 $css = '#loftloader-wrapper.pl-circles #loader {' . PHP_EOL . "\t" . 'color: ' . $color . ';' . PHP_EOL . '}' . PHP_EOL;
62 break;
63 case 'wave':
64 $css = '#loftloader-wrapper.pl-wave #loader {' . PHP_EOL . "\t" . 'color: ' . $color . ';' . PHP_EOL . '}' . PHP_EOL;
65 break;
66 case 'square':
67 $css = '#loftloader-wrapper.pl-square #loader span {' . PHP_EOL . "\t" . 'border: 4px solid ' . $color . ';' . PHP_EOL . '}' . PHP_EOL;
68 break;
69 case 'frame':
70 $css = '#loftloader-wrapper.pl-frame #loader {' . PHP_EOL . "\t" . 'color: ' . $color . ';' . PHP_EOL . '}' . PHP_EOL;
71 break;
72 case 'imgloading':
73 $width = absint($this->get_loader_setting('loftloader_img_width'));
74 $image = esc_url($this->get_loader_setting('loftloader_custom_img'));
75 $css = empty($width) ? '' : '#loftloader-wrapper.pl-imgloading #loader {' . PHP_EOL . "\t" . 'width: ' . $width . 'px;' . PHP_EOL . '}' . PHP_EOL;
76 $css .= '#loftloader-wrapper.pl-imgloading #loader span {' . PHP_EOL . "\t" . 'background-size: cover;' . PHP_EOL . "\t" . 'background-image: url(' . $image . ');' . PHP_EOL . '}' . PHP_EOL;
77 break;
78 case 'beating':
79 $css = '#loftloader-wrapper.pl-beating #loader {' . PHP_EOL . "\t" . 'color: ' . $color . ';' . PHP_EOL . '}' . PHP_EOL;
80 break;
81 }
82 $styles .= $this->generate_style('loftloader-lite-custom-loader', $css);
83 echo $styles;
84
85 ob_start();
86 }
87 /**
88 * @description loftloader html
89 */
90 public function show_loader_html(){
91 $image = esc_url($this->get_loader_setting('loftloader_custom_img'));
92 $ending = esc_attr($this->get_loader_setting('loftloader_bg_animation'));
93
94 $html = '<div id="loftloader-wrapper" class="pl-' . $this->type . '"' . $this->loader_attributes() . '>';
95 $html .= '<div class="loader-inner"><div id="loader">';
96 $html .= in_array($this->type, array('frame', 'imgloading'))
97 ? ('<span></span>' . (empty($image) ? '' : ('<img src="' . $image . '" alt="preloder">'))) : '<span></span>';
98 $html .= '</div></div>';
99 switch($ending){
100 case 'fade':
101 $html .= '<div class="loader-section section-fade"></div>';
102 break;
103 case 'up':
104 $html .= '<div class="loader-section section-slide-up"></div>';
105 break;
106 case 'split-v':
107 $html .= '<div class="loader-section section-up"></div>';
108 $html .= '<div class="loader-section section-down"></div>';
109 break;
110 default:
111 $html .= '<div class="loader-section section-left">';
112 $html .= '</div><div class="loader-section section-right"></div>';
113 }
114
115 if(!is_customize_preview()){
116 $close_description = $this->get_loader_setting('loftloader_show_close_tip');
117 $html .= sprintf(
118 '<div class="loader-close-button" style="display: none;"><span class="screen-reader-text">%s</span>%s</div>',
119 esc_html__('Close', 'loftloader'),
120 empty($close_description) ? '' : sprintf('<span class="close-des">%s</span>', $close_description)
121 );
122 }
123 $html .= '</div>';
124
125 $origin = ob_get_clean();
126 $regexp ='/<body[^>]*>/i';
127 if(preg_match($regexp, $origin, $match)){
128 $html = $match[0] . $html;
129 echo preg_replace($regexp, $html, $origin);
130 }
131 else{
132 echo $origin . $html;
133 }
134 }
135 /**
136 * Helper function to add manual loader settings
137 */
138 private function loader_attributes(){
139 $attrs = '';
140 $show_close_time = $this->get_loader_setting('loftloader_show_close_timer');
141 $show_close_time = number_format($show_close_time, 0, '.', '');
142 $attrs .= sprintf(' data-show-close-time="%s"', esc_js(esc_attr($show_close_time * 1000)));
143 return apply_filters('loftloader_loader_attributes', $attrs);
144 }
145 /**
146 * Helper function to test whether show loftloader
147 * @return boolean return true if loftloader enabled and display on current page, otherwise false
148 */
149 private function loader_enabled(){
150 if(($this->get_loader_setting('loftloader_main_switch') === 'on')){
151 $range = $this->get_loader_setting('loftloader_show_range');
152 if(($range === 'sitewide') || (($range === 'homepage') && is_front_page())){
153 return true;
154 }
155 else{
156 return false;
157 }
158 }
159 else{
160 return apply_filters('loftloader_loader_enabled', false);
161 }
162 }
163 /**
164 * Helper function get setting option
165 */
166 private function get_loader_setting($setting_id){
167 return apply_filters('loftloader_get_loader_setting', get_option($setting_id, $this->defaults[$setting_id]), $setting_id);
168 }
169 /**
170 * Helper function generate styles
171 */
172 private function generate_style($id, $style){
173 return '<style id="' . $id . '">' . $style . '</style>';
174 }
175 }
176 new LoftLoader_Front();
177 }