| Conditions | 14 |
| Paths | 27 |
| Total Lines | 87 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 |