forked from NateWr/restaurant-reservations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restaurant-reservations.php
301 lines (248 loc) · 9.96 KB
/
restaurant-reservations.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
<?php
/**
* Plugin Name: Restaurant Reservations
* Plugin URI: http://themeofthecrop.com
* Description: Accept restaurant reservations and bookings online.
* Version: 1.5.1
* Author: Theme of the Crop
* Author URI: http://themeofthecrop.com
* License: GNU General Public License v2.0 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* Text Domain: restaurant-reservations
* Domain Path: /languages/
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* 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.
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( ! defined( 'ABSPATH' ) )
exit;
if ( !class_exists( 'rtbInit' ) ) {
class rtbInit {
/**
* Set a flag which tracks whether the form has already been rendered on
* the page. Only one form per page for now.
* @todo support multiple forms per page
*/
public $form_rendered = false;
/**
* An object which stores a booking request, or an empty object if
* no request has been processed.
*/
public $request;
/**
* Initialize the plugin and register hooks
*/
public function __construct() {
// Common strings
define( 'RTB_PLUGIN_DIR', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
define( 'RTB_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
define( 'RTB_PLUGIN_FNAME', plugin_basename( __FILE__ ) );
define( 'RTB_BOOKING_POST_TYPE', 'rtb-booking' );
define( 'RTB_BOOKING_POST_TYPE_SLUG', 'booking' );
define( 'RTB_LOAD_FRONTEND_ASSETS', apply_filters( 'rtb-load-frontend-assets', true ) );
// Initialize the plugin
add_action( 'init', array( $this, 'load_textdomain' ) );
// Set up empty request object
$this->request = new stdClass();
$this->request->request_processed = false;
$this->request->request_inserted = false;
// Load query class
require_once( RTB_PLUGIN_DIR . '/includes/Query.class.php' );
// Add custom roles and capabilities
add_action( 'init', array( $this, 'add_roles' ) );
// Load custom post types
require_once( RTB_PLUGIN_DIR . '/includes/CustomPostTypes.class.php' );
$this->cpts = new rtbCustomPostTypes();
// Flush the rewrite rules for the custom post types
register_activation_hook( __FILE__, array( $this, 'rewrite_flush' ) );
// Load the template functions which print the booking form, etc
require_once( RTB_PLUGIN_DIR . '/includes/template-functions.php' );
// Load the admin bookings page
require_once( RTB_PLUGIN_DIR . '/includes/AdminBookings.class.php' );
new rtbAdminBookings();
// Load assets
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'register_assets' ) );
// Handle notifications
require_once( RTB_PLUGIN_DIR . '/includes/Notifications.class.php' );
$this->notifications = new rtbNotifications();
// Load settings
require_once( RTB_PLUGIN_DIR . '/includes/Settings.class.php' );
$this->settings = new rtbSettings();
// Append booking form to a post's $content variable
add_filter( 'the_content', array( $this, 'append_to_content' ) );
// Register the widget
add_action( 'widgets_init', array( $this, 'register_widgets' ) );
// Add links to plugin listing
add_filter('plugin_action_links', array( $this, 'plugin_action_links' ), 10, 2);
// Load the license handling
if ( file_exists( RTB_PLUGIN_DIR . '/includes/Licenses.class.php' ) ) {
require_once( RTB_PLUGIN_DIR . '/includes/Licenses.class.php' );
$this->licenses = new rtbLicenses();
}
// Add the addons page
require_once( RTB_PLUGIN_DIR . '/includes/Addons.class.php' );
new rtbAddons(
array(
'api_url' => 'http://api.themeofthecrop.com/addons/',
'plugin' => basename( plugin_dir_path( __FILE__ ) ),
)
);
// Load backwards compatibility functions
require_once( RTB_PLUGIN_DIR . '/includes/Compatibility.class.php' );
new rtbCompatibility();
}
/**
* Flush the rewrite rules when this plugin is activated to update with
* custom post types
* @since 0.0.1
*/
public function rewrite_flush() {
$this->cpts->load_cpts();
flush_rewrite_rules();
}
/**
* Load the plugin textdomain for localistion
* @since 0.0.1
*/
public function load_textdomain() {
load_plugin_textdomain( 'restaurant-reservations', false, plugin_basename( dirname( __FILE__ ) ) . "/languages/" );
}
/**
* Add a role to manage the bookings and add the capability to Editors,
* Administrators and Super Admins
* @since 0.0.1
*/
public function add_roles() {
// The booking manager should be able to access the bookings list and
// update booking statuses, but shouldn't be able to touch anything else
// in the account.
$booking_manager = add_role(
'rtb_booking_manager',
__( 'Booking Manager', 'restaurant-reservations' ),
array(
'read' => true,
'manage_bookings' => true,
)
);
$manage_bookings_roles = apply_filters(
'rtb_manage_bookings_roles',
array(
'administrator',
'editor',
)
);
global $wp_roles;
foreach ( $manage_bookings_roles as $role ) {
$wp_roles->add_cap( $role, 'manage_bookings' );
}
}
/**
* Append booking form to a post's $content variable
* @since 0.0.1
*/
function append_to_content( $content ) {
if ( !is_main_query() || !in_the_loop() || post_password_required() ) {
return $content;
}
$booking_page = $this->settings->get_setting( 'booking-page' );
if ( empty( $booking_page ) ) {
return $content;
}
global $post;
if ( $post->ID != $this->settings->get_setting( 'booking-page' ) ) {
return $content;
}
return $content . rtb_print_booking_form();
}
/**
* Enqueue the admin-only CSS and Javascript
* @since 0.0.1
*/
public function enqueue_admin_assets() {
// Use the page reference in $admin_page_hooks because
// it changes in SOME hooks when it is translated.
// https://core.trac.wordpress.org/ticket/18857
global $admin_page_hooks;
$screen = get_current_screen();
if ( empty( $screen ) || empty( $admin_page_hooks['rtb-bookings'] ) ) {
return;
}
if ( $screen->base == 'toplevel_page_rtb-bookings' || $screen->base == $admin_page_hooks['rtb-bookings'] . '_page_rtb-settings' || $screen->base == $admin_page_hooks['rtb-bookings'] . '_page_rtb-addons' ) {
wp_enqueue_style( 'rtb-admin', RTB_PLUGIN_URL . '/assets/css/admin.css' );
wp_enqueue_script( 'rtb-admin', RTB_PLUGIN_URL . '/assets/js/admin.js', array( 'jquery' ), '', true );
wp_localize_script(
'rtb-admin',
'rtb_admin',
array(
'nonce' => wp_create_nonce( 'rtb-admin' ),
'strings' => array(
'add_booking' => __( 'Add Booking', 'restaurant-reservations' ),
'edit_booking' => __( 'Edit Booking', 'restaurant-reservations' ),
'error_unspecified' => __( 'An unspecified error occurred. Please try again. If the problem persists, try logging out and logging back in.', 'restaurant-reservations' ),
),
)
);
}
// Enqueue frontend assets to add/edit bookins on the bookings page
if ( $screen->base == 'toplevel_page_rtb-bookings' ) {
$this->register_assets();
rtb_enqueue_assets();
}
}
/**
* Register the front-end CSS and Javascript for the booking form
* @since 0.0.1
*/
function register_assets() {
if ( !RTB_LOAD_FRONTEND_ASSETS ) {
return;
}
wp_register_style( 'pickadate-default', RTB_PLUGIN_URL . '/lib/simple-admin-pages/lib/pickadate/themes/default.css' );
wp_register_style( 'pickadate-date', RTB_PLUGIN_URL . '/lib/simple-admin-pages/lib/pickadate/themes/default.date.css' );
wp_register_style( 'pickadate-time', RTB_PLUGIN_URL . '/lib/simple-admin-pages/lib/pickadate/themes/default.time.css' );
wp_register_script( 'pickadate', RTB_PLUGIN_URL . '/lib/simple-admin-pages/lib/pickadate/picker.js', array( 'jquery' ), '', true );
wp_register_script( 'pickadate-date', RTB_PLUGIN_URL . '/lib/simple-admin-pages/lib/pickadate/picker.date.js', array( 'jquery' ), '', true );
wp_register_script( 'pickadate-time', RTB_PLUGIN_URL . '/lib/simple-admin-pages/lib/pickadate/picker.time.js', array( 'jquery' ), '', true );
wp_register_script( 'pickadate-legacy', RTB_PLUGIN_URL . '/lib/simple-admin-pages/lib/pickadate/legacy.js', array( 'jquery' ), '', true );
$i8n = $this->settings->get_setting( 'i8n' );
if ( !empty( $i8n ) ) {
wp_register_script( 'pickadate-i8n', RTB_PLUGIN_URL . '/lib/simple-admin-pages/lib/pickadate/translations/' . esc_attr( $i8n ) . '.js', array( 'jquery' ), '', true );
// Arabic and Hebrew are right-to-left languages
if ( $i8n == 'ar' || $i8n == 'he_IL' ) {
wp_register_style( 'pickadate-rtl', RTB_PLUGIN_URL . '/lib/simple-admin-pages/lib/pickadate/themes/rtl.css' );
}
}
wp_register_style( 'rtb-booking-form', RTB_PLUGIN_URL . '/assets/css/booking-form.css' );
wp_register_script( 'rtb-booking-form', RTB_PLUGIN_URL . '/assets/js/booking-form.js', array( 'jquery' ) );
}
/**
* Register the widgets
* @since 0.0.1
*/
public function register_widgets() {
require_once( RTB_PLUGIN_DIR . '/includes/WP_Widget.BookingFormWidget.class.php' );
register_widget( 'rtbBookingFormWidget' );
}
/**
* Add links to the plugin listing on the installed plugins page
* @since 0.0.1
*/
public function plugin_action_links( $links, $plugin ) {
if ( $plugin == RTB_PLUGIN_FNAME ) {
$links['help'] = '<a href="' . RTB_PLUGIN_URL . '/docs" title="' . __( 'View the help documentation for Restaurant Reservations', 'restaurant-reservations' ) . '">' . __( 'Help', 'restaurant-reservations' ) . '</a>';
}
return $links;
}
}
} // endif;
global $rtb_controller;
$rtb_controller = new rtbInit();