require_once get_template_directory() . '/includes/gallery-utils.php'; function custom_gallery_shortcode($atts, $content = null) { // Extract HTML content $content = do_shortcode($content); // Initialize variables $images = array(); $dom = new DOMDocument(); // Configure DOM libxml_use_internal_errors(true); $dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); libxml_clear_errors(); // Get main container $container = $dom->getElementsByTagName('div')->item(0); if (!$container) { $container = $dom->createElement('div'); $dom->appendChild($container); } // Get current post ID and slug $post_id = get_the_ID(); if (!$post_id) { return $content; } // Check if valid cache exists $cache_key = 'gallery_images_' . $post_id; $valid_images = get_transient($cache_key); if ($valid_images === false) { // No cache or expired, process images ini_set('memory_limit', '4096M'); set_time_limit(600); libxml_use_internal_errors(true); $dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); libxml_clear_errors(); $container = $dom->getElementsByTagName('div')->item(0); $links = $dom->getElementsByTagName('a'); $valid_images = array(); foreach ($links as $index => $link) { $img = $link->getElementsByTagName('img')->item(0); if ($img) { $image_url = $link->getAttribute('href'); if ($image_url === '#' || empty($image_url)) { $thumb_url = $img->getAttribute('src'); $image_url = str_replace('thumb-', '', $thumb_url); } if (!empty($image_url) && $image_url !== '#') { $thumb_url = $img->getAttribute('src'); $thumb_path = str_replace(home_url('/'), ABSPATH, $thumb_url); $thumb_dimensions = @getimagesize($thumb_path); $full_dimensions = get_image_dimensions($image_url); $valid_images[] = array( 'thumb' => $thumb_url, 'alt' => $img->getAttribute('alt') ?: '', 'width' => $thumb_dimensions ? $thumb_dimensions[0] : '', 'height' => $thumb_dimensions ? $thumb_dimensions[1] : '', 'url' => $image_url, 'full' => $image_url, 'full_width' => $full_dimensions['width'], 'full_height' => $full_dimensions['height'], 'index' => $index + 1 ); } } } set_transient($cache_key, $valid_images, 31536000); } $total_valid = count($valid_images); if ($total_valid === 0) { return $content; } // Set interactive attributes $container->setAttribute('data-post-id', $post_id); $container->setAttribute('data-total', $total_valid); $container->setAttribute('id', 'gallery-container-' . $post_id); // Initial 8 images $images_to_show = array_slice($valid_images, 0, 8); // Clear original container while ($container->hasChildNodes()) { $container->removeChild($container->firstChild); } // Build Grid $grid_container = $dom->createElement('div'); $grid_container->setAttribute('class', 'gallery-grid'); $grid_container->setAttribute('style', 'display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin: 0 -5px;'); $style = $dom->createElement('style'); $style->textContent = ' .gallery-grid, .additional-images { grid-template-columns: repeat(4, 1fr); gap: 10px; margin: 0 -5px; } .gallery-item img { width: 180px !important; height: 180px !important; object-fit: cover !important; display: block !important; margin: 0 auto !important; border-radius: 10px !important; } @media (max-width: 768px) { .gallery-grid, .additional-images { grid-template-columns: repeat(2, 1fr) !important; } } @media (max-width: 480px) { .gallery-item img { width: 100% !important; max-width: 180px !important; } } #gallery-cascade { margin-top: 30px; display: flex; flex-direction: column; align-items: center; gap: 20px; } #gallery-cascade img { max-width: 100%; height: auto; border-radius: 10px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); } '; $container->appendChild($style); foreach ($images_to_show as $image) { $div = $dom->createElement('div'); $div->setAttribute('class', 'gallery-item'); $div->setAttribute('style', 'margin: 5px; box-sizing: border-box; text-align: center;'); $img = $dom->createElement('img'); $img->setAttribute('src', $image['thumb']); $img->setAttribute('alt', $image['alt']); $img->setAttribute('loading', 'lazy'); $img->setAttribute('style', 'width: 180px; height: 180px; display: block; margin: 0 auto; border: 1px solid #ddd; border-radius: 10px; padding: 3px; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,0.1); object-fit: cover;'); $link = $dom->createElement('a'); $link->setAttribute('href', '#'); $link->setAttribute('class', 'link-like fullscreen-viewer'); $link->setAttribute('onclick', "if(window.openGalleryViewer) { openGalleryViewer($post_id, " . $image['index'] . "); return false; }"); $link->setAttribute('data-index', $image['index']); $link->setAttribute('style', 'display: block; width: 100%; height: 100%; border: none; background: none; padding: 0; cursor: pointer; text-decoration: none;'); $link->appendChild($img); $div->appendChild($link); $grid_container->appendChild($div); } $container->appendChild($grid_container); // "Load More" button if ($total_valid > 8) { $load_more_button = $dom->createElement('div'); $load_more_button->setAttribute('class', 'load-more-images'); $load_more_button->setAttribute('data-post-id', $post_id); $load_more_button->setAttribute('style', 'text-align: center; margin: 20px 0;'); $button = $dom->createElement('button'); $button->setAttribute('class', 'load-all-button'); $button->setAttribute('style', 'background-color: #f70094; color: black; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; font-weight: bold;'); $button->textContent = 'Load More'; $load_more_button->appendChild($button); $container->appendChild($load_more_button); $additional = $dom->createElement('div'); $additional->setAttribute('class', 'additional-images'); $additional->setAttribute('style', 'display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin: 0 -5px;'); $container->appendChild($additional); } // Cascade target $cascade_target = $dom->createElement('div'); $cascade_target->setAttribute('id', 'gallery-cascade'); $container->appendChild($cascade_target); // SEO: JSON-LD $schema_images = array(); foreach ($valid_images as $img) { $schema_images[] = array( "@type" => "ImageObject", "contentUrl" => $img['full'], "width" => $img['full_width'], "height" => $img['full_height'], "description" => $img['alt'] ); } $schema = array( "@context" => "https://schema.org", "@type" => "ImageGallery", "name" => get_the_title($post_id), "description" => get_the_excerpt($post_id), "image" => $schema_images ); $json_ld = $dom->createElement('script'); $json_ld->setAttribute('type', 'application/ld+json'); $json_ld->textContent = json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $container->appendChild($json_ld); // SEO: NoScript $noscript = $dom->createElement('noscript'); $ns_div = $dom->createElement('div'); foreach ($valid_images as $img) { $ns_img = $dom->createElement('img'); $ns_img->setAttribute('src', $img['full']); $ns_img->setAttribute('alt', $img['alt']); $ns_div->appendChild($ns_img); } $noscript->appendChild($ns_div); $container->appendChild($noscript); return $dom->saveHTML($container); } return $dom->saveHTML($container); } // Remover el shortcode existente si existe remove_shortcode('gallery'); // Agregar nuestro shortcode personalizado add_shortcode('gallery', 'custom_gallery_shortcode'); /** * Shortcode para botones de lectura * Uso: [gallery_buttons] */ function gallery_buttons_shortcode($atts) { $post_id = get_the_ID(); if (!$post_id) { return ''; } $post_slug = get_post_field('post_name', $post_id); if (empty($post_slug)) { return ''; } $base_url = home_url('media/viewer/' . $post_slug); $image_url = $base_url . '/image-1.php'; $cascade_url = $base_url . '/cascade.php'; // Verificar si existe el div uapv2-downloads en el contenido $content = get_post_field('post_content', $post_id); $has_downloads = strpos($content, 'uapv2-downloads') !== false; $output = '
'; return $output; } add_shortcode('gallery_buttons', 'gallery_buttons_shortcode');