PluginProbe
GutSlider – All in One Slider and Carousel Blocks for Gutenberg / 2.9.9
GutSlider – All in One Slider and Carousel Blocks for Gutenberg v2.9.9
3.1.0 3.0.0 2.13.2 2.13.1 2.13.0 trunk 1.0.0 2.1.0 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.1 2.5.3 2.5.4 2.5.5 2.6.1 All 56 releases
← All changes | build/blocks/post-slider/render.php +195 -261 2.3.0 → 2.9.9 View file →
@@ -1,243 +1,205 @@
1 1 <?php
2 2 /**
3 3 * Render the post slider block.
4 - *
4 + *
5 + * @param array $attributes The block attributes.
6 + * @return string The rendered block content.
5 7 */
6 8
7 9 // Exit if accessed directly.
8 -if( ! defined( 'ABSPATH' ) ) exit;
10 +if ( ! defined( 'ABSPATH' ) ) {
11 + exit;
12 +}
9 13
10 - if( ! function_exists( 'gutslider_post_slider' ) ) {
11 -
14 +if ( ! function_exists( 'gutslider_post_slider' ) ) {
12 15 function gutslider_post_slider( $attributes ) {
16 + // Extract attributes with default values
17 + $defaults = [
18 + 'queryPosts' => [],
19 + 'showTitle' => true,
20 + 'showCat' => true,
21 + 'showExcerpt' => true,
22 + 'showBtn' => true,
23 + 'linkTitle' => true,
24 + 'titleTag' => 'h2',
25 + 'btnLabel' => __( 'Read More', 'slider-blocks' ),
26 + 'enableOverlay' => true,
27 + 'sliderType' => 'slider',
28 + 'uniqueId' => '',
29 + 'navContainerPosition' => 'nav_inside',
30 + 'navPosition' => 'nav_cc',
31 + 'catAnimation' => '',
32 + 'titleAnimation' => '',
33 + 'exptAnimation' => '',
34 + 'btnAnimation' => '',
35 + 'sliderOptions' => [],
36 + 'showNavigation' => true,
37 + 'showPagination' => true,
38 + 'customNavIcon' => false,
39 + 'navIconSource' => 'library',
40 + 'navPrevIcon' => 'arrowLeft',
41 + 'navNextIcon' => 'arrowRight',
42 + 'customPrevSVG' => '',
43 + 'customNextSVG' => '',
44 + 'excerptLength' => 20
45 + ];
13 46
14 - // block attributes: Post Query
15 - $queryPosts = isset( $attributes['queryPosts'] ) ? $attributes['queryPosts'] : [];
47 + $atts = wp_parse_args( $attributes, $defaults );
16 48
17 - // block attributes
18 - $showTitle = isset( $attributes['showTitle'] ) ? $attributes['showTitle'] : true;
19 - $showCat = isset( $attributes['showCat'] ) ? $attributes['showCat'] : true;
20 - $showExcerpt = isset( $attributes['showExcerpt'] ) ? $attributes['showExcerpt'] : true;
21 - $showBtn = isset( $attributes['showBtn'] ) ? $attributes['showBtn'] : true;
22 - $linkTitle = isset( $attributes['linkTitle'] ) ? $attributes['linkTitle'] : true;
23 - $titleTag = isset( $attributes['titleTag'] ) ? $attributes['titleTag'] : 'h2';
24 - $btnLabel = isset( $attributes['btnLabel'] ) ? $attributes['btnLabel'] : 'Read More';
25 - $enableOverlay = isset( $attributes['enableOverlay'] ) ? $attributes['enableOverlay'] : true;
26 - $sliderType = isset( $attributes['sliderType'] ) ? $attributes['sliderType'] : 'slider';
27 - $uniqueId = isset( $attributes['uniqueId'] ) ? $attributes['uniqueId'] : '';
28 - $navContainerPosition = isset( $attributes['navContainerPosition'] ) ? $attributes['navContainerPosition'] : 'nav_inside';
29 - $navPosition = isset( $attributes['navPosition'] ) ? $attributes['navPosition'] : 'nav_cc';
49 + // Sanitize attributes
50 + $atts = array_map( 'sanitize_text_field', $atts );
51 + $atts['queryPosts'] = isset( $attributes['queryPosts'] ) ? $attributes['queryPosts'] : [];
52 + $atts['sliderOptions'] = isset( $attributes['sliderOptions'] ) ? $attributes['sliderOptions'] : [];
53 + $atts['focusPoints'] = isset( $attributes['focusPoints'] ) ? $attributes['focusPoints'] : [];
30 54
31 - // animation
32 - $catAnimation = isset( $attributes['catAnimation'] ) ? $attributes['catAnimation'] : '';
33 - $titleAnimation = isset( $attributes['titleAnimation'] ) ? $attributes['titleAnimation'] : '';
34 - $exptAnimation = isset( $attributes['exptAnimation'] ) ? $attributes['exptAnimation'] : '';
35 - $btnAnimation = isset( $attributes['btnAnimation'] ) ? $attributes['btnAnimation'] : '';
55 + // Block classes
56 + $block_classes = [
57 + esc_attr( $atts['uniqueId'] ),
58 + esc_attr( $atts['navContainerPosition'] ),
59 + esc_attr( $atts['navPosition'] ),
60 + ];
36 61
37 - // slider options
38 - $sliderOptions = isset( $attributes['sliderOptions'] ) ? $attributes['sliderOptions'] : [];
39 - $showNavigation = isset( $attributes['showNavigation'] ) ? $attributes['showNavigation'] : true;
40 - $showPagination = isset( $attributes['showPagination'] ) ? $attributes['showPagination'] : true;
41 - $customNavIcon = isset( $attributes['customNavIcon'] ) ? $attributes['customNavIcon'] : false;
42 - $navIconSource = isset( $attributes['navIconSource'] ) ? $attributes['navIconSource'] : 'library';
43 - $navPrevIcon = isset( $attributes['navPrevIcon'] ) ? $attributes['navPrevIcon'] : 'arrowLeft';
44 - $navNextIcon = isset( $attributes['navNextIcon'] ) ? $attributes['navNextIcon'] : 'arrowRight';
45 - $customPrevSVG = isset( $attributes['customPrevSVG'] ) ? $attributes['customPrevSVG'] : '';
46 - $customNextSVG = isset( $attributes['customNextSVG'] ) ? $attributes['customNextSVG'] : '';
47 - $excerptLength = isset( $attributes['excerptLength'] ) ? $attributes['excerptLength'] : 20;
62 + // Block wrapper attributes
63 + $block_props = get_block_wrapper_attributes( [
64 + 'class' => implode( ' ', array_filter( $block_classes ) )
65 + ] );
48 66
49 - // block classes
50 - $blockClasses = [ $uniqueId, $navContainerPosition, $navPosition ];
51 - // block wrapper attributes
52 - $blockProps = get_block_wrapper_attributes([
53 - 'class' => implode( ' ', $blockClasses )
54 - ]);
55 -
56 - // single block classes
57 - $swiperClasses = implode( ' ', [
67 + // Single block classes
68 + $swiper_classes = [
58 69 'swiper-slide',
59 - $sliderType === 'slider' ? 'slide-mode' : ''
60 - ]);
70 + $atts['sliderType'] === 'slider' ? 'slide-mode' : ''
71 + ];
61 72
62 - // block inner html sliderOptions
63 73 ?>
64 - <div <?php echo $blockProps; ?> data-swiper-options="<?php echo esc_attr( wp_json_encode( $sliderOptions ) ); ?>">
74 + <div <?php echo wp_kses_post( $block_props ); ?> data-swiper-options="<?php echo esc_attr( wp_json_encode( $atts['sliderOptions'] ) ); ?>">
65 75 <div class="swiper">
66 76 <div class="swiper-wrapper">
67 - <?php
68 - if( is_array( $queryPosts ) && ! empty( $queryPosts ) ) {
77 + <?php
78 + if ( is_array( $atts['queryPosts'] ) && ! empty( $atts['queryPosts'] ) ) {
79 + $index = 0;
80 + foreach ( $atts['queryPosts'] as $post ) {
81 + $index++;
82 + $post_title = isset( $post['title']['rendered'] ) ? $post['title']['rendered'] : '';
83 + $post_excerpt = isset( $post['excerpt']['rendered'] ) ? $post['excerpt']['rendered'] : '';
84 + $post_link = isset( $post['link'] ) ? $post['link'] : '';
85 + $post_image_id = isset( $post['featured_media'] ) ? $post['featured_media'] : '';
86 + $post_image = wp_get_attachment_image_url( $post_image_id, 'full' );
87 + // Check if focusPoints is set and not empty
88 + $focusPoints = isset( $atts['focusPoints'] ) && is_array( $atts['focusPoints'] ) ? $atts['focusPoints'] : [];
89 + $bg_position = isset( $focusPoints[$index - 1] ) && isset( $focusPoints[$index - 1]['x'] ) && isset( $focusPoints[$index - 1]['y'] )
90 + ? sprintf( 'background-position: %d%% %d%%;', round( $focusPoints[$index - 1]['x'] * 100 ), round( $focusPoints[$index - 1]['y'] * 100 ) )
91 + : 'background-position: center center;';
92 + $bg_style = $post_image ? sprintf( 'background-image: url(%s); %s', esc_url( $post_image ), $bg_position ) : $bg_position;
93 + $post_categories = isset( $post['categories'] ) ? $post['categories'] : [];
69 94
70 - foreach ($queryPosts as $post ) {
71 -
72 - $postTitle = isset( $post['title']['rendered'] ) ? $post['title']['rendered'] : '';
73 - $postExcerpt = isset( $post['excerpt']['rendered'] ) ? $post['excerpt']['rendered'] : '';
74 - $postLink = isset( $post['link'] ) ? $post['link'] : '';
75 - $postImageId = isset( $post['featured_media'] ) ? $post['featured_media'] : '';
76 - $postImage = wp_get_attachment_image_url( $postImageId, 'full', false );
77 - $bgStyle = !empty( $postImage ) ? 'background-image: url(' . esc_url( $postImage ) . ');' : '';
78 - $postCategories = isset( $post['categories'] ) ? $post['categories'] : [];
79 -
80 - ?>
81 -
82 - <div class="<?php echo esc_attr( $swiperClasses ); ?>">
83 - <div class="swiper-container-outer" style="<?php echo $bgStyle; ?>">
84 - <?php
85 - if( $enableOverlay ) {
95 + ?>
96 + <div class="<?php echo esc_attr( implode( ' ', $swiper_classes ) ); ?>">
97 + <div class="swiper-container-outer" style="<?php echo esc_attr( $bg_style ); ?>">
98 + <?php if ( $atts['enableOverlay'] ) : ?>
99 + <div class="gutslider-overlay"></div>
100 + <?php endif; ?>
101 + <div class="gutslider-content-wrapper">
102 + <div class="gutslider-content-inner">
103 + <?php if ( $atts['showCat'] ) : ?>
104 + <div class="post-categories <?php echo esc_attr( $atts['catAnimation'] ); ?>">
105 + <?php
106 + foreach ( $post_categories as $category_id ) {
107 + $category = get_category( $category_id );
108 + if ( $category ) {
109 + printf( '<span class="post-category">%s</span>', esc_html( $category->name ) );
110 + }
111 + }
86 112 ?>
87 - <div class="gutslider-overlay"></div>
88 - <?php
89 - }
90 - ?>
91 - <div class="gutslider-content-wrapper">
92 - <div class="gutslider-content-inner">
93 - <?php
94 - // post categories
95 - if( $showCat ) {
96 - ?>
97 - <div class="post-categories <?php echo esc_attr( $catAnimation ); ?>">
98 - <?php
99 - if( is_array( $postCategories ) && ! empty( $postCategories ) ) {
100 - foreach ($postCategories as $category ) {
101 - $catId = isset( $category ) ? $category : '';
102 - $category = get_category( $catId );
103 - ?>
104 - <span class="post-category">
105 - <?php
106 - echo esc_html( $category->name );
107 - ?>
108 - </span>
109 - <?php
110 - }
111 - }
112 - ?>
113 - </div>
114 - <?php
115 - }
113 + </div>
114 + <?php endif; ?>
116 115
117 - // post title
118 - if( $showTitle ) {
119 - if( $linkTitle ) {
120 - ?>
121 - <a href="<?php echo esc_url( $postLink ); ?>" class="post-title">
122 - <<?php echo esc_attr( $titleTag ); ?> class="post-title <?php echo esc_attr( $titleAnimation ); ?>">
123 - <?php
124 - echo esc_html( $postTitle );
125 - ?>
126 - </<?php echo esc_attr( $titleTag ); ?>>
127 - </a>
128 - <?php
129 - } else {
130 - ?>
131 - <<?php echo esc_attr( $titleTag ); ?> class="post-title <?php echo esc_attr( $titleAnimation ); ?>">
132 - <?php
133 - echo esc_html( $postTitle );
134 - ?>
135 - </<?php echo esc_attr( $titleTag ); ?>>
136 - <?php
137 - }
138 - }
116 + <?php if ( $atts['showTitle'] ) : ?>
117 + <?php
118 + $title_tag = tag_escape( $atts['titleTag'] );
119 + $title_content = sprintf(
120 + '<%1$s class="post-title %2$s">%3$s</%1$s>',
121 + $title_tag,
122 + esc_attr( $atts['titleAnimation'] ),
123 + esc_html( $post_title )
124 + );
139 125
140 - // post excerpt
141 - if( $showExcerpt ) {
142 - ?>
143 - <div class="post-excerpt <?php echo esc_attr( $exptAnimation ); ?>">
144 - <?php
145 - if( $excerptLength > 0 ) {
146 - $postExcerpt = wp_trim_words( $postExcerpt, $excerptLength, '...' );
147 - }
148 - echo wp_kses( $postExcerpt, wp_kses_allowed_html( 'post' ) );
149 - ?>
150 - </div>
151 - <?php
152 - }
126 + if ( $atts['linkTitle'] ) {
127 + printf(
128 + '<a href="%s" class="post-title">%s</a>',
129 + esc_url( $post_link ),
130 + $title_content // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
131 + );
132 + } else {
133 + echo wp_kses_post( $title_content );
134 + }
135 + ?>
136 + <?php endif; ?>
153 137
154 - // post button
155 - if( $showBtn ) {
156 - ?>
157 - <div class="post-cta-wrapper <?php echo esc_attr( $btnAnimation ); ?>">
158 - <a href="<?php echo esc_url( $postLink ); ?>" class="post-cta">
159 - <?php
160 - echo esc_html( $btnLabel );
161 - ?>
162 - </a>
163 - </div>
164 - <?php
138 + <?php if ( $atts['showExcerpt'] ) : ?>
139 + <div class="post-excerpt <?php echo esc_attr( $atts['exptAnimation'] ); ?>">
140 + <?php
141 + if ( $atts['excerptLength'] > 0 ) {
142 + $post_excerpt = wp_trim_words( $post_excerpt, $atts['excerptLength'], '...' );
165 143 }
144 + echo wp_kses_post( $post_excerpt );
166 145 ?>
167 146 </div>
168 - </div>
147 + <?php endif; ?>
148 +
149 + <?php if ( $atts['showBtn'] ) : ?>
150 + <div class="post-cta-wrapper <?php echo esc_attr( $atts['btnAnimation'] ); ?>">
151 + <a href="<?php echo esc_url( $post_link ); ?>" class="post-cta">
152 + <?php echo esc_html( $atts['btnLabel'] ); ?>
153 + </a>
154 + </div>
155 + <?php endif; ?>
169 156 </div>
170 157 </div>
171 - <?php
172 - }
173 -
158 + </div>
159 + </div>
160 + <?php
174 161 }
175 - ?>
162 + }
163 + ?>
176 164 </div>
177 165 <?php
178 - if( $showNavigation === true && $navContainerPosition === 'nav_inside' ) {
166 + if( $atts['showNavigation'] === '1' && $atts['navContainerPosition'] === 'nav_inside' ) {
179 167 ?>
180 - <div class="gutslider-nav nav_inside <?php echo esc_attr( $navPosition ); ?>">
181 - <?php
182 - if( $customNavIcon ) {
183 - ?>
184 - <div class="gutslider-prev">
185 - <?php
186 - if( $navIconSource === 'library' ) {
187 - ?>
188 - <div class="gutslider-prev-icon">
189 - <?php
190 - echo gutslider_display_icon( $navPrevIcon );
191 - ?>
192 - </div>
193 - <?php
194 - } else {
195 - ?>
196 - <div class="gutslider-prev-icon">
197 - <?php
198 - echo $customPrevSVG;
199 - ?>
200 - </div>
201 - <?php
202 - }
203 - ?>
204 - </div>
205 - <div class="gutslider-next">
206 - <?php
207 - if( $navIconSource === 'library' ) {
208 - ?>
209 - <div class="gutslider-next-icon">
210 - <?php
211 - echo gutslider_display_icon( $navNextIcon );
212 - ?>
213 - </div>
214 - <?php
215 - } else {
216 - ?>
217 - <div class="gutslider-next-icon">
218 - <?php
219 - echo $customNextSVG;
220 - ?>
221 - </div>
222 - <?php
223 - }
224 - ?>
225 - </div>
226 - <?php
227 - }else {
228 - ?>
229 - <div class="swiper-button-prev"></div>
230 - <div class="swiper-button-next"></div>
231 - <?php
232 - }
233 - ?>
168 + <div class="gutslider-nav nav_inside <?php echo esc_attr( $atts['navPosition'] ); ?>">
169 + <?php if ( $atts['customNavIcon'] ) : ?>
170 + <div class="gutslider-prev">
171 + <!-- <div class="gutslider-prev-icon"> -->
172 + <?php
173 + if ( $atts['navIconSource'] === 'library' ) {
174 + echo wp_kses( gutslider_display_icon( $atts['navPrevIcon'] ), gutslider_allowed_svg_tags() ); // Ensure this function properly escapes output
175 + } else {
176 + echo wp_kses( $atts['customPrevSVG'], gutslider_allowed_svg_tags() );
177 + }
178 + ?>
179 + <!-- </div> -->
180 + </div>
181 + <div class="gutslider-next">
182 + <!-- <div class="gutslider-next-icon"> -->
183 + <?php
184 + if ( $atts['navIconSource'] === 'library' ) {
185 + echo wp_kses( gutslider_display_icon( $atts['navNextIcon'] ), gutslider_allowed_svg_tags() ); // Ensure this function properly escapes output
186 + } else {
187 + echo wp_kses( $atts['customNextSVG'], gutslider_allowed_svg_tags() );
188 + }
189 + ?>
190 + <!-- </div> -->
191 + </div>
192 + <?php else : ?>
193 + <div class="swiper-button-prev"></div>
194 + <div class="swiper-button-next"></div>
195 + <?php endif; ?>
234 196 </div>
235 197 <?php
236 198 }
237 199 ?>
238 200 <?php
239 - if( $showPagination ) {
201 + if( $atts['showPagination'] === '1' ) {
240 202 ?>
241 203 <div class="swiper-pagination"></div>
242 204 <?php
243 205 }
@@ -243,68 +205,39 @@
243 205 }
244 206 ?>
245 207 </div>
246 208 <?php
247 - if( $showNavigation === true && $navContainerPosition === 'nav_outside' ) {
248 - ?>
249 - <div class="gutslider-nav nav_outside <?php echo esc_attr( $navPosition ); ?>">
250 - <?php
251 - if( $customNavIcon ) {
209 + if ( $atts['showNavigation'] === '1' && $atts['navContainerPosition'] === 'nav_outside' ) : ?>
210 + <div class="gutslider-nav nav_outside <?php echo esc_attr( $atts['navPosition'] ); ?>">
211 + <?php if ( $atts['customNavIcon'] ) : ?>
212 + <div class="gutslider-prev">
213 + <!-- <div class="gutslider-prev-icon"> -->
214 + <?php
215 + if ( $atts['navIconSource'] === 'library' ) {
216 + echo wp_kses( gutslider_display_icon( esc_attr( $atts['navPrevIcon'] ) ), gutslider_allowed_svg_tags() ); // Ensure this function properly escapes output
217 + } else {
218 + echo wp_kses( $atts['customPrevSVG'], gutslider_allowed_svg_tags() );
219 + }
252 220 ?>
253 - <div class="gutslider-prev">
254 - <?php
255 - if( $navIconSource === 'library' ) {
256 - ?>
257 - <div class="gutslider-prev-icon">
258 - <?php
259 - echo gutslider_display_icon( $navPrevIcon );
260 - ?>
261 - </div>
262 - <?php
263 - } else {
264 - ?>
265 - <div class="gutslider-prev-icon">
266 - <?php
267 - echo $customPrevSVG;
268 - ?>
269 - </div>
270 - <?php
271 - }
272 - ?>
273 - </div>
274 - <div class="gutslider-next">
275 - <?php
276 - if( $navIconSource === 'library' ) {
277 - ?>
278 - <div class="gutslider-next-icon">
279 - <?php
280 - echo gutslider_display_icon( $navNextIcon );
281 - ?>
282 - </div>
283 - <?php
284 - } else {
285 - ?>
286 - <div class="gutslider-next-icon">
287 - <?php
288 - echo $customNextSVG;
289 - ?>
290 - </div>
291 - <?php
292 - }
293 - ?>
294 - </div>
295 - <?php
296 - }else {
221 + <!-- </div> -->
222 + </div>
223 + <div class="gutslider-next">
224 + <!-- <div class="gutslider-next-icon"> -->
225 + <?php
226 + if ( $atts['navIconSource'] === 'library' ) {
227 + echo wp_kses( gutslider_display_icon( esc_attr( $atts['navNextIcon'] ) ), gutslider_allowed_svg_tags() ); // Ensure this function properly escapes output
228 + } else {
229 + echo wp_kses( $atts['customNextSVG'], gutslider_allowed_svg_tags() );
230 + }
297 231 ?>
298 - <div class="swiper-button-prev"></div>
299 - <div class="swiper-button-next"></div>
300 - <?php
301 - }
302 - ?>
303 - </div>
304 - <?php
305 - }
306 - ?>
232 + <!-- </div> -->
233 + </div>
234 + <?php else : ?>
235 + <div class="swiper-button-prev"></div>
236 + <div class="swiper-button-next"></div>
237 + <?php endif; ?>
238 + </div>
239 + <?php endif; ?>
307 240 </div>
308 241 <?php
309 242 }
310 243
@@ -310,5 +243,6 @@
310 243
311 244 }
312 245
313 246 // call the render callback function
314 - gutslider_post_slider( $attributes );
247 + gutslider_post_slider( $attributes );
248 +