-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
401 lines (305 loc) · 9.54 KB
/
functions.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
<?php
/**
* @package WordPress
* @subpackage themename
*/
// LOAD HELPER CLASSES
require_once('lib/classes/class-wp-acf-cpt.php');
require_once('lib/classes/class-validation.php');
require_once('lib/classes/class-ajax_response.php');
require_once('lib/classes/class-geocoder.php');
require_once('lib/classes/class-form-helper.php');
require_once('lib/classes/class-wrappers.php');
require_once('lib/classes/class-action_name.php');
// LOAD CORE CLASSES
require_once('lib/classes/class-hotel.php');
require_once('lib/classes/class-reservation.php');
require_once('lib/classes/class-dashboard.php');
require_once('lib/classes/class-dashboard-date.php');
require_once('lib/classes/class-rates.php');
require_once('lib/classes/class-custom-login.php');
/**
* add_global_query_args function.
*
* @access public
* @return void
*/
function add_global_query_args()
{
global $wp;
if( is_object( $wp ) )
{
$wp->add_query_var('hotel_id');
$wp->add_query_var('reservation_id');
$wp->add_query_var('report_id');
}
}
add_action( 'init', 'add_global_query_args' );
/**
* instantiate_globals function.
*
* @access public
* @param mixed $post
* @return void
*/
function instantiate_globals( $post )
{
global $wp;
if( is_object( $post ) )
{
$name = new Action_Name( $post );
$GLOBALS['page_type'] = $name->type;
}
$hotel_id = (int) get_query_var( 'hotel_id', null );
$reservation_id = (int) get_query_var( 'reservation_id', null );
$report_id = (int) get_query_var( 'report_id', null );
if( $hotel_id != null )
$GLOBALS['current_hotel'] = new Hotel( $hotel_id );
if( $reservation_id != null )
$GLOBALS['current_reservation'] = new Reservation( $reservation_id );
if( $report_id != null )
$GLOBALS['current_report'] = new Report( $report_id );
}
add_action( 'add_globals', 'instantiate_globals', 10, 1 );
/**
* check_if_user_exists function.
*
* @access public
* @return void
*/
function check_if_user_exists()
{
if( !is_user_logged_in() && !is_front_page() )
{
wp_redirect( '/login', 301 );
die();
}
}
add_action( 'template_redirect', 'check_if_user_exists' );
/**
* go_home function.
*
* @access public
* @return void
*/
function go_home()
{
wp_redirect( home_url() );
exit();
}
add_action('wp_logout','go_home');
/**
* create_register_codes_table function.
*
* @access public
* @return void
*/
function create_register_codes_table()
{
global $wpdb, $charset_collate, $db_version;
$table_name = $wpdb->prefix. "register_codes";
$charset_collate = $wpdb->get_charset_collate();
if( $wpdb->get_var( "SHOW TABLES LIKE '" . $table_name . "'" ) != $table_name )
{
$create_sql = "CREATE TABLE " . $table_name . " (
register_code_id INT(11) NOT NULL auto_increment,
register_code VARCHAR(12) NOT NULL,
register_code_used BOOLEAN NOT NULL,
register_code_used_by VARCHAR(300) NOT NULL,
PRIMARY KEY (register_code_id))$charset_collate;";
}
require_once( ABSPATH . "wp-admin/includes/upgrade.php" );
dbDelta( $create_sql );
if( !isset( $wpdb->register_codes ) )
{
$wpdb->register_codes = $table_name;
$wpdb->tables[] = str_replace( $wpdb->prefix, '', $table_name );
}
}
add_action( 'init', 'create_register_codes_table' );
/**
* example_add_dashboard_widgets function.
*
* @access public
* @return void
*/
function example_add_dashboard_widgets()
{
wp_add_dashboard_widget( 'register_codes', 'Generate User Register Codes', 'build_register_codes_widget' );
}
add_action( 'wp_dashboard_setup', 'example_add_dashboard_widgets' );
/**
* build_register_codes_widget function.
*
* @access public
* @return void
*/
function build_register_codes_widget()
{
global $wpdb;
$html = $code_data = '';
$codes = $wpdb->get_results( 'SELECT * FROM '.$wpdb->register_codes );
if( is_array( $codes ) )
{
foreach( $codes as $code )
{
if( $code->register_code_used == 0 )
{
$code_data .= '<tr>
<td>'.$code->register_code.'</td>
<td>
<form data-ajax-form data-action="">
<input type="hidden" name="code_id" value="'.(int) $code->register_code_id.'" />
<button type="submit" class="button button-small">Retire Code</button>
</form>
</td>
</tr>';
}
}
}
$html .= '<div data-overlay-parent><div data-overlay><i class="fa fa-fw fa-spin fa-spinner"></i></div></div>
<form data-ajax-form data-action="generate_register_codes">
<input type="number" name="number_of_codes" value="" />
<button type="submit" class="button btn-primary">Generate Codes</button>
</form>
<br />
<br />
<div data-updateable-content="generate_register_codes">
<table>
<thead>
<th>Code</th>
<th>Actions</th>
</thead>
<tbody>'.$code_data.'</tbody>
</table>
</div>';
echo $html;
}
/**
* generate_register_codes function.
*
* @access public
* @return void
*/
function generate_register_codes()
{
global $wpdb;
$strings = array();
$data = $_POST;
$characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
if( (int) $data['number_of_codes'] > 0 )
{
for( $j = 0; $j < (int) $data['number_of_codes']; $j++ )
{
$code = '';
for( $i = 0; $i < 12; $i++ )
{
$code .= $characters[ rand( 0, strlen( $characters ) - 1 ) ];
}
$strings[] = $code;
$code = '';
}
if( is_array( $strings ) )
{
foreach( $strings as $string )
{
$wpdb->insert( 'lrsgen_register_codes', array( 'register_code' => $string, 'register_code_used' => 0 ), array( '%s', '%d' ) );
}
}
}
}
add_action( 'wp_ajax_generate_register_codes', 'generate_register_codes' );
add_action( 'wp_ajax_nopriv_generate_register_codes', 'generate_register_codes' );
/**
* add_page_header function.
*
* @access public
* @return void
*/
function add_page_header()
{
global $post;
global $page_type;
$html = ' ';
if( is_object( $post ) )
{
$html .= '<header class="window-header"><h2>'.$post->post_title.'</h2></header>';
}
echo $html;
}
add_action( 'content_header', 'add_page_header', 5 );
/**
* add_main_menu function.
*
* @access public
* @return void
*/
function add_main_menu()
{
$menu = '';
$args = array(
'theme_location' => 'primary',
'menu' => 'primary',
'container' => false,
'echo' => false,
'items_wrap' => '%3$s',
);
$menu = wp_nav_menu( $args );
echo $menu;
}
add_action( 'main_navigation', 'add_main_menu', 10 );
/**
* remove_admin_bar function.
*
* @access public
* @return void
*/
function remove_admin_bar()
{
if ( !current_user_can('administrator') && !is_admin() )
{
show_admin_bar(false);
}
}
add_action('after_setup_theme', 'remove_admin_bar');
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// START CUSTOM FUNCTIONS ////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// END CUSTOM FUNCTIONS //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// START FORM HELPER FUNCTIONS ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
function get_hotel_room_types( $hotel )
{
$html = '';
if( is_object( $hotel ) )
{
$options = $hotel->hotel_all_room_types;
if( !empty( $options ) )
{
foreach( $options as $k => $option )
{
$data['field_id'] = 'hotel[hotel_room_types][]';
$data['checkbox'] = $option;
$data['selected'] = !empty( $hotel->hotel_room_types ) ? checked( in_array( $k, $hotel->hotel_room_types ), true, false ) : '' ;
ob_start();
get_template_part_with_data( 'helpers/', 'template','checkbox', $data );
$html .= ob_get_contents();
ob_get_clean();
}
}
}
return $html;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// END FORM HELPER FUNCTIONS /////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// START UTILITY FUNCTIONS ///////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
require_once('utility_functions.php');
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// END UTILITY FUNCTIONS /////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////