/
appointments.php
5118 lines (4397 loc) · 166 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: 1.5.5.1
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 = "1.5.5.1";
public $db_version;
public $local_time;
public $wh_table;
public $exceptions_table;
public $app_table;
public $workers_table;
/** @var AppointmentsGcal|bool */
public $gcal_api;
public $locale_error;
public $time_format;
public $datetime_format;
public $log_file;
public $salt;
public $plugin_dir;
public $worker;
public $location;
public $service;
public $openid;
public $plugin_url;
/** @var Appointments_Admin */
public $admin;
function __construct() {
include_once( 'includes/helpers.php' );
$this->plugin_dir = plugin_dir_path(__FILE__);
$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');
if ( !$this->start_of_week = get_option('start_of_week') ) $this->start_of_week = 0;
$this->time_format = get_option('time_format');
if (empty($this->time_format)) $this->time_format = "H:i";
$this->date_format = get_option('date_format');
if (empty($this->date_format)) $this->date_format = "Y-m-d";
$this->datetime_format = $this->date_format . " " . $this->time_format;
add_action( 'delete_user', array( &$this, 'delete_user' ) ); // Modify database in case a user is deleted
add_action( 'wpmu_delete_user', array( &$this, 'delete_user' ) ); // 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_action( 'init', array( &$this, 'cancel' ), 19 ); // Check cancellation of an appointment
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' );
if ( is_admin() ) {
include_once( 'admin/class-app-admin.php' );
$this->admin = new Appointments_Admin();
}
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
include_once( 'includes/class-app-ajax.php' );
new Appointments_AJAX();
}
// API login after the options have been initialized
add_action('init', array($this, 'setup_api_logins'), 10);
// 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( $this->plugin_dir . '/includes/widgets.php' );
add_action( 'widgets_init', array( &$this, 'widgets_init' ) );
// Buddypress
require_once($this->plugin_dir . '/includes/class_app_buddypress.php');
if (class_exists('App_BuddyPress')) App_BuddyPress::serve();
// Membership2 Integration
$m2_integration = $this->plugin_dir . '/includes/class_app_membership2.php';
if ( file_exists( $m2_integration ) ) {
require_once $m2_integration;
}
// Caching
if ( 'yes' == @$this->options['use_cache'] ) {
add_filter( 'the_content', array( &$this, 'pre_content' ), 8 ); // Check content before do_shortcode
add_filter( 'the_content', array( &$this, 'post_content' ), 100 ); // Serve this later than do_shortcode
add_action( 'wp_footer', array( &$this, 'save_script' ), 8 ); // Save script to database
add_action( 'permalink_structure_changed', array( &$this, 'flush_cache' ) );// Clear cache in case permalink changed
add_action( 'save_post', array( &$this, 'save_post' ), 10, 2 ); // Clear cache if it has shortcodes
}
$this->pages_to_be_cached = array();
$this->had_filter = false; // There can be a wpautop filter. We will check this later on.
// Membership integration
$this->membership_active = false;
add_action( 'plugins_loaded', array( &$this, 'check_membership_plugin') );
// Marketpress integration
$this->marketpress_active = $this->mp = false;
$this->mp_posts = array();
add_action( 'plugins_loaded', array( &$this, 'check_marketpress_plugin') );
$this->gcal_api = false;
add_action('init', array($this, 'setup_gcal_sync'), 10);
// Database variables
global $wpdb;
$this->db = &$wpdb;
$this->wh_table = $wpdb->prefix . "app_working_hours";
$this->exceptions_table = $wpdb->prefix . "app_exceptions";
$this->services_table = $wpdb->prefix . "app_services";
$this->workers_table = $wpdb->prefix . "app_workers";
$this->app_table = $wpdb->prefix . "app_appointments";
$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 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);
}
}
function maybe_upgrade() {
if ( $this->db_version == $this->version ) {
return;
}
if ( $this->db_version != $this->version ) {
wp_cache_flush();
}
update_option( 'app_db_version', $this->version );
}
function setup_gcal_sync () {
// GCal Integration
$this->gcal_api = false;
// Allow forced disabling in case of emergency
if ( !defined( 'APP_GCAL_DISABLE' ) ) {
require_once $this->plugin_dir . '/includes/class.gcal.php';
$this->gcal_api = new AppointmentsGcal();
}
}
function setup_api_logins () {
if (!@$this->options['accept_api_logins']) return false;
add_action('wp_ajax_nopriv_app_facebook_login', array($this, 'handle_facebook_login'));
add_action('wp_ajax_nopriv_app_get_twitter_auth_url', array($this, 'handle_get_twitter_auth_url'));
add_action('wp_ajax_nopriv_app_twitter_login', array($this, 'handle_twitter_login'));
add_action('wp_ajax_nopriv_app_ajax_login', array($this, 'ajax_login'));
add_action('wp_ajax_nopriv_app_google_plus_login', array($this, 'handle_gplus_login'));
// Google+ login
if (!class_exists('LightOpenID')) {
if( function_exists('curl_init') || in_array('https', stream_get_wrappers()) ) {
include_once( $this->plugin_dir . '/includes/external/lightopenid/openid.php' );
$this->openid = new LightOpenID;
}
}
else
$this->openid = new LightOpenID;
if ( @$this->openid ) {
if ( !session_id() )
@session_start();
add_action('wp_ajax_nopriv_app_get_google_auth_url', array($this, 'handle_get_google_auth_url'));
add_action('wp_ajax_nopriv_app_google_login', array($this, 'handle_google_login'));
$this->openid->identity = 'https://www.google.com/accounts/o8/id';
$this->openid->required = array('namePerson/first', 'namePerson/last', 'namePerson/friendly', 'contact/email');
if (!empty($_REQUEST['openid_ns'])) {
$cache = $this->openid->getAttributes();
if (isset($cache['namePerson/first']) || isset($cache['namePerson/last']) || isset($cache['contact/email'])) {
$_SESSION['app_google_user_cache'] = $cache;
}
}
if ( isset( $_SESSION['app_google_user_cache'] ) )
$this->_google_user_cache = $_SESSION['app_google_user_cache'];
else
$this->_google_user_cache = '';
}
}
/**
***************************************************************************************************************
* 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 smallest service ID
* We assume total number of services is not too high, which is the practical case.
* Otherwise this method might be expensive
* @return integer
*/
function get_first_service_id() {
$min = wp_cache_get( 'min_service_id', 'appointments_services' );
if ( false === $min ) {
$services = $this->get_services();
if ( $services ) {
$min = 9999999;
foreach ( $services as $service ) {
if ( $service->ID < $min )
$min = $service->ID;
}
wp_cache_set( 'min_service_id', $min, 'appointments_services' );
}
else
$min = 0; // No services ?? - Not possible but let's be safe
}
return apply_filters('app-services-first_service_id', $min);
}
/**
* 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 = $this->get_first_service_id() )
$service_id = 0;
return $service_id;
}
/**
* Get worker ID from front end
* worker = provider
* @return integer
*/
function get_worker_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;
}
/**
* Allow only certain order_by clauses
* @since 1.2.8
*/
function sanitize_order_by( $order_by="ID" ) {
$whitelist = apply_filters( 'app_order_by_whitelist', array( 'ID', 'name', 'start', 'end', 'duration', 'price',
'ID DESC', 'name DESC', 'start DESC', 'end DESC', 'duration DESC', 'price DESC', 'RAND()' ) );
if ( in_array( $order_by, $whitelist ) )
return $order_by;
else
return 'ID';
}
/**
* Get a single service with given ID
* @param ID: Id of the service to be retrieved
* @return object
*/
function get_service( $ID ) {
return appointments_get_service( $ID );
}
/**
* Get all workers
* @param order_by: ORDER BY clause for mysql
* @return array of objects
*/
function get_workers( $order_by="ID" ) {
$args = array(
'orderby' => $order_by
);
return appointments_get_workers( $args );
// @TODO: Some sorting:
// Sorting by name requires special case
if ( stripos( $order_by, 'name' ) !== false ) {
$workers_ = $this->db->get_results("SELECT * FROM " . $this->workers_table . " " );
if ( stripos( $order_by, 'desc' ) !== false )
usort( $workers_, array( &$this, 'get_workers_desc' ) );
else
usort( $workers_, array( &$this, 'get_workers_asc' ) );
$workers = $workers_;
}
else
$workers = $this->db->get_results("SELECT * FROM " . $this->workers_table . " ORDER BY ". esc_sql($order_by) ." " );
wp_cache_set( $orderby_cache_key, $order_by, 'appointments_workers' );
wp_cache_set( $results_cache_key, $workers, 'appointments_workers' );
}
/**
* Get all services
* @param order_by: ORDER BY clause for mysql
* @return array of objects
*/
function get_services( $order_by="ID" ) {
$args = array( 'orderby'=> $order_by );
return appointments_get_services( $args );
}
/**
* Helper function to sort workers in ascending order
* @since 1.1.9
* @return integer
*/
function get_workers_asc( $a, $b ) {
return strcmp( $this->get_worker_name( $a->ID ), $this->get_worker_name( $b->ID ) );
}
/**
* Helper function to sort workers in descending order
* @since 1.1.9
* @return integer
*/
function get_workers_desc( $a, $b ) {
return strcmp( $this->get_worker_name( $b->ID ), $this->get_worker_name( $a->ID ) );
}
/**
* Get workers giving a specific service (by its ID)
* We assume total number of workers is not too high, which is the practical case.
* Otherwise this method would be expensive
* @param ID: Id of the service to be retrieved
* @param order_by: ORDER BY clause for mysql
* @return array of objects
*/
function get_workers_by_service( $ID, $order_by = "ID" ) {
$workers = appointments_get_workers_by_service( $ID, $order_by );
if ( empty( $workers ) )
return false;
return $workers;
}
/**
* Check if there is only one worker giving the selected service
* @param service: Id of the service for which check will be done
* @since 1.1.1
* @return string (worker ID if there is one, otherwise 0)
*/
function is_single_worker( $service ) {
$workers = $this->get_workers_by_service( $service );
if ( $workers && 1 === count( $workers ) && is_object( $workers[0] ) ) {
return $workers[0]->ID;
}
else return 0;
}
/**
* Return a row from working hours table, i.e. days/hours we are working or we have break
* @param stat: open (works), or closed (breaks).
* @return object
*/
function get_work_break( $l, $w, $stat ) {
$work_break = null;
$cache_key = 'appointments_work_breaks-' . $l . '-' . $w;
$work_breaks = wp_cache_get( $cache_key );
if ( false === $work_breaks ) {
$work_breaks = $this->db->get_results( $this->db->prepare("SELECT * FROM {$this->wh_table} WHERE worker=%d AND location=%d", $w, $l) );
wp_cache_set( $cache_key, $work_breaks );
}
if ( $work_breaks ) {
foreach ( $work_breaks as $wb ) {
if ( $wb->status == $stat ) {
$work_break = $wb;
break;
}
}
}
return $work_break;
}
/**
* Return a row from exceptions table, i.e. days we are working or having holiday
* @return object
*/
function get_exception( $l, $w, $stat ) {
$exception = null;
$exceptions = wp_cache_get( 'exceptions_'. $l . '_' . $w );
if ( false === $exceptions ) {
$exceptions = $this->db->get_results( $this->db->prepare("SELECT * FROM {$this->exceptions_table} WHERE worker=%d AND location=%d", $w, $l) );
wp_cache_set( 'exceptions_'. $l . '_' . $w, $exceptions );
}
if ( $exceptions ) {
foreach ( $exceptions as $e ) {
if ( $e->status == $stat ) {
$exception = $e;
break;
}
}
}
return $exception;
}
/**
* Return an appointment given its ID
* @param app_id: ID of the appointment to be retreived from database
* @since 1.1.8
* @return object
*/
function get_app( $app_id ) {
if ( !$app_id )
return false;
$app = $this->db->get_row( $this->db->prepare("SELECT * FROM {$this->app_table} WHERE ID=%d", $app_id) );
return $app;
}
/**
* Return all reserve appointments (i.e. pending, paid, confirmed or reserved by GCal)
* @param week: Optionally appointments only in the number of week in ISO 8601 format (since 1.2.3).
* Weekly gives much better results in RAM usage compared to monthly, with a tolerable, slight increase in number of queries
* @return array of objects
*/
function get_reserve_apps( $l, $s, $w, $week=0 ) {
return appointments_get_appointments( $l, $s, $w, $week );
}
/**
* 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 ) {
$apps = false;
if ( false === $apps ) {
$services = $this->get_services();
if ( $services ) {
$apps = array();
foreach ( $services as $service ) {
$apps_worker = $this->get_reserve_apps( $l, $service->ID, $w, $week );
if ( $apps_worker )
$apps = array_merge( $apps, $apps_worker );
}
}
}
return $apps;
}
/**
* Return reserve appointments by service ID
* @param week: Optionally appointments only in the number of week in ISO 8601 format (since 1.2.3)
* @since 1.1.3
* @return array of objects
*/
function get_reserve_apps_by_service( $l, $s, $week=0 ) {
if ( false === $apps ) {
$workers = $this->get_workers_by_service( $s );
$apps = array();
if ( $workers ) {
foreach ( $workers as $worker ) {
$apps_service = $this->get_reserve_apps( $l, $s, $worker->ID, $week );
if ( $apps_service )
$apps = array_merge( $apps, $apps_service );
}
}
// Also include appointments by general staff for this service
$apps_service_0 = $this->get_reserve_apps( $l, $s, 0, $week );
if ( $apps_service_0 )
$apps = array_merge( $apps, $apps_service_0 );
// Remove duplicates
$apps = $this->array_unique_object_by_ID( $apps );
}
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
* @return bool
*/
function is_dummy( $user_id=0 ) {
global $wpdb, $current_user;
if ( !$user_id )
$user_id = $current_user->ID;
// A dummy should be a worker
$result = appointments_get_worker( $user_id );
if ( ! $result )
return false;
// This is only supported after V1.0.6 and if DB is altered
if ( !$this->db_version )
return false;
if ( $result->dummy )
return true;
return false;
}
public function is_worker( $worker_id ) {
return appointments_is_worker( $worker_id );
}
/**
* Find worker name given his ID
* @return string
*/
function get_worker_name( $worker=0, $php=true ) {
global $current_user;
$user_name = '';
if ( 0 == $worker ) {
// Show different text to authorized people
if ( is_admin() || App_Roles::current_user_can( 'manage_options', App_Roles::CTX_STAFF ) || appointments_is_worker( $current_user->ID ) )
$user_name = __('Our staff', 'my-plugin');
else
$user_name = __('A specialist', 'my-plugin');
}
else {
$userdata = get_userdata( $worker );
if (is_object($userdata) && !empty($userdata->app_name)) {
$user_name = $userdata->app_name;
}
if (empty($user_name)) {
if ( !$php )
$user_name = $userdata->user_login;
else
$user_name = $userdata->display_name;
if ( !$user_name ){
$first_name = get_user_meta($worker, 'first_name', true);
$last_name = get_user_meta($worker, 'last_name', true);
$user_name = $first_name . " " . $last_name;
}
if ( "" == trim( $user_name ) )
$user_name = $userdata->user_login;
}
}
return apply_filters( 'app_get_worker_name', $user_name, $worker );
}
/**
* Find worker email given his ID
* since 1.0.6
* @return string
*/
function get_worker_email( $worker=0 ) {
// Real person
if ( !$this->is_dummy( $worker ) ) {
$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', 'my-plugin');
$result = $this->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 = $this->db->get_row( $this->db->prepare("SELECT * FROM {$this->app_table} WHERE ID=%d", $app_id) );
if ( $result !== null ) {
// 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();
$service_obj = $this->get_service( $this->service );
$worker_obj = appointments_get_worker( $this->worker );
if ( $worker_obj && $worker_obj->price )
$worker_price = $worker_obj->price;
else
$worker_price = 0;
$price = $service_obj->price + $worker_price;
/**
* 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() {
$capacity = false;
if ( false === $capacity ) {
// If no worker is defined, capacity is always 1
$count = count( $this->get_workers() );
if ( !$count ) {
$capacity = 1;
}
else {
// Else, find number of workers giving that service and capacity of the service
$worker_count = count( $this->get_workers_by_service( $this->service ) );
$service = $this->get_service( $this->service );
if ( $service != null ) {
if ( !$service->capacity ) {
$capacity = $worker_count; // No service capacity limit
}
else
$capacity = min( $service->capacity, $worker_count ); // Return whichever smaller
}
else
$capacity = 1; // No service ?? - Not possible but let's be safe
}
}
return apply_filters( 'app_get_capacity', $capacity, $this->service, $this->worker );
}
/**
**************************************
* Methods for Specific Content Caching
* Developed especially for this plugin
**************************************
*/
/**
* Check if plugin should use cache
* Available for visitors for the moment
* TODO: extend this for logged in users too
* @since 1.0.2
*/
function use_cache() {
if ( 'yes' == $this->options["use_cache"] && !is_user_logged_in() )
return true;
return false;
}
/**
* Add a post ID to the array to be cached
*
*/
function add_to_cache( $post_id ) {
if ( $this->use_cache() )
$this->pages_to_be_cached[] = $post_id;
}
/**
* Serve content from cache DB if is available and post is supposed to be cached
* This is called before do_shortcode (this method's priority: 8)
* @return string (the content)
*/
function pre_content( $content ) {
global $post;
// Check if this page is to be cached
if ( !in_array( $post->ID, $this->pages_to_be_cached ) )
return $content;
// Get uri and mark it for other functions too
// The other functions are called after this (content with priority 100 and the other with footer hook)
$this->uri = $this->get_uri();
$result = $this->db->get_row( $this->db->prepare("SELECT * FROM {$this->cache_table} WHERE uri=%s", $this->uri) );
if ( $result != null ) {
// Clear uri so other functions do not deal with update/insert
$this->uri = false;
// We need to serve the scripts too
$this->script = $result->script;
// If wpautop had filter, it is almost certain that it was removed
if ( $this->had_filter )
$new_content = $result->content;
else
$new_content = wpautop( $result->content );
return $new_content . '<!-- Served from WPMU DEV Appointments+ Cache '. $result->created .' -->';
}
// If cache is empty return content
// If wpautop had filter, it is almost certain that it was removed
if ( $this->had_filter )
return $content;
else
return wpautop( $content ); // Add wpautop which we removed before
}
/**
* Save newly created content to cache DB
* @return string (the content)
*/
function post_content( $content ) {
// Check if this page is to be cached.
if ( !$this->uri )
return $content;
// Also don't save empty content
if ( !trim( $content ) ) {
$this->uri = '';
return $content;
}
// At this point it means there is no such a row, so we can safely insert
$this->db->insert( $this->cache_table,
array(
'uri' => $this->uri,
'created' => date ("Y-m-d H:i:s", $this->local_time ),
'content' => $content
)
);
return $content;
}
/**
* Save newly created scripts at wp footer location
* @return none
*/
function save_script() {
// Check if this page is to be cached
if ( !$this->uri || !trim( $this->script ) )
return;
// There must be already such a row
$this->db->update( $this->cache_table,
array( 'script' => $this->script ),
array( 'uri' => $this->uri )
);
}
/**
* Get request uri
* @return string
*/
function get_uri() {
// Get rid of # part
if ( strpos( $_SERVER['REQUEST_URI'], '#' ) !== false ) {
$uri_arr = explode( '#', $_SERVER['REQUEST_URI'] );
$uri = $uri_arr[0];
}
else
$uri = $_SERVER['REQUEST_URI'];
return $uri;
}
/**
* Clear cache in case saved post has our shortcodes
* @return none
*/
function save_post( $post_id, $post ) {
if ( strpos( $post->post_content, '[app_' ) !== false )
$this->flush_cache();
}
/**
* Flush both database and object caches
*
*/
function flush_cache( ) {
wp_cache_flush();
if ( 'yes' == @$this->options["use_cache"] )
$result = $this->db->query( "TRUNCATE TABLE {$this->cache_table} " );
}
/****************
* General methods
*****************
*/
/**