| 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 span,' . PHP_EOL . '#loftloader-wrapper.pl-sun #loader span:before {' . PHP_EOL . "\t" . 'background: ' . $color . ';' . PHP_EOL . '}' . PHP_EOL; |
| 59 |
break; |
| 60 |
case 'circles': |
| 61 |
$css = '#loftloader-wrapper.pl-circles #loader span,' . PHP_EOL . '#loftloader-wrapper.pl-circles #loader:before,' . PHP_EOL . '#loftloader-wrapper.pl-circles #loader:after {' . PHP_EOL . "\t" . 'background: ' . $color . ';' . PHP_EOL . '}' . PHP_EOL; |
| 62 |
break; |
| 63 |
case 'wave': |
| 64 |
$css = '#loftloader-wrapper.pl-wave #loader span,' . PHP_EOL . '#loftloader-wrapper.pl-wave #loader:before,' . PHP_EOL . '#loftloader-wrapper.pl-wave #loader:after {' . PHP_EOL . "\t" . 'background: ' . $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:before,' . PHP_EOL . '#loftloader-wrapper.pl-frame #loader:after,' . PHP_EOL . '#loftloader-wrapper.pl-frame #loader span:before,' . PHP_EOL . '#loftloader-wrapper.pl-frame #loader span:after {' . PHP_EOL . "\t" . 'background-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 |
} |
| 79 |
$styles .= $this->generate_style('loftloader-lite-custom-loader', $css); |
| 80 |
echo $styles; |
| 81 |
|
| 82 |
ob_start(); |
| 83 |
} |
| 84 |
/** |
| 85 |
* @description loftloader html |
| 86 |
*/ |
| 87 |
public function show_loader_html(){ |
| 88 |
$image = esc_url($this->get_loader_setting('loftloader_custom_img')); |
| 89 |
$ending = esc_attr($this->get_loader_setting('loftloader_bg_animation')); |
| 90 |
|
| 91 |
$html = '<div id="loftloader-wrapper" class="pl-' . $this->type . '">'; |
| 92 |
$html .= '<div class="loader-inner"><div id="loader">'; |
| 93 |
$html .= in_array($this->type, array('frame', 'imgloading')) |
| 94 |
? ('<span></span>' . (empty($image) ? '' : ('<img src="' . $image . '" alt="preloder">'))) : '<span></span>'; |
| 95 |
$html .= '</div></div>'; |
| 96 |
switch($ending){ |
| 97 |
case 'fade': |
| 98 |
$html .= '<div class="loader-section section-fade"></div>'; |
| 99 |
break; |
| 100 |
case 'up': |
| 101 |
$html .= '<div class="loader-section section-slide-up"></div>'; |
| 102 |
break; |
| 103 |
case 'split-v': |
| 104 |
$html .= '<div class="loader-section section-up"></div>'; |
| 105 |
$html .= '<div class="loader-section section-down"></div>'; |
| 106 |
break; |
| 107 |
default: |
| 108 |
$html .= '<div class="loader-section section-left">'; |
| 109 |
$html .= '</div><div class="loader-section section-right"></div>'; |
| 110 |
} |
| 111 |
$html .= '</div>'; |
| 112 |
|
| 113 |
$origin = ob_get_clean(); |
| 114 |
$regexp ='/<body[^>]*>/i'; |
| 115 |
if(preg_match($regexp, $origin, $match)){ |
| 116 |
$html = $match[0] . $html; |
| 117 |
echo preg_replace($regexp, $html, $origin); |
| 118 |
} |
| 119 |
else{ |
| 120 |
echo $origin . $html; |
| 121 |
} |
| 122 |
} |
| 123 |
/** |
| 124 |
* Helper function to test whether show loftloader |
| 125 |
* @return boolean return true if loftloader enabled and display on current page, otherwise false |
| 126 |
*/ |
| 127 |
private function loader_enabled(){ |
| 128 |
if(($this->get_loader_setting('loftloader_main_switch') === 'on')){ |
| 129 |
$range = $this->get_loader_setting('loftloader_show_range'); |
| 130 |
if(($range === 'sitewide') || (($range === 'homepage') && is_front_page())){ |
| 131 |
return true; |
| 132 |
} |
| 133 |
else{ |
| 134 |
return false; |
| 135 |
} |
| 136 |
} |
| 137 |
else{ |
| 138 |
return apply_filters('loftloader_loader_enabled', false); |
| 139 |
} |
| 140 |
} |
| 141 |
/** |
| 142 |
* Helper function get setting option |
| 143 |
*/ |
| 144 |
private function get_loader_setting($setting_id){ |
| 145 |
return apply_filters('loftloader_get_loader_setting', get_option($setting_id, $this->defaults[$setting_id]), $setting_id); |
| 146 |
} |
| 147 |
/** |
| 148 |
* Helper function generate styles |
| 149 |
*/ |
| 150 |
private function generate_style($id, $style){ |
| 151 |
return '<style id="' . $id . '">' . $style . '</style>'; |
| 152 |
} |
| 153 |
} |
| 154 |
new LoftLoader_Front(); |
| 155 |
} |