Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save barryhughes/65da2ae9ffcc99b88c42773a3248af16 to your computer and use it in GitHub Desktop.
Save barryhughes/65da2ae9ffcc99b88c42773a3248af16 to your computer and use it in GitHub Desktop.
Improve month view search so it is not restricted to the current month. Experimental! Use at own risk, there are known bugs/quirks with this code.
<?php
/**
* If a search is conducted in month view but the relevant events
* are in a different month, switch to that month.
*
* Experimental! Especially during live ajax navigation, there are
* parts of this that don't work cleanly (pushstate/URL changes).
* Unsupported, use at own risk.
*/
class Transmogrified_Month_View_Search {
private $month = '';
private $search = '';
private $target = '';
public function __construct() {
$this->month = empty( $_REQUEST['tribe-bar-date'] )
? date_create()->format( 'Y-m' )
: $_REQUEST['tribe-bar-date'];
$this->search = empty( $_REQUEST['tribe-bar-search'] )
? ''
: $_REQUEST['tribe-bar-search'];
add_action( 'admin_init', [ $this, 'listen_for_ajax_loads' ], 1 );
add_action( 'parse_query', [ $this, 'listen_for_direct_loads' ] );
}
public function listen_for_ajax_loads() {
if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
return;
}
if ( 'tribe_calendar' !== @$_POST['action'] ) {
return;
}
remove_action( 'parse_query', [ $this, 'listen_for_direct_loads' ] );
add_filter( 'tribe_events_header_attributes', [ $this, 'month_view_header_attrs' ] );
$this->assess();
}
public function listen_for_direct_loads( WP_Query $query ) {
remove_action( 'parse_query', [ $this, 'listen' ] );
if ( ! empty( $query->get( 'eventDate' ) ) ) {
$this->month = $query->get( 'eventDate' );
}
if (
! empty( $this->search )
&& 'month' === $query->get( 'eventDisplay' )
) {
$this->assess();
}
}
private function assess() {
$events = tribe_get_events( [
'eventDisplay' => 'custom',
'start_date' => $this->month,
's' => $this->search,
'posts_per_page' => 1
] );
if ( empty( $events ) ) {
return;
}
$first_event = current( $events );
$first_event_month = date_create( $first_event->EventStartDate )->format( 'Y-m' );
if ( $first_event_month !== $this->month ) {
$this->target = $first_event_month;
$this->switch_month();
}
}
private function switch_month() {
defined( 'DOING_AJAX' ) && DOING_AJAX
? $this->switch_for_ajax()
: $this->switch_for_direct_loads();
}
private function switch_for_ajax() {
# Naughty but functional trick
foreach ( ['eventDate', 'tribe-bar-date'] as $key ) {
$_POST[ $key ] = $_REQUEST[ $key ] = $this->target;
}
}
private function switch_for_direct_loads() {
wp_redirect( $this->get_target_url() );
exit;
}
public function month_view_header_attrs( $attrs ) {
$attrs['data-baseurl'] = $this->get_target_url();
return $attrs;
}
private function get_target_url() {
$url = Tribe__Events__Main::instance()->getLink( 'month', $this->target );
return add_query_arg( 'tribe-bar-search', $this->search, $url );
}
}
new Transmogrified_Month_View_Search;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment