-
Notifications
You must be signed in to change notification settings - Fork 1
/
random-blocks.php
206 lines (174 loc) · 5.53 KB
/
random-blocks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/**
* Plugin Name: Random Blocks
* Plugin URI: https://github.com/georgestephanis/random-blocks/
* Description: Some random blocks for the new WordPress Block-based Editor
* Version: 0.1
* Author: George Stephanis
* Author URI: https://stephanis.info/
* License: GPLv2+
* Text Domain: random-blocks
*/
// Address
add_action( 'init', 'register_address_block' );
function register_address_block() {
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
wp_register_style( 'address-block', plugins_url( 'address/address.css', __FILE__ ) );
wp_register_script(
'address-block',
plugins_url( 'address/address.js', __FILE__ ),
array(
'wp-blocks',
'wp-components',
'wp-i18n',
)
);
if ( function_exists( 'wp_set_script_translations' ) ) {
wp_set_script_translations( 'address-block', 'random-blocks' );
}
register_block_type( 'random-blocks/address', array(
'editor_script' => 'address-block',
'style' => 'address-block',
) );
}
// Business Hours
add_action( 'init', 'register_business_hours_block' );
function register_business_hours_block() {
global $wp_locale;
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
wp_register_script(
'business-hours',
plugins_url( 'business-hours/business-hours.js', __FILE__ ),
array(
'wp-blocks',
'wp-components',
'wp-element',
'wp-i18n',
)
);
if ( function_exists( 'wp_set_script_translations' ) ) {
wp_set_script_translations( 'business-hours', 'random-blocks' );
}
wp_localize_script( 'business-hours', 'businessHours', array(
'days' => array(
'Sun' => $wp_locale->get_weekday( 0 ),
'Mon' => $wp_locale->get_weekday( 1 ),
'Tue' => $wp_locale->get_weekday( 2 ),
'Wed' => $wp_locale->get_weekday( 3 ),
'Thu' => $wp_locale->get_weekday( 4 ),
'Fri' => $wp_locale->get_weekday( 5 ),
'Sat' => $wp_locale->get_weekday( 6 ),
),
'start_of_week' => (int) get_option( 'start_of_week', 0 ),
) );
wp_register_style( 'business-hours', plugins_url( 'business-hours/business-hours.css', __FILE__ ) );
if ( ! is_admin() ) {
$inline_style = '.business-hours .' . current_time( 'D' ) . ' { font-weight: 900; }';
wp_add_inline_style( 'business-hours', $inline_style );
}
register_block_type( 'random-blocks/business-hours', array(
'editor_script' => 'business-hours',
'style' => 'business-hours',
'render_callback' => 'render_business_hours_block',
) );
}
function render_business_hours_block( $attributes, $content ) {
global $wp_locale;
if ( empty( $attributes['hours'] ) || ! is_array( $attributes['hours'] ) ) {
return $content;
}
$start_of_week = (int) get_option( 'start_of_week', 0 );
$time_format = get_option( 'time_format' );
$today = current_time( 'D' );
$content = '<dl class="business-hours built-by-php">';
$days = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' );
if ( $start_of_week ) {
$chunk1 = array_slice( $attributes['hours'], 0, $start_of_week );
$chunk2 = array_slice( $attributes['hours'], $start_of_week );
$attributes['hours'] = array_merge( $chunk2, $chunk1 );
}
foreach ( $attributes['hours'] as $day => $hours ) {
$opening = strtotime( $hours['opening'] );
$closing = strtotime( $hours['closing'] );
$content .= '<dt class="' . esc_attr( $day ) . '">' . $wp_locale->get_weekday( array_search( $day, $days ) ) . '</dt>';
$content .= '<dd class="' . esc_attr( $day ) . '">';
if ( $hours['opening'] && $hours['closing'] ) {
$content .= date( $time_format, $opening );
$content .= ' — ';
$content .= date( $time_format, $closing );
if ( $today === $day ) {
$now = strtotime( current_time( 'H:i' ) );
if ( $now < $opening ) {
$content .= '<br />';
$content .= esc_html( sprintf( __( 'Opening in %s', 'random-blocks' ), human_time_diff( $now, $opening ) ) );
} elseif ( $now >= $opening && $now < $closing ) {
$content .= '<br />';
$content .= esc_html( sprintf( __( 'Closing in %s', 'random-blocks' ), human_time_diff( $now, $closing ) ) );
}
}
} else {
$content .= esc_html__( 'CLOSED', 'random-blocks' );
}
$content .= '</dd>';
}
$content .= '</dl>';
return $content;
}
// Contact Phone
add_action( 'init', 'register_contact_phone_block' );
function register_contact_phone_block() {
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
wp_register_script(
'contact-phone-editor',
plugins_url( 'contact-phone/contact-phone.js', __FILE__ ),
array(
'wp-blocks',
'wp-i18n',
)
);
if ( function_exists( 'wp_set_script_translations' ) ) {
wp_set_script_translations( 'contact-phone', 'random-blocks' );
}
wp_register_style( 'contact-phone-editor', plugins_url( 'contact-phone/contact-phone.css', __FILE__ ) );
register_block_type( 'random-blocks/contact-phone', array(
'editor_script' => 'contact-phone-editor',
'editor_style' => 'contact-phone-editor',
) );
}
// Time
add_action( 'init', 'register_time_block' );
function register_time_block() {
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
wp_register_script(
'time-converter-script',
plugins_url( 'time/time-converter-script.js', __FILE__ ),
array(
'jquery',
)
);
wp_register_script(
'time-block',
plugins_url( 'time/time.js', __FILE__ ),
array(
'wp-blocks',
'wp-components',
'wp-date',
'wp-i18n',
)
);
if ( function_exists( 'wp_set_script_translations' ) ) {
wp_set_script_translations( 'time-block', 'random-blocks' );
}
register_block_type( 'random-blocks/time', array(
'script' => 'time-converter-script',
'editor_script' => 'time-block',
) );
}