Completed
Push — fix/business-hours-block-defau... ( 3f5a7a )
by
unknown
62:05 queued 55:10
created

business-hours.php ➔ jetpack_business_hours_get_default_days()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 42
rs 9.248
c 0
b 0
f 0
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
 * @param string $content    String containing the business hours block content.
0 ignored issues
show
Bug introduced by
There is no parameter named $content. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
68
 *
69
 * @return string
70
 */
71
function jetpack_business_hours_render( $attributes ) {
72
	global $wp_locale;
73
74
	if ( empty( $attributes['days'] ) || ! is_array( $attributes['days'] ) ) {
75
		$attributes['days'] = jetpack_business_hours_get_default_days();
76
	}
77
78
	$start_of_week = (int) get_option( 'start_of_week', 0 );
79
	$time_format   = get_option( 'time_format' );
80
	$content       = sprintf(
81
		'<dl class="jetpack-business-hours %s">',
82
		! empty( $attributes['className'] ) ? esc_attr( $attributes['className'] ) : ''
83
	);
84
85
	$days = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' );
86
87
	if ( $start_of_week ) {
88
		$chunk1             = array_slice( $attributes['days'], 0, $start_of_week );
89
		$chunk2             = array_slice( $attributes['days'], $start_of_week );
90
		$attributes['days'] = array_merge( $chunk2, $chunk1 );
91
	}
92
93
	foreach ( $attributes['days'] as $day ) {
94
		$content   .= '<dt class="' . esc_attr( $day['name'] ) . '">' .
95
					ucfirst( $wp_locale->get_weekday( array_search( $day['name'], $days, true ) ) ) .
96
					'</dt>';
97
		$content   .= '<dd class="' . esc_attr( $day['name'] ) . '">';
98
		$days_hours = '';
99
100
		foreach ( $day['hours'] as $hour ) {
101
			$opening     = strtotime( $hour['opening'] );
102
			$closing     = strtotime( $hour['closing'] );
103
			if ( ! $opening || ! $closing ) {
104
				continue;
105
			}
106
			$days_hours .= sprintf(
107
				/* Translators: Business opening hours info. */
108
				_x( 'From %1$s to %2$s', 'from business opening hour to closing hour', 'jetpack' ),
109
				date( $time_format, $opening ),
110
				date( $time_format, $closing )
111
			);
112
			$days_hours .= '<br />';
113
		}
114
115
		if ( empty( $days_hours ) ) {
116
			$days_hours = esc_html__( 'Closed', 'jetpack' );
117
		}
118
		$content .= $days_hours;
119
		$content .= '</dd>';
120
	}
121
122
	$content .= '</dl>';
123
124
	/**
125
	 * Allows folks to filter the HTML content for the Business Hours block
126
	 *
127
	 * @since 7.1.0
128
	 *
129
	 * @param string $content The default HTML content set by `jetpack_business_hours_render`
130
	 * @param array $attributes Attributes generated in the block editor for the Business Hours block
131
	 */
132
	return apply_filters( 'jetpack_business_hours_content', $content, $attributes );
133
}
134