PluginProbe
Getwid – Gutenberg Blocks / 2.0.13
Getwid – Gutenberg Blocks v2.0.13
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-slider.php

post-slider.php in Getwid – Gutenberg Blocks 2.0.13, at includes/blocks/post-slider.php

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