12 * |
12 * |
13 * @return string Returns the block content. |
13 * @return string Returns the block content. |
14 */ |
14 */ |
15 function render_block_core_calendar( $attributes ) { |
15 function render_block_core_calendar( $attributes ) { |
16 global $monthnum, $year; |
16 global $monthnum, $year; |
|
17 |
|
18 // Calendar shouldn't be rendered |
|
19 // when there are no published posts on the site. |
|
20 if ( ! block_core_calendar_has_published_posts() ) { |
|
21 if ( is_user_logged_in() ) { |
|
22 return '<div>' . __( 'The calendar block is hidden because there are no published posts.' ) . '</div>'; |
|
23 } |
|
24 return ''; |
|
25 } |
17 |
26 |
18 $previous_monthnum = $monthnum; |
27 $previous_monthnum = $monthnum; |
19 $previous_year = $year; |
28 $previous_year = $year; |
20 |
29 |
21 if ( isset( $attributes['month'] ) && isset( $attributes['year'] ) ) { |
30 if ( isset( $attributes['month'] ) && isset( $attributes['year'] ) ) { |
57 ) |
66 ) |
58 ); |
67 ); |
59 } |
68 } |
60 |
69 |
61 add_action( 'init', 'register_block_core_calendar' ); |
70 add_action( 'init', 'register_block_core_calendar' ); |
|
71 |
|
72 /** |
|
73 * Returns whether or not there are any published posts. |
|
74 * |
|
75 * Used to hide the calendar block when there are no published posts. |
|
76 * This compensates for a known Core bug: https://core.trac.wordpress.org/ticket/12016 |
|
77 * |
|
78 * @return bool Has any published posts or not. |
|
79 */ |
|
80 function block_core_calendar_has_published_posts() { |
|
81 // Multisite already has an option that stores the count of the published posts. |
|
82 // Let's use that for multisites. |
|
83 if ( is_multisite() ) { |
|
84 return 0 < (int) get_option( 'post_count' ); |
|
85 } |
|
86 |
|
87 // On single sites we try our own cached option first. |
|
88 $has_published_posts = get_option( 'wp_calendar_block_has_published_posts', null ); |
|
89 if ( null !== $has_published_posts ) { |
|
90 return (bool) $has_published_posts; |
|
91 } |
|
92 |
|
93 // No cache hit, let's update the cache and return the cached value. |
|
94 return block_core_calendar_update_has_published_posts(); |
|
95 } |
|
96 |
|
97 /** |
|
98 * Queries the database for any published post and saves |
|
99 * a flag whether any published post exists or not. |
|
100 * |
|
101 * @return bool Has any published posts or not. |
|
102 */ |
|
103 function block_core_calendar_update_has_published_posts() { |
|
104 global $wpdb; |
|
105 $has_published_posts = (bool) $wpdb->get_var( "SELECT 1 as test FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1" ); |
|
106 update_option( 'wp_calendar_block_has_published_posts', $has_published_posts ); |
|
107 return $has_published_posts; |
|
108 } |
|
109 |
|
110 // We only want to register these functions and actions when |
|
111 // we are on single sites. On multi sites we use `post_count` option. |
|
112 if ( ! is_multisite() ) { |
|
113 /** |
|
114 * Handler for updating the has published posts flag when a post is deleted. |
|
115 * |
|
116 * @param int $post_id Deleted post ID. |
|
117 */ |
|
118 function block_core_calendar_update_has_published_post_on_delete( $post_id ) { |
|
119 $post = get_post( $post_id ); |
|
120 |
|
121 if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) { |
|
122 return; |
|
123 } |
|
124 |
|
125 block_core_calendar_update_has_published_posts(); |
|
126 } |
|
127 |
|
128 /** |
|
129 * Handler for updating the has published posts flag when a post status changes. |
|
130 * |
|
131 * @param string $new_status The status the post is changing to. |
|
132 * @param string $old_status The status the post is changing from. |
|
133 * @param WP_Post $post Post object. |
|
134 */ |
|
135 function block_core_calendar_update_has_published_post_on_transition_post_status( $new_status, $old_status, $post ) { |
|
136 if ( $new_status === $old_status ) { |
|
137 return; |
|
138 } |
|
139 |
|
140 if ( 'post' !== get_post_type( $post ) ) { |
|
141 return; |
|
142 } |
|
143 |
|
144 if ( 'publish' !== $new_status && 'publish' !== $old_status ) { |
|
145 return; |
|
146 } |
|
147 |
|
148 block_core_calendar_update_has_published_posts(); |
|
149 } |
|
150 |
|
151 add_action( 'delete_post', 'block_core_calendar_update_has_published_post_on_delete' ); |
|
152 add_action( 'transition_post_status', 'block_core_calendar_update_has_published_post_on_transition_post_status', 10, 3 ); |
|
153 } |