| 1 |
<?php |
| 2 |
|
| 3 |
namespace Getwid\Blocks; |
| 4 |
|
| 5 |
class PostCarousel extends AbstractBlock { |
| 6 |
|
| 7 |
public function __construct() { |
| 8 |
|
| 9 |
parent::__construct( 'getwid/post-carousel' ); |
| 10 |
|
| 11 |
register_block_type( |
| 12 |
getwid_get_plugin_path( 'assets/blocks/post-carousel' ), |
| 13 |
array( |
| 14 |
'render_callback' => array( $this, 'render_callback' ), |
| 15 |
) |
| 16 |
); |
| 17 |
} |
| 18 |
|
| 19 |
public function get_label() { |
| 20 |
return __( 'Post Carousel', 'getwid' ); |
| 21 |
} |
| 22 |
|
| 23 |
public function render_callback( $attributes, $content ) { |
| 24 |
|
| 25 |
$query_args = getwid_build_custom_post_type_query( $attributes ); |
| 26 |
$q = new \WP_Query( $query_args ); |
| 27 |
|
| 28 |
$use_template = false; |
| 29 |
$template_part_content = ''; |
| 30 |
|
| 31 |
if ( isset( $attributes['postTemplate'] ) && '' !== $attributes['postTemplate'] ) { |
| 32 |
$template_post = get_post( $attributes['postTemplate'], ARRAY_A ); |
| 33 |
|
| 34 |
if ( ! is_null( $template_post ) && '' !== $template_post['post_content'] ) { |
| 35 |
$use_template = true; |
| 36 |
$template_part_content = $template_post['post_content']; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
$block_name = 'wp-block-getwid-post-carousel'; |
| 41 |
$post_type = isset( $attributes['postType'] ) ? $attributes['postType'] : 'post'; |
| 42 |
|
| 43 |
$extra_attr = array( |
| 44 |
'block_name' => $block_name, |
| 45 |
); |
| 46 |
|
| 47 |
$class = $block_name; |
| 48 |
$class .= ' custom-post-type-' . $post_type; |
| 49 |
|
| 50 |
if ( isset( $attributes['align'] ) ) { |
| 51 |
$class .= ' align' . $attributes['align']; |
| 52 |
} |
| 53 |
if ( isset( $attributes['showPostDate'] ) && $attributes['showPostDate'] ) { |
| 54 |
$class .= ' has-dates'; |
| 55 |
} |
| 56 |
if ( isset( $attributes['className'] ) ) { |
| 57 |
$class .= ' ' . $attributes['className']; |
| 58 |
} |
| 59 |
|
| 60 |
$wrapper_class = $block_name . '__wrapper'; |
| 61 |
|
| 62 |
if ( isset( $attributes['sliderSlidesToShowDesktop'] ) && $attributes['sliderSlidesToShowDesktop'] > 1 ) { |
| 63 |
$class .= ' has-slides-gap-' . $attributes['sliderSpacing']; |
| 64 |
$class .= ' is-carousel'; |
| 65 |
} |
| 66 |
|
| 67 |
$class .= ' has-arrows-' . $attributes['sliderArrows']; |
| 68 |
$class .= ' has-dots-' . $attributes['sliderDots']; |
| 69 |
|
| 70 |
$slider_data = array( |
| 71 |
'sliderSlidesToShowDesktop' => $attributes['sliderSlidesToShowDesktop'], |
| 72 |
'getwid_slidesToShowLaptop' => $attributes['sliderSlidesToShowLaptop'], |
| 73 |
'getwid_slidesToShowTablet' => $attributes['sliderSlidesToShowTablet'], |
| 74 |
'getwid_slidesToShowMobile' => $attributes['sliderSlidesToShowMobile'], |
| 75 |
'getwid_autoplay_speed' => intval( $attributes['sliderAutoplaySpeed'] ), |
| 76 |
'getwid_animation_speed' => intval( $attributes['sliderAnimationSpeed'] ), |
| 77 |
'getwid_slidesToScroll' => $attributes['sliderSlidesToScroll'], |
| 78 |
'getwid_autoplay' => $attributes['sliderAutoplay'], |
| 79 |
'getwid_pause_on_hover' => $attributes['sliderPauseOnHover'], |
| 80 |
'getwid_infinite' => $attributes['sliderInfinite'], |
| 81 |
'getwid_center_mode' => $attributes['sliderCenterMode'], |
| 82 |
'getwid_arrows' => $attributes['sliderArrows'], |
| 83 |
'getwid_dots' => $attributes['sliderDots'], |
| 84 |
); |
| 85 |
|
| 86 |
$slider_options = wp_json_encode( $slider_data ); |
| 87 |
|
| 88 |
ob_start(); |
| 89 |
?> |
| 90 |
<div class="<?php echo esc_attr( $class ); ?>"> |
| 91 |
<div data-slider-option="<?php echo esc_attr( $slider_options ); ?>" class="<?php echo esc_attr( $wrapper_class ); ?>"> |
| 92 |
<?php |
| 93 |
if ( ! $use_template ) { |
| 94 |
$template = $post_type; |
| 95 |
$located = getwid_locate_template( 'post-carousel/' . $post_type ); |
| 96 |
if ( ! $located ) { |
| 97 |
$template = 'post'; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
if ( $q->have_posts() ) { |
| 102 |
while ( $q->have_posts() ) : |
| 103 |
$q->the_post(); |
| 104 |
?> |
| 105 |
<div class="<?php echo esc_attr( $block_name ); ?>__slide"> |
| 106 |
<?php |
| 107 |
if ( $use_template ) { |
| 108 |
echo do_blocks( $template_part_content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 109 |
} else { |
| 110 |
getwid_get_template_part( 'post-carousel/' . $template, $attributes, false, $extra_attr ); |
| 111 |
} |
| 112 |
?> |
| 113 |
</div> |
| 114 |
<?php |
| 115 |
endwhile; |
| 116 |
|
| 117 |
wp_reset_postdata(); |
| 118 |
} else { |
| 119 |
do_action( 'getwid/blocks/post-carousel/no-items', $attributes, $content ); |
| 120 |
} |
| 121 |
?> |
| 122 |
</div> |
| 123 |
</div> |
| 124 |
<?php |
| 125 |
|
| 126 |
$result = ob_get_clean(); |
| 127 |
|
| 128 |
return $result; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
getwid()->blocksManager()->addBlock( |
| 133 |
new PostCarousel() |
| 134 |
); |
| 135 |
|