/**
* Opening Hours Class
*/
class OpeningHours {
function __construct() {
$this->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
}
?>
/**
* Opening Period Class
*/
class OpeningPeriod {
public $currentTime;
function __construct( $data = array() ) {
$this->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 );
}
}
?>
/**
* Holiday Period Class
*/
class HolidayPeriod {
function __construct( $data = false ) {
$this->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);
}
}
?>
/**
* Special Opening Class
*/
class SpecialOpening {
function __construct( $data = false ) {
$this->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) );
}
}
?>
/**
* Functions
*/
function twoDigits( $num = false ) {
if ($num === false) return;
if ($num === 0) return '00';
return (strlen($num) == 1) ? '0'.$num : $num;
}
function timeFormat( $hour = 0, $minute = 0 ) {
return $hour * 100 + $minute;
}
function getDifference( $data ) {
$hours_diff = $data[2] - $data[0];
$min_diff = $data[3] - $data[1];
return $hours_diff * 60 + $min_diff; // return [minutes]
}
function timeString( $data, $timeFormat = false ) {
if (!$timeFormat)
$timeFormat = (op_get_setting( 'time-format' ) != '') ? op_get_setting( 'time-format' ) : 'H:i';
return dateString( $data, $timeFormat );
}
function dateString( $data, $dateFormat = false ) {
if (!$dateFormat)
$dateFormat = (op_get_setting( 'date-format' ) != '') ? op_get_setting( 'date-format' ) : 'd.m.Y';
return date( $dateFormat, $data['start'] ).' – '.date( $dateFormat, $data['end'] );
}
function timeExtension ( $hour ) {
return ($hour < 12) ? 'am' : 'pm';
}
function op_get_setting( $key ) {
global $wp_opening_hours;
return $wp_opening_hours->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';
}
?>
/**
* Register Styles
*/
function op_register_styles_backend() {
wp_enqueue_style('jQuery-ui-timepicker', op_baseurl() . '/js/jQuery.ui.timepicker/jquery.ui.timepicker.css', false, false, 'all');
wp_enqueue_style('jQuery-ui-style', 'https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css', false, false, 'all');
wp_enqueue_style('opening-hours-backend', apply_filters( 'op_backend_stylesheet', op_baseurl() . '/css/backend.css' ), false, false, 'all');
wp_register_script('jQuery-ui', 'https://code.jquery.com/ui/1.11.4/jquery-ui.js', false, null, false);
wp_register_script('jQuery-ui-timepicker', op_baseurl() . '/js/jQuery.ui.timepicker/jquery.ui.timepicker.js', false, null, false);
wp_enqueue_script('jQuery-ui');
wp_enqueue_script('jQuery-ui-timepicker');
}
add_action('admin_init', 'op_register_styles_backend');
function op_register_styles_frontend() {
wp_register_style('opening-hours-frontend', apply_filters( 'op_frontend_stylesheet', op_baseurl().'/css/frontend.css' ), false, false, 'all');
wp_enqueue_style( 'opening-hours-frontend' );
if (file_exists( op_assets_path().'/custom_style.css' )) :
wp_register_style( 'opening-hours-user-stylesheet', op_assets_path().'/custom_style.css', false, false, all );
wp_enqueue_style( 'opening-hours-user-stylesheet' );
endif;
}
add_action('wp_enqueue_scripts', 'op_register_styles_frontend');
/**
* Create Opening Hours Instance
*/
$wp_opening_hours = new OpeningHours;
/**
* Register Backend Options Pages
*/
function op_register_options_pages() {
// Top level menu item
add_menu_page(
apply_filters( 'op_menu_title_opening_hours', __('Opening Hours', op_textdomain()) ),
apply_filters( 'op_menu_title_opening_hours', __('Opening Hours', op_textdomain()) ),
apply_filters( 'op_min_user_capability', 'edit_pages' ),
'opening-hours',
'op_setup_page'
);
// Holidays Page
add_submenu_page(
'opening-hours',
apply_filters( 'op_menu_title_holidays', op__('Holidays') ),
apply_filters( 'op_menu_title_holidays', op__('Holidays') ),
apply_filters( 'op_min_user_capability', 'edit_pages' ),
'opening-hours-holidays',
'op_holidays_page'
);
// Special Openings Page
add_submenu_page(
'opening-hours',
apply_filters( 'op_menu_title_special_openings', op__('Special Openings') ),
apply_filters( 'op_menu_title_special_openings', op__('Special Openings') ),
apply_filters( 'op_min_user_capability', 'edit_pages' ),
'opening-hours-special-openings',
'op_special_openings_page'
);
// Settings Page
add_submenu_page (
'opening-hours',
apply_filters( 'op_menu_title_settings', op__('Settings') ),
apply_filters( 'op_menu_title_settings', op__('Settings') ),
apply_filters( 'op_min_user_capability', 'edit_pages' ),
'opening-hours-settings',
'op_settings_page'
);
}
function op_setup_page() {
global $wp_opening_hours;
// include template
require_once op_basepath() . '/templates/setup-page.php';
}
function op_holidays_page() {
global $wp_opening_hours;
// include template
require_once op_basepath() . '/templates/holidays-page.php';
}
function op_special_openings_page() {
global $wp_opening_hours;
// include template
require_once op_basepath() . '/templates/special-openings-page.php';
}
function op_settings_page() {
global $wp_opening_hours;
// include template
require_once op_basepath() . '/templates/settings-page.php';
}
add_action ('admin_menu', 'op_register_options_pages');
?>
/**
* Opening Hours Widgets
*/
/**
* Require Widget Classes
*/
$widgets = array(
'OpeningHoursOverview',
'OpeningHoursStatus',
'OpeningHoursHolidays',
'OpeningHoursSpecialOpenings',
);
foreach ( $widgets as $widget )
require_once op_basepath() . '/lib/widgets/'. $widget .'.widget.php';
/**
* Register Widgets
*/
function op_init_widgets() {
register_widget ( 'Opening_Hours_Overview' );
register_widget ( 'Opening_Hours_Status' );
register_widget ( 'Opening_Hours_Holidays' );
register_widget ( 'Opening_Hours_Special_Openings' );
}
add_action ( 'widgets_init', 'op_init_widgets' );
?>
/**
* Template Tags
*/
/**
* is_open – Returns wether the venue is open or not
* in $return_type: bool, when set to true, function returns an array( $bool, $context )
* out array( $bool, $context ) or $bool
*/
function is_open ( $return_type = false ) {
global $wp_opening_hours;
if (!is_object($wp_opening_hours)) $wp_opening_hours = new OpeningHours;
$today = strtolower( date('l', current_time('timestamp')) );
/* Check for Holidays */
foreach ((array) $wp_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 );
}
?>
/**
* Opening Hours Shortcodes
*/
/**
* Is Open Shortcode
*/
function op_shortcode_is_open ($atts) {
/* Extract Attributes */
extract(shortcode_atts( array(
'caption_open' => 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');
?>
ONGUENTS & Cie - Les Produits d'entretien Errold Fortin