Advertisement
True-Flounder7767

render

May 14th, 2025
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.00 KB | Help | 0 0
  1. $post_type = $attributes['postType'];
  2. $parent_post_id = isset( $attributes['postParent'] ) ? [ $attributes['postParent'] ] : [];
  3.  
  4. $events_query = new WP_Query(
  5.     [
  6.         'post_type'      => $post_type,
  7.         'posts_per_page' => -1,
  8.         'orderby'        => 'meta_value',
  9.         'post_parent__in' => $parent_post_id,
  10.         'meta_key'       => $post_type . '_date',
  11.         'order'          => 'ASC',
  12.     ]
  13. );
  14.  
  15. if ( ! $events_query->have_posts() ) {
  16.     return;
  17. }
  18.  
  19. $events = [];
  20.  
  21. while ( $events_query->have_posts() ) {
  22.     $events_query->the_post();
  23.  
  24.     $event_start_date = date( 'Y-m-d', strtotime( get_post_meta( get_the_ID(), $post_type . '_date', true ) ) );
  25.     $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' ) );
  26.  
  27.     $recurrence_type = get_post_meta( get_the_ID(), $post_type . '_recurrence', true );
  28.     $recurrence_end_date = get_post_meta( get_the_ID(), $post_type . '_recurrence_end', true );
  29.  
  30.     $custom_dates = get_post_meta( get_the_ID(), $post_type . '_custom_dates', true );
  31.  
  32.     $recurrence_end_timestamp = ! empty( $recurrence_end_date ) ? strtotime( $recurrence_end_date . ' 23:59:59' ) : null;
  33.     $last_date_of_recurrance = ! empty( $recurrence_end_date ) ? date( 'Y-m-d', strtotime( $recurrence_end_date ) ) : '';
  34.  
  35.     switch ( $recurrence_type ) {
  36.         case 'weekly':
  37.             $i = 0;
  38.             do {
  39.                 $next_start_date = date( 'Y-m-d', strtotime( $event_start_date . ' + ' . ( 7 * $i ) . ' days' ) );
  40.                 $next_end_date = date( 'Y-m-d H:i:s', strtotime( $event_end_date . ' + ' . ( 7 * $i ) . ' days' ) );
  41.  
  42.                 if (
  43.                     strtotime( $next_end_date ) >= strtotime( 'today' ) &&
  44.                     ( ! $recurrence_end_timestamp || strtotime( $next_start_date ) <= $recurrence_end_timestamp )
  45.                 ) {
  46.                     $events[] = [
  47.                         'title' => get_the_title(),
  48.                         'url'   => get_the_permalink(),
  49.                         'start' => $next_start_date,
  50.                         'end'   => $next_end_date,
  51.                     ];
  52.                 }
  53.  
  54.                 $i++;
  55.             } while (
  56.                 ( $recurrence_end_timestamp && strtotime( $next_start_date ) <= $recurrence_end_timestamp ) ||
  57.                 ( ! $recurrence_end_timestamp && $i < 10 )
  58.             );
  59.             break;
  60.  
  61.         case 'monthly':
  62.             $i = 0;
  63.             do {
  64.                 $next_start_date = date( 'Y-m-d', strtotime( $event_start_date . ' + ' . $i . ' month' ) );
  65.                 $next_end_date = date( 'Y-m-d', strtotime( $event_end_date . ' + ' . $i . ' month' ) );
  66.  
  67.                 if ( strtotime( $next_start_date ) >= strtotime( 'today' ) &&
  68.                      ( ! $recurrence_end_timestamp || strtotime( $next_start_date ) <= $recurrence_end_timestamp ) ) {
  69.                     $events[] = [
  70.                         'title' => get_the_title(),
  71.                         'url'   => get_the_permalink(),
  72.                         'start' => $next_start_date,
  73.                         'end'   => $next_end_date,
  74.                     ];
  75.                 }
  76.  
  77.                 $i++;
  78.             } while (
  79.                 ( $recurrence_end_timestamp && strtotime( $next_start_date ) <= $recurrence_end_timestamp ) ||
  80.                 ( ! $recurrence_end_timestamp && $i < 10 )
  81.             );
  82.             break;
  83.  
  84.         case 'custom':
  85.             if ( ! empty( $custom_dates ) ) {
  86.                 foreach ( $custom_dates as $custom_date ) {
  87.                     $start = isset( $custom_date['start'] ) ? date( 'Y-m-d', strtotime( $custom_date['start'] ) ) : '';
  88.                     $end = isset( $custom_date['end'] ) ? date( 'Y-m-d', strtotime( $custom_date['end'] . ' +1 day' ) ) : '';
  89.  
  90.                     if ( $start && ( empty( $recurrence_end_timestamp ) || strtotime( $start ) <= $recurrence_end_timestamp ) ) {
  91.                         if ( strtotime( $end ) >= strtotime( 'today' ) ) {
  92.                             $events[] = [
  93.                                 'title' => get_the_title(),
  94.                                 'url'   => get_the_permalink(),
  95.                                 'start' => $start,
  96.                                 'end'   => $end,
  97.                             ];
  98.                         }
  99.                     }
  100.                 }
  101.             }
  102.             break;
  103.  
  104.         default:
  105.             if ( strtotime( $event_end_date ) >= strtotime( 'today' ) ) {
  106.                 $events[] = [
  107.                     'title' => get_the_title(),
  108.                     'url'   => get_the_permalink(),
  109.                     'start' => $event_start_date,
  110.                     'end'   => $event_end_date,
  111.                 ];
  112.             }
  113.             break;
  114.     }
  115. }
  116. wp_reset_postdata();
  117. ?>
  118.  
  119. <div <?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>>
  120.     <div id="event-calendar" data-events="<?php echo esc_attr( json_encode( $events ) ); ?>"></div>
  121. </div>
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement