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