-
Notifications
You must be signed in to change notification settings - Fork 0
/
appointments.php
2428 lines (2097 loc) · 74.4 KB
/
appointments.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
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: Appointments+
Description: Lets you accept appointments from front end and manage or create them from admin side
Plugin URI: http://premium.wpmudev.org/project/appointments-plus/
Version: 2.2.3
Author: WPMU DEV
Author URI: http://premium.wpmudev.org/
Textdomain: appointments
WDP ID: 679841
*/
/*
Copyright 2007-2013 Incsub (http://incsub.com)
Author - Hakan Evin <hakan@incsub.com>
Contributor - Ve Bailovity (Incsub)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License (Version 2 - GPLv2) as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
if ( !class_exists( 'Appointments' ) ) {
class Appointments {
public $version = '2.2.3';
public $db_version;
public $timetables = array();
public $local_time;
/** @var bool|Appointments_Google_Calendar */
public $gcal_api = false;
public $locale_error;
public $time_format;
public $datetime_format;
public $log_file;
public $salt;
public $worker;
public $location;
public $service;
public $openid;
public $plugin_url;
/** @var Appointments_Admin */
public $admin;
/** @var Appointments_Addons_Loader */
public $addons_loader;
/** @var Appointments_Notifications_Manager */
public $notifications;
public $pro = false;
public $shortcodes = array();
function __construct() {
include_once( 'includes/helpers.php' );
include_once( 'includes/helpers-settings.php' );
include_once( 'includes/helpers-timetables.php' );
include_once( 'includes/deprecated-hooks.php' );
include_once( 'includes/class-app-notifications-manager.php' );
include_once( 'includes/class-app-api-logins.php' );
include_once( 'includes/class-app-sessions.php' );
// Load premium features
if ( _appointments_is_pro() ) {
include_once( appointments_plugin_dir() . 'includes/pro/class-app-pro.php' );
$this->pro = new Appointments_Pro();
}
$this->timetables = get_transient( 'app_timetables' );
if ( ! $this->timetables || ! is_array( $this->timetables ) ) {
$this->timetables = array();
}
$this->plugin_url = plugins_url(basename(dirname(__FILE__)));
// Read all options at once
$this->options = get_option( 'appointments_options' );
// To follow WP Start of week, time, date settings
$this->local_time = current_time('timestamp');
$this->start_of_week = appointments_week_start() - 1;
$this->time_format = appointments_get_date_format( 'time' );
$this->date_format = appointments_get_date_format( 'date' );
$this->datetime_format = appointments_get_date_format( 'full' );
add_action( 'delete_user', 'appointments_delete_worker' ); // Modify database in case a user is deleted
add_action( 'wpmu_delete_user', 'appointments_delete_worker' ); // Same as above
add_action( 'remove_user_from_blog', array( &$this, 'remove_user_from_blog' ), 10, 2 ); // Remove his records only for that blog
add_action( 'plugins_loaded', array(&$this, 'localization') ); // Localize the plugin
add_action( 'init', array( &$this, 'init' ), 20 ); // Initial stuff
add_filter( 'the_posts', array(&$this, 'load_styles') ); // Determine if we use shortcodes on the page
add_action( 'admin_init', array( $this, 'maybe_upgrade' ) );
include_once( 'includes/class-app-service.php' );
include_once( 'includes/class-app-worker.php' );
include_once( 'includes/class-app-appointment.php' );
include_once( 'includes/class-app-transaction.php' );
if ( is_admin() ) {
$this->load_admin();
}
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
include_once( 'includes/class-app-ajax.php' );
new Appointments_AJAX();
}
// Check for cookies
if (!empty($this->options['login_required']) && 'yes' === $this->options['login_required']) {
// If we require a login and we had an user logged in,
// we don't need cookies after they log out
add_action('wp_logout', array($this, 'drop_cookies_on_logout'));
}
// Widgets
require_once( appointments_plugin_dir() . 'includes/widgets.php' );
add_action( 'widgets_init', array( &$this, 'widgets_init' ) );
// Integration with other plugins/Themes
include_once( appointments_plugin_dir() . 'includes/integration/integration.php' );
$this->pages_to_be_cached = array();
$this->had_filter = false; // There can be a wpautop filter. We will check this later on.
add_action('init', array($this, 'get_gcal_api'), 10);
// Database variables
global $wpdb;
$this->db = &$wpdb;
$this->services_table = $wpdb->prefix . "app_services";
$this->transaction_table = $wpdb->prefix . "app_transactions";
$this->cache_table = $wpdb->prefix . "app_cache";
// DB version
$this->db_version = get_option( 'app_db_version' );
// Set meta tables
$wpdb->app_appointmentmeta = appointments_get_table( 'appmeta' );
// Set log file location
$uploads = wp_upload_dir();
if ( isset( $uploads["basedir"] ) )
$this->uploads_dir = $uploads["basedir"] . "/";
else
$this->uploads_dir = WP_CONTENT_DIR . "/uploads/";
$this->log_file = $this->uploads_dir . "appointments-log.txt";
// Other default settings
$this->script = $this->uri = $this->error_url = '';
$this->location = $this->service = $this->worker = 0;
$this->gcal_image = '<img src="' . $this->plugin_url . '/images/gc_button1.gif" />';
$this->locale_errlocale_error = false;
// Create a salt, if it doesn't exist from the previous installation
if ( !$salt = get_option( "appointments_salt" ) ) {
$salt = mt_rand();
add_option( "appointments_salt", $salt ); // Save it to be used until it is cleared manually
}
$this->salt = $salt;
// Deal with zero-priced appointments auto-confirm
if ( isset( $this->options['payment_required'] ) && 'yes' == $this->options['payment_required'] && !empty($this->options['allow_free_autoconfirm'])) {
if (!defined('APP_CONFIRMATION_ALLOW_FREE_AUTOCONFIRM')) define('APP_CONFIRMATION_ALLOW_FREE_AUTOCONFIRM', true);
}
$this->notifications = new Appointments_Notifications_Manager();
}
public function load_admin() {
include_once( 'admin/class-app-admin.php' );
$this->admin = new Appointments_Admin();
}
function maybe_upgrade() {
if ( isset( $_GET['app-clear'] ) && current_user_can( 'manage_options' ) ) {
appointments_clear_cache();
}
$db_version = get_option( 'app_db_version' );
if ( $db_version == $this->version ) {
return;
}
if ( false === $db_version ) {
appointments_activate();
}
appointments_clear_cache();
include_once( 'includes/class-app-upgrader.php' );
$upgrader = new Appointments_Upgrader( $this->version );
$upgrader->upgrade( $db_version, $this->version );
}
function get_gcal_api() {
if ( false === $this->gcal_api && ! defined( 'APP_GCAL_DISABLE' ) ) {
require_once appointments_plugin_dir() . 'includes/class-app-gcal.php';
$this->gcal_api = new Appointments_Google_Calendar();
}
return $this->gcal_api;
}
/**
***************************************************************************************************************
* Methods for optimization
*
* $l: location ID - For future use
* $s: service ID
* $w: worker ID
* $stat: Status (open: working or closed: not working)
* IMPORTANT: This plugin is NOT intended for hundreds of services or service providers,
* but it is intended to make database queries as cheap as possible with smaller number of services/providers.
* If you have lots of services and/or providers, codes will not scale and appointments pages will be VERY slow.
* If you need such an application, override some of the methods below with a child class.
***************************************************************************************************************
*/
/**
* Get location, service, worker
*/
function get_lsw() {
$this->location = $this->get_location_id();
$this->service = $this->get_service_id();
$this->worker = $this->get_worker_id();
}
/**
* Get location ID for future use
*/
function get_location_id() {
if ( isset( $_REQUEST["app_location_id"] ) )
return (int)$_REQUEST["app_location_id"];
return 0;
}
/**
* Get service ID from front end
* @return integer
*/
function get_service_id() {
if ( isset( $_REQUEST["app_service_id"] ) )
return (int)$_REQUEST["app_service_id"];
else if ( !$service_id = appointments_get_services_min_id() )
$service_id = 0;
return $service_id;
}
/**
* Get worker ID from front end
* worker = provider
* @return integer
*/
function get_worker_id() {
if( ! is_admin() ) {
if ( isset( $_GET["app_provider_id"] ) ) {
return (int) $_GET["app_provider_id"];
}
}
if ( isset( $_REQUEST["app_provider_id"] ) ){
return (int)$_REQUEST["app_provider_id"];
}
if ( isset( $_REQUEST["app_worker_id"] ) ){
return (int)$_REQUEST["app_worker_id"];
}
return 0;
}
/**
* Return all reserve appointments by worker ID
* @param week: Optionally appointments only in the number of week in ISO 8601 format (since 1.2.3)
* @return array of objects
*/
function get_reserve_apps_by_worker( $l, $w, $week=0 ) {
$cache_key = $l . '_' . $w . '_' . $week;
$cached = wp_cache_get( 'reserve_apps_by_worker' );
if ( false === $cached ) {
$cached = array();
}
if ( ! isset( $cached[ $cache_key ] ) ) {
$services = appointments_get_services();
$apps = array();
if ( $services ) {
foreach ( $services as $service ) {
$args = array(
'location' => $l,
'service' => $service->ID,
'worker' => $w,
'week' => $week
);
$apps_worker = appointments_get_appointments_filtered_by_services( $args );
if ( $apps_worker )
$apps = array_merge( $apps, $apps_worker );
}
}
$cached[ $cache_key ] = $apps;
wp_cache_set( 'reserve_apps_by_worker', $cached );
}
else {
$apps = $cached[ $cache_key ];
}
return $apps;
}
/**
* Find if a user is dummy
* @param user_id: Id of the user who will be checked if he is dummy
* since 1.0.6
*
* @deprecated since 2.1
*
* @return bool
*/
function is_dummy( $user_id=0 ) {
_deprecated_function( __FUNCTION__, '2.1', 'Appointments_Worker::is_dummy' );
if ( ! $user_id ) {
$user_id = get_current_user_id();
}
$worker = appointments_get_worker( $user_id );
if ( ! $worker ) {
return false;
}
return $worker->is_dummy();
}
/**
* Find worker email given his ID
* since 1.0.6
* @return string
*/
function get_worker_email( $worker=0 ) {
// Real person
$worker_obj = appointments_get_worker( $worker );
if ( ! $worker_obj ) {
return '';
}
if ( !$worker_obj->is_dummy() ) {
$worker_data = get_userdata( $worker );
if ( $worker_data )
$worker_email = $worker_data->user_email;
else
$worker_email = '';
return apply_filters( 'app_worker_email', $worker_email, $worker );
}
// Dummy
if ( isset( $this->options['dummy_assigned_to'] ) && $this->options['dummy_assigned_to'] ) {
$worker_data = get_userdata( $this->options['dummy_assigned_to'] );
if ( $worker_data )
$worker_email = $worker_data->user_email;
else
$worker_email = '';
return apply_filters( 'app_dummy_email', $worker_email, $worker );
}
// If not set anything, assign to admin
return $this->get_admin_email( );
}
/**
* Return admin email
* since 1.2.7
* @return string
*/
function get_admin_email( ) {
global $current_site;
$admin_email = get_option('admin_email');
if ( !$admin_email )
$admin_email = 'admin@' . $current_site->domain;
return apply_filters( 'app_get_admin_email', $admin_email );
}
/**
* Find service name given its ID
* @return string
*/
function get_service_name( $service=0 ) {
// Safe text if we delete a service
$name = __('Not defined', 'appointments');
$result = appointments_get_service( $service );
if ( $result )
$name = $result->name;
$name = apply_filters( 'app_get_service_name', $name, $service );
return stripslashes( $name );
}
/**
* Find client name given his appointment
* @return string
*/
function get_client_name( $app_id ) {
$name = '';
// This is only used on admin side, so an optimization is not required.
$result = appointments_get_appointment( $app_id );
if ( $result ) {
// Client can be a user
if ( $result->user ) {
$userdata = get_userdata( $result->user );
if ( $userdata ) {
$href = function_exists('bp_core_get_user_domain') && (defined('APP_BP_LINK_TO_PROFILE') && APP_BP_LINK_TO_PROFILE)
? bp_core_get_user_domain($result->user)
: admin_url("user-edit.php?user_id="). $result->user
;
$name = '<a href="' . apply_filters('app_get_client_name-href', $href, $app_id, $result) . '" target="_blank">' .
($result->name && !(defined('APP_USE_LEGACY_ADMIN_USERDATA_OVERRIDES') && APP_USE_LEGACY_ADMIN_USERDATA_OVERRIDES) ? $result->name : $userdata->user_login) .
'</a>';
}
else
$name = $result->name;
}
else {
$name = $result->name;
if ( !$name )
$name = $result->email;
}
}
return apply_filters( 'app_get_client_name', $name, $app_id, $result );
}
/**
* Get price for the current service and worker
* If worker has additional price (optional), it is added to the service price
* @param paypal: If set true, deposit price is calculated
* @return string
*/
function get_price( $paypal=false ) {
global $current_user;
$this->get_lsw();
$price = appointments_get_price( $this->service, $this->worker );
/**
* Filter allows other plugins or integrations to apply a discount to
* the price.
*/
$price = apply_filters( 'app_get_price_prepare', $price, $paypal, $this );
// Discount
if ( $this->is_member() && isset( $this->options["members_discount"] ) && $this->options["members_discount"] ) {
// Special condition: Free for members
if ( 100 == $this->options["members_discount"] )
$price = 0;
else
$price = $price * ( 100 - $this->options["members_discount"] )/100;
}
if ( $paypal ) {
// Deposit
if ( isset( $this->options["percent_deposit"] ) && $this->options["percent_deposit"] )
$price = $price * $this->options["percent_deposit"] / 100;
if ( isset( $this->options["fixed_deposit"] ) && $this->options["fixed_deposit"] )
$price = $this->options["fixed_deposit"];
// It is possible to ask special amounts to be paid
$price = apply_filters( 'app_paypal_amount', $price, $this->service, $this->worker, $current_user->ID );
} else {
$price = apply_filters( 'app_get_price', $price, $this->service, $this->worker, $current_user->ID );
}
// Use number_format right at the end, cause it converts the number to a string.
$price = number_format( $price, 2 );
return $price;
}
/**
* Get deposit given price
* This is required only for manual pricing
* @param price: the full price
* @since 1.0.8
* @return string
*/
function get_deposit( $price ) {
$deposit = 0;
if ( !$price )
return apply_filters( 'app_get_deposit', 0 );
// Discount
if ( $this->is_member() && isset( $this->options["members_discount"] ) && $this->options["members_discount"] ) {
// Special condition: Free for members
if ( 100 == $this->options["members_discount"] )
$price = 0;
else
$price = number_format( $price * ( 100 - $this->options["members_discount"] )/100, 2 );
}
// Deposit
if ( isset( $this->options["percent_deposit"] ) && $this->options["percent_deposit"] )
$deposit = number_format( $price * $this->options["percent_deposit"] / 100, 2 );
if ( isset( $this->options["fixed_deposit"] ) && $this->options["fixed_deposit"] )
$deposit = $this->options["fixed_deposit"];
return apply_filters( 'app_get_deposit', $deposit );
}
/**
* Get the capacity of the current service
* @return integer
*/
function get_capacity( $service_id = false ) {
if ( $service_id && $service = appointments_get_service( $service_id ) ) {
$service_id = $service->ID;
}
else {
$service_id = $this->service;
}
return appointments_get_service_capacity( $service_id );
}
/****************
* General methods
*****************
*/
/**
* Save a message to the log file
*/
function log( $message='' ) {
if ( $message ) {
$to_put = '<b>['. date_i18n( $this->datetime_format, $this->local_time ) .']</b> '. $message;
// Prevent multiple messages with same text and same timestamp
if ( !file_exists( $this->log_file ) || strpos( @file_get_contents( $this->log_file ), $to_put ) === false ) {
@file_put_contents( $this->log_file, $to_put . chr(10). chr(13), FILE_APPEND );
}
}
}
/**
* Remove tabs and breaks
*/
function esc_rn( $text ) {
$text = str_replace( array("\t","\n","\r"), "", $text );
return $text;
}
/**
* Converts number of seconds to hours:mins acc to the WP time format setting
* @param integer $secs Seconds
* @param bool $forced_format
* @param bool $do_i18n
* @return bool|int|string
*/
function secs2hours( $secs, $forced_format=false, $do_i18n = true ) {
$min = (int)($secs / 60);
$hours = "00";
if ( $min < 60 )
$hours_min = $hours . ":" . $min;
else {
$hours = (int)($min / 60);
if ( $hours < 10 )
$hours = "0" . $hours;
$mins = $min - $hours * 60;
if ( $mins < 10 )
$mins = "0" . $mins;
$hours_min = $hours . ":" . $mins;
}
if (!empty($forced_format)) $hours_min = strtotime($hours_min . ":00");
else if ($this->time_format) $hours_min = strtotime($hours_min . ":00"); // @TODO: TEST THIS THOROUGHLY!!!!
if( $do_i18n ) {
$hours_min = date_i18n( $this->time_format, $hours_min );
}
elseif ( $forced_format ) {
$hours_min = date( $forced_format, $hours_min );
}
else {
$hours_min = date( $this->time_format, $hours_min );
}
return $hours_min;
}
function secs_to_24h( $secs ) {
$min = (int)($secs / 60);
$hours = "00";
if ( $min < 60 )
$hours_min = $hours . ":" . $min;
else {
$hours = (int)($min / 60);
if ( $hours < 10 )
$hours = "0" . $hours;
$mins = $min - $hours * 60;
if ( $mins < 10 )
$mins = "0" . $mins;
$hours_min = $hours . ":" . $mins;
}
return date( 'H:i', strtotime( $hours_min ) );
}
/**
* Return an array of preset base times, so that strange values are not set
* @return array
*/
function time_base() {
$default = array( 10,15,30,60,90,120 );
$options = appointments_get_options();
$a = $options["additional_min_time"];
// Additional time bases
if ( isset( $a ) && $a && is_numeric( $a ) )
$default[] = $a;
return apply_filters( 'app_time_base', $default );
}
/**
* Return minimum set interval time
* If not set, return a safe time.
*
* @return integer
*/
function get_min_time() {
$options = appointments_get_options();
$min_time = $options['min_time'];
if ( $min_time && $min_time > apply_filters( 'app_safe_min_time', 9 ) ) {
return apply_filters( 'app-time-min_time', absint( $min_time ) );
} else {
return apply_filters( 'app-time-min_time', apply_filters( 'app_safe_time', 10 ) );
}
}
/**
* Number of days that an appointment can be taken
* @return integer
*/
function get_app_limit() {
$options = appointments_get_options();
$app_limit = $options['app_limit'];
if ( $app_limit ) {
return apply_filters( 'app_limit', absint( $app_limit ) );
} else {
return apply_filters( 'app_limit', 365 );
}
}
/**
* Return an array of weekdays
* @return array
*/
function weekdays() {
return array(
__('Sunday', 'appointments') => 'Sunday',
__('Monday', 'appointments') => 'Monday',
__('Tuesday', 'appointments') => 'Tuesday',
__('Wednesday', 'appointments') => 'Wednesday',
__('Thursday', 'appointments') => 'Thursday',
__('Friday', 'appointments') => 'Friday',
__('Saturday', 'appointments') => 'Saturday'
);
}
/**
* Return a selected field name to further customize them and make translation easier
* @return string (name of the field)
*/
function get_field_name( $key ) {
$field_names = array(
'name' => __('Name', 'appointments'),
'email' => __('Email', 'appointments'),
'phone' => __('Phone', 'appointments'),
'address' => __('Address', 'appointments'),
'city' => __('City', 'appointments'),
'note' => __('Note', 'appointments')
);
$field_names = apply_filters( 'app_get_field_name', $field_names );
if ( array_key_exists( $key, $field_names ) ) {
return $field_names[ $key ];
} else {
return __( 'Not defined', 'appointments' );
}
}
/**
* Return an array of all available front end box classes
* @return array
*/
function get_classes() {
return apply_filters( 'app_box_class_names',
array(
'free' => __( 'Free', 'appointments' ),
'busy' => __( 'Busy', 'appointments' ),
'notpossible' => __( 'Not possible', 'appointments' )
)
);
}
/**
* Return a default color for a selected box class
* @return string
*/
function get_preset( $class, $set ) {
$presets = array(
1 => array(
'free' => '48c048',
'busy' => 'ffffff',
'notpossible' => 'ffffff'
),
2 => array(
'free' => '73ac39',
'busy' => '616b6b',
'notpossible' => '8f99a3'
),
3 => array(
'free' => '40BF40',
'busy' => '454C54',
'notpossible' => '454C54'
)
);
return isset( $presets[ $set ][ $class ] ) ? $presets[ $set ][ $class ] : '111111';
}
/************************************************************
* Methods for Shortcodes and those related to shortcodes only
*************************************************************
*/
/**
* Generate an excerpt from the selected service/worker page
* Applies custom filter set instead of the default one.
*/
function get_excerpt( $page_id, $thumb_size, $thumb_class, $worker_id=0, $show_thumb_holder = false ) {
$text = '';
if ( !$page_id )
return $text;
$page = get_post( $page_id );
if ( !$page )
return $text;
$text = $page->post_excerpt;
if ( empty( $text ) ) {
$text = $page->post_content;
}
$text = strip_shortcodes( $text );
$text = apply_filters('app_the_content', $text, $page_id, $worker_id );
$text = str_replace(']]>', ']]>', $text);
$excerpt_length = apply_filters('app_excerpt_length', 55);
$excerpt_more = apply_filters('app_excerpt_more', ' … <a href="'. esc_url( get_permalink($page->ID) ) . '" target="_blank">' . __( 'More information <span class="meta-nav">→</span>', 'appointments' ) . '</a>');
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
if ( $show_thumb_holder ) {
// @TODO Little crap :( to avoid so many queries when there are many services
$thumb = '<div class="appointments-service-thumb appointments-service-thumb-' . absint( $page_id ) . '" data-page="' . absint( $page_id ) . '"></div>';
}
else {
$thumb = $this->get_thumbnail( $page_id, $thumb_size, $thumb_class, $worker_id );
}
return apply_filters( 'app_excerpt', $thumb. $text, $page_id, $worker_id );
}
/**
* Fetch content from the selected service/worker page.
* Applies custom filter set instead of the default one.
*/
function get_content( $page_id, $thumb_size, $thumb_class, $worker_id=0, $show_thumb_holder = false ) {
$content = '';
if ( !$page_id )
return $content;
$page = get_post( $page_id );
if ( !$page )
return $content;
if ( $show_thumb_holder ) {
// @TODO Little crap :( to avoid so many queries when there are many services
$thumb = '<div class="appointments-service-thumb appointments-service-thumb-' . absint( $page_id ) . '" data-page="' . absint( $page_id ) . '"></div>';
}
else {
$thumb = $this->get_thumbnail( $page_id, $thumb_size, $thumb_class, $worker_id );
}
$app_content = apply_filters( 'app_pre_content', wpautop( $this->strip_app_shortcodes( $page->post_content ), true ) );
return apply_filters( 'app_content', $thumb. $app_content, $page_id, $worker_id );
}
/**
* Clear app shortcodes
* @since 1.1.9
*/
function strip_app_shortcodes( $content ) {
// Don't even try to touch a non string, just in case
if ( !is_string( $content ) )
return $content;
else
return preg_replace( '%\[app_(.*?)\]%is', '', $content );
}
/**
* Get html code for thumbnail or avatar
*/
function get_thumbnail( $page_id, $thumb_size, $thumb_class, $worker_id ) {
if ( $thumb_size && 'none' != $thumb_size ) {
if ( strpos( $thumb_size, 'avatar' ) !== false ) {
if ( strpos( $thumb_size, ',' ) !== false ) {
$size_arr = explode( ",", $thumb_size );
$size = $size_arr[1];
}
else
$size = 96;
$thumb = get_avatar( $worker_id, $size );
if ( $thumb_class ) {
// Dirty, but faster than preg_replace
$thumb = str_replace( "class='", "class='".$thumb_class." ", $thumb );
$thumb = str_replace( 'class="', 'class="'.$thumb_class.' ', $thumb );
}
}
else {
if ( strpos( $thumb_size, ',' ) !== false )
$size = explode( ",", $thumb_size );
else
$size = $thumb_size;
$thumb = get_the_post_thumbnail( $page_id, $size, apply_filters( 'app_thumbnail_attr', array('class'=>$thumb_class) ) );
}
}
else
$thumb = '';
return apply_filters( 'app_thumbnail', $thumb, $page_id, $worker_id );
}
/**
* Build GCal url for GCal Button. It requires UTC time.
* @param start: Timestamp of the start of the app
* @param end: Timestamp of the end of the app
* @param php: If this is called for php. If false, called for js
* @param address: Address of the appointment
* @param city: City of the appointment
* @return string
*/
function gcal( $service, $start, $end, $php=false, $address, $city ) {
// Find time difference from Greenwich as GCal asks UTC
$text = sprintf(__('%s Appointment', 'appointments'), $this->get_service_name($service));
if (!$php) $text = esc_js( $text );
$gmt_start = get_gmt_from_date( date( 'Y-m-d H:i:s', $start ), "Ymd\THis\Z" );
$gmt_end = get_gmt_from_date( date( 'Y-m-d H:i:s', $end ), "Ymd\THis\Z" );
$location = isset($this->options["gcal_location"]) && '' != trim($this->options["gcal_location"])
? esc_js(str_replace(array('ADDRESS', 'CITY'), array($address, $city), $this->options["gcal_location"]))
: esc_js(get_bloginfo('description'))
;
$param = array(
'action' => 'TEMPLATE',
'text' => $text,
'dates' => $gmt_start . "/" . $gmt_end,
'sprop' => 'website:' . home_url(),
'location' => rawurlencode( $location )
);
return add_query_arg(
apply_filters('app_gcal_variables', $param, $service, $start, $end),
'http://www.google.com/calendar/event'
);
}
/**
* Die showing which field has a problem
*
* @param string $field_name
*/
function json_die( $field_name ) {
die( json_encode( array("error"=>sprintf( __( 'Something wrong about the submitted %s', 'appointments'), $this->get_field_name($field_name)))));
}
/**
* Check for too frequent back to back apps
* return true means no spam
* @return bool
*/
function check_spam() {
$options = appointments_get_options();
if (
! isset( $options["spam_time"] )
|| ! $options["spam_time"]
|| ! Appointments_Sessions::is_visitor_appointments_cookie_set()
) {
return true;
}
$apps = Appointments_Sessions::get_current_visitor_appointments();
if ( empty( $apps ) ) {
return true;
}
$checkdate = date( 'Y-m-d H:i:s', $this->local_time - $options["spam_time"] );
$results = appointments_get_appointments( array(
'app_id' => $apps,
'status' => 'pending',
'date_query' => array(
array(
'field' => 'created',
'compare' => '>',
'value' => $checkdate
)
)
) );
// A recent app is found
if ( $results ) {
return false;
}
return true;
}
/**
* Find timestamp of first day of month for a given time
* @param time: input whose first day will be found
* @param add: how many months to add
* @return integer (timestamp)
* @since 1.0.4
*/
function first_of_month( $time, $add ) {
$year = date( "Y", $time );
$month = date( "n", $time ); // Notice "n"
return mktime( 0, 0, 0, $month+$add, 1, $year );
}
/**
* Helper function to create a monthly schedule
*
* @deprecated 2.0.6
*/
function get_monthly_calendar( $timestamp=false, $class='', $long, $widget ) {
_deprecated_function( __FUNCTION__, '2.1', 'appointments_monthly_calendar' );
$this->get_lsw();
$args = array(
'service_id' => $this->service,
'worker_id' => $this->worker,
'location_id' => $this->location,
'class' => $class,
'long' => $long,
'echo' => false,
'widget' => $widget
);
return appointments_monthly_calendar( $timestamp, $args );