| 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 |
); |
| 130 |
|