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

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