-
Notifications
You must be signed in to change notification settings - Fork 21
/
class-geodir-ajax.php
1619 lines (1347 loc) · 42.5 KB
/
class-geodir-ajax.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* GeoDirectory GeoDir_AJAX.
*
* AJAX Event Handler.
*
* @class GeoDir_AJAX
* @package GeoDirectory/Classes
* @category Class
* @author AyeCode
*/
class GeoDir_AJAX {
/**
* Hook in ajax handlers.
*/
public static function init() {
//add_action( 'init', array( __CLASS__, 'define_ajax' ), 0 );
//add_action( 'template_redirect', array( __CLASS__, 'do_gd_ajax' ), 0 );
self::add_ajax_events();
}
/**
* Hook in methods - uses WordPress ajax handlers (admin-ajax).
*/
public static function add_ajax_events() {
// geodirectory_EVENT => nopriv
$ajax_events = array(
'get_custom_field_form' => false,
'get_custom_field_sorting_form' => false,
'auto_save_custom_field' => false,
'save_custom_field' => false,
'save_custom_sort_field' => false,
'delete_custom_field' => false,
'delete_custom_sort_field' => false,
'order_custom_fields' => false,
'order_custom_sort_fields' => false,
'insert_dummy_data' => false,
'delete_dummy_data' => false,
'wizard_insert_widgets_top' => false,
'wizard_insert_widgets' => false,
'wizard_setup_menu' => false,
'post_attachment_upload' => true,
'get_category_select' => false,
'user_add_fav' => false,
'save_post' => true,
'auto_save_post' => true,
'delete_revision' => true,
'user_delete_post' => false,
'import_export' => false,
'save_api_key' => false,
'bestof' => true,
'cpt_categories' => true,
'json_search_users' => false,
'ninja_forms' => true,
'get_tabs_form' => false,
'save_tab_item' => false,
'save_tabs_order' => false,
'delete_tab' => false,
'manual_map' => true,
'widget_listings' => true,
'recently_viewed_listings' => true,
'embed_widget' => true,
'embed_script' => true,
'timezone_data' => true,
'get_sort_options' => false,
'tool_regenerate_thumbnails' => true,
'regenerate_thumbnails' => true,
'post_author_action' => false,
'report_post_form' => true,
'submit_report_post' => true,
'new_wp_template' => true,
'business_hours_post_meta' => true,
);
foreach ( $ajax_events as $ajax_event => $nopriv ) {
add_action( 'wp_ajax_geodir_' . $ajax_event, array( __CLASS__, $ajax_event ) );
if ( $nopriv ) {
add_action( 'wp_ajax_nopriv_geodir_' . $ajax_event, array( __CLASS__, $ajax_event ) );
}
// GeoDir AJAX can be used for frontend ajax requests.
add_action( 'geodir_ajax_geodir_' . $ajax_event, array( __CLASS__, $ajax_event ) );
}
}
/**
* Get the CPT sort options for the listings widget.
*/
public static function get_sort_options(){
$post_type = !empty($_REQUEST['post_type']) ? sanitize_key( $_REQUEST['post_type'] ) : 'gd_place';
$sort_options = geodir_sort_by_options( $post_type );
echo json_encode($sort_options);
exit;
}
public static function embed_script(){
$html = GeoDir_External_Embed::get_embed_script();
if($html ){
echo $html;
}
exit;
}
public static function embed_widget(){
$html = GeoDir_External_Embed::get_embed_widget();
if($html ){
echo $html;
}
exit;
}
/**
* Set GD AJAX constant and headers.
*/
public static function define_ajax() {
if ( ! empty( $_GET['gd-ajax'] ) ) {
if(!defined('DOING_AJAX'))define( 'DOING_AJAX', true );
if(!defined('GD_DOING_AJAX'))define( 'GD_DOING_AJAX', true );
if ( ! WP_DEBUG || ( WP_DEBUG && ! WP_DEBUG_DISPLAY ) ) {
/** @scrutinizer ignore-unhandled */ @ini_set( 'display_errors', 0 ); // Turn off display_errors during AJAX events to prevent malformed JSON.
}
$GLOBALS['wpdb']->hide_errors();
}
}
/**
* Check for WC Ajax request and fire action.
*/
public static function do_gd_ajax() {
global $wp_query;
if ( ! empty( $_GET['gd-ajax'] ) ) {
$wp_query->set( 'gd-ajax', sanitize_text_field( wp_unslash( $_GET['gd-ajax'] ) ) );
}
$action = $wp_query->get( 'gd-ajax' );
if ( $action ) {
self::gd_ajax_headers();
$action = sanitize_text_field( $action );
do_action( 'gd_ajax_' . $action );
wp_die();
}
}
/**
* Send headers for GD Ajax Requests.
*
* @since 2.0.0.58
*/
private static function gd_ajax_headers() {
send_origin_headers();
/** @scrutinizer ignore-unhandled */ @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); // @codingStandardsIgnoreLine
/** @scrutinizer ignore-unhandled */ @header( 'X-Robots-Tag: noindex' ); // @codingStandardsIgnoreLine
send_nosniff_header();
nocache_headers();
status_header( 200 );
}
/**
* Manual map
*
* @since 2.0.0
*/
public static function manual_map() {
global $geodirectory, $mapzoom, $geodir_manual_map,$aui_bs5;
$prefix = isset( $_POST['trigger'] ) ? sanitize_title( geodir_clean( $_POST['trigger'] ) ) : 'geodir_manual_location_';
$prefix = esc_attr( $prefix );
$map_title = __( "Select Your Location", 'geodirectory' );
$location = $geodirectory->location->get_default_location();
$country = isset( $location->country ) ? $location->country : '';
$region = isset( $location->region ) ? $location->region : '';
$city = isset( $location->city ) ? $location->city : '';
$lat = isset( $location->latitude ) ? $location->latitude : '';
$lng = isset( $location->longitude ) ? $location->longitude : '';
$mapzoom = 8;
$design_style = geodir_design_style();
$geodir_manual_map = true;
// Try and center the map as close to the user as possible.
if ( $ip = geodir_get_ip() ) {
$geo = geodir_geo_by_ip( $ip );
if ( ! empty( $geo ) && ! empty( $geo['latitude'] ) && ! empty( $geo['longitude'] ) ) {
$lat = $geo['latitude'];
$lng = $geo['longitude'];
}
}
add_filter( 'geodir_add_listing_map_restrict', '__return_false' );
if ( $design_style ) {
echo aui()->alert(array(
'type'=> 'info',
'content'=> __("Auto location detection failed, please manually set your location below by dragging the map / marker.","geodirectory")
)
);
$template = $design_style . '/map/map-add-listing.php';
$tmpl_args = array(
'prefix' => $prefix,
'map_title' => $map_title,
'country' => $country,
'region' => $region,
'city' => $city,
'lat' => $lat,
'lng' => $lng,
'design_style' => $design_style
);
echo geodir_get_template_html( $template, $tmpl_args );
} else {
echo "<style>.lity-show #" . esc_attr( $prefix ) . "set_address_button,.lity-show .TopLeft,.lity-show .TopRight,.lity-show .BottomRight,.lity-show .BottomLeft{display:none}.lity-show .geodir_map_container{margin-top:0 !important}</style>";
include_once( GEODIRECTORY_PLUGIN_DIR . 'templates/map.php' );
}
?>
<input type="hidden" id="<?php echo esc_attr( $prefix ) . 'latitude'; ?>">
<input type="hidden" id="<?php echo esc_attr( $prefix ) . 'longitude'; ?>">
<?php
if( $design_style ) {
?>
<div class="text-right text-end">
<button type="button" class="btn btn-link" data<?php echo $aui_bs5 ? '-bs' : ''; ?>-dismiss="modal"><?php _e("Cancel","geodirectory");?></button>
<button class="btn btn-primary"
onclick="if(jQuery('#<?php echo esc_attr( $prefix ) . 'latitude'; ?>').val()==''){alert('<?php _e( 'Please drag the marker or the map to set the position.', 'geodirectory' ); ?>');}else{jQuery(window).triggerHandler('<?php echo esc_attr( $prefix ); ?>', [jQuery('#<?php echo esc_attr( $prefix ) . 'latitude'; ?>').val(), jQuery('#<?php echo esc_attr( $prefix ) . 'longitude'; ?>').val()]);}"><?php _e( 'Set my location', 'geodirectory' ); ?></button>
</div><?php
}else{
?>
<button style="float: right;margin: 10px 0 0 0;"
onclick="if(jQuery('#<?php echo esc_attr( $prefix ) . 'latitude'; ?>').val()==''){alert('<?php _e( 'Please drag the marker or the map to set the position.', 'geodirectory' ); ?>');}else{jQuery(window).triggerHandler('<?php echo esc_attr( $prefix ); ?>', [jQuery('#<?php echo esc_attr( $prefix ) . 'latitude'; ?>').val(), jQuery('#<?php echo esc_attr( $prefix ) . 'longitude'; ?>').val()]);}"><?php _e( 'Set my location', 'geodirectory' ); ?></button>
<?php
}
wp_die();
}
/**
* Delete Tab input form
*
* @since 2.0.0
*/
public static function delete_tab(){
// security
check_ajax_referer( 'gd_new_field_nonce', 'security' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( -1 );
}
$tab_id = isset($_POST['tab_id']) && $_POST['tab_id'] ? absint($_POST['tab_id']) : '';
if(!$tab_id){
wp_send_json_error( __("No tab_id provided.","geodirectory") );
}else{
$post_type = isset($_POST['post_type']) && $_POST['post_type'] ? esc_attr($_POST['post_type']) : '';
$result = GeoDir_Settings_Cpt_Tabs::delete_tab($tab_id,$post_type);
if(is_wp_error( $result ) ){
wp_send_json_error( $result->get_error_message() );
}else{
wp_send_json_success();
}
}
wp_die();
}
public static function recently_viewed_listings(){
global $gd_post, $post,$gd_layout_class, $geodir_is_widget_listing;
ob_start();
$list_per_page = !empty($_REQUEST['list_per_page']) ? absint($_REQUEST['list_per_page']) : '';
$post_type = !empty($_REQUEST['post_type']) ? sanitize_key( $_REQUEST['post_type'] ) : '';
$all_postypes = geodir_get_posttypes();
if (!in_array($post_type, $all_postypes)){
$post_type = 'gd_place';
}
$layout = empty( $_REQUEST['layout'] ) ? 'gridview_onehalf' : apply_filters( 'widget_layout', geodir_convert_listing_view_class($_REQUEST['layout']) );
$post_ids = !empty($_REQUEST['viewed_post_id']) ? json_decode(stripslashes($_REQUEST['viewed_post_id']), true) : '';
// elementor pro
$skin_id = ! empty( $_REQUEST['skin_id'] ) ? absint( $_REQUEST['skin_id'] ) : '';
$skin_column_gap = ! empty( $_REQUEST['skin_column_gap'] ) ? absint( $_REQUEST['skin_column_gap'] ) : '';
$skin_row_gap = ! empty( $_REQUEST['skin_row_gap'] ) ? absint( $_REQUEST['skin_row_gap'] ) : '';
// AUI
$column_gap = ! empty( $_REQUEST['column_gap'] ) ? absint( $_REQUEST['column_gap'] ) : '';
$row_gap = ! empty( $_REQUEST['row_gap'] ) ? absint( $_REQUEST['row_gap'] ) : '';
$card_border = ! empty( $_REQUEST['card_border'] ) ? sanitize_html_class( $_REQUEST['card_border'] ) : '';
$card_shadow = ! empty( $_REQUEST['card_shadow'] ) ? sanitize_html_class( $_REQUEST['card_shadow'] ) : '';
$listings_ids = array();
if( !empty( $post_type ) ) {
if( !empty( $post_ids ) && $post_ids !='' && !empty($post_ids[$post_type]) ) {
$listings_ids = $post_ids[$post_type];
}
}
$listings_ids = !empty( $listings_ids ) ? array_reverse($listings_ids) : array();
$listings_ids = !empty($listings_ids) ? array_slice($listings_ids, 0, $list_per_page) : array();
$widget_listings = array();
if(!empty($listings_ids)){
foreach($listings_ids as $post_id){
$post_id = absint($post_id);
$listing = geodir_get_post_info($post_id);
if(!empty($listing) && isset($listing->ID)){
$widget_listings[] = $listing;
}
}
}
if(!empty($widget_listings)){
// elementor
$skin_active = false;
$columns = 1;
if(defined( 'ELEMENTOR_PRO_VERSION' ) && $skin_id){
if(get_post_status ( $skin_id )=='publish'){
$skin_active = true;
}
if($skin_active){
$columns = isset($layout) ? absint($layout) : 1;
if($columns == '0'){$columns = 6;}// we have no 6 row option to lets use list view
}
}
$gd_layout_class = geodir_convert_listing_view_class( $layout );
// card border class
$card_border_class = '';
if(!empty($card_border)){
if($card_border=='none'){
$card_border_class = 'border-0';
}else{
$card_border_class = 'border-'.sanitize_html_class($card_border);
}
}
// card shadow
$card_shadow_class = '';
if(!empty($card_shadow)){
if($card_shadow=='small'){
$card_shadow_class = 'shadow-sm';
}elseif($card_shadow=='medium'){
$card_shadow_class = 'shadow';
}elseif($card_shadow=='large'){
$card_shadow_class = 'shadow-lg';
}
}
if($skin_active){
$column_gap = $skin_column_gap;
$row_gap = $skin_row_gap;
geodir_get_template( 'elementor/content-widget-listing.php', array( 'widget_listings' => $widget_listings,'skin_id' => $skin_id,'columns'=>$columns,'column_gap'=> $column_gap,'row_gap'=>$row_gap ) );
}else{
$design_style = geodir_design_style();
$template = $design_style ? $design_style."/content-widget-listing.php" : "content-widget-listing.php";
echo geodir_get_template_html( $template, array(
'widget_listings' => $widget_listings,
'column_gap_class' => $column_gap ? 'mb-'.absint($column_gap) : 'mb-4',
'row_gap_class' => $row_gap ? 'px-'.absint($row_gap) : '',
'card_border_class' => $card_border_class,
'card_shadow_class' => $card_shadow_class,
) );
}
}else{
echo aui()->alert(array(
'type'=> 'info',
'content'=> __("Your recently viewed listings will show here.","geodirectory")
)
);
}
echo ob_get_clean();
wp_die();
}
/**
* Save tabs order input form.
*
* @since 2.0.0
*/
public static function save_tabs_order(){
// security
check_ajax_referer( 'gd_new_field_nonce', 'security' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( -1 );
}
$tabs = isset($_POST['tabs']) && $_POST['tabs'] ? $_POST['tabs'] : '';
if(!$tabs){
wp_send_json_error( __("No tabs provided.","geodirectory") );
}else{
$result = GeoDir_Settings_Cpt_Tabs::set_tabs_orders($tabs);
if(is_wp_error( $result ) ){
wp_send_json_error( $result->get_error_message() );
}else{
wp_send_json_success();
}
}
wp_die();
}
/**
* Save tab item.
*
* @since 2.0.0
*/
public static function save_tab_item(){
// security
check_ajax_referer( 'gd_new_field_nonce', 'security' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( -1 );
}
$tab_type = isset($_POST['tab_type']) && $_POST['tab_type'] ? esc_attr($_POST['tab_type']) : '';
if(!$tab_type){
wp_send_json_error( __("No tab_type provided.","geodirectory") );
}else{
$result = GeoDir_Settings_Cpt_Tabs::save_tab_item($_POST);
if(is_wp_error( $result ) ){
wp_send_json_error( $result->get_error_message() );
}else{
wp_send_json_success($result);
}
}
wp_die();
}
/**
* Get tabs input form.
*
* @since 2.0.0
*/
public static function get_tabs_form(){
// security
check_ajax_referer( 'gd_new_field_nonce', 'security' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( -1 );
}
$tab_type = isset($_POST['tab_type']) && $_POST['tab_type'] ? esc_attr($_POST['tab_type']) : '';
if(!$tab_type){
wp_send_json_error( __("No tab_type provided.","geodirectory") );
}else{
$result = GeoDir_Settings_Cpt_Tabs::get_tab_item($_POST);
if(is_wp_error( $result ) ){
wp_send_json_error( $result->get_error_message() );
}else{
wp_send_json_success($result);
}
}
wp_die();
}
/**
* Get Ninja forms html.
*
* @since 2.0.0
*
* @return string
*/
public static function ninja_forms(){
check_ajax_referer( 'geodir_basic_nonce', 'security' );
$post_id = isset($_REQUEST['p']) ? absint($_REQUEST['p']) : url_to_postid( wp_get_referer() );
$form_id = isset($_REQUEST['extra']) ? absint($_REQUEST['extra']) : '';
if ( ! $post_id || ! $form_id ) {
return 'no post id';
}
global $post;
$the_post = get_post( $post_id );
$post = $the_post;
// fake the post_id for ninja forms
add_filter( 'url_to_postid', function ( $url ) {
global $post;
return add_query_arg( 'p', $post->ID, $url );
});
/*
* We only need the form and its basic CSS/JS so we hack away all lots of others stuff in a naughty way.
*/
remove_all_actions( 'wp_footer');
add_action( 'wp_footer', 'wp_print_footer_scripts', 20 );
add_action('wp_print_styles', function () {global $wp_styles; $wp_styles->queue = array('font-awesome');}, 1000);
add_action('wp_print_scripts', function () {global $wp_scripts; $wp_scripts->queue = array('jquery');}, 1000);
echo '<!DOCTYPE html><html lang="en-US"><head>';
wp_head();
echo "<style>body { background: #fff;padding: 20px 50px;}</style>";
echo '</head><body>';
// Set post back.
if ( $post->ID != $the_post->ID ) {
$post = $the_post;
}
// allow other plugins to override the call
$override_html = apply_filters('geodir_ajax_ninja_forms_override','',$post_id,$form_id);
if(!empty($override_html)){
echo $override_html;
}else{
echo do_shortcode( "[ninja_form id=$form_id]" );
}
wp_footer();
echo '</body></html>';
wp_die();
}
/**
* Setup wizard menu.
*
* @since 2.0.0
*/
public static function wizard_setup_menu(){
// security
check_ajax_referer( 'geodir-wizard-setup-menu', 'security' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( -1 );
}
$menu_id = isset($_REQUEST['menu_id']) ? sanitize_title_with_dashes($_REQUEST['menu_id']) : '';
$menu_location = isset($_REQUEST['menu_location']) ? sanitize_title_with_dashes($_REQUEST['menu_location']) : '';
$result = GeoDir_Admin_Dummy_Data::setup_menu( $menu_id, $menu_location);
if(is_wp_error( $result ) ){
wp_send_json_error( $result->get_error_message() );
}else{
wp_send_json_success(geodir_notification( array( 'info' => $result) ));
}
wp_die();
}
/**
* Adds widgets to sidebar during setup wizard.
*
* @since 2.0.0
*/
public static function wizard_insert_widgets_top(){
// security
check_ajax_referer( 'geodir-wizard-widgets-top', 'security' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( -1 );
}
$sidebar_id = isset($_REQUEST['sidebar_id']) ? sanitize_title_with_dashes($_REQUEST['sidebar_id']) : '';
$result = GeoDir_Admin_Dummy_Data::insert_widgets( $sidebar_id, 'top');
if(is_wp_error( $result ) ){
wp_send_json_error( $result->get_error_message() );
}else{
wp_send_json_success(geodir_notification( array( 'info' => $result) ));
}
wp_die();
}
/**
* Adds widgets to sidebar during setup wizard.
*
* @since 2.0.0
*/
public static function wizard_insert_widgets(){
// security
check_ajax_referer( 'geodir-wizard-widgets', 'security' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( -1 );
}
$sidebar_id = isset($_REQUEST['sidebar_id']) ? sanitize_title_with_dashes($_REQUEST['sidebar_id']) : '';
$result = GeoDir_Admin_Dummy_Data::insert_widgets( $sidebar_id);
if(is_wp_error( $result ) ){
wp_send_json_error( $result->get_error_message() );
}else{
wp_send_json_success(geodir_notification( array( 'info' => $result) ));
}
wp_die();
}
/**
* Best of listings widget ajax.
*
* @since 2.0.0
*/
public static function bestof(){
// security
check_ajax_referer( 'geodir_basic_nonce', 'security' );
GeoDir_Widget_Best_Of::best_of(array(), $_REQUEST);
wp_die();
}
/**
* GD Categories widget ajax.
*
* @since 2.0.0
*/
public static function cpt_categories(){
// security
check_ajax_referer( 'geodir_basic_nonce', 'security' );
GeoDir_Widget_Categories::get_categories($_POST);
wp_die();
}
/**
* User delete post.
*
* @since 2.0.0
*/
public static function user_delete_post(){
// security
check_ajax_referer( 'geodir_basic_nonce', 'security' );
$post_id = isset($_POST['post_id']) && $_POST['post_id'] ? absint($_POST['post_id']) : 0;
$data = array();
if(!$post_id){
$data['message'] = __( "No post_id provided.", "geodirectory" );
wp_send_json_error( $data );
}else{
$post_type = get_post_type( $post_id );
$result = GeoDir_User::delete_post( $post_id );
if(is_wp_error( $result ) ){
$data['message'] = $result->get_error_message();
wp_send_json_error( $data );
}else{
$data['message'] = __( 'You have successfully deleted the Listing.', 'geodirectory' );
$data['redirect_to'] = get_post_type_archive_link( $post_type );
wp_send_json_success( $data );
}
}
wp_die();
}
/**
* Import export.
*
* @since 2.0.0
*/
public static function import_export(){
// security
check_ajax_referer( 'geodir_import_export_nonce', '_nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( -1 );
}
$result = GeoDir_Admin_Import_Export::start_import_export();
if(is_wp_error( $result ) ){
wp_send_json_error( $result->get_error_message() );
}elseif(isset($result['error']) && !empty($result['error'])){
wp_send_json_error( $result['error'] );
}else{
wp_send_json($result);
}
wp_die();
}
/**
* Auto save post revisions and auto-drafts.
*
* @since 2.0.0
*/
public static function auto_save_post(){
// security
check_ajax_referer( 'geodir-save-post', 'security' );
$result = GeoDir_Post_Data::auto_save_post( $_REQUEST );
if(is_wp_error( $result ) ){
wp_send_json_error( $result->get_error_message() );
}else{
wp_send_json_success($result);
}
wp_die();
}
/**
* Delete post revisions and auto-drafts.
*
* @since 2.0.0
*
*/
public static function delete_revision(){
// security
check_ajax_referer( 'geodir-save-post', 'security' );
$result = GeoDir_Post_Data::delete_revision( $_REQUEST );
if(is_wp_error( $result ) ){
wp_send_json_error( $result->get_error_message() );
}else{
wp_send_json_success();
}
wp_die();
}
/**
* Save the post.
*
* @since 2.0.0
*/
public static function save_post(){
// security
check_ajax_referer( 'geodir-save-post', 'security' );
$result = GeoDir_Post_Data::ajax_save_post( $_REQUEST );
if(is_wp_error( $result ) ){
wp_send_json_error( $result->get_error_message() );
}else{
wp_send_json_success($result);
}
wp_die();
}
/**
* Add post to user favs.
*
* @since 2.0.0
*/
public static function user_add_fav() {
// security
check_ajax_referer( 'geodir_basic_nonce', 'security' );
$user_id = get_current_user_id();
$post_id = absint( $_REQUEST['pid'] );
$type_action = isset( $_REQUEST['type_action'] ) && $_REQUEST['type_action'] == 'add' ? 'add' : 'remove';
if ( $user_id && $post_id ) {
$data = array();
if ( $type_action == 'add' ) {
$result = GeoDir_User::add_fav( $post_id, $user_id );
if ( $result ) {
$data['action_text'] = apply_filters( 'geodir_favourite_text', __( 'Unfavorite', 'geodirectory' ) );
}
} else {
$result = GeoDir_User::remove_fav( $post_id, $user_id );
if ( $result ) {
$data['action_text'] = apply_filters( 'geodir_unfavourite_text', __( 'Favorite', 'geodirectory' ) );
}
}
if ( $result ){
wp_send_json_success( $data );
} else {
wp_send_json_error( __( 'Action failed.', 'geodirectory' ) );
}
} else {
wp_send_json_error( __( 'Action failed.', 'geodirectory' ) );
}
wp_die();
}
/**
* Get category select.
*
* @since 2.0.0
*/
public static function get_category_select(){
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( -1 );
}
$post_type = ! empty( $_REQUEST['post_type'] ) ? sanitize_text_field( $_REQUEST['post_type'] ) : '';
$selected = isset( $_REQUEST['selected'] ) ? sanitize_text_field( $_REQUEST['selected'] ) : '';
$tax = new GeoDir_Admin_Taxonomies();
$tax->get_category_select( $post_type, $selected, false, true );
wp_die();
}
/**
* Admin action to get a custom field sorting form.
*
* @since 2.0.0
*/
public static function get_custom_field_sorting_form(){
// security
check_ajax_referer( 'gd_new_field_nonce', 'security' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( -1 );
}
$cfs = new GeoDir_Settings_Cpt_Sorting();
$field = new stdClass();
$field->id = isset($_REQUEST['field_id']) ? sanitize_text_field($_REQUEST['field_id']) : '';
$field->post_type = isset($_REQUEST['listing_type']) ? sanitize_text_field($_REQUEST['listing_type']) : '';
$field->field_type = isset($_REQUEST['field_type']) ? sanitize_text_field($_REQUEST['field_type']) : '';
$field->field_type_key = isset($_REQUEST['field_type_key']) ? sanitize_text_field($_REQUEST['field_type_key']) : '';
$field->htmlvar_name = isset($_REQUEST['field_type_key']) ? sanitize_text_field($_REQUEST['field_type_key']) : '';
echo $cfs->output_custom_field_setting_item('',$field);
wp_die();
}
/**
* Post attachment upload.
*
* @since 2.0.0
*/
public static function post_attachment_upload(){
// security
check_ajax_referer( 'geodir_attachment_upload', '_ajax_nonce' );
GeoDir_Media::post_attachment_upload();
wp_die();
}
/**
* Admin action to insert dummy data.
*
* @since 2.0.0
*/
public static function insert_dummy_data(){
// security
check_ajax_referer( 'geodir_dummy_data', 'security' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( -1 );
}
// defind doing import
if( ! defined( 'GEODIR_DOING_IMPORT' ) ){
define( 'GEODIR_DOING_IMPORT', true );
}
//Suspend cache additions
wp_suspend_cache_addition(true);
$result = GeoDir_Admin_Dummy_Data::create_dummy_posts( $_REQUEST );
if(is_wp_error( $result ) ){
wp_send_json_error( $result->get_error_message() );
}else{
wp_send_json_success();
}
wp_die();
}
/**
* Admin action to delete dummy data.
*
* @since 2.0.0
*/
public static function delete_dummy_data(){
// Security
check_ajax_referer( 'geodir_dummy_data', 'security' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( -1 );
}
$post_type = ! empty( $_REQUEST['post_type'] ) ? sanitize_text_field( $_REQUEST['post_type'] ) : '';
if ( ! geodir_is_gd_post_type( $post_type ) ) {
wp_send_json_error( __( 'Invalid post type', 'geodirectory' ) );
}
$result = GeoDir_Admin_Dummy_Data::delete_dummy_posts( $post_type );
if ( is_wp_error( $result ) ) {
wp_send_json_error( $result->get_error_message() );
} else {
wp_send_json_success();
}
wp_die();
}
/**
* Admin action to get a custom field form.
*
* @since 2.0.0
*/
public static function get_custom_field_form(){
// security
check_ajax_referer( 'gd_new_field_nonce', 'security' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( -1 );
}
$cfs = new GeoDir_Settings_Cpt_Cf();
$field = new stdClass();
$field->id = isset($_REQUEST['field_id']) ? sanitize_text_field($_REQUEST['field_id']) : '';
$field->post_type = isset($_REQUEST['listing_type']) ? sanitize_text_field($_REQUEST['listing_type']) : '';
$field->field_type = isset($_REQUEST['field_type']) ? sanitize_text_field($_REQUEST['field_type']) : '';
$field->field_type_key = isset($_REQUEST['field_type_key']) ? sanitize_text_field($_REQUEST['field_type_key']) : '';
echo $cfs->output_custom_field_setting_item('',$field);
wp_die();
}
/**
* Admin action to save a custom sort field.
*
* @since 2.0.0
*/
public static function save_custom_sort_field(){
// security
check_ajax_referer( 'custom_fields_'.sanitize_text_field($_POST['field_id']), 'security' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( -1 );
}
$result = GeoDir_Settings_Cpt_Sorting::save_custom_field($_POST);
if(is_wp_error( $result ) ){
wp_send_json_error( $result->get_error_message() );
}else{
global $wpdb;
$fields = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . GEODIR_CUSTOM_SORT_FIELDS_TABLE . " WHERE id = %d OR tab_parent = %d ORDER BY sort_order ASC", array( $result, $result ) ) );
$cfs = new GeoDir_Settings_Cpt_Sorting();
if(count($fields) > 1){
echo $cfs->loop_fields_output($fields);
}else{
echo $cfs->output_custom_field_setting_item($result);
}
}
wp_die();
}
/**
* Admin action to delete a custom sort field.
*
* @since 2.0.0
*/
public static function delete_custom_sort_field(){
// security
$field_id = absint($_REQUEST['field_id']);
check_ajax_referer( 'custom_fields_'.$field_id, 'security' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( -1 );
}
$cfs = new GeoDir_Settings_Cpt_Sorting();
$result = $cfs->delete_custom_field($field_id);
if(is_wp_error( $result ) ){