-
Notifications
You must be signed in to change notification settings - Fork 4
/
class-locate-and-filter-admin.php
978 lines (895 loc) · 36.8 KB
/
class-locate-and-filter-admin.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
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* The admin-specific functionality of the plugin.
*
* @link http://monothemes.com/
* @since 1.0.0
*
* @package Locate_And_Filter
* @subpackage Locate_And_Filter/admin
*/
/**
* The admin-specific functionality of the plugin.
*
* Defines the plugin name, version, and two examples hooks for how to
* enqueue the admin-specific stylesheet and JavaScript.
*
* @package Locate_And_Filter
* @subpackage Locate_And_Filter/admin
* @author AMonin <[email protected]>
*/
class Locate_And_Filter_Admin
{
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* The Gmaps key for this app, used for geocoding in the admin
*
* @since 1.0.1
* @access private
* @var string $app_Gmaps_key
The GoogleMaps key.
*/
static $Gmaps_API_key = 'AIzaSyC0lZ7MbGfowxNTZva7fAyeTJ18dAWMUp0';
static $Bing_API_key = 'An5y6EfmR_Tmk0nMXSWFQD7JZ2sc5Zv793InR2eKkVmMpwxfa_XOzlEmIEPXIol9';
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $plugin_name
* The name of this plugin.
* @param string $version
* The version of this plugin.
*/
public function __construct($plugin_name, $version) {
$this->plugin_name = $plugin_name;
$this->version = '1.4.129';
}
/**
* Savezs WP root path for use in preview file
*
*
* @since 1.1.98
* @access private
*/
public static function saveRootPath() {
global $wp_filesystem;
// Initialize WP Filesystem
if (empty($wp_filesystem)) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
WP_Filesystem();
}
// Get the upload directory
$dir = wp_get_upload_dir();
$path = $dir['basedir'] . "/locateandfilter-cache";
// Check if the path is writable using WP Filesystem methods
if (!$wp_filesystem->is_writable($path)) {
// Try changing permissions if not writable
if (!$wp_filesystem->chmod($path, 0777)) {
return;
}
}
// Construct the path to the file
$file_path = $path . '/path2root';
// Get the real path of wp-load.php
$fpath = realpath(get_home_path()) . "/wp-load.php";
if (is_file($fpath)) {
// Write the path to the file using WP_Filesystem
$wp_filesystem->put_contents($file_path, $fpath);
} else {
// Try prefixes in case wp-load.php is not in the default location
$try_those_prefixes = array("admin", "private");
foreach ($try_those_prefixes as $prefix) {
$fpath = realpath(get_home_path()) . "/$prefix/wp-load.php";
if (is_file($fpath)) {
$wp_filesystem->put_contents($file_path, $fpath);
break;
}
}
}
}
/**
* Register new mime types
*
*
* @since 1.1.4
* @access private
*/
public function add_mime_types($mime_types){
$mime_types['kml'] = 'application/vnd.google-earth.kml+xml'; //Adding kml extension
$mime_types['svg'] = 'image/svg+xml';
return $mime_types;
}
/**
* serves the Gmaps Key
*
* @since 1.0.0
*/
public static function getGmapsAPIKey() {
$key = unserialize (get_option("locate-anything-option-googlemaps-key"));
if($key===false || empty($key)) $key = Locate_And_Filter_Admin::$Gmaps_API_key;
return $key;
}
/**
* serves the Bing Key
*
* @since 1.0.0
*/
public static function getBingAPIKey() {
$key = unserialize (get_option("locate-anything-option-bingmaps-key"));
if($key===false || empty($key)) $key = Locate_And_Filter_Admin::$Bing_API_key;
return $key;
}
/**
* Register the JavaScript for the dashboard.
*
* @since 1.0.0
*/
public function enqueue_scripts() {
$screen = get_current_screen();
$allowed_post_types = unserialize (get_option ( 'locate-anything-option-sources' ));
if(!is_array($allowed_post_types)) $allowed_post_types=array();
$allowed_post_types[]="locateandfiltermap";
$allowed_post_types[]="locateanythingmarker";
if(strpos($screen->base,'user')===false && !in_array($screen->post_type,$allowed_post_types)) return;
wp_enqueue_media();
wp_enqueue_script($this->plugin_name . "-adminjs", plugin_dir_url(__FILE__) . 'js/locate-and-filter-admin.js', array (
'jquery'
) , $this->version, false);
// leaflet JS
wp_enqueue_script($this->plugin_name . "-leaflet", plugin_dir_url(__FILE__) . '../public/js/leaflet-0.7.3/leaflet.js', array (
'jquery'
) , $this->version, false);
// leaflet-filters JS
wp_enqueue_script($this->plugin_name . "-leaflet-filters", plugin_dir_url(__FILE__) . '../public/js/leaflet-filters/leaflet-filters.js', array(
$this->plugin_name . "-leaflet"
) , $this->version, false);
wp_enqueue_script($this->plugin_name . "-googleAPI", "https://maps.googleapis.com/maps/api/js?key=".$this->getGmapsAPIKey()."&v=3.exp&libraries=places&language=en" . unserialize(get_option("locate-anything-option-map-language")) , array(
$this->plugin_name . "-leaflet-filters"
) , $this->version, false);
wp_enqueue_script($this->plugin_name . "-select2", plugin_dir_url(__FILE__) . 'js/select2_4.0.6-rc.1/js/select2.min.js', array() , $this->version, false);
// Awesome markers
wp_enqueue_script($this->plugin_name . "-awesomemarkers", plugin_dir_url(__FILE__) . '../public/js/leaflet.awesome-markers-2.0/leaflet.awesome-markers.js', array(
$this->plugin_name . "-leaflet"
) , $this->version, false);
// annotation plugin
wp_enqueue_script($this->plugin_name . "-anno", plugin_dir_url(__FILE__) . 'js/anno/anno.js', array(
'jquery'
) , $this->version, false);
wp_enqueue_script($this->plugin_name . "-anno-dependency", plugin_dir_url(__FILE__) . 'js/anno/scrollintoview/jquery.scrollintoview.min.js', array() , $this->version, false);
// Google Tiles
wp_enqueue_script($this->plugin_name . "-googleTiles", plugin_dir_url(__FILE__) . '../public/js/leaflet-plugins-master/layer/tile/Google.js', array(
$this->plugin_name . "-leaflet"
) , $this->version, false);
// leaflet markerCluster JS
wp_enqueue_script($this->plugin_name . "-leaflet-marker-cluster", plugin_dir_url(__FILE__) . '../public/js/leaflet.markercluster/leaflet.markercluster.js', array(
'jquery'
) , $this->version, false);
// google autocomplete
wp_enqueue_script($this->plugin_name . "-googleautojs", plugin_dir_url(__FILE__) . '../public/js/leaflet-google-autocomplete/js/leaflet-google-autocomplete.js', array(
$this->plugin_name . "-googleAPI") , $this->version, false);
// Edit Area js
//wp_enqueue_script($this->plugin_name . "-editArea", plugin_dir_url(__FILE__) . '../admin/js/edit_area/edit_area_full.js' , array('jquery') , $this->version, false);
// providers
wp_enqueue_script ( $this->plugin_name . "-leaflet-providers", plugin_dir_url ( __FILE__ ) . '../public/js/leaflet-providers/leaflet-providers.js', array (
'jquery'
), $this->version, false );
}
/**
* Register the stylesheets for the admin area.
*
* @since 1.0.0
*/
public function enqueue_styles() {
$screen = get_current_screen();
$allowed_post_types = unserialize (get_option ( 'locate-anything-option-sources' ));
if(!is_array($allowed_post_types)) $allowed_post_types=array();
$allowed_post_types[]="locateandfiltermap";
$allowed_post_types[]="locateanythingmarker";
if(strpos($screen->base,'user')===false && !in_array($screen->post_type,$allowed_post_types)) return;
wp_enqueue_style($this->plugin_name . "-admincss", plugin_dir_url(__FILE__) . 'css/locate-and-filter-admin.css', array() , $this->version, 'all');
wp_enqueue_style($this->plugin_name . "-annocss", plugin_dir_url(__FILE__) . 'js/anno/anno.css', array() , $this->version, 'all');
wp_enqueue_style($this->plugin_name . "-select2css", plugin_dir_url(__FILE__) . "js/select2_4.0.6-rc.1/css/select2.min.css", array() , $this->version, 'all');
// Ionicons
wp_enqueue_style($this->plugin_name . "-ioniconscss", 'https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css', array() , $this->version, 'all');
// Awesome markers
wp_enqueue_style($this->plugin_name . "-awesomemarkerscss", plugin_dir_url(__FILE__) . '../public/js/leaflet.awesome-markers-2.0/leaflet.awesome-markers.css', array() , $this->version, 'all');
// leaflet css
wp_enqueue_style($this->plugin_name . "-leaflet", plugin_dir_url(__FILE__) . '../public/js/leaflet-0.7.3/leaflet.css', array() , $this->version, 'all');
// leaflet-filters css
wp_enqueue_style($this->plugin_name . "-leaflet-filters", plugin_dir_url(__FILE__) . '../public/js/leaflet-filters/leaflet-filters.css', array() , $this->version, 'all');
// leaflet markerCluster css
wp_enqueue_style($this->plugin_name . "-leaflet-marker-cluster-default", plugin_dir_url(__FILE__) . '../public/js/leaflet.markercluster/MarkerCluster.Default.css', array() , $this->version, 'all');
wp_enqueue_style($this->plugin_name . "-leaflet-marker-cluster", plugin_dir_url(__FILE__) . '../public/js/leaflet.markercluster/MarkerCluster.css', array() , $this->version, 'all');
// leaflet Google automplete CSS
wp_enqueue_style($this->plugin_name . "-googleauto", plugin_dir_url(__FILE__) . '../public/js/leaflet-google-autocomplete/css/leaflet-google-autocomplete.css', array() , $this->version, 'all');
}
/**
* Adds metaboxes to the post types selected in the options page
*/
public function add_post_meta_boxes() {
// fetch the post types where LocateAnything will be active
$selected_post_types = unserialize(get_option('locate-anything-option-sources'));
// add the LocateAnything Metabox for each of them
if (is_array($selected_post_types)) $selected_post_types[] = "locateanythingmarker";
else $selected_post_types = array(
"locateanythingmarker"
);
foreach ($selected_post_types as $type) {
add_meta_box('locate-and-filter-class', // Unique ID
esc_html__('LocateAndFilter', 'locateandfilter') , // Title
'Locate_And_Filter_Admin::post_class_meta_box', // Callback function
$type, // Admin page (or post type)
'normal', // Context
'high'); // Priority
}
}
public function remove_anonymous_object_action( $tag, $class, $method, $priority=null ){
if( empty($GLOBALS['wp_filter'][ $tag ]) ){
return;
}
foreach ( $GLOBALS['wp_filter'][ $tag ] as $filterPriority => $filter ){
/* if( !($priority===null || $priority==$filterPriority) )
continue;*/
foreach ( $filter as $identifier => $function ){
try {
if( is_array( $function) && !is_a($function['function'],'Closure')
and is_a( $function['function'][0], $class )
and $method === $function['function'][1]
){
remove_action(
$tag,
array ( $function['function'][0], $method ),
$filterPriority
);
}
}
catch(Exception $e) { $done =false;}
}
}
}
/**
* DEPRECATED : unload all conflicting 3rd party plugin actions before preview
*/
public function clear_hooks_for_preview() {
if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'locate_and_filter_preview')) {
//wp_die(esc_html__('Nonce verification failed. Please refresh the page and try again.', 'locateandfilter'));
}
if(isset($_GET["locateAnything_preview"])) {
// Lifter LMS
Locate_And_Filter_Admin::remove_anonymous_object_action( 'wp_enqueue_scripts','LLMS_Frontend_Assets', 'enqueue_styles');
Locate_And_Filter_Admin::remove_anonymous_object_action( 'wp_enqueue_scripts','LLMS_Frontend_Assets', 'enqueue_scripts');
Locate_And_Filter_Admin::remove_anonymous_object_action( 'wp_loaded','LLMS_AJAX', 'register_script');
Locate_And_Filter_Admin::remove_anonymous_object_action( 'wp_footer','LLMS_Frontend_Assets', 'wp_footer' );
}
}
/**
* DEPRECATED : Loads the preview pane
*/
public function load_preview() {
if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'locate_and_filter_preview')) {
//wp_die(esc_html__('Nonce verification failed. Please refresh the page and try again.', 'locateandfilter'));
}
if(isset($_GET["locateAnything_preview"])){
include(plugin_dir_path(dirname(__FILE__)).'/admin/partials/locate-and-filter-preview.php');
die();
}
}
/**
* Adds metaboxes to the post types selected in the options page
*/
public function add_admin_meta_boxes() {
add_meta_box('locate-and-filter-class', // Unique ID
esc_html__('LocateAndFilter - Wordpress Plugin', 'locateandfilter') , // Title
'Locate_And_Filter_Admin::admin_class_meta_box', // Callback function
'locateandfiltermap', // Admin page (or post type)
'normal', // Context
'high'); // Priority
}
/**
* Checks cache permissions, called on action admin_notices
*
*/
public static function check_cache_permissions() {
global $wp_filesystem;
// Initialize WP Filesystem
if (empty($wp_filesystem)) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
WP_Filesystem();
}
$dir = wp_get_upload_dir();
$path = $dir['basedir'] . "/locateandfilter-cache";
// Check if the directory exists, if not create it using WP_Filesystem
if (!$wp_filesystem->exists($path)) {
if (!$wp_filesystem->mkdir($path, 0777)) {
echo '<div class="notice notice-error"><p>' . sprintf(
/* translators: %s is the directory path that needs write permissions */
esc_html__('<b>Error</b> : Could not create directory. Please add write permissions on the following directory: %s', 'locateandfilter'),
esc_html($path)
) . '</p></div>';
return;
}
}
// Check if the directory is writable
if (!$wp_filesystem->is_writable($path)) {
if (!$wp_filesystem->chmod($path, 0777)) {
echo '<div class="notice notice-error"><p>' . sprintf(
/* translators: %s is the directory path that needs write permissions */
esc_html__('<b>Error</b> : Please add write permissions on the following directory: %s', 'locateandfilter'),
esc_html($path)
) . '</p></div>';
}
}
}
/**
* Displays the settings page
*
*/
public static function admin_settings_page() {
include (plugin_dir_path(__FILE__) . 'partials/locate-and-filter-settings-admin.php');
}
/**
* Display the admin meta box.
*
*/
public static function admin_class_meta_box($object, $box) {
include (plugin_dir_path(__FILE__) . 'partials/locate-and-filter-metabox-admin.php');
}
/**
* Display the post meta box.
*
*/
public static function post_class_meta_box($object) {
include (plugin_dir_path(__FILE__) . 'partials/locate-and-filter-metabox-post.php');
}
/**
* Display the user meta box.
*
*/
public static function user_class_meta_box($object) {
$post_type = "user";
include (plugin_dir_path(__FILE__) . 'partials/locate-and-filter-metabox-post.php');
}
/**
* saves metabox fields
* @param int $post_id [description]
* @param WP Post Object $post
* @return int post_id
*/
public function save_metabox_data($post_id, $post) {
/* Verify the nonce before proceeding. */
if (!isset($_POST['locate_anything_class_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['locate_anything_class_nonce'])), "I961JpJQTj0crLKH0mGB")) return $post_id;
/* Get the post type object. */
$post_type = get_post_type_object($post->post_type);
/* Check if the current user has permission to edit the post. */
if (!current_user_can($post_type->cap->edit_post, $post_id)) return $post_id;
foreach ($_POST as $meta_key => $new_meta_value) {
if (strpos($meta_key, "locate-anything") !== false) Locate_And_Filter_Admin::add_update_metas($post_id, $meta_key, $new_meta_value);
if ( $meta_key == 'locate-anything-filters' ) {
$locate_anything_filters = $new_meta_value;
}
}
if (isset($locate_anything_filters)){
//$locate_anything_filters = $_POST['locate-anything-filters'];
$locateanything_custom_field_for_tax_elment = array();
$locateanything_custom_field_for_tax = get_option('locateanything_custom_field_for_tax');
//status checkbox
$locateanything_custom_field_for_tax_elment_checkbox = array();
$locateanything_custom_field_for_tax_checkbox = get_option('locateanything_custom_field_for_tax_checkbox');
//end status checkbox
foreach ($locate_anything_filters as $key => $value) {
$enable_icon = get_post_meta( $post_id, 'locate-anything-filter-selector-icon-'.$value, true );
$locateanything_custom_field_for_tax_elment[$value] = $enable_icon;
//status checkbox
$status = get_post_meta( $post_id, 'locate-anything-display-filter-'.$value, true );
if($status == 'checkbox') {
$locateanything_custom_field_for_tax_elment_checkbox[$value] = 'checkbox';
} else {
$locateanything_custom_field_for_tax_elment_checkbox[$value] = $status;
} //end status checkbox
}
if($locateanything_custom_field_for_tax){
$result = array_merge($locateanything_custom_field_for_tax, $locateanything_custom_field_for_tax_elment);
} else {
$result = $locateanything_custom_field_for_tax_elment;
}
update_option('locateanything_custom_field_for_tax', $result);
//status checkbox
if($locateanything_custom_field_for_tax_checkbox){
$result_ = array_merge($locateanything_custom_field_for_tax_checkbox, $locateanything_custom_field_for_tax_elment_checkbox);
} else {
$result_ = $locateanything_custom_field_for_tax_elment_checkbox;
}
update_option('locateanything_custom_field_for_tax_checkbox', $result_);
//end status checkbox
}
return $post_id;
}
/**
* Save the settings set in Option page
*/
public static function save_options() {
if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'locate_and_filter_preview')) {
//wp_die(esc_html__('Nonce verification failed. Please refresh the page and try again.', 'locateandfilter'));
}
foreach ($_POST as $k => $v) {
if (strpos($k, "locate-anything-option-") !== false) {
$v = self::locate_anything_sanitaze_option($k,$v);
update_option($k, serialize($v) , '', 'yes');
}
}
}
/**
* Utilitary function to add, delete, update metas
*/
public function add_update_metas($post_id, $meta_key, $new_meta_value) {
/* Get the meta value of the custom field key. */
$meta_value = get_post_meta($post_id, $meta_key, true);
/* If a new meta value was added and there was no previous value, add it. */
if ($new_meta_value !== false && '' == $meta_value) {
add_post_meta($post_id, $meta_key, $new_meta_value, true);
update_post_meta($post_id, $meta_key, $new_meta_value);
}
/* If the new meta value does not match the old value, update it. */
elseif ($new_meta_value !== false && $new_meta_value != $meta_value) update_post_meta($post_id, $meta_key, $new_meta_value);
/* If there is no new meta value but an old value exists, delete it. */
elseif ('' == $new_meta_value && $meta_value) delete_post_meta($post_id, $meta_key, $meta_value);
}
/**
* creates Admin Page in WP admin menu
*/
public function setup_admin_menu() {
add_submenu_page("edit.php?post_type=locateandfiltermap", "Options", "Options", "edit_posts", "locate-anything-settings", "Locate_And_Filter_Admin::admin_settings_page");
}
/**
* defines a custom types for the maps
*/
public function createCustomType() {
$labels = array(
'name' => __('LocateAndFilter', 'locateandfilter') ,
'singular_name' => __('Map', 'locateandfilter') ,
'add_new' => __('Add New', 'locateandfilter') ,
'add_new_item' => __('Add New Map', 'locateandfilter') ,
'edit_item' => __('Edit Map', 'locateandfilter') ,
'new_item' => __('New Map', 'locateandfilter') ,
'all_items' => __('All Map', 'locateandfilter') ,
'view_item' => __('View Map', 'locateandfilter') ,
'search_items' => __('Search Maps', 'locateandfilter') ,
'not_found' => __('No map found', 'locateandfilter') ,
'not_found_in_trash' => __('No map found in Trash', 'locateandfilter') ,
'menu_name' => __('LocateAndFilter', 'locateandfilter')
);
$supports = array(
'title'
);
$slug = "locateandfilterMap";
$args = array(
'labels' => $labels,
'public' => false,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-admin-site',
'query_var' => true,
'rewrite' => array(
'slug' => $slug
) ,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => $supports
);
register_post_type('locateandfilterMap', $args);
/* marker custom post type*/
$labels = array(
'name' => __('Marker Categories', "locateandfilter") ,
'singular_name' => __('Marker Category', "locateandfilter") ,
'search_items' => __('Search Categories', "locateandfilter") ,
'all_items' => __('All Categories', "locateandfilter") ,
'parent_item' => __('Parent Category', "locateandfilter") ,
'parent_item_colon' => __('Parent Category:', "locateandfilter") ,
'edit_item' => __('Edit Category', "locateandfilter") ,
'update_item' => __('Update Category', "locateandfilter") ,
'add_new_item' => __('Add New Category', "locateandfilter") ,
'new_item_name' => __('New Category Name', "locateandfilter") ,
'menu_name' => __('Categories', "locateandfilter") ,
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'locateanythingmarkercategory'
) ,
);
register_taxonomy('locateanythingmarkercategory', 'locateanythingMarker', $args);
$labels = array(
'name' => __('Marker Tags', "locateandfilter") ,
'singular_name' => __('Marker Tag', "locateandfilter") ,
'search_items' => __('Search Tags', "locateandfilter") ,
'all_items' => __('All Tags', "locateandfilter") ,
'parent_item' => __('Parent Tag', "locateandfilter") ,
'parent_item_colon' => __('Parent Tag:', "locateandfilter") ,
'edit_item' => __('Edit Tag', "locateandfilter") ,
'update_item' => __('Update Tag', "locateandfilter") ,
'add_new_item' => __('Add New Tag', "locateandfilter") ,
'new_item_name' => __('New Tag Name', "locateandfilter") ,
'menu_name' => __('Tags', "locateandfilter") ,
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'locateanythingmarkertag'
) ,
);
register_taxonomy('locateanythingmarkertag', 'locateanythingMarker', $args);
$labels = array(
'name' => __('Markers', 'locateandfilter') ,
'singular_name' => __('Marker', 'locateandfilter') ,
'add_new' => __('Add New', 'locateandfilter') ,
'add_new_item' => __('Add New Marker', 'locateandfilter') ,
'edit_item' => __('Edit Marker', 'locateandfilter') ,
'new_item' => __('New Marker', 'locateandfilter') ,
'all_items' => __('All Marker', 'locateandfilter') ,
'view_item' => __('View Marker', 'locateandfilter') ,
'search_items' => __('Search Markers', 'locateandfilter') ,
'not_found' => __('No Marker found', 'locateandfilter') ,
'not_found_in_trash' => __('No Marker found in Trash', 'locateandfilter') ,
'menu_name' => __('Markers', 'locateandfilter')
);
$supports = array(
'title',
'editor',
'excerpt','thumbnail'
);
$slug = "locateanythingMarker";
$args = array(
'labels' => $labels,
'public' => false,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array(
'slug' => $slug
) ,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => $supports,
'taxonomies' => array(
'locateanythingmarkercategory','locateanythingmarkertag'
)
);
register_post_type('locateanythingMarker', $args);
}
/**
* Returns the additional field list stored in options
* @param boolean $post_type [description]
* @return [type] [description]
*/
public static function getAdditional_field_list($post_type = false) {
$additional_field_list_json = stripslashes(unserialize(get_option('locate-anything-option-additional-field-list', '')));
if ($additional_field_list_json) {
$additional_field_list = json_decode($additional_field_list_json, true);
} else {
$additional_field_list = array();
}
if (!is_array($additional_field_list)) $additional_field_list = array();
if ($post_type !== false) {
foreach ($additional_field_list as $key => $field) {
if ($field["post_type"] !== $post_type) unset($additional_field_list[$key]);
}
}
return $additional_field_list;
}
/**
* displays additional fields and their tags
*
*/
public static function displayAdditionalFieldNotice($post_type) {
$additional_field_list_json = stripslashes(unserialize(get_option('locate-anything-option-additional-field-list', '')));
if ($additional_field_list_json) $additional_field_list = json_decode($additional_field_list_json, true); ?>
<div id="basic_fields_notice">
<?php
$post_types = array("basic"=>"basic");
$post_types += unserialize (get_option ( 'locate-anything-option-sources' ));
$post_types = apply_filters("locate_anything_add_sources",$post_types);
$already_displayed_tags =array();
foreach ( $post_types as $posttype =>$postTypeName ) {
if($postTypeName=="Users") $postTypeName = 'user';
$markups = Locate_And_Filter_Public::getBasicMarkupList($postTypeName);
foreach ($markups as $tag => $nothing) {
if (in_array($tag, $already_displayed_tags)) continue;
// Mark this tag as already displayed
array_push($already_displayed_tags, $tag);
?>
<div class="basic-markup basic-markup-<?php echo esc_attr($postTypeName); ?>">
<b><?php echo esc_html(ucfirst(str_replace(array("|", "_"), array("", " "), $tag))); ?></b> :
<?php echo esc_html($tag); ?>
</div>
<?php }
} ?>
</div>
<div id="additional_fields_notice">
<table id="additional_fields_notice">
<?php
if (is_array($additional_field_list)) {
foreach ($additional_field_list as $field) {
// Skip invalid or mismatched fields
if (is_null($field["field_description"]) || is_null($field["field_name"]) || $field['post_type'] !== $post_type) continue;
?>
<tr class="basic-markup basic-markup-<?php echo esc_attr($field['post_type']); ?>">
<td><b><?php echo esc_html($field["field_description"]) . " (" . esc_html($field['post_type']) . ")"; ?></b></td>
<td>|<?php echo esc_html($field['post_type']) . "::" . esc_attr(sanitize_key(remove_accents($field["field_description"]))); ?>|</td>
</tr>
<?php
}
}
?>
<tfoot id="filter_fields_notice"></tfoot>
</table>
</div>
<?php
}
/**
* Display additional fields form
*
*/
public static function displayAdditionalFields($post) {
// If the post type is not valid, exit the function
if (get_post_type($post) == false) return;
?>
<ul id="additional_fields">
<?php
// Get the additional field list and decode the JSON
$additional_field_list_json = stripslashes(unserialize(get_option('locate-anything-option-additional-field-list', '')));
if ($additional_field_list_json) $additional_field_list = json_decode($additional_field_list_json, true);
// Check if additional fields exist
if ($additional_field_list) {
if (is_array($additional_field_list) && $post !== "user") {
foreach ($additional_field_list as $field) {
// Check if the post type matches
if ($field["post_type"] == get_post_type($post)) { ?>
<li>
<b><?php echo esc_html($field["field_description"]); ?></b><br/>
<textarea name="<?php echo esc_attr($field["field_name"]); ?>">
<?php echo esc_textarea(get_post_meta($post->ID, $field["field_name"], true)); ?>
</textarea>
</li>
<?php
}
}
}
}
?>
</ul>
<?php
}
/**
* Returns default templates for a new map
*
*/
public static function getDefaultTemplates() {
return array(
"tooltip" => "<div class=\"tooltip-wrap\">
<div class=\"tooltip-thumb\">|small_thumbnail|</div>
<div class=\"tooltip-content\">|content_stripped|</div>
</div>
<a class=\"tooltip-link\" href=\"|post_link|\">read more</a>",
"nice-tooltip" => "<div class=\"tooltip-wrap\">
<div class=\"tooltip-thumb\">|medium_thumbnail|</div>
<div class=\"tooltip-content\">|content_stripped|</div>
</div>
<a class=\"tooltip-link\" href=\"|post_link|\">read more</a>",
"navlist" => "<div class=\"navlist-title\"><b>|title|</b></div>
<div class=\"navlist-content\">
<div class=\"navlist-thumbnail\">|small_thumbnail|</div>
<div class=\"navlist-stripp-content\">
|content_stripped|
<a class=\"navlist-link\" href=\"|post_link|\">read more</a>
</div>
</div>"
);
}
/**
* Returns all the metas for this post
* @param [int] $id [post ID]
* @return [array] array [metaname]=metavalue
*/
public static function getPostMetas($id) {
$t = array();
foreach (get_post_meta($id) as $k => $v) {
$t[$k] = current($v);
}
return $t;
}
/**
* Returns all the metas for this user
* @param [int] $id [post ID]
* @return [array] array [metaname]=metavalue
*/
public static function getUserMetas($id) {
$t = array();
foreach (get_user_meta($id) as $k => $v) {
$t[$k] = current($v);
}
return $t;
}
/**
* Returns select licences seed
* @param [int] $id [licence id]
* @return [string] seed
*/
public static function getLicence($id) {
$licences = array('label'=>"-license-lvl1");
$licences = apply_filters("add_seed_licence",$licences);
$license_key =unserialize(get_option("locate-anything-option-".$licences[$id]."-license"));
return array('seed'=>$licences[$id],'key'=>$license_key);
}
/**
* Geocodes address,
* @param [string] $address
* @return [false | array] returns false if unable to geocode address
*/
public static function geocode($address) {
// url encode the address
$address = urlencode($address);
// google map geocode api url
$gmaps_key = Locate_And_Filter_Admin::getGmapsAPIKey();
$url = "https://maps.google.com/maps/api/geocode/json?key=$gmaps_key&sensor=false&address={$address}";
// get the json response
$resp_json = Locate_And_Filter_Tools::file_get_contents_wp($url);
// decode the json
$resp = json_decode($resp_json, true);
// response status will be 'OK', if able to geocode given address
if ($resp['status'] == 'OK') {
// get the important data
$lati = $resp['results'][0]['geometry']['location']['lat'];
$longi = $resp['results'][0]['geometry']['location']['lng'];
$formatted_address = $resp['results'][0]['formatted_address'];
// verify if data is complete
if ($lati && $longi && $formatted_address) {
// put the data in the array
$data_arr = array();
array_push($data_arr, $lati, $longi, $formatted_address);
return $data_arr;
}
else return false;
}
else return false;
}
/**
* AJAX function : returns JSON encoded array of taxonomies tied to a post type
*/
/* get Taxonomies associated with type passed in request */
public function LA_getTaxonomies() {
if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'locate_and_filter_preview')) {
//wp_die(esc_html__('Nonce verification failed. Please refresh the page and try again.', 'locateandfilter'));
}
//echo wp_json_encode(get_object_taxonomies(sanitize_text_field($_REQUEST['type'])));
$type = isset($_REQUEST['type']) ? sanitize_text_field(wp_unslash($_REQUEST['type'])) : '';
echo wp_json_encode(get_object_taxonomies($type));
die();
}
/**
* AJAX function : returns JSON encoded array of taxonomies tied to a post type
*/
/* get Taxonomies associated with type passed in request */
public function LA_getTaxonomies_plus() {
if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'locate_and_filter_preview')) {
//wp_die(esc_html__('Nonce verification failed. Please refresh the page and try again.', 'locateandfilter'));
}
//$tax = get_object_taxonomies(sanitize_text_field($_REQUEST['type']));
$tax = isset($_REQUEST['type']) ? get_object_taxonomies(sanitize_text_field(wp_unslash($_REQUEST['type']))) : '';
//array_push($tax, $_REQUEST['type']);
echo wp_json_encode($tax);
die();
}
/**
* AJAX function : returns JSON encoded array of taxonomies tied to a post type
*/
/* get Taxonomies associated with type passed in request */
public function LA_getPOST_id() {
if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'locate_and_filter_preview')) {
//wp_die(esc_html__('Nonce verification failed. Please refresh the page and try again.', 'locateandfilter'));
}
$type = isset($_REQUEST['type']) ? sanitize_text_field(wp_unslash($_REQUEST['type'])) : '';
$all_post_ids = get_posts(array(
'fields' => 'ids',
'posts_per_page' => -1,
'post_type' => $type
));
echo wp_json_encode( $all_post_ids );
die();
}
/**
* AJAX function : returns JSON encoded array of taxonomies tied to a post type
*/
/* get Taxonomy terms associated with type passed in request */
public function LA_getTaxonomyTerms() {
if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'locate_and_filter_preview')) {
//wp_die(esc_html__('Nonce verification failed. Please refresh the page and try again.', 'locateandfilter'));
}
$map_id = isset($_REQUEST['map_id']) ? sanitize_text_field(wp_unslash($_REQUEST['map_id'])) : '';
$type = isset($_REQUEST['type']) ? sanitize_text_field(wp_unslash($_REQUEST['type'])) : '';
$selected = get_post_meta( $map_id, "locate-anything-allowed-filters-value-" . $type, true);
//$terms = get_terms(sanitize_text_field($_REQUEST['type']));
$terms = get_terms([
'taxonomy' => $type,
'hide_empty' => false,
]);
if ($terms) foreach ($terms as $in => $term) {
if (is_array($selected) && array_search($term->term_id, $selected) !== false) $terms[$in]->selected = 1;
else $terms[$in]->selected = 0;
if (!$selected) $terms[$in]->selected = 1;
}
echo wp_json_encode($terms);
die();
}
/**
* AJAX function : returns JSON encoded html code for layout
*/
public function getLayoutCode() {
if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'locate_and_filter_preview')) {
//wp_die(esc_html__('Nonce verification failed. Please refresh the page and try again.', 'locateandfilter'));
}
//$map_id = sanitize_text_field($_POST["map_id"]);
$map_id = isset($_POST['map_id']) ? sanitize_text_field(wp_unslash($_POST['map_id'])) : '';
//$layout_id = sanitize_text_field($_POST["layout_id"]);
$layout_id = isset($_POST['layout_id']) ? sanitize_text_field(wp_unslash($_POST['layout_id'])) : '';
$record = get_post_meta( intval($map_id) , "locate-anything-map-template-html-" . $layout_id, true);
if ($record == false) {
//echo json_encode(file_get_contents(Locate_And_Filter_Assets::getMapTemplates( $layout_id )->url));
echo wp_json_encode(Locate_And_Filter_Tools::get_local_file_contents(Locate_And_Filter_Assets::getMapTemplates( $layout_id )->url));
} else {
echo wp_json_encode($record);
}
die();
}
/**
* AJAX function : returns HTML of current filters
*/
public function getFilters() {
if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'locate_and_filter_preview')) {
//wp_die(esc_html__('Nonce verification failed. Please refresh the page and try again.', 'locateandfilter'));
}
$map_id = isset($_POST['map_id']) ? sanitize_text_field(wp_unslash($_POST['map_id'])) : '';
$type = isset($_POST['type']) ? sanitize_text_field(wp_unslash($_POST['type'])) : '';
echo esc_html(apply_filters("locate_anything_add_filter_choice", '', $map_id, $type ));
die();
}
/**
* function : sanitaze options value
*/
private static function locate_anything_sanitaze_option($key, $var){
if ( is_array( $var ) ) {
return array_map( 'sanitize_text_field', $var );
} else {
return sanitize_text_field( $var );
}
}
}