PluginProbe ʕ •ᴥ•ʔ
Author Website Templates – Create Writer, Author & Publisher Websites Easily / 1.1.5
Author Website Templates – Create Writer, Author & Publisher Websites Easily v1.1.5
trunk 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9
author-website-templates / build / blocks / child-author / book-list / render.php
author-website-templates / build / blocks / child-author / book-list Last commit date
block.json 4 months ago index.asset.php 4 months ago index.js 4 months ago render.php 4 months ago
render.php
335 lines
1 <?php
2 /**
3 * Book List Block - Server-Side Render
4 *
5 * @package Author_Website_Templates
6 * @since 1.0.9
7 */
8
9 // Prevent direct access
10 if (!defined('ABSPATH')) {
11 exit;
12 }
13
14 // Extract attributes
15 $posts_to_show = isset($attributes['postsToShow']) ? absint($attributes['postsToShow']) : 5;
16 $categories = isset($attributes['categories']) ? sanitize_text_field($attributes['categories']) : '';
17 $show_load_more = isset($attributes['showLoadMore']) ? (bool) $attributes['showLoadMore'] : true;
18 $load_more_text = isset($attributes['loadMoreText']) ? sanitize_text_field($attributes['loadMoreText']) : __('Load More Books', 'author-website-templates');
19 $block_id = isset($attributes['blockId']) ? sanitize_text_field($attributes['blockId']) : 'awt-book-list-' . wp_rand();
20 $book_list_style = isset($attributes['bookListStyle']) ? sanitize_text_field($attributes['bookListStyle']) : 'fun';
21
22 // Display Toggles
23 $show_category = isset($attributes['showCategory']) ? (bool) $attributes['showCategory'] : true;
24 $show_description = isset($attributes['showDescription']) ? (bool) $attributes['showDescription'] : true;
25 $show_buy_btn = isset($attributes['showBuyBtn']) ? (bool) $attributes['showBuyBtn'] : true;
26 $show_details_btn = isset($attributes['showDetailsBtn']) ? (bool) $attributes['showDetailsBtn'] : true;
27 $desc_source = isset($attributes['descSource']) ? sanitize_text_field($attributes['descSource']) : 'excerpt';
28
29 // Colors
30 $title_color = isset($attributes['titleColor']) ? $attributes['titleColor'] : '';
31 $desc_color = isset($attributes['descColor']) ? $attributes['descColor'] : '';
32 $cat_color = isset($attributes['catColor']) ? $attributes['catColor'] : '';
33
34 // Buy Button Colors
35 $btn_bg = isset($attributes['btnBg']) ? $attributes['btnBg'] : '';
36 $btn_text = isset($attributes['btnText']) ? $attributes['btnText'] : '';
37 $btn_border = isset($attributes['btnBorder']) ? $attributes['btnBorder'] : '';
38 $btn_hover_bg = isset($attributes['btnHoverBg']) ? $attributes['btnHoverBg'] : '';
39 $btn_hover_text = isset($attributes['btnHoverText']) ? $attributes['btnHoverText'] : '';
40 $btn_hover_border = isset($attributes['btnHoverBorder']) ? $attributes['btnHoverBorder'] : '';
41
42 // View Button Colors
43 $view_btn_bg = isset($attributes['viewBtnBg']) ? $attributes['viewBtnBg'] : '';
44 $view_btn_text = isset($attributes['viewBtnText']) ? $attributes['viewBtnText'] : '';
45 $view_btn_border = isset($attributes['viewBtnBorder']) ? $attributes['viewBtnBorder'] : '';
46 $view_btn_hover_bg = isset($attributes['viewBtnHoverBg']) ? $attributes['viewBtnHoverBg'] : '';
47 $view_btn_hover_text = isset($attributes['viewBtnHoverText']) ? $attributes['viewBtnHoverText'] : '';
48 $view_btn_hover_border = isset($attributes['viewBtnHoverBorder']) ? $attributes['viewBtnHoverBorder'] : '';
49
50 // Load More Button Colors
51 $loadmore_bg = isset($attributes['loadMoreBtnBg']) ? $attributes['loadMoreBtnBg'] : '';
52 $loadmore_text = isset($attributes['loadMoreBtnText']) ? $attributes['loadMoreBtnText'] : '';
53 $loadmore_hover_bg = isset($attributes['loadMoreBtnHoverBg']) ? $attributes['loadMoreBtnHoverBg'] : '';
54 $loadmore_hover_text = isset($attributes['loadMoreBtnHoverText']) ? $attributes['loadMoreBtnHoverText'] : '';
55
56 // Build query arguments
57 $args = array(
58 'post_type' => 'book',
59 'posts_per_page' => $posts_to_show,
60 'post_status' => 'publish',
61 'paged' => 1,
62 );
63
64 // Add category filter if specified
65 if (!empty($categories)) {
66 $cat_ids = array_map('absint', explode(',', $categories));
67 $args['tax_query'] = array(
68 array(
69 'taxonomy' => 'book-category',
70 'field' => 'term_id',
71 'terms' => $cat_ids,
72 ),
73 );
74 }
75
76 // Run query
77 $book_query = new WP_Query($args);
78
79 // Prepare data attributes for AJAX
80 $query_data = array(
81 'post_type' => 'book',
82 'posts_per_page' => $posts_to_show,
83 'categories' => $categories,
84 );
85
86 $container_class = 'awt-book-list-container awt-container container mx-auto px-6 py-24';
87 $container_class .= ($book_list_style === 'elegant') ? ' max-w-6xl space-y-24' : ' max-w-5xl space-y-20';
88
89 // Generate CSS Variables for Container
90 $css_variables = [
91 '--awt-title-color' => $title_color,
92 '--awt-desc-color' => $desc_color,
93 '--awt-cat-color' => $cat_color,
94 '--awt-btn-bg' => $btn_bg,
95 '--awt-btn-text' => $btn_text,
96 '--awt-btn-border' => $btn_border,
97 '--awt-btn-hover-bg' => $btn_hover_bg,
98 '--awt-btn-hover-text' => $btn_hover_text,
99 '--awt-btn-hover-border' => $btn_hover_border,
100 '--awt-view-btn-bg' => $view_btn_bg,
101 '--awt-view-btn-text' => $view_btn_text,
102 '--awt-view-btn-border' => $view_btn_border,
103 '--awt-view-btn-hover-bg' => $view_btn_hover_bg,
104 '--awt-view-btn-hover-text' => $view_btn_hover_text,
105 '--awt-view-btn-hover-border' => $view_btn_hover_border,
106 '--awt-loadmore-bg' => $loadmore_bg,
107 '--awt-loadmore-text' => $loadmore_text,
108 '--awt-loadmore-hover-bg' => $loadmore_hover_bg,
109 '--awt-loadmore-hover-text' => $loadmore_hover_text,
110 ];
111
112 $style_attr = '';
113 foreach ($css_variables as $var => $value) {
114 if (!empty($value)) {
115 $style_attr .= "{$var}: {$value}; ";
116 }
117 }
118
119 // Global Scoped Styles (Applied to both styles, using vars)
120 $css_rules = [];
121
122 // Typography
123 if ($title_color) $css_rules[] = "#{$block_id} h3, #{$block_id} .awt-book-title { color: var(--awt-title-color) !important; }";
124 if ($desc_color) $css_rules[] = "#{$block_id} p, #{$block_id} .awt-book-desc { color: var(--awt-desc-color) !important; }";
125 if ($cat_color) $css_rules[] = "#{$block_id} span, #{$block_id} .awt-book-cat { color: var(--awt-cat-color) !important; }";
126
127 // Buy Button Colors
128 if ($btn_bg || $btn_text || $btn_border) {
129 // Normal State
130 $rule = "#{$block_id} .awt-btn-buy, #{$block_id} .btn-yellow {";
131 if ($btn_bg) $rule .= "background-color: var(--awt-btn-bg) !important;";
132 if ($btn_text) $rule .= "color: var(--awt-btn-text) !important;";
133 if ($btn_border) $rule .= "border-color: var(--awt-btn-border) !important;";
134 $rule .= "}";
135 $css_rules[] = $rule;
136 }
137
138 if ($btn_hover_bg || $btn_hover_text || $btn_hover_border) {
139 // Hover State
140 $rule = "#{$block_id} .awt-btn-buy:hover, #{$block_id} .btn-yellow:hover {";
141 if ($btn_hover_bg) $rule .= "background-color: var(--awt-btn-hover-bg) !important;";
142 if ($btn_hover_text) $rule .= "color: var(--awt-btn-hover-text) !important;";
143 if ($btn_hover_border) $rule .= "border-color: var(--awt-btn-hover-border) !important;";
144 $rule .= "}";
145 $css_rules[] = $rule;
146 }
147
148 // View Button Colors
149 if ($view_btn_bg || $view_btn_text || $view_btn_border) {
150 // Normal State
151 $rule = "#{$block_id} .awt-btn-view, #{$block_id} .btn-white {";
152 if ($view_btn_bg) $rule .= "background-color: var(--awt-view-btn-bg) !important;";
153 if ($view_btn_text) $rule .= "color: var(--awt-view-btn-text) !important;";
154 if ($view_btn_border) $rule .= "border-color: var(--awt-view-btn-border) !important;";
155 $rule .= "}";
156 $css_rules[] = $rule;
157 }
158
159 if ($view_btn_hover_bg || $view_btn_hover_text || $view_btn_hover_border) {
160 // Hover State
161 $rule = "#{$block_id} .awt-btn-view:hover, #{$block_id} .btn-white:hover {";
162 if ($view_btn_hover_bg) $rule .= "background-color: var(--awt-view-btn-hover-bg) !important;";
163 if ($view_btn_hover_text) $rule .= "color: var(--awt-view-btn-hover-text) !important;";
164 if ($view_btn_hover_border) $rule .= "border-color: var(--awt-view-btn-hover-border) !important;";
165 $rule .= "}";
166 $css_rules[] = $rule;
167 }
168
169 // Load More Button Colors
170 if ($loadmore_bg || $loadmore_text) {
171 // Normal State
172 $rule = "#{$block_id} .awt-load-more-btn {";
173 if ($loadmore_bg) $rule .= "background-color: var(--awt-loadmore-bg) !important;";
174 if ($loadmore_text) $rule .= "color: var(--awt-loadmore-text) !important;";
175 $rule .= "}";
176 $css_rules[] = $rule;
177 }
178
179 if ($loadmore_hover_bg || $loadmore_hover_text) {
180 // Hover State
181 $rule = "#{$block_id} .awt-load-more-btn:hover {";
182 if ($loadmore_hover_bg) $rule .= "background-color: var(--awt-loadmore-hover-bg) !important;";
183 if ($loadmore_hover_text) $rule .= "color: var(--awt-loadmore-hover-text) !important;";
184 $rule .= "}";
185 $css_rules[] = $rule;
186 }
187
188 $style_block = '';
189 if (!empty($css_rules)) {
190 $style_block = "<style>" . implode("\n", $css_rules) . "</style>";
191 }
192 ?>
193
194 <?php echo $style_block; ?>
195 <div class="<?php echo esc_attr($container_class); ?>" id="<?php echo esc_attr($block_id); ?>"
196 style="<?php echo esc_attr($style_attr); ?>"
197 data-query="<?php echo esc_attr(wp_json_encode($query_data)); ?>" data-current-page="1"
198 data-max-pages="<?php echo esc_attr($book_query->max_num_pages); ?>">
199 <?php
200 // Render book list based on style
201 if ($book_list_style === 'elegant') {
202 // Custom Loop for Elegant Style
203 if ($book_query->have_posts()) {
204 while ($book_query->have_posts()) {
205 $book_query->the_post();
206
207 // Get Book Details
208 $book_id = get_the_ID();
209 $permalink = get_permalink();
210 $title = get_the_title();
211
212 // Description Logic
213 if ($desc_source === 'full') {
214 $description = get_the_content();
215 $description = wp_filter_nohtml_kses($description); // Basic cleanup
216 $description = strip_tags(get_the_content());
217 $description = wp_trim_words($description, 50, '...'); // Safe fallback
218 } else {
219 $description = get_post_meta($book_id, '_rsbs_short_description', true);
220 if(!$description) $description = get_the_excerpt();
221 }
222
223 $thumbnail = get_the_post_thumbnail_url($book_id, 'large');
224
225 // Get Category
226 $terms = get_the_terms($book_id, 'book-category');
227 $category = (!empty($terms) && !is_wp_error($terms)) ? $terms[0]->name : 'Fiction';
228
229 // Get Buy Link (Custom Meta or Fallback)
230 $buy_url = get_post_meta($book_id, 'awt_book_buy_url', true);
231 if(!$buy_url) $buy_url = '#';
232
233 // --- STYLE CLASSES START (Styles handled by CSS Vars above) ---
234
235 // Category Classes
236 $cat_classes = 'font-bold uppercase tracking-widest text-xs mb-2 block awt-book-cat';
237 if (!$cat_color) $cat_classes .= ' text-slate-500';
238
239 // Title Classes
240 $title_classes = 'text-3xl md:text-4xl font-serif font-bold my-3 transition-colors awt-book-title';
241 if (!$title_color) $title_classes .= ' text-slate-800 hover:text-slate-500';
242
243 // Description Classes
244 $desc_classes = 'text-lg mb-8 leading-relaxed font-light awt-book-desc';
245 if (!$desc_color) $desc_classes .= ' text-slate-500';
246
247 // Buy Button Classes
248 $btn_buy_classes = 'inline-block px-8 py-3 rounded font-medium transition-all duration-300 ease-in-out border text-sm uppercase tracking-wider shadow-sm hover:shadow-md text-center awt-btn-buy';
249 if (!$btn_bg) $btn_buy_classes .= ' bg-slate-900 hover:bg-slate-800';
250 if (!$btn_text) $btn_buy_classes .= ' text-white';
251 if (!$btn_border) $btn_buy_classes .= ' border-transparent';
252
253 // View Details Button Classes
254 $btn_view_classes = 'inline-block px-8 py-3 rounded font-medium transition-all duration-300 ease-in-out border text-sm uppercase tracking-wider text-center awt-btn-view';
255 if (!$view_btn_bg) $btn_view_classes .= ' bg-white hover:bg-slate-800';
256 if (!$view_btn_text) $btn_view_classes .= ' text-slate-800 hover:text-white';
257 if (!$view_btn_border) $btn_view_classes .= ' border-slate-800';
258
259 // --- STYLE CLASSES END ---
260
261 ?>
262 <article class="grid grid-cols-1 md:grid-cols-12 gap-12 items-center">
263 <div class="md:col-span-4 flex justify-center md:justify-start">
264 <a href="<?php echo esc_url($permalink); ?>" class="block w-full max-w-xs">
265 <?php if ($thumbnail): ?>
266 <img src="<?php echo esc_url($thumbnail); ?>" alt="<?php echo esc_attr($title); ?>"
267 class="rounded shadow-xl w-full hover:scale-[1.02] transition-transform duration-500">
268 <?php else: ?>
269 <div class="bg-gray-200 rounded shadow-xl w-full h-64 flex items-center justify-center text-gray-400">No
270 Cover</div>
271 <?php endif; ?>
272 </a>
273 </div>
274 <div class="md:col-span-8 text-center md:text-left">
275 <?php if($show_category): ?>
276 <span class="<?php echo esc_attr($cat_classes); ?>">
277 <?php echo esc_html($category); ?>
278 </span>
279 <?php endif; ?>
280
281 <a href="<?php echo esc_url($permalink); ?>">
282 <h3 class="<?php echo esc_attr($title_classes); ?>">
283 <?php echo esc_html($title); ?>
284 </h3>
285 </a>
286
287 <?php if($show_description): ?>
288 <p class="<?php echo esc_attr($desc_classes); ?>">
289 <?php echo wp_kses_post($description); ?>
290 </p>
291 <?php endif; ?>
292
293 <div class="flex flex-col sm:flex-row gap-4 justify-center md:justify-start">
294 <?php if($show_buy_btn): ?>
295 <a href="<?php echo esc_url($buy_url); ?>"
296 class="<?php echo esc_attr($btn_buy_classes); ?>"><?php esc_html_e('Buy Now', 'author-website-templates'); ?></a>
297 <?php endif; ?>
298
299 <?php if($show_details_btn): ?>
300 <a href="<?php echo esc_url($permalink); ?>"
301 class="<?php echo esc_attr($btn_view_classes); ?>"><?php esc_html_e('View Details', 'author-website-templates'); ?></a>
302 <?php endif; ?>
303 </div>
304 </div>
305 </article>
306
307 <?php if ($book_query->current_post + 1 < $book_query->post_count): ?>
308 <hr class="border-gray-100">
309 <?php endif; ?>
310 <?php
311 }
312 wp_reset_postdata();
313 } else {
314 echo '<p>' . esc_html__('No books found.', 'author-website-templates') . '</p>';
315 }
316
317 } else {
318 // Default "Fun" Style (using existing helper)
319 if (function_exists('rswpthemes_awt_render_book_list')) {
320 echo rswpthemes_awt_render_book_list($book_query);
321 } else {
322 echo '<p>' . esc_html__('Book list renderer not available.', 'author-website-templates') . '</p>';
323 }
324 }
325 ?>
326 <?php
327 if ($show_load_more && $book_query->max_num_pages > 1): ?>
328 <div class="text-center mt-12">
329 <button id="awt-load-more-btn-<?php echo esc_attr($block_id); ?>" class="btn-fun btn-yellow awt-load-more-btn"
330 data-container="#<?php echo esc_attr($block_id); ?>">
331 <?php echo esc_html($load_more_text); ?>
332 </button>
333 </div>
334 <?php endif; ?>
335 </div>