Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $post_type = $attributes['postType'];
- $parent_post_id = isset( $attributes['postParent'] ) ? [ $attributes['postParent'] ] : [];
- $events_query = new WP_Query(
- [
- 'post_type' => $post_type,
- 'posts_per_page' => -1,
- 'orderby' => 'meta_value',
- 'post_parent__in' => $parent_post_id,
- 'meta_key' => $post_type . '_date',
- 'order' => 'ASC',
- ]
- );
- if ( ! $events_query->have_posts() ) {
- return;
- }
- $events = [];
- while ( $events_query->have_posts() ) {
- $events_query->the_post();
- $event_start_date = date( 'Y-m-d', strtotime( get_post_meta( get_the_ID(), $post_type . '_date', true ) ) );
- $event_end_date = date( 'Y-m-d H:i:s', strtotime( get_post_meta( get_the_ID(), $post_type . '_end_date', true ) . ' 23:59:59' ) );
- $recurrence_type = get_post_meta( get_the_ID(), $post_type . '_recurrence', true );
- $recurrence_end_date = get_post_meta( get_the_ID(), $post_type . '_recurrence_end', true );
- $custom_dates = get_post_meta( get_the_ID(), $post_type . '_custom_dates', true );
- $recurrence_end_timestamp = ! empty( $recurrence_end_date ) ? strtotime( $recurrence_end_date . ' 23:59:59' ) : null;
- $last_date_of_recurrance = ! empty( $recurrence_end_date ) ? date( 'Y-m-d', strtotime( $recurrence_end_date ) ) : '';
- switch ( $recurrence_type ) {
- case 'weekly':
- $i = 0;
- do {
- $next_start_date = date( 'Y-m-d', strtotime( $event_start_date . ' + ' . ( 7 * $i ) . ' days' ) );
- $next_end_date = date( 'Y-m-d H:i:s', strtotime( $event_end_date . ' + ' . ( 7 * $i ) . ' days' ) );
- if (
- strtotime( $next_end_date ) >= strtotime( 'today' ) &&
- ( ! $recurrence_end_timestamp || strtotime( $next_start_date ) <= $recurrence_end_timestamp )
- ) {
- $events[] = [
- 'title' => get_the_title(),
- 'url' => get_the_permalink(),
- 'start' => $next_start_date,
- 'end' => $next_end_date,
- ];
- }
- $i++;
- } while (
- ( $recurrence_end_timestamp && strtotime( $next_start_date ) <= $recurrence_end_timestamp ) ||
- ( ! $recurrence_end_timestamp && $i < 10 )
- );
- break;
- case 'monthly':
- $i = 0;
- do {
- $next_start_date = date( 'Y-m-d', strtotime( $event_start_date . ' + ' . $i . ' month' ) );
- $next_end_date = date( 'Y-m-d', strtotime( $event_end_date . ' + ' . $i . ' month' ) );
- if ( strtotime( $next_start_date ) >= strtotime( 'today' ) &&
- ( ! $recurrence_end_timestamp || strtotime( $next_start_date ) <= $recurrence_end_timestamp ) ) {
- $events[] = [
- 'title' => get_the_title(),
- 'url' => get_the_permalink(),
- 'start' => $next_start_date,
- 'end' => $next_end_date,
- ];
- }
- $i++;
- } while (
- ( $recurrence_end_timestamp && strtotime( $next_start_date ) <= $recurrence_end_timestamp ) ||
- ( ! $recurrence_end_timestamp && $i < 10 )
- );
- break;
- case 'custom':
- if ( ! empty( $custom_dates ) ) {
- foreach ( $custom_dates as $custom_date ) {
- $start = isset( $custom_date['start'] ) ? date( 'Y-m-d', strtotime( $custom_date['start'] ) ) : '';
- $end = isset( $custom_date['end'] ) ? date( 'Y-m-d', strtotime( $custom_date['end'] . ' +1 day' ) ) : '';
- if ( $start && ( empty( $recurrence_end_timestamp ) || strtotime( $start ) <= $recurrence_end_timestamp ) ) {
- if ( strtotime( $end ) >= strtotime( 'today' ) ) {
- $events[] = [
- 'title' => get_the_title(),
- 'url' => get_the_permalink(),
- 'start' => $start,
- 'end' => $end,
- ];
- }
- }
- }
- }
- break;
- default:
- if ( strtotime( $event_end_date ) >= strtotime( 'today' ) ) {
- $events[] = [
- 'title' => get_the_title(),
- 'url' => get_the_permalink(),
- 'start' => $event_start_date,
- 'end' => $event_end_date,
- ];
- }
- break;
- }
- }
- wp_reset_postdata();
- ?>
- <div <?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>>
- <div id="event-calendar" data-events="<?php echo esc_attr( json_encode( $events ) ); ?>"></div>
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement