PluginProbe
Gutenify – Visual Site Builder Blocks & Starter Templates / 1.0.2
Gutenify – Visual Site Builder Blocks & Starter Templates v1.0.2
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 / src / blocks / post-list / index.php

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

258 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 *
13 * @return string Returns the block content.
14 */
15 function gutenify_render_post_list_block( $attributes ) {
16
17 global $post;
18 $args = array(
19 'posts_per_page' => ! empty( $attributes['query']['numberOfItems'] ) ? $attributes['query']['numberOfItems'] : 10,
20 'post_status' => 'publish',
21 'order' => ! empty( $attributes['query']['order'] ) ? $attributes['query']['order'] : 'desc',
22 'orderby' => ! empty( $attributes['query']['orderBy'] ) ? $attributes['query']['orderBy'] : 'date',
23 'suppress_filters' => false,
24 'post__not_in' => array( $post->ID ),
25 );
26
27 if ( isset( $attributes['query']['selectedCategories'] ) ) {
28
29 $args['category__in'] = array_column( $attributes['query']['selectedCategories'], 'id' );
30
31 }
32
33 $recent_posts = get_posts( $args );
34 $formatted_posts = gutenify_get_post_info( $recent_posts );
35
36 $block_style = null;
37
38 if ( isset( $attributes['className'] ) && strpos( $attributes['className'], 'is-style-horizontal' ) !== false ) {
39
40 $block_style = 'horizontal';
41
42 } elseif ( isset( $attributes['className'] ) && strpos( $attributes['className'], 'is-style-stacked' ) !== false ) {
43
44 $block_style = 'stacked';
45
46 }
47
48 return gutenify_post_list( $formatted_posts, $attributes );
49 }
50
51
52 /**
53 * Renders the list and grid styles.
54 *
55 * @param array $posts Current posts.
56 * @param array $attributes The block attributes.
57 *
58 * @return string Returns the block content for the list and grid styles.
59 */
60 function gutenify_post_list( $posts, $attributes ) {
61 $client_id = ! empty( $attributes['blockClientId'] ) ? $attributes['blockClientId'] : '';
62 $class_name = array( 'wp-block-gutenify-post-list', 'gutenify-section-' . $attributes['blockClientId'], 'gutenify-post-list-' . $attributes['layout'] );
63 $class = array( 'gutenify-post-list' );
64 $styles = array();
65 $block_style = ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'is-style-stacked' ) !== false ? 'stacked' : 'horizontal';
66 $columns = ! empty( $attributes['columns'] ) ? $attributes['columns'] : 1;
67
68 if ( isset( $attributes['className'] ) ) {
69 array_push( $class_name, $attributes['className'] );
70 }
71
72 if ( isset( $attributes['align'] ) ) {
73 array_push( $class_name, 'align' . $attributes['align'] );
74 }
75
76 if ( isset( $attributes['columns'] ) ) {
77 array_push( $class, 'has-columns has-' . $attributes['columns'] . '-columns has-responsive-columns' );
78 }
79
80 if ( isset( $attributes['listPosition'] ) && 'right' === $attributes['listPosition'] && 'horizontal' === $block_style ) {
81 array_push( $class, 'has-image-right' );
82 }
83
84 if ( isset( $attributes['imageSize'] ) && 'horizontal' === $block_style ) {
85 array_push( $class, 'has-' . $attributes['imageSize'] . '-image' );
86 }
87
88 if ( isset( $attributes['imageStyle'] ) ) {
89 array_push( $class, 'has-' . $attributes['imageStyle'] . '-image' );
90 }
91
92 if ( isset( $columns ) ) {
93 array_push( $class_name, 'gutenify-post-list-col-' . $columns );
94 }
95
96 $class_name = apply_filters( 'gutenify_block_wrapper_class', esc_attr( implode( ' ', $class_name ) ), $attributes );
97
98 $block_content = sprintf(
99 '<div class="%1$s"><div class="gutenify-post-list-section" id="gutenify-section-' . $client_id . '">',
100 $class_name,
101 );
102
103 $list_items_markup = '';
104
105 foreach ( $posts as $post ) {
106
107 $list_class = '';
108 $align_self_class = '';
109
110 $list_items_markup .= '<div class="gutenify-post-list-item gutenify-post-list-item-wrapper inner-block-shadow">';
111 $list_items_markup .= '<div class="gutenify-post-list-item-inner-wrapper">';
112
113 if ( ! empty( $attributes['displayFeaturedImage'] ) && null !== $post['thumbnailURL'] && $post['thumbnailURL'] ) {
114
115 $list_items_markup .= sprintf(
116 '<div class="gutenify-post-list-thumb">
117 <a class="image-hover-zoom" href="%1$s">
118 <img src="%2$s" />
119 </a>
120 </div>',
121 esc_url( $post['postLink'] ),
122 esc_url( $post['thumbnailURL'] )
123 );
124
125 if ( 'horizontal' === $block_style && ( isset( $attributes['displayPostContent'] ) && ! $attributes['displayPostContent'] ) && ( isset( $attributes['columns'] ) && 2 >= $attributes['columns'] ) ) {
126
127 $align_self_class = 'self-center';
128 }
129 } else {
130 $align_self_class = ' flex-start';
131 }
132
133 $list_items_markup .= sprintf(
134 '<div class="gutenify-post-list-text-content %s">',
135 esc_attr( $align_self_class )
136 );
137
138 if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] && 'stacked' === $block_style ) {
139
140 $list_items_markup .= sprintf(
141 '<time datetime="%1$s" class="wp-block-gutenify-posts__date">%2$s</time>',
142 esc_url( $post['date'] ),
143 esc_html( $post['dateReadable'] )
144 );
145
146 }
147
148 $title = $post['title'];
149
150 if ( ! $title ) {
151
152 $title = _x( '(no title)', 'placeholder when a post has no title', 'gutenify' );
153
154 }
155
156 $list_items_markup .= sprintf(
157 '<h3 class="gutenify-post-list-title"><a href="%1$s" rel="bookmark">%2$s</a></h3> ',
158 $post['postLink'],
159 $title
160 );
161 $meta_data = '';
162 if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] && 'horizontal' === $block_style ) {
163 $meta_data .= sprintf(
164 '<span class="posted-on"><time datetime="%1$s" class="gutenify-posts-date">%2$s</time></span>',
165 esc_html( $post['date'] ),
166 esc_html( $post['dateReadable'] )
167 );
168
169 }
170 if ( ! empty( $attributes['displayPostAuthor'] ) ) {
171 if ( ! get_the_author_meta( 'description' ) && post_type_supports( get_post_type(), 'author' ) ) {
172 $meta_data .= '<span class="byline">';
173 $meta_data .= sprintf(
174 '%s',
175 '<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>'
176 );
177 $meta_data .= '</span>';
178 }
179 }
180
181 if ( ! empty( $attributes['displayPostCategories'] ) ) {
182 /* translators: used between list items, there is a space after the comma. */
183 $categories_list = get_the_category_list( __( ', ' ), '', $post['ID'] );
184 if ( $categories_list ) {
185 $meta_data .= sprintf(
186 '<span class="cat-links">%s </span>',
187 $categories_list // phpcs:ignore WordPress.Security.EscapeOutput
188 );
189 }
190 }
191
192 if ( ! empty( $meta_data ) ) {
193 $list_items_markup .= sprintf( '<div class="gutenify-post-list-meta">%1$s</div>', $meta_data );
194 }
195
196 if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent'] ) {
197
198 $post_excerpt = $post['postExcerpt'];
199 $trimmed_excerpt = esc_html( wp_trim_words( $post_excerpt, $attributes['excerptLength'], ' &hellip; ' ) );
200 $list_items_markup .= '<div class="entry-summary">';
201 $list_items_markup .= sprintf(
202 '<p>%1$s</p>',
203 esc_html( $trimmed_excerpt )
204 );
205
206 if ( isset( $attributes['displayPostLink'] ) && $attributes['displayPostLink'] ) {
207
208 $list_items_markup .= sprintf(
209 '<a href="%1$s" class="more-link"><span class="more-button">%2$s<span class="screen-reader-text">%3$s</span></span></a>',
210 esc_url( $post['postLink'] ),
211 esc_html( $attributes['postLink'] ),
212 esc_html( $post['title'] )
213 );
214
215 }
216
217 $list_items_markup .= '</div>';
218 }
219
220 $list_items_markup .= '</div></div></div>';
221
222 }
223
224 $block_content .= $list_items_markup;
225 $block_content .= '</div>';
226 $block_content .= '</div>';
227 ob_start();
228 echo $block_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
229 $output = ob_get_contents();
230 ob_end_clean();
231 return $output;
232
233 }
234
235 /**
236 * Registers the `posts` block on server.
237 */
238 function gutenify_register_post_list_block() {
239 // Return early if this function does not exist.
240 if ( ! function_exists( 'register_block_type' ) ) {
241 return;
242 }
243
244 // Load attributes from block.json.
245 ob_start();
246 include GUTENIFY_PLUGIN_DIR . 'src/blocks/post-list/block.json';
247 $metadata = json_decode( ob_get_clean(), true );
248
249 register_block_type(
250 'gutenify/post-list',
251 array(
252 'attributes' => $metadata['attributes'],
253 'render_callback' => 'gutenify_render_post_list_block',
254 )
255 );
256 }
257 add_action( 'init', 'gutenify_register_post_list_block' );
258