Completed
Push — upgrade/eslint-5 ( 071910...9f2520 )
by
unknown
07:52
created

business-hours.php ➔ jetpack_business_hours_render()   C

Complexity

Conditions 10
Paths 15

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 15
nop 2
dl 0
loc 63
rs 6.9406
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
jetpack_register_block(
11
	'jetpack/business-hours',
12
	array( 'render_callback' => 'jetpack_business_hours_render' )
13
);
14
15
/**
16
 * Dynamic rendering of the block.
17
 *
18
 * @param array  $attributes Array containing the business hours block attributes.
19
 * @param string $content    String containing the business hours block content.
20
 *
21
 * @return string
22
 */
23
function jetpack_business_hours_render( $attributes, $content ) {
24
	global $wp_locale;
25
26
	if ( empty( $attributes['days'] ) || ! is_array( $attributes['days'] ) ) {
27
		return $content;
28
	}
29
30
	$start_of_week = (int) get_option( 'start_of_week', 0 );
31
	$time_format   = get_option( 'time_format' );
32
	$content       = sprintf(
33
		'<dl class="jetpack-business-hours %s">',
34
		! empty( $attributes['className'] ) ? esc_attr( $attributes['className'] ) : ''
35
	);
36
37
	$days = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' );
38
39
	if ( $start_of_week ) {
40
		$chunk1             = array_slice( $attributes['days'], 0, $start_of_week );
41
		$chunk2             = array_slice( $attributes['days'], $start_of_week );
42
		$attributes['days'] = array_merge( $chunk2, $chunk1 );
43
	}
44
45
	foreach ( $attributes['days'] as $day ) {
46
		$content   .= '<dt class="' . esc_attr( $day['name'] ) . '">' .
47
					ucfirst( $wp_locale->get_weekday( array_search( $day['name'], $days, true ) ) ) .
48
					'</dt>';
49
		$content   .= '<dd class="' . esc_attr( $day['name'] ) . '">';
50
		$days_hours = '';
51
52
		foreach ( $day['hours'] as $hour ) {
53
			$opening     = strtotime( $hour['opening'] );
54
			$closing     = strtotime( $hour['closing'] );
55
			if ( ! $opening || ! $closing ) {
56
				continue;
57
			}
58
			$days_hours .= sprintf(
59
				/* Translators: Business opening hours info. */
60
				_x( 'From %1$s to %2$s', 'from business opening hour to closing hour', 'jetpack' ),
61
				date( $time_format, $opening ),
62
				date( $time_format, $closing )
63
			);
64
			$days_hours .= '<br />';
65
		}
66
67
		if ( empty( $days_hours ) ) {
68
			$days_hours = esc_html__( 'Closed', 'jetpack' );
69
		}
70
		$content .= $days_hours;
71
		$content .= '</dd>';
72
	}
73
74
	$content .= '</dl>';
75
76
	/**
77
	 * Allows folks to filter the HTML content for the Business Hours block
78
	 *
79
	 * @since 7.1.0
80
	 *
81
	 * @param string $content The default HTML content set by `jetpack_business_hours_render`
82
	 * @param array $attributes Attributes generated in the block editor for the Business Hours block
83
	 */
84
	return apply_filters( 'jetpack_business_hours_content', $content, $attributes );
85
}
86