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

158 lines 6.1 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 . '">';
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 $html .= '</div>';
115
116 $origin = ob_get_clean();
117 $regexp ='/<body[^>]*>/i';
118 if(preg_match($regexp, $origin, $match)){
119 $html = $match[0] . $html;
120 echo preg_replace($regexp, $html, $origin);
121 }
122 else{
123 echo $origin . $html;
124 }
125 }
126 /**
127 * Helper function to test whether show loftloader
128 * @return boolean return true if loftloader enabled and display on current page, otherwise false
129 */
130 private function loader_enabled(){
131 if(($this->get_loader_setting('loftloader_main_switch') === 'on')){
132 $range = $this->get_loader_setting('loftloader_show_range');
133 if(($range === 'sitewide') || (($range === 'homepage') && is_front_page())){
134 return true;
135 }
136 else{
137 return false;
138 }
139 }
140 else{
141 return apply_filters('loftloader_loader_enabled', false);
142 }
143 }
144 /**
145 * Helper function get setting option
146 */
147 private function get_loader_setting($setting_id){
148 return apply_filters('loftloader_get_loader_setting', get_option($setting_id, $this->defaults[$setting_id]), $setting_id);
149 }
150 /**
151 * Helper function generate styles
152 */
153 private function generate_style($id, $style){
154 return '<style id="' . $id . '">' . $style . '</style>';
155 }
156 }
157 new LoftLoader_Front();
158 }