PluginProbe
Getwid – Gutenberg Blocks / 2.0.6
Getwid – Gutenberg Blocks v2.0.6
3.0.2 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.0.6, at includes/blocks/post-carousel.php

424 lines 13.2 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 'sliderAutoplaySpeed' => array(
112 'type' => 'string',
113 'default' => '6000'
114 ),
115 'sliderInfinite' => array(
116 'type' => 'boolean',
117 'default' => true
118 ),
119 'sliderAnimationSpeed' => array(
120 'type' => 'string',
121 'default' => '800'
122 ),
123 'sliderCenterMode' => array(
124 'type' => 'boolean',
125 'default' => false
126 ),
127 'sliderSpacing' => array(
128 'type' => 'string',
129 'default' => 'small'
130 ),
131 'sliderArrows' => array(
132 'type' => 'string',
133 'default' => 'inside'
134 ),
135 'sliderDots' => array(
136 'type' => 'string',
137 'default' => 'outside'
138 ),
139 'className' => array(
140 'type' => 'string'
141 ),
142
143 //Modal
144 'metaQuery' => array(
145 'type' => 'array',
146 'default' => []
147 ),
148 ),
149 'render_callback' => [ $this, 'render_callback' ]
150 )
151 );
152 /* #endregion */
153
154 if ( $this->isEnabled() ) {
155
156 add_filter( 'getwid/editor_blocks_js/dependencies', [ $this, 'block_editor_scripts' ] );
157 add_filter( 'getwid/blocks_style_css/dependencies', [ $this, 'block_frontend_styles' ] );
158
159 //Register JS/CSS assets
160 wp_register_script(
161 'slick',
162 getwid_get_plugin_url( 'vendors/slick/slick/slick.min.js' ),
163 [ 'jquery' ],
164 '1.9.0',
165 true
166 );
167
168 wp_register_style(
169 'slick',
170 getwid_get_plugin_url( 'vendors/slick/slick/slick.min.css' ),
171 [],
172 '1.9.0'
173 );
174
175 wp_register_style(
176 'slick-theme',
177 getwid_get_plugin_url( 'vendors/slick/slick/slick-theme.min.css' ),
178 [],
179 '1.9.0'
180 );
181 }
182 }
183
184 public function getLabel() {
185 return __('Post Carousel', 'getwid');
186 }
187
188 public function block_editor_scripts($scripts) {
189
190 //imagesloaded.min.js
191 if ( ! in_array( 'imagesloaded', $scripts ) ) {
192 array_push( $scripts, 'imagesloaded' );
193 }
194
195 //slick.min.js
196 if ( ! in_array( 'slick', $scripts ) ) {
197 array_push( $scripts, 'slick' );
198 }
199
200 return $scripts;
201 }
202
203 public function block_frontend_styles($styles) {
204
205 //fontawesome
206 // for /template-parts/*
207 $styles = getwid()->fontIconsManager()->enqueueFonts( $styles );
208
209 //slick.min.css
210 if ( ! in_array( 'slick', $styles ) ) {
211 array_push( $styles, 'slick' );
212 }
213
214 //slick-theme.min.css
215 if ( ! in_array( 'slick-theme', $styles ) ) {
216 array_push( $styles, 'slick-theme' );
217 }
218
219 return $styles;
220 }
221
222 public function block_frontend_assets() {
223
224 if ( is_admin() ) {
225 return;
226 }
227
228 //slick.min.js
229 if ( ! wp_script_is( 'slick', 'enqueued' ) ) {
230 wp_enqueue_script('slick');
231 }
232
233 //imagesloaded.min.js
234 if ( ! wp_script_is( 'imagesloaded', 'enqueued' ) ) {
235 wp_enqueue_script('imagesloaded');
236 }
237
238 if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) {
239 return;
240 }
241
242 $deps = [
243 'slick', 'slick-theme'
244 ];
245
246 //fontawesome
247 // for /template-parts/*
248 $deps = getwid()->fontIconsManager()->enqueueFonts( $deps );
249
250 add_filter( 'getwid/optimize/assets',
251 function ( $assets ) {
252 $assets[] = 'slick';
253 $assets[] = 'slick-theme';
254 $assets[] = getwid()->settings()->getPrefix() . '-blocks-common';
255
256 return $assets;
257 }
258 );
259
260 add_filter( 'getwid/optimize/should_load_common_css', '__return_true' );
261
262 $rtl = is_rtl() ? '.rtl' : '';
263
264 wp_enqueue_style(
265 self::$blockName,
266 getwid_get_plugin_url( 'assets/blocks/post-carousel/style' . $rtl . '.css' ),
267 $deps,
268 getwid()->settings()->getVersion()
269 );
270
271 wp_enqueue_script(
272 self::$blockName,
273 getwid_get_plugin_url( 'assets/blocks/post-carousel/frontend.js' ),
274 [ 'jquery', 'imagesloaded', 'slick' ],
275 getwid()->settings()->getVersion(),
276 true
277 );
278
279 if ( !$this->assetsAlreadyEnqueued ) {
280 $inline_script =
281 'var Getwid = Getwid || {};' .
282 'Getwid["isRTL"] = ' . json_encode( is_rtl() ) . ';';
283
284 wp_add_inline_script(
285 self::$blockName,
286 $inline_script,
287 'before'
288 );
289 }
290
291 $this->assetsAlreadyEnqueued = true;
292 }
293
294 public function render_callback( $attributes, $content ) {
295
296 //Custom Post Type
297 $query_args = getwid_build_custom_post_type_query( $attributes );
298
299 $q = new \WP_Query( $query_args );
300 //Custom Post Type
301
302 //Custom Template
303 $use_template = false;
304 $template_part_content = '';
305
306 if ( isset( $attributes[ 'postTemplate' ] ) && $attributes[ 'postTemplate' ] != '' ) {
307
308 $template_post = get_post( $attributes[ 'postTemplate' ], ARRAY_A );
309
310 //If post exist and content not empty
311 if ( ! is_null( $template_post ) && $template_post[ 'post_content' ] != '' ) {
312 $use_template = true;
313 $template_part_content = $template_post[ 'post_content' ];
314 }
315 }
316
317 $block_name = 'wp-block-getwid-post-carousel';
318
319 $post_type = isset( $attributes[ 'postType' ] ) ? $attributes[ 'postType' ] : 'post';
320
321 $extra_attr = array(
322 'block_name' => $block_name
323 );
324
325 $class = $block_name;
326 $class .= ' custom-post-type-' . $post_type;
327
328 if ( isset( $attributes[ 'align' ] ) ) {
329 $class .= ' align' . $attributes[ 'align' ];
330 }
331
332 if ( isset( $attributes[ 'showPostDate' ] ) && $attributes[ 'showPostDate' ] ) {
333 $class .= ' has-dates';
334 }
335 if ( isset( $attributes[ 'className' ] ) ) {
336 $class .= ' ' . $attributes[ 'className' ];
337 }
338
339 $wrapper_class = $block_name . '__wrapper';
340
341 if ( isset( $attributes[ 'sliderSlidesToShowDesktop' ] ) && $attributes[ 'sliderSlidesToShowDesktop' ] > 1 ) {
342 $class .= ' has-slides-gap-' . $attributes[ 'sliderSpacing' ];
343 $class .= ' is-carousel';
344 }
345
346 $class .= ' has-arrows-' . $attributes[ 'sliderArrows' ];
347 $class .= ' has-dots-' . $attributes[ 'sliderDots' ];
348
349 $sliderData = array(
350 'sliderSlidesToShowDesktop' => $attributes[ 'sliderSlidesToShowDesktop' ],
351 'getwid_slidesToShowLaptop' => $attributes[ 'sliderSlidesToShowLaptop' ],
352 'getwid_slidesToShowTablet' => $attributes[ 'sliderSlidesToShowTablet' ],
353 'getwid_slidesToShowMobile' => $attributes[ 'sliderSlidesToShowMobile' ],
354
355 'getwid_autoplay_speed' => intval( $attributes[ 'sliderAutoplaySpeed' ] ),
356 'getwid_animation_speed' => intval( $attributes[ 'sliderAnimationSpeed' ] ),
357
358 'getwid_slidesToScroll' => $attributes[ 'sliderSlidesToScroll'],
359 'getwid_autoplay' => $attributes[ 'sliderAutoplay' ],
360 'getwid_infinite' => $attributes[ 'sliderInfinite' ],
361
362 'getwid_center_mode' => $attributes[ 'sliderCenterMode' ],
363 'getwid_arrows' => $attributes[ 'sliderArrows' ],
364 'getwid_dots' => $attributes[ 'sliderDots' ]
365 );
366
367 $slider_options = json_encode( $sliderData );
368 ob_start();
369 ?>
370
371 <div class="<?php echo esc_attr( $class ); ?>">
372 <div data-slider-option="<?php echo esc_attr( $slider_options ); ?>" class="<?php echo esc_attr( $wrapper_class );?>">
373 <?php
374
375 if ( ! $use_template ) {
376 $template = $post_type;
377 $located = getwid_locate_template( 'post-carousel/' . $post_type );
378 if ( ! $located ) {
379 $template = 'post';
380 }
381 }
382
383 if ( $q->have_posts() ):
384 ob_start();
385
386 while( $q->have_posts() ):
387 $q->the_post();
388
389 ?>
390 <div class="<?php echo esc_attr( $block_name );?>__slide">
391 <?php
392 if ($use_template){
393 echo do_blocks( $template_part_content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
394 } else {
395 getwid_get_template_part( 'post-carousel/' . $template, $attributes, false, $extra_attr );
396 }
397 ?>
398 </div>
399 <?php
400
401 endwhile;
402
403 wp_reset_postdata();
404 ob_end_flush();
405 else:
406 do_action( 'getwid/blocks/post-carousel/no-items', $attributes, $content );
407 endif;
408 ?>
409 </div>
410 </div>
411 <?php
412
413 $result = ob_get_clean();
414
415 $this->block_frontend_assets();
416
417 return $result;
418 }
419 }
420
421 getwid()->blocksManager()->addBlock(
422 new \Getwid\Blocks\PostCarousel()
423 );
424