| 1 |
<?php |
| 2 |
|
| 3 |
namespace gutenify; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
class Post_Carousel { |
| 8 |
public static function init() { |
| 9 |
add_action( 'init', array( __CLASS__, 'register_block' ) ); |
| 10 |
|
| 11 |
add_filter( 'gutenify_render_block_gutenify/post-carousel', array( __CLASS__, 'render_block' ), 10, 4 ); |
| 12 |
} |
| 13 |
|
| 14 |
public static function register_block() { |
| 15 |
register_block_type( |
| 16 |
__DIR__, |
| 17 |
array( |
| 18 |
'render_callback' => array( __CLASS__, 'render_callback' ), |
| 19 |
) |
| 20 |
); |
| 21 |
} |
| 22 |
|
| 23 |
public static function render_callback( $attr, $content, $props ) { |
| 24 |
$layout = ! empty( $attr['layout'] ) ? esc_attr( $attr['layout'] ) : 1; |
| 25 |
$wrapper_attributes = get_block_wrapper_attributes( array( |
| 26 |
'class' => 'gutenify-post-carousel-' . $layout |
| 27 |
) ); |
| 28 |
|
| 29 |
$has_navigation = ! ( isset( $attr['hasNavigation']) && false === $attr['hasNavigation'] ); |
| 30 |
$has_pagination = ! ( isset( $attr['hasPagination']) && false === $attr['hasPagination'] ); |
| 31 |
|
| 32 |
$new_content = sprintf( '<div %s>', $wrapper_attributes ); |
| 33 |
$new_content .= '<div class="swiper-wrapper">'; |
| 34 |
$posts = Post_List::get_posts( $attr, $props ); |
| 35 |
$new_content .= self::display_posts( $posts, $attr, $props ); |
| 36 |
$new_content .= '</div>'; |
| 37 |
if ( $has_navigation ) { |
| 38 |
$new_content .= '<div class="navigation-wrap"> |
| 39 |
<div class="swiper-button-prev"></div> |
| 40 |
<div class="swiper-button-next"></div> |
| 41 |
</div>'; |
| 42 |
} |
| 43 |
|
| 44 |
if ( $has_pagination ) { |
| 45 |
$new_content .= '<div class="swiper-pagination"></div>'; |
| 46 |
} |
| 47 |
$new_content .= '</div>'; |
| 48 |
return $new_content; |
| 49 |
} |
| 50 |
|
| 51 |
public static function display_posts($posts, $attributes, $props) |
| 52 |
{ |
| 53 |
$constants = \gutenify\Helpers::plugin_constants(); |
| 54 |
$plugin_main_slug = $constants['plugin_main_slug']; |
| 55 |
$plugin_main_camel_case_name = $constants['plugin_main_camel_case_name']; |
| 56 |
|
| 57 |
|
| 58 |
$list_items_markup = ''; |
| 59 |
|
| 60 |
$has_image = !$attributes['displayFeaturedImage'] ? 'has-no-feature-image': ''; |
| 61 |
|
| 62 |
$default_image_url = $constants['core_base_url'] . 'assets/images/placeholder-attachment.png'; |
| 63 |
|
| 64 |
foreach ($posts as $post) { |
| 65 |
|
| 66 |
$list_class = ''; |
| 67 |
$align_self_class = ''; |
| 68 |
|
| 69 |
$list_items_markup .= '<div class="gutenify-post-carousel-item gutenify-post-carousel-item-wrapper swiper-slide ">'; |
| 70 |
$list_items_markup .= '<div class="gutenify-post-carousel-item-inner-wrapper ' . $has_image . '">'; |
| 71 |
|
| 72 |
if (!empty($attributes['displayFeaturedImage'])) { |
| 73 |
$thumbnail_url = $default_image_url; |
| 74 |
if (has_post_thumbnail($post['ID'])) { |
| 75 |
$thumbnail_url = get_the_post_thumbnail_url($post['ID']); |
| 76 |
} |
| 77 |
$list_items_markup .= sprintf( |
| 78 |
'<div class="gutenify-post-carousel-thumb"> |
| 79 |
<a class="image-hover-zoom" href="%1$s"> |
| 80 |
<img src="%2$s" /> |
| 81 |
</a> |
| 82 |
</div>', |
| 83 |
esc_url($post['postLink']), |
| 84 |
esc_url($thumbnail_url) |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
$list_items_markup .= sprintf( |
| 89 |
'<div class="gutenify-post-carousel-text-content %s">', |
| 90 |
esc_attr($align_self_class) |
| 91 |
); |
| 92 |
|
| 93 |
$title = $post['title']; |
| 94 |
|
| 95 |
if (!$title) { |
| 96 |
|
| 97 |
$title = _x('(no title)', 'placeholder when a post has no title', 'gutenify'); |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
$list_items_markup .= sprintf( |
| 102 |
'<h3 class="gutenify-post-carousel-title"><a href="%1$s" rel="bookmark">%2$s</a></h3> ', |
| 103 |
esc_url( $post['postLink'] ), |
| 104 |
esc_html( $title ) |
| 105 |
); |
| 106 |
$meta_data = ''; |
| 107 |
if (isset($attributes['displayPostDate']) && $attributes['displayPostDate']) { |
| 108 |
$meta_data .= sprintf( |
| 109 |
'<span class="posted-on"><time datetime="%1$s" class="gutenify-posts-date">%2$s</time></span>', |
| 110 |
esc_html($post['date']), |
| 111 |
esc_html($post['dateReadable']) |
| 112 |
); |
| 113 |
|
| 114 |
} |
| 115 |
if (!empty($attributes['displayPostAuthor'])) { |
| 116 |
if (!get_the_author_meta('description') && post_type_supports(get_post_type(), 'author')) { |
| 117 |
$meta_data .= '<span class="byline">'; |
| 118 |
$meta_data .= sprintf( |
| 119 |
'%s', |
| 120 |
'<a href="' . esc_url(get_author_posts_url($post['author_id'])) . '" rel="author">' . esc_html(get_the_author_meta('display_name', $post['author_id'])) . '</a>' |
| 121 |
); |
| 122 |
$meta_data .= '</span>'; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if (!empty($attributes['displayPostCategories'])) { |
| 127 |
/* translators: used between list items, there is a space after the comma. */ |
| 128 |
$categories_list = get_the_category_list(__(', '), '', $post['ID']); |
| 129 |
if ($categories_list) { |
| 130 |
$meta_data .= sprintf( |
| 131 |
'<span class="cat-links">%s </span>', |
| 132 |
$categories_list // phpcs:ignore WordPress.Security.EscapeOutput |
| 133 |
); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if (!empty($meta_data)) { |
| 138 |
$list_items_markup .= sprintf('<div class="gutenify-post-carousel-meta">%1$s</div>', $meta_data); |
| 139 |
} |
| 140 |
|
| 141 |
if (isset($attributes['displayPostContent']) && $attributes['displayPostContent']) { |
| 142 |
|
| 143 |
$post_excerpt = $post['postExcerpt']; |
| 144 |
$trimmed_excerpt = esc_html(wp_trim_words($post_excerpt, $attributes['excerptLength'], ' … ')); |
| 145 |
$list_items_markup .= '<div class="entry-summary">'; |
| 146 |
$list_items_markup .= sprintf( |
| 147 |
'<p>%1$s</p>', |
| 148 |
esc_html($trimmed_excerpt) |
| 149 |
); |
| 150 |
|
| 151 |
if (isset($attributes['displayPostLink']) && $attributes['displayPostLink']) { |
| 152 |
|
| 153 |
$list_items_markup .= sprintf( |
| 154 |
'<a href="%1$s" class="more-link"><span class="more-button">%2$s<span class="screen-reader-text">%3$s</span></span></a>', |
| 155 |
esc_url($post['postLink']), |
| 156 |
esc_html($attributes['postLink']), |
| 157 |
esc_html($post['title']) |
| 158 |
); |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
$list_items_markup .= '</div>'; |
| 163 |
} |
| 164 |
|
| 165 |
$list_items_markup .= '</div></div></div>'; |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
return $list_items_markup; |
| 170 |
} |
| 171 |
|
| 172 |
public static function render_block( $block_content, $block, $instance, $block_id ) { |
| 173 |
|
| 174 |
|
| 175 |
$css_thread = ''; |
| 176 |
//color |
| 177 |
$css_chunk = ''; |
| 178 |
if (!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['textColor'])) { |
| 179 |
$css_chunk .= 'color: ' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['textColor'] ) . ';'; |
| 180 |
} |
| 181 |
|
| 182 |
$background_color = !empty( $instance->attributes['blockAdvanceOptions']['innerBlock']['backgroundColor'] ) ? esc_attr($instance->attributes['blockAdvanceOptions']['innerBlock']['backgroundColor'] ) : ''; |
| 183 |
$background_color = ! empty( $block['attrs']['blockAdvanceOptions']['innerBlock']['backgroundColor'] ) ? esc_attr($block['attrs']['blockAdvanceOptions']['innerBlock']['backgroundColor'] ) : $background_color; |
| 184 |
|
| 185 |
if ( ! empty( $background_color ) ) { |
| 186 |
$css_chunk .= 'background-color: ' . esc_attr( $background_color ). ' ;'; |
| 187 |
} |
| 188 |
|
| 189 |
if (!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['backgroundGradient'])) { |
| 190 |
$css_chunk .= 'background: ' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['backgroundGradient'] ) . ';'; |
| 191 |
} |
| 192 |
|
| 193 |
if (!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['borderColor'])) { |
| 194 |
$css_chunk .= 'border-color: ' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['borderColor'] ) . ';'; |
| 195 |
} |
| 196 |
|
| 197 |
//borderWidth |
| 198 |
if ( ! empty( $block['attrs']['blockAdvanceOptions']['innerBlock']['borderWidth'] ) ) { |
| 199 |
$css_chunk .= 'border-style:solid;'; |
| 200 |
$css_chunk .= \gutenify\Style_Helpers::box_control( $block['attrs']['blockAdvanceOptions']['innerBlock']['borderWidth'], 'border-', '-width' ); |
| 201 |
} |
| 202 |
|
| 203 |
//borderWidth |
| 204 |
if ( ! empty( $block['attrs']['blockAdvanceOptions']['innerBlock']['borderRadius'] ) ) { |
| 205 |
$css_chunk .= \gutenify\Style_Helpers::border_radius_control( $block['attrs']['blockAdvanceOptions']['innerBlock']['borderRadius'] ); |
| 206 |
} |
| 207 |
|
| 208 |
if ( ! empty( $block['attrs']['blockAdvanceOptions']['innerBlock']['boxShadow'] ) ) { |
| 209 |
$css_chunk .= \gutenify\Style_Helpers::custom_box_shadow_control( $block['attrs']['blockAdvanceOptions']['innerBlock']['boxShadow'] ); |
| 210 |
} |
| 211 |
|
| 212 |
$inner_block_selector = '.' . $block_id . ' .gutenify-post-carousel-item-inner-wrapper'; |
| 213 |
|
| 214 |
$css_thread .= $inner_block_selector . '{'; |
| 215 |
$css_thread .= $css_chunk; |
| 216 |
$css_thread .= '}'; |
| 217 |
|
| 218 |
/** |
| 219 |
* Content padding |
| 220 |
*/ |
| 221 |
if ( ! empty( $block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding'] ) ) { |
| 222 |
$css_thread .= Dynamic_Styles::get_spacing_with_media( '.' . $block_id . '.wp-block-gutenify-post-carousel .gutenify-post-carousel-item .gutenify-post-carousel-item-inner-wrapper .gutenify-post-carousel-text-content', $block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding'], 'padding-' ); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
/** |
| 227 |
* Hover |
| 228 |
*/ |
| 229 |
$css_chunk = ''; |
| 230 |
if (!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['hoverTextColor'])) { |
| 231 |
$css_chunk .= 'color: ' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['hoverTextColor'] ) . ';'; |
| 232 |
} |
| 233 |
|
| 234 |
if (!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['hoverBackgroundColor'])) { |
| 235 |
$css_chunk .= 'background-color: ' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['hoverBackgroundColor'] ) . ';'; |
| 236 |
} |
| 237 |
|
| 238 |
if (!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['hoverBackgroundGradient'])) { |
| 239 |
$css_chunk .= 'background: ' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['hoverBackgroundGradient'] ) . ';'; |
| 240 |
} |
| 241 |
|
| 242 |
if (!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['hoverBorderColor'])) { |
| 243 |
$css_chunk .= 'border-color: ' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['hoverBorderColor'] ) . ';'; |
| 244 |
} |
| 245 |
|
| 246 |
$inner_block_selector = $inner_block_selector . ':hover'; |
| 247 |
|
| 248 |
$css_thread .= $inner_block_selector . '{'; |
| 249 |
$css_thread .= $css_chunk; |
| 250 |
$css_thread .= '}'; |
| 251 |
|
| 252 |
|
| 253 |
/** |
| 254 |
* Thubmnail |
| 255 |
*/ |
| 256 |
$css_chunk = ''; |
| 257 |
if (!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['thumbnailMinHeight'])) { |
| 258 |
$css_chunk .= 'height:' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['thumbnailMinHeight'] ) . ';'; |
| 259 |
} |
| 260 |
$thumbnail_selector = '.' . $block_id . ' .gutenify-post-carousel-item-inner-wrapper .gutenify-post-carousel-thumb img'; |
| 261 |
|
| 262 |
$css_thread .= $thumbnail_selector . '{'; |
| 263 |
$css_thread .= $css_chunk; |
| 264 |
$css_thread .= '}'; |
| 265 |
|
| 266 |
$css_chunk = ''; |
| 267 |
if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['postTitleSize'])){ |
| 268 |
$css_chunk .= 'font-size:'. esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['postTitleSize'] ); |
| 269 |
} |
| 270 |
$post_title_selector = '.' . $block_id . ' .gutenify-post-carousel-title'; |
| 271 |
$css_thread .= $post_title_selector . '{'; |
| 272 |
$css_thread .= $css_chunk; |
| 273 |
$css_thread .= '}'; |
| 274 |
|
| 275 |
$handle = 'gutenify_' . str_replace( '/', '_', $block['blockName'] ) . '_' . $block_id; |
| 276 |
wp_add_inline_style( $handle, $css_thread ); |
| 277 |
|
| 278 |
return $block_content; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
Post_Carousel::init(); |
| 283 |
|
| 284 |
|