myEasy-Preloader. * Version: 1.3.0 * Author: Temitope Somuwa * Requires at least: 5.2 * Requires PHP: 7.2 * License: GPLv2 or later * License URI: https://www.gnu.org/licenses/gpl-2.0.html * Text Domain: myeasy-preloader */ if ( ! defined( 'ABSPATH' ) ) { exit; } define( 'STOJA_PRELOADER_VERSION', '1.3.0' ); final class Stoja_Preloader { const OPT = 'stoja_options'; private $printed = false; public static function defaults() { return array( 'enabled' => 1, 'logo_id' => 0, 'text' => '', 'bg_color' => '#ffffff', 'text_color' => '#111111', 'accent_color' => '#d71920', 'logo_width' => 160, 'text_size' => 18, // px 'text_weight' => '400', // 400 | 600 | 700 | 800 'text_style' => 'normal', // normal | italic 'text_case' => 'none', // none | uppercase | capitalize 'text_font' => 'inherit', // inherit | sans | serif | mono | custom 'text_font_custom' => '', 'text_spacing' => 1, // letter-spacing px 'animation' => 'ring', // ring | pulse | dots | bars | progress | ripple | bounce | flip | none 'min_time' => 600, // ms 'fade_time' => 500, // ms 'once_session' => 0, 'display_mode' => 'all', // all | home | include | exclude 'pages' => array(), // page IDs (0 = latest-posts homepage) ); } private function clean_font_name( $name ) { $name = preg_replace( '/[^A-Za-z0-9 _\-]/', '', (string) $name ); return substr( trim( $name ), 0, 60 ); } private function animation_keys() { return array( 'ring', 'pulse', 'dots', 'bars', 'progress', 'ripple', 'bounce', 'flip', 'none' ); } private function font_stack( $key, $custom = '' ) { if ( 'custom' === $key ) { $name = $this->clean_font_name( $custom ); return '' !== $name ? '"' . $name . '",sans-serif' : 'inherit'; } $map = array( 'inherit' => 'inherit', 'sans' => '-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif', 'serif' => 'Georgia,"Times New Roman",Times,serif', 'mono' => 'ui-monospace,SFMono-Regular,Menlo,Consolas,monospace', ); return isset( $map[ $key ] ) ? $map[ $key ] : 'inherit'; } private function opts() { $saved = get_option( self::OPT, array() ); if ( is_array( $saved ) && ! isset( $saved['display_mode'] ) && ! empty( $saved['home_only'] ) ) { $saved['display_mode'] = 'home'; // migrate from 1.0.0 } return wp_parse_args( $saved, self::defaults() ); } public function __construct() { add_action( 'admin_menu', array( $this, 'menu' ) ); add_action( 'admin_init', array( $this, 'register' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_assets' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ), 1 ); add_action( 'wp_body_open', array( $this, 'markup' ), 1 ); add_action( 'wp_footer', array( $this, 'markup' ), 1 ); // fallback for themes without wp_body_open add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'links' ) ); } /* ---------------- Front end ---------------- */ private function should_show() { if ( is_admin() || is_feed() || is_embed() || is_customize_preview() || wp_doing_ajax() ) { return false; } // Don't interfere with page builders' editors/previews. if ( isset( $_GET['elementor-preview'] ) || isset( $_GET['preview'] ) || isset( $_GET['et_fb'] ) || isset( $_GET['fl_builder'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification return false; } $o = $this->opts(); if ( ! $o['enabled'] ) { return false; } $mode = $o['display_mode']; if ( 'home' === $mode && ! ( is_front_page() || is_home() ) ) { return false; } if ( 'include' === $mode || 'exclude' === $mode ) { $ids = array_map( 'intval', (array) $o['pages'] ); $cur = (int) get_queried_object_id(); $match = ( $cur && in_array( $cur, $ids, true ) ) || ( is_front_page() && in_array( 0, $ids, true ) ); if ( 'include' === $mode && ! $match ) { return false; } if ( 'exclude' === $mode && $match ) { return false; } } return true; } public function enqueue() { if ( ! $this->should_show() ) { return; } $o = $this->opts(); $fade = min( 3000, max( 0, (int) $o['fade_time'] ) ); $min = min( 5000, max( 0, (int) $o['min_time'] ) ); $size = min( 80, max( 10, (int) $o['text_size'] ) ); $width = min( 600, max( 40, (int) $o['logo_width'] ) ); $space = min( 12, max( 0, (int) $o['text_spacing'] ) ); $weight = in_array( (string) $o['text_weight'], array( '400', '600', '700', '800' ), true ) ? (string) $o['text_weight'] : '400'; $style = ( 'italic' === $o['text_style'] ) ? 'italic' : 'normal'; $case = in_array( $o['text_case'], array( 'none', 'uppercase', 'capitalize' ), true ) ? $o['text_case'] : 'none'; $bg = sanitize_hex_color( $o['bg_color'] ) ? sanitize_hex_color( $o['bg_color'] ) : '#ffffff'; $color = sanitize_hex_color( $o['text_color'] ) ? sanitize_hex_color( $o['text_color'] ) : '#111111'; $accent = sanitize_hex_color( $o['accent_color'] ) ? sanitize_hex_color( $o['accent_color'] ) : '#d71920'; $css = '#stoja-preloader{position:fixed;inset:0;z-index:999999;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:22px;'; $css .= 'background:' . $bg . ';color:' . $color . ';opacity:1;visibility:visible;'; $css .= 'transition:opacity ' . $fade . 'ms ease,visibility ' . $fade . 'ms ease;text-align:center;padding:24px}'; $css .= '#stoja-preloader.stoja-hide{opacity:0;visibility:hidden;pointer-events:none}'; $css .= '#stoja-preloader .stoja-logo-wrap{position:relative;display:flex;align-items:center;justify-content:center;padding:18px}'; $css .= '#stoja-preloader img{display:block;width:' . $width . 'px;max-width:70vw;height:auto}'; $css .= '#stoja-preloader .stoja-text{font-size:' . $size . 'px;font-weight:' . $weight . ';font-style:' . $style . ';'; $css .= 'text-transform:' . $case . ';font-family:' . $this->font_stack( $o['text_font'], $o['text_font_custom'] ) . ';'; $css .= 'letter-spacing:' . $space . 'px;line-height:1.4;max-width:90vw}'; $css .= '@media (max-width:600px){#stoja-preloader .stoja-text{font-size:max(14px,calc(' . $size . 'px * .8))}}'; $css .= '#stoja-preloader.stoja-ring .stoja-logo-wrap::after{content:"";position:absolute;inset:0;border:3px solid transparent;border-top-color:' . $accent . ';border-right-color:' . $accent . ';border-radius:50%;animation:stoja-spin 1s linear infinite}'; $css .= '#stoja-preloader.stoja-pulse img,#stoja-preloader.stoja-pulse .stoja-text{animation:stoja-pulse 1.4s ease-in-out infinite}'; $css .= '@keyframes stoja-spin{to{transform:rotate(360deg)}}'; $css .= '@keyframes stoja-pulse{0%,100%{opacity:1;transform:scale(1)}50%{opacity:.55;transform:scale(.96)}}'; $css .= '#stoja-preloader .stoja-loader{display:flex;align-items:center;justify-content:center;gap:8px}'; // Dots. $css .= '#stoja-preloader.stoja-dots .stoja-loader span{width:12px;height:12px;border-radius:50%;background:' . $accent . ';animation:stoja-dot 1.2s ease-in-out infinite both}'; $css .= '#stoja-preloader.stoja-dots .stoja-loader span:nth-child(1){animation-delay:-.32s}#stoja-preloader.stoja-dots .stoja-loader span:nth-child(2){animation-delay:-.16s}'; $css .= '@keyframes stoja-dot{0%,80%,100%{transform:scale(0)}40%{transform:scale(1)}}'; // Bars. $css .= '#stoja-preloader.stoja-bars .stoja-loader{height:40px;gap:5px}'; $css .= '#stoja-preloader.stoja-bars .stoja-loader span{width:5px;height:100%;background:' . $accent . ';animation:stoja-bar 1.1s ease-in-out infinite}'; $css .= '#stoja-preloader.stoja-bars .stoja-loader span:nth-child(2){animation-delay:-1s}#stoja-preloader.stoja-bars .stoja-loader span:nth-child(3){animation-delay:-.9s}'; $css .= '#stoja-preloader.stoja-bars .stoja-loader span:nth-child(4){animation-delay:-.8s}#stoja-preloader.stoja-bars .stoja-loader span:nth-child(5){animation-delay:-.7s}'; $css .= '@keyframes stoja-bar{0%,40%,100%{transform:scaleY(.35)}20%{transform:scaleY(1)}}'; // Progress bar. $css .= '#stoja-preloader.stoja-progress .stoja-loader{position:relative;width:min(240px,70vw);height:4px;border-radius:4px;overflow:hidden;background:rgba(128,128,128,.25)}'; $css .= '#stoja-preloader.stoja-progress .stoja-loader span{position:absolute;top:0;bottom:0;left:-40%;width:40%;border-radius:4px;background:' . $accent . ';animation:stoja-slide 1.2s ease-in-out infinite}'; $css .= '@keyframes stoja-slide{0%{left:-40%}100%{left:100%}}'; // Ripple. $css .= '#stoja-preloader.stoja-ripple .stoja-logo-wrap::before,#stoja-preloader.stoja-ripple .stoja-logo-wrap::after{content:"";position:absolute;inset:0;border:3px solid ' . $accent . ';border-radius:50%;opacity:0;animation:stoja-ripple 1.8s ease-out infinite}'; $css .= '#stoja-preloader.stoja-ripple .stoja-logo-wrap::after{animation-delay:.9s}'; $css .= '@keyframes stoja-ripple{0%{transform:scale(.8);opacity:.8}100%{transform:scale(1.5);opacity:0}}'; // Bounce and flip (logo only). $css .= '#stoja-preloader.stoja-bounce img{animation:stoja-bounce 1s ease-in-out infinite}'; $css .= '@keyframes stoja-bounce{0%,100%{transform:translateY(0)}50%{transform:translateY(-16px)}}'; $css .= '#stoja-preloader.stoja-flip img{animation:stoja-flip 1.8s ease-in-out infinite}'; $css .= '@keyframes stoja-flip{0%{transform:perspective(500px) rotateY(0)}100%{transform:perspective(500px) rotateY(360deg)}}'; $css .= 'html.stoja-skip #stoja-preloader{display:none!important}'; $css .= '@media (prefers-reduced-motion:reduce){#stoja-preloader *,#stoja-preloader *::before,#stoja-preloader *::after{animation:none!important}}'; $js = <<<'JS' (function(){ var once = __ONCE__, min = __MIN__, start = Date.now(), done = false; try { if (once && sessionStorage.getItem('stoja_seen')) { document.documentElement.classList.add('stoja-skip'); return; } } catch(e){} function hide(){ if (done) return; done = true; var el = document.getElementById('stoja-preloader'); if (!el) return; el.classList.add('stoja-hide'); try { if (once) sessionStorage.setItem('stoja_seen','1'); } catch(e){} setTimeout(function(){ if (el.parentNode) el.parentNode.removeChild(el); }, __REMOVE__); } function onLoad(){ setTimeout(hide, Math.max(0, min - (Date.now() - start))); } if (document.readyState === 'complete') { onLoad(); } else { window.addEventListener('load', onLoad); } setTimeout(hide, 10000); })(); JS; $js = str_replace( array( '__ONCE__', '__MIN__', '__REMOVE__' ), array( $o['once_session'] ? 'true' : 'false', (string) $min, (string) ( $fade + 100 ) ), $js ); wp_register_style( 'stoja-preloader', false, array(), STOJA_PRELOADER_VERSION ); wp_enqueue_style( 'stoja-preloader' ); wp_add_inline_style( 'stoja-preloader', $css ); wp_register_script( 'stoja-preloader', false, array(), STOJA_PRELOADER_VERSION, false ); wp_enqueue_script( 'stoja-preloader' ); wp_add_inline_script( 'stoja-preloader', $js ); } public function markup() { if ( $this->printed || ! $this->should_show() ) { return; } $this->printed = true; $o = $this->opts(); $logo = $o['logo_id'] ? wp_get_attachment_image_url( (int) $o['logo_id'], 'full' ) : ''; $anim = in_array( $o['animation'], $this->animation_keys(), true ) ? $o['animation'] : 'ring'; if ( ! $logo && in_array( $anim, array( 'bounce', 'flip' ), true ) ) { $anim = 'dots'; // these two animate the logo, so fall back when there is none. } ?>
Temitope Somuwa' ), array( 'strong' => array() ) ); ?>