| Conditions | 10 |
| Paths | 15 |
| Total Lines | 63 |
| 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['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 |