PluginProbe
Getwid – Gutenberg Blocks / 2.1.0
Getwid – Gutenberg Blocks v2.1.0
3.0.1 3.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.2.0 trunk 1.8.7 1.9.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
getwid / includes / blocks / post-carousel.php

post-carousel.php in Getwid – Gutenberg Blocks 2.1.0, at includes/blocks/post-carousel.php

429 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Getwid\Blocks;
4
5 class PostCarousel extends \Getwid\Blocks\AbstractBlock {
6
7 protected static $blockName = 'getwid/post-carousel';
8 private $assetsAlreadyEnqueued = false;
9
10 public function __construct() {
11
12 parent::__construct( self::$blockName );
13
14 /* #region Register block */
15 register_block_type(
16 'getwid/post-carousel',
17 array(
18 'attributes' => array(
19 'postTemplate' => array(
20 'type' => 'string'
21 ),
22
23 //Custom Post Type
24 'postsToShow' => array(
25 'type' => 'number',
26 'default' => 5
27 ),
28 'offset' => array(
29 'type' => 'number',
30 'default' => 0
31 ),
32 'ignoreSticky' => array(
33 'type' => 'boolean',
34 'default' => true
35 ),
36 'filterById' => array(
37 'type' => 'string'
38 ),
39 'excludeById' => array(
40 'type' => 'string'
41 ),
42 'excludeCurrentPost' => array(
43 'type' => 'boolean',
44 'default' => false
45 ),
46 'childPagesCurrentPage' => array(
47 'type' => 'boolean',
48 'default' => false
49 ),
50 'parentPageId' => array(
51 'type' => 'string'
52 ),
53 'postType' => array(
54 'type' => 'string',
55 'default' => 'post'
56 ),
57 'taxonomy' => array(
58 'type' => 'array',
59 'items' => [
60 'type' => 'string'
61 ],
62 ),
63 'terms' => array(
64 'type' => 'array',
65 'items' => [
66 'type' => 'string'
67 ],
68 ),
69 'relation' => array(
70 'type' => 'string',
71 'default' => 'AND'
72 ),
73 'order' => array(
74 'type' => 'string',
75 'default' => 'desc'
76 ),
77 'orderBy' => array(
78 'type' => 'string',
79 'default' => 'date'
80 ),
81 //Custom Post Type
82 'align' => array(
83 'type' => 'string'
84 ),
85
86 //Slider
87 'sliderSlidesToShowDesktop' => array(
88 'type' => 'string',
89 'default' => '2'
90 ),
91 'sliderSlidesToShowLaptop' => array(
92 'type' => 'string',
93 'default' => '1'
94 ),
95 'sliderSlidesToShowTablet' => array(
96 'type' => 'string',
97 'default' => '1'
98 ),
99 'sliderSlidesToShowMobile' => array(
100 'type' => 'string',
101 'default' => '1'
102 ),
103 'sliderSlidesToScroll' => array(
104 'type' => 'string',
105 'default' => '1'
106 ),
107 'sliderAutoplay' => array(
108 'type' => 'boolean',
109 'default' => false
110 ),
111 'sliderPauseOnHover' => array(
112 'type' => 'boolean',
113 'default' => false
114 ),
115 'sliderAutoplaySpeed' => array(
116 'type' => 'string',
117 'default' => '6000'
118 ),
119 'sliderInfinite' => array(
120 'type' => 'boolean',
121 'default' => true
122 ),
123 'sliderAnimationSpeed' => array(
124 'type' => 'string',
125 'default' => '800'
126 ),
127 'sliderCenterMode' => array(
128 'type' => 'boolean',
129 'default' => false
130 ),
131 'sliderSpacing' => array(
132 'type' => 'string',
133 'default' => 'small'
134 ),
135 'sliderArrows' => array(
136 'type' => 'string',
137 'default' => 'inside'
138 ),
139 'sliderDots' => array(
140 'type' => 'string',
141 'default' => 'outside'
142 ),
143 'className' => array(
144 'type' => 'string'
145 ),
146
147 //Modal
148 'metaQuery' => array(
149 'type' => 'array',
150 'default' => []
151 ),
152 ),
153 'render_callback' => [ $this, 'render_callback' ]
154 )
155 );
156 /* #endregion */
157
158 if ( $this->isEnabled() ) {
159
160 add_filter( 'getwid/editor_blocks_js/dependencies', [ $this, 'block_editor_scripts' ] );
161 add_filter( 'getwid/blocks_style_css/dependencies', [ $this, 'block_frontend_styles' ] );
162
163 //Register JS/CSS assets
164 wp_register_script(
165 'slick',
166 getwid_get_plugin_url( 'vendors/slick/slick/slick.min.js' ),
167 [ 'jquery' ],
168 '1.9.0',
169 true
170 );
171
172 wp_register_style(
173 'slick',
174 getwid_get_plugin_url( 'vendors/slick/slick/slick.min.css' ),
175 [],
176 '1.9.0'
177 );
178
179 wp_register_style(
180 'slick-theme',
181 getwid_get_plugin_url( 'vendors/slick/slick/slick-theme.min.css' ),
182 [],
183 '1.9.0'
184 );
185 }
186 }
187
188 public function getLabel() {
189 return __('Post Carousel', 'getwid');
190 }
191
192 public function block_editor_scripts($scripts) {
193
194 //imagesloaded.min.js
195 if ( ! in_array( 'imagesloaded', $scripts ) ) {
196 array_push( $scripts, 'imagesloaded' );
197 }
198
199 //slick.min.js
200 if ( ! in_array( 'slick', $scripts ) ) {
201 array_push( $scripts, 'slick' );
202 }
203
204 return $scripts;
205 }
206
207 public function block_frontend_styles($styles) {
208
209 //fontawesome
210 // for /template-parts/*
211 $styles = getwid()->fontIconsManager()->enqueueFonts( $styles );
212
213 //slick.min.css
214 if ( ! in_array( 'slick', $styles ) ) {
215 array_push( $styles, 'slick' );
216 }
217
218 //slick-theme.min.css
219 if ( ! in_array( 'slick-theme', $styles ) ) {
220 array_push( $styles, 'slick-theme' );
221 }
222
223 return $styles;
224 }
225
226 public function block_frontend_assets() {
227
228 if ( is_admin() ) {
229 return;
230 }
231
232 //slick.min.js
233 if ( ! wp_script_is( 'slick', 'enqueued' ) ) {
234 wp_enqueue_script('slick');
235 }
236
237 //imagesloaded.min.js
238 if ( ! wp_script_is( 'imagesloaded', 'enqueued' ) ) {
239 wp_enqueue_script('imagesloaded');
240 }
241
242 if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) {
243 return;
244 }
245
246 $deps = [
247 'slick', 'slick-theme'
248 ];
249
250 //fontawesome
251 // for /template-parts/*
252 $deps = getwid()->fontIconsManager()->enqueueFonts( $deps );
253
254 add_filter( 'getwid/optimize/assets',
255 function ( $assets ) {
256 $assets[] = 'slick';
257 $assets[] = 'slick-theme';
258 $assets[] = getwid()->settings()->getPrefix() . '-blocks-common';
259
260 return $assets;
261 }
262 );
263
264 add_filter( 'getwid/optimize/should_load_common_css', '__return_true' );
265
266 $rtl = is_rtl() ? '.rtl' : '';
267
268 wp_enqueue_style(
269 self::$blockName,
270 getwid_get_plugin_url( 'assets/blocks/post-carousel/style' . $rtl . '.css' ),
271 $deps,
272 getwid()->settings()->getVersion()
273 );
274
275 wp_enqueue_script(
276 self::$blockName,
277 getwid_get_plugin_url( 'assets/blocks/post-carousel/frontend.js' ),
278 [ 'jquery', 'imagesloaded', 'slick' ],
279 getwid()->settings()->getVersion(),
280 true
281 );
282
283 if ( !$this->assetsAlreadyEnqueued ) {
284 $inline_script =
285 'var Getwid = Getwid || {};' .
286 'Getwid["isRTL"] = ' . json_encode( is_rtl() ) . ';';
287
288 wp_add_inline_script(
289 self::$blockName,
290 $inline_script,
291 'before'
292 );
293 }
294
295 $this->assetsAlreadyEnqueued = true;
296 }
297
298 public function render_callback( $attributes, $content ) {
299
300 //Custom Post Type
301 $query_args = getwid_build_custom_post_type_query( $attributes );
302
303 $q = new \WP_Query( $query_args );
304 //Custom Post Type
305
306 //Custom Template
307 $use_template = false;
308 $template_part_content = '';
309
310 if ( isset( $attributes[ 'postTemplate' ] ) && $attributes[ 'postTemplate' ] != '' ) {
311
312 $template_post = get_post( $attributes[ 'postTemplate' ], ARRAY_A );
313
314 //If post exist and content not empty
315 if ( ! is_null( $template_post ) && $template_post[ 'post_content' ] != '' ) {
316 $use_template = true;
317 $template_part_content = $template_post[ 'post_content' ];
318 }
319 }
320
321 $block_name = 'wp-block-getwid-post-carousel';
322
323 $post_type = isset( $attributes[ 'postType' ] ) ? $attributes[ 'postType' ] : 'post';
324
325 $extra_attr = array(
326 'block_name' => $block_name
327 );
328
329 $class = $block_name;
330 $class .= ' custom-post-type-' . $post_type;
331
332 if ( isset( $attributes[ 'align' ] ) ) {
333 $class .= ' align' . $attributes[ 'align' ];
334 }
335
336 if ( isset( $attributes[ 'showPostDate' ] ) && $attributes[ 'showPostDate' ] ) {
337 $class .= ' has-dates';
338 }
339 if ( isset( $attributes[ 'className' ] ) ) {
340 $class .= ' ' . $attributes[ 'className' ];
341 }
342
343 $wrapper_class = $block_name . '__wrapper';
344
345 if ( isset( $attributes[ 'sliderSlidesToShowDesktop' ] ) && $attributes[ 'sliderSlidesToShowDesktop' ] > 1 ) {
346 $class .= ' has-slides-gap-' . $attributes[ 'sliderSpacing' ];
347 $class .= ' is-carousel';
348 }
349
350 $class .= ' has-arrows-' . $attributes[ 'sliderArrows' ];
351 $class .= ' has-dots-' . $attributes[ 'sliderDots' ];
352
353 $sliderData = array(
354 'sliderSlidesToShowDesktop' => $attributes[ 'sliderSlidesToShowDesktop' ],
355 'getwid_slidesToShowLaptop' => $attributes[ 'sliderSlidesToShowLaptop' ],
356 'getwid_slidesToShowTablet' => $attributes[ 'sliderSlidesToShowTablet' ],
357 'getwid_slidesToShowMobile' => $attributes[ 'sliderSlidesToShowMobile' ],
358
359 'getwid_autoplay_speed' => intval( $attributes[ 'sliderAutoplaySpeed' ] ),
360 'getwid_animation_speed' => intval( $attributes[ 'sliderAnimationSpeed' ] ),
361
362 'getwid_slidesToScroll' => $attributes[ 'sliderSlidesToScroll' ],
363 'getwid_autoplay' => $attributes[ 'sliderAutoplay' ],
364 'getwid_pause_on_hover' => $attributes[ 'sliderPauseOnHover' ],
365 'getwid_infinite' => $attributes[ 'sliderInfinite' ],
366
367 'getwid_center_mode' => $attributes[ 'sliderCenterMode' ],
368 'getwid_arrows' => $attributes[ 'sliderArrows' ],
369 'getwid_dots' => $attributes[ 'sliderDots' ]
370 );
371
372 $slider_options = json_encode( $sliderData );
373 ob_start();
374 ?>
375
376 <div class="<?php echo esc_attr( $class ); ?>">
377 <div data-slider-option="<?php echo esc_attr( $slider_options ); ?>" class="<?php echo esc_attr( $wrapper_class );?>">
378 <?php
379
380 if ( ! $use_template ) {
381 $template = $post_type;
382 $located = getwid_locate_template( 'post-carousel/' . $post_type );
383 if ( ! $located ) {
384 $template = 'post';
385 }
386 }
387
388 if ( $q->have_posts() ):
389 ob_start();
390
391 while( $q->have_posts() ):
392 $q->the_post();
393
394 ?>
395 <div class="<?php echo esc_attr( $block_name );?>__slide">
396 <?php
397 if ($use_template){
398 echo do_blocks( $template_part_content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
399 } else {
400 getwid_get_template_part( 'post-carousel/' . $template, $attributes, false, $extra_attr );
401 }
402 ?>
403 </div>
404 <?php
405
406 endwhile;
407
408 wp_reset_postdata();
409 ob_end_flush();
410 else:
411 do_action( 'getwid/blocks/post-carousel/no-items', $attributes, $content );
412 endif;
413 ?>
414 </div>
415 </div>
416 <?php
417
418 $result = ob_get_clean();
419
420 $this->block_frontend_assets();
421
422 return $result;
423 }
424 }
425
426 getwid()->blocksManager()->addBlock(
427 new \Getwid\Blocks\PostCarousel()
428 );
429