summaryrefslogtreecommitdiff
blob: d3afa01d5b79f215e21a9d54eed8136024a93520 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
 * Business Hours Block.
 *
 * @since 7.1.0
 *
 * @package Jetpack
 */

jetpack_register_block(
	'jetpack/business-hours',
	array( 'render_callback' => 'jetpack_business_hours_render' )
);

/**
 * Get's default days / hours to render a business hour block with no data provided.
 *
 * @return array
 */
function jetpack_business_hours_get_default_days() {
	return array(
		array(
			'name'  => 'Sun',
			'hours' => array(),
		),
		array(
			'name'  => 'Mon',
			'hours' => array(
				array(
					'opening' => '09:00',
					'closing' => '17:00',
				),
			),
		),
		array(
			'name'  => 'Tue',
			'hours' => array(
				array(
					'opening' => '09:00',
					'closing' => '17:00',
				),
			),
		),
		array(
			'name'  => 'Wed',
			'hours' => array(
				array(
					'opening' => '09:00',
					'closing' => '17:00',
				),
			),
		),
		array(
			'name'  => 'Thu',
			'hours' => array(
				array(
					'opening' => '09:00',
					'closing' => '17:00',
				),
			),
		),
		array(
			'name'  => 'Fri',
			'hours' => array(
				array(
					'opening' => '09:00',
					'closing' => '17:00',
				),
			),
		),
		array(
			'name'  => 'Sat',
			'hours' => array(),
		),
	);
}

/**
 * Dynamic rendering of the block.
 *
 * @param array $attributes Array containing the business hours block attributes.
 *
 * @return string
 */
function jetpack_business_hours_render( $attributes ) {
	global $wp_locale;

	if ( empty( $attributes['days'] ) || ! is_array( $attributes['days'] ) ) {
		$attributes['days'] = jetpack_business_hours_get_default_days();
	}

	$start_of_week = (int) get_option( 'start_of_week', 0 );
	$time_format   = get_option( 'time_format' );
	$content       = sprintf(
		'<dl class="jetpack-business-hours %s">',
		! empty( $attributes['className'] ) ? esc_attr( $attributes['className'] ) : ''
	);

	$days = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' );

	if ( $start_of_week ) {
		$chunk1             = array_slice( $attributes['days'], 0, $start_of_week );
		$chunk2             = array_slice( $attributes['days'], $start_of_week );
		$attributes['days'] = array_merge( $chunk2, $chunk1 );
	}

	foreach ( $attributes['days'] as $day ) {
		$content   .= '<div class="jetpack-business-hours__item"><dt class="' . esc_attr( $day['name'] ) . '">' .
					ucfirst( $wp_locale->get_weekday( array_search( $day['name'], $days, true ) ) ) .
					'</dt>';
		$content   .= '<dd class="' . esc_attr( $day['name'] ) . '">';
		$days_hours = '';

		foreach ( $day['hours'] as $key => $hour ) {
			$opening = strtotime( $hour['opening'] );
			$closing = strtotime( $hour['closing'] );
			if ( ! $opening || ! $closing ) {
				continue;
			}
			$days_hours .= sprintf(
				'%1$s - %2$s',
				date( $time_format, $opening ),
				date( $time_format, $closing )
			);
			if ( $key + 1 < count( $day['hours'] ) ) {
				$days_hours .= ', ';
			}
		}

		if ( empty( $days_hours ) ) {
			$days_hours = esc_html__( 'Closed', 'jetpack' );
		}
		$content .= $days_hours;
		$content .= '</dd></div>';
	}

	$content .= '</dl>';

	Jetpack_Gutenberg::load_assets_as_required( 'business-hours' );

	/**
	 * Allows folks to filter the HTML content for the Business Hours block
	 *
	 * @since 7.1.0
	 *
	 * @param string $content The default HTML content set by `jetpack_business_hours_render`
	 * @param array $attributes Attributes generated in the block editor for the Business Hours block
	 */
	return apply_filters( 'jetpack_business_hours_content', $content, $attributes );
}