guard( 'etn_manage_event' ); if ( $check ) { return $check; } $status = ( null === $status || '' === $status ) ? 'all' : $status; if ( ! in_array( $status, self::ALLOWED_STATUSES, true ) ) { return [ 'error' => "Invalid status '{$status}'. Accepted values: " . implode( ', ', self::ALLOWED_STATUSES ) . '.' ]; } try { $limit = min( (int) ( $limit ?? 20 ), 50 ); $limit = max( 1, $limit ); $today = current_time( 'Y-m-d' ); $query_args = [ 'post_type' => 'etn', 'posts_per_page' => $limit, 'post_status' => 'any', 'orderby' => 'meta_value', 'meta_key' => 'etn_start_date', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_query 'order' => 'ASC', ]; switch ( $status ) { case 'draft': $query_args['post_status'] = 'draft'; // Avoid the meta_value orderby INNER JOIN, which drops metaless drafts. $query_args['orderby'] = 'date'; unset( $query_args['meta_key'] ); break; case 'upcoming': $query_args['post_status'] = 'publish'; $query_args['meta_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_query [ 'key' => 'etn_start_date', 'value' => $today, 'compare' => '>=', 'type' => 'DATE', ], ]; break; case 'past': $query_args['post_status'] = 'publish'; $query_args['meta_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_query [ 'key' => 'etn_end_date', 'value' => $today, 'compare' => '<', 'type' => 'DATE', ], ]; break; case 'ongoing': $query_args['post_status'] = 'publish'; $query_args['meta_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_query 'relation' => 'AND', [ 'key' => 'etn_start_date', 'value' => $today, 'compare' => '<=', 'type' => 'DATE', ], [ 'key' => 'etn_end_date', 'value' => $today, 'compare' => '>=', 'type' => 'DATE', ], ]; break; default: // Avoid the meta_value orderby INNER JOIN, which drops metaless events. $query_args['orderby'] = 'date'; unset( $query_args['meta_key'] ); break; } if ( ! empty( $search ) ) { $query_args['s'] = sanitize_text_field( $search ); } $query = new \WP_Query( $query_args ); $events = []; if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); $post_id = get_the_ID(); $location_data = get_post_meta( $post_id, 'etn_event_location', true ); $location = ''; if ( is_array( $location_data ) && ! empty( $location_data['address'] ) ) { $location = $location_data['address']; } elseif ( is_string( $location_data ) ) { $location = $location_data; } $category_terms = get_the_terms( $post_id, 'etn_category' ); $categories = []; if ( $category_terms && ! is_wp_error( $category_terms ) ) { foreach ( $category_terms as $term ) { $categories[] = $term->name; } } $tag_terms = get_the_terms( $post_id, 'etn_tags' ); $tags = []; if ( $tag_terms && ! is_wp_error( $tag_terms ) ) { foreach ( $tag_terms as $term ) { $tags[] = $term->name; } } $events[] = [ 'id' => $post_id, 'title' => get_the_title(), 'post_status' => get_post_status( $post_id ), 'start_date' => get_post_meta( $post_id, 'etn_start_date', true ), 'end_date' => get_post_meta( $post_id, 'etn_end_date', true ), 'start_time' => get_post_meta( $post_id, 'etn_start_time', true ), 'end_time' => get_post_meta( $post_id, 'etn_end_time', true ), 'timezone' => get_post_meta( $post_id, 'event_timezone', true ), 'event_type' => get_post_meta( $post_id, 'event_type', true ), 'location' => $location, 'categories' => $categories, 'tags' => $tags, 'edit_url' => get_edit_post_link( $post_id, 'raw' ), ]; } } wp_reset_postdata(); // "total" counts only the rows shown. "total_matching" is the real count. // Why: the model said "you have 50 events" on a site with more than 2,000. $matching = (int) ( $query->found_posts ?? count( $events ) ); $result = [ 'events' => $events, 'total' => count( $events ), 'total_matching' => $matching, ]; if ( $matching > count( $events ) ) { $result['note'] = sprintf( 'Only the first %d of %d matching events are listed. For counts use eventin-count-events.', count( $events ), $matching ); } return $result; } catch ( \Exception $e ) { Log::error( 'Eventin_List_Events_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Failed to retrieve events: ' . $e->getMessage() ]; } } }