Created
November 10, 2012 17:28
-
-
Save WebBaker/4051841 to your computer and use it in GitHub Desktop.
Enforced Facebook Time Adjustment (Importing into The Events Calendar)
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
| /** | |
| * Automates the process of correcting times imported from Facebook by a fixed | |
| * number of hours (plus or minus). | |
| * | |
| * @uses DateTime which introduces a requirement for PHP 5.2 or greater | |
| */ | |
| class FBEventsTimeCorrection { | |
| protected $negative = false; | |
| protected $hours = 0; | |
| protected $modifier = ''; | |
| protected $eventData = array(); | |
| const CORRECTION_FORMAT = '#^([\\+-]?)(\\d{1,2})$#'; | |
| /** | |
| * Sets up automated correction of imported Facebook event times. | |
| * | |
| * The correction should be specified in the form of an optional positive or | |
| * negative operator (+/-) followed by an integral number in the range 0-12, | |
| * such as +11 or -7. | |
| * | |
| * If not preceded by a positive/negative operator then positive is assumed. | |
| * | |
| * @param string $correction | |
| * @throws Exception | |
| */ | |
| public function __construct($correction) { | |
| if ($this->setCorrection($correction)) | |
| add_filter('tribe_fb_parse_facebook_event', array($this, 'applyCorrection')); | |
| else throw new Exception('The specified correction cannot be understood.'); | |
| } | |
| /** | |
| * Tests to see if the specified correction is in an understandable format. | |
| * | |
| * @param string $correction | |
| * @return bool | |
| */ | |
| public function setCorrection($correction) { | |
| $matches = array(); | |
| // Test to see if a basic format of [+/-]n has been specified then try to set | |
| if (preg_match(self::CORRECTION_FORMAT, $correction, $matches) === 1) { | |
| if (count($matches) !== 3) return false; | |
| else return $this->setDirectionAndHours($matches[1], $matches[2]); | |
| } | |
| return false; | |
| } | |
| /** | |
| * Sets the internal variables used to record the correction. | |
| * | |
| * If the $hours param is out of bounds this method will return bool false. | |
| * | |
| * @param $direction | |
| * @param $hours | |
| * @return bool | |
| */ | |
| protected function setDirectionAndHours($direction, $hours) { | |
| // Do we have a negative operator? | |
| if ($direction === '-') $this->negative = true; | |
| // Extract the number of hours and ensure they are within range | |
| $this->hours = (int) $hours; | |
| if ($this->hours < 0 or $this->hours > 12) return false; | |
| return true; | |
| } | |
| /** | |
| * Attempts to adjust the event date/time. | |
| * | |
| * Although we require the DateTime class (available as of PHP 5.2) we will | |
| * degrade gracefully for earlier versions of PHP and give an opportunity | |
| * for a replacement class to be used on those versions (ie, from PEAR). | |
| * | |
| * @param array $eventData | |
| * @return array | |
| */ | |
| public function applyCorrection($eventData) { | |
| if (class_exists('DateTime')) { | |
| $this->correctDateTime('Start', $eventData); | |
| $this->correctDateTime('End', $eventData); | |
| } | |
| return $eventData; | |
| } | |
| /** | |
| * Performs the correction of event date/time. | |
| * | |
| * @param $modifier | |
| * @param $eventData | |
| */ | |
| protected function correctDateTime($modifier, &$eventData) { | |
| $this->modifier = $modifier; | |
| $this->eventData = $eventData; | |
| $datetime = $this->getDateTimeObject(); | |
| $datetime->modify($this->getAdjustment()); | |
| $this->rebuildEventData($datetime); | |
| $eventData = $this->eventData; | |
| } | |
| /** | |
| * Creates a DateTime object based on the Start|End date/time. Whether it | |
| * is the Start or End is determined by the $modifier property. | |
| * | |
| * @return DateTime | |
| */ | |
| protected function getDateTimeObject() { | |
| $date = $this->eventData["Event{$this->modifier}Date"]; | |
| $hour = $this->eventData["Event{$this->modifier}Hour"]; | |
| $minute = $this->eventData["Event{$this->modifier}Minute"]; | |
| $meridian = $this->eventData["Event{$this->modifier}Meridian"]; | |
| // Convert hour to 24hr format | |
| if ($meridian === 'PM') $hour += 12; | |
| $hour = sprintf('%02d', $hour); | |
| $minute = sprintf('%02d', $minute); | |
| return new DateTime("$date $hour:$minute:00"); | |
| } | |
| /** | |
| * Generates a strtotime() compatible modifier string. | |
| * | |
| * @return string | |
| */ | |
| protected function getAdjustment() { | |
| $adjustment = ''; | |
| if ($this->negative) $adjustment .= '-'; | |
| $adjustment .= $this->hours.' hours'; | |
| return $adjustment; | |
| } | |
| /** | |
| * Rebuilds the event data array with our adjusted date/time. | |
| */ | |
| protected function rebuildEventData(DateTime $datetime) { | |
| $this->eventData["Event{$this->modifier}Date"] = $datetime->format('Y-m-d'); | |
| $this->eventData["Event{$this->modifier}Hour"] = $datetime->format('g'); | |
| $this->eventData["Event{$this->modifier}Minute"] = $datetime->format('i'); | |
| $this->eventData["Event{$this->modifier}Meridian"] = $datetime->format('A'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment