Files
centro-de-funciones/functions/admin/modules-dashboard.php
T
2026-09-28 16:09:57 -06:00

411 lines
15 KiB
PHP

<?php
if (!defined('ABSPATH')) {
exit;
}
/*
|--------------------------------------------------------------------------
| Módulos del centro de funciones
|--------------------------------------------------------------------------
*/
function alcastromol_default_modules() {
return [
'tailwind' => '1',
'cache' => '1',
'optimizer' => '0',
'animejs' => '0',
];
}
function alcastromol_get_modules() {
$saved = get_option('alcastromol_modules', []);
if (!is_array($saved)) {
$saved = [];
}
return array_merge(alcastromol_default_modules(), $saved);
}
function alcastromol_is_module_active($module) {
$modules = alcastromol_get_modules();
return isset($modules[$module]) && $modules[$module] === '1';
}
function alcastromol_module_icon($name) {
$icons = [
'tailwind' => '<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M12 6.2c-2.6 0-4.2 1.3-5 3.8 1.1-1.5 2.4-2.1 4-1.7.9.2 1.6.8 2.3 1.5.9 1 1.9 1.9 3.7 1.9 2.6 0 4.2-1.3 5-3.8-1.1 1.5-2.4 2.1-4 1.7-.9-.2-1.6-.8-2.3-1.5-.9-1-1.9-1.9-3.7-1.9z"/><path d="M6.3 12.1c-2.6 0-4.2 1.3-5 3.8 1.1-1.5 2.4-2.1 4-1.7.9.2 1.6.8 2.3 1.5.9 1 1.9 1.9 3.7 1.9 2.6 0 4.2-1.3 5-3.8-1.1 1.5-2.4 2.1-4 1.7-.9-.2-1.6-.8-2.3-1.5-.9-1-1.9-1.9-3.7-1.9z"/></svg>',
'cache' => '<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" aria-hidden="true"><ellipse cx="12" cy="6.5" rx="6.5" ry="2.6"/><path d="M5.5 6.5v5.2c0 1.5 2.9 2.6 6.5 2.6s6.5-1.1 6.5-2.6V6.5"/><path d="M5.5 11.7v5.2c0 1.5 2.9 2.6 6.5 2.6s6.5-1.1 6.5-2.6v-5.2"/></svg>',
'optimizer' => '<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M14 3.5H7.2A1.7 1.7 0 0 0 5.5 5.2v13.6A1.7 1.7 0 0 0 7.2 20.5h9.6a1.7 1.7 0 0 0 1.7-1.7V8z"/><path d="M14 3.5V8h4.5"/><path d="M12 11.2v5.2"/><path d="M9.6 14.6 12 17l2.4-2.4"/></svg>',
'animejs' => '<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 16c2.2-6 4.2-9 8-9s5.8 3 8 9"/><path d="M8 16.5 12 8l4 8.5"/></svg>',
];
return isset($icons[$name]) ? $icons[$name] : '';
}
function alcastromol_get_module_cards() {
return [
'tailwind' => [
'title' => 'Tailwind v3',
'desc' => 'Carga Tailwind CSS v3 en el sitio. En desarrollo usa el CDN y en producción el CSS compilado, con la configuración del tema.',
'settings' => admin_url('admin.php?page=alcastromol-tailwind-mode'),
'bg' => '#e8f3ff',
'color' => '#3b82f6',
'icon' => 'tailwind',
'badge' => '',
'locked' => false,
],
'cache' => [
'title' => 'Cache de Tailwind',
'desc' => 'Guarda el HTML del sitio para que Tailwind detecte las clases de páginas, entradas, productos, taxonomías y templates.',
'settings' => admin_url('admin.php?page=tailwind-cache'),
'bg' => '#f4e9ff',
'color' => '#9333ea',
'icon' => 'cache',
'badge' => '',
'locked' => false,
],
'optimizer' => [
'title' => 'Optimizador de archivos',
'desc' => 'Detecta los CSS y JS del sitio. Puedes retirar su URL, agrupar el CSS crítico y el no crítico, juntar el JS en el header, o marcar data-nowprocket y media print.',
'settings' => admin_url('admin.php?page=alcastromol-optimizer'),
'bg' => '#e7f8ef',
'color' => '#16a34a',
'icon' => 'optimizer',
'badge' => '',
'locked' => false,
],
'animejs' => [
'title' => 'Anime.js',
'desc' => 'Carga Anime.js 4 desde el CDN. Cada import (animate, text, utils y el resto) se activa por separado y solo entra al sitio el que esté encendido.',
'settings' => admin_url('admin.php?page=alcastromol-animejs'),
'bg' => '#fff1e8',
'color' => '#ea580c',
'icon' => 'animejs',
'badge' => '',
'locked' => false,
],
];
}
add_action('admin_menu', function () {
add_menu_page(
'Centro de funciones',
'Centro de funciones',
'manage_options',
'alcastromol-functions',
'alcastromol_functions_dashboard_page',
'dashicons-screenoptions',
58
);
add_submenu_page(
'alcastromol-functions',
'Centro de funciones',
'Inicio',
'manage_options',
'alcastromol-functions',
'alcastromol_functions_dashboard_page'
);
}, 9);
add_action('wp_ajax_alcastromol_toggle_module', function () {
if (!current_user_can('manage_options')) {
wp_send_json_error(['message' => 'No tienes permisos.'], 403);
}
check_ajax_referer('alcastromol_toggle_module', 'nonce');
$module = sanitize_key($_POST['module'] ?? '');
$active = ($_POST['active'] ?? '0') === '1' ? '1' : '0';
$allowed = ['tailwind', 'cache', 'optimizer', 'animejs'];
if (!in_array($module, $allowed, true)) {
wp_send_json_error(['message' => 'Esta función todavía no se puede activar.'], 400);
}
$modules = alcastromol_get_modules();
$modules[$module] = $active;
update_option('alcastromol_modules', $modules);
wp_send_json_success([
'module' => $module,
'active' => $active,
]);
});
function alcastromol_functions_dashboard_page() {
if (!current_user_can('manage_options')) {
return;
}
$modules = alcastromol_get_modules();
$cards = alcastromol_get_module_cards();
$nonce = wp_create_nonce('alcastromol_toggle_module');
?>
<div class="wrap alcastromol-functions-wrap">
<h1>Centro de funciones</h1>
<p class="alcastromol-functions-intro">Activa las funciones del plugin y abre sus ajustes.</p>
<div class="alcastromol-modules" data-nonce="<?php echo esc_attr($nonce); ?>">
<?php foreach ($cards as $id => $card) : ?>
<?php
$active = !empty($modules[$id]) && $modules[$id] === '1';
$locked = !empty($card['locked']);
?>
<article class="alcastromol-module<?php echo $active ? ' is-active' : ''; ?><?php echo $locked ? ' is-locked' : ''; ?>">
<div class="alcastromol-module-icon" style="background:<?php echo esc_attr($card['bg']); ?>;color:<?php echo esc_attr($card['color']); ?>;">
<?php echo alcastromol_module_icon($card['icon']); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</div>
<header>
<h2>
<?php echo esc_html($card['title']); ?>
<?php if ($card['badge'] !== '') : ?>
<span class="alcastromol-badge"><?php echo esc_html($card['badge']); ?></span>
<?php endif; ?>
</h2>
<p><?php echo esc_html($card['desc']); ?></p>
</header>
<div class="alcastromol-module-status">
<?php if ($card['settings'] !== '') : ?>
<a class="button button-secondary" href="<?php echo esc_url($card['settings']); ?>">Ajustes</a>
<?php else : ?>
<span class="button button-secondary disabled" aria-disabled="true">Ajustes</span>
<?php endif; ?>
<label class="alcastromol-switch">
<input
class="alcastromol-module-toggle"
type="checkbox"
value="<?php echo esc_attr($id); ?>"
<?php checked($active); ?>
<?php disabled($locked); ?>
aria-label="<?php echo esc_attr($card['title']); ?>"
>
<span class="track"></span>
</label>
</div>
</article>
<?php endforeach; ?>
</div>
</div>
<style>
body.toplevel_page_alcastromol-functions #wpcontent {
background: #f3f5f7;
}
.alcastromol-functions-wrap {
max-width: 1280px;
}
.alcastromol-functions-wrap > h1 {
font-weight: 600;
padding: 8px 0 0;
}
.alcastromol-functions-intro {
margin: 4px 0 18px;
color: #646970;
font-size: 14px;
}
.alcastromol-modules {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 16px;
}
.alcastromol-module {
display: flex;
flex-direction: column;
min-height: 248px;
padding: 22px 20px 16px;
background: #fff;
border: 1px solid #e7eaee;
border-radius: 10px;
box-shadow: 0 1px 2px rgba(16, 24, 40, 0.04);
}
.alcastromol-module-icon {
display: flex;
align-items: center;
justify-content: center;
width: 46px;
height: 46px;
border-radius: 12px;
}
.alcastromol-module header {
margin-top: 16px;
}
.alcastromol-module h2 {
margin: 0 0 8px;
font-size: 16px;
line-height: 1.3;
font-weight: 650;
color: #1d2327;
}
.alcastromol-badge {
display: inline-block;
margin-left: 6px;
padding: 2px 6px;
border-radius: 4px;
background: #f43f5e;
color: #fff;
font-size: 10px;
font-weight: 700;
letter-spacing: .03em;
vertical-align: middle;
}
.alcastromol-module header p {
margin: 0;
min-height: 66px;
color: #646970;
font-size: 13px;
line-height: 1.55;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}
.alcastromol-module-status {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: auto;
padding-top: 18px;
}
.alcastromol-module-status .button.disabled {
opacity: .55;
pointer-events: none;
}
.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: #2ea3f7;
}
.alcastromol-switch input:checked + .track:before {
transform: translateX(16px);
}
.alcastromol-switch input:focus-visible + .track {
outline: 2px solid #2271b1;
outline-offset: 2px;
}
.alcastromol-switch input:disabled + .track {
opacity: .5;
cursor: not-allowed;
}
.alcastromol-module.is-saving {
opacity: .72;
}
@media (max-width: 1400px) {
.alcastromol-modules {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
}
@media (max-width: 960px) {
.alcastromol-modules {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
@media (max-width: 782px) {
.alcastromol-modules {
grid-template-columns: 1fr;
}
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function () {
var grid = document.querySelector('.alcastromol-modules');
if (!grid || typeof ajaxurl === 'undefined') {
return;
}
grid.addEventListener('change', function (event) {
var input = event.target;
if (!input.classList || !input.classList.contains('alcastromol-module-toggle') || input.disabled) {
return;
}
var card = input.closest('.alcastromol-module');
var checked = input.checked;
card.classList.toggle('is-active', checked);
card.classList.add('is-saving');
var data = new FormData();
data.append('action', 'alcastromol_toggle_module');
data.append('nonce', grid.getAttribute('data-nonce'));
data.append('module', input.value);
data.append('active', checked ? '1' : '0');
fetch(ajaxurl, {
method: 'POST',
credentials: 'same-origin',
body: data
}).then(function (response) {
return response.json();
}).then(function (json) {
if (!json || !json.success) {
throw new Error('toggle failed');
}
}).catch(function () {
input.checked = !checked;
card.classList.toggle('is-active', input.checked);
}).finally(function () {
card.classList.remove('is-saving');
});
});
});
</script>
<?php
}