Advertisement
BakerMan

Tribe - default start/end time helper

Sep 10th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1. /**
  2.  * Helps to change the default start/end times for new event.
  3.  */
  4. class Change_Tribe_Default_Event_Times
  5. {
  6.     const TWELVEHOUR = 12;
  7.     const TWENTYFOURHOUR = 24;
  8.  
  9.     protected $start = 9;
  10.     protected $end = 11;
  11.     protected $mode = self::TWELVEHOUR;
  12.  
  13.  
  14.     /**
  15.      * Provide the desired default start and end hours in 24hr format (ie 15 = 3pm).
  16.      *
  17.      * @param $start_hour
  18.      * @param $end_hour
  19.      */
  20.     public function __construct($start_hour, $end_hour) {
  21.         $this->settings($start_hour, $end_hour);
  22.         $this->add_filters();
  23.     }
  24.  
  25.  
  26.     protected function settings($start_hour, $end_hour) {
  27.         $this->set_mode();
  28.         $this->start = $this->safe_hour($start_hour);
  29.         $this->end = $this->safe_hour($end_hour);
  30.     }
  31.  
  32.  
  33.     protected function add_filters() {
  34.         add_filter('tribe_get_hour_options', array($this, 'change_default_time'), 10, 3);
  35.         add_filter('tribe_get_meridian_options', array($this, 'change_default_meridian'), 10, 3);
  36.     }
  37.  
  38.  
  39.     protected function set_mode() {
  40.         if (strstr(get_option('time_format', TribeDateUtils::TIMEFORMAT), 'H'))
  41.             $this->mode = self::TWENTYFOURHOUR;
  42.     }
  43.  
  44.  
  45.     protected function safe_hour($hour) {
  46.         $hour = absint($hour);
  47.         if ($hour < 0) $hour = 0;
  48.         if ($hour > 23) $hour = 23;
  49.         return $hour;
  50.     }
  51.  
  52.  
  53.     public function change_default_time($hour, $date, $isStart) {
  54.         if ('post-new.php' !== $GLOBALS['pagenow']) return $hour; // Only intervene if it's a new event
  55.  
  56.         if ($isStart) return $this->corrected_time($this->start);
  57.         else return $this->corrected_time($this->end);
  58.     }
  59.  
  60.  
  61.     /**
  62.      * If working in the 12hr clock, converts the hour appropriately.
  63.      *
  64.      * @param $hour
  65.      * @return int
  66.      */
  67.     protected function corrected_time($hour) {
  68.         if (self::TWENTYFOURHOUR === $this->mode) return $hour;
  69.         if ($hour > 12) return $hour - 12;
  70.         return $hour;
  71.     }
  72.  
  73.  
  74.     public function change_default_meridian($meridian, $date, $isStart) {
  75.         if ('post-new.php' !== $GLOBALS['pagenow']) return $meridian; // Only intervene if it's a new event
  76.  
  77.         $meridian = 'am';
  78.         if ($isStart && 12 <= $this->start) $meridian = 'pm';
  79.         if (! $isStart && 12 <= $this->end) $meridian = 'pm';
  80.  
  81.         if (strstr(get_option('time_format', TribeDateUtils::TIMEFORMAT), 'A'))
  82.             $meridian = strtoupper($meridian);
  83.  
  84.         return $meridian;
  85.     }
  86. }
  87.  
  88. // If you mostly deal with night time soirees you could set the default start time
  89. // to 7pm and end time to 11pm - but remember to do so with the 24hr clock
  90. new Change_Tribe_Default_Event_Times(19, 23);
  91.  
  92. // If you run early morning workouts you might set the default start time to
  93. // 5am and end time to 6am
  94. new Change_Tribe_Default_Event_Times(5, 6);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement