PluginProbe
Gutenify – Visual Site Builder Blocks & Starter Templates / 1.8.0
Gutenify – Visual Site Builder Blocks & Starter Templates v1.8.0
1.9.0 1.8.0 1.7.0 1.6.6 1.6.5 1.6.4 trunk 0.0.1 0.0.2 0.0.3 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 All 74 releases
gutenify / core / dist / blocks / post-list / index.php

index.php in Gutenify – Visual Site Builder Blocks & Starter Templates 1.8.0, at core/dist/blocks/post-list/index.php

473 lines 18.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Gutenify Post List
4 * Plugin URI: https://gutenify.com
5 * Description: Post List block.
6 * Version: 1.0.0
7 * Author: Gutenify
8 *
9 * @package gutenify
10 */
11
12 defined( 'ABSPATH' ) || exit;
13
14 /**
15 * Renders the block on server.
16 *
17 * @param array $attributes The block attributes.
18 *
19 * @return string Returns the block content.
20 */
21 function gutenify_render_block_post_list( $attributes ) {
22 global $post;
23 $args = array(
24 'posts_per_page' => ! empty( $attributes['query']['numberOfItems'] ) ? $attributes['query']['numberOfItems'] : 10,
25 'post_status' => 'publish',
26 'order' => ! empty( $attributes['query']['order'] ) ? $attributes['query']['order'] : 'desc',
27 'orderby' => ! empty( $attributes['query']['orderBy'] ) ? $attributes['query']['orderBy'] : 'date',
28 'suppress_filters' => false,
29 'post_type' => ! empty( $attributes['query']['postTypes'] ) ? (array) $attributes['query']['postTypes'] : 'post',
30 'category__in' => array(),
31 'post__not_in' => array( get_the_ID() ),
32 );
33 if ( ! empty( $attributes['query']['selectedCategories'] ) ) {
34
35 $args['category__in'] = array_column( $attributes['query']['selectedCategories'], 'id' );
36
37 }
38
39 if ( ! empty( $attributes['query']['tax']['category'] ) ) {
40
41 $args['category__in'] = array_merge( $attributes['query']['tax']['category'], $args['category__in'] );
42
43 }
44
45 if ( ! empty( $attributes['query']['tax']['tag'] ) ) {
46 $args['tag__in'] = $attributes['query']['tax']['tag'];
47 }
48
49 if ( ! empty( $attributes['query']['authorIds'] ) ) {
50 $args['author__in'] = $attributes['query']['authorIds'];
51 }
52
53 if ( ! empty( $attributes['query']['postTypes'] ) && in_array( 'attachment', $attributes['query']['postTypes'] ) ) {
54 $args['post_status'] = 'inherit';
55
56 }
57 $recent_posts = get_posts( $args );
58
59 $formatted_posts = gutenify_get_post_info( $recent_posts );
60
61 $block_style = null;
62
63 if ( isset( $attributes['className'] ) && strpos( $attributes['className'], 'is-style-horizontal' ) !== false ) {
64
65 $block_style = 'horizontal';
66
67 } elseif ( isset( $attributes['className'] ) && strpos( $attributes['className'], 'is-style-stacked' ) !== false ) {
68
69 $block_style = 'stacked';
70
71 }
72
73 return gutenify_post_list( $formatted_posts, $attributes );
74 }
75
76
77 /**
78 * Renders the list and grid styles.
79 *
80 * @param array $posts Current posts.
81 * @param array $attributes The block attributes.
82 *
83 * @return string Returns the block content for the list and grid styles.
84 */
85 function gutenify_post_list( $posts, $attributes ) {
86 $client_id = ! empty( $attributes['blockClientId'] ) ? $attributes['blockClientId'] : '';
87 $list_layout = ! empty( $attributes['blockAdvanceOptions']['postListLayout'] ) ? $attributes['blockAdvanceOptions']['postListLayout'] : 'layout-1';
88 $class_name = array( 'wp-block-gutenify-post-list', 'gutenify-section-' . $attributes['blockClientId'], 'gutenify-post-list-' . $attributes['layout'], 'gutenify--post-list--post-list-' . $list_layout, 'gutenify-post-list-version-2' );
89 $class = array( 'gutenify-post-list' );
90 $styles = array();
91 $block_style = ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'is-style-stacked' ) !== false ? 'stacked' : 'horizontal';
92 $columns = ! empty( $attributes['columns'] ) ? $attributes['columns'] : 1;
93 $constants = \gutenify\Helpers::plugin_constants();
94
95 if ( isset( $attributes['className'] ) ) {
96 array_push( $class_name, $attributes['className'] );
97 }
98
99 if ( isset( $attributes['align'] ) ) {
100 array_push( $class_name, 'align' . $attributes['align'] );
101 }
102
103 if ( isset( $attributes['columns'] ) ) {
104 array_push( $class, 'has-columns has-' . $attributes['columns'] . '-columns has-responsive-columns' );
105 }
106
107 if ( isset( $attributes['listPosition'] ) && 'right' === $attributes['listPosition'] && 'horizontal' === $block_style ) {
108 array_push( $class, 'has-image-right' );
109 }
110
111 if ( isset( $attributes['imageSize'] ) && 'horizontal' === $block_style ) {
112 array_push( $class, 'has-' . $attributes['imageSize'] . '-image' );
113 }
114
115 if ( isset( $attributes['imageStyle'] ) ) {
116 array_push( $class, 'has-' . $attributes['imageStyle'] . '-image' );
117 }
118
119 if ( isset( $columns ) ) {
120 array_push( $class_name, 'gutenify-post-list-col-' . $columns );
121 }
122
123 $class_name = apply_filters( 'gutenify_block_wrapper_class', esc_attr( implode( ' ', $class_name ) ), $attributes );
124
125 $block_content = sprintf(
126 '<div class="%1$s">',
127 $class_name,
128 );
129
130 $list_items_markup = '';
131
132 $constants = \Gutenify\Helpers::plugin_constants();
133 $default_image_url = $constants['core_base_url'] . 'assets/images/placeholder-attachment.png';
134 foreach ( $posts as $post ) {
135
136 $list_class = '';
137 $align_self_class = '';
138
139 $list_items_markup .= '<div class="gutenify-post-list-item gutenify-post-list-item-wrapper inner-block-shadow">';
140 $list_items_markup .= '<div class="gutenify-post-list-item-inner-wrapper">';
141
142 if ( ! empty( $attributes['displayFeaturedImage'] ) ) {
143 $thumbnail_url = $default_image_url;
144 if ( has_post_thumbnail( $post['ID'] ) ) {
145 $thumbnail_url = get_the_post_thumbnail_url( $post['ID'] );
146 }
147
148 $list_items_markup .= sprintf(
149 '<div class="gutenify-post-list-thumb">
150 <a class="image-hover-zoom" href="%1$s">
151 <img src="%2$s" />
152 </a>
153 </div>',
154 esc_url( $post['postLink'] ),
155 esc_url( $thumbnail_url ),
156 esc_url( $thumbnail_url )
157 );
158
159 if ( 'horizontal' === $block_style && ( ! isset( $attributes['displayPostContent'] ) || ! $attributes['displayPostContent'] ) && ( isset( $attributes['columns'] ) && 2 >= $attributes['columns'] ) ) {
160 $align_self_class = 'self-center';
161 }
162 } else {
163 $align_self_class = isset( $align_self_class ) ? $align_self_class . ' flex-start' : 'flex-start';
164 }
165
166 $list_items_markup .= sprintf(
167 '<div class="gutenify-post-list-text-content %s">',
168 esc_attr( $align_self_class )
169 );
170
171 if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] && 'stacked' === $block_style ) {
172
173 $list_items_markup .= sprintf(
174 '<time datetime="%1$s" class="wp-block-gutenify-posts__date">%2$s</time>',
175 esc_url( $post['date'] ),
176 esc_html( $post['dateReadable'] )
177 );
178
179 }
180
181 $title = $post['title'];
182
183 if ( ! $title ) {
184
185 $title = _x( '(no title)', 'placeholder when a post has no title', 'gutenify' );
186
187 }
188
189 $list_items_markup .= sprintf(
190 '<h3 class="gutenify-post-list-title"><a href="%1$s" rel="bookmark">%2$s</a></h3> ',
191 esc_url( $post['postLink'] ),
192 esc_html( $title )
193 );
194 $meta_data = '';
195 if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] && 'horizontal' === $block_style ) {
196 $meta_data .= sprintf(
197 '<span class="posted-on"><time datetime="%1$s" class="gutenify-posts-date">%2$s</time></span>',
198 esc_html( $post['date'] ),
199 esc_html( $post['dateReadable'] )
200 );
201
202 }
203 if ( ! empty( $attributes['displayPostAuthor'] ) ) {
204 if ( ! get_the_author_meta( 'description' ) && post_type_supports( get_post_type(), 'author' ) ) {
205 $meta_data .= '<span class="byline">';
206 $meta_data .= sprintf(
207 '%s',
208 '<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>'
209 );
210 $meta_data .= '</span>';
211 }
212 }
213
214 if ( ! empty( $attributes['displayPostCategories'] ) ) {
215 /* translators: used between list items, there is a space after the comma. */
216 $categories_list = get_the_category_list( __( ', ' ), '', $post['ID'] );
217 if ( $categories_list ) {
218 $meta_data .= sprintf(
219 '<span class="cat-links">%s </span>',
220 $categories_list // phpcs:ignore WordPress.Security.EscapeOutput
221 );
222 }
223 }
224
225 if ( ! empty( $meta_data ) ) {
226 $list_items_markup .= sprintf( '<div class="gutenify-post-list-meta">%1$s</div>', $meta_data );
227 }
228
229 if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent'] ) {
230
231 $post_excerpt = $post['postExcerpt'];
232 $trimmed_excerpt = esc_html( wp_trim_words( $post_excerpt, $attributes['excerptLength'], ' &hellip; ' ) );
233 $list_items_markup .= '<div class="entry-summary">';
234 $list_items_markup .= sprintf(
235 '<p>%1$s</p>',
236 esc_html( $trimmed_excerpt )
237 );
238
239 if ( isset( $attributes['displayPostLink'] ) && $attributes['displayPostLink'] ) {
240
241 $list_items_markup .= sprintf(
242 '<a href="%1$s" class="more-link"><span class="more-button">%2$s<span class="screen-reader-text">%3$s</span></span></a>',
243 esc_url( $post['postLink'] ),
244 esc_html( $attributes['postLink'] ),
245 esc_html( $post['title'] )
246 );
247
248 }
249
250 $list_items_markup .= '</div>';
251 }
252
253 $list_items_markup .= '</div></div></div>';
254
255 }
256
257 $block_content .= $list_items_markup;
258 // $block_content .= '</div>';
259 $block_content .= '</div>';
260 ob_start();
261 echo $block_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
262 $output = ob_get_contents();
263 ob_end_clean();
264 return $output;
265 }
266
267 /**
268 * Load all translations for our plugin from the MO file.
269 */
270 function gutenify_load_textdomain_block_post_list() {
271 load_plugin_textdomain( 'gutenify', false, basename( __DIR__ ) . '/languages' );
272 }
273 add_action( 'init', 'gutenify_load_textdomain_block_post_list' );
274
275 /**
276 * Registers all block assets so that they can be enqueued through Gutenberg in
277 * the corresponding context.
278 *
279 * Passes translations to JavaScript.
280 */
281 function gutenify_register_block_post_list() {
282
283 // Register the block by passing the location of block.json to register_block_type.
284 register_block_type(
285 __DIR__,
286 array(
287 'render_callback' => 'gutenify_render_block_post_list',
288 )
289 );
290
291 if ( function_exists( 'wp_set_script_translations' ) ) {
292 /**
293 * May be extended to wp_set_script_translations( 'my-handle', 'my-domain',
294 * plugin_dir_path( MY_PLUGIN ) . 'languages' ) ). For details see
295 * https://make.wordpress.org/core/2018/11/09/new-javascript-i18n-support-in-wordpress/
296 */
297 wp_set_script_translations( 'gutenify-block-post-list', 'gutenify' );
298 }
299 }
300 add_action( 'init', 'gutenify_register_block_post_list' );
301
302
303 function gutenify_post_list_render_block ($block_content, $block){
304 if ((isset($block['blockName']) && 'gutenify/post-list' !== $block['blockName'])|| is_admin()) {
305 return $block_content;
306 }
307
308 $block_id = wp_unique_id('gtfy-');
309 $block_client_id = !empty($block['attrs']['blockClientId']) ? 'gutenify-section-' . esc_attr($block['attrs']['blockClientId']) : $block_id;
310
311 $block_content = new WP_HTML_Tag_Processor($block_content);
312 $block_content->next_tag();
313 $block_content->add_class($block_client_id);
314
315 //selector
316 $root_selector = '.' . $block_client_id . '.wp-block-gutenify-post-list' ;
317 $inner_block_selector = $root_selector . ' .gutenify-post-list-item-inner-wrapper';
318 $css ='';
319
320 $css .= $inner_block_selector . '{';
321
322 //color
323 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['textColor'])){
324 $css .= 'color: ' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['textColor'] ) . ';';
325 }
326
327 //background Color
328 if(!empty($block['attrs']['backgroundGradient'])){
329 $css .= 'background:' . esc_attr( $block['attrs']['backgroundGradient'] ) . ';';
330 }elseif(!empty($block['attrs']['backgroundColor'])){
331 $css .= 'background:' . esc_attr( $block['attrs']['backgroundColor'] ) . ';';
332 }elseif(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['backgroundGradient'])){
333 $css .= 'background: ' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['backgroundGradient'] ) . ';';
334 }elseif(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['backgroundColor'])){
335 $css .= 'background: ' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['backgroundColor'] ) . ';';
336 }
337
338 //border color
339 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['borderColor'])){
340 $css .= 'border-color: ' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['borderColor'] ) . ';';
341
342 }
343
344 if ( ! empty( $block['attrs']['blockAdvanceOptions']['innerBlock']['borderWidth'] ) ) {
345 $css .= 'border-style:solid;';
346 if ( ! is_array( $block['attrs']['blockAdvanceOptions']['innerBlock']['borderWidth'] ) ) {
347 $css .= 'border-width: ' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['borderWidth'] ) . 'px;';
348 } else {
349 $css .= \gutenify\Style_Helpers::box_control( $block['attrs']['blockAdvanceOptions']['innerBlock']['borderWidth'], 'border-', '-width');
350 }
351 }
352
353 if ( ! empty( $block['attrs']['blockAdvanceOptions']['innerBlock']['borderRadius'] ) ) {
354 $css .= \gutenify\Style_Helpers::border_radius_control( $block['attrs']['blockAdvanceOptions']['innerBlock']['borderRadius'] );
355 }
356
357 if ( ! empty( $block['attrs']['blockAdvanceOptions']['innerBlock']['boxShadow'] ) ) {
358 $css .= \gutenify\Style_Helpers::custom_box_shadow_control( $block['attrs']['blockAdvanceOptions']['innerBlock']['boxShadow'] );
359 }
360 $css .='}';
361
362
363 //thumbnail
364 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['thumbnailMinHeight'])){
365 $css .= $root_selector . '.wp-block-gutenify-post-list .gutenify-post-list-item-inner-wrapper .gutenify-post-list-thumb img {';
366 $css .= 'height:' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['thumbnailMinHeight'] ) . ';}';
367 }
368 //padding
369 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding'])){
370
371 $css .= $inner_block_selector . ' .gutenify-post-list-text-content{';
372 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['desktop']['top'])){
373 $css .= 'padding-top:' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['desktop']['top'] ) . ';';
374 }
375 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['desktop']['bottom'])){
376 $css .= 'padding-bottom:' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['desktop']['bottom'] ) . ';';
377 }
378 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['desktop']['left'])){
379 $css .= 'padding-left:' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['desktop']['left'] ) . ';';
380 }
381 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['desktop']['right'])){
382 $css .= 'padding-right:' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['desktop']['right'] ) . ';';
383 }
384 $css .='}';
385
386 $css .= '@media only screen and (max-width: 992px) {';
387 $css .= $inner_block_selector . ' .gutenify-post-list-text-content{';
388 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['tablet']['top'])){
389 $css .= 'padding-top:' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['tablet']['top'] ) . ';';
390 }
391 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['tablet']['bottom'])){
392 $css .= 'padding-bottom:' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['tablet']['bottom'] ) . ';';
393 }
394 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['tablet']['left'])){
395 $css .= 'padding-left:' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['tablet']['left'] ) . ';';
396 }
397 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['tablet']['right'])){
398 $css .= 'padding-right:' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['tablet']['right'] ) . ';';
399 }
400 $css .='}';
401 $css .='}';
402
403 $css .= '@media only screen and (max-width: 768px) {';
404
405 $css .= $inner_block_selector . ' .gutenify-post-list-text-content{';
406 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['mobile']['top'])){
407 $css .= 'padding-top:' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['mobile']['top'] ) . ';';
408 }
409 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['mobile']['bottom'])){
410 $css .= 'padding-bottom:' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['mobile']['bottom'] ) . ';';
411 }
412 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['mobile']['left'])){
413 $css .= 'padding-left:' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['mobile']['left'] ) . ';';
414 }
415 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['mobile']['right'])){
416 $css .= 'padding-right:' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['contentPadding']['mobile']['right'] ) . ';';
417 }
418 $css .='}';
419 $css .='}';
420
421
422
423 }
424
425
426 if (!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['postTitleSize'])) {
427 $css .= $inner_block_selector . ' .gutenify-post-carousel-title { font-size: ' . esc_attr($block['attrs']['blockAdvanceOptions']['innerBlock']['postTitleSize']) . '; }';
428 }
429
430 //hover style
431 $css .= $inner_block_selector . ':hover {';
432
433 //color
434 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['hoverTextColor'])){
435 $css .= ' color: ' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['hoverTextColor'] ) . ';';
436 }
437
438 //background
439 if(!empty($block['attrs']['hoverBackgroundGradient'])){
440 $css .= 'background:' . esc_attr( $block['attrs']['hoverBackgroundGradient'] ) . ';';
441 }elseif(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['hoverBackgroundGradient'])){
442 $css .= 'background: ' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['hoverBackgroundGradient'] ) . ';';
443 }elseif(!empty($block['attrs']['hoverBackgroundColor'])){
444 $css .= 'background:' . esc_attr( $block['attrs']['hoverBackgroundColor'] ) . ';';
445 }elseif(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['hoverBackgroundColor'])){
446 $css .= 'background: ' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['hoverBackgroundColor'] ) . ';';
447 }
448
449
450 //border color
451 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['hoverBorderColor'])){
452 $css .= 'border-color: ' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['hoverBorderColor'] ) . ';';
453 }
454
455 if ( ! empty( $block['attrs']['blockAdvanceOptions']['innerBlock']['hoverBoxShadow'] ) ) {
456 $css .= \gutenify\Style_Helpers::custom_box_shadow_control( $block['attrs']['blockAdvanceOptions']['innerBlock']['hoverBoxShadow'] );
457 }
458
459 $css .= '}';
460
461
462 //font size
463 if(!empty($block['attrs']['blockAdvanceOptions']['innerBlock']['postTitleSize'])){
464 $css .= $root_selector . ' .gutenify-post-list-title{ font-size:' . esc_attr( $block['attrs']['blockAdvanceOptions']['innerBlock']['postTitleSize'] ) . ';}';
465 }
466
467 $handle = 'gutenify-block-inline-handle';
468 wp_add_inline_style('wp-block-library', $css);
469 return $block_content->get_updated_html();
470 }
471
472 add_filter('render_block', 'gutenify_post_list_render_block', 200, 2);
473