diff --git a/README.md b/README.md index 9ab0004..9f9ecb2 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,10 @@ Copy the `team51-focal-point` folder to your `mu-plugins` directory. ## Changelog +### 2.1.3 + +- **Calendar grid alignment:** fixed month grid date placement when WordPress **Week Starts On** is set to Sunday (or any `start_of_week` other than Monday). Dates and events now appear under the correct weekday column. + ### 2.1.2 - **Calendar loading UX:** added a loading skeleton while calendar month requests are in flight. diff --git a/package-lock.json b/package-lock.json index f97514f..769dbe1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "wpcomsp-simple-events", - "version": "2.1.1", + "version": "2.1.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "wpcomsp-simple-events", - "version": "2.1.1", + "version": "2.1.3", "license": "GPL-2.0-or-later", "dependencies": { "ajv": "^8.17.1" diff --git a/package.json b/package.json index 09e4e96..772bd0a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wpcomsp-simple-events", - "version": "2.1.2", + "version": "2.1.3", "description": "A simple Gutenberg-first event management plugin that integrates with WooCommerce Box Office.", "author": { "name": "WordPress.com Special Projects Team", diff --git a/plugin.php b/plugin.php index 62373c5..bb70533 100644 --- a/plugin.php +++ b/plugin.php @@ -3,7 +3,7 @@ * Simple Events Plugin bootstrap file. * * @since 1.0.0 - * @version 2.1.2 + * @version 2.1.3 * @author WordPress.com Special Projects * @license GPL-3.0-or-later * @@ -14,7 +14,7 @@ * Description: Event management frontend for WooCommerce Box Office. * Requires at least: 6.5 * Tested up to: 6.9 - * Version: 2.1.2 + * Version: 2.1.3 * Requires PHP: 8.0 * Author: WordPress.com Special Projects * Author URI: https://wpspecialprojects.wordpress.com @@ -32,7 +32,7 @@ function_exists( 'get_plugin_data' ) || require_once ABSPATH . 'wp-admin/includes/plugin.php'; define( 'SE_METADATA', get_plugin_data( __FILE__, false, false ) ); -define( 'SE_VERSION', '2.1.2' ); +define( 'SE_VERSION', '2.1.3' ); define( 'SE_BASENAME', plugin_basename( __FILE__ ) ); define( 'SE_PLUGIN_DIR', untrailingslashit( plugin_dir_path( __FILE__ ) ) ); define( 'SE_PLUGIN_URL', untrailingslashit( plugin_dir_url( __FILE__ ) ) ); diff --git a/src/classes/class-se-calendar.php b/src/classes/class-se-calendar.php index 1bc510b..45b5c65 100644 --- a/src/classes/class-se-calendar.php +++ b/src/classes/class-se-calendar.php @@ -60,7 +60,7 @@ public function register_routes(): void { * @param string $format The display format for the days of the week. * * @return array Days of the week. - **@category Events + * *@category Events */ public function simple_events_get_days_of_week( $format = null ) { global $wp_locale; @@ -442,56 +442,48 @@ private function get_events_by_date( $date ): array { /** - * Get the start day based on the given date and start of the week. + * Get the first calendar cell for the month (weeks align to WordPress start of week). * - * @param mixed $date The date for which to calculate the start day. - * @param integer $start_of_week The start of the week (0-6, where 0 is Sunday). + * @param mixed $date First day of the month to display. + * @param integer $start_of_week Attribute `start_of_week` option: 0 = Sunday through 6 = Saturday. * - * @return DateTime The start day based on the given date and start of the week. + * @return DateTime First grid day at 00:00:00. */ private function get_start_day( $date, $start_of_week ) { - $start_date = clone $date; - $start_day_interval = 0; + $start_date = clone $date; + $start_date->setTime( 0, 0, 0 ); - $start_day_week_position = $date->format( 'w' ); + $w = (int) $start_date->format( 'w' ); + $days_back_to_sow = ( $w - (int) $start_of_week + 7 ) % 7; - if ( $start_of_week > 0 && 0 === intval( $start_day_week_position ) ) { - $start_day_week_position = 7; - } - - if ( 0 !== intval( $start_day_week_position ) ) { - $start_day_interval = abs( 1 - $start_day_week_position ); - } - - return $start_date->sub( new DateInterval( 'P' . $start_day_interval . 'D' ) ); + return 0 === $days_back_to_sow + ? $start_date + : $start_date->sub( new DateInterval( 'P' . $days_back_to_sow . 'D' ) ); } /** - * Get the end day based on the given date and start of the week. + * Get the last day shown in the grid for the month (inclusive), at 23:59:59. * - * @param Date $date The date to calculate the end day from. - * @param integer $start_of_week The start of the `week (0-6, where 0 is Sunday). + * @param mixed $date Any day in the month (used to find last day of month). + * @param integer $start_of_week Attribute `start_of_week` option: 0 = Sunday through 6 = Saturday. * - * @return DateTime The end day based on the given date and start of the week. + * @return DateTime End boundary for the period. */ private function get_end_day( $date, $start_of_week ) { - $end_date = clone $date; - $last_day_interval = 0; + $end_date = clone $date; + $end_date->modify( 'last day of this month' ); + $end_date->setTime( 0, 0, 0 ); - $last_day_of_month = $end_date->modify( 'last day of this month' ); - $last_day_week_position = $last_day_of_month->format( 'w' ); + $w = (int) $end_date->format( 'w' ); + $offset_in_row = ( $w - (int) $start_of_week + 7 ) % 7; + $days_to_add = 6 - $offset_in_row; - if ( $start_of_week > 0 && 0 === intval( $last_day_week_position ) ) { - $last_day_week_position = 7; + if ( $days_to_add > 0 ) { + $end_date->add( new DateInterval( 'P' . $days_to_add . 'D' ) ); } - - if ( 0 !== intval( $last_day_week_position ) ) { - $last_day_interval = 7 - $last_day_week_position; - } - $end_date->setTime( 23, 59, 59 ); - return $end_date->add( new DateInterval( 'P' . $last_day_interval . 'D' ) ); + return $end_date; } }