Files
2026-09-28 16:09:57 -06:00

429 lines
13 KiB
PHP

<?php
if (!defined('ABSPATH')) {
exit;
}
/*
|--------------------------------------------------------------------------
| Anime.js por CDN
|--------------------------------------------------------------------------
|
| Carga Anime.js 4 desde jsDelivr. Cada subpath oficial se activa aparte,
| así el sitio solo descarga los módulos encendidos.
|
*/
define('ALCASTROMOL_ANIMEJS_VERSION', '4.3.6');
define('ALCASTROMOL_ANIMEJS_OPTION', 'alcastromol_animejs_imports');
function alcastromol_animejs_catalog() {
return [
'animation' => [
'label' => 'animate',
'import' => "import { animate } from 'animejs/animation'",
'path' => 'animation/index.js',
'desc' => 'Animaciones con animate().',
],
'text' => [
'label' => 'text',
'import' => "import { splitText } from 'animejs/text'",
'path' => 'text/index.js',
'desc' => 'Parte títulos y párrafos en líneas, palabras o letras.',
],
'utils' => [
'label' => 'utils',
'import' => "import { stagger, random } from 'animejs/utils'",
'path' => 'utils/index.js',
'desc' => 'stagger, random y utilidades de tiempo y targets.',
],
'timeline' => [
'label' => 'timeline',
'import' => "import { createTimeline } from 'animejs/timeline'",
'path' => 'timeline/index.js',
'desc' => 'Secuencias con createTimeline().',
],
'timer' => [
'label' => 'timer',
'import' => "import { createTimer } from 'animejs/timer'",
'path' => 'timer/index.js',
'desc' => 'Temporizadores con createTimer().',
],
'animatable' => [
'label' => 'animatable',
'import' => "import { createAnimatable } from 'animejs/animatable'",
'path' => 'animatable/index.js',
'desc' => 'Valores animables que se actualizan al vuelo.',
],
'draggable' => [
'label' => 'draggable',
'import' => "import { createDraggable } from 'animejs/draggable'",
'path' => 'draggable/index.js',
'desc' => 'Arrastre con createDraggable().',
],
'layout' => [
'label' => 'layout',
'import' => "import { createLayout } from 'animejs/layout'",
'path' => 'layout/index.js',
'desc' => 'Transiciones de layout con createLayout().',
],
'scope' => [
'label' => 'scope',
'import' => "import { createScope } from 'animejs/scope'",
'path' => 'scope/index.js',
'desc' => 'Agrupa animaciones y las limpia con el scope.',
],
'engine' => [
'label' => 'engine',
'import' => "import { engine } from 'animejs/engine'",
'path' => 'engine/index.js',
'desc' => 'Control del motor: pausa, tiempo y velocidad.',
],
'easings' => [
'label' => 'easings',
'import' => "import * as easings from 'animejs/easings'",
'path' => 'easings/index.js',
'desc' => 'Curvas, spring y cubic-bezier.',
],
'svg' => [
'label' => 'svg',
'import' => "import * as svg from 'animejs/svg'",
'path' => 'svg/index.js',
'desc' => 'Motion path, morph y drawable.',
],
'waapi' => [
'label' => 'waapi',
'import' => "import { waapi } from 'animejs/waapi'",
'path' => 'waapi/index.js',
'desc' => 'animate() ligero sobre la Web Animations API.',
],
'events' => [
'label' => 'events',
'import' => "import * as events from 'animejs/events'",
'path' => 'events/index.js',
'desc' => 'Scroll y otros observadores de eventos.',
],
];
}
function alcastromol_animejs_default_imports() {
$defaults = [];
foreach (alcastromol_animejs_catalog() as $id => $item) {
$defaults[$id] = $id === 'animation' ? '1' : '0';
}
return $defaults;
}
function alcastromol_animejs_get_imports() {
$saved = get_option(ALCASTROMOL_ANIMEJS_OPTION, null);
$defaults = alcastromol_animejs_default_imports();
if (!is_array($saved)) {
return $defaults;
}
$imports = $defaults;
foreach ($defaults as $id => $value) {
if (array_key_exists($id, $saved)) {
$imports[$id] = $saved[$id] === '1' ? '1' : '0';
}
}
return $imports;
}
function alcastromol_animejs_is_import_active($id) {
$imports = alcastromol_animejs_get_imports();
return isset($imports[$id]) && $imports[$id] === '1';
}
function alcastromol_animejs_module_url($path) {
return 'https://cdn.jsdelivr.net/npm/animejs@' . ALCASTROMOL_ANIMEJS_VERSION . '/dist/modules/' . ltrim($path, '/');
}
function alcastromol_animejs_should_load() {
if (is_admin() || wp_doing_ajax() || wp_doing_cron()) {
return false;
}
if (defined('REST_REQUEST') && REST_REQUEST) {
return false;
}
if (is_feed() || is_embed() || is_customize_preview()) {
return false;
}
if (function_exists('alcastromol_is_module_active') && !alcastromol_is_module_active('animejs')) {
return false;
}
if (function_exists('alcastromol_is_tailwind_builder_context') && alcastromol_is_tailwind_builder_context()) {
return false;
}
return true;
}
function alcastromol_animejs_enabled_ids() {
$enabled = [];
foreach (alcastromol_animejs_get_imports() as $id => $active) {
if ($active === '1' && isset(alcastromol_animejs_catalog()[$id])) {
$enabled[] = $id;
}
}
return $enabled;
}
function alcastromol_animejs_print() {
if (!alcastromol_animejs_should_load()) {
return;
}
$catalog = alcastromol_animejs_catalog();
$enabled = alcastromol_animejs_enabled_ids();
if (!$enabled) {
return;
}
$imports = [];
$pairs = [];
foreach ($enabled as $id) {
$alias = 'mod_' . $id;
$url = alcastromol_animejs_module_url($catalog[$id]['path']);
$imports[] = 'import * as ' . $alias . " from '" . esc_url($url) . "';";
$pairs[] = $id . ': ' . $alias;
}
echo "<script type=\"module\" id=\"alcastromol-animejs\">\n";
echo implode("\n", $imports) . "\n";
echo "const modules = { " . implode(', ', $pairs) . " };\n";
echo "const api = { modules };\n";
echo "for (const mod of Object.values(modules)) { Object.assign(api, mod); }\n";
echo "window.animejs = api;\n";
echo "window.dispatchEvent(new CustomEvent('animejs:ready'));\n";
echo "</script>\n";
}
add_action('wp_head', 'alcastromol_animejs_print', 20);
add_action('admin_menu', function () {
add_submenu_page(
'alcastromol-functions',
'Anime.js',
'Anime.js',
'manage_options',
'alcastromol-animejs',
'alcastromol_animejs_admin_page'
);
}, 22);
add_action('admin_post_alcastromol_animejs_save', function () {
if (!current_user_can('manage_options')) {
wp_die('No tienes permisos.');
}
check_admin_referer('alcastromol_animejs_save');
$posted = isset($_POST['imports']) && is_array($_POST['imports']) ? wp_unslash($_POST['imports']) : [];
$imports = alcastromol_animejs_default_imports();
foreach ($imports as $id => $value) {
$imports[$id] = !empty($posted[$id]) ? '1' : '0';
}
update_option(ALCASTROMOL_ANIMEJS_OPTION, $imports);
wp_safe_redirect(admin_url('admin.php?page=alcastromol-animejs&updated=1'));
exit;
});
function alcastromol_animejs_admin_page() {
if (!current_user_can('manage_options')) {
return;
}
$catalog = alcastromol_animejs_catalog();
$imports = alcastromol_animejs_get_imports();
$active = function_exists('alcastromol_is_module_active') && alcastromol_is_module_active('animejs');
?>
<div class="wrap alcastromol-animejs">
<h1>Anime.js</h1>
<p class="alcastromol-animejs-intro">
Anime.js <?php echo esc_html(ALCASTROMOL_ANIMEJS_VERSION); ?> entra desde jsDelivr.
Enciende solo los imports que el sitio va a usar. Quedan en <code>window.animejs</code> cuando dispara <code>animejs:ready</code>.
</p>
<?php if (!$active) : ?>
<div class="notice notice-warning">
<p>Anime.js está apagado. Actívalo en Centro de funciones para cargarlo en el sitio.</p>
</div>
<?php endif; ?>
<?php if (isset($_GET['updated']) && $_GET['updated'] === '1') : ?>
<div class="notice notice-success is-dismissible">
<p>Imports de Anime.js guardados.</p>
</div>
<?php endif; ?>
<form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
<?php wp_nonce_field('alcastromol_animejs_save'); ?>
<input type="hidden" name="action" value="alcastromol_animejs_save">
<div class="alcastromol-animejs-list">
<?php foreach ($catalog as $id => $item) : ?>
<?php $on = !empty($imports[$id]) && $imports[$id] === '1'; ?>
<label class="alcastromol-animejs-row<?php echo $on ? ' is-on' : ''; ?>">
<span class="alcastromol-animejs-copy">
<strong><?php echo esc_html($item['label']); ?></strong>
<code><?php echo esc_html($item['import']); ?></code>
<span><?php echo esc_html($item['desc']); ?></span>
</span>
<span class="alcastromol-switch">
<input type="checkbox" name="imports[<?php echo esc_attr($id); ?>]" value="1" <?php checked($on); ?>>
<span class="track"></span>
</span>
</label>
<?php endforeach; ?>
</div>
<?php submit_button('Guardar imports'); ?>
</form>
<p class="alcastromol-animejs-use">
<code>document.addEventListener('animejs:ready', function () { animejs.animate('.card', { opacity: [0, 1], y: [22, 0] }); });</code>
</p>
</div>
<style>
.alcastromol-animejs {
max-width: 860px;
}
.alcastromol-animejs-intro {
color: #646970;
font-size: 14px;
}
.alcastromol-animejs-list {
display: flex;
flex-direction: column;
gap: 8px;
margin: 16px 0 8px;
}
.alcastromol-animejs-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
padding: 14px 16px;
background: #fff;
border: 1px solid #e7eaee;
border-radius: 10px;
}
.alcastromol-animejs-row.is-on {
border-color: #fdba74;
}
.alcastromol-animejs-copy {
display: flex;
flex-direction: column;
gap: 3px;
min-width: 0;
}
.alcastromol-animejs-copy strong {
color: #1d2327;
font-size: 14px;
}
.alcastromol-animejs-copy code {
font-size: 12px;
background: transparent;
padding: 0;
}
.alcastromol-animejs-copy span {
color: #646970;
font-size: 13px;
}
.alcastromol-animejs-use {
margin-top: 8px;
}
.alcastromol-animejs-use code {
display: block;
padding: 12px 14px;
white-space: normal;
}
.alcastromol-switch {
position: relative;
display: inline-block;
width: 38px;
height: 22px;
flex: 0 0 auto;
}
.alcastromol-switch input {
position: absolute;
opacity: 0;
width: 0;
height: 0;
}
.alcastromol-switch .track {
position: absolute;
inset: 0;
background: #c3c4c7;
border-radius: 999px;
cursor: pointer;
transition: background .15s ease;
}
.alcastromol-switch .track:before {
content: "";
position: absolute;
width: 16px;
height: 16px;
left: 3px;
top: 3px;
background: #fff;
border-radius: 50%;
box-shadow: 0 1px 2px rgba(0, 0, 0, .18);
transition: transform .15s ease;
}
.alcastromol-switch input:checked + .track {
background: #ea580c;
}
.alcastromol-switch input:checked + .track:before {
transform: translateX(16px);
}
.alcastromol-switch input:focus-visible + .track {
outline: 2px solid #2271b1;
outline-offset: 2px;
}
</style>
<?php
}