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 +129 -403 2.0.53.0.2 View file →
@@ -1,403 +1,129 @@
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 -);
1 +<?php
2 +
3 +namespace Getwid\Blocks;
4 +
5 +class PostSlider extends AbstractBlock {
6 +
7 + private $assets_already_enqueued = false;
8 +
9 + public function __construct() {
10 +
11 + parent::__construct( 'getwid/post-slider' );
12 +
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 + }
20 +
21 + public function get_label() {
22 + return __( 'Post Slider', 'getwid' );
23 + }
24 +
25 + public function render_callback( $attributes, $content ) {
26 +
27 + $query_args = getwid_build_custom_post_type_query( $attributes );
28 + $q = new \WP_Query( $query_args );
29 +
30 + $use_template = false;
31 + $template_part_content = '';
32 +
33 + if ( isset( $attributes['postTemplate'] ) && '' !== $attributes['postTemplate'] ) {
34 + $template_post = get_post( $attributes['postTemplate'], ARRAY_A );
35 +
36 + if ( ! is_null( $template_post ) && '' !== $template_post['post_content'] ) {
37 + $use_template = true;
38 + $template_part_content = $template_post['post_content'];
39 + }
40 + }
41 +
42 + $block_name = 'wp-block-getwid-post-slider';
43 + $post_type = isset( $attributes['postType'] ) ? $attributes['postType'] : 'post';
44 +
45 + $extra_attr = array(
46 + 'block_name' => $block_name,
47 + 'back_end' => getwid_is_block_editor(),
48 + );
49 +
50 + $class = $block_name;
51 + $class .= ' custom-post-type-' . $post_type;
52 +
53 + if ( isset( $attributes['align'] ) ) {
54 + $class .= ' align' . $attributes['align'];
55 + }
56 + if ( isset( $attributes['className'] ) ) {
57 + $class .= ' ' . $attributes['className'];
58 + }
59 +
60 + $content_class = $block_name . '__content';
61 + $slide_style = '';
62 +
63 + if ( isset( $attributes['minHeight'] ) ) {
64 + $slide_style .= 'min-height:' . $attributes['minHeight'] . ';';
65 + }
66 +
67 + $class .= ' has-arrows-' . $attributes['sliderArrows'];
68 + $class .= ' has-dots-' . $attributes['sliderDots'];
69 +
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'],
79 + );
80 +
81 + $slider_options = wp_json_encode( $slider_data );
82 +
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 + }
95 +
96 + if ( $q->have_posts() ) {
97 + while ( $q->have_posts() ) :
98 + $q->the_post();
99 + ?>
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>
109 + <?php
110 + endwhile;
111 +
112 + wp_reset_postdata();
113 + } else {
114 + do_action( 'getwid/blocks/post-slider/no-items', $attributes, $content );
115 + }
116 + ?>
117 + </div>
118 + </div>
119 + <?php
120 +
121 + $result = ob_get_clean();
122 +
123 + return $result;
124 + }
125 +}
126 +
127 +getwid()->blocksManager()->addBlock(
128 + new PostSlider()
129 +);