| 1 |
<?php |
| 2 |
/** |
| 3 |
* Module: Timings Blocks. |
| 4 |
* |
| 5 |
* @package Orderable/Classes |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Layouts blocks class. |
| 12 |
*/ |
| 13 |
class Orderable_Timings_Blocks { |
| 14 |
/** |
| 15 |
* Init. |
| 16 |
*/ |
| 17 |
public static function run() { |
| 18 |
add_action( 'init', array( __CLASS__, 'register_blocks' ) ); |
| 19 |
|
| 20 |
add_filter( 'orderable_upcoming_open_hours', array( __CLASS__, 'check_services_enabled' ), 20, 2 ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Register blocks. |
| 25 |
*/ |
| 26 |
public static function register_blocks() { |
| 27 |
if ( ! function_exists( 'register_block_type' ) ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
wp_register_script( |
| 32 |
'orderable-timings', |
| 33 |
ORDERABLE_URL . 'inc/modules/timings/assets/admin/js/block-timings.js', |
| 34 |
array( |
| 35 |
'wp-blocks', |
| 36 |
'wp-i18n', |
| 37 |
'wp-element', |
| 38 |
'wp-components', |
| 39 |
'wp-editor', |
| 40 |
), |
| 41 |
ORDERABLE_VERSION, |
| 42 |
array( |
| 43 |
'in_footer' => false, |
| 44 |
) |
| 45 |
); |
| 46 |
|
| 47 |
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 48 |
$suffix_css = ( is_rtl() ? '-rtl' : '' ) . $suffix; |
| 49 |
|
| 50 |
wp_enqueue_style( 'orderable-timings-admin', ORDERABLE_URL . 'inc/modules/timings/assets/admin/css/timings' . $suffix_css . '.css', array(), ORDERABLE_VERSION ); |
| 51 |
|
| 52 |
$locations = Orderable_Location::store_has_multi_locations() ? Orderable_Multi_Location_Pro_Helper::get_all_locations() : array(); |
| 53 |
|
| 54 |
$locations = array_filter( |
| 55 |
array_map( |
| 56 |
function( $location ) { |
| 57 |
if ( empty( $location->location_data['location_id'] ) ) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
return array( |
| 62 |
'label' => sprintf( |
| 63 |
// translators: %1$s - location name, %2$d - location ID. |
| 64 |
__( '%1$s (ID: %2$d)', 'orderable' ), |
| 65 |
$location->location_data['title'], |
| 66 |
$location->location_data['location_id'] |
| 67 |
), |
| 68 |
'value' => $location->location_data['location_id'], |
| 69 |
); |
| 70 |
}, |
| 71 |
$locations |
| 72 |
) |
| 73 |
); |
| 74 |
|
| 75 |
wp_localize_script( |
| 76 |
'orderable-timings', |
| 77 |
'orderable_timings_block_vars', |
| 78 |
array( |
| 79 |
'admin_url' => get_admin_url(), |
| 80 |
'locations' => $locations, |
| 81 |
) |
| 82 |
); |
| 83 |
|
| 84 |
register_block_type( |
| 85 |
'orderable/open-hours', |
| 86 |
array( |
| 87 |
'editor_script' => 'orderable-timings', |
| 88 |
'render_callback' => array( __CLASS__, 'open_hours_block_handler' ), |
| 89 |
'attributes' => array( |
| 90 |
'location_id' => array( |
| 91 |
'default' => '', |
| 92 |
'type' => 'string', |
| 93 |
), |
| 94 |
), |
| 95 |
) |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Handle block: Layout. |
| 101 |
* |
| 102 |
* @param array $attributes Block attributes. |
| 103 |
* @return string |
| 104 |
*/ |
| 105 |
public static function open_hours_block_handler( $attributes = array() ) { |
| 106 |
return Orderable_Timings::orderable_open_hours_shortcode( $attributes ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Check if the services are enabled for the open hours days. |
| 111 |
* |
| 112 |
* Since we use the the [orderable-open-hours] to render the |
| 113 |
* block and the shortcode has a different behaviour when we |
| 114 |
* try to render it on the block editor (admin), we need to |
| 115 |
* check again to get the correct services enabled. |
| 116 |
* |
| 117 |
* @param array $open_hours Location upcoming open hours. |
| 118 |
* @param Orderable_Location_Single $location Location object. |
| 119 |
* @return array |
| 120 |
*/ |
| 121 |
public static function check_services_enabled( $open_hours, $location ) { |
| 122 |
$is_delivery_enabled = empty( $location->location_data['delivery'] ) ? false : $location->location_data['delivery']; |
| 123 |
$is_pickup_enabled = empty( $location->location_data['pickup'] ) ? false : $location->location_data['pickup']; |
| 124 |
|
| 125 |
if ( ! $is_delivery_enabled && ! $is_pickup_enabled ) { |
| 126 |
return $open_hours; |
| 127 |
} |
| 128 |
|
| 129 |
$is_pickup_hours_same_as_delivery = (bool) $location->location_data['pickup_hours_same_as_delivery']; |
| 130 |
|
| 131 |
if ( $is_delivery_enabled ) { |
| 132 |
$service_hours_delivery = $location->get_service_hours( 'delivery', true, true ); |
| 133 |
$service_hours_delivery_days = empty( $service_hours_delivery[0]['days'] ) ? array() : $service_hours_delivery[0]['days']; |
| 134 |
} |
| 135 |
|
| 136 |
foreach ( $service_hours_delivery_days as $day ) { |
| 137 |
$day = (int) $day; |
| 138 |
if ( empty( $open_hours[ $day ] ) ) { |
| 139 |
continue; |
| 140 |
} |
| 141 |
|
| 142 |
$open_hours[ $day ]['services']['delivery'] = (bool) $is_delivery_enabled; |
| 143 |
|
| 144 |
if ( ! $is_pickup_hours_same_as_delivery ) { |
| 145 |
continue; |
| 146 |
} |
| 147 |
|
| 148 |
$open_hours[ $day ]['services']['pickup'] = (bool) $is_pickup_enabled; |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
if ( ! $is_pickup_enabled || $is_pickup_hours_same_as_delivery ) { |
| 153 |
return $open_hours; |
| 154 |
} |
| 155 |
|
| 156 |
$service_hours_pickup = $location->get_service_hours( 'pickup', true, true ); |
| 157 |
$service_hours_pickup_days = empty( $service_hours_pickup[0]['days'] ) ? array() : $service_hours_pickup[0]['days']; |
| 158 |
|
| 159 |
foreach ( $service_hours_pickup_days as $day ) { |
| 160 |
$day = (int) $day; |
| 161 |
if ( empty( $open_hours[ $day ] ) ) { |
| 162 |
continue; |
| 163 |
} |
| 164 |
|
| 165 |
$open_hours[ $day ]['services']['pickup'] = (bool) $is_pickup_enabled; |
| 166 |
} |
| 167 |
|
| 168 |
return $open_hours; |
| 169 |
} |
| 170 |
} |
| 171 |
|