Skip to content

Instantly share code, notes, and snippets.

@kaskad88
Created May 3, 2023 13:19
Show Gist options
  • Save kaskad88/69a2661933e6cf5169d57e13a250a4e9 to your computer and use it in GitHub Desktop.
Save kaskad88/69a2661933e6cf5169d57e13a250a4e9 to your computer and use it in GitHub Desktop.
add_action( 'jet-engine/listing/calendar/before', function ( $settings, $render ) {
$calendar_id = 'custom_calendar';
if ( empty( $settings['_element_id'] ) || $calendar_id !== $settings['_element_id'] ) {
return;
}
$posts_week_day = array();
$date_key = $settings['group_by_key'];
$current_month = $render->get_current_month();
$human_current_month = date( 'F Y', $current_month );
$human_prev_month = date( 'F Y', strtotime( $human_current_month . ' - 1 month' ) );
$human_next_month = date( 'F Y', strtotime( $human_current_month . ' + 1 month' ) );
if ( ! empty( $render->prev_month_posts ) ) {
foreach ( $render->prev_month_posts as $day_num => $posts ) {
foreach ( $posts as $ind => $post ) {
if ( ! isset( $posts_week_day[ $post->ID ] ) ) {
$date = get_post_meta( $post->ID, $date_key, true );
$posts_week_day[ $post->ID ] = date( 'w', $date );
}
$cal_week_day = date( 'w', strtotime( $day_num . ' ' . $human_prev_month ) );
if ( $cal_week_day !== $posts_week_day[ $post->ID ] ) {
unset( $render->prev_month_posts[ $day_num ][ $ind ] );
}
}
}
}
if ( ! empty( $render->multiday_events ) ) {
foreach ( $render->multiday_events as $day_num => $posts ) {
foreach ( $posts as $ind => $post ) {
if ( ! isset( $posts_week_day[ $post->ID ] ) ) {
$date = get_post_meta( $post->ID, $date_key, true );
$posts_week_day[ $post->ID ] = date( 'w', $date );
}
$cal_week_day = date( 'w', strtotime( $day_num . ' ' . $human_current_month ) );
if ( $cal_week_day !== $posts_week_day[ $post->ID ] ) {
unset( $render->multiday_events[ $day_num ][ $ind ] );
}
}
}
}
if ( ! empty( $render->next_month_posts ) ) {
foreach ( $render->next_month_posts as $day_num => $posts ) {
foreach ( $posts as $ind => $post ) {
if ( ! isset( $posts_week_day[ $post->ID ] ) ) {
$date = get_post_meta( $post->ID, $date_key, true );
$posts_week_day[ $post->ID ] = date( 'w', $date );
}
$cal_week_day = date( 'w', strtotime( $day_num . ' ' . $human_next_month ) );
if ( $cal_week_day !== $posts_week_day[ $post->ID ] ) {
unset( $render->next_month_posts[ $day_num ][ $ind ] );
}
}
}
}
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment