← All changes
|
extensions/blocks/business-hours/business-hours.php
+9
-132
13.6.2
→
16.3
View file →
| @@ -9,10 +9,13 @@ | ||
| 9 | 9 | |
| 10 | 10 | namespace Automattic\Jetpack\Extensions\Business_Hours; |
| 11 | 11 | |
| 12 | 12 | use Automattic\Jetpack\Blocks; |
| 13 | -use Jetpack_Gutenberg; | |
| 14 | 13 | |
| 14 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 15 | + exit( 0 ); | |
| 16 | +} | |
| 17 | + | |
| 15 | 18 | /** |
| 16 | 19 | * Registers the block for use in Gutenberg |
| 17 | 20 | * This is done via an action so that we can disable |
| 18 | 21 | * registration if we need to. |
| @@ -27,72 +30,12 @@ | ||
| 27 | 30 | } |
| 28 | 31 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 29 | 32 | |
| 30 | 33 | /** |
| 31 | - * Get's default days / hours to render a business hour block with no data provided. | |
| 34 | + * Dynamic rendering of the block. | |
| 32 | 35 | * |
| 33 | - * @return array | |
| 34 | - */ | |
| 35 | -function get_default_days() { | |
| 36 | - return array( | |
| 37 | - array( | |
| 38 | - 'name' => 'Sun', | |
| 39 | - 'hours' => array(), | |
| 40 | - ), | |
| 41 | - array( | |
| 42 | - 'name' => 'Mon', | |
| 43 | - 'hours' => array( | |
| 44 | - array( | |
| 45 | - 'opening' => '09:00', | |
| 46 | - 'closing' => '17:00', | |
| 47 | - ), | |
| 48 | - ), | |
| 49 | - ), | |
| 50 | - array( | |
| 51 | - 'name' => 'Tue', | |
| 52 | - 'hours' => array( | |
| 53 | - array( | |
| 54 | - 'opening' => '09:00', | |
| 55 | - 'closing' => '17:00', | |
| 56 | - ), | |
| 57 | - ), | |
| 58 | - ), | |
| 59 | - array( | |
| 60 | - 'name' => 'Wed', | |
| 61 | - 'hours' => array( | |
| 62 | - array( | |
| 63 | - 'opening' => '09:00', | |
| 64 | - 'closing' => '17:00', | |
| 65 | - ), | |
| 66 | - ), | |
| 67 | - ), | |
| 68 | - array( | |
| 69 | - 'name' => 'Thu', | |
| 70 | - 'hours' => array( | |
| 71 | - array( | |
| 72 | - 'opening' => '09:00', | |
| 73 | - 'closing' => '17:00', | |
| 74 | - ), | |
| 75 | - ), | |
| 76 | - ), | |
| 77 | - array( | |
| 78 | - 'name' => 'Fri', | |
| 79 | - 'hours' => array( | |
| 80 | - array( | |
| 81 | - 'opening' => '09:00', | |
| 82 | - 'closing' => '17:00', | |
| 83 | - ), | |
| 84 | - ), | |
| 85 | - ), | |
| 86 | - array( | |
| 87 | - 'name' => 'Sat', | |
| 88 | - 'hours' => array(), | |
| 89 | - ), | |
| 90 | - ); | |
| 91 | -} | |
| 92 | - | |
| 93 | -/** | |
| 94 | - * Dynamic rendering of the block. | |
| 36 | + * The render implementation lives in render.php and is only loaded when the | |
| 37 | + * block is actually rendered, keeping it out of the eager front-end path. | |
| 95 | 38 | * |
| 96 | 39 | * @param array $attributes Array containing the business hours block attributes. |
| 97 | 40 | * |
| 98 | 41 | * @return string |
| @@ -97,73 +40,7 @@ | ||
| 97 | 40 | * |
| 98 | 41 | * @return string |
| 99 | 42 | */ |
| 100 | 43 | 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 | - $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports(); | |
| 108 | - | |
| 109 | - $start_of_week = (int) get_option( 'start_of_week', 0 ); | |
| 110 | - $time_format = get_option( 'time_format' ); | |
| 111 | - $content = sprintf( | |
| 112 | - '<dl class="jetpack-business-hours%s%s"%s>', | |
| 113 | - ! empty( $attributes['className'] ) ? ' ' . esc_attr( $attributes['className'] ) : '', | |
| 114 | - ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '', | |
| 115 | - ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '' | |
| 116 | - ); | |
| 117 | - | |
| 118 | - $days = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ); | |
| 119 | - | |
| 120 | - if ( $start_of_week ) { | |
| 121 | - $chunk1 = array_slice( $attributes['days'], 0, $start_of_week ); | |
| 122 | - $chunk2 = array_slice( $attributes['days'], $start_of_week ); | |
| 123 | - $attributes['days'] = array_merge( $chunk2, $chunk1 ); | |
| 124 | - } | |
| 125 | - | |
| 126 | - foreach ( $attributes['days'] as $day ) { | |
| 127 | - $content .= '<div class="jetpack-business-hours__item"><dt class="' . esc_attr( $day['name'] ) . '">' . | |
| 128 | - ucfirst( $wp_locale->get_weekday( array_search( $day['name'], $days, true ) ) ) . | |
| 129 | - '</dt>'; | |
| 130 | - $content .= '<dd class="' . esc_attr( $day['name'] ) . '">'; | |
| 131 | - $days_hours = ''; | |
| 132 | - | |
| 133 | - foreach ( $day['hours'] as $hour ) { | |
| 134 | - $opening = strtotime( $hour['opening'] ); | |
| 135 | - $closing = strtotime( $hour['closing'] ); | |
| 136 | - if ( ! $opening || ! $closing ) { | |
| 137 | - continue; | |
| 138 | - } | |
| 139 | - if ( $days_hours !== '' ) { | |
| 140 | - $days_hours .= ', '; | |
| 141 | - } | |
| 142 | - $days_hours .= sprintf( | |
| 143 | - '%1$s - %2$s', | |
| 144 | - gmdate( $time_format, $opening ), | |
| 145 | - gmdate( $time_format, $closing ) | |
| 146 | - ); | |
| 147 | - } | |
| 148 | - | |
| 149 | - if ( empty( $days_hours ) ) { | |
| 150 | - $days_hours = esc_html__( 'Closed', 'jetpack' ); | |
| 151 | - } | |
| 152 | - $content .= $days_hours; | |
| 153 | - $content .= '</dd></div>'; | |
| 154 | - } | |
| 155 | - | |
| 156 | - $content .= '</dl>'; | |
| 157 | - | |
| 158 | - Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); | |
| 159 | - | |
| 160 | - /** | |
| 161 | - * Allows folks to filter the HTML content for the Business Hours block | |
| 162 | - * | |
| 163 | - * @since 7.1.0 | |
| 164 | - * | |
| 165 | - * @param string $content The default HTML content set by `jetpack_business_hours_render` | |
| 166 | - * @param array $attributes Attributes generated in the block editor for the Business Hours block | |
| 167 | - */ | |
| 168 | - return apply_filters( 'jetpack_business_hours_content', $content, $attributes ); | |
| 44 | + require_once __DIR__ . '/render.php'; | |
| 45 | + return render_implementation( $attributes ); | |
| 169 | 46 | } |