PluginProbe
Gutenify – Visual Site Builder Blocks & Starter Templates / 1.1.2
Gutenify – Visual Site Builder Blocks & Starter Templates v1.1.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.1.2, at src/blocks/post-list/index.php

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