| @@ -1,408 +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 | - '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 | -); | |
| 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 | +); | |