setup(); $this->getSettings(); $this->getHolidays(); $this->getSpecialOpenings(); } /** * Setup – intializes opening hours */ function setup() { if (get_option('wp_opening_hours')) : $this->data = json_decode( get_option('wp_opening_hours'), true ); else : $this->data = array(); endif; $this->weekdays = array( 'monday' => op__('Monday'), 'tuesday' => op__('Tuesday'), 'wednesday' => op__('Wednesday'), 'thursday' => op__('Thursday'), 'friday' => op__('Friday'), 'saturday' => op__('Saturday'), 'sunday' => op__('Sunday') ); foreach ( $this->data as $key => $values ) : $periods = array(); foreach ( $this->data[ $key ][ 'times' ] as $period ) : $periods[] = new OpeningPeriod( array( 'day' => $key, 'times' => $period ) ); endforeach; $this->$key = $periods; endforeach; } /** * Save – Saves opening hours to wp_option */ function save() { $data = array(); foreach ($this->weekdays as $key => $caption) : if (is_array($this->$key)) $periods = $this->$key; foreach ($periods as $period) : if ($period->correctValues()) : $data[ $key ]['times'][] = $period->times; else : unset( $period ); endif; endforeach; endforeach; $success = update_option( 'wp_opening_hours', json_encode( $data ) ); $this->setup(); return $success; } /** * Add Dummy Sets – adds an empty set for unset days */ function addDummySets() { foreach ($this->weekdays as $key => $caption) : if (!is_array( $this->$key ) or !count( $this->$key )) $this->$key = array( new OpeningPeriod( array( 'day' => $key, 'times' => array(0, 0, 0, 0) ) ) ); endforeach; } /** * Returns an array with all periods */ function allPeriods() { $periods = array(); foreach ($this->weekdays as $key => $caption) : if (is_array( $this->$key )) foreach ($this->$key as $period) $periods[] = $period; endforeach; return $periods; } /** * Returns number of all periods */ function numberPeriods() { return count( $this->allPeriods() ); } /** * Returns an array with all days */ function allDays() { $days = array(); foreach ($this->weekdays as $key => $caption) : $days[ $key ] = (is_array( $this->$key )) ? $this->$key : array(); endforeach; return $days; } /** * Settings */ function getSettings() { $this->settings = json_decode( get_option('wp_opening_hours_settings'), true ); } function saveSettings() { update_option( 'wp_opening_hours_settings', json_encode( $this->settings ) ); } function applySettings( $args = array() ) { // Format of $args: array( $setting_key => $value ) foreach ( $args as $key => $value ) : $this->settings[ $key ] = $value; endforeach; $this->saveSettings(); } /** * Get Holidays – Sets up the Holidays array from WP Option */ function getHolidays() { $holidays = json_decode( get_option('wp_opening_hours_holidays'), true ); $this->holidays = array(); foreach ((array) $holidays as $holiday) : $this->holidays[] = new HolidayPeriod( $holiday ); endforeach; } /** * Get Special Openings – Sets up the Special Openings array from WP Option */ function getSpecialOpenings() { $specialOpenings = json_decode( get_option('wp_opening_hours_special_openings'), true ); $this->specialOpenings = array(); foreach ((array) $specialOpenings as $special_opening) : $this->specialOpenings[] = new SpecialOpening( $special_opening ); endforeach; } /** * Saves the Holidays array to WP Option */ function saveHolidays() { $holidays = array(); foreach ((array) $this->holidays as $holiday) : $holidays[] = $holiday->data; endforeach; update_option('wp_opening_hours_holidays', json_encode( $holidays )); } /** * Saves Special Openings Array to WP Option */ function saveSpecialOpenings() { $specialOpenings = array(); foreach ((array) $this->specialOpenings as $special_opening) : $specialOpenings[] = $special_opening->data; endforeach; update_option( 'wp_opening_hours_special_openings', json_encode( $specialOpenings ) ); } /** * Add Holiday Dummy – Adds an empty Holiday to the array of Holidays */ function addHolidayDummy() { if (!count($this->holidays)) $this->holidays = array( new HolidayPeriod ); } /** * Add Special Opening Dummy – Adds an empty Special Opening to the array of Special Openings */ function addSpecialOpeningDummy() { if (!count($this->specialOpenings)) $this->specialOpenings = array( new SpecialOpening ); } /** * HELPERS */ function getSets( $key ) { return $this->data[ $key ][ 'times' ]; } function numberSets( $key ) { return count($this->getSets( $key )); } function timeString( $key = false, $set = false, $edge = false ) { if (!$key or $set === false) return; $string = ''; $setdata = $this->data[ (string)$key ][ 'times' ][ (string)$set ]; if ($edge == 'start') : return twoDigits($setdata[0]).':'.twoDigits($setdata[1]); elseif ($edge == 'end') : return twoDigits($setdata[2]).':'.twoDigits($setdata[3]); else : return twoDigits($setdata[0]).':'.twoDigits($setdata[1]).' – '.twoDigits($setdata[2]).':'.twoDigits($setdata[3]); endif; } function isRunning ( $key = false, $set = false ) { if (!$key or $set === false) return; if ($key != strtolower( date('l', time()) )) return false; $setdata = $this->data[ (string)$key ][ 'times' ][ (string)$set ]; $currentTime = date('H', current_time('timestamp'))*100 + date('i', current_time('timestamp')); return ( $currentTime >= $setdata[0]*100 + $setdata[1] and $currentTime <= $setdata[2]*100 + $setdata[3] ); } // End of Class } ?>setup( $data ); $this->timestamp = current_time('timestamp'); $this->currentTime = timeFormat( date('H', $this->timestamp), date('i', $this->timestamp) ); } /** * Setup period instance */ function setup ( $data = false ) { if ($data === false or !is_array($data)) return; if ($data['day']) $this->day = $data['day']; $this->times = $data['times']; $this->start = timeFormat( $this->times[0], $this->times[1] ); $this->end = timeFormat( $this->times[2], $this->times[3] ); $this->start_ts = mktime( $this->times[0], $this->times[1], 0, 0, 0, 0 ); $this->end_ts = mktime( $this->times[2], $this->times[3], 0, 0, 0, 0 ); $this->duration = getDifference( $this->times ); if ($this->isRunning()) : $this->stillRunning = getDifference( array( date('H', $this->timestamp), date('i', $this->timestamp), // now $this->times[2], $this->times[3] // end ) ); endif; $this->is_running = $this->isRunning(); } /** * Is this period currently running? */ function isRunning() { if ( strtolower( date('l', current_time('timestamp')) ) != $this->day ) return false; return ( $this->start <= $this->currentTime and $this->end >= $this->currentTime ); } /** * Returns Time Value */ function val( $edge = 'start', $type = 'hour' ) { $position = ($edge == 'start') ? 0 : 2; if ($type == 'minute') $position++; if (!is_array($this->times)) return false; return $this->times[ $position ]; } /** * Removes nonsense-entries */ function correctValues() { return ( $this->start < $this->end ); } } ?>setup( $data ); } /** * Setup period instance */ function setup ( $data = false ) { if ($data === false or !is_array($data)) return; /* ex: $data = array( 'name' => 'My Holiday', 'start' => '10/16/2013', 'end' => '10/19/2013' ); */ $this->data = $data; if ($data['name']) $this->name = $data['name']; $this->start = $data['start']; $this->start_arr = explode('/', $data['start']); $this->start_num = $this->start_arr[2].$this->start_arr[0].$this->start_arr[1]; $this->start_ts = mktime( 0, 0, 0, $this->start_arr[0], $this->start_arr[1], $this->start_arr[2] ); $this->end = $data['end']; $this->end_arr = explode('/', $data['end']); $this->end_num = $this->end_arr[2].$this->end_arr[0].$this->end_arr[1]; $this->end_ts = mktime( 0, 0, 0, $this->end_arr[0], $this->end_arr[1], $this->end_arr[2] ); } function isRunning() { $now = current_time( 'timestamp' ); return ($now >= $this->start_ts and $now <= $this->end_ts); } } ?>setup( $data ); } /** * Setup period instance */ function setup ( $data = false ) { if ($data === false or !is_array($data)) return; /* ex: $data = array( 'name' => 'My Holiday', 'date' => '10/16/2013' 'start' => '08:00', 'end' => '18:00' ); */ $this->data = $data; if ($data['name']) $this->name = $data['name']; /* Special Opening -> Date */ $this->date_str = $data['date']; $this->date_arr = explode('/', $data['date']); $this->date_assoc = array( 'day' => $this->date_arr[1], 'month' => $this->date_arr[0], 'year' => $this->date_arr[2] ); /* Special Opening -> Time Start */ $this->start_str = $data['start']; $this->start_arr = explode(':', $data['start']); $this->start = $this->start_arr[0]*100 + $this->start_arr[1]; $this->start_ts = mktime( (int)$this->start_arr[0], (int)$this->start_arr[1], 0, (int)$this->date_assoc['month'], (int)$this->date_assoc['day'], (int)$this->date_assoc['year'] ); /* Special Opening -> Time End */ $this->end_str = $data['end']; $this->end_arr = explode(':', $data['end']); $this->end = $this->end_arr[0]*100 + $this->end_arr[1]; $this->end_ts = mktime( (int)$this->end_arr[0], (int)$this->end_arr[1], 0, (int)$this->date_assoc['month'], (int)$this->date_assoc['day'], (int)$this->date_assoc['year'] ); $this->is_running = $this->isRunning(); } /** * Checks if Special Opening is currently running */ function isRunning() { $now = current_time( 'timestamp' ); return ($now >= $this->start_ts and $now <= $this->end_ts); } /** * Checks if the Special Opening's date is today */ function isToday() { $now = current_time( 'timestamp' ); return ( date('m/d/Y', $this->start_ts) == date('m/d/Y', $now) ); } } ?>settings[ $key ]; } function op_assets_path( $url = false ) { $assets_directory = wp_upload_dir(); return ( $url ) ? $assets_directory['baseurl'].'/opening-hours' : $assets_directory['basedir'].'/opening-hours'; } ?>holidays as $holiday) if ($holiday->isRunning()) return ($return_type) ? array( false, 'holiday' ) : false; /* Check for Special Openings */ foreach ((array) $wp_opening_hours->specialOpenings as $specialOpening) : if ( $specialOpening->isToday() ) : // Only take todays Special Opening if ( $specialOpening->isRunning() ) : // Check if Special Opening is Running return ( $return_type ) ? array( true, 'special-opening' ) : true; else : return ( $return_type ) ? array( false, 'special-opening' ) : false; endif; endif; endforeach; /* Check for regular Opening Periods */ foreach ((array) $wp_opening_hours->$today as $period) if ($period->isRunning()) return ($return_type) ? array( true, 'regular' ) : true; return ($return_type) ? array( false, 'regular' ) : false; } /** * is_closed – Returns the opposite of is_open */ function is_closed ( $return_type = false ) { return !is_open( $return_type ); } ?> apply_filters( 'op_status_shortcode_default_open', op__('We\'re currently open') ), 'caption_closed' => apply_filters( 'op_status_shortcode_default_closed', op__('We\'re currently closed') ) ), $atts, apply_filters( 'op_status_shortcode_key', 'is-open' ))); /* Return right string */ return apply_filters( 'op_status_shortcode_output', (is_open()) ? apply_filters( 'op_status_shortcode_open', $caption_open ) : apply_filters( 'op_status_shortcode_closed', $caption_closed ) ); } /** * Register Shortcode */ add_shortcode (apply_filters( 'op_status_shortcode_key', 'is-open' ), 'op_shortcode_is_open'); ?> À PROPOS - Les Produits d'entretien Errold Fortin