render.php
189 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Render callback for the Latest Stories block. |
| 4 | * |
| 5 | * @package AuthorWebsiteTemplates |
| 6 | */ |
| 7 | |
| 8 | $posts_to_show = isset($attributes['postsToShow']) ? $attributes['postsToShow'] : 3; |
| 9 | $order = isset($attributes['order']) ? $attributes['order'] : 'desc'; |
| 10 | $order_by = isset($attributes['orderBy']) ? $attributes['orderBy'] : 'date'; |
| 11 | $section_title = isset($attributes['sectionTitle']) ? $attributes['sectionTitle'] : 'Latest Adventures'; |
| 12 | $read_more_text = isset($attributes['readMoreText']) ? $attributes['readMoreText'] : 'Read Adventure'; |
| 13 | $show_date = isset($attributes['showDate']) ? $attributes['showDate'] : true; |
| 14 | $show_category = isset($attributes['showCategory']) ? $attributes['showCategory'] : true; |
| 15 | $show_excerpt = isset($attributes['showExcerpt']) ? $attributes['showExcerpt'] : true; |
| 16 | $show_read_more = isset($attributes['showReadMore']) ? $attributes['showReadMore'] : true; |
| 17 | $show_view_all = isset($attributes['showViewAll']) ? $attributes['showViewAll'] : true; |
| 18 | $view_all_text = isset($attributes['viewAllText']) ? $attributes['viewAllText'] : 'View All Posts →'; |
| 19 | $view_all_url = isset($attributes['viewAllUrl']) ? $attributes['viewAllUrl'] : '#'; |
| 20 | |
| 21 | $stories_style = isset($attributes['storiesStyle']) ? $attributes['storiesStyle'] : 'fun'; |
| 22 | |
| 23 | $is_fun = $stories_style === 'fun'; |
| 24 | $is_elegant = $stories_style === 'elegant'; |
| 25 | |
| 26 | $section_bg_color = isset($attributes['sectionBgColor']) ? $attributes['sectionBgColor'] : '#FFFDF5'; |
| 27 | if ($is_elegant && $section_bg_color === '#FFFDF5') { |
| 28 | $section_bg_color = '#ffffff'; |
| 29 | } |
| 30 | |
| 31 | $card_bg_color = isset($attributes['cardBgColor']) ? $attributes['cardBgColor'] : '#ffffff'; |
| 32 | $title_color = isset($attributes['titleColor']) ? $attributes['titleColor'] : '#264653'; |
| 33 | $meta_color = isset($attributes['metaColor']) ? $attributes['metaColor'] : '#EF476F'; |
| 34 | |
| 35 | // Override default colors for Elegant if user hasn't set custom ones (assuming defaults match fun style defaults) |
| 36 | if ($is_elegant) { |
| 37 | if ($title_color === '#264653') |
| 38 | $title_color = ''; // Use class color |
| 39 | if ($meta_color === '#EF476F') |
| 40 | $meta_color = ''; // Use class color |
| 41 | } |
| 42 | |
| 43 | $read_more_color = isset($attributes['readMoreColor']) && !empty($attributes['readMoreColor']) ? $attributes['readMoreColor'] : $meta_color; |
| 44 | $view_all_color = isset($attributes['viewAllColor']) && !empty($attributes['viewAllColor']) ? $attributes['viewAllColor'] : ''; |
| 45 | |
| 46 | // CSS Classes based on style |
| 47 | $header_class = $is_elegant |
| 48 | ? 'flex justify-between items-end mb-12 border-b border-gray-100 pb-6' |
| 49 | : 'flex justify-between items-end mb-12'; |
| 50 | |
| 51 | $title_class = $is_elegant |
| 52 | ? 'text-3xl md:text-4xl font-serif font-bold text-brand-dark text-center md:text-left' |
| 53 | : 'text-4xl font-black text-center md:text-left'; |
| 54 | |
| 55 | $view_all_class = $is_elegant |
| 56 | ? 'hidden md:inline-block font-medium text-brand-dark hover:text-gray-600 mb-2' |
| 57 | : 'hidden md:inline-block font-bold transition-colors mb-2 text-blue-sky hover:text-navy'; |
| 58 | |
| 59 | $inner_card_class = $is_elegant |
| 60 | ? '' |
| 61 | : 'overflow-hidden rounded-xl shadow-lg mb-4'; |
| 62 | |
| 63 | $image_wrapper_class = $is_elegant |
| 64 | ? 'h-56 rounded-lg bg-gray-100 mb-4 overflow-hidden' |
| 65 | : ''; // Fun uses inner_card for overflow hidden |
| 66 | |
| 67 | $image_class = $is_elegant |
| 68 | ? 'w-full h-full object-cover transition-transform duration-500 group-hover:scale-105' |
| 69 | : 'w-full h-48 object-cover transition-transform duration-500 group-hover:scale-110'; |
| 70 | |
| 71 | $meta_class = $is_elegant |
| 72 | ? 'text-brand-gray font-bold text-xs uppercase tracking-widest block mb-1' |
| 73 | : 'font-bold text-sm uppercase block mb-2'; |
| 74 | |
| 75 | $post_title_class = $is_elegant |
| 76 | ? 'text-xl font-serif font-bold text-brand-dark mt-2 group-hover:text-gray-600 transition-colors' |
| 77 | : 'text-xl font-black mt-2 group-hover:opacity-75 transition-opacity'; |
| 78 | |
| 79 | $args = array( |
| 80 | 'post_type' => 'post', |
| 81 | 'posts_per_page' => $posts_to_show, |
| 82 | 'post_status' => 'publish', |
| 83 | 'orderby' => $order_by, |
| 84 | 'order' => $order, |
| 85 | ); |
| 86 | |
| 87 | $latest_posts = new WP_Query($args); |
| 88 | |
| 89 | $wrapper_attributes = get_block_wrapper_attributes(array( |
| 90 | 'class' => 'awt-child-author-latest-stories py-24', |
| 91 | 'style' => 'background-color: ' . esc_attr($section_bg_color) . ';', |
| 92 | )); |
| 93 | ?> |
| 94 | |
| 95 | <section <?php echo $wrapper_attributes; ?>> |
| 96 | <div class="awt-container mx-auto px-6"> |
| 97 | <div class="<?php echo esc_attr($header_class); ?>"> |
| 98 | <h2 class="<?php echo esc_attr($title_class); ?>" |
| 99 | style="<?php echo $title_color ? 'color: ' . esc_attr($title_color) . ';' : ''; ?>"> |
| 100 | <?php echo esc_html($section_title); ?> |
| 101 | </h2> |
| 102 | <?php if ($show_view_all): ?> |
| 103 | <a href="<?php echo esc_url($view_all_url); ?>" class="<?php echo esc_attr($view_all_class); ?>" |
| 104 | style="<?php echo !empty($view_all_color) ? 'color: ' . esc_attr($view_all_color) . ';' : ''; ?>"> |
| 105 | <?php echo esc_html($view_all_text); ?> |
| 106 | </a> |
| 107 | <?php endif; ?> |
| 108 | </div> |
| 109 | |
| 110 | <?php if ($latest_posts->have_posts()): ?> |
| 111 | <div class="grid grid-cols-1 md:grid-cols-3 gap-8"> |
| 112 | <?php while ($latest_posts->have_posts()): |
| 113 | $latest_posts->the_post(); ?> |
| 114 | <article class="group cursor-pointer"> |
| 115 | <div class="<?php echo esc_attr($inner_card_class); ?>" |
| 116 | style="<?php echo $is_fun ? 'background-color: ' . esc_attr($card_bg_color) . ';' : ''; ?>"> |
| 117 | |
| 118 | <?php if ($is_elegant): ?> |
| 119 | <div class="<?php echo esc_attr($image_wrapper_class); ?>"><?php endif; ?> |
| 120 | |
| 121 | <a href="<?php the_permalink(); ?>" class="block"> |
| 122 | <?php if (has_post_thumbnail()): ?> |
| 123 | <img src="<?php echo get_the_post_thumbnail_url(get_the_ID(), 'medium_large'); ?>" |
| 124 | alt="<?php the_title_attribute(); ?>" class="<?php echo esc_attr($image_class); ?>"> |
| 125 | <?php else: ?> |
| 126 | <div class="w-full h-48 bg-gray-200 flex items-center justify-center text-gray-400"> |
| 127 | <?php esc_html_e('No Image', 'author-website-templates'); ?> |
| 128 | </div> |
| 129 | <?php endif; ?> |
| 130 | </a> |
| 131 | |
| 132 | <?php if ($is_elegant): ?> |
| 133 | </div><?php endif; ?> |
| 134 | </div> |
| 135 | |
| 136 | <?php if ($show_date || $show_category): ?> |
| 137 | <span class="<?php echo esc_attr($meta_class); ?>" |
| 138 | style="<?php echo $meta_color ? 'color: ' . esc_attr($meta_color) . ';' : ''; ?>"> |
| 139 | <?php |
| 140 | if ($show_date) { |
| 141 | echo get_the_date(); |
| 142 | } |
| 143 | |
| 144 | if ($show_date && $show_category && has_category()) { |
| 145 | echo ' • '; |
| 146 | } |
| 147 | |
| 148 | if ($show_category) { |
| 149 | $categories = get_the_category(); |
| 150 | if (!empty($categories)) { |
| 151 | echo esc_html($categories[0]->name); |
| 152 | } |
| 153 | } |
| 154 | ?> |
| 155 | </span> |
| 156 | <?php endif; ?> |
| 157 | |
| 158 | <h3 class="<?php echo esc_attr($post_title_class); ?>"> |
| 159 | <a href="<?php the_permalink(); ?>" |
| 160 | style="<?php echo $title_color ? 'color: ' . esc_attr($title_color) . ';' : ''; ?>"> |
| 161 | <?php the_title(); ?> |
| 162 | </a> |
| 163 | </h3> |
| 164 | |
| 165 | <?php if ($show_excerpt): ?> |
| 166 | <div class="text-gray-600 mt-2 text-base leading-relaxed"> |
| 167 | <?php echo wp_trim_words(get_the_excerpt(), 20); ?> |
| 168 | </div> |
| 169 | <?php endif; ?> |
| 170 | |
| 171 | <?php if ($show_read_more): ?> |
| 172 | <a href="<?php the_permalink(); ?>" class="mt-4 font-bold inline-block hover:underline" |
| 173 | style="color: <?php echo esc_attr($read_more_color); ?>;"> |
| 174 | <?php echo esc_html($read_more_text); ?> → |
| 175 | </a> |
| 176 | <?php endif; ?> |
| 177 | </article> |
| 178 | <?php endwhile; ?> |
| 179 | </div> |
| 180 | <?php wp_reset_postdata(); ?> |
| 181 | <?php else: ?> |
| 182 | <div class="text-center p-8 border-2 border-dashed border-gray-300 rounded-lg bg-white/50"> |
| 183 | <p class="text-gray-500 text-lg"> |
| 184 | <?php esc_html_e('No adventures found yet!', 'author-website-templates'); ?> |
| 185 | </p> |
| 186 | </div> |
| 187 | <?php endif; ?> |
| 188 | </div> |
| 189 | </section> |