wp/wp-includes/blocks/calendar.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
--- a/wp/wp-includes/blocks/calendar.php	Thu Sep 29 08:06:27 2022 +0200
+++ b/wp/wp-includes/blocks/calendar.php	Fri Sep 05 18:40:08 2025 +0200
@@ -8,6 +8,11 @@
 /**
  * Renders the `core/calendar` block on server.
  *
+ * @since 5.2.0
+ *
+ * @global int $monthnum.
+ * @global int $year.
+ *
  * @param array $attributes The block attributes.
  *
  * @return string Returns the block content.
@@ -30,33 +35,54 @@
 	if ( isset( $attributes['month'] ) && isset( $attributes['year'] ) ) {
 		$permalink_structure = get_option( 'permalink_structure' );
 		if (
-			strpos( $permalink_structure, '%monthnum%' ) !== false &&
-			strpos( $permalink_structure, '%year%' ) !== false
+			str_contains( $permalink_structure, '%monthnum%' ) &&
+			str_contains( $permalink_structure, '%year%' )
 		) {
-			// phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
 			$monthnum = $attributes['month'];
-			// phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
-			$year = $attributes['year'];
+			$year     = $attributes['year'];
 		}
 	}
 
+	$color_block_styles = array();
+
+	// Text color.
+	$preset_text_color          = array_key_exists( 'textColor', $attributes ) ? "var:preset|color|{$attributes['textColor']}" : null;
+	$custom_text_color          = $attributes['style']['color']['text'] ?? null;
+	$color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color;
+
+	// Background Color.
+	$preset_background_color          = array_key_exists( 'backgroundColor', $attributes ) ? "var:preset|color|{$attributes['backgroundColor']}" : null;
+	$custom_background_color          = $attributes['style']['color']['background'] ?? null;
+	$color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color;
+
+	// Generate color styles and classes.
+	$styles        = wp_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) );
+	$inline_styles = empty( $styles['css'] ) ? '' : sprintf( ' style="%s"', esc_attr( $styles['css'] ) );
+	$classnames    = empty( $styles['classnames'] ) ? '' : ' ' . esc_attr( $styles['classnames'] );
+	if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
+		$classnames .= ' has-link-color';
+	}
+	// Apply color classes and styles to the calendar.
+	$calendar = str_replace( '<table', '<table' . $inline_styles, get_calendar( true, false ) );
+	$calendar = str_replace( 'class="wp-calendar-table', 'class="wp-calendar-table' . $classnames, $calendar );
+
 	$wrapper_attributes = get_block_wrapper_attributes();
 	$output             = sprintf(
 		'<div %1$s>%2$s</div>',
 		$wrapper_attributes,
-		get_calendar( true, false )
+		$calendar
 	);
 
-	// phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
 	$monthnum = $previous_monthnum;
-	// phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
-	$year = $previous_year;
+	$year     = $previous_year;
 
 	return $output;
 }
 
 /**
  * Registers the `core/calendar` block on server.
+ *
+ * @since 5.2.0
  */
 function register_block_core_calendar() {
 	register_block_type_from_metadata(
@@ -75,6 +101,8 @@
  * Used to hide the calendar block when there are no published posts.
  * This compensates for a known Core bug: https://core.trac.wordpress.org/ticket/12016
  *
+ * @since 5.9.0
+ *
  * @return bool Has any published posts or not.
  */
 function block_core_calendar_has_published_posts() {
@@ -98,6 +126,10 @@
  * Queries the database for any published post and saves
  * a flag whether any published post exists or not.
  *
+ * @since 5.9.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
  * @return bool Has any published posts or not.
  */
 function block_core_calendar_update_has_published_posts() {
@@ -113,6 +145,8 @@
 	/**
 	 * Handler for updating the has published posts flag when a post is deleted.
 	 *
+	 * @since 5.9.0
+	 *
 	 * @param int $post_id Deleted post ID.
 	 */
 	function block_core_calendar_update_has_published_post_on_delete( $post_id ) {
@@ -128,6 +162,8 @@
 	/**
 	 * Handler for updating the has published posts flag when a post status changes.
 	 *
+	 * @since 5.9.0
+	 *
 	 * @param string  $new_status The status the post is changing to.
 	 * @param string  $old_status The status the post is changing from.
 	 * @param WP_Post $post       Post object.