PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 14.9.2
Jetpack – WP Security, Backup, Speed, & Growth v14.9.2
12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 14.4.2 All 500 releases
jetpack / extensions / blocks / business-hours / business-hours.php
business-hours.php
174 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Business Hours Block.
4 *
5 * @since 7.1.0
6 *
7 * @package automattic/jetpack
8 */
9
10 namespace Automattic\Jetpack\Extensions\Business_Hours;
11
12 use Automattic\Jetpack\Blocks;
13 use Jetpack_Gutenberg;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit( 0 );
17 }
18
19 /**
20 * Registers the block for use in Gutenberg
21 * This is done via an action so that we can disable
22 * registration if we need to.
23 */
24 function register_block() {
25 Blocks::jetpack_register_block(
26 __DIR__,
27 array(
28 'render_callback' => __NAMESPACE__ . '\render',
29 )
30 );
31 }
32 add_action( 'init', __NAMESPACE__ . '\register_block' );
33
34 /**
35 * Get's default days / hours to render a business hour block with no data provided.
36 *
37 * @return array
38 */
39 function get_default_days() {
40 return array(
41 array(
42 'name' => 'Sun',
43 'hours' => array(),
44 ),
45 array(
46 'name' => 'Mon',
47 'hours' => array(
48 array(
49 'opening' => '09:00',
50 'closing' => '17:00',
51 ),
52 ),
53 ),
54 array(
55 'name' => 'Tue',
56 'hours' => array(
57 array(
58 'opening' => '09:00',
59 'closing' => '17:00',
60 ),
61 ),
62 ),
63 array(
64 'name' => 'Wed',
65 'hours' => array(
66 array(
67 'opening' => '09:00',
68 'closing' => '17:00',
69 ),
70 ),
71 ),
72 array(
73 'name' => 'Thu',
74 'hours' => array(
75 array(
76 'opening' => '09:00',
77 'closing' => '17:00',
78 ),
79 ),
80 ),
81 array(
82 'name' => 'Fri',
83 'hours' => array(
84 array(
85 'opening' => '09:00',
86 'closing' => '17:00',
87 ),
88 ),
89 ),
90 array(
91 'name' => 'Sat',
92 'hours' => array(),
93 ),
94 );
95 }
96
97 /**
98 * Dynamic rendering of the block.
99 *
100 * @param array $attributes Array containing the business hours block attributes.
101 *
102 * @return string
103 */
104 function render( $attributes ) {
105 global $wp_locale;
106
107 if ( empty( $attributes['days'] ) || ! is_array( $attributes['days'] ) ) {
108 $attributes['days'] = get_default_days();
109 }
110
111 $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
112
113 $start_of_week = (int) get_option( 'start_of_week', 0 );
114 $time_format = get_option( 'time_format' );
115 $content = sprintf(
116 '<dl class="jetpack-business-hours%s%s"%s>',
117 ! empty( $attributes['className'] ) ? ' ' . esc_attr( $attributes['className'] ) : '',
118 ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '',
119 ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : ''
120 );
121
122 $days = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' );
123
124 if ( $start_of_week ) {
125 $chunk1 = array_slice( $attributes['days'], 0, $start_of_week );
126 $chunk2 = array_slice( $attributes['days'], $start_of_week );
127 $attributes['days'] = array_merge( $chunk2, $chunk1 );
128 }
129
130 foreach ( $attributes['days'] as $day ) {
131 $content .= '<div class="jetpack-business-hours__item"><dt class="' . esc_attr( $day['name'] ) . '">' .
132 ucfirst( $wp_locale->get_weekday( array_search( $day['name'], $days, true ) ) ) .
133 '</dt>';
134 $content .= '<dd class="' . esc_attr( $day['name'] ) . '">';
135 $days_hours = '';
136
137 foreach ( $day['hours'] as $hour ) {
138 $opening = strtotime( $hour['opening'] );
139 $closing = strtotime( $hour['closing'] );
140 if ( ! $opening || ! $closing ) {
141 continue;
142 }
143 if ( $days_hours !== '' ) {
144 $days_hours .= ', ';
145 }
146 $days_hours .= sprintf(
147 '%1$s - %2$s',
148 gmdate( $time_format, $opening ),
149 gmdate( $time_format, $closing )
150 );
151 }
152
153 if ( empty( $days_hours ) ) {
154 $days_hours = esc_html__( 'Closed', 'jetpack' );
155 }
156 $content .= $days_hours;
157 $content .= '</dd></div>';
158 }
159
160 $content .= '</dl>';
161
162 Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
163
164 /**
165 * Allows folks to filter the HTML content for the Business Hours block
166 *
167 * @since 7.1.0
168 *
169 * @param string $content The default HTML content set by `jetpack_business_hours_render`
170 * @param array $attributes Attributes generated in the block editor for the Business Hours block
171 */
172 return apply_filters( 'jetpack_business_hours_content', $content, $attributes );
173 }
174