diff --git a/.gitignore b/.gitignore
index 2cd3041..ca9155e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,4 @@
# Cache
-/functions/tailwindcss
/functions/tailwindcss/css
/functions/tailwindcss/css-production
diff --git a/functions/tailwindcss/admin-integration.php b/functions/tailwindcss/admin-integration.php
new file mode 100644
index 0000000..8746f2e
--- /dev/null
+++ b/functions/tailwindcss/admin-integration.php
@@ -0,0 +1,157 @@
+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(
+ '%s',
+ 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 '
Cache Tailwind generado. ';
+ echo $post_id ? 'Entrada #' . esc_html($post_id) . '.' : '';
+ echo '
';
+ } else {
+ echo 'No se pudo generar el cache Tailwind de la entrada.
';
+ }
+});
+
+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());
+ ?>
+
+ Compilar CSS de Tailwind
+ Genera el archivo CSS productivo utilizando el cache HTML actual y Tailwind Standalone CLI.
+
+
+
+
+
Tamaño:
+ · Modificado:
+
+
+
+
+
+
+ 1,
+ 'post_types' => [],
+ 'taxonomies' => [],
+ 'authors' => 0,
+ 'oxygen_templates' => alcastromol_is_oxygen_enabled() ? 1 : 0,
+ ];
+
+}
+
+
+/*
+|--------------------------------------------------------------------------
+| Obtener opciones guardadas
+|--------------------------------------------------------------------------
+*/
+
+function alcastromol_get_cache_options() {
+
+ $saved = get_option(ALCASTROMOL_TAILWIND_CACHE_OPTIONS, []);
+
+ if (!is_array($saved)) {
+ $saved = [];
+ }
+
+ return wp_parse_args($saved, alcastromol_get_default_cache_options());
+
+}
+
+
+/*
+|--------------------------------------------------------------------------
+| Guardar opciones
+|--------------------------------------------------------------------------
+*/
+
+function alcastromol_save_cache_options($options) {
+
+ $clean = alcastromol_get_default_cache_options();
+
+ $clean['home'] = !empty($options['home']) ? 1 : 0;
+
+ $clean['post_types'] = [];
+
+ if (!empty($options['post_types']) && is_array($options['post_types'])) {
+ foreach ($options['post_types'] as $post_type) {
+ $clean['post_types'][] = sanitize_key($post_type);
+ }
+ }
+
+ $clean['taxonomies'] = [];
+
+ if (!empty($options['taxonomies']) && is_array($options['taxonomies'])) {
+ foreach ($options['taxonomies'] as $taxonomy) {
+ $clean['taxonomies'][] = sanitize_key($taxonomy);
+ }
+ }
+
+ $clean['authors'] = !empty($options['authors']) ? 1 : 0;
+
+ $clean['oxygen_templates'] = (alcastromol_is_oxygen_enabled() && !empty($options['oxygen_templates'])) ? 1 : 0;
+
+ update_option(ALCASTROMOL_TAILWIND_CACHE_OPTIONS, $clean);
+
+ return $clean;
+
+}
+
+
+/*
+|--------------------------------------------------------------------------
+| Detectar post types cacheables
+|--------------------------------------------------------------------------
+*/
+
+function alcastromol_detect_cacheable_post_types() {
+
+ $post_types = get_post_types([
+ 'public' => true,
+ ], 'objects');
+
+ $exclude = [
+ 'attachment',
+ ];
+
+ $items = [];
+
+ foreach ($post_types as $post_type => $object) {
+
+ if (in_array($post_type, $exclude, true)) {
+ continue;
+ }
+
+ $items[$post_type] = [
+ 'name' => $post_type,
+ 'label' => $object->labels->name ?? $post_type,
+ ];
+
+ }
+
+ if (post_type_exists('product') && !isset($items['product'])) {
+ $items['product'] = [
+ 'name' => 'product',
+ 'label' => 'Productos WooCommerce',
+ ];
+ }
+
+ return $items;
+
+}
+
+
+/*
+|--------------------------------------------------------------------------
+| Detectar taxonomías cacheables
+|--------------------------------------------------------------------------
+*/
+
+function alcastromol_detect_cacheable_taxonomies() {
+
+ $taxonomies = get_taxonomies([
+ 'public' => true,
+ ], 'objects');
+
+ $items = [];
+
+ foreach ($taxonomies as $taxonomy => $object) {
+
+ $items[$taxonomy] = [
+ 'name' => $taxonomy,
+ 'label' => $object->labels->name ?? $taxonomy,
+ ];
+
+ }
+
+ return $items;
+
+}
+
+
+/*
+|--------------------------------------------------------------------------
+| Obtener HTML final renderizado
+|--------------------------------------------------------------------------
+*/
+
+function alcastromol_fetch_html($url) {
+
+ $response = wp_remote_get($url, [
+ 'timeout' => 20,
+ 'sslverify' => false,
+ 'headers' => [
+ 'User-Agent' => 'Alcastromol Tailwind Cache Generator',
+ ],
+ ]);
+
+ if (is_wp_error($response)) {
+ return false;
+ }
+
+ $code = wp_remote_retrieve_response_code($response);
+
+ if ($code < 200 || $code >= 400) {
+ return false;
+ }
+
+ $html = wp_remote_retrieve_body($response);
+
+ if (!$html) {
+ return false;
+ }
+
+ return $html;
+
+}
+
+
+/*
+|--------------------------------------------------------------------------
+| Limpiar HTML
+|--------------------------------------------------------------------------
+*/
+
+function alcastromol_clean_html($html) {
+
+ $html = preg_replace('//', '', $html);
+
+ return $html;
+
+}
+
+
+/*
+|--------------------------------------------------------------------------
+| Guardar cache
+|--------------------------------------------------------------------------
+*/
+
+function alcastromol_save_cache($slug, $html) {
+
+ if (!file_exists(ALCASTROMOL_TAILWIND_CACHE)) {
+ wp_mkdir_p(ALCASTROMOL_TAILWIND_CACHE);
+ }
+
+ $file = ALCASTROMOL_TAILWIND_CACHE . sanitize_file_name($slug) . '.html';
+
+ file_put_contents($file, $html);
+
+}
+
+
+/*
+|--------------------------------------------------------------------------
+| Eliminar archivo individual
+|--------------------------------------------------------------------------
+*/
+
+function alcastromol_delete_cache_file($slug) {
+
+ $file = ALCASTROMOL_TAILWIND_CACHE . sanitize_file_name($slug) . '.html';
+
+ if (file_exists($file)) {
+ unlink($file);
+ }
+
+}
+
+
+/*
+|--------------------------------------------------------------------------
+| Eliminar todo el cache
+|--------------------------------------------------------------------------
+*/
+
+function alcastromol_delete_all_cache() {
+
+ if (!file_exists(ALCASTROMOL_TAILWIND_CACHE)) {
+ return 0;
+ }
+
+ $files = glob(ALCASTROMOL_TAILWIND_CACHE . '*.html');
+
+ if (!$files) {
+ return 0;
+ }
+
+ $deleted = 0;
+
+ foreach ($files as $file) {
+
+ if (is_file($file)) {
+ unlink($file);
+ $deleted++;
+ }
+
+ }
+
+ return $deleted;
+
+}
+
+
+/*
+|--------------------------------------------------------------------------
+| Patrones, conteos y acciones por tipo de contenido
+|--------------------------------------------------------------------------
+*/
+
+function alcastromol_get_cache_patterns_by_target($target_type, $target_name = '') {
+
+ $target_type = sanitize_key($target_type);
+ $target_name = sanitize_key($target_name);
+
+ switch ($target_type) {
+
+ case 'home':
+ return ['home.html'];
+
+ case 'post_type':
+ if (!$target_name) return [];
+ return [$target_name . '-*.html'];
+
+ case 'taxonomy':
+ if (!$target_name) return [];
+ return ['taxonomy-' . $target_name . '-*.html'];
+
+ case 'oxygen_templates':
+ return ['oxygen-template-*.html'];
+
+ case 'authors':
+ return ['author-*.html'];
+
+ }
+
+ return [];
+
+}
+
+function alcastromol_get_cache_files_by_target($target_type, $target_name = '') {
+
+ if (!file_exists(ALCASTROMOL_TAILWIND_CACHE)) {
+ return [];
+ }
+
+ $patterns = alcastromol_get_cache_patterns_by_target($target_type, $target_name);
+
+ if (empty($patterns)) {
+ return [];
+ }
+
+ $files = [];
+
+ foreach ($patterns as $pattern) {
+
+ $found = glob(ALCASTROMOL_TAILWIND_CACHE . $pattern);
+
+ if (!empty($found)) {
+ $files = array_merge($files, $found);
+ }
+
+ }
+
+ $files = array_filter(array_unique($files), 'is_file');
+
+ return array_values($files);
+
+}
+
+function alcastromol_count_cache_files_by_target($target_type, $target_name = '') {
+
+ return count(alcastromol_get_cache_files_by_target($target_type, $target_name));
+
+}
+
+function alcastromol_delete_cache_by_target($target_type, $target_name = '') {
+
+ $files = alcastromol_get_cache_files_by_target($target_type, $target_name);
+
+ if (empty($files)) {
+ return 0;
+ }
+
+ $deleted = 0;
+
+ foreach ($files as $file) {
+
+ if (is_file($file) && unlink($file)) {
+ $deleted++;
+ }
+
+ }
+
+ return $deleted;
+
+}
+
+function alcastromol_get_urls_by_cache_target($target_type, $target_name = '') {
+
+ $target_type = sanitize_key($target_type);
+ $target_name = sanitize_key($target_name);
+
+ $items = [];
+
+ if ($target_type === 'home') {
+
+ $items[] = [
+ 'url' => home_url('/'),
+ 'slug' => 'home',
+ 'type' => 'home',
+ ];
+
+ return $items;
+
+ }
+
+ if ($target_type === 'post_type') {
+
+ if (!$target_name || !post_type_exists($target_name)) {
+ return [];
+ }
+
+ $post_ids = get_posts([
+ 'post_type' => $target_name,
+ 'post_status' => 'publish',
+ 'posts_per_page' => -1,
+ 'fields' => 'ids',
+ 'no_found_rows' => true,
+ 'ignore_sticky_posts' => true,
+ 'suppress_filters' => false,
+ 'update_post_meta_cache' => false,
+ 'update_post_term_cache' => false,
+ ]);
+
+ foreach ($post_ids as $post_id) {
+
+ $post = get_post($post_id);
+
+ if (!$post) continue;
+
+ $permalink = get_permalink($post_id);
+
+ if (!$permalink) continue;
+
+ $items[] = [
+ 'url' => $permalink,
+ 'slug' => $post->post_type . '-' . $post->ID . '-' . $post->post_name,
+ 'type' => $post->post_type,
+ ];
+
+ }
+
+ return $items;
+
+ }
+
+ if ($target_type === 'taxonomy') {
+
+ if (!$target_name || !taxonomy_exists($target_name)) {
+ return [];
+ }
+
+ $terms = get_terms([
+ 'taxonomy' => $target_name,
+ 'hide_empty' => false,
+ ]);
+
+ if (is_wp_error($terms) || empty($terms)) {
+ return [];
+ }
+
+ foreach ($terms as $term) {
+
+ $term_link = get_term_link($term);
+
+ if (is_wp_error($term_link)) {
+ continue;
+ }
+
+ $items[] = [
+ 'url' => $term_link,
+ 'slug' => 'taxonomy-' . $target_name . '-' . $term->term_id . '-' . $term->slug,
+ 'type' => 'taxonomy_' . $target_name,
+ ];
+
+ }
+
+ return $items;
+
+ }
+
+ if ($target_type === 'authors') {
+
+ $authors = get_users([
+ 'who' => 'authors',
+ ]);
+
+ foreach ($authors as $author) {
+
+ $items[] = [
+ 'url' => get_author_posts_url($author->ID),
+ 'slug' => 'author-' . $author->ID . '-' . $author->user_nicename,
+ 'type' => 'author',
+ ];
+
+ }
+
+ return $items;
+
+ }
+
+ return $items;
+
+}
+
+function alcastromol_generate_cache_by_target($target_type, $target_name = '') {
+
+ $target_type = sanitize_key($target_type);
+ $target_name = sanitize_key($target_name);
+
+ $generated = 0;
+ $failed = 0;
+ $failed_urls = [];
+ $total = 0;
+
+ if ($target_type === 'oxygen_templates') {
+
+ if (!alcastromol_is_oxygen_enabled()) {
+ return [
+ 'total' => 0,
+ 'generated' => 0,
+ 'failed' => 0,
+ 'failed_urls' => [],
+ ];
+ }
+
+ $oxygen_items = alcastromol_get_oxygen_templates_cache_items();
+ $total = count($oxygen_items);
+
+ foreach ($oxygen_items as $item) {
+
+ if (empty($item['slug']) || empty($item['html'])) {
+ $failed++;
+ continue;
+ }
+
+ $html = alcastromol_clean_html($item['html']);
+
+ alcastromol_save_cache($item['slug'], $html);
+
+ $generated++;
+
+ }
+
+ return [
+ 'total' => $total,
+ 'generated' => $generated,
+ 'failed' => $failed,
+ 'failed_urls' => $failed_urls,
+ 'count' => alcastromol_count_cache_files_by_target($target_type, $target_name),
+ ];
+
+ }
+
+ $items = alcastromol_get_urls_by_cache_target($target_type, $target_name);
+ $total = count($items);
+
+ foreach ($items as $item) {
+
+ if (empty($item['url']) || empty($item['slug'])) {
+ $failed++;
+ continue;
+ }
+
+ $result = alcastromol_cache_url($item['url'], $item['slug']);
+
+ if ($result) {
+ $generated++;
+ } else {
+ $failed++;
+ $failed_urls[] = $item['url'];
+ }
+
+ }
+
+ return [
+ 'total' => $total,
+ 'generated' => $generated,
+ 'failed' => $failed,
+ 'failed_urls' => $failed_urls,
+ 'count' => alcastromol_count_cache_files_by_target($target_type, $target_name),
+ ];
+
+}
+
+function alcastromol_render_cache_action_row($args) {
+
+ $target_type = sanitize_key($args['target_type'] ?? '');
+ $target_name = sanitize_key($args['target_name'] ?? '');
+ $label = $args['label'] ?? '';
+ $code = $args['code'] ?? '';
+ $input_name = $args['input_name'] ?? '';
+ $input_value = $args['input_value'] ?? '';
+ $checked = !empty($args['checked']);
+
+ $count = alcastromol_count_cache_files_by_target($target_type, $target_name);
+
+ ?>
+
+
+
+
+ archivos
+
+
+
+
+
+
+ post_type . '-' . $post->ID . '-' . $post->post_name;
+
+}
+
+
+/*
+|--------------------------------------------------------------------------
+| Cache individual al guardar Gutenberg / post / page / CPT / product
+|--------------------------------------------------------------------------
+*/
+
+function alcastromol_cache_post($post_id) {
+
+ if (wp_is_post_revision($post_id)) return;
+
+ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
+
+ $post = get_post($post_id);
+
+ if (!$post) return;
+
+ if ($post->post_status !== 'publish') return;
+
+ /*
+ | Si es template Oxygen, lo cacheamos con la función especial.
+ */
+
+ if ($post->post_type === 'ct_template') {
+ if (alcastromol_is_oxygen_enabled()) {
+ alcastromol_cache_oxygen_template($post_id);
+ }
+ return;
+ }
+
+ $options = alcastromol_get_cache_options();
+
+ if (empty($options['post_types']) || !in_array($post->post_type, $options['post_types'], true)) {
+ return;
+ }
+
+ $url = get_permalink($post_id);
+
+ if (!$url) return;
+
+ $slug = alcastromol_get_post_cache_slug($post_id);
+
+ if (!$slug) return;
+
+ alcastromol_cache_url($url, $slug);
+
+}
+
+add_action('save_post', 'alcastromol_cache_post', 30);
+add_action('save_post_product', 'alcastromol_cache_post', 30);
+add_action('save_post_ct_template', 'alcastromol_cache_post', 30);
+
+
+/*
+|--------------------------------------------------------------------------
+| Obtener HTML/metas de un template Oxygen
+|--------------------------------------------------------------------------
+*/
+
+function alcastromol_get_oxygen_template_html($template_id) {
+
+ $post = get_post($template_id);
+
+ if (!$post || $post->post_type !== 'ct_template') {
+ return false;
+ }
+
+ $html_parts = [];
+
+ if (!empty($post->post_content)) {
+ $html_parts[] = $post->post_content;
+ }
+
+ $possible_meta_keys = [
+ 'ct_builder_shortcodes',
+ 'ct_builder_json',
+ 'ct_template_type',
+ 'ct_template_order',
+ 'ct_use_template',
+ 'ct_parent_template',
+ 'ct_inner_content',
+ ];
+
+ foreach ($possible_meta_keys as $meta_key) {
+
+ $meta_value = get_post_meta($template_id, $meta_key, true);
+
+ if (!empty($meta_value)) {
+
+ if (is_array($meta_value) || is_object($meta_value)) {
+ $meta_value = wp_json_encode($meta_value);
+ }
+
+ if (is_string($meta_value)) {
+ $html_parts[] = $meta_value;
+ }
+
+ }
+
+ }
+
+ /*
+ | Extra: tomar todos los metas del template.
+ | Oxygen puede guardar clases y estructura en distintos campos.
+ */
+
+ $all_meta = get_post_meta($template_id);
+
+ if (!empty($all_meta) && is_array($all_meta)) {
+
+ foreach ($all_meta as $meta_key => $meta_values) {
+
+ if (empty($meta_values)) {
+ continue;
+ }
+
+ foreach ($meta_values as $meta_value) {
+
+ if (empty($meta_value)) {
+ continue;
+ }
+
+ if (is_array($meta_value) || is_object($meta_value)) {
+ $meta_value = wp_json_encode($meta_value);
+ }
+
+ if (is_string($meta_value)) {
+ $html_parts[] = $meta_value;
+ }
+
+ }
+
+ }
+
+ }
+
+ $html = implode("\n", array_filter($html_parts));
+
+ if (empty($html)) {
+ return false;
+ }
+
+ return $html;
+
+}
+
+
+/*
+|--------------------------------------------------------------------------
+| Cache individual de template Oxygen
+|--------------------------------------------------------------------------
+*/
+
+function alcastromol_cache_oxygen_template($template_id) {
+
+ if (!alcastromol_is_oxygen_enabled()) {
+ return false;
+ }
+
+ $options = alcastromol_get_cache_options();
+
+ if (empty($options['oxygen_templates'])) {
+ return false;
+ }
+
+ $post = get_post($template_id);
+
+ if (!$post || $post->post_type !== 'ct_template') {
+ return false;
+ }
+
+ if (!in_array($post->post_status, ['publish', 'draft'], true)) {
+ return false;
+ }
+
+ $html = alcastromol_get_oxygen_template_html($template_id);
+
+ if (!$html) {
+ return false;
+ }
+
+ $slug = 'oxygen-template-' . $template_id . '-' . $post->post_name;
+
+ $html = alcastromol_clean_html($html);
+
+ alcastromol_save_cache($slug, $html);
+
+ return true;
+
+}
+
+
+/*
+|--------------------------------------------------------------------------
+| Obtener templates Oxygen para cache masivo
+|--------------------------------------------------------------------------
+*/
+
+function alcastromol_get_oxygen_templates_cache_items() {
+
+ $items = [];
+
+ if (!alcastromol_is_oxygen_enabled()) {
+ return $items;
+ }
+
+ if (!post_type_exists('ct_template')) {
+ return $items;
+ }
+
+ $template_ids = get_posts([
+ 'post_type' => 'ct_template',
+ 'post_status' => ['publish', 'draft'],
+ 'posts_per_page' => -1,
+ 'fields' => 'ids',
+ 'no_found_rows' => true,
+ 'ignore_sticky_posts' => true,
+ 'suppress_filters' => false,
+ 'update_post_meta_cache' => true,
+ 'update_post_term_cache' => false,
+ ]);
+
+ foreach ($template_ids as $template_id) {
+
+ $post = get_post($template_id);
+
+ if (!$post) {
+ continue;
+ }
+
+ $html = alcastromol_get_oxygen_template_html($template_id);
+
+ if (!$html) {
+ continue;
+ }
+
+ $items[] = [
+ 'slug' => 'oxygen-template-' . $template_id . '-' . $post->post_name,
+ 'html' => $html,
+ ];
+
+ }
+
+ return $items;
+
+}
+
+
+/*
+|--------------------------------------------------------------------------
+| Obtener todas las URLs públicas seleccionadas
+|--------------------------------------------------------------------------
+*/
+
+function alcastromol_get_all_urls() {
+
+ $options = alcastromol_get_cache_options();
+
+ $urls = [];
+
+ /*
+ |--------------------------------------------------------------------------
+ | Home
+ |--------------------------------------------------------------------------
+ */
+
+ if (!empty($options['home'])) {
+
+ $urls[] = [
+ 'url' => home_url('/'),
+ 'slug' => 'home',
+ 'type' => 'home',
+ ];
+
+ }
+
+
+ /*
+ |--------------------------------------------------------------------------
+ | Post types seleccionados
+ |--------------------------------------------------------------------------
+ */
+
+ $selected_post_types = [];
+
+ if (!empty($options['post_types']) && is_array($options['post_types'])) {
+ $selected_post_types = $options['post_types'];
+ }
+
+ $selected_post_types = array_filter($selected_post_types, function ($post_type) {
+ return post_type_exists($post_type);
+ });
+
+ if (!empty($selected_post_types)) {
+
+ $post_ids = get_posts([
+ 'post_type' => $selected_post_types,
+ 'post_status' => 'publish',
+ 'posts_per_page' => -1,
+ 'fields' => 'ids',
+ 'no_found_rows' => true,
+ 'ignore_sticky_posts' => true,
+ 'suppress_filters' => false,
+ 'update_post_meta_cache' => false,
+ 'update_post_term_cache' => false,
+ ]);
+
+ foreach ($post_ids as $post_id) {
+
+ $post = get_post($post_id);
+
+ if (!$post) continue;
+
+ $permalink = get_permalink($post_id);
+
+ if (!$permalink) continue;
+
+ $urls[] = [
+ 'url' => $permalink,
+ 'slug' => $post->post_type . '-' . $post->ID . '-' . $post->post_name,
+ 'type' => $post->post_type,
+ ];
+
+ }
+
+ }
+
+
+ /*
+ |--------------------------------------------------------------------------
+ | Taxonomías seleccionadas
+ |--------------------------------------------------------------------------
+ */
+
+ $selected_taxonomies = [];
+
+ if (!empty($options['taxonomies']) && is_array($options['taxonomies'])) {
+ $selected_taxonomies = $options['taxonomies'];
+ }
+
+ $selected_taxonomies = array_filter($selected_taxonomies, function ($taxonomy) {
+ return taxonomy_exists($taxonomy);
+ });
+
+ if (!empty($selected_taxonomies)) {
+
+ foreach ($selected_taxonomies as $tax) {
+
+ $terms = get_terms([
+ 'taxonomy' => $tax,
+ 'hide_empty' => false,
+ ]);
+
+ if (is_wp_error($terms) || empty($terms)) {
+ continue;
+ }
+
+ foreach ($terms as $term) {
+
+ $term_link = get_term_link($term);
+
+ if (is_wp_error($term_link)) {
+ continue;
+ }
+
+ $urls[] = [
+ 'url' => $term_link,
+ 'slug' => 'taxonomy-' . $tax . '-' . $term->term_id . '-' . $term->slug,
+ 'type' => 'taxonomy_' . $tax,
+ ];
+
+ }
+
+ }
+
+ }
+
+
+ /*
+ |--------------------------------------------------------------------------
+ | Autores
+ |--------------------------------------------------------------------------
+ */
+
+ if (!empty($options['authors'])) {
+
+ $authors = get_users([
+ 'who' => 'authors',
+ ]);
+
+ foreach ($authors as $author) {
+
+ $urls[] = [
+ 'url' => get_author_posts_url($author->ID),
+ 'slug' => 'author-' . $author->ID . '-' . $author->user_nicename,
+ 'type' => 'author',
+ ];
+
+ }
+
+ }
+
+
+ /*
+ |--------------------------------------------------------------------------
+ | Eliminar URLs duplicadas
+ |--------------------------------------------------------------------------
+ */
+
+ $unique_urls = [];
+
+ foreach ($urls as $item) {
+
+ if (empty($item['url']) || empty($item['slug'])) {
+ continue;
+ }
+
+ $unique_urls[$item['url']] = $item;
+
+ }
+
+ return array_values($unique_urls);
+
+}
+
+
+/*
+|--------------------------------------------------------------------------
+| Generar cache completa
+|--------------------------------------------------------------------------
+*/
+
+function alcastromol_generate_full_cache() {
+
+ $options = alcastromol_get_cache_options();
+
+ $urls = alcastromol_get_all_urls();
+
+ $generated = 0;
+ $failed = 0;
+ $products = 0;
+ $oxygen_templates = 0;
+ $failed_urls = [];
+
+ /*
+ |--------------------------------------------------------------------------
+ | Cache de URLs públicas
+ |--------------------------------------------------------------------------
+ */
+
+ foreach ($urls as $item) {
+
+ if (!empty($item['type']) && $item['type'] === 'product') {
+ $products++;
+ }
+
+ $result = alcastromol_cache_url($item['url'], $item['slug']);
+
+ if ($result) {
+ $generated++;
+ } else {
+ $failed++;
+ $failed_urls[] = $item['url'];
+ }
+
+ }
+
+
+ /*
+ |--------------------------------------------------------------------------
+ | Cache de templates Oxygen
+ |--------------------------------------------------------------------------
+ */
+
+ if (alcastromol_is_oxygen_enabled() && !empty($options['oxygen_templates'])) {
+
+ $oxygen_items = alcastromol_get_oxygen_templates_cache_items();
+
+ foreach ($oxygen_items as $item) {
+
+ if (empty($item['slug']) || empty($item['html'])) {
+ continue;
+ }
+
+ $html = alcastromol_clean_html($item['html']);
+
+ alcastromol_save_cache($item['slug'], $html);
+
+ $oxygen_templates++;
+ $generated++;
+
+ }
+
+ }
+
+
+ return [
+ 'total' => count($urls) + $oxygen_templates,
+ 'products' => $products,
+ 'oxygen_templates' => $oxygen_templates,
+ 'generated' => $generated,
+ 'failed' => $failed,
+ 'failed_urls' => $failed_urls,
+ ];
+
+}
+
+
+/*
+|--------------------------------------------------------------------------
+| AJAX guardar opciones
+|--------------------------------------------------------------------------
+*/
+
+add_action('wp_ajax_alcastromol_save_tailwind_cache_options', function () {
+
+ if (!current_user_can('manage_options')) {
+ wp_send_json_error('No tienes permisos para guardar opciones.');
+ }
+
+ check_ajax_referer('alcastromol_tailwind_cache_nonce', 'nonce');
+
+ $raw_options = $_POST['options'] ?? [];
+
+ if (!is_array($raw_options)) {
+ $raw_options = [];
+ }
+
+ $options = alcastromol_save_cache_options($raw_options);
+
+ wp_send_json_success([
+ 'message' => 'Opciones guardadas',
+ 'options' => $options,
+ ]);
+
+});
+
+
+/*
+|--------------------------------------------------------------------------
+| AJAX generar cache
+|--------------------------------------------------------------------------
+*/
+
+add_action('wp_ajax_alcastromol_generate_tailwind_cache', function () {
+
+ if (!current_user_can('manage_options')) {
+ wp_send_json_error('No tienes permisos para generar el cache.');
+ }
+
+ check_ajax_referer('alcastromol_tailwind_cache_nonce', 'nonce');
+
+ $result = alcastromol_generate_full_cache();
+
+ wp_send_json_success([
+ 'message' => 'Cache generada',
+ 'total' => $result['total'],
+ 'products' => $result['products'],
+ 'oxygen_templates' => $result['oxygen_templates'],
+ 'generated' => $result['generated'],
+ 'failed' => $result['failed'],
+ 'failed_urls' => $result['failed_urls'],
+ ]);
+
+});
+
+
+/*
+|--------------------------------------------------------------------------
+| AJAX eliminar cache
+|--------------------------------------------------------------------------
+*/
+
+add_action('wp_ajax_alcastromol_delete_tailwind_cache', function () {
+
+ if (!current_user_can('manage_options')) {
+ wp_send_json_error('No tienes permisos para eliminar el cache.');
+ }
+
+ check_ajax_referer('alcastromol_tailwind_cache_nonce', 'nonce');
+
+ $deleted = alcastromol_delete_all_cache();
+
+ wp_send_json_success([
+ 'message' => 'Cache eliminado',
+ 'deleted' => $deleted,
+ ]);
+
+});
+
+
+/*
+|--------------------------------------------------------------------------
+| AJAX generar cache por tipo de contenido
+|--------------------------------------------------------------------------
+*/
+
+add_action('wp_ajax_alcastromol_generate_tailwind_cache_target', function () {
+
+ if (!current_user_can('manage_options')) {
+ wp_send_json_error('No tienes permisos para generar este cache.');
+ }
+
+ check_ajax_referer('alcastromol_tailwind_cache_nonce', 'nonce');
+
+ $target_type = sanitize_key($_POST['target_type'] ?? '');
+ $target_name = sanitize_key($_POST['target_name'] ?? '');
+
+ $allowed = ['home', 'post_type', 'taxonomy', 'oxygen_templates', 'authors'];
+
+ if (!in_array($target_type, $allowed, true)) {
+ wp_send_json_error('Tipo de cache inválido.');
+ }
+
+ $result = alcastromol_generate_cache_by_target($target_type, $target_name);
+
+ wp_send_json_success([
+ 'message' => 'Cache generado',
+ 'target_type' => $target_type,
+ 'target_name' => $target_name,
+ 'total' => $result['total'],
+ 'generated' => $result['generated'],
+ 'failed' => $result['failed'],
+ 'failed_urls' => $result['failed_urls'],
+ 'count' => $result['count'],
+ ]);
+
+});
+
+
+/*
+|--------------------------------------------------------------------------
+| POST administrativo para generar cache por tipo de contenido
+|--------------------------------------------------------------------------
+*/
+add_action('admin_post_alcastromol_generate_tailwind_cache_target_post', function () {
+ if (!current_user_can('manage_options')) {
+ wp_die('No tienes permisos para generar este cache.');
+ }
+ check_admin_referer('alcastromol_tailwind_cache_nonce', 'nonce');
+ $target_type = sanitize_key($_POST['target_type'] ?? '');
+ $target_name = sanitize_key($_POST['target_name'] ?? '');
+ $allowed = ['home', 'post_type', 'taxonomy', 'oxygen_templates', 'authors'];
+ if (!in_array($target_type, $allowed, true)) {
+ wp_die('Tipo de cache inválido.');
+ }
+ if (function_exists('set_time_limit')) {
+ @set_time_limit(0);
+ }
+ ignore_user_abort(true);
+ $result = alcastromol_generate_cache_by_target($target_type, $target_name);
+ $redirect = add_query_arg([
+ 'page' => 'tailwind-cache',
+ 'cache_target_result' => 'generated',
+ 'cache_target_type' => $target_type,
+ 'cache_target_name' => $target_name,
+ 'cache_target_total' => (int) $result['total'],
+ 'cache_target_created' => (int) $result['generated'],
+ 'cache_target_failed' => (int) $result['failed'],
+ 'cache_target_count' => (int) $result['count'],
+ ], admin_url('admin.php'));
+ wp_safe_redirect($redirect);
+ exit;
+});
+
+
+/*
+|--------------------------------------------------------------------------
+| AJAX eliminar cache por tipo de contenido
+|--------------------------------------------------------------------------
+*/
+
+add_action('wp_ajax_alcastromol_delete_tailwind_cache_target', function () {
+
+ if (!current_user_can('manage_options')) {
+ wp_send_json_error('No tienes permisos para eliminar este cache.');
+ }
+
+ check_ajax_referer('alcastromol_tailwind_cache_nonce', 'nonce');
+
+ $target_type = sanitize_key($_POST['target_type'] ?? '');
+ $target_name = sanitize_key($_POST['target_name'] ?? '');
+
+ $allowed = ['home', 'post_type', 'taxonomy', 'oxygen_templates', 'authors'];
+
+ if (!in_array($target_type, $allowed, true)) {
+ wp_send_json_error('Tipo de cache inválido.');
+ }
+
+ $deleted = alcastromol_delete_cache_by_target($target_type, $target_name);
+
+ wp_send_json_success([
+ 'message' => 'Cache eliminado',
+ 'target_type' => $target_type,
+ 'target_name' => $target_name,
+ 'deleted' => $deleted,
+ 'count' => alcastromol_count_cache_files_by_target($target_type, $target_name),
+ ]);
+
+});
+
+
+/*
+|--------------------------------------------------------------------------
+| Página herramientas
+|--------------------------------------------------------------------------
+*/
+
+add_action('admin_menu', function () {
+
+ add_submenu_page(
+ 'alcastromol-functions',
+ 'Cache de Tailwind',
+ 'Cache de Tailwind',
+ 'manage_options',
+ 'tailwind-cache',
+ 'alcastromol_tailwind_tools_page'
+ );
+
+}, 20);
+
+
+function alcastromol_tailwind_tools_page() {
+
+ $options = alcastromol_get_cache_options();
+
+ $post_types = alcastromol_detect_cacheable_post_types();
+
+ $taxonomies = alcastromol_detect_cacheable_taxonomies();
+
+ $nonce = wp_create_nonce('alcastromol_tailwind_cache_nonce');
+
+?>
+
+
+
+
Generar Cache Tailwind
+
+
+ Selecciona qué contenido quieres cachear para que Tailwind detecte clases de Gutenberg,
+ WooCommerce, CPTs, taxonomías y templates de Oxygen.
+
+
+
+
+
Contenido cacheable detectado
+
+
+
+
+
+
+ Cache generado correctamente.
+ Tipo:
+ Items encontrados: ·
+ Generados: ·
+ Fallidos: ·
+ Archivos actuales:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+{"use strict";var e={82256:(e,t,n)=>{n.r(t),n.d(t,{default:()=>Xe});var s=n(36968),i=n.n(s),o=n(27361),a=n.n(o),r=n(23279),l=n.n(r),c=n(70655),d=n(20144),u=n(38085),p=n(18076),h=n(25970),g=n(67505),m=n(85450),v=n(71035);const y=1,f=3;function _(e,t,n,{enableContentEditableAndFocus:s,saveContentEditable:i}){const o=e=>{const a=[];return e.forEach((e=>{if(e.nodeType===f){const t=e.nodeValue;"%%CHILDREN%%"===t?.trim()?a.push(n()):t&&a.push(t)}else if(e.nodeType===y&&e instanceof Element){const n=e.attributes,r=e.nodeName,l={},c={},d={},u=[];let p,h;for(let t=0;t/,"").replace("