PluginProbe
Gutenberg / 16.6.0
Gutenberg v16.6.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / build / block-library / blocks / calendar.php

calendar.php in Gutenberg 16.6.0, at build/block-library/blocks/calendar.php

177 lines 6.2 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 `core/calendar` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Renders the `core/calendar` block on server.
10 *
11 * @param array $attributes The block attributes.
12 *
13 * @return string Returns the block content.
14 */
15 function gutenberg_render_block_core_calendar( $attributes ) {
16 global $monthnum, $year;
17
18 // Calendar shouldn't be rendered
19 // when there are no published posts on the site.
20 if ( ! gutenberg_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 }
26
27 $previous_monthnum = $monthnum;
28 $previous_year = $year;
29
30 if ( isset( $attributes['month'] ) && isset( $attributes['year'] ) ) {
31 $permalink_structure = get_option( 'permalink_structure' );
32 if (
33 str_contains( $permalink_structure, '%monthnum%' ) &&
34 str_contains( $permalink_structure, '%year%' )
35 ) {
36 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
37 $monthnum = $attributes['month'];
38 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
39 $year = $attributes['year'];
40 }
41 }
42
43 $color_block_styles = array();
44
45 // Text color.
46 $preset_text_color = array_key_exists( 'textColor', $attributes ) ? "var:preset|color|{$attributes['textColor']}" : null;
47 $custom_text_color = _wp_array_get( $attributes, array( 'style', 'color', 'text' ), null );
48 $color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color;
49
50 // Background Color.
51 $preset_background_color = array_key_exists( 'backgroundColor', $attributes ) ? "var:preset|color|{$attributes['backgroundColor']}" : null;
52 $custom_background_color = _wp_array_get( $attributes, array( 'style', 'color', 'background' ), null );
53 $color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color;
54
55 // Generate color styles and classes.
56 $styles = gutenberg_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) );
57 $inline_styles = empty( $styles['css'] ) ? '' : sprintf( ' style="%s"', esc_attr( $styles['css'] ) );
58 $classnames = empty( $styles['classnames'] ) ? '' : ' ' . esc_attr( $styles['classnames'] );
59 if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
60 $classnames .= ' has-link-color';
61 }
62 // Apply color classes and styles to the calendar.
63 $calendar = str_replace( '<table', '<table' . $inline_styles, get_calendar( true, false ) );
64 $calendar = str_replace( 'class="wp-calendar-table', 'class="wp-calendar-table' . $classnames, $calendar );
65
66 $wrapper_attributes = get_block_wrapper_attributes();
67 $output = sprintf(
68 '<div %1$s>%2$s</div>',
69 $wrapper_attributes,
70 $calendar
71 );
72
73 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
74 $monthnum = $previous_monthnum;
75 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
76 $year = $previous_year;
77
78 return $output;
79 }
80
81 /**
82 * Registers the `core/calendar` block on server.
83 */
84 function gutenberg_register_block_core_calendar() {
85 register_block_type_from_metadata(
86 __DIR__ . '/calendar',
87 array(
88 'render_callback' => 'gutenberg_render_block_core_calendar',
89 )
90 );
91 }
92
93 add_action( 'init', 'gutenberg_register_block_core_calendar', 20 );
94
95 /**
96 * Returns whether or not there are any published posts.
97 *
98 * Used to hide the calendar block when there are no published posts.
99 * This compensates for a known Core bug: https://core.trac.wordpress.org/ticket/12016
100 *
101 * @return bool Has any published posts or not.
102 */
103 function gutenberg_block_core_calendar_has_published_posts() {
104 // Multisite already has an option that stores the count of the published posts.
105 // Let's use that for multisites.
106 if ( is_multisite() ) {
107 return 0 < (int) get_option( 'post_count' );
108 }
109
110 // On single sites we try our own cached option first.
111 $has_published_posts = get_option( 'wp_calendar_block_has_published_posts', null );
112 if ( null !== $has_published_posts ) {
113 return (bool) $has_published_posts;
114 }
115
116 // No cache hit, let's update the cache and return the cached value.
117 return gutenberg_block_core_calendar_update_has_published_posts();
118 }
119
120 /**
121 * Queries the database for any published post and saves
122 * a flag whether any published post exists or not.
123 *
124 * @return bool Has any published posts or not.
125 */
126 function gutenberg_block_core_calendar_update_has_published_posts() {
127 global $wpdb;
128 $has_published_posts = (bool) $wpdb->get_var( "SELECT 1 as test FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1" );
129 update_option( 'wp_calendar_block_has_published_posts', $has_published_posts );
130 return $has_published_posts;
131 }
132
133 // We only want to register these functions and actions when
134 // we are on single sites. On multi sites we use `post_count` option.
135 if ( ! is_multisite() ) {
136 /**
137 * Handler for updating the has published posts flag when a post is deleted.
138 *
139 * @param int $post_id Deleted post ID.
140 */
141 function gutenberg_block_core_calendar_update_has_published_post_on_delete( $post_id ) {
142 $post = get_post( $post_id );
143
144 if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) {
145 return;
146 }
147
148 gutenberg_block_core_calendar_update_has_published_posts();
149 }
150
151 /**
152 * Handler for updating the has published posts flag when a post status changes.
153 *
154 * @param string $new_status The status the post is changing to.
155 * @param string $old_status The status the post is changing from.
156 * @param WP_Post $post Post object.
157 */
158 function gutenberg_block_core_calendar_update_has_published_post_on_transition_post_status( $new_status, $old_status, $post ) {
159 if ( $new_status === $old_status ) {
160 return;
161 }
162
163 if ( 'post' !== get_post_type( $post ) ) {
164 return;
165 }
166
167 if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
168 return;
169 }
170
171 gutenberg_block_core_calendar_update_has_published_posts();
172 }
173
174 add_action( 'delete_post', 'gutenberg_block_core_calendar_update_has_published_post_on_delete' );
175 add_action( 'transition_post_status', 'gutenberg_block_core_calendar_update_has_published_post_on_transition_post_status', 10, 3 );
176 }
177