1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Business Hours Block. |
4
|
|
|
* |
5
|
|
|
* @since 7.1.0 |
6
|
|
|
* |
7
|
|
|
* @package Jetpack |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
jetpack_register_block( |
11
|
|
|
'jetpack/business-hours', |
12
|
|
|
array( 'render_callback' => 'jetpack_business_hours_render' ) |
13
|
|
|
); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Get's default days / hours to render a business hour block with no data provided. |
17
|
|
|
* |
18
|
|
|
* @return array |
19
|
|
|
*/ |
20
|
|
|
function jetpack_business_hours_get_default_days() { |
21
|
|
|
return array( |
22
|
|
|
array( |
23
|
|
|
'name' => 'Sun', |
24
|
|
|
'hours' => array(), |
25
|
|
|
), |
26
|
|
|
array( |
27
|
|
|
'name' => 'Mon', |
28
|
|
|
'hours' => array( |
29
|
|
|
array( 'opening' => '09:00', 'closing' => '17:00' ) |
30
|
|
|
), |
31
|
|
|
), |
32
|
|
|
array( |
33
|
|
|
'name' => 'Tue', |
34
|
|
|
'hours' => array( |
35
|
|
|
array( 'opening' => '09:00', 'closing' => '17:00' ) |
36
|
|
|
), |
37
|
|
|
), |
38
|
|
|
array( |
39
|
|
|
'name' => 'Wed', |
40
|
|
|
'hours' => array( |
41
|
|
|
array( 'opening' => '09:00', 'closing' => '17:00' ) |
42
|
|
|
), |
43
|
|
|
), |
44
|
|
|
array( |
45
|
|
|
'name' => 'Thu', |
46
|
|
|
'hours' => array( |
47
|
|
|
array( 'opening' => '09:00', 'closing' => '17:00' ) |
48
|
|
|
), |
49
|
|
|
), |
50
|
|
|
array( |
51
|
|
|
'name' => 'Fri', |
52
|
|
|
'hours' => array( |
53
|
|
|
array( 'opening' => '09:00', 'closing' => '17:00' ) |
54
|
|
|
), |
55
|
|
|
), |
56
|
|
|
array( |
57
|
|
|
'name' => 'Sat', |
58
|
|
|
'hours' => array(), |
59
|
|
|
), |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Dynamic rendering of the block. |
65
|
|
|
* |
66
|
|
|
* @param array $attributes Array containing the business hours block attributes. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
function jetpack_business_hours_render( $attributes ) { |
71
|
|
|
global $wp_locale; |
72
|
|
|
|
73
|
|
|
if ( empty( $attributes['days'] ) || ! is_array( $attributes['days'] ) ) { |
74
|
|
|
$attributes['days'] = jetpack_business_hours_get_default_days(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$start_of_week = (int) get_option( 'start_of_week', 0 ); |
78
|
|
|
$time_format = get_option( 'time_format' ); |
79
|
|
|
$content = sprintf( |
80
|
|
|
'<dl class="jetpack-business-hours %s">', |
81
|
|
|
! empty( $attributes['className'] ) ? esc_attr( $attributes['className'] ) : '' |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$days = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ); |
85
|
|
|
|
86
|
|
|
if ( $start_of_week ) { |
87
|
|
|
$chunk1 = array_slice( $attributes['days'], 0, $start_of_week ); |
88
|
|
|
$chunk2 = array_slice( $attributes['days'], $start_of_week ); |
89
|
|
|
$attributes['days'] = array_merge( $chunk2, $chunk1 ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
foreach ( $attributes['days'] as $day ) { |
93
|
|
|
$content .= '<dt class="' . esc_attr( $day['name'] ) . '">' . |
94
|
|
|
ucfirst( $wp_locale->get_weekday( array_search( $day['name'], $days, true ) ) ) . |
95
|
|
|
'</dt>'; |
96
|
|
|
$content .= '<dd class="' . esc_attr( $day['name'] ) . '">'; |
97
|
|
|
$days_hours = ''; |
98
|
|
|
|
99
|
|
|
foreach ( $day['hours'] as $hour ) { |
100
|
|
|
$opening = strtotime( $hour['opening'] ); |
101
|
|
|
$closing = strtotime( $hour['closing'] ); |
102
|
|
|
if ( ! $opening || ! $closing ) { |
103
|
|
|
continue; |
104
|
|
|
} |
105
|
|
|
$days_hours .= sprintf( |
106
|
|
|
/* Translators: Business opening hours info. */ |
107
|
|
|
_x( 'From %1$s to %2$s', 'from business opening hour to closing hour', 'jetpack' ), |
108
|
|
|
date( $time_format, $opening ), |
109
|
|
|
date( $time_format, $closing ) |
110
|
|
|
); |
111
|
|
|
$days_hours .= '<br />'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ( empty( $days_hours ) ) { |
115
|
|
|
$days_hours = esc_html__( 'Closed', 'jetpack' ); |
116
|
|
|
} |
117
|
|
|
$content .= $days_hours; |
118
|
|
|
$content .= '</dd>'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$content .= '</dl>'; |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Allows folks to filter the HTML content for the Business Hours block |
125
|
|
|
* |
126
|
|
|
* @since 7.1.0 |
127
|
|
|
* |
128
|
|
|
* @param string $content The default HTML content set by `jetpack_business_hours_render` |
129
|
|
|
* @param array $attributes Attributes generated in the block editor for the Business Hours block |
130
|
|
|
*/ |
131
|
|
|
return apply_filters( 'jetpack_business_hours_content', $content, $attributes ); |
132
|
|
|
} |
133
|
|
|
|