Changeset 3812923 for init-fx-engine/trunk/init-fx-engine.php
- Timestamp:
- 11/20/2026 04:34:16 AM (2 hours ago)
- File:
-
- 1 edited
-
init-fx-engine/trunk/init-fx-engine.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
init-fx-engine/trunk/init-fx-engine.php
r3723121 r3812923 2 2 /** 3 3 * Plugin Name: Init FX Engine 4 * Description: Add interactive visual effects like fireworks, emoji rain, snowfall and seasonal holiday scenes — triggered by comments, keywords, or holidays. Now with native Block Editor support. Make your WordPress site come alive!4 * Description: Add interactive visual effects like fireworks, emoji rain, snowfall, particle backgrounds and seasonal holiday scenes — triggered by comments, keywords, blocks or holidays. Scripts only load on pages that actually use them. Make your WordPress site come alive! 5 5 * Plugin URI: https://inithtml.com/plugin/init-fx-engine/ 6 * Version: 2.0. 16 * Version: 2.0.2 7 7 * Author: Init HTML 8 8 * Author URI: https://inithtml.com/ … … 10 10 * Domain Path: /languages 11 11 * Requires at least: 6.9 12 * Tested up to: 7.113 12 * Requires PHP: 7.4 14 13 * License: GPLv2 or later 15 14 * License URI: https://www.gnu.org/licenses/gpl-2.0.html 15 * 16 * @package Init_FX_Engine 16 17 */ 17 18 … … 20 21 // === DEFINE CONSTANTS === 21 22 22 define( 'INIT_PLUGIN_SUITE_FX_ENGINE_VERSION', '2.0.1' );23 define( 'INIT_PLUGIN_SUITE_FX_ENGINE_SLUG', 'init-fx-engine' );24 define( 'INIT_PLUGIN_SUITE_FX_ENGINE_OPTION', 'init_plugin_suite_fx_engine_settings' );25 define( 'INIT_PLUGIN_SUITE_FX_ENGINE_URL', plugin_dir_url( __FILE__ ) );26 define( 'INIT_PLUGIN_SUITE_FX_ENGINE_PATH', plugin_dir_path( __FILE__ ) );27 define( 'INIT_PLUGIN_SUITE_FX_ENGINE_ASSETS_URL', INIT_PLUGIN_SUITE_FX_ENGINE_URL . 'assets/' );28 define( 'INIT_PLUGIN_SUITE_FX_ENGINE_ASSETS_PATH', INIT_PLUGIN_SUITE_FX_ENGINE_PATH . 'assets/' );29 define( 'INIT_PLUGIN_SUITE_FX_ENGINE_INCLUDES_PATH', INIT_PLUGIN_SUITE_FX_ENGINE_PATH . 'includes/' );23 define( 'INIT_PLUGIN_SUITE_FX_ENGINE_VERSION', '2.0.2' ); 24 define( 'INIT_PLUGIN_SUITE_FX_ENGINE_SLUG', 'init-fx-engine' ); 25 define( 'INIT_PLUGIN_SUITE_FX_ENGINE_OPTION', 'init_plugin_suite_fx_engine_settings' ); 26 define( 'INIT_PLUGIN_SUITE_FX_ENGINE_URL', plugin_dir_url( __FILE__ ) ); 27 define( 'INIT_PLUGIN_SUITE_FX_ENGINE_PATH', plugin_dir_path( __FILE__ ) ); 28 define( 'INIT_PLUGIN_SUITE_FX_ENGINE_ASSETS_URL', INIT_PLUGIN_SUITE_FX_ENGINE_URL . 'assets/' ); 29 define( 'INIT_PLUGIN_SUITE_FX_ENGINE_ASSETS_PATH', INIT_PLUGIN_SUITE_FX_ENGINE_PATH . 'assets/' ); 30 define( 'INIT_PLUGIN_SUITE_FX_ENGINE_INCLUDES_PATH', INIT_PLUGIN_SUITE_FX_ENGINE_PATH . 'includes/' ); 30 31 31 // === LOAD SHORTCODES ===32 // === LOAD MODULES === 32 33 34 require_once INIT_PLUGIN_SUITE_FX_ENGINE_INCLUDES_PATH . 'assets.php'; 33 35 require_once INIT_PLUGIN_SUITE_FX_ENGINE_INCLUDES_PATH . 'shortcodes.php'; 34 require_once INIT_PLUGIN_SUITE_FX_ENGINE_INCLUDES_PATH . 'settings-page.php'; 36 require_once INIT_PLUGIN_SUITE_FX_ENGINE_INCLUDES_PATH . 'snowfall.php'; 37 require_once INIT_PLUGIN_SUITE_FX_ENGINE_INCLUDES_PATH . 'seasonal.php'; 35 38 require_once INIT_PLUGIN_SUITE_FX_ENGINE_INCLUDES_PATH . 'blocks.php'; 36 require_once INIT_PLUGIN_SUITE_FX_ENGINE_INCLUDES_PATH . 'seasonal.php';37 require_once INIT_PLUGIN_SUITE_FX_ENGINE_INCLUDES_PATH . 'settings-seasonal.php';38 39 39 // === ENQUEUE JS ENGINE === 40 41 add_action( 'wp_enqueue_scripts', 'init_plugin_suite_fx_engine_enqueue_scripts' ); 42 function init_plugin_suite_fx_engine_enqueue_scripts() { 43 wp_enqueue_script( 44 'init-plugin-suite-fx-confetti', 45 INIT_PLUGIN_SUITE_FX_ENGINE_ASSETS_URL . 'js/canvas-confetti.min.js', 46 [], 47 INIT_PLUGIN_SUITE_FX_ENGINE_VERSION, 48 true 49 ); 50 51 wp_enqueue_script( 52 'init-plugin-suite-fx-engine', 53 INIT_PLUGIN_SUITE_FX_ENGINE_ASSETS_URL . 'js/fx-engine.js', 54 ['init-plugin-suite-fx-confetti'], 55 INIT_PLUGIN_SUITE_FX_ENGINE_VERSION, 56 true 57 ); 58 59 $inlinefmt = get_option('init_plugin_suite_fx_engine_inlinefmt', [ 60 'enabled' => true, 61 ]); 62 63 $i18n = [ 64 'tap_to_reveal' => __('Tap to reveal', 'init-fx-engine'), 65 ]; 66 67 wp_add_inline_script( 68 'init-plugin-suite-fx-engine', 69 'window.INIT_FX = window.INIT_FX || {}; 70 window.INIT_FX.inlinefmt = ' . wp_json_encode($inlinefmt) . '; 71 window.INIT_FX.i18n = ' . wp_json_encode($i18n) . '; 72 window.INIT_FX.assets = ' . wp_json_encode(init_plugin_suite_fx_engine_lazy_assets()) . ';', 73 'before' 74 ); 75 76 $raw_keywords = get_option('init_plugin_suite_fx_engine_keywords', []); 77 $raw_keywords = is_array($raw_keywords) ? $raw_keywords : []; 78 $mapped = []; 79 80 foreach ($raw_keywords as $effect => $raw) { 81 if (empty($raw)) continue; 82 83 if ($effect === 'emojiRain') { 84 $entries = array_map('trim', explode(',', $raw)); 85 foreach ($entries as $entry) { 86 [$kw, $emoji] = array_pad(explode(':', $entry, 2), 2, null); 87 if ($kw) { 88 $mapped[$effect][] = ['keyword' => $kw, 'emoji' => $emoji]; 89 } 90 } 91 } else { 92 $words = array_map('trim', explode(',', $raw)); 93 foreach ($words as $kw) { 94 if ($kw) { 95 $mapped[$effect][] = ['keyword' => $kw]; 96 } 97 } 98 } 99 } 100 101 // OK dùng localize cho data 102 wp_localize_script('init-plugin-suite-fx-engine', 'FX_KEYWORDS', $mapped); 40 if ( is_admin() ) { 41 require_once INIT_PLUGIN_SUITE_FX_ENGINE_INCLUDES_PATH . 'settings-page.php'; 42 require_once INIT_PLUGIN_SUITE_FX_ENGINE_INCLUDES_PATH . 'settings-seasonal.php'; 103 43 } 104 105 /**106 * Thư viện chỉ tải khi cần (lazy load) — fx-engine.js tự chèn <script> khi hiệu ứng được gọi.107 *108 * @return array109 */110 function init_plugin_suite_fx_engine_lazy_assets() {111 return [112 'fireworks' => INIT_PLUGIN_SUITE_FX_ENGINE_ASSETS_URL . 'js/fireworks.min.js?ver=' . rawurlencode( INIT_PLUGIN_SUITE_FX_ENGINE_VERSION ),113 ];114 }115 116 /**117 * PRELOADER - Anti-flash solution (FIXED VERSION)118 * - Che content ngay từ đầu bằng CSS critical119 * - Preloader show immediately, content hidden cho đến khi ready120 * - Fixed z-index và visibility issues121 */122 add_action('wp_head', function () {123 $preloader = get_option('init_plugin_suite_fx_engine_preloader', [124 'enabled' => false,125 'style' => 'dot',126 'bg' => '#ffffff',127 'session_once' => false128 ]);129 130 if (empty($preloader['enabled'])) {131 return;132 }133 134 $bg = esc_attr($preloader['bg']);135 $bgCSS = strpos($bg, ',') !== false ? "linear-gradient(to right, {$bg})" : $bg;136 137 // Critical CSS để che content ngay lập tức - FIXED VERSION138 ?>139 <style id="init-fx-critical-preloader">140 html.init-fx-preloading {141 overflow: hidden !important;142 }143 144 html.init-fx-preloading body {145 overflow: hidden !important;146 visibility: hidden !important;147 transition: visibility 0.3s ease !important;148 }149 150 html:not(.init-fx-preloading) body {151 visibility: visible !important;152 }153 154 html.init-fx-preloading #init-fx-preloader {155 visibility: visible !important;156 opacity: 1 !important;157 }158 159 #init-fx-preloader {160 position: fixed !important;161 top: 0 !important;162 left: 0 !important;163 width: 100vw !important;164 height: 100vh !important;165 background: <?php echo esc_attr($bgCSS); ?> !important;166 z-index: 999999999 !important; /* Fixed z-index */167 display: flex !important;168 align-items: center !important;169 justify-content: center !important;170 visibility: hidden;171 opacity: 1;172 transition: opacity 0.8s cubic-bezier(0.4, 0.0, 0.2, 1), visibility 0s linear 0.8s !important;173 }174 175 html.init-fx-preloading #wpadminbar {176 visibility: hidden !important;177 opacity: 0 !important;178 pointer-events: none !important;179 }180 181 @keyframes preloaderFadeOut {182 0% {183 opacity: 1;184 visibility: visible;185 transform: scale(1);186 }187 70% {188 opacity: 0.3;189 visibility: visible;190 transform: scale(1.02);191 }192 100% {193 opacity: 0;194 visibility: hidden;195 transform: scale(1);196 }197 }198 199 #init-fx-preloader.fx-animate-out {200 animation: preloaderFadeOut 1s cubic-bezier(0.25, 0.46, 0.45, 0.94) forwards !important;201 }202 203 #init-fx-preloader * {204 z-index: inherit !important;205 }206 </style>207 208 <script>209 (function() {210 document.documentElement.classList.add('init-fx-preloading');211 212 window.INIT_FX = window.INIT_FX || {};213 window.INIT_FX.preloader = <?php echo wp_json_encode($preloader); ?>;214 215 function createPreloaderImmediately() {216 var container = document.body || document.documentElement;217 218 var existing = document.getElementById('init-fx-preloader');219 if (!existing && container) {220 var preloader = document.createElement('div');221 preloader.id = 'init-fx-preloader';222 preloader.setAttribute('data-style', '<?php echo esc_js($preloader['style']); ?>');223 224 preloader.style.cssText = `225 position: fixed !important;226 top: 0 !important;227 left: 0 !important;228 width: 100vw !important;229 height: 100vh !important;230 background: <?php echo esc_js($bgCSS); ?> !important;231 z-index: 999999999 !important;232 display: flex !important;233 align-items: center !important;234 justify-content: center !important;235 visibility: visible !important;236 opacity: 1 !important;237 `;238 239 container.appendChild(preloader);240 return true;241 }242 return false;243 }244 245 if (!createPreloaderImmediately()) {246 var retryCount = 0;247 var retryInterval = setInterval(function() {248 if (createPreloaderImmediately() || retryCount++ > 20) {249 clearInterval(retryInterval);250 }251 }, 10);252 }253 })();254 </script>255 <?php256 }, 0); // Priority 0 để chạy đầu tiên257 258 // Enqueue JS để xử lý preloader animation259 add_action('wp_enqueue_scripts', function () {260 $preloader = get_option('init_plugin_suite_fx_engine_preloader', [261 'enabled' => false,262 'style' => 'dot',263 'bg' => '#ffffff',264 'session_once' => false, // thêm default luôn265 ]);266 267 if (empty($preloader['enabled'])) {268 return;269 }270 271 wp_enqueue_script(272 'init-plugin-suite-fx-preloader',273 INIT_PLUGIN_SUITE_FX_ENGINE_ASSETS_URL . 'js/fx-preloader.js',274 [],275 INIT_PLUGIN_SUITE_FX_ENGINE_VERSION,276 false // Load sớm ở header277 );278 279 // Truyền settings xuống JS280 wp_add_inline_script(281 'init-plugin-suite-fx-preloader',282 'window.INIT_FX = window.INIT_FX || {};283 window.INIT_FX.preloader = ' . wp_json_encode($preloader) . ';',284 'before'285 );286 });287 288 // Fallback nếu JS fail - auto remove preloader sau 3.5s289 add_action('wp_footer', function () {290 $preloader = get_option('init_plugin_suite_fx_engine_preloader', [291 'enabled' => false292 ]);293 294 if (empty($preloader['enabled'])) {295 return;296 }297 298 ?>299 <script>300 setTimeout(function() {301 document.documentElement.classList.remove('init-fx-preloading');302 var preloader = document.getElementById('init-fx-preloader');303 var criticalCSS = document.getElementById('init-fx-critical-preloader');304 305 if (preloader) preloader.remove();306 if (criticalCSS) criticalCSS.remove();307 }, 3500);308 </script>309 <?php310 }, 999);311 312 /**313 * SNOWFALL314 */315 add_action('wp_enqueue_scripts', function () {316 $snowfall = get_option('init_plugin_suite_fx_engine_snowfall', [317 'enabled' => false,318 'mode' => 'auto',319 'custom_start' => '',320 'custom_end' => '',321 'homepage_only' => false,322 ]);323 324 // Không bật thì thôi325 if (empty($snowfall['enabled'])) {326 return;327 }328 329 // Nếu chọn chỉ chạy ở trang chủ → thoát nếu không phải homepage330 // (static front page hoặc blog home — xem includes/seasonal.php)331 if (!empty($snowfall['homepage_only']) && !init_plugin_suite_fx_engine_is_homepage_request()) {332 return;333 }334 335 $should_run = false;336 $today = current_time('Y-m-d');337 338 if (($snowfall['mode'] ?? 'auto') === 'auto') {339 $year = current_time('Y');340 $start = "$year-12-20";341 $end = "$year-12-31";342 $should_run = ($today >= $start && $today <= $end);343 } elseif (($snowfall['mode'] ?? 'auto') === 'custom') {344 $start = sanitize_text_field($snowfall['custom_start'] ?? '');345 $end = sanitize_text_field($snowfall['custom_end'] ?? '');346 if ($start && $end) {347 $should_run = ($today >= $start && $today <= $end);348 }349 }350 351 if (!$should_run) {352 return;353 }354 355 // Normalize + default356 $snowfall = wp_parse_args($snowfall, [357 'enabled' => false,358 'mode' => 'auto',359 'custom_start' => '',360 'custom_end' => '',361 'homepage_only' => false,362 363 // Snow settings364 'amount' => 80,365 'size' => 4,366 'speed' => 1.2,367 'opacity' => 0.6,368 ]);369 370 // Hard clamp371 $snowfall['amount'] = max(20, min(200, (int) $snowfall['amount']));372 $snowfall['size'] = max(1, min(10, (int) $snowfall['size']));373 $snowfall['speed'] = max(0.3, min(5, (float) $snowfall['speed']));374 $snowfall['opacity'] = max(0.1, min(1, (float) $snowfall['opacity']));375 376 wp_enqueue_script(377 'init-plugin-suite-fx-particles',378 INIT_PLUGIN_SUITE_FX_ENGINE_ASSETS_URL . 'js/particles.min.js',379 [],380 INIT_PLUGIN_SUITE_FX_ENGINE_VERSION,381 true382 );383 384 wp_enqueue_script(385 'init-plugin-suite-fx-snowfall',386 INIT_PLUGIN_SUITE_FX_ENGINE_ASSETS_URL . 'js/fx-snowfall.js',387 ['init-plugin-suite-fx-particles'],388 INIT_PLUGIN_SUITE_FX_ENGINE_VERSION,389 true390 );391 392 // Chỉ truyền những thứ frontend cần393 $frontend_snowfall = [394 'amount' => $snowfall['amount'],395 'size' => $snowfall['size'],396 'speed' => $snowfall['speed'],397 'opacity' => $snowfall['opacity'],398 ];399 400 wp_add_inline_script(401 'init-plugin-suite-fx-snowfall',402 'window.INIT_FX = window.INIT_FX || {}; window.INIT_FX.snowfall = ' . wp_json_encode($frontend_snowfall) . ';',403 'before'404 );405 });406 407 /**408 * GRAYSCALE409 * - Chuyển sang enqueue thay vì in thẳng <style>/<script> ở footer410 */411 add_action('wp_enqueue_scripts', function () {412 $grayscale = get_option('init_plugin_suite_fx_engine_grayscale', [413 'enabled' => false,414 'mode' => 'off',415 'custom_start' => '',416 'custom_end' => ''417 ]);418 419 if (empty($grayscale['enabled'])) {420 return;421 }422 423 $today = current_time('Y-m-d');424 $mode = $grayscale['mode'] ?? 'off';425 $should_run = false;426 427 if ($mode === 'always') {428 $should_run = true;429 } elseif ($mode === 'custom') {430 $start = sanitize_text_field($grayscale['custom_start'] ?? '');431 $end = sanitize_text_field($grayscale['custom_end'] ?? '');432 if ($start && $end) {433 $should_run = ($today >= $start && $today <= $end);434 }435 }436 437 if (!$should_run) {438 return;439 }440 441 // Tạo handle “ảo” để gắn inline CSS/JS442 $handle = 'init-plugin-suite-fx-grayscale';443 444 wp_register_style($handle, false, [], INIT_PLUGIN_SUITE_FX_ENGINE_VERSION);445 wp_enqueue_style($handle);446 wp_add_inline_style(447 $handle,448 'body.init-fx-grayscale{filter:grayscale(100%) !important;-webkit-filter:grayscale(100%) !important;}'449 );450 451 wp_register_script($handle, false, [], INIT_PLUGIN_SUITE_FX_ENGINE_VERSION, true);452 wp_enqueue_script($handle);453 wp_add_inline_script(454 $handle,455 'document.addEventListener("DOMContentLoaded",function(){document.body.classList.add("init-fx-grayscale");});'456 );457 });458 44 459 45 // ========================== … … 461 47 // ========================== 462 48 463 add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'init_plugin_suite_fx_engine_add_settings_link'); 464 // Add a "Settings" link to the plugin row in the Plugins admin screen 465 function init_plugin_suite_fx_engine_add_settings_link($links) { 466 $settings_link = '<a href="' . admin_url('options-general.php?page=' . INIT_PLUGIN_SUITE_FX_ENGINE_SLUG) . '">' . __('Settings', 'init-fx-engine') . '</a>'; 467 array_unshift($links, $settings_link); 468 return $links; 49 add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'init_plugin_suite_fx_engine_add_settings_link' ); 50 /** 51 * Add a "Settings" link to the plugin row in the Plugins admin screen. 52 * 53 * @param array $links Existing action links. 54 * @return array 55 */ 56 function init_plugin_suite_fx_engine_add_settings_link( $links ) { 57 $settings_link = sprintf( 58 '<a href="%s">%s</a>', 59 esc_url( admin_url( 'options-general.php?page=' . INIT_PLUGIN_SUITE_FX_ENGINE_SLUG ) ), 60 esc_html__( 'Settings', 'init-fx-engine' ) 61 ); 62 63 array_unshift( $links, $settings_link ); 64 65 return $links; 469 66 }
Note:
See TracChangeset
for help on using the changeset viewer.