| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `posts` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Renders the block on server. |
| 10 |
* |
| 11 |
* @param array $attributes The block attributes. |
| 12 |
* @param string $content Block Content. |
| 13 |
* |
| 14 |
* @return string Returns the block content. |
| 15 |
*/ |
| 16 |
function gutenify_render_slider_block( $attributes, $content, $props ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed |
| 17 |
return gutenify_slider( $attributes, $content, $props ); |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
/** |
| 22 |
* Renders the list and grid styles. |
| 23 |
* |
| 24 |
* @param array $attributes The block attributes. |
| 25 |
* |
| 26 |
* @return string Returns the block content for the list and grid styles. |
| 27 |
*/ |
| 28 |
function gutenify_slider( $attributes, $content, $props ) { |
| 29 |
$client_id = ! empty( $attributes['blockClientId'] ) ? $attributes['blockClientId'] : ''; |
| 30 |
$class_name = array(); |
| 31 |
$class = array( 'swiper-container', 'gutenify-slider' ); |
| 32 |
$styles = array(); |
| 33 |
$block_style = ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'is-style-stacked' ) !== false ? 'stacked' : 'horizontal'; |
| 34 |
|
| 35 |
$attributes['layout'] = ! empty( $attributes['layout'] ) ? $attributes['layout'] : 'layout-1'; |
| 36 |
array_push( $class_name, 'wp-block-gutenify-slider', 'gutenify-section-' . $attributes['blockClientId'], 'gutenify-post-carousel-' . $attributes['layout'] ); |
| 37 |
|
| 38 |
if ( isset( $attributes['className'] ) ) { |
| 39 |
array_push( $class_name, $attributes['className'] ); |
| 40 |
} |
| 41 |
|
| 42 |
if ( isset( $attributes['align'] ) ) { |
| 43 |
array_push( $class_name, 'align' . $attributes['align'] ); |
| 44 |
} |
| 45 |
|
| 46 |
if ( isset( $attributes['columns'] ) ) { |
| 47 |
array_push( $class, 'has-columns has-' . $attributes['columns'] . '-columns has-responsive-columns' ); |
| 48 |
} |
| 49 |
|
| 50 |
if ( isset( $attributes['listPosition'] ) && 'right' === $attributes['listPosition'] && 'horizontal' === $block_style ) { |
| 51 |
array_push( $class, 'has-image-right' ); |
| 52 |
} |
| 53 |
|
| 54 |
if ( isset( $attributes['imageSize'] ) && 'horizontal' === $block_style ) { |
| 55 |
array_push( $class, 'has-' . $attributes['imageSize'] . '-image' ); |
| 56 |
} |
| 57 |
|
| 58 |
if ( isset( $attributes['imageStyle'] ) ) { |
| 59 |
array_push( $class, 'has-' . $attributes['imageStyle'] . '-image' ); |
| 60 |
} |
| 61 |
|
| 62 |
$class_name = apply_filters( 'gutenify--slider--wrapper-class', $class_name, $props ); |
| 63 |
$block_content = sprintf( |
| 64 |
'<div class="%1$s"><div id="gutenify-section-' . $client_id . '" class="gutenify-slider-section"><div class="%2$s" style="%3$s"><div class="swiper-wrapper">', |
| 65 |
esc_attr( implode( ' ', $class_name ) ), |
| 66 |
esc_attr( implode( ' ', apply_filters( 'gutenify_render_wrapper_class', $class, $attributes ) ) ), |
| 67 |
esc_attr( implode( ' ', apply_filters( 'gutenify_render_wrapper_styles', $styles, $attributes ) ) ) |
| 68 |
); |
| 69 |
|
| 70 |
$list_items_markup = ''; |
| 71 |
|
| 72 |
$list_items_markup = $content; |
| 73 |
|
| 74 |
$block_content .= $list_items_markup; |
| 75 |
$block_content .= '</div>'; |
| 76 |
$has_navigation = ! empty( $attributes['hasNavigation'] ) && true === $attributes['hasNavigation']; |
| 77 |
$has_pagination = ! empty( $attributes['hasPagination'] ) && true === $attributes['hasPagination']; |
| 78 |
if ( $has_navigation ) { |
| 79 |
$block_content .= '<div class="swiper-button-next"></div> |
| 80 |
<div class="swiper-button-prev"></div>'; |
| 81 |
} |
| 82 |
if ( $has_pagination ) { |
| 83 |
$block_content .= '<div class="swiper-pagination"></div>'; |
| 84 |
} |
| 85 |
$block_content .= '</div>'; |
| 86 |
$block_content .= '</div>'; |
| 87 |
$block_content .= '</div>'; |
| 88 |
|
| 89 |
$columns = ! empty( $attributes['columns'] ) ? $attributes['columns'] : 1; |
| 90 |
$space_between = ! empty( $attributes['spaceBetween'] ) ? $attributes['spaceBetween'] : 0; |
| 91 |
$block_client_id = ! empty( $attributes['blockClientId'] ) ? $attributes['blockClientId'] : 1; |
| 92 |
ob_start(); |
| 93 |
wp_enqueue_script( 'gutenify-swiper' ); |
| 94 |
$config = '{ |
| 95 |
loop: true, |
| 96 |
slidesPerView: ' . $columns . ', |
| 97 |
breakpoints: { |
| 98 |
// when window width is >= 320px |
| 99 |
320: { |
| 100 |
slidesPerView: 1, |
| 101 |
spaceBetween: 20 |
| 102 |
}, |
| 103 |
// when window width is >= 640px |
| 104 |
640: { |
| 105 |
slidesPerView: 1, |
| 106 |
spaceBetween: 15 |
| 107 |
}, |
| 108 |
1024: { |
| 109 |
slidesPerView: ' . $columns . ', |
| 110 |
spaceBetween: ' . $space_between . ' |
| 111 |
} |
| 112 |
},'; |
| 113 |
$config .= ' spaceBetween:' . $space_between . ','; |
| 114 |
if ( $has_navigation ) { |
| 115 |
$config .= ' navigation:{ |
| 116 |
nextEl: ".swiper-button-next", |
| 117 |
prevEl: ".swiper-button-prev", |
| 118 |
},'; |
| 119 |
} |
| 120 |
$config .= ' |
| 121 |
pagination:{ |
| 122 |
el: ".swiper-pagination", |
| 123 |
clickable: true, |
| 124 |
}, |
| 125 |
'; |
| 126 |
$config .= ' |
| 127 |
scrollbar:{ draggable: true }, |
| 128 |
on: { |
| 129 |
init: function () { |
| 130 |
jQuery(document).trigger("gutenifySlideinit" ); |
| 131 |
}, |
| 132 |
}, |
| 133 |
}'; |
| 134 |
$slider_var = 'slider_' . str_replace( '-', '_', $block_client_id ); |
| 135 |
$slider_function_name = 'reinitSlider' . $slider_var; |
| 136 |
wp_add_inline_script( |
| 137 |
'gutenify-swiper', |
| 138 |
'var ' . $slider_var . ' = new Swiper("#gutenify-section-' . $block_client_id . ' .swiper-container", ' . $config . '); |
| 139 |
jQuery(function($){ |
| 140 |
$(document).on("containerStretched", function(){ |
| 141 |
var swiperWrapper = $("' . $block_client_id . ' .swiper-wrapper"); |
| 142 |
var newSlides = swiperWrapper.children(".swiper-slide").clone(true); |
| 143 |
' . $slider_var . '.destroy(); |
| 144 |
swiperWrapper.empty().append(newSlides); |
| 145 |
swiperWrapper.attr("style", ""); |
| 146 |
mySwiper = new Swiper("#gutenify-section-' . $block_client_id . ' .swiper-container", ' . $config . '); |
| 147 |
}); |
| 148 |
});' |
| 149 |
); |
| 150 |
$block_content .= '<script> |
| 151 |
jQuery(function($){ |
| 152 |
function ' . $slider_function_name . '() { |
| 153 |
var swiperWrapper = $("#gutenify-section-' . $block_client_id . ' .swiper-wrapper"); |
| 154 |
var newSlides = swiperWrapper.children(".swiper-slide").clone(true); |
| 155 |
' . $slider_var . '.destroy(); |
| 156 |
swiperWrapper.empty().append(newSlides); |
| 157 |
swiperWrapper.attr("style", ""); |
| 158 |
mySwiper = new Swiper("#gutenify-section-' . $block_client_id . ' .swiper-container", ' . $config . '); |
| 159 |
} |
| 160 |
|
| 161 |
var ' . $slider_var . ' = new Swiper("#gutenify-section-' . $block_client_id . ' .swiper-container", ' . $config . '); |
| 162 |
$(window).on("resize", function(){ |
| 163 |
' . $slider_function_name . '(); |
| 164 |
}); |
| 165 |
' . $slider_var . '.on("slideChangeTransitionEnd", function (a,b) { |
| 166 |
jQuery(document).trigger("gutenifySlideChangeTransitionEnd" ); |
| 167 |
}); |
| 168 |
' . $slider_var . '.on("slideChangeTransitionStart", function (a,b) { |
| 169 |
jQuery(document).trigger("gutenifySlideChangeTransitionStart" ); |
| 170 |
}); |
| 171 |
$(document).on("containerStretched", function(){ |
| 172 |
' . $slider_function_name . '(); |
| 173 |
}); |
| 174 |
});</script>'; |
| 175 |
echo $block_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 176 |
$output = ob_get_contents(); |
| 177 |
ob_end_clean(); |
| 178 |
return $output; |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Registers the `gallery` block on server. |
| 184 |
*/ |
| 185 |
function gutenify_register_slider_block() { |
| 186 |
// Return early if this function does not exist. |
| 187 |
if ( ! function_exists( 'register_block_type' ) ) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
// Load attributes from block.json. |
| 192 |
ob_start(); |
| 193 |
include GUTENIFY_PLUGIN_DIR . 'src/blocks/slider/block.json'; |
| 194 |
$metadata = json_decode( ob_get_clean(), true ); |
| 195 |
|
| 196 |
register_block_type( |
| 197 |
'gutenify/slider', |
| 198 |
array( |
| 199 |
'attributes' => $metadata['attributes'], |
| 200 |
'render_callback' => 'gutenify_render_slider_block', |
| 201 |
) |
| 202 |
); |
| 203 |
} |
| 204 |
add_action( 'init', 'gutenify_register_slider_block' ); |
| 205 |
|