Completed
Push — updates/infinity-vanilla-js ( fda899...9466ff )
by
unknown
07:55
created

business-hours.php ➔ jetpack_business_hours_render()   C

Complexity

Conditions 11
Paths 36

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 36
nop 1
dl 0
loc 66
rs 6.5951
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
namespace Automattic\Jetpack\Extensions\Business_Hours;
11
12
use Jetpack_Gutenberg;
13
14
const FEATURE_NAME = 'business-hours';
15
const BLOCK_NAME   = 'jetpack/' . FEATURE_NAME;
16
17
/**
18
 * Registers the block for use in Gutenberg
19
 * This is done via an action so that we can disable
20
 * registration if we need to.
21
 */
22
function register_block() {
23
	jetpack_register_block(
24
		BLOCK_NAME,
25
		array( 'render_callback' => __NAMESPACE__ . '\render' )
26
	);
27
}
28
add_action( 'init', __NAMESPACE__ . '\register_block' );
29
30
/**
31
 * Get's default days / hours to render a business hour block with no data provided.
32
 *
33
 * @return array
34
 */
35
function get_default_days() {
36
	return array(
37
		array(
38
			'name'  => 'Sun',
39
			'hours' => array(),
40
		),
41
		array(
42
			'name'  => 'Mon',
43
			'hours' => array(
44
				array(
45
					'opening' => '09:00',
46
					'closing' => '17:00',
47
				),
48
			),
49
		),
50
		array(
51
			'name'  => 'Tue',
52
			'hours' => array(
53
				array(
54
					'opening' => '09:00',
55
					'closing' => '17:00',
56
				),
57
			),
58
		),
59
		array(
60
			'name'  => 'Wed',
61
			'hours' => array(
62
				array(
63
					'opening' => '09:00',
64
					'closing' => '17:00',
65
				),
66
			),
67
		),
68
		array(
69
			'name'  => 'Thu',
70
			'hours' => array(
71
				array(
72
					'opening' => '09:00',
73
					'closing' => '17:00',
74
				),
75
			),
76
		),
77
		array(
78
			'name'  => 'Fri',
79
			'hours' => array(
80
				array(
81
					'opening' => '09:00',
82
					'closing' => '17:00',
83
				),
84
			),
85
		),
86
		array(
87
			'name'  => 'Sat',
88
			'hours' => array(),
89
		),
90
	);
91
}
92
93
/**
94
 * Dynamic rendering of the block.
95
 *
96
 * @param array $attributes Array containing the business hours block attributes.
97
 *
98
 * @return string
99
 */
100
function render( $attributes ) {
101
	global $wp_locale;
102
103
	if ( empty( $attributes['days'] ) || ! is_array( $attributes['days'] ) ) {
104
		$attributes['days'] = get_default_days();
105
	}
106
107
	$start_of_week = (int) get_option( 'start_of_week', 0 );
108
	$time_format   = get_option( 'time_format' );
109
	$content       = sprintf(
110
		'<dl class="jetpack-business-hours %s">',
111
		! empty( $attributes['className'] ) ? esc_attr( $attributes['className'] ) : ''
112
	);
113
114
	$days = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' );
115
116
	if ( $start_of_week ) {
117
		$chunk1             = array_slice( $attributes['days'], 0, $start_of_week );
118
		$chunk2             = array_slice( $attributes['days'], $start_of_week );
119
		$attributes['days'] = array_merge( $chunk2, $chunk1 );
120
	}
121
122
	foreach ( $attributes['days'] as $day ) {
123
		$content   .= '<div class="jetpack-business-hours__item"><dt class="' . esc_attr( $day['name'] ) . '">' .
124
					ucfirst( $wp_locale->get_weekday( array_search( $day['name'], $days, true ) ) ) .
125
					'</dt>';
126
		$content   .= '<dd class="' . esc_attr( $day['name'] ) . '">';
127
		$days_hours = '';
128
129
		foreach ( $day['hours'] as $key => $hour ) {
130
			$opening = strtotime( $hour['opening'] );
131
			$closing = strtotime( $hour['closing'] );
132
			if ( ! $opening || ! $closing ) {
133
				continue;
134
			}
135
			$days_hours .= sprintf(
136
				'%1$s - %2$s',
137
				gmdate( $time_format, $opening ),
138
				gmdate( $time_format, $closing )
139
			);
140
			if ( $key + 1 < count( $day['hours'] ) ) {
141
				$days_hours .= ', ';
142
			}
143
		}
144
145
		if ( empty( $days_hours ) ) {
146
			$days_hours = esc_html__( 'Closed', 'jetpack' );
147
		}
148
		$content .= $days_hours;
149
		$content .= '</dd></div>';
150
	}
151
152
	$content .= '</dl>';
153
154
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
155
156
	/**
157
	 * Allows folks to filter the HTML content for the Business Hours block
158
	 *
159
	 * @since 7.1.0
160
	 *
161
	 * @param string $content The default HTML content set by `jetpack_business_hours_render`
162
	 * @param array $attributes Attributes generated in the block editor for the Business Hours block
163
	 */
164
	return apply_filters( 'jetpack_business_hours_content', $content, $attributes );
165
}
166