Skip to content

Instantly share code, notes, and snippets.

@vicskf
Created October 19, 2017 13:43
Show Gist options
  • Save vicskf/9b05e51cb4d7219f631964e91a5b5f2b to your computer and use it in GitHub Desktop.
Save vicskf/9b05e51cb4d7219f631964e91a5b5f2b to your computer and use it in GitHub Desktop.
Events Calendar PRO > Use today as the start of the week for the This Week widget
<?php
/**
* Use today as the start of the week for the This Week widget.
*
* @param $instance
* @param $widget
*
* @return mixed
* @author Barry Hughes
*
*/
function this_week_dynamic_start_init( $instance, $widget ) {
if ( ! is_a( $widget, 'Tribe__Events__Pro__This_Week_Widget' ) ) {
return $instance;
}
add_filter( 'pre_option_start_of_week', function () {
static $has_run = 0;
if ( ! $has_run++ ) {
return (int) date_i18n( 'w' );
}
} );
return $instance;
}
/**
* Adjust the week start date for this week ajax requests.
*
* Monkeying with superglobals in this way is definitely bad, this
* works around other issues currently in ECP.
*/
function this_week_dynamic_start_ajax() {
$nominal_start = date_create( $_POST['start_date'] );
$day_of_week = date_i18n( 'N' );
$start_of_week = $nominal_start->format( 'N' );
$adjustment = $start_of_week - $day_of_week;
$adjustment = $adjustment > 0 ? '+' . $adjustment : '-' . $adjustment;
$_POST['start_date'] = $nominal_start->modify( $adjustment . ' days' )->format( 'Y-m-d' );
}
add_filter( 'widget_display_callback', 'this_week_dynamic_start_init', 10, 2 );
add_action( 'wp_ajax_tribe_this_week', 'this_week_dynamic_start_ajax' );
add_action( 'wp_ajax_nopriv_tribe_this_week', 'this_week_dynamic_start_ajax' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment