Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
40.82% covered (danger)
40.82%
40 / 98
0.00% covered (danger)
0.00%
0 / 2
CRAP
n/a
0 / 0
Automattic\Jetpack\Extensions\Business_Hours\get_default_days
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 1
2
Automattic\Jetpack\Extensions\Business_Hours\render_implementation
95.12% covered (success)
95.12%
39 / 41
0.00% covered (danger)
0.00%
0 / 1
13
1<?php
2/**
3 * Business Hours block render implementation.
4 *
5 * Loaded lazily from business-hours.php only when the block is rendered, to keep
6 * the render body out of the eager front-end PHP/opcache footprint.
7 *
8 * @package automattic/jetpack
9 */
10
11namespace Automattic\Jetpack\Extensions\Business_Hours;
12
13use Jetpack_Gutenberg;
14
15if ( ! defined( 'ABSPATH' ) ) {
16    exit( 0 );
17}
18
19/**
20 * Get's default days / hours to render a business hour block with no data provided.
21 *
22 * @return array
23 */
24function get_default_days() {
25    return array(
26        array(
27            'name'  => 'Sun',
28            'hours' => array(),
29        ),
30        array(
31            'name'  => 'Mon',
32            'hours' => array(
33                array(
34                    'opening' => '09:00',
35                    'closing' => '17:00',
36                ),
37            ),
38        ),
39        array(
40            'name'  => 'Tue',
41            'hours' => array(
42                array(
43                    'opening' => '09:00',
44                    'closing' => '17:00',
45                ),
46            ),
47        ),
48        array(
49            'name'  => 'Wed',
50            'hours' => array(
51                array(
52                    'opening' => '09:00',
53                    'closing' => '17:00',
54                ),
55            ),
56        ),
57        array(
58            'name'  => 'Thu',
59            'hours' => array(
60                array(
61                    'opening' => '09:00',
62                    'closing' => '17:00',
63                ),
64            ),
65        ),
66        array(
67            'name'  => 'Fri',
68            'hours' => array(
69                array(
70                    'opening' => '09:00',
71                    'closing' => '17:00',
72                ),
73            ),
74        ),
75        array(
76            'name'  => 'Sat',
77            'hours' => array(),
78        ),
79    );
80}
81
82/**
83 * Dynamic rendering of the block.
84 *
85 * @param array $attributes Array containing the business hours block attributes.
86 *
87 * @return string
88 */
89function render_implementation( $attributes ) {
90    global $wp_locale;
91
92    if ( empty( $attributes['days'] ) || ! is_array( $attributes['days'] ) ) {
93        $attributes['days'] = get_default_days();
94    }
95
96    $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
97
98    $start_of_week = (int) get_option( 'start_of_week', 0 );
99    $time_format   = get_option( 'time_format' );
100    $content       = sprintf(
101        '<dl class="jetpack-business-hours%s%s"%s>',
102        ! empty( $attributes['className'] ) ? ' ' . esc_attr( $attributes['className'] ) : '',
103        ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '',
104        ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : ''
105    );
106
107    $days = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' );
108
109    if ( $start_of_week ) {
110        $chunk1             = array_slice( $attributes['days'], 0, $start_of_week );
111        $chunk2             = array_slice( $attributes['days'], $start_of_week );
112        $attributes['days'] = array_merge( $chunk2, $chunk1 );
113    }
114
115    foreach ( $attributes['days'] as $day ) {
116        $content   .= '<div class="jetpack-business-hours__item"><dt class="' . esc_attr( $day['name'] ) . '">' .
117                    ucfirst( $wp_locale->get_weekday( array_search( $day['name'], $days, true ) ) ) .
118                    '</dt>';
119        $content   .= '<dd class="' . esc_attr( $day['name'] ) . '">';
120        $days_hours = '';
121
122        foreach ( $day['hours'] as $hour ) {
123            $opening = strtotime( $hour['opening'] );
124            $closing = strtotime( $hour['closing'] );
125            if ( ! $opening || ! $closing ) {
126                continue;
127            }
128            if ( $days_hours !== '' ) {
129                $days_hours .= ', ';
130            }
131            $days_hours .= sprintf(
132                '%1$s - %2$s',
133                gmdate( $time_format, $opening ),
134                gmdate( $time_format, $closing )
135            );
136        }
137
138        if ( empty( $days_hours ) ) {
139            $days_hours = esc_html__( 'Closed', 'jetpack' );
140        }
141        $content .= $days_hours;
142        $content .= '</dd></div>';
143    }
144
145    $content .= '</dl>';
146
147    Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
148
149    /**
150     * Allows folks to filter the HTML content for the Business Hours block
151     *
152     * @since 7.1.0
153     *
154     * @param string $content The default HTML content set by `jetpack_business_hours_render`
155     * @param array $attributes Attributes generated in the block editor for the Business Hours block
156     */
157    return apply_filters( 'jetpack_business_hours_content', $content, $attributes );
158}