Completed
Push — fix/update-likes-and-sharing-a... ( b29dbf...9ec4fe )
by
unknown
09:36
created

business-hours.php ➔ jetpack_business_hours_render()   C

Complexity

Conditions 14
Paths 27

Size

Total Lines 87

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
nc 27
nop 2
dl 0
loc 87
rs 5.2969
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['hours'] ) || ! is_array( $attributes['hours'] ) ) {
27
		return $content;
28
	}
29
30
	$start_of_week = (int) get_option( 'start_of_week', 0 );
31
	$time_format   = get_option( 'time_format' );
32
	$today         = current_time( 'D' );
33
	$content       = sprintf(
34
		'<dl class="jetpack-business-hours %s">',
35
		! empty( $attributes['className'] ) ? esc_attr( $attributes['className'] ) : ''
36
	);
37
38
	$days = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' );
39
40
	if ( $start_of_week ) {
41
		$chunk1              = array_slice( $attributes['hours'], 0, $start_of_week );
42
		$chunk2              = array_slice( $attributes['hours'], $start_of_week );
43
		$attributes['hours'] = array_merge( $chunk2, $chunk1 );
44
	}
45
46
	foreach ( $attributes['hours'] as $day => $hours ) {
47
		$content   .= '<dt class="' . esc_attr( $day ) . '">' .
48
					ucfirst( $wp_locale->get_weekday( array_search( $day, $days, true ) ) ) .
49
					'</dt>';
50
		$content   .= '<dd class="' . esc_attr( $day ) . '">';
51
		$days_hours = '';
52
53
		foreach ( $hours as $hour ) {
54
			if ( empty( $hour['opening'] ) || empty( $hour['closing'] ) ) {
55
				continue;
56
			}
57
			$opening     = strtotime( $hour['opening'] );
58
			$closing     = strtotime( $hour['closing'] );
59
			$days_hours .= sprintf(
60
				/* Translators: Business opening hours info. */
61
				_x( 'From %1$s to %2$s', 'from business opening hour to closing hour', 'jetpack' ),
62
				date( $time_format, $opening ),
63
				date( $time_format, $closing )
64
			);
65
66
			if ( $today === $day ) {
67
				$now = strtotime( current_time( 'H:i' ) );
68
				if ( $now < $opening ) {
69
					$days_hours .= '<br />';
70
					$days_hours .= esc_html(
71
						sprintf(
72
							/* Translators: Amount of time until business opens. */
73
							_x( 'Opening in %s', 'Amount of time until business opens', 'jetpack' ),
74
							human_time_diff( $now, $opening )
75
						)
76
					);
77
				} elseif ( $now >= $opening && $now < $closing ) {
78
					$days_hours .= '<br />';
79
					$days_hours .= esc_html(
80
						sprintf(
81
							/* Translators: Amount of time until business closes. */
82
							_x( 'Closing in %s', 'Amount of time until business closes', 'jetpack' ),
83
							human_time_diff( $now, $closing )
84
						)
85
					);
86
				}
87
			}
88
			$days_hours .= '<br />';
89
		}
90
91
		if ( empty( $days_hours ) ) {
92
			$days_hours = esc_html__( 'Closed', 'jetpack' );
93
		}
94
		$content .= $days_hours;
95
		$content .= '</dd>';
96
	}
97
98
	$content .= '</dl>';
99
100
	/**
101
	 * Allows folks to filter the HTML content for the Business Hours block
102
	 *
103
	 * @since 7.1.0
104
	 *
105
	 * @param string $content The default HTML content set by `jetpack_business_hours_render`
106
	 * @param array $attributes Attributes generated in the block editor for the Business Hours block
107
	 */
108
	return apply_filters( 'jetpack_business_hours_content', $content, $attributes );
109
}
110