|
1 <?php |
|
2 /** |
|
3 * Server-side rendering of the `core/latest-posts` block. |
|
4 * |
|
5 * @package WordPress |
|
6 */ |
|
7 |
|
8 /** |
|
9 * Renders the `core/latest-posts` block on server. |
|
10 * |
|
11 * @param array $attributes The block attributes. |
|
12 * |
|
13 * @return string Returns the post content with latest posts added. |
|
14 */ |
|
15 function render_block_core_latest_posts( $attributes ) { |
|
16 $args = array( |
|
17 'posts_per_page' => $attributes['postsToShow'], |
|
18 'post_status' => 'publish', |
|
19 'order' => $attributes['order'], |
|
20 'orderby' => $attributes['orderBy'], |
|
21 'suppress_filters' => false, |
|
22 ); |
|
23 |
|
24 if ( isset( $attributes['categories'] ) ) { |
|
25 $args['category'] = $attributes['categories']; |
|
26 } |
|
27 |
|
28 $recent_posts = get_posts( $args ); |
|
29 |
|
30 $list_items_markup = ''; |
|
31 |
|
32 foreach ( $recent_posts as $post ) { |
|
33 $title = get_the_title( $post ); |
|
34 if ( ! $title ) { |
|
35 $title = __( '(Untitled)' ); |
|
36 } |
|
37 $list_items_markup .= sprintf( |
|
38 '<li><a href="%1$s">%2$s</a>', |
|
39 esc_url( get_permalink( $post ) ), |
|
40 $title |
|
41 ); |
|
42 |
|
43 if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) { |
|
44 $list_items_markup .= sprintf( |
|
45 '<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>', |
|
46 esc_attr( get_the_date( 'c', $post ) ), |
|
47 esc_html( get_the_date( '', $post ) ) |
|
48 ); |
|
49 } |
|
50 |
|
51 $list_items_markup .= "</li>\n"; |
|
52 } |
|
53 |
|
54 $class = 'wp-block-latest-posts'; |
|
55 if ( isset( $attributes['align'] ) ) { |
|
56 $class .= ' align' . $attributes['align']; |
|
57 } |
|
58 |
|
59 if ( isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) { |
|
60 $class .= ' is-grid'; |
|
61 } |
|
62 |
|
63 if ( isset( $attributes['columns'] ) && 'grid' === $attributes['postLayout'] ) { |
|
64 $class .= ' columns-' . $attributes['columns']; |
|
65 } |
|
66 |
|
67 if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) { |
|
68 $class .= ' has-dates'; |
|
69 } |
|
70 |
|
71 if ( isset( $attributes['className'] ) ) { |
|
72 $class .= ' ' . $attributes['className']; |
|
73 } |
|
74 |
|
75 $block_content = sprintf( |
|
76 '<ul class="%1$s">%2$s</ul>', |
|
77 esc_attr( $class ), |
|
78 $list_items_markup |
|
79 ); |
|
80 |
|
81 return $block_content; |
|
82 } |
|
83 |
|
84 /** |
|
85 * Registers the `core/latest-posts` block on server. |
|
86 */ |
|
87 function register_block_core_latest_posts() { |
|
88 register_block_type( |
|
89 'core/latest-posts', |
|
90 array( |
|
91 'attributes' => array( |
|
92 'categories' => array( |
|
93 'type' => 'string', |
|
94 ), |
|
95 'className' => array( |
|
96 'type' => 'string', |
|
97 ), |
|
98 'postsToShow' => array( |
|
99 'type' => 'number', |
|
100 'default' => 5, |
|
101 ), |
|
102 'displayPostDate' => array( |
|
103 'type' => 'boolean', |
|
104 'default' => false, |
|
105 ), |
|
106 'postLayout' => array( |
|
107 'type' => 'string', |
|
108 'default' => 'list', |
|
109 ), |
|
110 'columns' => array( |
|
111 'type' => 'number', |
|
112 'default' => 3, |
|
113 ), |
|
114 'align' => array( |
|
115 'type' => 'string', |
|
116 ), |
|
117 'order' => array( |
|
118 'type' => 'string', |
|
119 'default' => 'desc', |
|
120 ), |
|
121 'orderBy' => array( |
|
122 'type' => 'string', |
|
123 'default' => 'date', |
|
124 ), |
|
125 ), |
|
126 'render_callback' => 'render_block_core_latest_posts', |
|
127 ) |
|
128 ); |
|
129 } |
|
130 |
|
131 add_action( 'init', 'register_block_core_latest_posts' ); |