render.php
135 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Render Callback for Books Gallery Block |
| 4 | * |
| 5 | * @param array $attributes Block attributes |
| 6 | * @return string HTML output |
| 7 | */ |
| 8 | |
| 9 | // Extract attributes with defaults |
| 10 | $heading = isset($attributes['heading']) ? $attributes['heading'] : 'My Book Shelf'; |
| 11 | $books_per_page = isset($attributes['booksPerPage']) ? intval($attributes['booksPerPage']) : 8; |
| 12 | $books_per_row = isset($attributes['booksPerRow']) ? intval($attributes['booksPerRow']) : 4; |
| 13 | $order = isset($attributes['order']) ? $attributes['order'] : 'DESC'; |
| 14 | $orderby = isset($attributes['orderby']) ? $attributes['orderby'] : 'date'; |
| 15 | $exclude_books = isset($attributes['excludeBooks']) ? $attributes['excludeBooks'] : ''; |
| 16 | $section_bg_color = isset($attributes['sectionBgColor']) ? $attributes['sectionBgColor'] : ''; |
| 17 | $section_padding = isset($attributes['sectionPadding']) ? intval($attributes['sectionPadding']) : null; |
| 18 | $heading_color = isset($attributes['headingColor']) ? $attributes['headingColor'] : ''; |
| 19 | $book_title_color = isset($attributes['bookTitleColor']) ? $attributes['bookTitleColor'] : ''; |
| 20 | $card_bg_color = isset($attributes['cardBgColor']) ? $attributes['cardBgColor'] : ''; |
| 21 | $block_id = isset($attributes['blockId']) ? $attributes['blockId'] : 'books-gallery'; |
| 22 | $gallery_style = isset($attributes['galleryStyle']) ? $attributes['galleryStyle'] : 'fun'; |
| 23 | |
| 24 | $is_fun = $gallery_style === 'fun'; |
| 25 | $is_elegant = $gallery_style === 'elegant'; |
| 26 | |
| 27 | // Map books per row to Tailwind grid classes |
| 28 | $grid_class = match ($books_per_row) { |
| 29 | 2 => 'grid-cols-1 md:grid-cols-2', |
| 30 | 3 => 'grid-cols-1 md:grid-cols-3', |
| 31 | 5 => 'grid-cols-2 md:grid-cols-5', |
| 32 | default => 'grid-cols-2 md:grid-cols-4', // 4 is default |
| 33 | }; |
| 34 | |
| 35 | // Conditional container class |
| 36 | $container_class = ($section_bg_color || $section_padding !== null) ? '' : 'py-24'; |
| 37 | |
| 38 | // Build inline styles |
| 39 | $styles = ''; |
| 40 | if ($section_bg_color) { |
| 41 | $styles .= 'background-color: ' . esc_attr($section_bg_color) . ';'; |
| 42 | } |
| 43 | if ($section_padding !== null) { |
| 44 | $styles .= 'padding-top: ' . esc_attr($section_padding) . 'px;'; |
| 45 | $styles .= 'padding-bottom: ' . esc_attr($section_padding) . 'px;'; |
| 46 | } |
| 47 | |
| 48 | // Parse exclude IDs |
| 49 | $exclude_ids = array(); |
| 50 | if (!empty($exclude_books)) { |
| 51 | $exclude_ids = array_map('trim', explode(',', $exclude_books)); |
| 52 | $exclude_ids = array_filter($exclude_ids, 'is_numeric'); |
| 53 | } |
| 54 | |
| 55 | // Build WP_Query arguments |
| 56 | $query_args = array( |
| 57 | 'post_type' => 'book', |
| 58 | 'posts_per_page' => $books_per_page, |
| 59 | 'order' => $order, |
| 60 | 'orderby' => $orderby, |
| 61 | 'post_status' => 'publish', |
| 62 | ); |
| 63 | |
| 64 | if (!empty($exclude_ids)) { |
| 65 | $query_args['post__not_in'] = $exclude_ids; |
| 66 | } |
| 67 | |
| 68 | // Run query |
| 69 | $books_query = new WP_Query($query_args); |
| 70 | |
| 71 | // Conditional Classes & Styles using PHP variables |
| 72 | $heading_class = ($is_elegant) ? 'text-3xl md:text-5xl font-serif font-bold text-brand-dark text-center mb-12' : 'section-heading'; |
| 73 | |
| 74 | $grid_gap_class = ($is_elegant) ? 'gap-8 lg:gap-12' : 'gap-8'; |
| 75 | |
| 76 | $card_wrapper_class = ($is_elegant) ? 'group text-center block' : 'book-card group block transition-transform duration-300 hover:scale-105'; |
| 77 | |
| 78 | $image_wrapper_class = ($is_elegant) ? 'overflow-hidden rounded bg-gray-100 shadow-md mb-6 aspect-[2/3]' : 'book-cover mb-4 rounded-lg overflow-hidden shadow-lg'; |
| 79 | |
| 80 | $image_class = ($is_elegant) ? 'object-cover w-full h-full transition-transform duration-500 group-hover:scale-105' : 'w-full h-auto'; |
| 81 | |
| 82 | $title_class = ($is_elegant) ? 'text-lg font-serif font-bold text-brand-dark group-hover:text-gray-600 transition-colors' : 'book-title text-xl font-bold text-center text-navy'; |
| 83 | |
| 84 | ?> |
| 85 | |
| 86 | <section class="alignfull block-<?php echo esc_attr($block_id); ?> <?php echo esc_attr($container_class); ?>" |
| 87 | style="<?php echo esc_attr($styles); ?>"> |
| 88 | <div class="awt-container mx-auto px-6"> |
| 89 | <?php if (!empty($heading)): ?> |
| 90 | <h2 class="<?php echo esc_attr($heading_class); ?>" |
| 91 | style="<?php echo $heading_color ? 'color: ' . esc_attr($heading_color) . ';' : ''; ?>"> |
| 92 | <?php echo esc_html($heading); ?> |
| 93 | </h2> |
| 94 | <?php endif; ?> |
| 95 | |
| 96 | <?php if ($books_query->have_posts()): ?> |
| 97 | <div class="grid <?php echo esc_attr($grid_class); ?> <?php echo esc_attr($grid_gap_class); ?>"> |
| 98 | <?php while ($books_query->have_posts()): |
| 99 | $books_query->the_post(); ?> |
| 100 | <div class="<?php echo esc_attr($card_wrapper_class); ?>"> |
| 101 | <a href="<?php echo esc_url(get_permalink()); ?>" class="block"> |
| 102 | <?php if (has_post_thumbnail()): ?> |
| 103 | <div class="<?php echo esc_attr($image_wrapper_class); ?>"> |
| 104 | <img src="<?php echo esc_url(get_the_post_thumbnail_url(get_the_ID(), 'full')); ?>" |
| 105 | alt="<?php echo esc_attr(get_the_title()); ?>" class="<?php echo esc_attr($image_class); ?>" /> |
| 106 | </div> |
| 107 | <?php else: ?> |
| 108 | <div class="<?php echo esc_attr($image_wrapper_class); ?> bg-gray-200 flex items-center justify-center relative" |
| 109 | style="<?php echo ($is_fun) ? 'height: 300px;' : ''; /* Aspect ratio handles height in elegant */ ?>"> |
| 110 | <span class="text-gray-500 absolute inset-0 flex items-center justify-center">No Image</span> |
| 111 | </div> |
| 112 | <?php endif; ?> |
| 113 | |
| 114 | <h3 class="<?php echo esc_attr($title_class); ?>" |
| 115 | style="<?php echo $book_title_color ? 'color: ' . esc_attr($book_title_color) . ';' : ''; ?>"> |
| 116 | <?php echo esc_html(get_the_title()); ?> |
| 117 | </h3> |
| 118 | </a> |
| 119 | </div> |
| 120 | <?php endwhile; ?> |
| 121 | </div> |
| 122 | <?php else: ?> |
| 123 | <div class="no-books-message text-center py-12"> |
| 124 | <p class="text-xl text-gray-600"> |
| 125 | <?php echo esc_html__('No books found in RS Book Showcase.', 'author-website-templates'); ?> |
| 126 | </p> |
| 127 | </div> |
| 128 | <?php endif; ?> |
| 129 | |
| 130 | <?php wp_reset_postdata(); ?> |
| 131 | </div> |
| 132 | </section> |
| 133 | |
| 134 | <?php |
| 135 |