Files
amerike/wp-content/plugins/centro-de-funciones/functions/tailwindcss/admin-integration.php
T
2026-10-04 15:01:32 -06:00

158 lines
8.0 KiB
PHP

<?php
if (!defined('ABSPATH')) exit;
function alcastromol_tailwind_post_is_cacheable($post) {
$post = get_post($post);
if (!$post || $post->post_status !== 'publish') return false;
$options = alcastromol_get_cache_options();
$types = isset($options['post_types']) && is_array($options['post_types']) ? $options['post_types'] : [];
return in_array($post->post_type, $types, true);
}
function alcastromol_tailwind_get_post_cache_action_url($post_id, $context = 'admin') {
$post_id = absint($post_id);
$endpoint = admin_url('admin-post.php');
if ($context === 'front' && function_exists('pll_home_url')) {
$endpoint = trailingslashit(pll_home_url()) . 'wp-admin/admin-post.php';
}
$url = add_query_arg([
'action' => 'alcastromol_generate_tailwind_post_cache',
'post_id' => $post_id,
'context' => sanitize_key($context),
], $endpoint);
return wp_nonce_url($url, 'alcastromol_generate_tailwind_post_cache_' . $post_id);
}
function alcastromol_tailwind_add_post_row_action($actions, $post) {
if (function_exists('alcastromol_is_module_active') && !alcastromol_is_module_active('cache')) return $actions;
if (!current_user_can('manage_options') || !alcastromol_tailwind_post_is_cacheable($post)) return $actions;
$actions['alcastromol_tailwind_cache'] = sprintf(
'<a href="%s" aria-label="%s">%s</a>',
esc_url(alcastromol_tailwind_get_post_cache_action_url($post->ID, 'admin')),
esc_attr(sprintf('Generar cache Tailwind para %s', get_the_title($post))),
esc_html__('Generar cache Tailwind')
);
return $actions;
}
add_filter('post_row_actions', 'alcastromol_tailwind_add_post_row_action', 20, 2);
add_filter('page_row_actions', 'alcastromol_tailwind_add_post_row_action', 20, 2);
add_action('admin_post_alcastromol_generate_tailwind_post_cache', function () {
$post_id = absint($_REQUEST['post_id'] ?? 0);
$context = sanitize_key($_REQUEST['context'] ?? 'admin');
if (!$post_id || !current_user_can('manage_options') || !current_user_can('edit_post', $post_id)) {
wp_die('No tienes permisos para generar este cache.');
}
check_admin_referer('alcastromol_generate_tailwind_post_cache_' . $post_id);
$post = get_post($post_id);
if (!$post || !alcastromol_tailwind_post_is_cacheable($post)) {
wp_die('Este post type no está activo en la configuración de cache de Tailwind.');
}
$url = get_permalink($post_id);
$slug = alcastromol_get_post_cache_slug($post_id);
$ok = $url && $slug ? alcastromol_cache_url($url, $slug) : false;
if ($context === 'front') {
wp_redirect($url ?: home_url('/'));
exit;
}
$object = get_post_type_object($post->post_type);
$fallback = ($object && $object->show_ui)
? add_query_arg('post_type', $post->post_type, admin_url('edit.php'))
: admin_url('edit.php');
$redirect = wp_get_referer() ?: $fallback;
$redirect = add_query_arg([
'tailwind_post_cache' => $ok ? 'generated' : 'failed',
'tailwind_post_id' => $post_id,
], $redirect);
wp_safe_redirect($redirect);
exit;
});
add_action('admin_notices', function () {
if (empty($_GET['tailwind_post_cache'])) return;
$status = sanitize_key($_GET['tailwind_post_cache']);
$post_id = absint($_GET['tailwind_post_id'] ?? 0);
if ($status === 'generated') {
echo '<div class="notice notice-success is-dismissible"><p><strong>Cache Tailwind generado.</strong> ';
echo $post_id ? 'Entrada #' . esc_html($post_id) . '.' : '';
echo '</p></div>';
} else {
echo '<div class="notice notice-error is-dismissible"><p><strong>No se pudo generar el cache Tailwind de la entrada.</strong></p></div>';
}
});
add_action('admin_bar_menu', function ($bar) {
if (!current_user_can('manage_options')) return;
$bar->add_node([
'id' => 'alcastromol-tailwind',
'title' => 'Centro de funciones',
'href' => admin_url('admin.php?page=alcastromol-functions'),
'meta' => ['class' => 'alcastromol-tailwind-admin-bar'],
]);
$bar->add_node([
'id' => 'alcastromol-functions-home',
'parent' => 'alcastromol-tailwind',
'title' => 'Inicio',
'href' => admin_url('admin.php?page=alcastromol-functions'),
]);
$bar->add_node([
'id' => 'alcastromol-tailwind-mode',
'parent' => 'alcastromol-tailwind',
'title' => 'Tailwind v3',
'href' => admin_url('admin.php?page=alcastromol-tailwind-mode'),
]);
$bar->add_node([
'id' => 'alcastromol-tailwind-cache',
'parent' => 'alcastromol-tailwind',
'title' => 'Cache de Tailwind',
'href' => admin_url('admin.php?page=tailwind-cache'),
]);
if (!is_admin() && is_singular()) {
$post_id = get_queried_object_id();
if ($post_id && alcastromol_tailwind_post_is_cacheable($post_id) && (!function_exists('alcastromol_is_module_active') || alcastromol_is_module_active('cache'))) {
$bar->add_node([
'id' => 'alcastromol-tailwind-current-post-cache',
'parent' => 'alcastromol-tailwind',
'title' => 'Generar cache de esta entrada',
'href' => alcastromol_tailwind_get_post_cache_action_url($post_id, 'front'),
]);
}
}
}, 90);
function alcastromol_tailwind_render_compile_section() {
$mode = alcastromol_get_tailwind_mode();
$cli = alcastromol_tailwind_get_cli_path();
$result = get_transient('alcastromol_tailwind_compile_result_' . get_current_user_id());
if ($result) delete_transient('alcastromol_tailwind_compile_result_' . get_current_user_id());
?>
<hr style="margin:28px 0;">
<h2>Compilar CSS de Tailwind</h2>
<p>Genera el archivo CSS productivo utilizando el cache HTML actual y Tailwind Standalone CLI.</p>
<?php if (is_array($result)) : ?>
<div class="notice <?php echo !empty($result['success']) ? 'notice-success' : 'notice-error'; ?> inline" style="max-width:900px;">
<p><strong><?php echo esc_html($result['message'] ?? 'Proceso terminado.'); ?></strong></p>
<?php if (!empty($result['file_size'])) : ?>
<p>Tamaño: <code><?php echo esc_html($result['file_size']); ?></code>
<?php if (!empty($result['modified'])) : ?> · Modificado: <code><?php echo esc_html($result['modified']); ?></code><?php endif; ?></p>
<?php endif; ?>
<?php if (!empty($result['stderr'])) : ?><pre style="white-space:pre-wrap;"><?php echo esc_html($result['stderr']); ?></pre><?php endif; ?>
</div>
<?php endif; ?>
<table class="widefat striped" style="max-width:900px; margin:14px 0 16px;"><tbody>
<tr><td style="width:220px;"><strong>Modo actual</strong></td><td><code><?php echo esc_html($mode); ?></code>
<?php if ($mode !== 'production') : ?> · <a href="<?php echo esc_url(admin_url('admin.php?page=alcastromol-tailwind-mode')); ?>">Cambiar a producción</a><?php endif; ?></td></tr>
<tr><td><strong>CLI detectado</strong></td><td><?php if ($cli) : ?><code><?php echo esc_html($cli); ?></code><?php else : ?><span style="color:#b32d2e;">No detectado</span><?php endif; ?></td></tr>
<tr><td><strong>CSS productivo</strong></td><td><code><?php echo esc_html(ALCASTROMOL_TAILWIND_OUTPUT_CSS); ?></code>
<?php if (file_exists(ALCASTROMOL_TAILWIND_OUTPUT_CSS)) : ?><br><?php echo esc_html(size_format(filesize(ALCASTROMOL_TAILWIND_OUTPUT_CSS))); ?> · <?php echo esc_html(date_i18n('d/m/Y g:i a', filemtime(ALCASTROMOL_TAILWIND_OUTPUT_CSS))); ?><?php endif; ?></td></tr>
</tbody></table>
<form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
<?php wp_nonce_field('alcastromol_compile_tailwind_cli_nonce'); ?>
<input type="hidden" name="action" value="alcastromol_compile_tailwind_cli">
<?php submit_button('Generar CSS compilado', 'primary', 'submit', false, (!$cli || $mode !== 'production') ? ['disabled'=>'disabled'] : []); ?>
</form>
<?php
}