PluginProbe
Getwid – Gutenberg Blocks / 1.8.7
Getwid – Gutenberg Blocks v1.8.7
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-slider.php

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

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