1, 'post_types' => [], 'taxonomies' => [], 'authors' => 0, 'oxygen_templates' => 1, ]; } /* |-------------------------------------------------------------------------- | 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'] = !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') { $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') { 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) { $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 (!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 (!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'], ]); }); /* |-------------------------------------------------------------------------- | 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_management_page( 'Tailwind Cache', 'Tailwind Cache', 'manage_options', 'tailwind-cache', 'alcastromol_tailwind_tools_page' ); }); 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

General

'home', 'target_name' => '', 'label' => 'Home', 'code' => '', 'input_name' => 'home', 'input_value' => '1', 'checked' => !empty($options['home']), ]); ?>

Post types / Gutenberg / WooCommerce / CPTs

$item) : ?> 'post_type', 'target_name' => $post_type, 'label' => $item['label'], 'code' => $post_type, 'input_name' => 'post_types[]', 'input_value' => $post_type, 'checked' => in_array($post_type, $options['post_types'], true), ]); ?>

No se detectaron post types públicos.

Taxonomías

$item) : ?> 'taxonomy', 'target_name' => $taxonomy, 'label' => $item['label'], 'code' => $taxonomy, 'input_name' => 'taxonomies[]', 'input_value' => $taxonomy, 'checked' => in_array($taxonomy, $options['taxonomies'], true), ]); ?>

No se detectaron taxonomías públicas.

Oxygen

'oxygen_templates', 'target_name' => '', 'label' => 'Templates de Oxygen', 'code' => 'ct_template', 'input_name' => 'oxygen_templates', 'input_value' => '1', 'checked' => !empty($options['oxygen_templates']), ]); ?>

Autores

'authors', 'target_name' => '', 'label' => 'Author pages', 'code' => '', 'input_name' => 'authors', 'input_value' => '1', 'checked' => !empty($options['authors']), ]); ?>