diff --git a/includes/sp-event-permalink.php b/includes/sp-event-permalink.php index c88a5f8..48fbbcc 100644 --- a/includes/sp-event-permalink.php +++ b/includes/sp-event-permalink.php @@ -75,15 +75,27 @@ function custom_event_rewrite_flush() { register_activation_hook(__FILE__, 'custom_event_rewrite_flush'); register_deactivation_hook(__FILE__, 'flush_rewrite_rules'); -// Modify the query to handle custom permalinks and include future posts -function custom_event_parse_request($query) { - $post_type = sp_array_value( $query->query, 'post_type', null ); - if (isset($query->query_vars['post_type']) && $query->query_vars['post_type'] === 'sp_event') { - if (isset($query->query_vars['p'])) { - $query->set('post_type', 'sp_event'); - $query->set('p', $query->query_vars['p']); - $query->set('post_status', array('publish', 'future')); - } - } +// Modify the front-end single event query to allow scheduled events to resolve. +function custom_event_parse_request( $query ) { + if ( ! $query instanceof WP_Query ) { + return; + } + + if ( is_admin() || ! $query->is_main_query() ) { + return; + } + + if ( 'sp_event' !== $query->get( 'post_type' ) ) { + return; + } + + $post_id = absint( $query->get( 'p' ) ); + if ( $post_id <= 0 ) { + return; + } + + $query->set( 'post_type', 'sp_event' ); + $query->set( 'p', $post_id ); + $query->set( 'post_status', array( 'publish', 'future' ) ); } add_action('pre_get_posts', 'custom_event_parse_request'); diff --git a/tests/test-sp-event-permalink.php b/tests/test-sp-event-permalink.php new file mode 100644 index 0000000..1e4f042 --- /dev/null +++ b/tests/test-sp-event-permalink.php @@ -0,0 +1,81 @@ +original_wp_query = $GLOBALS['wp_query']; + $this->original_wp_the_query = $GLOBALS['wp_the_query']; + set_current_screen( 'front' ); + } + + /** + * Restore global query references. + */ + public function tear_down(): void { + $GLOBALS['wp_query'] = $this->original_wp_query; + $GLOBALS['wp_the_query'] = $this->original_wp_the_query; + set_current_screen( 'front' ); + parent::tear_down(); + } + + /** + * The admin event list query should not be altered by permalink handling. + */ + public function test_admin_event_queries_are_not_modified() { + set_current_screen( 'edit-sp_event' ); + + $query = new WP_Query(); + $query->set( 'post_type', 'sp_event' ); + $query->set( 'p', 123 ); + $query->set( 'post_status', 'future' ); + + custom_event_parse_request( $query ); + + $this->assertSame( 'future', $query->get( 'post_status' ) ); + $this->assertSame( 123, $query->get( 'p' ) ); + } + + /** + * Front-end single event requests should include future posts. + */ + public function test_frontend_single_event_queries_include_future_posts() { + $query = new WP_Query(); + $query->set( 'post_type', 'sp_event' ); + $query->set( 'p', 456 ); + + $GLOBALS['wp_query'] = $query; + $GLOBALS['wp_the_query'] = $query; + + custom_event_parse_request( $query ); + + $this->assertSame( 'sp_event', $query->get( 'post_type' ) ); + $this->assertSame( 456, $query->get( 'p' ) ); + $this->assertSame( array( 'publish', 'future' ), $query->get( 'post_status' ) ); + } +}