| Conditions | 11 |
| Paths | 36 |
| Total Lines | 66 |
| 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 |
||
| 100 | function render( $attributes ) { |
||
| 101 | global $wp_locale; |
||
| 102 | |||
| 103 | if ( empty( $attributes['days'] ) || ! is_array( $attributes['days'] ) ) { |
||
| 104 | $attributes['days'] = get_default_days(); |
||
| 105 | } |
||
| 106 | |||
| 107 | $start_of_week = (int) get_option( 'start_of_week', 0 ); |
||
| 108 | $time_format = get_option( 'time_format' ); |
||
| 109 | $content = sprintf( |
||
| 110 | '<dl class="jetpack-business-hours %s">', |
||
| 111 | ! empty( $attributes['className'] ) ? esc_attr( $attributes['className'] ) : '' |
||
| 112 | ); |
||
| 113 | |||
| 114 | $days = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ); |
||
| 115 | |||
| 116 | if ( $start_of_week ) { |
||
| 117 | $chunk1 = array_slice( $attributes['days'], 0, $start_of_week ); |
||
| 118 | $chunk2 = array_slice( $attributes['days'], $start_of_week ); |
||
| 119 | $attributes['days'] = array_merge( $chunk2, $chunk1 ); |
||
| 120 | } |
||
| 121 | |||
| 122 | foreach ( $attributes['days'] as $day ) { |
||
| 123 | $content .= '<div class="jetpack-business-hours__item"><dt class="' . esc_attr( $day['name'] ) . '">' . |
||
| 124 | ucfirst( $wp_locale->get_weekday( array_search( $day['name'], $days, true ) ) ) . |
||
| 125 | '</dt>'; |
||
| 126 | $content .= '<dd class="' . esc_attr( $day['name'] ) . '">'; |
||
| 127 | $days_hours = ''; |
||
| 128 | |||
| 129 | foreach ( $day['hours'] as $key => $hour ) { |
||
| 130 | $opening = strtotime( $hour['opening'] ); |
||
| 131 | $closing = strtotime( $hour['closing'] ); |
||
| 132 | if ( ! $opening || ! $closing ) { |
||
| 133 | continue; |
||
| 134 | } |
||
| 135 | $days_hours .= sprintf( |
||
| 136 | '%1$s - %2$s', |
||
| 137 | gmdate( $time_format, $opening ), |
||
| 138 | gmdate( $time_format, $closing ) |
||
| 139 | ); |
||
| 140 | if ( $key + 1 < count( $day['hours'] ) ) { |
||
| 141 | $days_hours .= ', '; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | if ( empty( $days_hours ) ) { |
||
| 146 | $days_hours = esc_html__( 'Closed', 'jetpack' ); |
||
| 147 | } |
||
| 148 | $content .= $days_hours; |
||
| 149 | $content .= '</dd></div>'; |
||
| 150 | } |
||
| 151 | |||
| 152 | $content .= '</dl>'; |
||
| 153 | |||
| 154 | Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Allows folks to filter the HTML content for the Business Hours block |
||
| 158 | * |
||
| 159 | * @since 7.1.0 |
||
| 160 | * |
||
| 161 | * @param string $content The default HTML content set by `jetpack_business_hours_render` |
||
| 162 | * @param array $attributes Attributes generated in the block editor for the Business Hours block |
||
| 163 | */ |
||
| 164 | return apply_filters( 'jetpack_business_hours_content', $content, $attributes ); |
||
| 165 | } |
||
| 166 |