array( 'min_range' => 1, 'max_range' => 12, ), ) ); // Back-compat for legacy `month` query arg if present. if ( false === $month_input || null === $month_input ) { $month_input = filter_input( INPUT_GET, 'month', FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 1, 'max_range' => 12, ), ) ); } $year_input = filter_input( INPUT_GET, $year_param, FILTER_VALIDATE_INT, array( 'options' => array( // Choose a sane range for years. 'min_range' => 1970, 'max_range' => 2200, ), ) ); // Back-compat for legacy `year` query arg if present. if ( false === $year_input || null === $year_input ) { $year_input = filter_input( INPUT_GET, 'year', FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 1970, 'max_range' => 2200, ), ) ); } $curMonth = ( false !== $month_input && null !== $month_input ) ? (int) $month_input : (int) wp_date( 'n' ); $curYear = ( false !== $year_input && null !== $year_input ) ? (int) $year_input : (int) wp_date( 'Y' ); // Get previous and next month and year. $prev_month = $curMonth - 1; $prev_year = $curYear; if ( $prev_month < 1 ) { $prev_month = 12; --$prev_year; } $next_month = $curMonth + 1; $next_year = $curYear; if ( $next_month > 12 ) { $next_month = 1; ++$next_year; } // Get the number of days in the month. $days_in_month = cal_days_in_month( CAL_GREGORIAN, $curMonth, $curYear ); // Get the first day of the month in site timezone. $tz = wp_timezone(); $first_ts = ( new DateTimeImmutable( sprintf( '%04d-%02d-01 00:00:00', $curYear, $curMonth ), $tz ) )->getTimestamp(); // Respect the site's chosen week start (0 = Sunday, 6 = Saturday). $start_of_week = (int) get_option( 'start_of_week', 1 ); if ( $start_of_week < 0 || $start_of_week > 6 ) { $start_of_week = 0; } // Calculate how many empty cells to prepend before day 1 based on the start day. $first_weekday_of_month = (int) wp_date( 'w', $first_ts ); // 0 (Sun) - 6 (Sat). $leading_empty_cells = ( $first_weekday_of_month - $start_of_week + 7 ) % 7; // Build weekday labels in the configured order. $weekdays = array( array( 'abbr' => __( 'Sun', 'aa-events' ), 'label' => __( 'Sunday', 'aa-events' ), ), array( 'abbr' => __( 'Mon', 'aa-events' ), 'label' => __( 'Monday', 'aa-events' ), ), array( 'abbr' => __( 'Tue', 'aa-events' ), 'label' => __( 'Tuesday', 'aa-events' ), ), array( 'abbr' => __( 'Wed', 'aa-events' ), 'label' => __( 'Wednesday', 'aa-events' ), ), array( 'abbr' => __( 'Thu', 'aa-events' ), 'label' => __( 'Thursday', 'aa-events' ), ), array( 'abbr' => __( 'Fri', 'aa-events' ), 'label' => __( 'Friday', 'aa-events' ), ), array( 'abbr' => __( 'Sat', 'aa-events' ), 'label' => __( 'Saturday', 'aa-events' ), ), ); $ordered_weekdays = array_merge( array_slice( $weekdays, $start_of_week ), array_slice( $weekdays, 0, $start_of_week ) ); // Get events for the current month. $events = new WP_Query( array( 'post_type' => 'event', 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => 'event_datetime', 'value' => array( "$curYear-$curMonth-01 00:00:00", "$curYear-$curMonth-$days_in_month 23:59:59" ), 'compare' => 'BETWEEN', 'type' => 'DATETIME', ), ), ) ); $events_by_day = array(); if ( $events->have_posts() ) { while ( $events->have_posts() ) { $events->the_post(); $event_date = get_field( 'event_datetime' ); $event_ts = ( new DateTimeImmutable( $event_date, $tz ) )->getTimestamp(); $day = (int) wp_date( 'j', $event_ts ); $events_by_day[ $day ][] = '' . get_the_title() . ''; } wp_reset_postdata(); } // Get today's date. $todayDay = intval( wp_date( 'j' ) ); $todayMonth = intval( wp_date( 'n' ) ); $todayYear = intval( wp_date( 'Y' ) ); // Helper to check if today is in current view. $isTodayInCurrentMonth = ( $curMonth === $todayMonth && $curYear === $todayYear ); ?> '; echo '
' . esc_html( $day ) . '
'; if ( isset( $eventsByDay[ $day ] ) ) { echo ''; } echo ''; }; // Overwrite the default day cell output in the table loop. ob_start(); for ( $day = 1; $day <= $days_in_month; $day++ ) { if ( ( ( $day + $leading_empty_cells - 1 ) % 7 ) === 0 && $day > 1 ) { echo ''; } $originalEchoDayCell( $day, $leading_empty_cells, $events_by_day ); } $calendarRows = ob_get_clean(); // Replace the original loop output with the new one. $calendarOutput = preg_replace( '/for\s*\(\s*\$day\s*=\s*1;\s*\$day\s*<=\s*\$days_in_month;\s*\$day\+\+\s*\)\s*\{.*?\}/s', $calendarRows, ob_get_contents() ); ?>
getTimestamp(); $next_ts = ( new DateTimeImmutable( sprintf( '%04d-%02d-01 00:00:00', $next_year, $next_month ), $tz ) )->getTimestamp(); $prevMonthName = wp_date( 'F', $prev_ts ); $nextMonthName = wp_date( 'F', $next_ts ); ?> " >«

" > »
'; } // Loop through the days of the month. for ( $day = 1; $day <= $days_in_month; $day++ ) { if ( ( ( $day + $leading_empty_cells - 1 ) % 7 ) === 0 && $day > 1 ) { echo ''; } $cellClass = ''; if ( $isTodayInCurrentMonth && $day === $todayDay ) { $cellClass = 'today-cell'; } $cell_ts = ( new DateTimeImmutable( sprintf( '%04d-%02d-%02d 00:00:00', $curYear, $curMonth, $day ), $tz ) )->getTimestamp(); echo ''; } // Add empty cells for the last week. $remaining_days = ( 7 - ( ( $days_in_month + $leading_empty_cells ) % 7 ) ) % 7; if ( $remaining_days < 7 ) { for ( $i = 0; $i < $remaining_days; $i++ ) { echo ''; } } ?>
'; echo '
' . esc_html( $day ) . '
'; if ( isset( $events_by_day[ $day ] ) ) { echo '
    '; foreach ( $events_by_day[ $day ] as $event ) { echo '
  • ' . wp_kses_post( $event ) . '
  • '; } echo '
'; } echo '