PluginProbe
WP Booking System – Booking Calendar / trunk
WP Booking System – Booking Calendar vtrunk
2.0.19.14 trunk 0.1 0.2 0.3 0.4 0.5 0.6 0.7 1.0 1.1 1.2 1.2.1 1.2.2 1.2.3 1.3 1.3.1 1.3.2 1.3.3 1.4 1.4.1 1.5 1.5.2 1.5.4 2.0.0 All 31 releases
wp-booking-system / wp-booking-system.php

wp-booking-system.php in WP Booking System – Booking Calendar trunk, at wp-booking-system.php

582 lines 17.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: WP Booking System
4 * Plugin URI: https://www.wpbookingsystem.com/
5 * Description: A set-and-forget booking calendar for your rental business.
6 * Version: 2.0.19.14
7 * Author: Veribo, Roland Murg
8 * Author URI: https://www.wpbookingsystem.com/
9 * Text Domain: wp-booking-system
10 * License: GPL2
11 *
12 * == Copyright ==
13 * Copyright 2019 WP Booking System (www.wpbookingsystem.com)
14 *
15 */
16
17 // Exit if accessed directly
18 if (!defined('ABSPATH')) {
19 exit;
20 }
21
22 /**
23 * Main plugin class
24 *
25 */
26 class WP_Booking_System
27 {
28
29 /**
30 * The current instance of the object
31 *
32 * @access private
33 * @var WP_Booking_System
34 *
35 */
36 private static $instance;
37
38 /**
39 * A list with the objects that handle database requests
40 *
41 * @access public
42 * @var array
43 *
44 */
45 public $db = array();
46
47 /**
48 * A list with the objects that handle submenu pages
49 *
50 * @access public
51 * @var array
52 *
53 */
54 public $submenu_pages = array();
55
56 /**
57 * Constructor
58 *
59 */
60 public function __construct()
61 {
62
63 // Defining constants
64 define('WPBS_VERSION', '2.0.19.14');
65 define('WPBS_FILE', __FILE__);
66 define('WPBS_BASENAME', plugin_basename(__FILE__));
67 define('WPBS_PLUGIN_DIR', plugin_dir_path(__FILE__));
68 define('WPBS_PLUGIN_DIR_URL', plugin_dir_url(__FILE__));
69
70 $this->include_files();
71 $this->load_db_layer();
72
73 define('WPBS_TRANSLATION_TEXTDOMAIN', 'wp-booking-system');
74
75 // Check if just updated
76 add_action('plugins_loaded', array($this, 'update_check'), 20);
77
78 // Load the textdomain and the translation folders
79 add_action('plugins_loaded', array($this, 'load_text_domain'), 30);
80
81 // Update the database tables
82 add_action('wpbs_update_check', array($this, 'update_database_tables'));
83
84 // Add and remove main plugin page
85 add_action('admin_menu', array($this, 'add_main_menu_page'), 10);
86 add_action('admin_menu', array($this, 'remove_main_menu_page'), 11);
87
88 // Add submenu pages
89 add_action('wp_loaded', array($this, 'load_admin_submenu_pages'), 11);
90
91 // Admin scripts
92 add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
93
94 // Front-end scripts
95 add_action('wp_enqueue_scripts', array($this, 'enqueue_front_end_scripts'));
96
97 // Remove plugin query args from the URL
98 add_filter('removable_query_args', array($this, 'removable_query_args'));
99
100 // Add body class for WP versions greater than 5.3
101 add_filter( 'admin_body_class', array($this, 'admin_body_class') );
102
103 // Add a 5 star review call to action to admin footer text
104 add_filter('admin_footer_text', array($this, 'admin_footer_text'));
105
106 register_activation_hook(__FILE__, array($this, 'set_cron_jobs'));
107 register_deactivation_hook(__FILE__, array($this, 'unset_cron_jobs'));
108
109 /**
110 * Plugin initialized
111 *
112 */
113 do_action('wpbs_initialized');
114
115 }
116
117 /**
118 * Returns an instance of the plugin object
119 *
120 * @return WP_Booking_System
121 *
122 */
123 public static function instance()
124 {
125
126 if (!isset(self::$instance) && !(self::$instance instanceof WP_Booking_System)) {
127 self::$instance = new WP_Booking_System;
128 }
129
130 return self::$instance;
131
132 }
133
134 /**
135 * Add the main menu page
136 *
137 */
138 public function add_main_menu_page()
139 {
140
141 add_menu_page('WP Booking System', 'WP Booking<br /> System', apply_filters('wpbs_menu_page_capability', 'manage_options'), 'wp-booking-system', '', 'dashicons-calendar-alt');
142
143 }
144
145 /**
146 * Remove the main menu page as we will rely only on submenu pages
147 *
148 */
149 public function remove_main_menu_page()
150 {
151
152 remove_submenu_page('wp-booking-system', 'wp-booking-system');
153
154 }
155
156 /**
157 * Checks to see if the current version of the plugin matches the version
158 * saved in the database
159 *
160 * @return void
161 *
162 */
163 public function update_check()
164 {
165
166 $db_version = get_option('wpbs_version', '');
167 $do_update = false;
168
169 // If current version number differs from saved version number
170 if ($db_version != WPBS_VERSION) {
171
172 $do_update = true;
173
174 // Update the version number in the db
175 update_option('wpbs_version', WPBS_VERSION);
176
177 // Add first activation time
178 if (get_option('wpbs_first_activation', '') == '') {
179 update_option('wpbs_first_activation', time());
180 }
181
182 }
183
184 if ($do_update) {
185
186 // Hook for fresh update
187 do_action('wpbs_update_check', $db_version);
188
189 // Trigger set cron jobs
190 $this->set_cron_jobs();
191
192 }
193
194 }
195
196 /**
197 * Creates and updates the database tables
198 *
199 * @return void
200 *
201 */
202 public function update_database_tables()
203 {
204
205 foreach ($this->db as $db_class) {
206
207 $db_class->create_table();
208
209 }
210
211 }
212
213 /**
214 * Loads plugin text domain
215 *
216 */
217 public function load_text_domain()
218 {
219
220 $locale = apply_filters( 'plugin_locale', get_locale(), WPBS_TRANSLATION_TEXTDOMAIN );
221
222 // Search for Translation in /wp-content/languages/plugin/
223 if (file_exists(trailingslashit( WP_LANG_DIR ) . 'plugins' . WPBS_TRANSLATION_TEXTDOMAIN . '-' . $locale . '.mo')) {
224 load_plugin_textdomain(WPBS_TRANSLATION_TEXTDOMAIN, false, trailingslashit( WP_LANG_DIR ));
225 }
226 // Search for Translation in /wp-content/languages/
227 elseif (file_exists(trailingslashit( WP_LANG_DIR ) . WPBS_TRANSLATION_TEXTDOMAIN . '-' . $locale . '.mo')) {
228 load_textdomain(WPBS_TRANSLATION_TEXTDOMAIN, trailingslashit( WP_LANG_DIR ) . WPBS_TRANSLATION_TEXTDOMAIN . '-' . $locale . '.mo');
229 // Search for Translation in /wp-content/plugins/wp-booking-system-premium/languages/
230 } else {
231 load_plugin_textdomain(WPBS_TRANSLATION_TEXTDOMAIN, false, plugin_basename(dirname(__FILE__)) . '/languages');
232 }
233
234 }
235
236 /**
237 * Sets an action hook for modules to add custom schedules
238 *
239 */
240 public function set_cron_jobs()
241 {
242
243 do_action('wpbs_set_cron_jobs');
244
245 }
246
247 /**
248 * Sets an action hook for modules to remove custom schedules
249 *
250 */
251 public function unset_cron_jobs()
252 {
253
254 do_action('wpbs_unset_cron_jobs');
255
256 }
257
258 /**
259 * Include files
260 *
261 * @return void
262 *
263 */
264 public function include_files()
265 {
266
267 /**
268 * Include abstract classes
269 *
270 */
271 include WPBS_PLUGIN_DIR . 'includes/abstracts/abstract-class-db.php';
272 include WPBS_PLUGIN_DIR . 'includes/abstracts/abstract-class-list-table.php';
273 include WPBS_PLUGIN_DIR . 'includes/abstracts/abstract-class-object-db.php';
274 include WPBS_PLUGIN_DIR . 'includes/abstracts/abstract-class-object-meta-db.php';
275 include WPBS_PLUGIN_DIR . 'includes/abstracts/abstract-class-object.php';
276 include WPBS_PLUGIN_DIR . 'includes/abstracts/abstract-class-submenu-page.php';
277
278 /**
279 * Include all functions.php files from all plugin folders
280 *
281 */
282 $this->_recursively_include_files(WPBS_PLUGIN_DIR . 'includes');
283
284 /**
285 * Helper hook to include files early
286 *
287 */
288 do_action('wpbs_include_files');
289
290 }
291
292 /**
293 * Recursively includes all functions.php files from the given directory path
294 *
295 * @param string $dir_path
296 *
297 */
298 protected function _recursively_include_files($dir_path)
299 {
300
301 $folders = array_filter(glob($dir_path . '/*'), 'is_dir');
302
303 foreach ($folders as $folder_path) {
304
305 if (file_exists($folder_path . '/functions.php')) {
306 include $folder_path . '/functions.php';
307 }
308
309 $this->_recursively_include_files($folder_path);
310
311 }
312
313 }
314
315 /**
316 * Sets up all objects that handle database related requests and adds them to the
317 * $db property of the app
318 *
319 */
320 public function load_db_layer()
321 {
322
323 /**
324 * Hook to register db class handlers
325 * The array element should be 'class_slug' => 'class_name'
326 *
327 * @param array
328 *
329 */
330 $db_classes = apply_filters('wpbs_register_database_classes', array());
331
332 if (empty($db_classes)) {
333 return;
334 }
335
336 foreach ($db_classes as $db_class_slug => $db_class_name) {
337
338 $this->db[$db_class_slug] = new $db_class_name;
339
340 }
341
342 }
343
344 /**
345 * Sets up all objects that handle submenu pages and adds them to the
346 * $submenu_pages property of the app
347 *
348 */
349 public function load_admin_submenu_pages()
350 {
351
352 /**
353 * Hook to register submenu_pages class handlers
354 * The array element should be 'submenu_page_slug' => array( 'class_name' => array(), 'data' => array() )
355 *
356 * @param array
357 *
358 */
359 $submenu_pages = apply_filters('wpbs_register_submenu_page', array());
360
361 if (empty($submenu_pages)) {
362 return;
363 }
364
365 foreach ($submenu_pages as $submenu_page_slug => $submenu_page) {
366
367 if (empty($submenu_page['data'])) {
368 continue;
369 }
370
371 if (empty($submenu_page['data']['page_title']) || empty($submenu_page['data']['menu_title']) || empty($submenu_page['data']['capability']) || empty($submenu_page['data']['menu_slug'])) {
372 continue;
373 }
374
375 $this->submenu_pages[$submenu_page['data']['menu_slug']] = new $submenu_page['class_name']($submenu_page['data']['page_title'], $submenu_page['data']['menu_title'], $submenu_page['data']['capability'], $submenu_page['data']['menu_slug']);
376
377 }
378
379 }
380
381 /**
382 * Enqueue the scripts and style for the admin area
383 *
384 */
385 public function enqueue_admin_scripts($hook)
386 {
387 if (strpos($hook, 'wpbs') !== false || strpos($hook, 'widgets') !== false || in_array(get_post_type(), array('post', 'page'))) {
388
389 if (!wp_script_is('chosen')) {
390
391 wp_enqueue_script('wpbs-chosen', WPBS_PLUGIN_DIR_URL . 'assets/libs/chosen/chosen.jquery.min.js', array('jquery'), WPBS_VERSION);
392 wp_enqueue_style('wpbs-chosen', WPBS_PLUGIN_DIR_URL . 'assets/libs/chosen/chosen.css', array(), WPBS_VERSION);
393
394 }
395
396 }
397
398 if (strpos($hook, 'wpbs') !== false) {
399
400 $settings = get_option('wpbs_settings', array());
401
402 // Edit calendar scripts
403 wp_register_script('wpbs-script-edit-calendar', WPBS_PLUGIN_DIR_URL . 'assets/js/script-admin-edit-calendar.js', array('jquery', 'jquery-ui-sortable', 'wp-color-picker', 'jquery-ui-datepicker'), WPBS_VERSION);
404 wp_localize_script('wpbs-script-edit-calendar', 'wpbs_plugin_settings', $settings);
405 wp_enqueue_script('wpbs-script-edit-calendar');
406
407 // Edit form scripts
408 wp_register_script('wpbs-script-edit-form', WPBS_PLUGIN_DIR_URL . 'assets/js/script-admin-edit-form.js', array('jquery', 'jquery-ui-sortable', 'wp-color-picker', 'jquery-ui-datepicker'), WPBS_VERSION);
409 wp_enqueue_script('wpbs-script-edit-form');
410 wp_localize_script('wpbs-script-edit-form', 'wpbs_localized_data', array('wpbs_plugins_dir_url' => WPBS_PLUGIN_DIR_URL));
411
412 // Edit booking scripts
413 wp_register_script('wpbs-script-edit-booking', WPBS_PLUGIN_DIR_URL . 'assets/js/script-admin-edit-booking.js', array('jquery', 'jquery-ui-sortable', 'wp-color-picker', 'jquery-ui-datepicker'), WPBS_VERSION);
414 wp_enqueue_script('wpbs-script-edit-booking');
415 wp_localize_script('wpbs-script-edit-booking', 'wpbs_localized_data_booking', array(
416 'wpbs_plugins_dir_url' => WPBS_PLUGIN_DIR_URL,
417 'open_bookings_token' => wp_create_nonce('wpbs_open_booking_details'),
418 'email_customer_token' => wp_create_nonce('wpbs_booking_email_customer'),
419 ));
420
421 // Color picker
422 wp_enqueue_style('jquery-style', WPBS_PLUGIN_DIR_URL . 'assets/css/jquery-ui.css', array(), WPBS_VERSION);
423 wp_enqueue_style('wp-color-picker');
424
425 // Datepicker Custom
426 wp_enqueue_style('wpbs-datepicker', WPBS_PLUGIN_DIR_URL . 'assets/css/datepicker.css', array(), WPBS_VERSION);
427
428 }
429
430 if (!empty($_GET['page']) && $_GET['page'] == 'wpbs-upgrader') {
431
432 wp_register_script('wpbs-script-upgrader', WPBS_PLUGIN_DIR_URL . 'assets/js/script-admin-upgrader.js', array('jquery'), WPBS_VERSION);
433 wp_enqueue_script('wpbs-script-upgrader');
434
435 }
436
437 if (!empty($_GET['page']) && $_GET['page'] == 'wpbs-settings') {
438
439 wp_register_script('wpbs-script-uninstaller', WPBS_PLUGIN_DIR_URL . 'assets/js/script-admin-uninstaller.js', array('jquery'), WPBS_VERSION);
440 wp_enqueue_script('wpbs-script-uninstaller');
441
442 }
443
444 // Plugin styles
445 wp_register_style('wpbs-admin-style', WPBS_PLUGIN_DIR_URL . 'assets/css/style-admin.css', array(), WPBS_VERSION);
446 wp_enqueue_style('wpbs-admin-style');
447
448 // Plugin script
449 wp_register_script('wpbs-admin-script', WPBS_PLUGIN_DIR_URL . 'assets/js/script-admin.js', array('jquery', 'jquery-ui-sortable', 'wp-color-picker', 'jquery-ui-datepicker'), WPBS_VERSION);
450 wp_enqueue_script('wpbs-admin-script');
451
452 // Plugin styles from the front-end. Needed for the actual calendar
453 wp_register_style('wpbs-front-end-style', WPBS_PLUGIN_DIR_URL . 'assets/css/style-front-end.css', array(), WPBS_VERSION);
454 wp_enqueue_style('wpbs-front-end-style');
455
456 // Icon Font
457 wp_register_style('wpbs-icons-font', WPBS_PLUGIN_DIR_URL . 'assets/css/icons-font.css', array(), WPBS_VERSION);
458 wp_enqueue_style('wpbs-icons-font');
459
460 /**
461 * Hook to enqueue scripts immediately after the plugin's scripts
462 *
463 */
464 do_action('wpbs_enqueue_admin_scripts');
465
466 }
467
468 /**
469 * Enqueue the scripts and style for the front-end part
470 *
471 */
472 public function enqueue_front_end_scripts()
473 {
474
475 $settings = get_option('wpbs_settings', array());
476
477 // Plugin styles
478 wp_register_style('wpbs-style', WPBS_PLUGIN_DIR_URL . 'assets/css/style-front-end.min.css', array(), WPBS_VERSION);
479 wp_enqueue_style('wpbs-style');
480
481 // Plugin styles
482 if (!isset($settings['form_styling']) || $settings['form_styling'] == 'default') {
483 wp_register_style('wpbs-style-form', WPBS_PLUGIN_DIR_URL . 'assets/css/style-front-end-form.min.css', array(), WPBS_VERSION);
484 wp_enqueue_style('wpbs-style-form');
485 }
486
487 // Plugin script
488 wp_register_script('wpbs-script', WPBS_PLUGIN_DIR_URL . 'assets/js/script-front-end.min.js', array('jquery'), WPBS_VERSION, true);
489 wp_localize_script('wpbs-script', 'wpbs_ajax', array(
490 'token' => wp_create_nonce('wpbs_form_ajax')
491 ));
492
493 wp_enqueue_script('wpbs-script');
494
495 // Add ajax_url into the front-end
496 $ajax_url = "
497 var wpbs_ajaxurl = '" . admin_url('admin-ajax.php') . "';
498 ";
499
500 wp_add_inline_script('wpbs-script', $ajax_url, 'before');
501
502 // Google reCaptcha V2
503 if (isset($settings['recaptcha_v2_site_key']) && !empty($settings['recaptcha_v2_site_key'])) {
504 wp_register_script('google-recaptcha', 'https://www.google.com/recaptcha/api.js', array(), WPBS_VERSION, false);
505 wp_enqueue_script('google-recaptcha');
506 }
507
508 /**
509 * Hook to enqueue scripts immediately after the plugin's scripts
510 *
511 */
512 do_action('wpbs_enqueue_front_end_scripts');
513
514 }
515
516 /**
517 * Removes the query variables from the URL upon page load
518 *
519 */
520 public function removable_query_args($args = array())
521 {
522
523 $args[] = 'wpbs_message';
524
525 return $args;
526
527 }
528
529 /**
530 * Add custom class to the <body> tag in WP Admin
531 *
532 * @param string $text
533 *
534 */
535 public function admin_body_class($classes){
536 if ( version_compare( get_bloginfo('version'), '5.3', '>=' ) ) {
537 $classes .= ' wpbs-greater-5-3';
538 }
539 if ( version_compare( get_bloginfo('version'), '7.0', '>=' ) ) {
540 $classes .= ' wpbs-greater-7';
541 }
542 return $classes;
543 }
544
545 /**
546 * Replace admin footer text with a rate plugin message
547 *
548 * @param string $text
549 *
550 */
551 public function admin_footer_text($text)
552 {
553
554 if (isset($_GET['page']) && strpos($_GET['page'], 'wpbs') !== false) {
555 return sprintf(__('If you enjoy using <strong>WP Booking System</strong>, please <a href="%s" target="_blank">leave us a �
556
557
558
559
560 rating</a>. Big thank you for this!', 'wp-booking-system'), 'https://wordpress.org/support/plugin/wp-booking-system/reviews/?rate=5#new-post');
561 }
562
563 return $text;
564
565 }
566
567 }
568
569 /**
570 * Returns the WP Booking System instanced object
571 *
572 */
573 function wp_booking_system()
574 {
575
576 return WP_Booking_System::instance();
577
578 }
579
580 // Let's get the party started
581 wp_booking_system();
582