Completed
Push — update/masterbar-rm-left-nav ( 7930b3...ec0ec1 )
by Jeremy
13:33 queued 06:04
created

business-hours.php ➔ jetpack_business_hours_get_default_days()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 57
rs 8.9381
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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(
30
					'opening' => '09:00',
31
					'closing' => '17:00',
32
				),
33
			),
34
		),
35
		array(
36
			'name'  => 'Tue',
37
			'hours' => array(
38
				array(
39
					'opening' => '09:00',
40
					'closing' => '17:00',
41
				),
42
			),
43
		),
44
		array(
45
			'name'  => 'Wed',
46
			'hours' => array(
47
				array(
48
					'opening' => '09:00',
49
					'closing' => '17:00',
50
				),
51
			),
52
		),
53
		array(
54
			'name'  => 'Thu',
55
			'hours' => array(
56
				array(
57
					'opening' => '09:00',
58
					'closing' => '17:00',
59
				),
60
			),
61
		),
62
		array(
63
			'name'  => 'Fri',
64
			'hours' => array(
65
				array(
66
					'opening' => '09:00',
67
					'closing' => '17:00',
68
				),
69
			),
70
		),
71
		array(
72
			'name'  => 'Sat',
73
			'hours' => array(),
74
		),
75
	);
76
}
77
78
/**
79
 * Dynamic rendering of the block.
80
 *
81
 * @param array $attributes Array containing the business hours block attributes.
82
 *
83
 * @return string
84
 */
85
function jetpack_business_hours_render( $attributes ) {
86
	global $wp_locale;
87
88
	if ( empty( $attributes['days'] ) || ! is_array( $attributes['days'] ) ) {
89
		$attributes['days'] = jetpack_business_hours_get_default_days();
90
	}
91
92
	$start_of_week = (int) get_option( 'start_of_week', 0 );
93
	$time_format   = get_option( 'time_format' );
94
	$content       = sprintf(
95
		'<dl class="jetpack-business-hours %s">',
96
		! empty( $attributes['className'] ) ? esc_attr( $attributes['className'] ) : ''
97
	);
98
99
	$days = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' );
100
101
	if ( $start_of_week ) {
102
		$chunk1             = array_slice( $attributes['days'], 0, $start_of_week );
103
		$chunk2             = array_slice( $attributes['days'], $start_of_week );
104
		$attributes['days'] = array_merge( $chunk2, $chunk1 );
105
	}
106
107
	foreach ( $attributes['days'] as $day ) {
108
		$content   .= '<dt class="' . esc_attr( $day['name'] ) . '">' .
109
					ucfirst( $wp_locale->get_weekday( array_search( $day['name'], $days, true ) ) ) .
110
					'</dt>';
111
		$content   .= '<dd class="' . esc_attr( $day['name'] ) . '">';
112
		$days_hours = '';
113
114
		foreach ( $day['hours'] as $hour ) {
115
			$opening = strtotime( $hour['opening'] );
116
			$closing = strtotime( $hour['closing'] );
117
			if ( ! $opening || ! $closing ) {
118
				continue;
119
			}
120
			$days_hours .= sprintf(
121
				/* Translators: Business opening hours info. */
122
				_x( 'From %1$s to %2$s', 'from business opening hour to closing hour', 'jetpack' ),
123
				date( $time_format, $opening ),
124
				date( $time_format, $closing )
125
			);
126
			$days_hours .= '<br />';
127
		}
128
129
		if ( empty( $days_hours ) ) {
130
			$days_hours = esc_html__( 'Closed', 'jetpack' );
131
		}
132
		$content .= $days_hours;
133
		$content .= '</dd>';
134
	}
135
136
	$content .= '</dl>';
137
138
	/**
139
	 * Allows folks to filter the HTML content for the Business Hours block
140
	 *
141
	 * @since 7.1.0
142
	 *
143
	 * @param string $content The default HTML content set by `jetpack_business_hours_render`
144
	 * @param array $attributes Attributes generated in the block editor for the Business Hours block
145
	 */
146
	return apply_filters( 'jetpack_business_hours_content', $content, $attributes );
147
}
148