booked.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. <?php
  2. /*
  3. Plugin Name: Booked
  4. Plugin URI: https://getbooked.io
  5. Description: Powerful appointment booking made simple.
  6. Version: 2.1
  7. Author: Boxy Studio
  8. Author URI: https://boxystudio.com
  9. Text Domain: booked
  10. */
  11. define( 'BOOKED_VERSION', '2.1' );
  12. define( 'BOOKED_WELCOME_SCREEN', get_option('booked_welcome_screen',true) );
  13. define( 'BOOKED_DEMO_MODE', get_option('booked_demo_mode',false) );
  14. define( 'BOOKED_PLUGIN_URL', untrailingslashit( plugin_dir_url( __FILE__ ) ) );
  15. define( 'BOOKED_PLUGIN_DIR', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
  16. define( 'BOOKED_STYLESHEET_DIR', get_stylesheet_directory() );
  17. define( 'BOOKED_PLUGIN_TEMPLATES_DIR', BOOKED_PLUGIN_DIR . '/templates/' );
  18. define( 'BOOKED_AJAX_INCLUDES_DIR', BOOKED_PLUGIN_DIR . '/includes/ajax/' );
  19. // Included Add-Ons - Installation/Activation
  20. define( 'BOOKED_WC_VERSION', '1.4.9' ); // Updated in 2.0.10
  21. define( 'BOOKED_FEA_VERSION', '1.1.15' ); // ---
  22. define( 'BOOKED_CF_VERSION', '1.1.5' ); // Updated in 2.0.7
  23. require_once BOOKED_PLUGIN_DIR . '/includes/add-ons/init.php';
  24. // Booked Updates
  25. require_once BOOKED_PLUGIN_DIR . '/includes/updates/plugin-update-checker.php';
  26. $booked_update_check = PucFactory::buildUpdateChecker('http://boxyupdates.com/get/?action=get_metadata&slug=booked', __FILE__, 'booked');
  27. // Booked Mailer Functions
  28. require_once('includes/mailer_functions.php');
  29. if(!class_exists('booked_plugin')) {
  30. class booked_plugin {
  31. /**
  32. * Construct the plugin object
  33. */
  34. public function __construct() {
  35. $this->booked_screens = apply_filters('booked_admin_booked_screens', array('booked-pending','booked-appointments','booked-settings','booked-welcome'));
  36. require_once(sprintf("%s/post-types/booked_appointments.php", BOOKED_PLUGIN_DIR));
  37. $booked_appointments_post_type = new booked_appointments_post_type();
  38. require_once(sprintf("%s/includes/general-functions.php", BOOKED_PLUGIN_DIR));
  39. require_once(sprintf("%s/includes/shortcodes.php", BOOKED_PLUGIN_DIR));
  40. require_once(sprintf("%s/includes/widgets.php", BOOKED_PLUGIN_DIR));
  41. require_once(sprintf("%s/includes/ajax/init.php", BOOKED_PLUGIN_DIR));
  42. require_once(sprintf("%s/includes/ajax/init_admin.php", BOOKED_PLUGIN_DIR));
  43. $booked_ajax = new Booked_AJAX();
  44. $booked_admin_ajax = new Booked_Admin_AJAX();
  45. add_action('admin_init', array(&$this, 'admin_init'), 9);
  46. add_action('admin_menu', array(&$this, 'add_menu'));
  47. add_action('admin_bar_menu', array(&$this, 'add_admin_bar_menu'), 65);
  48. add_action('the_posts', array(&$this, 'add_to_calendar_check'));
  49. add_action('admin_enqueue_scripts', array(&$this, 'admin_styles'));
  50. add_action('admin_enqueue_scripts', array(&$this, 'admin_scripts'));
  51. add_action('manage_users_custom_column', array(&$this, 'booked_add_custom_user_columns'), 15, 3);
  52. add_filter('manage_users_columns', array(&$this, 'booked_add_user_columns'), 15, 1);
  53. add_filter('user_contactmethods', array(&$this, 'booked_phone_numbers'));
  54. add_action('booked_profile_tabs', array(&$this, 'booked_profile_tabs'));
  55. add_action('booked_profile_tab_content', array(&$this, 'booked_profile_tab_content'));
  56. add_action('wp_enqueue_scripts', array(&$this, 'front_end_scripts'),1);
  57. add_action('admin_menu', array(&$this, 'booked_add_pending_appt_bubble' ));
  58. add_action('admin_notices', array(&$this, 'booked_pending_notice' ));
  59. add_action('admin_notices', array(&$this, 'booked_no_profile_page_notice' ));
  60. add_action('parent_file', array(&$this, 'booked_tax_menu_correction'));
  61. add_action( 'booked_custom_calendars_add_form_fields', array(&$this, 'booked_calendars_add_custom_fields'), 10, 2 );
  62. add_action( 'booked_custom_calendars_edit_form_fields', array(&$this, 'booked_calendars_edit_custom_fields'), 10, 2 );
  63. add_action( 'create_booked_custom_calendars', array(&$this, 'booked_save_calendars_custom_fields'), 10, 2 );
  64. add_action( 'edited_booked_custom_calendars', array(&$this, 'booked_save_calendars_custom_fields'), 10, 2 );
  65. add_action('init', array(&$this, 'init'),10);
  66. // Prevent WooCommerce from Redirecting "Booking Agents" to the My Account page.
  67. add_filter('woocommerce_prevent_admin_access', array(&$this, 'booked_wc_check_admin_access'));
  68. // Allow other plugins/themes to apply Booked capabilities to other user roles
  69. add_filter( 'booked_user_roles', array(&$this,'booked_user_roles_filter') );
  70. // Email Reminders (Added in v1.8.0)
  71. add_filter( 'cron_schedules', array(&$this,'cron_schedules'));
  72. add_action( 'booked_send_admin_reminders', array($this, 'admin_reminders'), 20 );
  73. add_action( 'booked_send_user_reminders', array($this, 'user_reminders'), 20 );
  74. $user_email_content = get_option('booked_reminder_email',false);
  75. $user_email_subject = get_option('booked_reminder_email_subject',false);
  76. if ($user_email_content && $user_email_subject):
  77. if ( !wp_next_scheduled('booked_send_user_reminders') ):
  78. wp_schedule_event( time(),'booked_everyfive','booked_send_user_reminders' );
  79. endif;
  80. else:
  81. wp_clear_scheduled_hook( 'booked_send_user_reminders' );
  82. endif;
  83. $admin_email_content = get_option('booked_admin_reminder_email',false);
  84. $admin_email_subject = get_option('booked_admin_reminder_email_subject',false);
  85. if ($admin_email_content && $admin_email_subject):
  86. if ( !wp_next_scheduled('booked_send_admin_reminders') ):
  87. wp_schedule_event(time(),'booked_everyfive','booked_send_admin_reminders');
  88. endif;
  89. else:
  90. wp_clear_scheduled_hook('booked_send_admin_reminders');
  91. endif;
  92. }
  93. public static function admin_reminders(){
  94. $admin_reminder_buffer = get_option('booked_admin_reminder_buffer',30);
  95. $start_timestamp = current_time('timestamp');
  96. $end_timestamp = strtotime(date_i18n('Y-m-d H:i:s',current_time('timestamp')).' + '.$admin_reminder_buffer.' minutes');
  97. $args = array(
  98. 'post_type' => 'booked_appointments',
  99. 'posts_per_page' => -1,
  100. 'post_status' => array('publish','future'),
  101. 'meta_query' => array(
  102. array(
  103. 'key' => '_appointment_timestamp',
  104. 'value' => array( $start_timestamp, $end_timestamp ),
  105. 'compare' => 'BETWEEN',
  106. )
  107. )
  108. );
  109. $bookedAppointments = new WP_Query($args);
  110. if( $bookedAppointments->have_posts() ):
  111. while ( $bookedAppointments->have_posts() ):
  112. $bookedAppointments->the_post();
  113. global $post;
  114. $appt_id = $post->ID;
  115. $reminder_sent = get_post_meta($appt_id,'_appointment_admin_reminder_sent',true);
  116. $calendars = get_the_terms( $appt_id, 'booked_custom_calendars' );
  117. if ( !empty($calendars) ):
  118. foreach( $calendars as $calendar ):
  119. $calendar_id = $calendar->term_id;
  120. endforeach;
  121. else:
  122. $calendar_id = false;
  123. endif;
  124. if ( !$reminder_sent && apply_filters( 'booked_prepare_sending_reminder', true, $appt_id ) ):
  125. $email_content = get_option('booked_admin_reminder_email',false);
  126. $email_subject = get_option('booked_admin_reminder_email_subject',false);
  127. if ($email_content && $email_subject):
  128. $admin_email = booked_which_admin_to_send_email( $calendar_id );
  129. $token_replacements = booked_get_appointment_tokens( $appt_id );
  130. $email_content = booked_token_replacement( $email_content,$token_replacements );
  131. $email_subject = booked_token_replacement( $email_subject,$token_replacements );
  132. update_post_meta($appt_id,'_appointment_admin_reminder_sent',true);
  133. do_action( 'booked_admin_reminder_email', $admin_email, $email_subject, $email_content, $token_replacements['email'], $token_replacements['name'] );
  134. endif;
  135. endif;
  136. endwhile;
  137. endif;
  138. wp_reset_postdata();
  139. }
  140. public static function user_reminders(){
  141. $user_reminder_buffer = get_option('booked_reminder_buffer',30);
  142. $start_timestamp = current_time('timestamp');
  143. $end_timestamp = strtotime(date_i18n('Y-m-d H:i:s',current_time('timestamp')).' + '.$user_reminder_buffer.' minutes');
  144. $args = array(
  145. 'post_type' => 'booked_appointments',
  146. 'posts_per_page' => -1,
  147. 'post_status' => array('publish','future'),
  148. 'meta_query' => array(
  149. array(
  150. 'key' => '_appointment_timestamp',
  151. 'value' => array( $start_timestamp, $end_timestamp ),
  152. 'compare' => 'BETWEEN',
  153. )
  154. )
  155. );
  156. $bookedAppointments = new WP_Query($args);
  157. if($bookedAppointments->have_posts()):
  158. while ($bookedAppointments->have_posts()):
  159. $bookedAppointments->the_post();
  160. global $post;
  161. $appt_id = $post->ID;
  162. $reminder_sent = get_post_meta($appt_id,'_appointment_user_reminder_sent',true);
  163. $send_mail = true;
  164. if ( !$reminder_sent && apply_filters( 'booked_prepare_sending_reminder', true, $appt_id ) ):
  165. $email_content = get_option('booked_reminder_email',false);
  166. $email_subject = get_option('booked_reminder_email_subject',false);
  167. if ($email_content && $email_subject):
  168. $token_replacements = booked_get_appointment_tokens( $appt_id );
  169. $email_content = booked_token_replacement( $email_content,$token_replacements );
  170. $email_subject = booked_token_replacement( $email_subject,$token_replacements );
  171. update_post_meta($appt_id,'_appointment_user_reminder_sent',true);
  172. do_action( 'booked_reminder_email', $token_replacements['email'], $email_subject, $email_content );
  173. endif;
  174. endif;
  175. endwhile;
  176. endif;
  177. wp_reset_postdata();
  178. }
  179. public static function cron_schedules( $schedules ) {
  180. $schedules['booked_everyfive'] = array(
  181. 'interval' => 60 * 5,
  182. 'display' => esc_html__('Every Five Minutes', 'booked')
  183. );
  184. return $schedules;
  185. }
  186. public static function activate() {
  187. set_transient( '_booked_welcome_screen_activation_redirect', true, 30 );
  188. }
  189. public function booked_wc_check_admin_access( $redirect_to ) {
  190. $booked_current_user = wp_get_current_user();
  191. if( is_array( $booked_current_user->roles ) && in_array( 'booked_booking_agent', $booked_current_user->roles ) ) {
  192. return false;
  193. }
  194. return $redirect_to;
  195. }
  196. public function admin_init() {
  197. if (isset($_POST['booked_export_appointments_csv'])):
  198. include('includes/export-csv.php');
  199. endif;
  200. $booked_redirect_non_admins = get_option('booked_redirect_non_admins',false);
  201. // Redirect non-admin users
  202. if ($booked_redirect_non_admins):
  203. if (!current_user_can('edit_booked_appointments') && !defined( 'DOING_AJAX' )){
  204. $booked_profile_page = booked_get_profile_page();
  205. if ($booked_profile_page):
  206. $redirect_url = get_permalink($booked_profile_page);
  207. else:
  208. $redirect_url = home_url();
  209. endif;
  210. wp_redirect( $redirect_url );
  211. exit;
  212. }
  213. endif;
  214. // Set up the settings for this plugin
  215. require_once(sprintf("%s/includes/admin-functions.php", BOOKED_PLUGIN_DIR));
  216. require_once(sprintf("%s/includes/dashboard-widget.php", BOOKED_PLUGIN_DIR));
  217. $this->init_settings();
  218. // Welcome Screen Redirect
  219. if ( !get_transient( '_booked_welcome_screen_activation_redirect' ) ) {
  220. return;
  221. }
  222. delete_transient( '_booked_welcome_screen_activation_redirect' );
  223. if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
  224. return;
  225. }
  226. if (BOOKED_WELCOME_SCREEN):
  227. wp_safe_redirect( add_query_arg( array( 'page' => 'booked-welcome' ), admin_url( 'admin.php' ) ) );
  228. exit;
  229. endif;
  230. // END Welcome Screen Redirect
  231. return;
  232. } // END public static function activate
  233. public function booked_curl($url){
  234. if ( function_exists('curl_init') ):
  235. $ch = curl_init();
  236. $timeout = 5;
  237. curl_setopt($ch, CURLOPT_URL, $url);
  238. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  239. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  240. $data = curl_exec($ch);
  241. curl_close($ch);
  242. if ($data):
  243. return $data;
  244. else:
  245. return false;
  246. endif;
  247. else:
  248. return false;
  249. endif;
  250. }
  251. public function booked_profile_tabs($default_tabs){
  252. foreach($default_tabs as $slug => $name):
  253. echo '<li'.($name['class'] ? ' class="'.$name['class'].'"' : '').'><a href="#'.$slug.'"><i class="booked-icon '.$name['booked-icon'].'"></i>'.$name['title'].'</a></li>';
  254. endforeach;
  255. }
  256. public function booked_profile_tab_content($default_tabs){
  257. foreach($default_tabs as $slug => $name):
  258. echo '<div id="profile-'.$slug.'" class="booked-tab-content bookedClearFix">';
  259. call_user_func('booked_profile_content_'.$slug);
  260. echo '</div>';
  261. endforeach;
  262. }
  263. public function init() {
  264. // Hide the Admin Bar from subscribers.
  265. $booked_current_user = wp_get_current_user();
  266. if ( isset($booked_current_user->roles[0]) && in_array( 'subscriber',$booked_current_user->roles ) ) {
  267. add_filter('show_admin_bar', '__return_false');
  268. }
  269. // Include the Booked functions file.
  270. require_once(sprintf("%s/includes/functions.php", BOOKED_PLUGIN_DIR));
  271. // Start a session if none is started yet.
  272. if( !session_id() ) {
  273. session_start();
  274. }
  275. // Check to see if the plugin was updated.
  276. $current_version = get_option('booked_version_check','1.6.20');
  277. if ( version_compare( $current_version, BOOKED_VERSION ) < 0 && !BOOKED_DEMO_MODE):
  278. update_option('booked_version_check',BOOKED_VERSION);
  279. set_transient( '_booked_welcome_screen_activation_redirect', true, 60 );
  280. set_transient('booked_show_new_tags',true,60*60*24*15);
  281. else:
  282. update_option('booked_version_check',BOOKED_VERSION);
  283. endif;
  284. }
  285. public function add_to_calendar_check($posts){
  286. if (empty($posts)):
  287. return $posts;
  288. endif;
  289. $found = false;
  290. foreach ($posts as $post):
  291. $profile_shortcode = stripos($post->post_content, '[booked-profile');
  292. $appts_shortcode = stripos($post->post_content, '[booked-appointments');
  293. if ( $profile_shortcode !== false || $appts_shortcode !== false):
  294. $found = true;
  295. break;
  296. endif;
  297. endforeach;
  298. return $posts;
  299. }
  300. static function plugin_settings() {
  301. $plugin_options = array(
  302. 'booked_login_redirect_page',
  303. 'booked_custom_login_message',
  304. 'booked_appointment_redirect_type',
  305. 'booked_appointment_success_redirect_page',
  306. 'booked_registration_name_requirements',
  307. 'booked_hide_admin_bar_menu',
  308. 'booked_timeslot_intervals',
  309. 'booked_appointment_buffer',
  310. 'booked_appointment_limit',
  311. 'booked_cancellation_buffer',
  312. 'booked_new_appointment_default',
  313. 'booked_prevent_appointments_before',
  314. 'booked_prevent_appointments_after',
  315. 'booked_booking_type',
  316. 'booked_require_guest_email_address',
  317. 'booked_hide_default_calendar',
  318. 'booked_hide_unavailable_timeslots',
  319. 'booked_hide_google_link',
  320. 'booked_hide_weekends',
  321. 'booked_dont_allow_user_cancellations',
  322. 'booked_show_only_titles',
  323. 'booked_hide_end_times',
  324. 'booked_hide_available_timeslots',
  325. 'booked_public_appointments',
  326. 'booked_redirect_non_admins',
  327. 'booked_light_color',
  328. 'booked_dark_color',
  329. 'booked_button_color',
  330. 'booked_email_logo',
  331. 'booked_default_email_user',
  332. 'booked_email_force_sender',
  333. 'booked_email_force_sender_from',
  334. 'booked_emailer_disabled',
  335. 'booked_reminder_buffer',
  336. 'booked_admin_reminder_buffer',
  337. 'booked_reminder_email',
  338. 'booked_admin_reminder_email',
  339. 'booked_reminder_email_subject',
  340. 'booked_admin_reminder_email_subject',
  341. 'booked_registration_email_subject',
  342. 'booked_registration_email_content',
  343. 'booked_approval_email_content',
  344. 'booked_approval_email_subject',
  345. 'booked_cancellation_email_content',
  346. 'booked_cancellation_email_subject',
  347. 'booked_appt_confirmation_email_content',
  348. 'booked_appt_confirmation_email_subject',
  349. 'booked_admin_appointment_email_content',
  350. 'booked_admin_appointment_email_subject',
  351. 'booked_admin_cancellation_email_content',
  352. 'booked_admin_cancellation_email_subject'
  353. );
  354. return $plugin_options;
  355. }
  356. public function init_settings() {
  357. $plugin_options = $this->plugin_settings();
  358. foreach($plugin_options as $option_name) {
  359. register_setting('booked_plugin-group', $option_name);
  360. }
  361. }
  362. public function booked_phone_numbers($profile_fields) {
  363. $profile_fields['booked_phone'] = esc_html__('Phone Number','booked');
  364. return $profile_fields;
  365. }
  366. /**********************
  367. ADD MENUS FUNCTION
  368. **********************/
  369. public function add_menu() {
  370. add_menu_page( esc_html__('Appointments','booked'), esc_html__('Appointments','booked'), 'edit_booked_appointments', 'booked-appointments', array(&$this, 'admin_calendar'), 'dashicons-calendar-alt', 58 );
  371. add_submenu_page('booked-appointments', esc_html__('Pending','booked'), esc_html__('Pending','booked'), 'edit_booked_appointments', 'booked-pending', array(&$this, 'admin_pending_list'));
  372. add_submenu_page('booked-appointments', esc_html__('Calendars','booked'), esc_html__('Calendars','booked'), 'manage_booked_options', 'edit-tags.php?taxonomy=booked_custom_calendars');
  373. add_submenu_page('booked-appointments', esc_html__('Settings','booked'), esc_html__('Settings','booked'), 'edit_booked_appointments', 'booked-settings', array(&$this, 'plugin_settings_page'));
  374. add_submenu_page('booked-appointments', esc_html__('What\'s New?','booked'), esc_html__('What\'s New?','booked'), 'manage_booked_options', 'booked-welcome', array(&$this, 'booked_welcome_content'));
  375. }
  376. public function add_admin_bar_menu() {
  377. $hide_menu = get_option('booked_hide_admin_bar_menu',false);
  378. if (!$hide_menu):
  379. global $wp_admin_bar;
  380. $wp_admin_bar->add_menu(array('id' => 'booked', 'title' => '<span class="ab-icon"></span>'.esc_html__('Appointments','booked'), 'href' => get_admin_url().'admin.php?page=booked-appointments'));
  381. $wp_admin_bar->add_menu(array('parent' => 'booked', 'title' => esc_html__('Appointments','booked'), 'id' => 'booked-appointments', 'href' => get_admin_url().'admin.php?page=booked-appointments'));
  382. $wp_admin_bar->add_menu(array('parent' => 'booked', 'title' => esc_html__('Pending','booked'), 'id' => 'booked-pending', 'href' => get_admin_url().'admin.php?page=booked-pending'));
  383. if (current_user_can('manage_booked_options')):
  384. $wp_admin_bar->add_menu(array('parent' => 'booked', 'title' => esc_html__('Calendars','booked'), 'id' => 'booked-calendars', 'href' => get_admin_url().'edit-tags.php?taxonomy=booked_custom_calendars'));
  385. endif;
  386. $wp_admin_bar->add_menu(array('parent' => 'booked', 'title' => esc_html__('Settings','booked'), 'id' => 'booked-settings', 'href' => get_admin_url().'admin.php?page=booked-settings'));
  387. endif;
  388. }
  389. public function booked_welcome_content(){
  390. include(sprintf("%s/templates/welcome.php", BOOKED_PLUGIN_DIR));
  391. }
  392. // Move Taxonomy (custom calendars) to Appointments Menu
  393. public function booked_tax_menu_correction($parent_file) {
  394. global $current_screen;
  395. $taxonomy = $current_screen->taxonomy;
  396. if ($taxonomy == 'booked_custom_calendars')
  397. $parent_file = 'booked-appointments';
  398. return $parent_file;
  399. }
  400. // Booked Settings
  401. public function plugin_settings_page() {
  402. if(!current_user_can('edit_booked_appointments')) {
  403. wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'booked'));
  404. }
  405. include(sprintf("%s/templates/settings.php", BOOKED_PLUGIN_DIR));
  406. }
  407. // Booked Pending Appointments List
  408. public function admin_pending_list() {
  409. if(!current_user_can('edit_booked_appointments')) {
  410. wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'booked'));
  411. }
  412. include(sprintf("%s/templates/pending-list.php", BOOKED_PLUGIN_DIR));
  413. }
  414. // Booked Appointment Calendar
  415. public function admin_calendar() {
  416. if(!current_user_can('edit_booked_appointments')) {
  417. wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'booked'));
  418. }
  419. include(sprintf("%s/templates/admin-calendar.php", BOOKED_PLUGIN_DIR));
  420. }
  421. // Add Pending Appointments Bubble
  422. public function booked_add_pending_appt_bubble() {
  423. global $submenu;
  424. $pending = booked_pending_appts_count();
  425. foreach ( $submenu as $key => $value ) :
  426. if ( $key == 'booked-appointments' ) :
  427. if ( $pending ) { $submenu[$key][1][0] .= " <span style='position:relative; top:1px; margin:0 0 0 2px' class='update-plugins count-$pending' title='$pending'><span style='padding:0 3px 0 1px; display:inline-block; min-width:5px; text-align:center;' class='update-count'>" . $pending . "</span></span>"; }
  428. return;
  429. endif;
  430. endforeach;
  431. }
  432. public function booked_no_profile_page_notice() {
  433. if (current_user_can('manage_booked_options')):
  434. $booked_booking_type = get_option('booked_booking_type','registered');
  435. $booked_redirect_type = get_option('booked_appointment_redirect_type',false);
  436. $booked_profile_page = booked_get_profile_page();
  437. $page = (isset($_GET['page']) ? $page = $_GET['page'] : $page = false);
  438. if ($booked_booking_type == 'registered' && $booked_redirect_type == 'booked-profile' && !$booked_profile_page && $page != 'booked-welcome'):
  439. echo '<div class="update-nag" style="line-height:28px; border-left-color:#DB5933;">';
  440. echo sprintf(esc_html__( 'You need to create a page with the %s shortcode. It is required with your current settings.','booked' ),'<code>[booked-profile]</code>').'&nbsp;&nbsp;&nbsp;<a href="'.get_admin_url().'post-new.php?post_type=page" class="button-primary">'.esc_html__('Create a Page','booked').'</a>&nbsp;&nbsp;<a href="'.get_admin_url().'admin.php?page=booked-settings" class="button">'.esc_html__('Change Settings','booked').'</a>';
  441. echo '</div>';
  442. endif;
  443. endif;
  444. }
  445. public function booked_pending_notice() {
  446. if (current_user_can('edit_booked_appointments')):
  447. $pending = booked_pending_appts_count();
  448. $page = (isset($_GET['page']) ? $page = $_GET['page'] : $page = false);
  449. if ($pending && $page != 'booked-pending' && $page != 'booked-welcome'):
  450. echo '<div class="update-nag" style="line-height:28px">';
  451. echo sprintf( _n( 'There is %s pending appointment.', 'There are %s pending appointments.', $pending, 'booked' ), $pending ).'&nbsp;&nbsp;&nbsp;<a href="'.get_admin_url().'admin.php?page=booked-pending" class="button">'._n('View Pending Appointment','View Pending Appointments',$pending,'booked').'</a>';
  452. echo '</div>';
  453. endif;
  454. endif;
  455. }
  456. /**********************
  457. ADD USER FIELD TO CALENDAR TAXONOMY PAGE
  458. **********************/
  459. public function booked_calendars_add_custom_fields($tag) {
  460. ?><div class="form-field">
  461. <label for="term_meta[notifications_user_id]"><?php esc_html_e('Assign this calendar to','booked'); ?>:</label>
  462. <select name="term_meta[notifications_user_id]" id="term_meta[notifications_user_id]">
  463. <option value=""><?php esc_html_e('Default','booked'); ?></option><?php
  464. $all_users = get_users();
  465. $allowed_users = array();
  466. foreach ( $all_users as $user ):
  467. $wp_user = new WP_User($user->ID);
  468. if ( in_array( 'administrator', $wp_user->roles ) || in_array( 'booked_booking_agent', $wp_user->roles ) ):
  469. array_push($allowed_users, $user);
  470. endif;
  471. endforeach;
  472. if(!empty($allowed_users)) :
  473. foreach($allowed_users as $u) :
  474. $user_id = $u->ID;
  475. $email = $u->data->user_email; ?>
  476. <option value="<?php echo $email; ?>"><?php echo $email; ?></option><?php
  477. endforeach;
  478. endif;
  479. ?></select>
  480. <p><?php esc_html_e('This will use your setting from the Booked Settings panel by default.','booked'); ?></p>
  481. </div><?php
  482. }
  483. public function booked_calendars_edit_custom_fields($tag) {
  484. $t_id = $tag->term_id;
  485. $term_meta = get_option( "taxonomy_$t_id" );
  486. $selected_value = $term_meta['notifications_user_id'];
  487. $all_users = get_users();
  488. $allowed_users = array();
  489. foreach ( $all_users as $user ):
  490. $wp_user = new WP_User($user->ID);
  491. if ( in_array( 'administrator', $wp_user->roles ) || in_array( 'booked_booking_agent', $wp_user->roles ) ):
  492. array_push($allowed_users, $user);
  493. endif;
  494. endforeach; ?>
  495. <tr class="form-field">
  496. <th scope="row" valign="top">
  497. <label for="term_meta[notifications_user_id]"><?php esc_html_e('Assign this calendar to','booked'); ?>:</label>
  498. </th>
  499. <td>
  500. <select name="term_meta[notifications_user_id]" id="term_meta[notifications_user_id]">
  501. <option value=""><?php esc_html_e('Default','booked'); ?></option>
  502. <?php if(!empty($allowed_users)) :
  503. foreach($allowed_users as $u) :
  504. $user_id = $u->ID;
  505. $email = $u->data->user_email; ?>
  506. <option value="<?php echo $email; ?>"<?php echo ($selected_value == $email ? ' selected="selected"' : ''); ?>><?php echo $email; ?></option>
  507. <?php endforeach;
  508. endif; ?>
  509. </select><br>
  510. <span class="description"><?php esc_html_e('This will use your setting from the Booked Settings panel by default.'); ?></span>
  511. </td>
  512. </tr><?php
  513. }
  514. /**********************
  515. SAVE USER FIELD FROM CALENDAR TAXONOMY PAGE
  516. **********************/
  517. public function booked_save_calendars_custom_fields( $term_id ) {
  518. if ( isset( $_POST['term_meta'] ) ) {
  519. $t_id = $term_id;
  520. $term_meta = get_option( "taxonomy_$t_id" );
  521. $cat_keys = array_keys( $_POST['term_meta'] );
  522. foreach ( $cat_keys as $key ) {
  523. if ( isset ( $_POST['term_meta'][$key] ) ) {
  524. $term_meta[$key] = $_POST['term_meta'][$key];
  525. }
  526. }
  527. update_option( "taxonomy_$t_id", $term_meta );
  528. }
  529. }
  530. /**********************
  531. ADD USER COLUMN FOR APPOINTMENT COUNTS
  532. **********************/
  533. public function booked_add_user_columns( $defaults ) {
  534. $defaults['booked_appointments'] = esc_html__('Appointments', 'booked');
  535. return $defaults;
  536. }
  537. public function booked_add_custom_user_columns( $value, $column_name, $id ) {
  538. if ( $column_name == 'booked_appointments' ) {
  539. $args = array(
  540. 'posts_per_page' => -1,
  541. 'meta_key' => '_appointment_timestamp',
  542. 'orderby' => 'meta_value_num',
  543. 'order' => 'ASC',
  544. 'meta_query' => array(
  545. array(
  546. 'key' => '_appointment_timestamp',
  547. 'value' => strtotime(date_i18n('Y-m-d H:i:s')),
  548. 'compare' => '>=',
  549. ),
  550. ),
  551. 'author' => $id,
  552. 'post_type' => 'booked_appointments',
  553. 'post_status' => array('publish','future'),
  554. 'suppress_filters' => true );
  555. $appointments = get_posts($args);
  556. $count = count($appointments);
  557. $appointments = array_slice($appointments, 0, 5);
  558. $time_format = get_option('time_format');
  559. $date_format = get_option('date_format');
  560. ob_start();
  561. if ($count){
  562. echo '<strong>'.$count.' '._n('Upcoming Appointment','Upcoming Appointments',$count,'booked').':</strong>';
  563. echo '<span style="font-size:12px;">';
  564. foreach($appointments as $appointment):
  565. $timeslot = get_post_meta($appointment->ID, '_appointment_timeslot',true);
  566. $timeslot = explode('-',$timeslot);
  567. $timestamp = get_post_meta($appointment->ID, '_appointment_timestamp',true);
  568. echo '<br>' . date_i18n($date_format,$timestamp) . ' @ ' . date_i18n($time_format,strtotime($timeslot[0])) . '&ndash;' . date_i18n($time_format,strtotime($timeslot[1]));
  569. endforeach;
  570. if ($count > 5):
  571. $diff = $count - 5;
  572. echo '<br>...'.esc_html__('and','booked').' '.$diff.' '.esc_html__('more','booked');
  573. endif;
  574. echo '</span>';
  575. }
  576. return ob_get_clean();
  577. } else {
  578. return $value;
  579. }
  580. }
  581. // --------- ADMIN SCRIPTS/STYLES --------- //
  582. public function admin_scripts() {
  583. $current_page = (isset($_GET['page']) ? $_GET['page'] : false);
  584. $screen = get_current_screen();
  585. // Gonna need jQuery
  586. wp_enqueue_script('jquery');
  587. // For Serializing Arrays
  588. if ($current_page == 'booked-settings' || $screen->id == 'dashboard'):
  589. wp_enqueue_script('booked-serialize', BOOKED_PLUGIN_URL . '/assets/js/jquery.serialize.js', array(), BOOKED_VERSION);
  590. endif;
  591. // Load the rest of the stuff!
  592. if (in_array($current_page,$this->booked_screens) || $screen->id == 'dashboard'):
  593. wp_enqueue_media();
  594. wp_enqueue_script('wp-color-picker');
  595. wp_enqueue_script('jquery-ui');
  596. wp_enqueue_script('jquery-ui-sortable');
  597. wp_enqueue_script('jquery-ui-datepicker');
  598. wp_enqueue_script('spin-js', BOOKED_PLUGIN_URL . '/assets/js/spin.min.js', array(), '2.0.1');
  599. wp_enqueue_script('spin-jquery', BOOKED_PLUGIN_URL . '/assets/js/spin.jquery.js', array(), '2.0.1');
  600. wp_enqueue_script('booked-chosen', BOOKED_PLUGIN_URL . '/assets/js/chosen/chosen.jquery.min.js', array(), '1.2.0');
  601. wp_enqueue_script('booked-fitvids', BOOKED_PLUGIN_URL . '/assets/js/fitvids.js', array(), '1.1');
  602. wp_enqueue_script('booked-tooltipster', BOOKED_PLUGIN_URL . '/assets/js/tooltipster/js/jquery.tooltipster.min.js', array(), '3.3.0', true);
  603. wp_enqueue_script('booked-calendar-popup', BOOKED_PLUGIN_URL . '/assets/js/jquery.bookedCalendarPopup.js', array(), BOOKED_VERSION);
  604. wp_register_script('booked-admin', BOOKED_PLUGIN_URL . '/assets/js/admin-functions.js', array(), BOOKED_VERSION);
  605. // WPML Compatibility with AJAX calls
  606. $ajax_url = admin_url( 'admin-ajax.php' );
  607. $wpml_current_language = apply_filters( 'wpml_current_language', NULL );
  608. if ( $wpml_current_language ) {
  609. $ajax_url = add_query_arg( 'wpml_lang', $wpml_current_language, $ajax_url );
  610. }
  611. $booked_js_vars = array(
  612. 'ajax_url' => $ajax_url,
  613. 'ajaxRequests' => array(),
  614. 'i18n_slot' => esc_html__('Space Available','booked'),
  615. 'i18n_slots' => esc_html__('Spaces Available','booked'),
  616. 'i18n_add' => esc_html__('Add Timeslots','booked'),
  617. 'i18n_time_error' => esc_html__('The "End Time" needs to be later than the "Start Time".','booked'),
  618. 'i18n_bulk_add_confirm' => esc_html__('Are you sure you want to add those bulk time slots?','booked'),
  619. 'i18n_all_fields_required' => esc_html__('All fields are required.','booked'),
  620. 'i18n_single_add_confirm' => esc_html__('You are about to add the following time slot(s)','booked'),
  621. 'i18n_to' => esc_html__('to','booked'),
  622. 'i18n_please_wait' => esc_html__('Please wait ...','booked'),
  623. 'i18n_update_appointment' => esc_html__('Update Appointment','booked'),
  624. 'i18n_create_appointment' => esc_html__('Create Appointment','booked'),
  625. 'i18n_all_day' => esc_html__('All day','booked'),
  626. 'i18n_enable' => esc_html__('Enable','booked'),
  627. 'i18n_disable' => esc_html__('Disable','booked'),
  628. 'i18n_change_date' => esc_html__('Change Date','booked'),
  629. 'i18n_choose_customer' => esc_html__('Please choose a customer.','booked'),
  630. 'i18n_fill_out_required_fields' => esc_html__('Please fill out all required fields.','booked'),
  631. 'i18n_confirm_ts_delete' => esc_html__('Are you sure you want to delete this time slot?','booked'),
  632. 'i18n_confirm_cts_delete' => esc_html__('Are you sure you want to delete this custom time slot?','booked'),
  633. 'i18n_confirm_appt_delete' => esc_html__('Are you sure you want to cancel this appointment?','booked'),
  634. 'i18n_clear_timeslots_confirm' => esc_html__('Are you sure you want to delete all of the timeslots for this day?','booked'),
  635. 'i18n_appt_required_fields' => esc_html__('A name, email address and password are required.','booked'),
  636. 'i18n_appt_required_guest_fields' => esc_html__('A name is required.','booked'),
  637. 'i18n_appt_required_guest_fields_surname' => esc_html__('A first and last name are required.','booked'),
  638. 'i18n_appt_required_guest_fields_all' => esc_html__('A first name, last name and email address are required.','booked'),
  639. 'i18n_appt_required_guest_fields_name_email' => esc_html__('A name and an email address are required.','booked'),
  640. 'i18n_confirm_appt_approve' => esc_html__('Are you sure you want to approve this appointment?','booked'),
  641. 'i18n_confirm_appt_approve_all' => esc_html__('Are you sure you want to approve ALL pending appointments?','booked'),
  642. 'i18n_confirm_appt_delete_all' => esc_html__('Are you sure you want to delete ALL pending appointments?','booked'),
  643. 'i18n_confirm_appt_delete_past' => esc_html__('Are you sure you want to delete all PASSED pending appointments?','booked'),
  644. );
  645. wp_localize_script('booked-admin', 'booked_js_vars', $booked_js_vars );
  646. wp_enqueue_script('booked-admin');
  647. endif;
  648. }
  649. public function admin_styles() {
  650. $current_page = (isset($_GET['page']) ? $_GET['page'] : false);
  651. $screen = get_current_screen();
  652. if (in_array($current_page,$this->booked_screens) || $screen->id == 'dashboard'):
  653. wp_enqueue_style('wp-color-picker');
  654. wp_enqueue_style('booked-icons', BOOKED_PLUGIN_URL . '/assets/css/icons.css', array(), BOOKED_VERSION);
  655. wp_enqueue_style('booked-tooltipster', BOOKED_PLUGIN_URL . '/assets/js/tooltipster/css/tooltipster.css', array(), '3.3.0');
  656. wp_enqueue_style('booked-tooltipster-theme', BOOKED_PLUGIN_URL . '/assets/js/tooltipster/css/themes/tooltipster-light.css', array(), '3.3.0');
  657. wp_enqueue_style('chosen', BOOKED_PLUGIN_URL . '/assets/js/chosen/chosen.min.css', array(), '1.2.0');
  658. wp_enqueue_style('booked-animations', BOOKED_PLUGIN_URL . '/assets/css/animations.css', array(), BOOKED_VERSION);
  659. wp_enqueue_style('booked-admin', BOOKED_PLUGIN_URL . '/assets/css/admin-styles.css', array(), BOOKED_VERSION);
  660. endif;
  661. }
  662. // --------- FRONT-END SCRIPTS/STYLES --------- //
  663. public function front_end_scripts() {
  664. wp_enqueue_script('jquery');
  665. wp_enqueue_script('jquery-ui');
  666. wp_enqueue_script('jquery-ui-datepicker');
  667. wp_register_script('booked-atc', BOOKED_PLUGIN_URL . '/assets/js/atc.min.js', array(), '1.6.1', true );
  668. wp_enqueue_script('booked-spin-js', BOOKED_PLUGIN_URL . '/assets/js/spin.min.js', array(), '2.0.1', true);
  669. wp_enqueue_script('booked-spin-jquery', BOOKED_PLUGIN_URL . '/assets/js/spin.jquery.js', array(), '2.0.1', true);
  670. wp_enqueue_script('booked-tooltipster', BOOKED_PLUGIN_URL . '/assets/js/tooltipster/js/jquery.tooltipster.min.js', array(), '3.3.0', true);
  671. wp_register_script('booked-functions', BOOKED_PLUGIN_URL . '/assets/js/functions.js', array(), BOOKED_VERSION, true);
  672. $booked_redirect_type = get_option('booked_appointment_redirect_type','booked-profile');
  673. $booked_detect_profile_page = booked_get_profile_page();
  674. if ($booked_redirect_type == 'booked-profile'):
  675. $profile_page = ($booked_detect_profile_page ? $booked_detect_profile_page : false);
  676. elseif ($booked_redirect_type == 'page'):
  677. $profile_page = get_option('booked_appointment_success_redirect_page',false);
  678. else:
  679. $profile_page = false;
  680. endif;
  681. $profile_page = ( $profile_page ? esc_url( get_permalink( $profile_page ) ) : ( $booked_detect_profile_page ? esc_url( get_permalink( $booked_detect_profile_page->ID ) ) : false ) );
  682. // WPML Compatibility with AJAX calls
  683. $ajax_url = admin_url( 'admin-ajax.php' );
  684. $wpml_current_language = apply_filters( 'wpml_current_language', NULL );
  685. if ( $wpml_current_language ) {
  686. $ajax_url = add_query_arg( 'wpml_lang', $wpml_current_language, $ajax_url );
  687. }
  688. $booked_js_vars = array(
  689. 'ajax_url' => $ajax_url,
  690. 'profilePage' => $profile_page,
  691. 'publicAppointments' => get_option('booked_public_appointments',false),
  692. 'i18n_confirm_appt_delete' => esc_html__('Are you sure you want to cancel this appointment?','booked'),
  693. 'i18n_please_wait' => esc_html__('Please wait ...','booked'),
  694. 'i18n_wrong_username_pass' => esc_html__('Wrong username/password combination.','booked'),
  695. 'i18n_fill_out_required_fields' => esc_html__('Please fill out all required fields.','booked'),
  696. 'i18n_guest_appt_required_fields' => esc_html__('Please enter your name to book an appointment.','booked'),
  697. 'i18n_appt_required_fields' => esc_html__('Please enter your name, your email address and choose a password to book an appointment.','booked'),
  698. 'i18n_appt_required_fields_guest' => esc_html__('Please fill in all "Information" fields.','booked'),
  699. 'i18n_password_reset' => esc_html__('Please check your email for instructions on resetting your password.','booked'),
  700. 'i18n_password_reset_error' => esc_html__('That username or email is not recognized.','booked'),
  701. );
  702. wp_localize_script( 'booked-functions', 'booked_js_vars', $booked_js_vars );
  703. wp_enqueue_script('booked-functions');
  704. }
  705. public static function front_end_styles() {
  706. wp_enqueue_style('booked-icons', BOOKED_PLUGIN_URL . '/assets/css/icons.css', array(), BOOKED_VERSION);
  707. wp_enqueue_style('booked-tooltipster', BOOKED_PLUGIN_URL . '/assets/js/tooltipster/css/tooltipster.css', array(), '3.3.0');
  708. wp_enqueue_style('booked-tooltipster-theme', BOOKED_PLUGIN_URL . '/assets/js/tooltipster/css/themes/tooltipster-light.css', array(), '3.3.0');
  709. wp_enqueue_style('booked-animations', BOOKED_PLUGIN_URL . '/assets/css/animations.css', array(), BOOKED_VERSION);
  710. wp_enqueue_style('booked-styles', BOOKED_PLUGIN_URL . '/assets/css/styles.css', array(), BOOKED_VERSION);
  711. wp_enqueue_style('booked-responsive', BOOKED_PLUGIN_URL . '/assets/css/responsive.css', array(), BOOKED_VERSION);
  712. if ( defined('NECTAR_THEME_NAME') && NECTAR_THEME_NAME == 'salient' ):
  713. wp_enqueue_style('booked-salient-overrides', BOOKED_PLUGIN_URL . '/assets/css/theme-specific/salient.css', array(), BOOKED_VERSION);
  714. endif;
  715. }
  716. public static function front_end_color_theme() {
  717. if (!isset($_GET['print'])):
  718. $colors_pattern_file = BOOKED_PLUGIN_DIR . '/assets/css/color-theme.php';
  719. if ( !file_exists($colors_pattern_file) ) {
  720. return;
  721. }
  722. ob_start();
  723. include(esc_attr($colors_pattern_file));
  724. $booked_color_css = ob_get_clean();
  725. $compressed_booked_color_css = booked_compress_css( $booked_color_css );
  726. echo '<style type="text/css" media="screen">';
  727. echo $compressed_booked_color_css;
  728. echo '</style>';
  729. endif;
  730. }
  731. public static function booked_user_roles_filter( $booked_user_roles ) {
  732. return $booked_user_roles;
  733. }
  734. } // END class booked_plugin
  735. } // END if(!class_exists('booked_plugin'))
  736. if(class_exists('booked_plugin')) {
  737. // Add the "Booking Agent" User Role
  738. $booking_agent = add_role(
  739. 'booked_booking_agent',
  740. esc_html__( 'Booking Agent','booked' ),
  741. array(
  742. 'read' => true,
  743. )
  744. );
  745. // Add Capabilities to User Roles (the below array can be filtered to include more or exclude any of the defaults)
  746. $booked_user_roles = apply_filters( 'booked_user_roles', array('administrator','booked_booking_agent') );
  747. foreach($booked_user_roles as $role_name):
  748. $role_caps = get_role($role_name);
  749. $role_caps->add_cap('edit_booked_appointments');
  750. endforeach;
  751. $booked_admin_caps = get_role('administrator');
  752. $booked_admin_caps->add_cap('manage_booked_options');
  753. // Activation Hook
  754. register_activation_hook(__FILE__, array('booked_plugin', 'activate'));
  755. // Initiate the Booked Class
  756. $booked_plugin = new booked_plugin();
  757. // Add a link to the settings page onto the plugin page
  758. if(isset($booked_plugin)) {
  759. // Add the settings link to the plugins page
  760. function booked_custom_links($links) {
  761. $custom_links[] = '<a href="admin.php?page=booked-settings">'.esc_html__('Settings','booked').'</a>';
  762. $custom_links[] = '<a href="'.trailingslashit(get_admin_url()).'admin.php?page=booked-welcome">'.esc_html__('What\'s New?','booked').'</a>';
  763. foreach($custom_links as $custom_link):
  764. array_unshift($links, $custom_link);
  765. endforeach;
  766. return $links;
  767. }
  768. $plugin = plugin_basename(__FILE__);
  769. add_filter("plugin_action_links_$plugin", 'booked_custom_links');
  770. // Load the Front-End Styles and Color Settings
  771. add_action('wp_enqueue_scripts', array('booked_plugin', 'front_end_styles'));
  772. add_action('wp_enqueue_scripts', array('booked_plugin', 'front_end_color_theme'));
  773. }
  774. }
  775. // Localization
  776. function booked_local_init(){
  777. $domain = 'booked';
  778. $locale = apply_filters('plugin_locale', get_locale(), $domain);
  779. load_textdomain($domain, WP_LANG_DIR.'/booked/'.$domain.'-'.$locale.'.mo');
  780. load_plugin_textdomain($domain, FALSE, dirname(plugin_basename(__FILE__)).'/languages/');
  781. }
  782. add_action('after_setup_theme', 'booked_local_init');