PluginProbe
WP Directory Kit / 1.1.0
WP Directory Kit v1.1.0
1.5.6 1.5.5 1.5.4 1.5.3 trunk 1.1.0 1.1.6 1.1.8 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.8 1.4.0 1.4.1 All 35 releases
wpdirectorykit / application / helpers / Basic.php

Basic.php in WP Directory Kit 1.1.0, at application/helpers/Basic.php

1,705 lines 60.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly.
5 }
6
7 function wdk_generate_fields($fields, $db_data)
8 {
9 if(is_array($fields))
10 foreach($fields as $field)
11 {
12 $field = (object) $field;
13
14 $field_type = $field->field_type;
15
16 $field_view_path = WPDIRECTORYKIT_PATH.'application/views/fields_edit/'.$field_type.'.php';
17
18 if(file_exists($field_view_path))
19 {
20 include $field_view_path;
21 }
22 else
23 {
24 echo __('Missing VIEW file: ', 'wpdirectorykit').esc_html($field_type).'.php';
25 }
26 }
27 }
28
29 if ( ! function_exists('wdk_generate_search_form'))
30 {
31 function wdk_generate_search_form($form_id=1, $subfolder='', $print = TRUE, $predefinedfields_query = array())
32 {
33 /* load WMVC data */
34 $WMVC = &wdk_get_instance();
35 $WMVC->model('searchform_m');
36 $WMVC->model('field_m');
37 $output = '';
38
39 /* get WMVC form data */
40 $fields = $WMVC->searchform_m->get($form_id, TRUE);
41 $user_fields = NULL;
42
43 /* get WMVC form fields */
44 if(is_object($fields))
45 $user_fields = json_decode($fields->searchform_json);
46
47 /* generate WMVC form fields html */
48 if(is_array($user_fields))
49 foreach($user_fields as $used_field)
50 {
51 if($used_field->field_id == 'cat') {
52 $field = array(
53 "idfield" => 'category',
54 "field_type" => 'CATEGORY',
55 "field_label" => __('Category', 'wpdirectorykit'),
56 "prefix" => '',
57 "suffix" => '',
58 "values_list" => '',
59 );
60 }else if($used_field->field_id == 'loc') {
61 $field = array(
62 "idfield" => 'location',
63 "field_type" => 'LOCATION',
64 "field_label" => __('Location', 'wpdirectorykit'),
65 "prefix" => '',
66 "suffix" => '',
67 "values_list" => '',
68 );
69 }else if($used_field->field_id == 'address') {
70 $field = array(
71 "idfield" => 'address',
72 "field_type" => 'INPUTBOX',
73 "field_label" => __('Address', 'wpdirectorykit'),
74 "prefix" => '',
75 "suffix" => '',
76 "values_list" => '',
77 );
78 }else if($used_field->field_id == 'post_title') {
79 $field = array(
80 "idfield" => 'post_title',
81 "field_type" => 'INPUTBOX',
82 "field_label" => __('Title', 'wpdirectorykit'),
83 "prefix" => '',
84 "suffix" => '',
85 "values_list" => '',
86 );
87 }else if($used_field->field_id == 'search') {
88 $field = array(
89 "idfield" => 'search',
90 "field_type" => 'INPUTBOX',
91 "field_label" => __('Search', 'wpdirectorykit'),
92 "prefix" => '',
93 "suffix" => '',
94 "values_list" => '',
95 );
96 }else if(function_exists('run_wdk_bookings') && $used_field->field_id == 'booking_date') {
97 $field = array(
98 "idfield" => 'booking_date',
99 "field_type" => 'BOOKINGS_DATE',
100 "field_label" => __('Booking Date', 'wpdirectorykit'),
101 "prefix" => '',
102 "suffix" => '',
103 "values_list" => '',
104 );
105 }else if($used_field->field_id == 'more') {
106 $field = array(
107 "idfield" => 'MORE',
108 "field_type" => 'MORE',
109 "field_label" => __('MORE', 'wpdirectorykit'),
110 "prefix" => '',
111 "suffix" => '',
112 "values_list" => '',
113 );
114 } else {
115 $field_data = $WMVC->field_m->get_fields_data($used_field->field_id);
116 if($field_data)
117 $field = array(
118 "idfield" => wmvc_show_data('idfield', $field_data),
119 "field_type" => wmvc_show_data('field_type', $field_data),
120 "field_label" =>wmvc_show_data('field_label', $field_data),
121 "prefix" => wmvc_show_data('prefix', $field_data),
122 "suffix" => wmvc_show_data('suffix', $field_data),
123 "values_list" => wmvc_show_data('values_list', $field_data)
124 );
125 }
126
127 /* if field not detected or remove from fields list, skip field in search */
128 if(empty($field)) continue;
129
130 /* add from user fields data */
131 $field['field_id'] = wmvc_show_data('field_id', $used_field);
132 $field['class'] = wmvc_show_data('class', $used_field);
133 $field['query_type'] = wmvc_show_data('query_type', $used_field);
134 $field['columns'] = wmvc_show_data('columns', $used_field);
135
136
137 $field_data = array();
138 $field_data ['field_data'] = $field;
139 $field_data ['predefinedfields_query'] = $predefinedfields_query;
140
141 $field_view_path = WPDIRECTORYKIT_PATH.'application/views/search_fields/'.$subfolder.(wmvc_show_data('field_type', $field)).'.php';
142 if(file_exists($field_view_path))
143 {
144 $output .= $WMVC->view('search_fields/'.$subfolder.(wmvc_show_data('field_type', $field)), $field_data, FALSE);
145 }
146 else
147 {
148 /* show missing view file of field, only if print enable */
149 if($print)
150 $output .= __('Missing VIEW file: ', 'wpdirectorykit').esc_html($subfolder).(wmvc_show_data('field_type', $field)).'.php';
151 }
152
153 }
154
155 if($print){
156 echo $output;
157 return FALSE;
158 }
159
160 return $output;
161 }
162 }
163
164 function &wdk_get_instance()
165 {
166 global $Winter_MVC_WDK;
167
168 if(empty($Winter_MVC_WDK))
169 {
170 $Winter_MVC_WDK = new MVC_Loader(plugin_dir_path( __FILE__ ).'../../');
171
172 $Winter_MVC_WDK->load_helper('basic');
173 }
174
175 return $Winter_MVC_WDK;
176 }
177
178 function wdk_prepare_search_query_GET($columns = array(), $model_name = NULL, $external_columns = array(), $custom_parameters = array(), $skip_postget = FALSE)
179 {
180 global $Winter_MVC_WDK;
181 $WMVC = &$Winter_MVC_WDK;
182 $wdk_bookings_joinded = false;
183 static $available_fields_listings = NULL;
184 static $available_fields_static = array();
185 static $list_fields_static = NULL;
186
187 $_GET_clone = array();
188
189 if(!$skip_postget)
190 $_GET_clone = array_merge($_GET, $_POST);
191
192 if(isset($_GET_clone['order_by']) && $_GET_clone['order_by'] =='undefined') {
193 unset($_GET_clone['order_by']);
194 }
195
196 if(isset($custom_parameters['order_by'])) {
197
198 if(isset($_GET_clone['order_by']) && !empty($_GET_clone['order_by'])) {
199 $_GET_clone['order_by'] = $custom_parameters['order_by'].', '.$_GET_clone['order_by'];
200 } else {
201 $_GET_clone['order_by'] = $custom_parameters['order_by'];
202 }
203
204 unset($custom_parameters['order_by']);
205 }
206
207
208 if(!empty($custom_parameters))
209 $_GET_clone = array_merge($_GET_clone, $custom_parameters);
210
211 $WMVC->model('listing_m');
212 $WMVC->model('field_m');
213 $WMVC->model('listingfield_m');
214
215
216 $smart_search = '';
217 if(isset($_GET_clone['search']))
218 $smart_search = sanitize_text_field($_GET_clone['search']);
219
220 if(isset($_GET_clone['field_search']))
221 $smart_search = sanitize_text_field($_GET_clone['field_search']);
222
223 if(!isset($available_fields_static[$model_name]))
224 {
225 $available_fields = $WMVC->$model_name->get_available_fields();
226
227 $available_fields_static[$model_name] = $available_fields;
228 }
229 else
230 {
231 $available_fields = $available_fields_static[$model_name];
232 }
233
234 if($available_fields_listings === NULL)
235 $available_fields_listings = $WMVC->listingfield_m->get_available_fields();
236
237 /* Пet column names from wdk_listings_fields and add to allow search +
238 in _GET_clone replace field_#id to field_id_TYPE*/
239
240 if($list_fields_static === NULL)
241 {
242 $WMVC->db->where(array('field_type !='=> 'SECTION'));
243 $list_fields = $WMVC->field_m->get();
244 $list_fields_static = $list_fields;
245 }
246 else
247 {
248 $list_fields = $list_fields_static;
249 }
250
251
252 foreach($list_fields as $field_data) {
253
254 $field_id = wmvc_show_data('idfield',$field_data);
255 $field_type = wmvc_show_data('field_type',$field_data);
256 $column_name ='field_'.$field_id.'_'.$field_type;
257 if(!isset($available_fields_listings[$column_name])) continue;
258
259 if(isset($_GET_clone['field_'.$field_id])) {
260 $_GET_clone['field_'.$field_id.'_'.$field_type] = apply_filters( 'wdk-currency-conversion/convert/default_value',sanitize_text_field($_GET_clone['field_'.$field_id]), $field_id);
261 }
262
263 if(isset($_GET_clone['field_'.$field_id.'_max'])) {
264 $_GET_clone['field_'.$field_id.'_'.$field_type.'_max'] = apply_filters( 'wdk-currency-conversion/convert/default_value',sanitize_text_field($_GET_clone['field_'.$field_id.'_max']), $field_id);
265 }
266 if(isset($_GET_clone['field_'.$field_id.'_min'])) {
267 $_GET_clone['field_'.$field_id.'_'.$field_type.'_min'] = apply_filters( 'wdk-currency-conversion/convert/default_value',sanitize_text_field($_GET_clone['field_'.$field_id.'_min']), $field_id);
268 }
269 if(isset($_GET_clone['field_'.$field_id.'_exactly'])) {
270 $_GET_clone['field_'.$field_id.'_'.$field_type.'_exactly'] = sanitize_text_field($_GET_clone['field_'.$field_id.'_exactly']);
271 }
272
273 $columns[] = $column_name;
274 $columns[] = $column_name.'_max';
275 $columns[] = $column_name.'_min';
276 $columns[] = $column_name.'_exactly';
277 $external_columns[] = $column_name;
278 $external_columns[] = $column_name.'_max';
279 $external_columns[] = $column_name.'_min';
280 $external_columns[] = $column_name.'_exactly';
281 }
282
283 if(isset($_GET_clone['search_location'])) {
284 $column_name ='location_id';
285 $_GET_clone[$column_name] = $_GET_clone['search_location'];
286 $columns[] = $column_name;
287 $external_columns[] = $column_name;
288 }
289
290 if(isset($_GET_clone['search_category'])) {
291 $column_name ='category_id';
292 $_GET_clone[$column_name] = $_GET_clone['search_category'];
293 $columns[] = $column_name;
294 $external_columns[] = $column_name;
295 }
296
297 if(isset($_GET_clone['field_address'])) {
298 $column_name ='address';
299 $_GET_clone[$column_name] = $_GET_clone['field_address'];
300 $columns[] = $column_name;
301 $external_columns[] = $column_name;
302 }
303
304 if (function_exists('run_wdk_bookings') && isset($_GET_clone['field_booking_date_from'])) {
305 $column_name ='field_booking_date_from';
306 $_GET_clone[$column_name] = $_GET_clone['field_booking_date_from'];
307 $columns[] = $column_name;
308 $external_columns[] = $column_name;
309 }
310
311 if (function_exists('run_wdk_bookings') && isset($_GET_clone['field_booking_date_to'])) {
312 $column_name ='field_booking_date_to';
313 $_GET_clone[$column_name] = $_GET_clone['field_booking_date_to'];
314 $columns[] = $column_name;
315 $external_columns[] = $column_name;
316 }
317
318 //$table_name = substr($model_name, 0, -2);
319 $columns_original = array();
320 foreach($columns as $key=>$val)
321 {
322 $columns_original[$val] = $val;
323
324 // if column contain also "table_name.*"
325 $splited = explode('.', $val);
326 if(wmvc_count($splited) == 2)
327 $val = $splited[1];
328
329 if(isset($available_fields[$val]))
330 {
331
332 }
333 else
334 {
335 if(!in_array($columns[$key], $external_columns))
336 {
337 unset($columns[$key]);
338 }
339 }
340 }
341
342 if(wmvc_count($_GET_clone) > 0)
343 {
344 unset($_GET_clone['search']);
345 unset($_GET_clone['field_search']);
346
347 // For quick/smart search
348 if(wmvc_count($columns) > 0 && !empty($smart_search))
349 {
350 $gen_q = '';
351 foreach($columns as $key=>$value)
352 {
353 if(substr_count($value, 'id') > 0 && is_numeric($smart_search))
354 {
355 $gen_q.="$value = $smart_search OR ";
356 }
357 else if(substr_count($value, 'date') > 0)
358 {
359 $gen_search = $smart_search;
360
361 $gen_q.="$value LIKE '%$gen_search%' OR ";
362 }
363 elseif(substr($value, -8) == '_exactly')
364 {
365
366 }
367 elseif(substr($value, -4) == '_max')
368 {
369
370 }
371 else if(substr($value, -4) == '_min')
372 {
373
374 }
375 else
376 {
377 $gen_q.="$value LIKE '%$smart_search%' OR ";
378 }
379 }
380
381 for($i = 1 ; $i <= 2; $i++){
382 $gen_q.="location_".$i.".location_title LIKE '%$smart_search%' OR ";
383 }
384
385 $gen_q = substr($gen_q, 0, -4);
386
387 if(!empty($gen_q))
388 $WMVC->db->where("($gen_q)");
389 }
390
391 // For column search
392 if(isset($_GET_clone))
393 {
394 $gen_q = '';
395
396 foreach($_GET_clone as $key=>$val)
397 {
398 if(!empty($val) && in_array($key, $columns))
399 {
400 $col_name = $key;
401
402 $val = esc_sql($val);
403 //if(isset($key))
404 // $col_name = $key;
405
406 if(strpos($key, 'skip') !== FALSE) continue;
407
408 if($key == 'field_booking_date_from')
409 {
410 if(!$wdk_bookings_joinded){
411 $WMVC->db->join($WMVC->db->prefix.'wdk_booking_price ON '.$WMVC->listing_m->_table_name.'.post_id = '.$WMVC->db->prefix.'wdk_booking_price.post_id', NULL, 'LEFT');
412 $WMVC->db->distinct($WMVC->listing_m->_table_name.'.post_id');
413 $WMVC->db->join($WMVC->db->prefix.'wdk_booking_reservation ON '.$WMVC->listing_m->_table_name.'.post_id = '.$WMVC->db->prefix.'wdk_booking_price.post_id', NULL, 'LEFT');
414 $wdk_bookings_joinded = true;
415 $gen_q.= $WMVC->db->prefix.'wdk_booking_price.is_activated = 1 AND ';
416 }
417
418 if((bool)strtotime($val) && strtotime($val) > 1000)
419 {
420 if(!get_option('wdk_bookings_is_hours_enabled')) {
421 $gen_search = date('Y-m-d', strtotime($val));
422 $gen_q.= "DATE_FORMAT(".$WMVC->db->prefix."wdk_booking_price.date_from, '%Y-%m-%d') <= '".$gen_search."' AND ";
423 } else {
424 $gen_search = date('Y-m-d H:i:s', strtotime($val));
425 $gen_q.= $WMVC->db->prefix."wdk_booking_price.date_from >'".$gen_search."' AND ";
426 }
427 }
428
429 if( isset($_GET_clone['field_booking_date_from']) && isset($_GET_clone['field_booking_date_to'])) {
430
431 if(!get_option('wdk_bookings_is_hours_enabled')) {
432 $gen_search_from = date('Y-m-d', strtotime($_GET_clone['field_booking_date_from']));
433 $gen_search_to = date('Y-m-d', strtotime($_GET_clone['field_booking_date_to']));
434
435 $gen_q.= $WMVC->listing_m->_table_name.".post_id NOT IN (
436 SELECT post_id
437 FROM ".$WMVC->db->prefix."wdk_booking_reservation
438 WHERE
439 ".$WMVC->db->prefix."wdk_booking_reservation.is_approved = 1
440 AND
441 DATE_FORMAT(".$WMVC->db->prefix."wdk_booking_reservation.date_from, '%Y-%m-%d') < '".$gen_search_to."'
442 AND
443 DATE_FORMAT(".$WMVC->db->prefix."wdk_booking_reservation.date_to, '%Y-%m-%d') > '".$gen_search_from."'
444 ) AND ";
445
446 } else {
447 $gen_search_from = date('Y-m-d H:i:s', strtotime($_GET_clone['field_booking_date_from']));
448 $gen_search_to = date('Y-m-d H:i:s', strtotime($_GET_clone['field_booking_date_to']));
449
450 $gen_q.= $WMVC->listing_m->_table_name.".post_id NOT IN (
451 SELECT post_id
452 FROM ".$WMVC->db->prefix."wdk_booking_reservation
453 WHERE
454 ".$WMVC->db->prefix."wdk_booking_reservation.is_approved = 1
455 AND
456 ".$WMVC->db->prefix."wdk_booking_reservation.date_from < '".$gen_search_to."'
457 AND
458 ".$WMVC->db->prefix."wdk_booking_reservation.date_to > '".$gen_search_from."'
459 ) AND ";
460 }
461 }
462
463 }
464 elseif($key == 'field_booking_date_to')
465 {
466 if(!$wdk_bookings_joinded){
467 $WMVC->db->join($WMVC->db->prefix.'wdk_booking_price ON '.$WMVC->listing_m->_table_name.'.post_id = '.$WMVC->db->prefix.'wdk_booking_price.post_id',NULL, 'LEFT');
468 $WMVC->db->join($WMVC->db->prefix.'wdk_booking_reservation ON '.$WMVC->listing_m->_table_name.'.post_id = '.$WMVC->db->prefix.'wdk_booking_price.post_id',NULL, 'LEFT');
469 $wdk_bookings_joinded = true;
470 $gen_q.= $WMVC->db->prefix.'wdk_booking_price.is_activated = 1 AND ';
471 }
472
473 if((bool)strtotime($val) && strtotime($val) > 1000)
474 {
475
476 if(!get_option('wdk_bookings_is_hours_enabled')) {
477 $gen_search = date('Y-m-d', strtotime($val));
478 $gen_q.= "DATE_FORMAT(".$WMVC->db->prefix."wdk_booking_price.date_to, '%Y-%m-%d') >= '".$gen_search."' AND ";
479 } else {
480 $gen_search = date('Y-m-d H:i:s', strtotime($val));
481 $gen_q.= $WMVC->db->prefix."wdk_booking_price.date_to >'".$gen_search."' AND ";
482 }
483 }
484
485 }
486 elseif(substr($key, -8) == '_exactly')
487 {
488 $col_name = substr($key, 0, -8);
489 $gen_q.=$col_name." = '".$val."' AND ";
490 }
491 elseif(substr($key, -4) == '_max')
492 {
493 $col_name = substr($key, 0, -4);
494
495 if(strpos($key, 'NUMBER') !== FALSE) {
496 $gen_q.=$col_name." <= ".intval($val)." AND ";
497 } else {
498 $gen_q.=$col_name." <= '".$val."' AND ";
499 }
500 }
501 else if(substr($key, -4) == '_min')
502 {
503 $col_name = substr($key, 0, -4);
504
505 if(strpos($key, 'NUMBER') !== FALSE) {
506 $gen_q.=$col_name." >= ".intval($val)." AND ";
507 } else {
508 $gen_q.=$col_name." >= '".$val."' AND ";
509 }
510 }
511 elseif(substr_count($key, 'id') > 0 && is_numeric($val))
512 {
513 // ID is always numeric
514
515 if($key == 'location_id') {
516 $gen_q .= "(`location_1`.`parent_path` = ".$val." OR ";
517 $gen_q .= "`location_2`.`parent_path` = ".$val." OR ";
518 $gen_q .= $col_name." = ".$val.") AND ";
519 } elseif($key == 'category_id') {
520 $gen_q .= "('parent_path' = ".$val." OR ";
521 $gen_q .= $col_name." = ".$val.") AND ";
522 } else {
523 $gen_q.=$col_name." = ".$val." AND ";
524 }
525 }
526 else if(substr_count($key, 'date') > 0)
527 {
528 // DATE VALUES
529
530 $gen_search = $val;
531
532 $detect_date = strtotime($gen_search);
533
534 if(wdk_is_date($val) && $detect_date > 1000)
535 {
536 $gen_search = date('Y-m-d H:i:s', $detect_date);
537 $gen_q.=$col_name." > '".$gen_search."' AND ";
538 }
539 else
540 {
541 $gen_q.=$col_name." LIKE '%".$gen_search."%' AND ";
542
543 }
544 }
545 else if(substr_count($key, 'is_') > 0)
546 {
547 // CHECKBOXES
548
549 if($val=='on')
550 {
551 $gen_search = 1;
552 $gen_q.=$col_name." LIKE '%".$gen_search."%' AND ";
553 }
554 else if($val=='off')
555 {
556 $gen_q.=$col_name." IS NULL AND ";
557 }
558 }
559 else
560 {
561 $gen_q.=$col_name." LIKE '%".$val."%' AND ";
562 }
563 }
564
565 }
566
567 $gen_q = substr($gen_q, 0, -5);
568
569 if(!empty($gen_q))
570 $WMVC->db->where("($gen_q)");
571 }
572
573 // order
574 if(isset($_GET_clone['order_by']))
575 {
576 $_GET_clone['order_by'] = str_replace('post_id', $WMVC->db->prefix.'wdk_listings.post_id', $_GET_clone['order_by']);
577 $WMVC->db->order_by($_GET_clone['order_by']);
578 }
579
580 }
581 }
582
583 function wdk_users_prepare_search_query_GET($columns = array(), $model_name = NULL, $external_columns = array(), $custom_parameters = array(), $skip_postget = FALSE)
584 {
585 global $Winter_MVC_WDK;
586 $WMVC = &$Winter_MVC_WDK;
587
588 $_GET_clone = array();
589
590 if(!$skip_postget)
591 $_GET_clone = array_merge($_GET, $_POST);
592
593 if(isset($custom_parameters['order_by'])) {
594
595 if(isset($_GET_clone['order_by']) && !empty($_GET_clone['order_by'])) {
596 $_GET_clone['order_by'] = $custom_parameters['order_by'].', '.$_GET_clone['order_by'];
597 } else {
598 $_GET_clone['order_by'] = $custom_parameters['order_by'];
599 }
600
601 unset($custom_parameters['order_by']);
602 }
603
604
605 if(!empty($custom_parameters))
606 $_GET_clone = array_merge($_GET_clone, $custom_parameters);
607
608 $WMVC->model($model_name);
609
610 $smart_search = '';
611 if(isset($_GET_clone['profile_search']))
612 $smart_search = sanitize_text_field($_GET_clone['profile_search']);
613
614 $available_fields = array('user_login','user_nicename','user_email','user_url','display_name');
615
616 if(isset($_GET_clone['is_activated'])){
617 $_GET_clone[$WMVC->$model_name->_table_name.'.is_activated'] = sanitize_text_field($_GET_clone['is_activated']);
618 unset($_GET_clone['is_activated']);
619 }
620
621 if(isset($_GET_clone['is_approved'])){
622 $_GET_clone[$WMVC->$model_name->_table_name.'.is_approved'] = sanitize_text_field($_GET_clone['is_approved']);
623 unset($_GET_clone['is_approved']);
624 }
625
626
627 //$table_name = substr($model_name, 0, -2);
628 $columns_original = array();
629 foreach($columns as $key=>$val)
630 {
631 $columns_original[$val] = $val;
632
633 // if column contain also "table_name.*"
634 $splited = explode('.', $val);
635 if(wmvc_count($splited) == 2)
636 $val = $splited[1];
637
638 if(isset($available_fields[$val]))
639 {
640
641 }
642 else
643 {
644 if(!in_array($columns[$key], $external_columns))
645 {
646 unset($columns[$key]);
647 }
648 }
649 }
650
651 if(wmvc_count($_GET_clone) > 0)
652 {
653 unset($_GET_clone['search']);
654
655 // For quick/smart search
656 if(wmvc_count($columns) > 0 && !empty($smart_search))
657 {
658 $gen_q = '';
659 foreach($columns as $key=>$value)
660 {
661
662 if($value == 'post_type')
663 {
664 $value = $WMVC->$model_name->_table_name.'.'.$value;
665 }
666
667 if(substr_count($value, 'id') > 0 && is_numeric($smart_search))
668 {
669 $gen_q.="$value = $smart_search OR ";
670 }
671 else if(substr_count($value, 'date') > 0)
672 {
673 //$gen_search = wmvc_generate_slug($smart_search, ' ');
674
675 $gen_search = $smart_search;
676
677 $gen_q.="$value LIKE '%$gen_search%' OR ";
678 }
679 else
680 {
681 $gen_q.="$value LIKE '%$smart_search%' OR ";
682 }
683 }
684 $gen_q = substr($gen_q, 0, -4);
685
686 if(!empty($gen_q))
687 $WMVC->db->where("($gen_q)");
688 }
689
690 // For column search
691 if(isset($_GET_clone))
692 {
693 $gen_q = '';
694
695 foreach($_GET_clone as $key=>$val)
696 {
697 if(!empty($val) && in_array($key, $columns))
698 {
699 $col_name = $key;
700
701 if($col_name == 'post_type')
702 {
703 $col_name = $WMVC->$model_name->_table_name.'.'.$col_name;
704 }
705
706 //if(isset($key))
707 // $col_name = $key;
708
709 if(strpos($key, 'skip') !== FALSE) continue;
710
711 if(substr($key, -8) == '_exactly')
712 {
713 $col_name = substr($key, 0, -8);
714 $gen_q.=$col_name." = '".$val."' AND ";
715 }
716 elseif(substr($key, -4) == '_max')
717 {
718 $col_name = substr($key, 0, -4);
719 $gen_q.=$col_name." <= '".$val."' AND ";
720 }
721 else if(substr($key, -4) == '_min')
722 {
723 $col_name = substr($key, 0, -4);
724
725 $gen_q.=$col_name." >= '".$val."' AND ";
726 }
727 elseif(substr_count($key, 'id') > 0 && is_numeric($val))
728 {
729 // ID is always numeric
730
731 $gen_q.=$col_name." = ".$val." AND ";
732 }
733 else if(substr_count($key, 'date') > 0)
734 {
735 // DATE VALUES
736
737 $gen_search = $val;
738
739 $detect_date = strtotime($gen_search);
740
741 if(wdk_is_date($val) && $detect_date > 1000)
742 {
743 $gen_search = date('Y-m-d H:i:s', $detect_date);
744 $gen_q.=$col_name." > '".$gen_search."' AND ";
745 }
746 else
747 {
748 $gen_q.=$col_name." LIKE '%".$gen_search."%' AND ";
749 }
750 }
751 else if(substr_count($key, 'is_') > 0)
752 {
753 // CHECKBOXES
754
755 if($val=='on')
756 {
757 $gen_search = 1;
758 $gen_q.=$col_name." LIKE '%".$gen_search."%' AND ";
759 }
760 else if($val=='off')
761 {
762 $gen_q.=$col_name." IS NULL AND ";
763 }
764 }
765 else
766 {
767 $gen_q.=$col_name." LIKE '%".$val."%' AND ";
768 }
769 }
770
771 }
772
773 $gen_q = substr($gen_q, 0, -5);
774
775 if(!empty($gen_q))
776 $WMVC->db->where("($gen_q)");
777 }
778
779 // order
780 if(isset($_GET_clone['order_by']))
781 {
782 $_GET_clone['order_by'] = str_replace('post_id', $WMVC->db->prefix.'wdk_favorite.post_id', $_GET_clone['order_by']);
783 $WMVC->db->order_by($_GET_clone['order_by']);
784 }
785
786 }
787 }
788
789 function wdk_date() {
790 $date_format = get_option('date_format');
791 $time_format = get_option('time_format');
792 $date = date("{$date_format} {$time_format}", current_time('timestamp'));
793 return $date;
794 }
795
796 if(!function_exists('wdk_placeholder_image_src')) {
797 function wdk_placeholder_image_src () {
798 $i = WPDIRECTORYKIT_URL.'/public/img/placeholder.jpg';
799
800 return $i;
801 }
802 }
803
804
805 function wdk_wp_frontend_paginate($total_items, $per_page = 10, $page_var = 'wmvc_paged', $texts = array(), $enable_latest_first = TRUE, $enable_count = FALSE, $enable_compact = FALSE, $limit_items = 1)
806 {
807 $current_page = 1;
808
809 if(isset($_GET[$page_var]))
810 $current_page = intval(wmvc_xss_clean($_GET[$page_var]));
811
812 if(!isset($texts['previous_page']))$texts['previous_page'] = 'Previous page';
813 if(!isset($texts['next_page']))$texts['next_page'] = 'Next page';
814 if(!isset($texts['first_page']))$texts['first_page'] = '';
815 if(!isset($texts['last_page']))$texts['last_page'] = '';
816 if(!isset($texts['items']))$texts['items'] = 'items';
817
818 if(empty($current_page))$current_page = 1;
819
820 // get url
821 $url = strtok($_SERVER["REQUEST_URI"], '?');
822 $qs_parameters = wmvc_xss_clean( $_GET );
823 unset($qs_parameters[$page_var]);
824
825 $qs_part = http_build_query($qs_parameters);
826 $url.='?'.$qs_part;
827
828 // total pages
829 $total_pages = intval($total_items/$per_page+0.99);
830
831 if($current_page == 1) {
832 $limit_items = $limit_items * 2;
833 }
834
835 if($current_page == $total_pages) {
836 $limit_items = $limit_items * 2;
837 }
838
839 $output = '<nav class="wdk-pagination navigation pagination" role="navigation">';
840 if($enable_count){
841 $output.= '<span class="displaying-num">'.$total_items.' '.$texts['items'].'</span>';
842 }
843
844 $output .= '<div class="nav-links">';
845
846 if($enable_latest_first && $current_page-1 > 0)
847 $output.= '<a class="next page-numbers" href="'.esc_url($url).'#results"><span class="screen-reader-text">'.esc_html($texts['first_page']).'</span><span aria-hidden="true">«</span></a>';
848
849 if ($current_page-1 > 0) {
850 $output.= '<a class="next page-numbers" href="'.esc_url($url).'&amp;'.esc_attr($page_var).'='.esc_attr($current_page-1).'#results"><span class="screen-reader-text">'.esc_html($texts['previous_page']).'</span><span aria-hidden="true">‹</span></a>';
851 } elseif($enable_compact)
852 {
853 $output.= '<span class="next page-numbers disabled" href=""><span class="screen-reader-text">'.esc_html($texts['previous_page']).'</span><span aria-hidden="true">‹</span></span>';
854 }
855
856 if (!$enable_compact) {
857 for ($i = 1 ; $i <= $total_pages; $i++) {
858 $class = ($current_page == $i) ? "active" : "";
859 if(($current_page-$limit_items) <= $i && ($current_page+$limit_items) >= $i) {
860 if (($current_page == $i)) {
861 $output.= '<span aria-current="page" class="page-numbers current">'.$i.'</span>';
862 } else {
863 $output.= '<a class="page-numbers" href="'.esc_url($url).'&amp;'.esc_attr($page_var).'='.esc_attr($i).'#results">'.esc_attr($i).'</a>';
864 }
865 }
866 }
867 } else {
868 $output.= '<span class="wdk_pages_range">'.$current_page.' '.esc_html__('of','wpdirectorykit').' '.$total_pages.'</span>';
869 }
870
871 if($current_page+1 <= $total_pages)
872 {
873 $output.= '<a class="next page-numbers" href="'.esc_url($url).'&amp;'.esc_attr($page_var).'='.esc_attr($current_page+1).'#results"><span class="screen-reader-text">'.esc_html($texts['next_page']).'</span><span aria-hidden="true">›</span></a>';
874 }
875 elseif($enable_compact)
876 {
877 $output.= '<a class="next page-numbers disabled" href=""><span class="screen-reader-text">'.esc_html($texts['next_page']).'</span><span aria-hidden="true">›</span></a>';
878 }
879
880 if($enable_latest_first && $current_page+1 <= $total_pages)
881 $output.= '<a class="next page-numbers" href="'.esc_url($url).'&amp;'.esc_attr($page_var).'='.esc_attr($total_pages).'#results"><span class="screen-reader-text">'.esc_html($texts['last_page']).'</span><span aria-hidden="true">»</span></a>';
882
883
884 $output.= '</div>';
885 $output.= '</nav>';
886
887 return $output;
888 }
889
890 function wdk_viewe($content) {
891 // @codingStandardsIgnoreStart
892 echo wp_kses_post($content); // WPCS: XSS ok, sanitization ok.
893 // @codingStandardsIgnoreEnd
894 }
895
896
897 if(!function_exists('wdk_search_fields_toggle')){
898 function wdk_search_fields_toggle ($class_add = NULL){
899 static $wdk_visible_filters = 0;
900 global $wdk_visible_filters_limit;
901 global $wdk_button_search_defined;
902 global $wdk_enable_search_fields_toggle;
903 global $wdk_activate_more;
904
905 if(!$wdk_enable_search_fields_toggle) return false;
906
907 $wdk_visible_filters++;
908
909 if($wdk_activate_more || (!$wdk_button_search_defined && $wdk_visible_filters == $wdk_visible_filters_limit)){
910 $wdk_button_search_defined=true;
911 $wdk_activate_more = false;
912
913 global $wdk_text_search_button;
914 if(empty($wdk_text_search_button))
915 $wdk_text_search_button = esc_html__('Search','wpdirectorykit');
916
917 global $wdk_text_more_button;
918 if(empty($wdk_text_more_button))
919 $wdk_text_more_button = esc_html__('More','wpdirectorykit');
920
921 $form_closed = 'display: none;';
922 if(isset($_GET['wdk_search_additional_opened']) && wmvc_xss_clean($_GET['wdk_search_additional_opened']) == 1) {
923 $form_closed = '';
924 }
925
926 ?>
927 <div class="wdk-col wdk-col-btns">
928 <div class="wdk-field wdk-field-btn">
929 <div class="wdk-field-group">
930 <button id="wdk-search-additional" type="button" class="wdk-search-additional-btn"><?php echo esc_html($wdk_text_more_button); ?><i class="wdk-toggle-icon"></i></button>
931 <input type='checkbox' style="display: none !important" value='1' name='wdk_search_additional_opened' />
932 </div>
933 <div class="wdk-field-group">
934 <button id="wdk-start-primary" type="submit" class="wdk-search-start wdk-click-load-animation">&nbsp;&nbsp;<?php echo esc_html($wdk_text_search_button);?>&nbsp;<i class="fa fa-spinner fa-spin fa-ajax-indicator" style="display: none;"></i>&nbsp;</button>
935 <?php if(function_exists('run_wdk_save_search') && get_option('wdk_save_search_show_on_searchform')):?>
936 <div class="section-widget-control right">
937 <a class="wdk-c-btn wdk-c-edit wdk-save-search-button" href="#" data-url="<?php echo esc_url(admin_url('admin-ajax.php')); ?>" title="<?php echo esc_attr_e('Save Search', 'selio'); ?>" target="_blank">
938 <i class="fas fa-save" aria-hidden="true"></i>
939 <i class="fa fa-spinner fa-spin fa-ajax-indicator"></i>
940 </a>
941 </div>
942 <?php endif;?>
943 </div>
944 </div>
945 </div>
946 <div id='wdk-form-additional' class="wdk-col" style="<?php echo esc_attr($form_closed) ;?>">
947 <div class="wdk-row">
948 <?php
949 }
950 }
951 }
952
953
954 /*
955 * @param $filter_ids string|array, included ids, what will be visible, other skiped, if set 1_fetch_child, where 1 is category_id, will be detected all childs and filtered, for visible
956 *
957 */
958 if ( ! function_exists('wdk_treefield_option'))
959 {
960 function wdk_treefield_option($name = '', $table=NULL, $selected = NULL,
961 $column = 'category_title', $language_id=NULL, $empty_value='', $filter_ids = '', $user_check = FALSE)
962 {
963 $WMVC = &wdk_get_instance();
964
965 if(is_array($filter_ids)) $filter_ids = implode(',', $filter_ids);
966
967 static $counter = 0;
968
969 $model_name = $table;
970 if($table == 'calendar_listing_m')
971 $model_name = 'listing_m';
972
973 $table_name = str_replace('_m', '', $table);
974
975 $attribute_id = 'id'.$table_name;
976
977 if($table_name == 'icons_list') {
978
979 } else {
980 $WMVC->model($model_name);
981 $attribute_id = $WMVC ->$model_name->_primary_key;
982 $selected = (int) $selected;
983 }
984
985 if(empty($selected))
986 $selected='';
987
988 $form = '<input name="'.$name.'" value="'.$selected.'" class="wdk-hidden" type="text" id="wdktreeelem'.$counter.'" readonly/>';
989
990 $skip_id = '';
991 //load javascript library
992 if($counter==0)
993 {
994 wp_enqueue_script('wdk-treefield');
995 wp_enqueue_style('wdk-treefield');
996 }
997 ?>
998 <script>
999 jQuery(document).ready(function($) {
1000 $('#wdktreeelem<?php echo esc_js($counter);?>:not(.init)').wdkTreefield({
1001 ajax_url: '<?php echo admin_url('admin-ajax.php'); ?>',
1002 ajax_param: {
1003 "page": 'wdk_frontendajax',
1004 "function": 'treefieldid',
1005 "action": 'wdk_public_action',
1006 "table": '<?php echo esc_js($table); ?>',
1007 "filter_ids": '<?php echo esc_js($filter_ids); ?>',
1008 "start_id": '<?php if(!empty($filter_ids)) esc_js($selected); else echo ""; ?>',
1009 "empty_value": '<?php echo esc_js($empty_value); ?>',
1010 "user_check": '<?php echo esc_js($user_check); ?>'
1011 },
1012 attribute_id: '<?php echo esc_js($attribute_id); ?>',
1013 language_id: '<?php echo esc_js($language_id); ?>',
1014 attribute_value: '<?php echo esc_js($column); ?>',
1015 skip_id: '<?php echo esc_js($skip_id); ?>',
1016 empty_value: ' - ',
1017 text_search: '<?php esc_html_e('Search term', 'wpdirectorykit');?>',
1018 text_no_results: '<?php esc_html_e('No results found', 'wpdirectorykit');?>',
1019 callback_selected: function(key) {
1020 $('#wdktreeelem<?php echo esc_js($counter);?>').trigger("change");
1021 }
1022 }).addClass('init');
1023 });
1024 </script>
1025 <?php
1026 $counter++;
1027 return $form;
1028 }
1029 }
1030
1031 if ( ! function_exists('wdk_filter_decimal'))
1032 {
1033 function wdk_filter_decimal($string = '')
1034 {
1035 if(substr($string, -3, 3) === ".00") {
1036 return substr($string, 0, -3);
1037 }
1038
1039 return $string;
1040 }
1041 }
1042
1043 if ( ! function_exists('wdk_is_date'))
1044 {
1045 function wdk_is_date($date, $format = 'Y-m-d H:i:s')
1046 {
1047 $d = DateTime::createFromFormat($format, $date);
1048 return $d && $d->format($format) == $date;
1049 }
1050 }
1051
1052 if ( ! function_exists('wdk_url_suffix'))
1053 {
1054 function wdk_url_suffix($base_url, $extension_url="")
1055 {
1056 if(strpos($base_url,'?') !== FALSE){
1057 $base_url .='&';
1058 } else {
1059 $base_url .='?';
1060 }
1061 return $base_url.$extension_url;
1062 }
1063 }
1064
1065 /**
1066 * Insert an attachment from an URL address.
1067 *
1068 * @param String $url
1069 * @param Int $parent_post_id
1070 * @return Int Attachment ID
1071 */
1072 function wdk_insert_attachment_from_url($url, $parent_post_id = null) {
1073
1074 if( !class_exists( 'WP_Http' ) )
1075 include_once( ABSPATH . WPINC . '/class-http.php' );
1076
1077 $http = new WP_Http();
1078 $response = $http->request( $url );
1079 if( is_wp_error($response) || $response['response']['code'] != 200 ) {
1080 return false;
1081 }
1082
1083 $upload = wp_upload_bits( basename($url), null, $response['body'] );
1084 if( !empty( $upload['error'] ) ) {
1085 return false;
1086 }
1087
1088 $file_path = $upload['file'];
1089 $file_name = basename( $file_path );
1090 $file_type = wp_check_filetype( $file_name, null );
1091 $attachment_title = sanitize_file_name( pathinfo( $file_name, PATHINFO_FILENAME ) );
1092 $wp_upload_dir = wp_upload_dir();
1093
1094 $post_info = array(
1095 'guid' => $wp_upload_dir['url'] . '/' . $file_name,
1096 'post_mime_type' => $file_type['type'],
1097 'post_title' => $attachment_title,
1098 'post_content' => '',
1099 'post_status' => 'inherit',
1100 );
1101
1102 // Create the attachment
1103 $attach_id = wp_insert_attachment( $post_info, $file_path, $parent_post_id );
1104
1105 // Include image.php
1106 require_once( ABSPATH . 'wp-admin/includes/image.php' );
1107
1108 // Define attachment metadata
1109 $attach_data = wp_generate_attachment_metadata( $attach_id, $file_path );
1110
1111 // Assign metadata to attachment
1112 wp_update_attachment_metadata( $attach_id, $attach_data );
1113
1114 return $attach_id;
1115
1116 }
1117
1118 if(!function_exists('wdk_jsdateformat')) {
1119 function wdk_jsdateformat($format = '') {
1120 $date_specific = array(
1121 // Day
1122 'd' => 'dd',
1123 'D' => 'D',
1124 'j' => 'd',
1125 'l' => 'DD',
1126 'N' => '',
1127 'S' => '',
1128 'w' => '',
1129 'z' => 'o',
1130 // Week
1131 'W' => '',
1132 // Month
1133 'F' => 'MM',
1134 'm' => 'mm',
1135 'M' => 'M',
1136 'n' => 'm',
1137 't' => '',
1138 // Year
1139 'L' => '',
1140 'o' => '',
1141 'y' => 'y',
1142 'Y' => 'yy',
1143 // Time
1144 'a' => '',
1145 'A' => '',
1146 'B' => '',
1147 'g' => '',
1148 'G' => '',
1149 'h' => '',
1150 'H' => '',
1151 'i' => '',
1152 's' => '',
1153 'u' => ''
1154 );
1155 return str_replace(array_keys($date_specific), array_values($date_specific), $format);
1156 }
1157
1158 }
1159
1160 function wdk_current_url()
1161 {
1162 global $wp;
1163 return add_query_arg( $wp->query_vars, home_url( $wp->request ) );
1164 }
1165
1166 if(!function_exists('wdk_generate_profile_permalink')) {
1167 /**
1168 * Insert user id and return link on user profile page
1169 *
1170 * @return Int user ID
1171 */
1172
1173 function wdk_generate_profile_permalink($profile = array())
1174 {
1175
1176 $profile_slug = '';
1177 if(wmvc_show_data('user_login', $profile, false)){
1178 $profile_slug = wmvc_show_data('user_login', $profile);
1179 } else if(is_intval($profile)){
1180 $profile_data = get_userdata($profile);
1181 if(wmvc_show_data('user_login', $profile_data, false)){
1182 $profile_slug = wmvc_show_data('user_login', $profile_data);
1183 } else {
1184 $profile_slug = $profile;
1185 }
1186 }
1187
1188 $user_profile_page = get_option('wdk_membership_profile_preview_page');
1189 if(!$user_profile_page) {
1190 return '#';
1191 }
1192 // for polylang to detect translated page version
1193 if(function_exists('pll_get_post'))
1194 $user_profile_page = pll_get_post($user_profile_page);
1195
1196 return wdk_url_suffix(get_permalink($user_profile_page), 'slug='. $profile_slug);
1197 }
1198 }
1199
1200 if(!function_exists('wdk_get_profile_page_id')) {
1201 /**
1202 * Insert user id and return link on user profile page
1203 *
1204 * @return Int user ID
1205 */
1206
1207 function wdk_get_profile_page_id()
1208 {
1209 global $wp_query;
1210 if(get_option('wdk_membership_profile_preview_page') && isset($wp_query->post) && get_option('wdk_membership_profile_preview_page') == $wp_query->post->ID) {
1211 if (wmvc_show_data('slug', $_GET, false)) {
1212 if(is_intval(wmvc_show_data('slug', $_GET))) {
1213 return wmvc_show_data('slug', $_GET);
1214 } else {
1215 $profile = get_user_by('login',wmvc_show_data('slug', $_GET));
1216 return (wmvc_show_data('ID', $profile, false)) ? wmvc_show_data('ID', $profile, false) : false;
1217 }
1218 }
1219 }
1220
1221 return false;
1222 }
1223 }
1224
1225 if(!function_exists('wdk_get_date')) {
1226 /**
1227 * Return date in format
1228 *
1229 */
1230 function wdk_get_date($datetime = NULL, $time = TRUE, $default='timestamp')
1231 {
1232 if(is_null($datetime))
1233 {
1234 if($default == 'timestamp')
1235 {
1236 $datetime = current_time('timestamp');
1237 }
1238 else
1239 {
1240 return $default;
1241 }
1242 }
1243 else if(!is_numeric($datetime))
1244 {
1245 $datetime = strtotime($datetime);
1246 }
1247
1248 $date_format = get_option('date_format');
1249 $time_format = ($time) ? get_option('time_format') : '';
1250 $date = date_i18n("{$date_format} {$time_format}", $datetime);
1251 return $date;
1252 }
1253 }
1254
1255 if(!function_exists('wdk_login_url')) {
1256 /**
1257 * Return date in format
1258 * @param string|url redirect url
1259 * @param string custom_message, showed on login form like alert, visible only on wdk login form
1260 */
1261 function wdk_login_url($url_redirect = NULL, $custom_message = '')
1262 {
1263 $url = wp_login_url($url_redirect);
1264
1265 if(get_option('wdk_membership_login_page')){
1266 $url = wdk_url_suffix(get_permalink(get_option('wdk_membership_login_page')), 'redirect_to='. urlencode($url_redirect).'&custom_message='. urlencode($custom_message));
1267 }
1268
1269 return $url;
1270 }
1271 }
1272
1273 if(!function_exists('wdk_mail')) {
1274 /**
1275 * Return date in format
1276 * @param string|url redirect url
1277 */
1278 function wdk_mail( $email_to, $subject = '', $data = array(), $layout = 'default', $email_from = '', $attachments = array(), $reply_to = '')
1279 {
1280 $WMVC = &wdk_get_instance();
1281
1282 $ret = false;
1283 if(empty($email_from))
1284 $email_from = get_bloginfo('admin_email');
1285
1286 if(empty($subject))
1287 $subject = __("New message", "wpdirectorykit");
1288
1289 $headers = array('Content-Type: text/html; charset=UTF-8');
1290 $headers[] = 'From: '.$email_from;
1291
1292 if(!empty($reply_to)) {
1293 $headers[] = 'Reply-To: '.$reply_to;
1294 } else {
1295 $headers[] = 'Reply-To: '.$email_from;
1296 }
1297
1298 $data['subject'] = $subject;
1299
1300 $message = $WMVC->view('email/'.$layout, $data, FALSE);
1301 $ret = wp_mail( $email_to, $subject, $message, $headers, $attachments );
1302
1303 return $ret;
1304 }
1305 }
1306
1307 if(!function_exists('wdk_show_data')) {
1308 function wdk_show_data($field_name, &$db_value = NULL, $default = '', $xss_clean = TRUE, $skip_post = FALSE)
1309 {
1310 if(!$skip_post && isset($_POST[$field_name]))
1311 {
1312 if($xss_clean === FALSE)
1313 return stripslashes($_POST[$field_name]);
1314
1315 return wmvc_xss_clean(stripslashes($_POST[$field_name]));
1316 }
1317
1318
1319 if(is_array($db_value))
1320 {
1321 if(isset($db_value[$field_name]))
1322 {
1323 if($xss_clean === FALSE)
1324 return $db_value[$field_name];
1325
1326 return wmvc_xss_clean($db_value[$field_name]);
1327 }
1328 else
1329 {
1330 return $default;
1331 }
1332 }
1333
1334 if(is_object($db_value))
1335 {
1336 if(isset($db_value->$field_name))
1337 {
1338 if($xss_clean === FALSE)
1339 return $db_value->$field_name;
1340
1341 return wmvc_xss_clean($db_value->$field_name);
1342 }
1343 else
1344 {
1345 return $default;
1346 }
1347 }
1348
1349 if(!empty($db_value))
1350 {
1351 if($xss_clean === FALSE)
1352 return $db_value;
1353
1354 return wmvc_xss_clean($db_value);
1355 }
1356
1357 if($xss_clean === FALSE)
1358 return $default;
1359
1360 return wmvc_xss_clean($default);
1361 }
1362 }
1363
1364
1365 if (!function_exists('wdk_input_checked'))
1366 {
1367 function wdk_input_checked($field_id, $db_data, $value = 1)
1368 {
1369
1370 if(wmvc_show_data($field_id, $db_data, false) && wmvc_show_data($field_id, $db_data) == $value)
1371 {
1372 return 'checked';
1373 }
1374
1375 return '';
1376 }
1377 }
1378
1379 function wdk_upload_file($field_name, $file_id)
1380 {
1381 static $media_element_counter = 0;
1382
1383 $media_element_counter++;
1384
1385 $img_field = $field_name.'_'.$media_element_counter;
1386
1387 wp_register_script( 'wpmediaelement_file', WPDIRECTORYKIT_URL . 'admin/js/jquery.wpmediaelement_file.js', array( 'jquery' ), false, false );
1388 wp_enqueue_script( 'wpmediaelement_file' );
1389 wp_enqueue_media();
1390
1391 ?>
1392 <div id="<?php echo esc_attr($field_name); ?>meta-box-id" class="postbox-upload">
1393 <?php
1394 // Get WordPress' media upload URL
1395 $upload_link = '#';
1396
1397 // Get the file src
1398
1399 // Get the file src
1400 $your_file_src = get_attached_file(intval( $file_id));
1401
1402 // For convenience, see if the array is valid
1403 $you_have_file = false;
1404 if($your_file_src)
1405 $you_have_file = basename($your_file_src);
1406
1407 ?>
1408
1409 <!-- Your file container, which can be manipulated with js -->
1410 <div class="custom-img-container">
1411 <?php if ( $you_have_file ) : ?>
1412 <?php echo esc_html($you_have_file); ?>
1413 <?php endif; ?>
1414 </div>
1415
1416 <?php //if(sw_user_in_role('administrator')): ?>
1417 <!-- Your add & remove file links -->
1418 <p class="hide-if-no-js">
1419 <a class="upload-custom-img <?php if ( $you_have_file ) { echo 'hidden'; } ?>"
1420 href="<?php echo esc_url($upload_link) ?>">
1421 <?php echo esc_html__('Select file','wmvc_win') ?>
1422 </a>
1423 <a class="delete-custom-img <?php if ( ! $you_have_file ) { echo 'hidden'; } ?>"
1424 href="#">
1425 <?php echo esc_html__('Remove file','wmvc_win') ?>
1426 </a>
1427 </p>
1428 <?php //endif; ?>
1429
1430 <!-- A hidden input to set and post the chosen file id -->
1431 <input class="logo_file_id" type="hidden" id="<?php echo esc_html($field_name); ?>" name="<?php echo esc_html($field_name); ?>" value="<?php echo esc_html($file_id); ?>" />
1432 </div>
1433
1434 <?php
1435 $custom_js ='';
1436 $custom_js .=" jQuery(function($) {
1437 if( typeof jQuery.fn.wpMediaElementFile == 'function')
1438 $('#".esc_js($field_name)."meta-box-id.postbox-upload').wpMediaElementFile();
1439 });";
1440
1441 echo "<script>".$custom_js."</script>";
1442
1443 ?>
1444
1445 <?php
1446 }
1447
1448
1449 if(!function_exists('wdk_access_check')) {
1450 /**
1451 * Generate listing card html
1452 *
1453 * @param array $listing The listing data.
1454 * @param array $settings The settings.
1455 * @param bool $json_output Encode for json, default false
1456 * @param string $html Html for sprintf(), where
1457 * %1$s - content
1458 * @return string
1459 */
1460 function wdk_access_check($model_name, $item_id, $user_id=NULL, $method='edit') {
1461
1462 if(!empty($item_id) && !is_numeric($item_id))
1463 exit('Issue with ID');
1464
1465 if (in_array($model_name, array('reviews_m','reviews_type_m','reviews_option_m','reviews_data_m'))) {
1466 global $Winter_MVC_wdk_reviews;
1467 $WMVC = $Winter_MVC_wdk_reviews;
1468 }elseif(in_array($model_name,array('package_m','payment_m'))) {
1469 global $Winter_MVC_wdk_payments;
1470 $WMVC = $Winter_MVC_wdk_payments;
1471 }elseif(in_array($model_name,array('favorite_m','favoritecategory_m'))) {
1472 global $Winter_MVC_wdk_favorites;
1473 $WMVC = $Winter_MVC_wdk_favorites;
1474 }elseif(in_array($model_name,array('currency_m'))) {
1475 global $Winter_MVC_wdk_currency;
1476 $WMVC = $Winter_MVC_wdk_currency;
1477 }elseif(in_array($model_name,array('calendar_m','price_m','reservation_m'))) {
1478 global $Winter_MVC_wdk_bookings;
1479 $WMVC = $Winter_MVC_wdk_bookings;
1480 }elseif(in_array($model_name,array('save_search_m'))) {
1481 global $Winter_MVC_wdk_save_search;
1482 $WMVC = $Winter_MVC_wdk_save_search;
1483 } else {
1484 $WMVC = &wdk_get_instance();
1485 }
1486
1487 if(empty($user_id))
1488 $user_id = get_current_user_id();
1489
1490 if(wmvc_user_in_role('administrator')) {
1491 return true;
1492 }
1493
1494 if(substr($model_name,-2,2) == '_m')
1495 {
1496 // its model
1497 $WMVC->model($model_name);
1498
1499 if(empty($item_id) || $WMVC->$model_name->is_related($item_id, $user_id, $method))
1500 {
1501 // User is related
1502 return true;
1503 }
1504 else
1505 {
1506 //echo $CI->db->last_query();
1507 exit('Access denied ROLES RELATED');
1508 }
1509 }
1510 else
1511 {
1512 return true;
1513 }
1514
1515 exit('Access denied ROLES');
1516
1517 }
1518 }
1519
1520 if ( ! function_exists('wdk_recaptcha_field'))
1521 {
1522 function wdk_recaptcha_field($is_compact=false, $style="", $load_script=true)
1523 {
1524 static $counter = 0;
1525 static $recaptcha_array = array();
1526
1527 if(get_option('wdk_recaptcha_site_key') !== FALSE && get_option('wdk_recaptcha_secret_key') !== NULL)
1528 {
1529 if($load_script && $counter===0)
1530 {
1531 echo "<script src='https://www.google.com/recaptcha/api.js?onload=CaptchaCallback&amp;render=explicit'></script>";
1532 }
1533 $counter++;
1534
1535 $compact_tag='';
1536 $size_tag='';
1537 if($is_compact)
1538 {
1539 $compact_tag='data-size="compact"';
1540 $size_tag='compact';
1541 }
1542
1543 $recaptcha_array[$counter] = array('size'=>$size_tag);
1544
1545 echo '<div id="recaptcha_called_'.$counter.'" class="g-recaptcha" style="'.$style.'" '.$compact_tag.' data-sitekey="'.get_option('wdk_recaptcha_site_key').'"></div>';
1546 ?>
1547
1548 <script>
1549 <?php if($counter===1)echo 'var ';?>CaptchaCallback = function(){
1550 <?php for($j=1;$j<=$counter;$j++): ?>
1551 grecaptcha.render(document.getElementById('recaptcha_called_<?php echo $j;?>'), {'size' : '<?php echo $recaptcha_array[$j]['size']; ?>', 'sitekey' : '<?php echo get_option('wdk_recaptcha_site_key'); ?>'});
1552 <?php endfor; ?>
1553 };
1554 </script>
1555
1556 <?php
1557 }
1558 }
1559 }
1560
1561 function is_wdk_valid_recaptcha()
1562 {
1563 if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
1564 //your site secret key
1565 if(wdk_valid_recaptcha_curl($_POST['g-recaptcha-response']))
1566 {
1567 return TRUE;
1568 }
1569 }
1570 return FALSE;
1571 }
1572
1573 function wdk_valid_recaptcha_curl($g_recaptcha_response='') {
1574 $url = 'https://www.google.com/recaptcha/api/siteverify';
1575 $args = array(
1576 'timeout' => 200,
1577 'blocking' => true,
1578 'headers' => array(),
1579 'body' => array(
1580 'secret' => get_option('wdk_recaptcha_secret_key'),
1581 'response' => $g_recaptcha_response,
1582 'remoteip' => sanitize_textarea_field($_SERVER['REMOTE_ADDR'])
1583 ),
1584 'cookies' => array()
1585 );
1586 $response = wp_remote_post( $url, $args );
1587
1588 if ( is_wp_error( $response ) ) {
1589 /*$error_message = $response->get_error_message();*/
1590 return true;
1591 } else {
1592 $response = json_decode($response['body']);
1593 return $response->success;
1594 }
1595 }
1596
1597 if(!function_exists('wdk_get_option')) {
1598 /**
1599 * Cached and get wp options
1600 *
1601 * @param string option Option key
1602 * @return string alt or title
1603 */
1604
1605 function wdk_get_option($option_key = '') {
1606 return get_option($option_key);
1607 if(isset($options[$option_key])) {
1608 return $options[$option_key];
1609 }
1610
1611 $options[$option_key] = get_option($option_key);
1612
1613 return $options[$option_key];
1614 }
1615 }
1616
1617 if(!function_exists('wdk_server_current_url')) {
1618 /**
1619 * Get current url basic on $_SERVER, exists function wdk_current_url(), on some urls have issue on listing preview page
1620 *
1621 * @return string url
1622 */
1623
1624 function wdk_server_current_url()
1625 {
1626 return (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
1627 }
1628 }
1629
1630
1631 if(!function_exists('wdk_filter_date_decimal')) {
1632 /**
1633 * Filter 00:00:00 from date
1634 *
1635 * @param string
1636 * @return string date without 00:00:00
1637 */
1638
1639 function wdk_filter_date_decimal($data)
1640 {
1641 return str_replace(' 00:00:00','', $data);
1642 }
1643 }
1644
1645 function wdk_get_post()
1646 {
1647 $post = array();
1648
1649 foreach($_POST as $key => $val)
1650 {
1651 if(is_array($val))
1652 {
1653 return wdk_clean($_POST);
1654 }
1655 else
1656 {
1657 $post[sanitize_text_field($key)] = wp_kses_post($val);
1658 }
1659 }
1660
1661 return $post;
1662 }
1663
1664
1665 function wdk_get_get()
1666 {
1667 $log_array = array();
1668
1669 foreach($_GET as $key => $val)
1670 {
1671 if(is_array($val))
1672 {
1673 return wdk_clean($_POST);
1674 }
1675 else
1676 {
1677 $log_array[sanitize_text_field($key)] = wp_kses_post($val);
1678 }
1679 }
1680
1681 return $log_array;
1682 }
1683
1684 function wdk_clean($array)
1685 {
1686 $arr_cleaned = array();
1687 foreach($array as $key=>$val)
1688 {
1689 if(is_array($val))
1690 {
1691 $arr_cleaned[sanitize_text_field($key)] = wdk_clean($val);
1692 }
1693 else
1694 {
1695 $arr_cleaned[sanitize_text_field($key)] = wp_kses_post($val);
1696 }
1697
1698 }
1699
1700 //dump($arr_cleaned);
1701
1702 return $arr_cleaned;
1703 }
1704
1705 ?>