Completed
Push — update/linkedin-apiv2 ( c98f48...1ce83e )
by
unknown
06:58
created

business-hours.php ➔ jetpack_business_hours_render()   C

Complexity

Conditions 14
Paths 27

Size

Total Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
nc 27
nop 2
dl 0
loc 75
rs 5.5587
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_type(
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 ) ) ) .
49
					   '</dt>';
50
		$content    .= '<dd class="' . esc_attr( $day ) . '">';
51
		$days_hours = '';
52
53
		foreach ( $hours as $hour ) {
54
			if ( ! $hour['opening'] && $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( sprintf(
71
					/* Translators: Amount of time until business opens. */
72
						_x( 'Opening in %s', 'Amount of time until business opens', 'jetpack' ),
73
						human_time_diff( $now, $opening )
74
					) );
75
				} elseif ( $now >= $opening && $now < $closing ) {
76
					$days_hours .= '<br />';
77
					$days_hours .= esc_html( sprintf(
78
					/* Translators: Amount of time until business closes. */
79
						_x( 'Closing in %s', 'Amount of time until business closes', 'jetpack' ),
80
						human_time_diff( $now, $closing )
81
					) );
82
				}
83
			}
84
			$days_hours .= '<br />';
85
		}
86
87
		if ( empty( $days_hours ) ) {
88
			$days_hours = esc_html__( 'CLOSED', 'jetpack' );
89
		}
90
		$content .= $days_hours;
91
		$content .= '</dd>';
92
	}
93
94
	$content .= '</dl>';
95
96
	return $content;
97
}
98