-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnections_hours.php
876 lines (690 loc) · 22.5 KB
/
connections_hours.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
<?php
/**
* An Extension for the Connections plugin which adds a metabox for
* adding the business hours of operation and a widget to display
* them.
*
* @package Connections Business Directory Extension - Open Hours
* @category Extension
* @author Steven A. Zahm
* @license GPL-2.0+
* @link https://connections-pro.com
* @copyright 2021 Steven A. Zahm
*
* @wordpress-plugin
* Plugin Name: Connections Business Directory Extension - Open Hours
* Plugin URI: https://connections-pro.com/add-on/hours/
* Description: An extension for the Connections plugin which allows you to add the business hours of operation to an entry and a widget to display them.
* Version: 1.2.1
* Author: Steven A. Zahm
* Author URI: https://connections-pro.com
* License: GPL-2.0+
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: connections_hours
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( ! class_exists('Connections_Business_Hours') ) {
class Connections_Business_Hours {
const VERSION = '1.2.1';
/**
* Stores the instance of this class.
*
* @since 1.1
*
* @var Connections_Business_Hours
*/
private static $instance;
/**
* @var string The absolute path this this file.
*
* @since 1.1
*/
private $file = '';
/**
* @var string The URL to the plugin's folder.
*
* @since 1.1
*/
private $url = '';
/**
* @var string The absolute path to this plugin's folder.
*
* @since 1.1
*/
private $path = '';
/**
* @var string The basename of the plugin.
*
* @since 1.1
*/
private $basename = '';
/**
* Connections_Business_Hours constructor.
*/
public function __construct() { /* Do nothing here */ }
/**
* @since 1.1
*
* @return Connections_Business_Hours
*/
public static function instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Connections_Business_Hours ) ) {
self::$instance = $self = new self;
$self->file = __FILE__;
$self->url = plugin_dir_url( $self->file );
$self->path = plugin_dir_path( $self->file );
$self->basename = plugin_basename( $self->file );
/**
* This should run on the `plugins_loaded` action hook. Since the extension loads on the
* `plugins_loaded` action hook, load immediately.
*/
cnText_Domain::register(
'connections_hours',
$self->basename,
'load'
);
self::loadDependencies();
self::hooks();
}
return self::$instance;
}
/**
* @since 1.1
*
* @return string
*/
public function getBaseURL() {
return $this->url;
}
private static function loadDependencies() {
require_once( 'includes/class.widgets.php' );
}
/**
* @since 1.1
*/
private static function hooks() {
// Register CSS and JavaScript.
add_action( 'init', array( __CLASS__ , 'registerScripts' ) );
if ( is_admin() ) {
// Enqueue the admin CSS and JS
add_action( 'cn_admin_enqueue_edit_styles', array( __CLASS__, 'adminStyles' ) );
// Since we're using a custom field, we need to add our own sanitization method.
add_filter( 'cn_meta_sanitize_field-business_hours', array( __CLASS__, 'sanitize') );
}
// Register the metabox and fields.
add_action( 'cn_metabox', array( __CLASS__, 'registerMetabox') );
// Business Hours uses a custom field type, so let's add the action to add it.
add_action( 'cn_meta_field-business_hours', array( __CLASS__, 'field' ), 10, 2 );
// Add the business hours option to the admin settings page.
add_filter( 'cn_content_blocks', array( __CLASS__, 'settingsOption') );
// Enqueue the public CSS
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueueScripts' ), 11 );
// Add the action that'll be run when calling $entry->getContentBlock( 'business_hours' ) from within a template.
add_action( 'cn_output_meta_field-business_hours', array( __CLASS__, 'block' ), 10, 4 );
// Register the widget.
add_action( 'widgets_init', array( 'cnbhHoursWidget', 'register' ) );
}
/**
* Callback for the `init` action.
*
* Register the CSS and JS files.
*
* @since 1.0
*/
public static function registerScripts() {
$url = Connections_Business_Hours()->getBaseURL();
$url = cnURL::makeProtocolRelative( $url );
// If SCRIPT_DEBUG is set and TRUE load the non-minified JS files, otherwise, load the minified files.
$min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
$requiredCSS = class_exists( 'Connections_Form' ) ? array( 'cn-public', 'cn-form-public' ) : array( 'cn-public' );
// Register CSS.
wp_register_style( 'cnbh-admin' , "{$url}assets/css/cnbh-admin$min.css", array( 'cn-admin', 'cn-admin-jquery-ui' ) , self::VERSION );
wp_register_style( 'cnbh-public', "{$url}assets/css/cnbh-public$min.css", $requiredCSS, self::VERSION );
// Register JavaScript.
wp_register_script( 'jquery-timepicker' , "{$url}assets/js/jquery-ui-timepicker-addon$min.js", array( 'jquery', 'jquery-ui-datepicker', 'jquery-ui-slider' ) , '1.4.3' );
wp_register_script( 'cnbh-ui-js' , "{$url}assets/js/cnbh-common$min.js", array( 'jquery-timepicker' ) , self::VERSION, true );
wp_localize_script( 'cnbh-ui-js', 'cnbhDateTimePickerOptions', Connections_Business_Hours::dateTimePickerOptions() );
}
/**
* Callback for the `cn_admin_enqueue_edit_styles` action.
*
* Enqueues the CSS on the Connections admin pages only.
*
* @since 1.0
*
* @param string $pageHook The current admin page hook.
*/
public static function adminStyles( $pageHook ) {
wp_enqueue_style( 'cnbh-admin' );
}
/**
* Callback for the `wp_enqueue_scripts` action.
*
* Enqueues the CSS.
*
* NOTE: This will only be enqueued if Form is installed and active
* because a CSS file registered by Form is listed as a dependency
* when registering 'cnbh-public'.
*
* @since 1.0
*/
public static function enqueueScripts() {
wp_enqueue_style( 'cnbh-public' );
}
/**
* Returns the date picker options.
*
* @since 1.0
*
* @return array
*/
public static function dateTimePickerOptions() {
$options = array(
'currentText' => __( 'Now', 'connections_hours' ),
'closeText' => __( 'Done', 'connections_hours' ),
'amNames' => array( __( 'AM', 'connections_hours' ), __( 'A', 'connections_hours' ) ),
'pmNames' => array( __( 'PM', 'connections_hours' ), __( 'P', 'connections_hours' ) ),
'timeFormat' => cnFormatting::dateFormatPHPTojQueryUI( self::timeFormat() ),
'timeSuffix' => '',
'timeOnlyTitle' => __( 'Choose Time', 'connections_hours' ),
'timeText' => __( 'Time', 'connections_hours' ),
'hourText' => __( 'Hour', 'connections_hours' ),
'minuteText' => __( 'Minute', 'connections_hours' ),
'secondText' => __( 'Second', 'connections_hours' ),
'millisecText' => __( 'Millisecond', 'connections_hours' ),
'microsecText' => __( 'Microsecond', 'connections_hours' ),
'timezoneText' => __( 'Time Zone', 'connections_hours' ),
'isRTL' => is_rtl(),
'parse' => 'loose',
);
return apply_filters( 'cnbh_timepicker_options', $options );
}
/**
* Returns the time format.
*
* @since 1.0
*
* @return string
*/
public static function timeFormat() {
return apply_filters( 'cnbh_time_format', get_option('time_format') );
}
/**
* Format a time supplied as string to a format from a format.
*
* @since 1.0
*
* @param string $value
* @param null $to
* @param null $from
*
* @return string
*/
public static function formatTime( $value, $to = NULL, $from = NULL ) {
$to = is_null( $to ) ? self::timeFormat() : $to;
$from = is_null( $from ) ? self::timeFormat() : $from;
if ( strlen( $value ) > 0 ) {
return cnDate::createFromFormat( $from, $value )->format( $to );
} else {
return $value;
}
}
/**
* Return the weekdays with teh start day as defined in the WP General Settings.
*
* @since 1.0
*
* @return array
*/
public static function getWeekdays() {
global $wp_locale;
// Output the weekdays sorted by the start of the week
// set in the WP General Settings. The array keys need to be
// retained which is why array_shift and array push are not
// being used.
$weekStart = apply_filters( 'cnbh_start_of_week', get_option('start_of_week') );
$weekday = $wp_locale->weekday;
for ( $i = 0; $i < $weekStart; $i++ ) {
$day = array_slice( $weekday, 0, 1, true );
unset( $weekday[ $i ] );
$weekday = $weekday + $day;
}
return $weekday;
}
/**
* Callback for the `cn_content_blocks` filter.
*
* Add the Business Open Hours as an option to display in the Content Block settings.
*
* @since 1.0
*
* @param array $blocks
*
* @return array
*/
public static function settingsOption( $blocks ) {
$blocks['business_hours'] = __( 'Business Hours', 'connections_hours' );
return $blocks;
}
/**
* Callback for the `cn_metabox` action.
*
* Register the business open hours metabox.
*
* @since 1.0
*/
public static function registerMetabox() {
$atts = array(
'id' => 'business-hours',
'title' => __( 'Business Hours', 'connections_hours' ),
'context' => 'normal',
'priority' => 'core',
'fields' => array(
array(
'id' => 'business_hours',
'type' => 'business_hours',
),
),
);
cnMetaboxAPI::add( $atts );
}
/**
* Callback for the `cn_meta_field-business_hours` action.
*
* Display the business open hours fields within the registered metabox.
*
* @since 1.0
*
* @param array $field
* @param array $value
*/
public static function field( $field, $value ) {
?>
<table id="start_of_week">
<thead>
<tr>
<th><?php _e( 'Weekday', 'connections_hours' ); ?></th>
<td><?php _e( 'Open', 'connections_hours' ); ?></td>
<td><?php _e( 'Close', 'connections_hours' ); ?></td>
<td><?php _e( 'Add / Remove Period', 'connections_hours' ); ?></td>
</tr>
</thead>
<tfoot>
<tr>
<th><?php _e( 'Weekday', 'connections_hours' ); ?></th>
<td><?php _e( 'Open', 'connections_hours' ); ?></td>
<td><?php _e( 'Close', 'connections_hours' ); ?></td>
<td><?php _e( 'Add / Remove Period', 'connections_hours' ); ?></td>
</tr>
</tfoot>
<tbody>
<tr id="cnbh-period" style="display: none;">
<td> </td>
<td>
<?php
cnHTML::field(
array(
'type' => 'text',
'class' => '',
'id' => $field['id'] . '[day][period][open]',
'required' => false,
'label' => '',
'before' => '',
'after' => '',
'return' => false,
)
);
?>
</td>
<td>
<?php
cnHTML::field(
array(
'type' => 'text',
'class' => '',
'id' => $field['id'] . '[day][period][close]',
'required' => false,
'label' => '',
'before' => '',
'after' => '',
'return' => false,
)
);
?>
</td>
<td><span class="button cnbh-remove-period">–</span><span class="button cnbh-add-period">+</span></td>
</tr>
<?php
if ( ( ! is_array( $value ) ) ) $value = array();
foreach ( self::getWeekdays() as $key => $day ) {
// If there are no periods saved for the day,
// add an empty period to prevent index not found errors.
if ( ! isset( $value[ $key ] ) ) {
$value[ $key ] = array(
0 => array(
'open' => '',
'close' => ''
),
);
}
foreach ( $value[ $key ] as $period => $data ) {
$open = cnHTML::field(
array(
'type' => 'text',
'class' => array( 'timepicker', 'clearable' ),
'id' => $field['id'] . '[' . $key . '][' . $period . '][open]',
'required' => FALSE,
'label' => '',
'before' => '',
'after' => '',
'return' => TRUE,
),
self::formatTime( $data['open'], NULL, 'H:i' )
);
$close = cnHTML::field(
array(
'type' => 'text',
'class' => array( 'timepicker', 'clearable' ),
'id' => $field['id'] . '[' . $key . '][' . $period . '][close]',
'required' => FALSE,
'label' => '',
'before' => '',
'after' => '',
'return' => TRUE,
),
self::formatTime( $data['close'], NULL, 'H:i' )
);
if ( $period == 0 ) {
// Display the "+" button only. This button should only be shown on the first period of the day.
$buttons = sprintf( '<span class="button cnbh-remove-period" data-day="%1$d" data-period="%2$d" style="display: none;">–</span><span class="button cnbh-add-period" data-day="%1$d" data-period="%2$d">+</span>',
$key,
$period
);
} else {
// Display both buttons. Both buttons should be shown for every period after the first.
$buttons = sprintf( '<span class="button cnbh-remove-period" data-day="%1$d" data-period="%2$d">–</span><span class="button cnbh-add-period" data-day="%1$d" data-period="%2$d">+</span>',
$key,
$period
);
}
printf( '<tr %1$s %2$s %3$s><th>%4$s</th><td>%5$s</td><td>%6$s</td><td>%7$s</td></tr>',
'class="cnbh-day-' . absint( $key ) . '"',
$period == 0 ? 'id="cnbh-day-' . absint( $key ) . '"' : '',
$period == 0 ? 'data-count="' . absint( count( $value[ $key ] ) - 1 ) . '"' : '',
$period == 0 ? esc_attr( $day ) : ' ',
$open,
$close,
$buttons
);
}
}
?>
</tbody>
</table>
<?php
printf( '<p>%s</p>', __( 'To create a closed day or closed period within a day, leave both the open and close hours blank.', 'connections_hours' ) );
// Enqueue the JS required for the metabox.
wp_enqueue_script( 'cnbh-ui-js' );
}
/**
* Sanitize the times as a text input using the cnSanitize class.
*
* @since 1.0
*
* @param array $value The opening/closing hours.
*
* @return array
*/
public static function sanitize( $value ) {
$hasOpenHours = FALSE;
if ( empty( $value ) ) return $value;
foreach ( $value as $key => $day ) {
foreach ( $day as $period => $time ) {
if ( 0 < strlen( $time['open'] ) || 0 < strlen( $time['open'] ) ) {
// Save all time values in 24hr format.
$time['open'] = self::formatTime( $time['open'], 'H:i' );
$time['close'] = self::formatTime( $time['close'], 'H:i' );
$value[ $key ][ $period ]['open'] = cnSanitize::string( 'text', $time['open'] );
$value[ $key ][ $period ]['close'] = cnSanitize::string( 'text', $time['close'] );
$hasOpenHours = TRUE;
}
}
}
if ( ! $hasOpenHours ) {
return NULL;
}
return $value;
}
/**
* Callback for the `cn_output_meta_field-business_hours` action.
*
* @since 1.0
*
* @see cnEntry_HTML::getContentBlock()
*
* Render the business open hours.
*
* @param string $id The field id.
* @param array $value The business hours data.
* @param cnEntry_HTML $entry
* @param array $atts The shortcode atts array passed from the calling action.
*
*@internal
*/
public static function block( $id, $value, $entry, $atts ) {
global $wp_locale;
$defaults = array(
'header' => TRUE,
'footer' => FALSE,
'day_name' => 'full', // Valid options are 'full', 'abbrev' or 'initial'.
'show_closed_day' => TRUE,
'show_closed_period' => FALSE,
'show_if_no_hours' => FALSE,
'show_open_status' => TRUE,
'highlight_open_period' => TRUE,
'open_close_separator' => '–',
);
$atts = wp_parse_args( $atts, $defaults );
if ( ! self::hasOpenHours( $value ) ) return;
echo '<div class="cnbh-block">';
// Whether or not to display the open status message.
if ( $atts['show_open_status'] && self::openStatus( $value ) ) {
printf( '<p class="cnbh-status cnbh-status-open">%s</p>' , __( 'We are currently open.', 'connections_hours' ) );
} elseif ( $atts['show_open_status'] ) {
printf( '<p class="cnbh-status cnbh-status-closed">%s</p>' , __( 'Sorry, we are currently closed.', 'connections_hours' ) );
}
?>
<table class="cnbh">
<?php if ( $atts['header'] ) : ?>
<thead>
<tr>
<th> </th>
<th><?php _e( 'Open', 'connections_hours' ); ?></th>
<th class="cnbh-separator"> </th>
<th><?php _e( 'Close', 'connections_hours' ); ?></th>
</tr>
</thead>
<?php endif; ?>
<?php if ( $atts['footer'] ) : ?>
<tfoot>
<tr>
<th> </th>
<th><?php _e( 'Open', 'connections_hours' ); ?></th>
<th class="cnbh-separator"> </th>
<th><?php _e( 'Close', 'connections_hours' ); ?></th>
</tr>
</tfoot>
<?php endif; ?>
<tbody>
<?php
foreach ( self::getWeekdays() as $key => $day ) {
// Display the day as either its initial or abbreviation.
switch ( $atts['day_name'] ) {
case 'initial' :
$day = $wp_locale->get_weekday_initial( $day );
break;
case 'abbrev' :
$day = $wp_locale->get_weekday_abbrev( $day );
break;
}
// Show the "Closed" message if there are no open and close hours recorded for the day.
if ( $atts['show_closed_day'] && ! self::openToday( $value[ $key ] ) ) {
printf( '<tr %1$s %2$s %3$s><th>%4$s</th><td class="cnbh-closed" colspan="3">%5$s</td></tr>',
'class="cnbh-day-' . absint( $key ) . '"',
'id="cnbh-day-' . absint( $key ) . '"',
'data-count="' . absint( count( $value[ $key ] ) - 1 ) . '"',
esc_attr( $day ),
__( 'Closed Today', 'connections_hours' )
);
// Exit this loop.
continue;
}
// If there are open and close hours recorded for the day, loop thru the open periods.
foreach ( $value[ $key ] as $period => $time ) {
// Show the "Closed" message if there are no open and close hours recorded for the period.
if ( self::openPeriod( $time ) ) {
printf( '<tr %1$s %2$s %3$s><th>%4$s</th><td class="cnbh-open">%5$s</td><td class="cnbh-separator">%6$s</td><td class="cnbh-close">%7$s</td></tr>',
'class="cnbh-day-' . absint( $key ) . ( $atts['highlight_open_period'] && date( 'w', current_time( 'timestamp' ) ) == $key && self::isOpen( $time['open'], $time['close'] ) ? ' cnbh-open-period' : '' ) . '"',
$period == 0 ? 'id="cnbh-day-' . absint( $key ) . '"' : '',
$period == 0 ? 'data-count="' . absint( count( $value[ $key ] ) - 1 ) . '"' : '',
$period == 0 ? esc_attr( $day ) : ' ',
self::formatTime( $time['open'], NULL, 'H:i' ),
esc_attr( $atts['open_close_separator'] ),
self::formatTime( $time['close'], NULL, 'H:i' )
);
} elseif ( $atts['show_closed_period'] && $period > 0 ) {
printf( '<tr %1$s %2$s %3$s><th>%4$s</th><td class="cnbh-closed" colspan="3">%5$s</td></tr>',
'class="cnbh-day-' . absint( $key ) . '"',
'id="cnbh-day-' . absint( $key ) . '"',
'data-count="' . absint( count( $value[ $key ] ) - 1 ) . '"',
$period == 0 ? esc_attr( $day ) : ' ',
__( 'Closed Period', 'connections_hours' )
);
}
}
}
?>
</tbody>
</table>
<?php
echo '</div>';
}
/**
* Whether or not the business is currently open or not.
*
* @param array $value
*
* @return bool
*/
public static function openStatus( $value ) {
foreach ( self::getWeekdays() as $key => $day ) {
foreach ( $value[ $key ] as $period => $time ) {
if ( date( 'w', current_time( 'timestamp' ) ) == $key &&
self::openPeriod( $time ) &&
self::isOpen( $time['open'], $time['close'] )
) {
return TRUE;
}
}
}
return FALSE;
}
/**
* Whether or not there are any open hours during the week.
*
* @since 1.0
*
* @param array $days
*
* @return boolean
*/
public static function hasOpenHours( $days ) {
foreach ( $days as $key => $day ) {
if ( self::openToday( $day ) ) return TRUE;
}
return FALSE;
}
/**
* Whether or not the day has any open periods.
*
* @since 1.0
*
* @param array $day
*
* @return bool
*/
private static function openToday( $day ) {
foreach ( $day as $period => $data ) {
if ( self::openPeriod( $data ) ) return TRUE;
}
return FALSE;
}
/**
* Whether or not the period is open.
*
* @since 1.0
*
* @param array $period
*
* @return bool
*/
private static function openPeriod( $period ) {
if ( empty( $period ) ) return FALSE;
if ( ! empty( $period['open'] ) && ! empty( $period['close'] ) ) return TRUE;
return FALSE;
}
/**
* Whether or not the business is open.
*
* @link http://stackoverflow.com/a/17145145
*
* @since 1.0
*
* @param string $t1 Time open.
* @param string $t2 Time close.
* @param null $tn Time now.
*
* @return bool
*/
private static function isOpen( $t1, $t2, $tn = NULL ) {
$tn = is_null( $tn ) ? date( 'H:i', current_time( 'timestamp' ) ) : self::formatTime( $tn, 'H:i' );
$t1 = +str_replace( ':', '', $t1 );
$t2 = +str_replace( ':', '', $t2 );
$tn = +str_replace( ':', '', $tn );
if ( $t2 >= $t1 ) {
return $t1 <= $tn && $tn < $t2;
} else {
return ! ( $t2 <= $tn && $tn < $t1 );
}
}
}
/**
* Start up the extension.
*
* @since 1.0
*
* @return Connections_Business_Hours|false
*/
function Connections_Business_Hours() {
if ( class_exists( 'connectionsLoad' ) ) {
return Connections_Business_Hours::instance();
} else {
add_action(
'admin_notices',
function() {
echo '<div id="message" class="error"><p><strong>ERROR:</strong> Connections must be installed and active in order use Connections Business Hours.</p></div>';
}
);
return false;
}
}
/**
* We'll load the extension on `plugins_loaded` so we know Connections will be loaded and ready first.
* Set priority 11, so we know Form is loaded first.
*/
add_action( 'plugins_loaded', 'Connections_Business_Hours', 11 );
}