Last active
October 5, 2016 22:28
-
-
Save barryhughes/7eacaf61ff26ecadf1dec98f7aff9bfb to your computer and use it in GitHub Desktop.
Stub/example of reliably detecting if month view is empty, even if filtered via Filter Bar, early enough to perform a redirect (TEC 4.2.x/4.3)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Stub: take action if month view contains no events. | |
| * | |
| * Currently (in TEC 4.2/4.3) the month view template object does not | |
| * reliably determine if there are any events available until late in the request | |
| * (and if a Filter Bar filter is applied, it will be too late to perform a | |
| * redirect). | |
| * | |
| * This function works around this to detect if month view is empty nice and | |
| * early; this regrettably involves some undesirable hacks at present however | |
| * the overhead of this approach should be fairly neglible: it simply changes when | |
| * some queries run. | |
| */ | |
| add_action( 'template_redirect', function() { | |
| global $wp_query, | |
| $wp_filter; | |
| $month_view = false; | |
| // We're only interested in month view | |
| if ( 'month' !== $wp_query->get( 'eventDisplay' ) ) { | |
| return; | |
| } | |
| // Template objects are anonymous: if nothing is set up on the following hook we'll be | |
| // unable to obtain a reference to the current template object | |
| if ( ! isset( $wp_filter['tribe_events_before_view'] ) ) { | |
| return; | |
| } | |
| // Obtain a reference to the month view template class | |
| foreach ( $wp_filter['tribe_events_before_view'] as $priority => $callbacks ) { | |
| foreach ( $callbacks as $single_callback ) { | |
| if ( ! is_array( $single_callback['function'] ) ) { | |
| continue; | |
| } | |
| if ( is_a( current( $single_callback['function'] ), 'Tribe__Events__Template__Month' ) ) { | |
| $month_view = current( $single_callback['function'] ); | |
| break; | |
| } | |
| } | |
| } | |
| // Bail if we were unable to extract the reference to the month view class | |
| if ( ! $month_view ) { | |
| return; | |
| } | |
| // Run setup_view early and stop it from running a second time later in the request | |
| $month_view->setup_view(); | |
| remove_action( 'tribe_events_before_view', array( $month_view, 'setup_view' ) ); | |
| // Count the true number of events per day | |
| while ( Tribe__Events__Template__Month::have_days() ) { | |
| Tribe__Events__Template__Month::the_day(); | |
| $days_events = Tribe__Events__Template__Month::get_current_day(); | |
| // If non-zero/non-empty then we have at least one event, no need to do anything | |
| if ( $days_events['total_events'] ) { | |
| return; | |
| } | |
| } | |
| /** | |
| * @todo DO SOMETHING HERE! | |
| * | |
| * If execution reaches this point, we know that the (possibly filtered) month view does | |
| * not contain any events; so we can redirect to another page/view or take some other | |
| * action. | |
| */ | |
| return; | |
| }, 50 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment