-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgk-reservation-steakhouse.php
384 lines (317 loc) · 17.3 KB
/
gk-reservation-steakhouse.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
<?php
/*
Plugin Name: GK Reservation Steakhouse
Plugin URI:
Description: Simple plugin for adding simple reservation form in the Steakhouse theme
Version: 1.0.2
Author: GavickPro
Author URI: http://www.gavick.com
Text Domain: gk-reservation-steakhouse
Domain Path: /languages/
*/
global $pagenow;
if (!class_exists('ReCaptcha') && !function_exists('_recaptcha_qsencode')) {
include_once('recaptchalib.php');
}
/**
* i18n - language files should be like gk-reservation-en_GB.po and gk-reservation-en_GB.mo
*/
add_action( 'plugins_loaded', 'gk_reservation_steakhouse_load_textdomain' );
function gk_reservation_steakhouse_load_textdomain() {
load_plugin_textdomain('gk-reservation-steakhouse', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/');
}
// Actions
add_action('steakhouse_plugin_messages', array('GK_Reservation_Steakhouse', 'action'));
add_action('admin_menu', array('GK_Reservation_Steakhouse', 'admin_menu'));
// Shortcodes
add_shortcode('GK_Reservation', array('GK_Reservation_Steakhouse', 'shortcode'));
// Class with all plugin functionality
class GK_Reservation_Steakhouse {
// Code used to create a shortcode
static function shortcode($atts) {
// Get the shortcode params
$atts = shortcode_atts(array(
'fields' => 'date,time,size,email,name,phone,special',
'recaptcha' => 'off',
'info' => '',
'email' => '',
'email_title' => __('Reservation Form', 'gk-reservation-steakhouse')
), $atts);
// Check if there is a field to render
if($atts['fields'] !== '') {
$output = '<form class="gk-reservation-form" method="post">';
$fields = explode(',', $atts['fields']);
if(in_array('date', $fields) || in_array('time', $fields) || in_array('size', $fields)) {
$output .= '<div class="gk-reservation-party-details">';
}
if(in_array('date', $fields)) {
$date_value = '';
if(isset($_POST['gk-reservation-date'])) {
$date_value = esc_attr($_POST['gk-reservation-date']);
}
$output .= '<div class="gk-reservation-date"><input id="auto-cal" type="text" name="gk-reservation-date" class="gk-required" placeholder="'.__('Date', 'gk-reservation-steakhouse').'" value="'.$date_value.'" /></div>';
}
if(in_array('time', $fields)) {
$time_value = '';
if(isset($_POST['gk-reservation-time'])) {
$time_value = esc_attr($_POST['gk-reservation-name']);
}
$output .= '<div class="gk-reservation-time"><input type="text" name="gk-reservation-time" class="gk-required" placeholder="'.__('Time', 'gk-reservation-steakhouse').'" value="'.$time_value.'" /></div>';
}
if(in_array('size', $fields)) {
$size_value = '';
if(isset($_POST['gk-reservation-size'])) {
$size_value = esc_attr($_POST['gk-reservation-size']);
}
$output .= '<input type="text" name="gk-reservation-size" class="gk-required" placeholder="'.__('Party Size*', 'gk-reservation-steakhouse').'" value="'.$size_value.'" />';
}
if(in_array('date', $fields) || in_array('time', $fields) || in_array('size', $fields)) {
$output .= '</div>';
}
if(in_array('name', $fields) || in_array('email', $fields) || in_array('phone', $fields) || in_array('special', $fields)) {
$output .= '<div class="gk-reservation-party-info">';
}
if(in_array('name', $fields)) {
$name_value = '';
if(isset($_POST['gk-reservation-name'])) {
$name_value = esc_attr($_POST['gk-reservation-name']);
}
$output .= '<input type="text" name="gk-reservation-name" class="gk-required" placeholder="'.__('Name', 'gk-reservation-steakhouse').'" value="'.$name_value.'" />';
}
if(in_array('email', $fields)) {
$email_value = '';
if(isset($_POST['gk-reservation-email'])) {
$email_value = esc_attr($_POST['gk-reservation-email']);
}
$output .= '<input type="email" name="gk-reservation-email" class="gk-required" placeholder="'.__('E-mail', 'gk-reservation-steakhouse').'" value="'.$email_value.'" />';
}
if(in_array('phone', $fields)) {
$phone_value = '';
if(isset($_POST['gk-reservation-phone'])) {
$phone_value = esc_attr($_POST['gk-reservation-phone']);
}
$output .= '<input type="text" name="gk-reservation-phone" class="gk-required" placeholder="'.__('Phone', 'gk-reservation-steakhouse').'" value="'.$phone_value.'" />';
}
if(in_array('special', $fields)) {
$output .= '<textarea name="gk-reservation-special" placeholder="'.__('Special instructions', 'gk-reservation-steakhouse').'">';
if(isset($_POST['gk-reservation-special'])) {
$output .= esc_html(esc_textarea($_POST['gk-reservation-special']));
}
$output .= '</textarea>';
}
if($atts['info'] != '') {
$output .= '<small>' . $atts['info'] . '</small>';
}
if(
$atts['recaptcha'] == 'on' &&
get_option('gk_reservation_steakhouse_recaptcha_public_key', '') != '' &&
get_option('gk_reservation_steakhouse_recaptcha_private_key', '') != ''
) {
wp_enqueue_script( 'gk-captcha-script', 'https://www.google.com/recaptcha/api.js', array( 'jquery' ), false, false);
$publickey = get_option('gk_reservation_steakhouse_recaptcha_public_key', '');
$output .= '<div class="g-recaptcha" data-sitekey="'.$publickey.'"></div>';
}
$output .= '<input type="submit" class="gk-submit button button-border" value="'.__('Make reservation', 'gk-reservation-steakhouse').'" />';
if($atts['email'] !== '') {
$output .= '<input type="hidden" name="gk-reservation-to" value="'.base64_encode($atts['email']).'" />';
}
$output .= '<input type="hidden" name="gk-reservation-title" value="'.$atts['email_title'].'" />';
if(in_array('name', $fields) || in_array('email', $fields) || in_array('phone', $fields) || in_array('special', $fields)) {
$output .= '</div>';
}
$output .= '</form>';
return $output;
}
// Return empty string when there is no fields to render
return '';
}
// Code used to validate and send the message from the form
static function action() {
if(!isset($_POST['gk-reservation-title'])) {
return false;
}
// flag used to detect if the page is validated
$validated = true;
$resp = null;
$errors = array(
"recaptcha" => ''
);
$publickey = get_option('gk_reservation_steakhouse_recaptcha_public_key', '');
$privatekey = get_option('gk_reservation_steakhouse_recaptcha_private_key', '');
// flag to detect if e-mail was sent
$message_sent = false;
// variable for the input fields output
$output = array(
"date" => '',
"time" => '',
"size" => '',
"name" => '',
"email" => '',
"phone" => '',
"special" => ''
);
// check the fields
$fields = array('date', 'time', 'size', 'name', 'email', 'phone');
$fields_len = count($fields);
for($i = 0; $i < $fields_len; $i++) {
if(isset($_POST['gk-reservation-' . $fields[$i]])) {
if($fields[$i] !== '' && trim($_POST['gk-reservation-' . $fields[$i]]) === '') {
$validated = false;
} else {
$output[$fields[$i]] = esc_attr(esc_html($_POST['gk-reservation-' . $fields[$i]]));
if( trim($_POST['gk-reservation-special']) !== '') {
$output['special'] = esc_attr(esc_html($_POST['gk-reservation-special']));
}
}
}
}
// reCAPTCHA validation
if(
isset($_POST['g-recaptcha-response']) &&
$publickey != '' &&
$privatekey != ''
) {
$reCaptcha = new ReCaptcha($privatekey);
$resp = $reCaptcha->verifyResponse(
$_SERVER["REMOTE_ADDR"],
$_POST["g-recaptcha-response"]
);
if ($resp != null && $resp->success ) {
$validated = true;
} else {
$validated = false;
$errors['recaptcha'] = __("The reCAPTCHA wasn't entered correctly. Go back and try it again.", 'gk-reservation-steakhouse');
}
}
// if the all fields was correct
if($validated) {
// send an e-mail
$email = '';
if(isset($_POST['gk-reservation-to'])) {
$email = base64_decode($_POST['gk-reservation-to']);
}
// if the user specified blank e-mail or not specified it
if(trim($email) == '') {
$email = get_option('admin_email');
}
// e-mail structure
$subject = esc_attr(esc_html($_POST['gk-reservation-title']));
$body = "<html>";
$body .= "<body>";
$body .= "<h1 style=\"font-size: 24px; border-bottom: 4px solid #EEE; margin: 10px 0; padding: 10px 0; font-weight: normal; font-style: italic;\">".$subject."</strong></h1>";
if($output['date'] !== '' || $output['time'] !== '' || $output['size'] !== '') {
$body .= "<div>";
$body .= "<h2 style=\"font-size: 16px; font-weight: normal; border-bottom: 1px solid #EEE; padding: 5px 0; margin: 10px 0;\">".__('Party details:', 'gk-reservation-steakhouse')."</h2>";
$body .= "<p>";
if($output['date'] !== '') {
$body .= $output['date'];
}
$body .= ' ';
if($output['time'] !== '') {
$body .= $output['time'];
}
$body .= ' ';
if($output['size'] !== '') {
$body .= __('Party size: ', 'gk-reservation-steakhouse') . $output['size'];
}
$body .= "</p>";
$body .= "</div>";
}
if($output['name'] !== '') {
$body .= "<div>";
$body .= "<h2 style=\"font-size: 16px; font-weight: normal; border-bottom: 1px solid #EEE; padding: 5px 0; margin: 10px 0;\">".__('Name: ', 'gk-reservation-steakhouse')."</h2>";
$body .= "<p>".$output['name']."</p>";
$body .= "</div>";
}
if($output['email'] !== '') {
$body .= "<div>";
$body .= "<h2 style=\"font-size: 16px; font-weight: normal; border-bottom: 1px solid #EEE; padding: 5px 0; margin: 10px 0;\">".__('E-mail: ', 'gk-reservation-steakhouse')."</h2>";
$body .= "<p>".$output['email']."</p>";
$body .= "</div>";
}
if($output['phone'] !== '') {
$body .= "<div>";
$body .= "<h2 style=\"font-size: 16px; font-weight: normal; border-bottom: 1px solid #EEE; padding: 5px 0; margin: 10px 0;\">".__('Phone: ', 'gk-reservation-steakhouse')."</h2>";
$body .= "<p>".$output['phone']."</p>";
$body .= "</div>";
}
if($output['special'] !== '') {
$body .= "<div>";
$body .= "<h2 style=\"font-size: 16px; font-weight: normal; border-bottom: 1px solid #EEE; padding: 5px 0; margin: 10px 0;\">".__('Special instructions:', 'gk-reservation-steakhouse')."</h2>";
$body .= $output['special'];
$body .= "</div>";
}
$body .= "</body>";
$body .= "</html>";
if($output['name'] !== '' && $output['email'] !== '') {
$headers[] = 'From: '.get_bloginfo().' <'.get_option( 'admin_email' ).'>';
$headers[] = 'Reply-To: ' . $output['email'];
$headers[] = 'Content-type: text/html';
} else if($output['name'] !== '' && $output['email'] === '') {
$headers[] = 'From: '.$output['name'];
$headers[] = 'Content-type: text/html';
} else if($output['name'] === '' && $output['email'] !== '') {
$headers[] = 'From: '.get_bloginfo().' <'.get_option( 'admin_email' ).'>';
$headers[] = 'Reply-To: ' . $output['email'];
$headers[] = 'Content-type: text/html';
} else {
$headers[] = 'Content-type: text/html';
}
wp_mail($email, $subject, $body, $headers);
$message_sent = true;
}
if($message_sent) {
unset($_POST['gk-reservation-date']);
unset($_POST['gk-reservation-time']);
unset($_POST['gk-reservation-size']);
unset($_POST['gk-reservation-name']);
unset($_POST['gk-reservation-email']);
unset($_POST['gk-reservation-phone']);
unset($_POST['gk-reservation-special']);
echo '<div class="gk-reservation-success">'.__('Your reservation has been sucessfully sent.', 'gk-reservation-steakhouse').'</div>';
} else {
if(isset($errors) && $errors['recaptcha'] != '') {
echo '<div class="gk-reservation-error">'.__("The reCAPTCHA wasn't entered correctly. Go back and try it again.", 'gk-reservation-steakhouse').'</div>';
} else {
echo '<div class="gk-reservation-error">'.__('Please check the fields in the reservation form before sending.', 'gk-reservation-steakhouse').'</div>';
}
}
}
// Function used for adding custom settings page for the plugin
static function admin_menu() {
//create new top-level menu
add_plugins_page('GK Reservation Steakhouse', 'GK Reservation Steakhouse', 'administrator', __FILE__, array('GK_Reservation_Steakhouse', 'settings_page'));
//call register settings function
add_action('admin_init', array('GK_Reservation_Steakhouse', 'register_settings'));
}
// Function used to specify the basic settings of the plugin
static function register_settings() {
//register our settings
register_setting('gk-reservation-steakhouse-config', 'gk_reservation_steakhouse_recaptcha_public_key');
register_setting('gk-reservation-steakhouse-config', 'gk_reservation_steakhouse_recaptcha_private_key');
}
// Code used to generate the settings page
static function settings_page() {
?>
<div class="wrap">
<h2><?php _e('GK Reservation Steakhouse Plugin Settings', 'gk-reservation-steakhouse'); ?></h2>
<form method="post" action="options.php">
<?php settings_fields('gk-reservation-steakhouse-config'); ?>
<?php do_settings_sections('gk-reservation-steakhouse-config'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e('reCAPTCHA public key', 'gk-reservation-steakhouse'); ?></th>
<td><input type="text" name="gk_reservation_steakhouse_recaptcha_public_key" value="<?php echo esc_attr(get_option('gk_reservation_steakhouse_recaptcha_public_key')); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('reCAPTCHA private key', 'gk-reservation-steakhouse'); ?></th>
<td><input type="text" name="gk_reservation_steakhouse_recaptcha_private_key" value="<?php echo esc_attr(get_option('gk_reservation_steakhouse_recaptcha_private_key')); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
}
// EOF