1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Business Hours Block. |
4
|
|
|
* |
5
|
|
|
* @since 7.1.0 |
6
|
|
|
* |
7
|
|
|
* @package Jetpack |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
jetpack_register_block_type( |
11
|
|
|
'jetpack/business-hours', |
12
|
|
|
array( 'render_callback' => 'jetpack_business_hours_render' ) |
13
|
|
|
); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Returns a filterable answer to the question: should we use a standardized schema in the business hours mark-up? |
17
|
|
|
* |
18
|
|
|
* @return bool |
19
|
|
|
*/ |
20
|
|
|
function jetpack_business_hours_should_use_schema() { |
21
|
|
|
/** |
22
|
|
|
* Filters if business hours html should use schema from https://schema.org/openingHours |
23
|
|
|
* |
24
|
|
|
* @since 7.1.0 |
25
|
|
|
* |
26
|
|
|
* @param string $should_use_schema Defaults to true |
27
|
|
|
*/ |
28
|
|
|
return apply_filters( 'jetpack_business_hours_should_use_schema', true ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Returns the opening html tag for the business hours block. |
33
|
|
|
* |
34
|
|
|
* @param array $attributes Attributes for the business hours block established in the block editor. |
35
|
|
|
* |
36
|
|
|
* @return string The opening tag for the business hours markup. |
37
|
|
|
*/ |
38
|
|
|
function jetpack_business_hours_opening_tag( $attributes ) { |
39
|
|
|
if ( jetpack_business_hours_should_use_schema() ) { |
40
|
|
|
$itemtype = apply_filters( 'jetpack_business_hours_item_type', 'https://schema.org/LocalBusiness' ); |
41
|
|
|
return sprintf( |
42
|
|
|
'<dl class="jetpack-business-hours %s" itemscope itemtype="%s">', |
43
|
|
|
! empty( $attributes['className'] ) ? esc_attr( $attributes['className'] ) : '', |
44
|
|
|
$itemtype |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
return sprintf( |
48
|
|
|
'<dl class="jetpack-business-hours %s">', |
49
|
|
|
! empty( $attributes['className'] ) ? esc_attr( $attributes['className'] ) : '' |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns the opening `<span>` tag for a set of opening - closing hours for a given day. |
55
|
|
|
* |
56
|
|
|
* @param string $day eg 'Mon' |
57
|
|
|
* @param int $opens timestamp |
58
|
|
|
* @param int $closes timestamp |
59
|
|
|
* |
60
|
|
|
* @return string The tag |
61
|
|
|
*/ |
62
|
|
|
function jetpack_business_hours_opening_tag_for_days_hours( $day, $opens, $closes ) { |
63
|
|
|
if ( jetpack_business_hours_should_use_schema() ) { |
64
|
|
|
return sprintf( |
65
|
|
|
'<span itemprop="openingHours" content="%s %s-%s">', |
66
|
|
|
substr( $day, 0, 2 ), |
67
|
|
|
date( 'H:i', $opens ), |
68
|
|
|
date( 'H:i', $closes ) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
return '<span>'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Dynamic rendering of the block. |
76
|
|
|
* |
77
|
|
|
* @param array $attributes Array containing the business hours block attributes. |
78
|
|
|
* @param string $content String containing the business hours block content. |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
function jetpack_business_hours_render( $attributes, $content ) { |
83
|
|
|
global $wp_locale; |
84
|
|
|
|
85
|
|
|
if ( empty( $attributes['hours'] ) || ! is_array( $attributes['hours'] ) ) { |
86
|
|
|
return $content; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$start_of_week = (int) get_option( 'start_of_week', 0 ); |
90
|
|
|
$time_format = get_option( 'time_format' ); |
91
|
|
|
$today = current_time( 'D' ); |
92
|
|
|
$content = jetpack_business_hours_opening_tag(); |
|
|
|
|
93
|
|
|
|
94
|
|
|
$days = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ); |
95
|
|
|
|
96
|
|
|
if ( $start_of_week ) { |
97
|
|
|
$chunk1 = array_slice( $attributes['hours'], 0, $start_of_week ); |
98
|
|
|
$chunk2 = array_slice( $attributes['hours'], $start_of_week ); |
99
|
|
|
$attributes['hours'] = array_merge( $chunk2, $chunk1 ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
foreach ( $attributes['hours'] as $day => $hours ) { |
103
|
|
|
$content .= '<dt class="' . esc_attr( $day ) . '">' . |
104
|
|
|
ucfirst( $wp_locale->get_weekday( array_search( $day, $days ) ) ) . |
105
|
|
|
'</dt>'; |
106
|
|
|
$content .= '<dd class="' . esc_attr( $day ) . '">'; |
107
|
|
|
$days_hours = ''; |
108
|
|
|
|
109
|
|
|
foreach ( $hours as $hour ) { |
110
|
|
|
if ( empty( $hour['opening'] ) || empty( $hour['closing'] ) ) { |
111
|
|
|
continue; |
112
|
|
|
} |
113
|
|
|
$opening = strtotime( $hour['opening'] ); |
114
|
|
|
$closing = strtotime( $hour['closing'] ); |
115
|
|
|
$days_hours .= jetpack_business_hours_opening_tag_for_days_hours( $day, $opening, $closing ); |
116
|
|
|
$days_hours .= sprintf( |
117
|
|
|
/* Translators: Business opening hours info. */ |
118
|
|
|
_x( 'From %1$s to %2$s', 'from business opening hour to closing hour', 'jetpack' ), |
119
|
|
|
date( $time_format, $opening ), |
120
|
|
|
date( $time_format, $closing ) |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
if ( $today === $day ) { |
124
|
|
|
$now = strtotime( current_time( 'H:i' ) ); |
125
|
|
|
if ( $now < $opening ) { |
126
|
|
|
$days_hours .= '<br />'; |
127
|
|
|
$days_hours .= esc_html( sprintf( |
128
|
|
|
/* Translators: Amount of time until business opens. */ |
129
|
|
|
_x( 'Opening in %s', 'Amount of time until business opens', 'jetpack' ), |
130
|
|
|
human_time_diff( $now, $opening ) |
131
|
|
|
) ); |
132
|
|
|
} elseif ( $now >= $opening && $now < $closing ) { |
133
|
|
|
$days_hours .= '<br />'; |
134
|
|
|
$days_hours .= esc_html( sprintf( |
135
|
|
|
/* Translators: Amount of time until business closes. */ |
136
|
|
|
_x( 'Closing in %s', 'Amount of time until business closes', 'jetpack' ), |
137
|
|
|
human_time_diff( $now, $closing ) |
138
|
|
|
) ); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
$days_hours .= '</span><br />'; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if ( empty( $days_hours ) ) { |
145
|
|
|
$days_hours = esc_html__( 'Closed', 'jetpack' ); |
146
|
|
|
} |
147
|
|
|
$content .= $days_hours; |
148
|
|
|
$content .= '</dd>'; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$content .= '</dl>'; |
152
|
|
|
|
153
|
|
|
return $content; |
154
|
|
|
} |
155
|
|
|
|
This check looks for function calls that miss required arguments.