| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPPIZZA_TEMPLATES_OPENINGTIMES Class |
| 4 |
* |
| 5 |
* @package WPPIZZA |
| 6 |
* @subpackage Opening times |
| 7 |
* @copyright Copyright (c) 2015, Oliver Bach |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 3.0 |
| 10 |
* |
| 11 |
*/ |
| 12 |
if ( ! defined( 'ABSPATH' ) ) exit;/*Exit if accessed directly*/ |
| 13 |
|
| 14 |
/* ================================================================================================================================= * |
| 15 |
* |
| 16 |
* |
| 17 |
* |
| 18 |
* CLASS - WPPIZZA_TEMPLATES_OPENINGTIMES |
| 19 |
* |
| 20 |
* |
| 21 |
* |
| 22 |
* ================================================================================================================================= */ |
| 23 |
|
| 24 |
class WPPIZZA_MARKUP_OPENINGTIMES{ |
| 25 |
|
| 26 |
/****************************************************************************** |
| 27 |
* |
| 28 |
* |
| 29 |
* [construct] |
| 30 |
* |
| 31 |
* |
| 32 |
*******************************************************************************/ |
| 33 |
function __construct() { |
| 34 |
} |
| 35 |
|
| 36 |
/****************************************************************************** |
| 37 |
* |
| 38 |
* |
| 39 |
* [methods] |
| 40 |
* |
| 41 |
* |
| 42 |
*******************************************************************************/ |
| 43 |
/*************************************** |
| 44 |
[apply attributes] |
| 45 |
***************************************/ |
| 46 |
function attributes($atts=null){ |
| 47 |
|
| 48 |
|
| 49 |
/*************************************** |
| 50 |
skip if not cart or shortcode |
| 51 |
***************************************/ |
| 52 |
if($atts['type'] != 'cart' && $atts['type'] != 'openingtimes'){ |
| 53 |
return; |
| 54 |
} |
| 55 |
/*************************************** |
| 56 |
skip if cart and not set /enabled |
| 57 |
****************************************/ |
| 58 |
if($atts['type'] == 'cart' && empty($atts['openingtimes']) ){ |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
/**get markup**/ |
| 63 |
$markup = $this->get_markup($atts); |
| 64 |
return $markup; |
| 65 |
} |
| 66 |
|
| 67 |
/*************************************** |
| 68 |
[markup] |
| 69 |
***************************************/ |
| 70 |
function get_markup($atts, $widget_wrapper = true){ |
| 71 |
global $wppizza_options; |
| 72 |
static $unique_id=0;$unique_id++;/* set unique id */ |
| 73 |
|
| 74 |
$txt = $wppizza_options['localization'];/*put all text varibles into something easier to deal with**/ |
| 75 |
|
| 76 |
$weekDayStart=get_option('start_of_week',7); |
| 77 |
/* filter to use long day format */ |
| 78 |
$day_format = apply_filters('wppizza_filter_openingtimes_day_format', 'D');/* filter to return 'l' if you want to return full textual representation */ |
| 79 |
$day_format = ($day_format=='l') ? 'l' : 'D' ;/*force D or l as nothing else makes sense here really*/ |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
/**group identical standard opening times**/ |
| 84 |
foreach($wppizza_options['openingtimes']['opening_times_standard'] as $k=>$v){ |
| 85 |
if($k==0 && $weekDayStart!=0){$k=7;}/*for sorting reasons , set sunday temporarily to 7 here unless weekstart is set to sunday anyway**/ |
| 86 |
/* |
| 87 |
do not group days with the same opening hours |
| 88 |
*/ |
| 89 |
$nogrouping = !empty($wppizza_options['opening_times_format']['dont_group_days']) ? '|'.$k : ''; |
| 90 |
|
| 91 |
if(!isset($times[''.$v['open'].'|'.$v['close'].''.$nogrouping])){ |
| 92 |
$times[''.$v['open'].'|'.$v['close'].''.$nogrouping]=array(); |
| 93 |
$times[''.$v['open'].'|'.$v['close'].''.$nogrouping][]=$k; |
| 94 |
}else{ |
| 95 |
$times[''.$v['open'].'|'.$v['close'].''.$nogrouping][]=$k; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
foreach($times as $k=>$arr){ |
| 100 |
/*to have sundays last when sorting, set it to 7*/ |
| 101 |
asort($arr); |
| 102 |
$grouped[$k]=array('firstday'=>reset($arr),'days'=>$arr,'consecutivedays'=>$this->days_concat($arr)); |
| 103 |
} |
| 104 |
|
| 105 |
/**sort by first day in array so we start with a monday regardless**/ |
| 106 |
asort($grouped); |
| 107 |
|
| 108 |
/**ini array**/ |
| 109 |
$openingtimes = array(); |
| 110 |
foreach($grouped as $k=>$v){ |
| 111 |
$nonConsec=explode(",",$v['consecutivedays']); |
| 112 |
$groupClasses=''; |
| 113 |
$groupDays=''; |
| 114 |
|
| 115 |
foreach($nonConsec as $b=>$c){ |
| 116 |
$groupClasses.=' '.WPPIZZA_PREFIX.'-optm-'.$c.''; |
| 117 |
$consecDays=explode("-",$c); |
| 118 |
if($b>0){$groupDays.=', ';} |
| 119 |
if(count($consecDays)>1){ |
| 120 |
/**create appropriate separator**/ |
| 121 |
if(($consecDays[0]+1)==$consecDays[1]){$separator=', ';}else{$separator='-';} |
| 122 |
foreach($consecDays as $cc=>$cd){ |
| 123 |
if($cc>0){ |
| 124 |
$groupDays.=$separator; |
| 125 |
} |
| 126 |
$groupDays.=wppizza_format_weekday($cd,$day_format); |
| 127 |
} |
| 128 |
}else{ |
| 129 |
$groupDays.=wppizza_format_weekday($consecDays[0],$day_format); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/* class */ |
| 134 |
$openingtimes[$k]['class'] = trim($groupClasses); |
| 135 |
|
| 136 |
/* days */ |
| 137 |
$openingtimes[$k]['days'] = $groupDays; |
| 138 |
|
| 139 |
/* times */ |
| 140 |
$open=explode("|",$k); |
| 141 |
if($open[0]==$open[1]){ |
| 142 |
$openingtimes[$k]['times'] = $txt['openinghours_closed']; |
| 143 |
}else{ |
| 144 |
if(($open[0]=='00:00' || $open[0]=='0:00' ) && $open[1]=='24:00'){ |
| 145 |
$openingtimes[$k]['times'] = $txt['openinghours_24hrs']; |
| 146 |
|
| 147 |
}else{ |
| 148 |
$openingtimes[$k]['times'] = wppizza_format_time($open[0],$wppizza_options['opening_times_format']).'-'.wppizza_format_time($open[1],$wppizza_options['opening_times_format']); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
/* set unique id */ |
| 153 |
$id = WPPIZZA_PREFIX.'-opening-hours-'.$unique_id; |
| 154 |
|
| 155 |
/* set width , if defined */ |
| 156 |
$style = (!empty($atts['width'])) ? ' style="width:'.esc_html($atts['width']).'"' : '' ; |
| 157 |
|
| 158 |
/* classes */ |
| 159 |
$class= array(); |
| 160 |
/* class for all */ |
| 161 |
$class[] = WPPIZZA_PREFIX.'-opening-hours'; |
| 162 |
/* if not from shortcode (i.e part of cart) */ |
| 163 |
if(!empty($atts['type']) && $atts['type'] != 'openingtimes'){ |
| 164 |
$class[] = WPPIZZA_PREFIX.'-opening-hours-'.$atts['type']; |
| 165 |
} |
| 166 |
if(!empty($atts['class'])){ |
| 167 |
$class[] = esc_html($atts['class']); |
| 168 |
} |
| 169 |
/* |
| 170 |
allow class filtering |
| 171 |
implode for output |
| 172 |
*/ |
| 173 |
$class = apply_filters('wppizza_filter_openingtimes_widget_class', $class, $atts, $unique_id); |
| 174 |
$class = trim(implode(' ', $class)); |
| 175 |
|
| 176 |
|
| 177 |
/* |
| 178 |
ini array |
| 179 |
*/ |
| 180 |
$markup = array(); |
| 181 |
/* |
| 182 |
get markup |
| 183 |
*/ |
| 184 |
if(file_exists( WPPIZZA_TEMPLATE_DIR . '/markup/global/openingtimes.php')){ |
| 185 |
require(WPPIZZA_TEMPLATE_DIR.'/markup/global/openingtimes.php'); |
| 186 |
}else{ |
| 187 |
require(WPPIZZA_PATH.'templates/markup/global/openingtimes.php'); |
| 188 |
} |
| 189 |
/* |
| 190 |
omit wrapper (for example if using ajax to re-fill widget content) |
| 191 |
*/ |
| 192 |
if(empty($widget_wrapper)){ |
| 193 |
unset($markup['div_']); |
| 194 |
unset($markup['_div']); |
| 195 |
} |
| 196 |
|
| 197 |
/* |
| 198 |
apply filter if required and implode for output |
| 199 |
*/ |
| 200 |
$markup = apply_filters('wppizza_filter_openingtimes_widget_markup', $markup, $atts, $unique_id); |
| 201 |
$markup = trim(implode('', $markup)); |
| 202 |
|
| 203 |
|
| 204 |
return $markup; |
| 205 |
} |
| 206 |
|
| 207 |
/*************************** |
| 208 |
helper |
| 209 |
***************************/ |
| 210 |
|
| 211 |
function days_concat($days){ |
| 212 |
|
| 213 |
// Define all days of the week, st sun(0) to 7 |
| 214 |
static $all_days = array('0','1', '2', '3', '4', '5', '6','7'); |
| 215 |
|
| 216 |
// prepare our output |
| 217 |
$output = array(); |
| 218 |
|
| 219 |
// loop through all days of the week |
| 220 |
foreach ( $all_days as $i => $day ){ |
| 221 |
// if it is included, |
| 222 |
if ( in_array( $day, $days ) ){ |
| 223 |
$output[] = $day; |
| 224 |
}else{/*if not*/ |
| 225 |
$output[] = '#'; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
// clean everything up |
| 230 |
$output = implode( '#', $output ); |
| 231 |
$output = trim( $output, '#' ); |
| 232 |
|
| 233 |
// two or more consecutive hashes = days that are two or more apart |
| 234 |
$output = preg_split( '/##+/', $output, 0, PREG_SPLIT_NO_EMPTY ); |
| 235 |
|
| 236 |
// turn consecutive days into dashed days |
| 237 |
foreach ( $output as $i => $value ){ |
| 238 |
$output[ $i ] = preg_replace( '/#(\w+#)*/', '-', $value ); |
| 239 |
} |
| 240 |
// format with commas |
| 241 |
$output = implode( ',', $output ); |
| 242 |
|
| 243 |
return $output; |
| 244 |
} |
| 245 |
|
| 246 |
} |
| 247 |
?> |