PluginProbe
Getwid – Gutenberg Blocks / 3.0.2
Getwid – Gutenberg Blocks v3.0.2
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
← All changes | includes/blocks/post-slider.php +90 -369 2.0.133.0.2 View file →
@@ -1,408 +1,129 @@
1 1 <?php
2 2
3 3 namespace Getwid\Blocks;
4 4
5 -class PostSlider extends \Getwid\Blocks\AbstractBlock {
5 +class PostSlider extends AbstractBlock {
6 6
7 - protected static $blockName = 'getwid/post-slider';
8 - private $assetsAlreadyEnqueued = false;
7 + private $assets_already_enqueued = false;
9 8
10 - public function __construct() {
9 + public function __construct() {
11 10
12 - parent::__construct( self::$blockName );
11 + parent::__construct( 'getwid/post-slider' );
13 12
14 - /* #region Register block */
15 - register_block_type(
16 - 'getwid/post-slider',
17 - array(
18 - 'attributes' => array(
19 - 'postTemplate' => array(
20 - 'type' => 'string',
21 - ),
13 + register_block_type(
14 + getwid_get_plugin_path( 'assets/blocks/post-slider' ),
15 + array(
16 + 'render_callback' => array( $this, 'render_callback' ),
17 + )
18 + );
19 + }
22 20
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
21 + public function get_label() {
22 + return __( 'Post Slider', 'getwid' );
23 + }
82 24
83 - //Content
84 - 'minHeight' => array(
85 - 'type' => 'string',
86 - ),
25 + public function render_callback( $attributes, $content ) {
87 26
88 - //Posts
89 - 'align' => array(
90 - 'type' => 'string',
91 - ),
92 - 'textAlignment' => array(
93 - 'type' => 'string',
94 - 'default' => 'left',
95 - ),
27 + $query_args = getwid_build_custom_post_type_query( $attributes );
28 + $q = new \WP_Query( $query_args );
96 29
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 - ),
30 + $use_template = false;
31 + $template_part_content = '';
130 32
131 - 'className' => array(
132 - 'type' => 'string',
133 - ),
33 + if ( isset( $attributes['postTemplate'] ) && '' !== $attributes['postTemplate'] ) {
34 + $template_post = get_post( $attributes['postTemplate'], ARRAY_A );
134 35
135 - //Modal
136 - 'metaQuery' => array(
137 - 'type' => 'array',
138 - 'default' => []
139 - ),
140 - ),
141 - 'render_callback' => [ $this, 'render_callback' ]
142 - )
143 - );
144 - /* #endregion */
36 + if ( ! is_null( $template_post ) && '' !== $template_post['post_content'] ) {
37 + $use_template = true;
38 + $template_part_content = $template_post['post_content'];
39 + }
40 + }
145 41
146 - if ( $this->isEnabled() ) {
42 + $block_name = 'wp-block-getwid-post-slider';
43 + $post_type = isset( $attributes['postType'] ) ? $attributes['postType'] : 'post';
147 44
148 - add_filter( 'getwid/editor_blocks_js/dependencies', [ $this, 'block_editor_scripts'] );
149 - add_filter( 'getwid/blocks_style_css/dependencies', [ $this, 'block_frontend_styles' ] );
45 + $extra_attr = array(
46 + 'block_name' => $block_name,
47 + 'back_end' => getwid_is_block_editor(),
48 + );
150 49
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 - );
50 + $class = $block_name;
51 + $class .= ' custom-post-type-' . $post_type;
159 52
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 - );
53 + if ( isset( $attributes['align'] ) ) {
54 + $class .= ' align' . $attributes['align'];
173 55 }
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' );
56 + if ( isset( $attributes['className'] ) ) {
57 + $class .= ' ' . $attributes['className'];
185 58 }
186 59
187 - //slick.min.js
188 - if ( ! in_array( 'slick', $scripts ) ) {
189 - array_push( $scripts, 'slick' );
190 - }
60 + $content_class = $block_name . '__content';
61 + $slide_style = '';
191 62
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');
63 + if ( isset( $attributes['minHeight'] ) ) {
64 + $slide_style .= 'min-height:' . $attributes['minHeight'] . ';';
228 65 }
229 66
230 - if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) {
231 - return;
232 - }
67 + $class .= ' has-arrows-' . $attributes['sliderArrows'];
68 + $class .= ' has-dots-' . $attributes['sliderDots'];
233 69
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 - }
70 + $slider_data = array(
71 + 'getwid_fade_effect' => $attributes['sliderAnimationEffect'],
72 + 'getwid_autoplay' => $attributes['sliderAutoplay'],
73 + 'getwid_pause_on_hover' => $attributes['sliderPauseOnHover'],
74 + 'getwid_autoplay_speed' => intval( $attributes['sliderAutoplaySpeed'] ),
75 + 'getwid_infinite' => $attributes['sliderInfinite'],
76 + 'getwid_animation_speed' => intval( $attributes['sliderAnimationSpeed'] ),
77 + 'getwid_arrows' => $attributes['sliderArrows'],
78 + 'getwid_dots' => $attributes['sliderDots'],
250 79 );
251 80
252 - add_filter( 'getwid/optimize/should_load_common_css', '__return_true' );
81 + $slider_options = wp_json_encode( $slider_data );
253 82
254 - $rtl = is_rtl() ? '.rtl' : '';
83 + ob_start();
84 + ?>
85 + <div class="<?php echo esc_attr( $class ); ?>">
86 + <div data-slider-option="<?php echo esc_attr( $slider_options ); ?>" class="<?php echo esc_attr( $content_class ); ?>">
87 + <?php
88 + if ( ! $use_template ) {
89 + $template = $post_type;
90 + $located = getwid_locate_template( 'post-slider/' . $post_type );
91 + if ( ! $located ) {
92 + $template = 'post';
93 + }
94 + }
255 95
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 -
96 + if ( $q->have_posts() ) {
97 + while ( $q->have_posts() ) :
98 + $q->the_post();
374 99 ?>
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>
100 + <div class="<?php echo esc_attr( $block_name ); ?>__slide" style="<?php echo esc_attr( $slide_style ); ?>">
101 + <?php
102 + if ( $use_template ) {
103 + echo do_blocks( $template_part_content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
104 + } else {
105 + getwid_get_template_part( 'post-slider/' . $template, $attributes, false, $extra_attr );
106 + }
107 + ?>
108 + </div>
384 109 <?php
110 + endwhile;
385 111
386 - endwhile;
387 -
388 - wp_reset_postdata();
389 - ob_end_flush();
390 - else:
112 + wp_reset_postdata();
113 + } else {
391 114 do_action( 'getwid/blocks/post-slider/no-items', $attributes, $content );
392 - endif;
393 - ?>
394 - </div>
395 - </div>
396 - <?php
115 + }
116 + ?>
117 + </div>
118 + </div>
119 + <?php
397 120
398 - $result = ob_get_clean();
121 + $result = ob_get_clean();
399 122
400 - $this->block_frontend_assets();
401 -
402 - return $result;
403 - }
123 + return $result;
124 + }
404 125 }
405 126
406 127 getwid()->blocksManager()->addBlock(
407 - new \Getwid\Blocks\PostSlider()
128 + new PostSlider()
408 129 );