Нема описа

business-hours.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Business Hours Block.
  4. *
  5. * @since 7.1.0
  6. *
  7. * @package automattic/jetpack
  8. */
  9. namespace Automattic\Jetpack\Extensions\Business_Hours;
  10. use Automattic\Jetpack\Blocks;
  11. use Jetpack_Gutenberg;
  12. const FEATURE_NAME = 'business-hours';
  13. const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;
  14. /**
  15. * Registers the block for use in Gutenberg
  16. * This is done via an action so that we can disable
  17. * registration if we need to.
  18. */
  19. function register_block() {
  20. Blocks::jetpack_register_block(
  21. BLOCK_NAME,
  22. array( 'render_callback' => __NAMESPACE__ . '\render' )
  23. );
  24. }
  25. add_action( 'init', __NAMESPACE__ . '\register_block' );
  26. /**
  27. * Get's default days / hours to render a business hour block with no data provided.
  28. *
  29. * @return array
  30. */
  31. function get_default_days() {
  32. return array(
  33. array(
  34. 'name' => 'Sun',
  35. 'hours' => array(),
  36. ),
  37. array(
  38. 'name' => 'Mon',
  39. 'hours' => array(
  40. array(
  41. 'opening' => '09:00',
  42. 'closing' => '17:00',
  43. ),
  44. ),
  45. ),
  46. array(
  47. 'name' => 'Tue',
  48. 'hours' => array(
  49. array(
  50. 'opening' => '09:00',
  51. 'closing' => '17:00',
  52. ),
  53. ),
  54. ),
  55. array(
  56. 'name' => 'Wed',
  57. 'hours' => array(
  58. array(
  59. 'opening' => '09:00',
  60. 'closing' => '17:00',
  61. ),
  62. ),
  63. ),
  64. array(
  65. 'name' => 'Thu',
  66. 'hours' => array(
  67. array(
  68. 'opening' => '09:00',
  69. 'closing' => '17:00',
  70. ),
  71. ),
  72. ),
  73. array(
  74. 'name' => 'Fri',
  75. 'hours' => array(
  76. array(
  77. 'opening' => '09:00',
  78. 'closing' => '17:00',
  79. ),
  80. ),
  81. ),
  82. array(
  83. 'name' => 'Sat',
  84. 'hours' => array(),
  85. ),
  86. );
  87. }
  88. /**
  89. * Dynamic rendering of the block.
  90. *
  91. * @param array $attributes Array containing the business hours block attributes.
  92. *
  93. * @return string
  94. */
  95. function render( $attributes ) {
  96. global $wp_locale;
  97. if ( empty( $attributes['days'] ) || ! is_array( $attributes['days'] ) ) {
  98. $attributes['days'] = get_default_days();
  99. }
  100. $start_of_week = (int) get_option( 'start_of_week', 0 );
  101. $time_format = get_option( 'time_format' );
  102. $content = sprintf(
  103. '<dl class="jetpack-business-hours %s">',
  104. ! empty( $attributes['className'] ) ? esc_attr( $attributes['className'] ) : ''
  105. );
  106. $days = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' );
  107. if ( $start_of_week ) {
  108. $chunk1 = array_slice( $attributes['days'], 0, $start_of_week );
  109. $chunk2 = array_slice( $attributes['days'], $start_of_week );
  110. $attributes['days'] = array_merge( $chunk2, $chunk1 );
  111. }
  112. foreach ( $attributes['days'] as $day ) {
  113. $content .= '<div class="jetpack-business-hours__item"><dt class="' . esc_attr( $day['name'] ) . '">' .
  114. ucfirst( $wp_locale->get_weekday( array_search( $day['name'], $days, true ) ) ) .
  115. '</dt>';
  116. $content .= '<dd class="' . esc_attr( $day['name'] ) . '">';
  117. $days_hours = '';
  118. foreach ( $day['hours'] as $key => $hour ) {
  119. $opening = strtotime( $hour['opening'] );
  120. $closing = strtotime( $hour['closing'] );
  121. if ( ! $opening || ! $closing ) {
  122. continue;
  123. }
  124. $days_hours .= sprintf(
  125. '%1$s - %2$s',
  126. gmdate( $time_format, $opening ),
  127. gmdate( $time_format, $closing )
  128. );
  129. if ( $key + 1 < count( $day['hours'] ) ) {
  130. $days_hours .= ', ';
  131. }
  132. }
  133. if ( empty( $days_hours ) ) {
  134. $days_hours = esc_html__( 'Closed', 'jetpack' );
  135. }
  136. $content .= $days_hours;
  137. $content .= '</dd></div>';
  138. }
  139. $content .= '</dl>';
  140. Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
  141. /**
  142. * Allows folks to filter the HTML content for the Business Hours block
  143. *
  144. * @since 7.1.0
  145. *
  146. * @param string $content The default HTML content set by `jetpack_business_hours_render`
  147. * @param array $attributes Attributes generated in the block editor for the Business Hours block
  148. */
  149. return apply_filters( 'jetpack_business_hours_content', $content, $attributes );
  150. }