This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
savvymapper.php
1082 lines (882 loc) · 33.9 KB
/
savvymapper.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
/*
Copyright (C) 2016 Cimbura.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* SavvyMapper connects your WordPress install to your GIS data in a savvy way
*
* Plugin Name: SavvyMapper Core
* Author: Michael Moore
* Author URI: http://cimbura.com
* Version: 0.1.1
*/
/**
* @class Presents the SavvyMapper UI and API to WordPress
*/
class SavvyMapper {
/**
* @var The singleton instance itself.
*/
private static $_instance = null;
/**
* @var The users settings.
*
* Includes information needed to create connections, like
* usernames and API keys
*
* (Not yet set or used)
*/
var $settings;
/**
* @var The loaded interfaces.
*
* An array of geo-api interface instances
*
* An interface is something we could talk to, if we had
* credentials or other settings. An interface will
* probably usually represent a service's API or a file type
*
* The array is associative, with the shortname as they key
* and the instance as the value.
*
* There should be one interface per service type
*
* Corresponds with savvymapper_connections
*/
var $interfaces;
/**
* @var The loaded interfaces.
*/
var $interface_classes;
/**
* @var The mappings between savvyconnector instances and post types.
*
* It's an array, the keys are the post type, the values are the mapping details
*
* Corresponds with savvymapper_mappings
*/
var $mappings;
/**
*
*/
/**
* Get the singleton instance.
*/
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self;
}
return self::$_instance;
}
/**
* Initializes the SavvyMapper instance
*/
protected function __construct() {
$this->settings = array();
$this->get_mappings();
$this->setup_actions();
$this->setup_shortcodes();
}
/**
* Set up all the hooks and filters
*/
function setup_actions() {
add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'load_scripts' ) );
add_filter( 'plugin_action_links', array( $this, 'plugin_add_settings_link' ), 10, 5 );
add_action( 'wp_ajax_savvy_autocomplete', array( $this, 'ajax_autocomplete' ) );
// add_action( 'wp_ajax_nopriv_savvy_autocomplete', array( $this, 'ajax_autocomplete' ) );
add_action( 'wp_ajax_savvy_get_interface_options_form', array( $this, 'get_interface_options_form' ) );
// add_action( 'wp_ajax_nopriv_savvy_get_interface_options_form', array( $this, 'get_interface_options_form' ) );
add_action( 'wp_ajax_savvy_get_mapping_options_form', array( $this, 'get_mapping_options_form' ) );
// add_action( 'wp_ajax_nopriv_savvy_get_mapping_options_form', array( $this, 'get_mapping_options_form' ) );
add_action( 'wp_ajax_savvy_get_geojson_for_post', array( $this, 'get_geojson_for_post' ) );
add_action( 'wp_ajax_nopriv_savvy_get_geojson_for_post', array( $this, 'get_geojson_for_post' ) );
add_action( 'wp_ajax_savvy_clearcache', array( $this, 'clear_cache' ) );
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_meta' ) );
add_action( 'loop_start', array( $this, 'make_archive_map' ) );
add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
add_action( 'admin_init', array( $this, 'settings_init' ) );
add_action( 'plugins_loaded', array( $this, 'load_interfaces' ) );
}
/**
* Set up the shortcodes
*/
function setup_shortcodes() {
add_shortcode( 'savvymapper', array( $this, 'do_shortcodes' ) );
}
/**
* Load the needed javascript
*/
function load_scripts() {
$plugin_dir_url = plugin_dir_url( __FILE__ );
wp_enqueue_style( 'cartodbcss','//cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/themes/css/cartodb.css' );
// wp_enqueue_script('cartodbjs','//libs.cartocdn.com/cartodb.js/v3/3.15/cartodb.js');
// wp_enqueue_script( 'cartodbjs','//cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/cartodb.js' );
wp_enqueue_script( 'cartodbjs','//cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/cartodb.uncompressed.js' );
wp_enqueue_style( 'savvycss',$plugin_dir_url . 'assets/savvy.css' );
wp_enqueue_style( 'savvyautocompletecss',$plugin_dir_url . 'assets/jquery.auto-complete.css' );
wp_enqueue_script( 'savvyautocompletejs',$plugin_dir_url . 'assets/jquery.auto-complete.js',array( 'jquery' ) );
wp_enqueue_script( 'savvyclassjs',$plugin_dir_url . 'assets/savvyclass.js' );
wp_enqueue_script( 'savvymapper',$plugin_dir_url . 'assets/savvymapper.js',array( 'jquery', 'savvyclassjs' ) );
wp_localize_script( 'savvymapper', 'ajaxurl', admin_url( 'admin-ajax.php' ) );
wp_enqueue_script( 'savvymapjs',$plugin_dir_url . 'assets/savvymap.js',array( 'jquery', 'cartodbjs' ) );
}
/**
* Execute the shortcodes
*
* @param array $attrs The shortcode attributes.
* @param string $contents What was between two sets of shortcode tags.
*
* @return HTML
*/
function do_shortcodes( $attrs, $contents ) {
global $post;
$html = '';
if ( isset( $attrs['attr'] ) ) {
list($connection, $mapping, $current_settings) = $this->get_post_info_by_post_id( $post->ID );
$attrs = $this->make_attrs( $attrs, $mapping );
// Attributes are simpler, so we'll just ask for fetures based on a search
$json = $connection->get_attribute_shortcode_geojson( $attrs, $contents, $mapping, $current_settings );
$json = apply_filters( 'savvymapper_geojson', $json, $mapping );
// what happens if multiple features match a query
$allProp = array();
foreach ( $json['features'] as $feature ) {
// multiple can be: empty (unique), all (show all) or first (show first)
// We only collect non-empty properties to display
$allProp[] = $feature['properties'][ $attrs['attr'] ];
if ( $attrs['multiple'] == 'first' ) {
// If we're asking for a single, just break
break;
}
}
// If we're unique, do that now
if ( $attrs['multiple'] !== 'all' ) {
$allProp = array_unique( $allProp );
}
// Remove empty values
$allProp = array_filter( $allProp );
sort( $allProp );
$allProp = apply_filters( 'savvymapper_attr_values', $allProp, $mapping, $attrs['attr'] );
if ( count( $allProp ) > 0 ) {
$propHtml = implode( '</span><span class="savvy-attr">', $allProp );
$propHtml = '<span class="savvy-attr">' . $propHtml . '</span>';
}
$finalHtml = '<span class="savvy-attrs" data-attr="' . $attrs['attr'] . '">' . $propHtml . '</span>';
$finalHtml = apply_filters( 'savvymapper_attr_html', $finalHtml, $mapping, $attrs['attr'] );
return $finalHtml;
} else if ( isset( $attrs['show'] ) ) {
// For maps, start with the default supported options, then
// ask the connection to parse out any more details it
// supports, then put it all in a data- attribute for
// javascript to process
if ( $attrs['show'] == 'map' ) {
$current_settings_str = get_post_meta( $post->ID, 'savvymapper_post_meta', true );
$current_settings_ar = json_decode( $current_settings_str, true );
// Backwards compatibility with 0.0.1 -- remove soon
if ( ! empty( $current_settings_ar['mapping_id'] ) ) {
$current_settings_ar = array( $current_settings_ar );
}
$mapSetup = $this->make_attrs( $attrs );
unset( $mapSetup['attr'] );
unset( $mapSetup['multiple'] );
unset( $mapSetup['show'] );
unset( $mapSetup['onarchive'] );
unset( $mapSetup['show_popups'] );
unset( $mapSetup['show_features'] );
$mapMeta = array();
$classes = array(
'savvy_map_div',
'savvy_page_map_div',
);
foreach ( $current_settings_ar as $idx => $current_settings ) {
$mapping = $this->get_mappings( $current_settings['mapping_id'] );
$connection = $this->get_connections( $mapping['connection_id'] );
$mapMeta['post_type'] = $mapping['post_type'];
$mapMeta['post_id'] = $post->ID;
$mapMeta['mapping_ids'][] = $mapping['mapping_id'];
$mapMeta['mapping_slugs'][] = $mapping[' mapping_slug'];
$mapSetup['layers'][] = $this->make_layer_config( array(), $contents, $connection, $mapping, $current_settings );
$classes[] = 'savvy_map_' . $connection->get_type();
$classes[] = 'savvy_map_' . $mapping['mapping_slug'];
}
$mapMeta['map_id'] = implode( '_',$mapMeta['mapping_ids'] );
$classes = array_unique( $classes );
$html .= "<div class='" . implode( ' ', $classes ) . "' data-mapmeta='" . json_encode( $mapMeta ) . "' data-map='" . json_encode( $mapSetup ) . "'></div>";
return $html;
}
}
}
/**
* Set up the default attributes for shortcodes.
*
* @param array $attrs The attrs passed in by a shortcode.
* @param array $mapping The current mapping.
*
* @return The array of attrs to actually use
*/
function make_attrs( $attrs = array(), $mapping = array() ) {
// Order of presidence goes
// 1. $attrs
// 2. $mapping
// 3. $defaults
// Default shortcode option that all interfaces need to support
$defaults = array(
'attr' => null,
'multiple' => 'unique',
'show' => null,
'onarchive' => 'show',
'show_popups' => 1,
'show_features' => 1,
'zoom' => 'default',
'lat' => 'default',
'lng' => 'default',
'layers' => array(),
);
// Get attributes out of $mapping
$mapping_defaults = array_intersect_key( $mapping, $defaults );
// Now override the hard-coded defaults with the mapping defaults
$mapping_defaults = array_merge( $defaults, $mapping_defaults );
// Finally, override that with the passed in $attrs since the shortcode take higest presidence
$attrs = array_merge( $mapping_defaults, $attrs );
return $attrs;
}
function make_layer_config( $attrs, $contents, $connection, $mapping, $current_settings ) {
global $post;
$attrs = $this->make_attrs( $attrs, $mapping );
$layerSetup = array(
'show_popups' => $attrs['show_popups'],
'show_features' => $attrs['show_features'],
'mapping_id' => $mapping['mapping_id'],
'mapping_slug' => $mapping['mapping_slug'],
);
$connectionMapSetup = $connection->get_map_shortcode_properties( $attrs, $contents, $mapping, $current_settings );
$layerSetup = array_merge( $layerSetup, $connectionMapSetup );
return $layerSetup;
}
/**
* Add a link to the settings page, just to be nice
*
* @param array $actions Array of actions link.
* @param string $plugin_file Link to a plugin file, hopefully this one.
*
* @return The modified list of actions.
*/
function plugin_add_settings_link( $actions, $plugin_file ) {
static $plugin;
if ( ! isset( $plugin ) ) {
$plugin = plugin_basename( __FILE__ );
}
if ( $plugin == $plugin_file ) {
$settings = array( 'settings' => '<a href="options-general.php?page=savvymapper">' . __( 'Settings', 'General' ) . '</a>' );
$actions = array_merge( $settings, $actions );
}
return $actions;
}
/**
* Autocomplete!
*/
function ajax_autocomplete() {
$mapping = $this->get_mappings( $_GET['mapping_id'] );
$connection = $this->get_connections( $mapping['connection_id'] );
$suggestions = $connection->autocomplete( $mapping, $_GET['term'] );
foreach ( $suggestions as &$v ) { $v = (string) $v; }
header( 'Content-Type: application/json' );
print json_encode( $suggestions );
exit();
}
/**
* Make metaboxes!
*/
function add_meta_boxes() {
global $post;
$this->get_mappings();
foreach ( $this->mappings as $mapping ) {
if ( $mapping['post_type'] == $post->post_type ) {
$connection = $this->connections[ $mapping['connection_id'] ];
if ( empty( $connection ) ){
continue;
}
add_meta_box(
$mapping['mapping_id'],
'<strong>SavvyMapper:</strong> ' . $connection->get_connection_name() . '<span class="dashicons dashicons-arrow-right-alt2"></span>' . $mapping['mapping_name'],
array( $this,'make_meta_box' ),
null,
'advanced',
'default',
array( $connection,$mapping )
);
}
}
}
/**
* Make a single metabox
*/
function make_meta_box( $post, $metabox ) {
$connection = $metabox['args'][0];
$mapping = $metabox['args'][1];
list($cur_connection, $cur_mapping, $current_settings ) = $this->get_post_info_by_post_id( $post->ID, $mapping['mapping_id'] );
$mapSetup = array(
'lat' => 'default',
'lng' => 'default',
'zoom' => 'default',
'layers' => array(),
);
$mapSetup['layers'][] = $this->make_layer_config( array(), array(), $connection, $mapping, $current_settings );
$mapMeta = array(
'post_type' => $post->post_type,
'post_id' => $post->ID,
'mapping_ids' => array( $mapping['mapping_id'] ),
'mapping_slugs' => array( $mapping['mapping_slug'] ),
'map_id' => $mapping['mapping_id'],
);
$html = "<div class='savvy_metabox_wrapper' data-mapping_id='" . $mapping['mapping_id'] . "'>";
// Two columns
// Col. 1: settings, help and hidden metadata
$html .= '<div class="savvymapper_metabox_col">';
$html .= '<label>Look up value <em>' . $mapping['lookup_field'] . '</em>: </label>';
$html .= '<input class="savvy_lookup_ac" name="savvymapper_lookup_value[]" value="' . $current_settings['lookup_value'] . '">';
$html .= '<br>' . "\n";
$html .= "<input type='hidden' name='savvyampper_mapping_id[]' value='" . $mapping['mapping_id'] . "'>";
$html .= $connection->extra_metabox_fields( $post, $mapping, $current_settings );
ob_start();
wp_nonce_field( 'savvymapper_meta_box', 'savvymapper_meta_box_nonce' );
$html .= ob_get_clean();
$html .= '<hr>';
$lookup_fields = $connection->get_attribute_names( $mapping );
// $html .= '<h3>Shortcode Cheat Sheet</h3>';
// $html .= '<p>Coming Soon</p>';
$html .= '</div>';
// Col. 2: map div
$html .= '<div class="savvymapper_metabox_col">';
$classes = array(
'savvy_metabox_map_div',
'savvy_metabox_map_' . $connection->get_type(),
'savvy_map_' . $mapping['mapping_slug'],
);
$html .= "<div class='" . implode( ' ', $classes ) . "' data-mapmeta='" . json_encode( $mapMeta ) . "' data-map='" . json_encode( $mapSetup ) . "'></div>";
$html .= '</div>';
$html .= '</div>';
print $html;
}
/**
* Save the posted metadata for the current post
*
* @param $post_id The post ID for this meta data
*/
function save_meta( $post_id ) {
// Check if our nonce is set.
if ( ! isset( $_POST['savvymapper_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['savvymapper_meta_box_nonce'],'savvymapper_meta_box' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
$post_meta = array();
foreach ( $_POST['savvyampper_mapping_id'] as $idx => $mapping_id ) {
$mapping = $this->get_mappings( $_POST['savvyampper_mapping_id'][ $idx ] );
$connection = $this->get_connections( $mapping['connection_id'] );
$current_settings = array();
// Capture common fields for all interfaces
$settings = array(
'mapping_id' => $mapping['mapping_id'],
'lookup_value' => sanitize_text_field( $_POST['savvymapper_lookup_value'][ $idx ] ),
);
$connectionMeta = $connection->save_meta( $post_id, $idx );
// Merge them
$settings = array_merge( $settings, $connectionMeta );
$post_meta[] = $settings;
}
// serialize settings and update post meta
update_post_meta( $post_id,'savvymapper_post_meta',json_encode( $post_meta ) );
}
/**
* Make the map for the archive, if the settings say so
*/
function make_archive_map( $query ) {
if ( ! is_archive() ) {
return;
}
if ( $query->is_main_query() ) {
$post_type = get_post_type();
foreach ( $this->mappings as $mapping_type => $mapping ) {
if ( $mapping_type === $post_type ) {
$this->make_archive_map();
}
}
}
}
/**
* Add the admin menu entry
*/
function add_admin_menu() {
add_menu_page(
'SavvyMapper', // page title
'SavvyMapper', // menu title
'manage_options', // capability
'savvymapper', // menu slug
array( $this,'options_page' ) // function to show page contents
// icon url
// position
);
add_submenu_page(
'savvymapper', // parent_slug
'Post Mapping', // page title
'Post Mapping', // menu title
'manage_options', // capability
'savvy_connections', // menu slug
array( $this,'connection_mapping_page' ) // function to get output contents
);
}
/**
* Show the mappings and edit them
*/
function connection_mapping_page() {
$html = '<div class="wrap savvyoptions_wrap">';
$html .= '<h2>SavvyMapper Post Mapping</h2>';
$html .= '<p>Configure mapping between your post types and your SavvyMapper connections.</p>';
// Get all post types and make an option dropdown.
$post_types = get_post_types( array( 'public' => true ) );
ksort( $post_types );
$post_type_options = array();
foreach ( $post_types as $post_type ) {
$post_type_object = get_post_type_object( $post_type );
// $post_type_list[$post_type] = $post_type_object->labels->singular_name;
$post_type_options[] = '<option value="' . $post_type . '">' . $post_type_object->labels->name . '</option>';
}
$connection_options = array();
$connections = $this->get_connections();
foreach ( $connections as $connection ) {
$connection_options[] = '<option value="' . $connection->get_id() . '">' . $connection->get_connection_name() . '</option>';
}
// Show all the settings
$mapping = $this->get_mappings();
$html .= '<div id="savvy_mapping_settings">';
foreach ( $mapping as $one_mapping ) {
$html .= $this->_get_mapping_options_form( $one_mapping );
}
$html .= '<br></div>';
// Show the form to add new mappings
$html .= '<div id="savvy_mapping_form">';
$html .= '<label for="savvy_post_type" class="toplabel">Post Type</label><select name="savvy_post_type">' . implode( "\n", $post_type_options ) . '</select> ';
$html .= '<label for="savvy_connection_id" class="toplabel">Connection</label><select name="savvy_connection_id">' . implode( "\n", $connection_options ) . '</select> ';
$html .= '<input type="button" onclick="SAVVY._add_mapping(this);" value="Add Mapping">';
$html .= '</div>';
// Save options form
$html .= '<form method="post" action="options.php">';
ob_start();
settings_fields( 'savvymapper_mapping_page' );
do_settings_sections( 'savvymapper_mapping_page' );
submit_button();
$html .= ob_get_clean();
$html .= '</form>';
$html .= '</div>';
print $html;
}
/**
* Print the options form
*/
function options_page() {
$html = '<div class="wrap savvyoptions_wrap">';
$html .= '<p>Welcome to SavvyMapper.';
$html .= '<h2>SavvyMapper</h2>';
$html .= '<h3>Settings</h3>';
$html .= '<div id="savvysettings">';
$html .= '<p>In order to reduce calls to GIS services and to improve your site\'s speed, SavvyMapper caches results by default. Set to 0 to disable.</p>';
$html .= '<label>Cache Timeout<br>(in minutes)</label><input data-name="cachetimeout" value="' . $this->get_settings( 'cachetimeout' ) . '"><br>';
$html .= '<input type="button" id="savvyclearcache" value="Clear Cache"><br>';
$html .= '</div>';
$html .= '<h3>Connections</h3>';
$html .= '<p>Add connections to services here.</p>';
$html .= '<p>After creating a connection, you can map data from that connection to post types on the <i>Post Mapping</i> options page.</p>';
foreach ( $this->interface_classes as $interface ) {
$html .= '<input type="button" onclick="SAVVY._add_connection(this);" data-type="' . $interface->get_type() . '" value="Add ' . $interface->get_name() . ' Connection"> ';
}
$html .= '<hr>';
$html .= '<div id="savvyconnectionoptions">';
$connections = $this->get_connections();
foreach ( $connections as $connection ) {
$html .= $this->_get_interface_options_form( $connection );
}
$html .= '</div>';
$html .= '<form method="post" action="options.php">';
ob_start();
settings_fields( 'savvymapper_plugin_page' );
do_settings_sections( 'savvymapper_plugin_page' );
submit_button();
$html .= ob_get_clean();
$html .= '</form>';
$html .= '</div>';
print $html;
}
/**
*
*/
function settings_init() {
register_setting(
'savvymapper_plugin_page', // option group
'savvymapper_connections' // option name
); // sanitize_callback
add_settings_section(
'savvymapper_plugin_page_section', // id
null, // title
'__return_false', // callback
'savvymapper_plugin_page' // page
);
add_settings_field(
'savvymapper_connections', // id
__( 'SavvyMapper Connections', 'wordpress' ), // title
array( $this,'savvymapper_connections_callback' ), // callback
'savvymapper_plugin_page', // page
'savvymapper_plugin_page_section', // section
array() // args
);
register_setting(
'savvymapper_mapping_page', // option group
'savvymapper_mappings' // option name
);
add_settings_section(
'savvymapper_mapping_page_section', // id
null, // title
'__return_false', // callback
'savvymapper_mapping_page' // page
);
add_settings_field(
'savvymapper_mappings', // id
__( 'SavvyMapper Mappings', 'wordpress' ), // title
array( $this,'savvymapper_mapping_callback' ), // callback
'savvymapper_mapping_page', // page
'savvymapper_mapping_page_section', // section
array() // args
);
}
function load_interfaces() {
require_once( dirname( __FILE__ ) . '/savvyinterface.php' );
$this->interface_classes = apply_filters( 'savvymapper_load_interfaces', $this->interface_classes );
// Now that we have all our interfaces we can make our connections
$this->get_connections();
}
/**
* This is called by ajax to make new boxes, and by options page to load existing options
*/
function get_interface_options_form() {
$interface_type = $_GET['interface'];
$interface = $this->interface_classes[ $interface_type ];
print $this->_get_interface_options_form( $interface );
exit();
}
/**
* Actually create the options form
*
* @param SavvyInterface $interface An instance of SavvyInterface.
*
* @return HTML for a single instance-config block.
*/
function _get_interface_options_form( $interface ) {
$html = '';
$html .= '<div class="instance-config">';
$html .= '<h3 class="savvyheader"><span class="savvyhinner"><span class="dashicons dashicons-no remove-instance"></span>Connection to <em>' . $interface->get_name() . '</em></span></h3>';
$html .= '<label>Connection Name</label><input type="text" data-name="connection_name" value="' . $interface->get_connection_name() . '"><br>' . "\n";
$html .= '<input type="hidden" data-name="interface" value="' . $interface->get_type() . '">' . "\n";
$html .= '<input type="hidden" data-name="_id" value="' . $interface->get_id() . '">' . "\n";
$html .= $interface->options_div();
$html .= '<hr>';
$html .= '</div>';
return $html;
}
/**
* This is called by ajax to make new data mapping boxes, and by options page to load existing values
*/
function get_mapping_options_form() {
$connection_id = $_GET['connection_id'];
$mapping = array(
'post_type' => $_GET['post_type'],
'connection_id' => $_GET['connection_id'],
'layer_order' => 0,
);
print $this->_get_mapping_options_form( $mapping );
exit();
}
function _get_mapping_options_form( $mapping ) {
$postType = $mapping['post_type'];
$post_type_info = get_post_type_object( $postType );
$connections = $this->get_connections();
$connection = $connections[ $mapping['connection_id'] ];
if ( empty( $connection ) ){
return;
}
$mapping_id = (empty( $mapping['mapping_id'] ) ? time() : $mapping['mapping_id']);
$html = '<div class="mapping-config">';
$html .= '<h3 class="savvyheader"><span class="savvyhinner"><span class="dashicons dashicons-no remove-instance"></span>Mapping from <em>' . $post_type_info->labels->name . '</em> to <em>' . $connection->get_connection_name() . '</em></span></h3>';
$html .= '<label>Mapping Name</label><input type="text" data-name="mapping_name" value="' . $mapping['mapping_name'] . '"><br>' . "\n";
$html .= '<label>Mapping Slug</label><input type="text" readonly="readonly" value="' . sanitize_title($mapping['mapping_name']) . '"><br>' . "\n";
$html .= '<label>Layer Order</label><input type="text" data-name="layer_order" value="' . $mapping['layer_order'] . '"><br>' . "\n";
$html .= '<input type="hidden" data-name="connection_id" value="' . $connection->get_id() . '">';
$html .= '<input type="hidden" data-name="mapping_id" value="' . $mapping_id . '">';
$html .= '<input type="hidden" data-name="post_type" value="' . $mapping['post_type'] . '">';
$html .= $connection->mapping_div( $mapping );
$marker_checked = ( ! isset( $mapping['show_features'] ) || $mapping['show_features'] != 0 ? 'checked="checked"' : '' );
$html .= '<label>Show features</label><input type="checkbox" data-name="show_features" value="1" ' . $marker_checked . '><br>';
$popups_checked = ( ! isset( $mapping['show_popups'] ) || $mapping['show_popups'] != 0 ? 'checked="checked"' : '' );
$html .= '<label>Show popups</label><input type="checkbox" data-name="show_popups" value="1" ' . $popups_checked . '>';
$html .= '<hr>';
$html .= '</div>';
return $html;
}
function savvymapper_connections_title_callback() {
$args = func_get_args();
print '<h2 class="savvymapper_options_title">' . $args[0] . '</h2>';
}
function savvymapper_connections_callback() {
$settings = get_option( 'savvymapper_connections', '{}' );
print '<textarea name="savvymapper_connections" id="savvymapper_connections">' . $settings . '</textarea>';
}
function savvymapper_mapping_callback() {
$settings = get_option( 'savvymapper_mappings', "{'mapping':[]}" );
print '<textarea name="savvymapper_mappings" id="savvymapper_mappings">' . $settings . '</textarea>';
}
/**
* Get the known connections, requesting them from the database, if needed
*
* A connection will have at least:
* {'interface': $interface_type, '_id': $the_id}
*
* @return $this->connections, an array of connections
*/
function get_connections( $connection_id = false ) {
if ( isset( $this->connections ) ) {
if ( $connection_id ) {
return $this->connections[ $connection_id ];
}
return $this->connections;
}
$settings_connections_string = get_option( 'savvymapper_connections', '{}' );
$settings = json_decode( $settings_connections_string, true );
if ( empty( $settings['connections'] ) ) {
$settings['connections'] = array();
}
$connections_list = array();
foreach ( $settings['connections'] as $connection ) {
$interfaceClass = get_class( $this->interface_classes[ $connection['interface'] ] );
$connections_list[ $connection['_id'] ] = new $interfaceClass( $connection );
}
unset( $settings['connections'] );
$this->settings = $settings;
$this->connections = $connections_list;
if ( $connection_id ) {
return $this->connections[ $connection_id ];
}
return $this->connections;
}
/**
* Get the known mappings, requesting them from the database, if needed
*
* @return $this->mappings, an array of mappings
*/
function get_mappings( $mapping_id = false ) {
if ( isset( $this->mappings ) ) {
if ( $mapping_id ) {
return $this->mappings[ $mapping_id ];
}
return $this->mappings;
}
$mapping_string = get_option( 'savvymapper_mappings', "{'mapping':[]}" );
$mapping = json_decode( $mapping_string, true );
if ( empty( $mapping['mappings'] ) ) {
$mapping['mappings'] = array();
}
$this->mappings = array();
foreach ( $mapping['mappings'] as $mapping ) {
$mapping['mapping_slug'] = sanitize_title( $mapping['mapping_name'] );
$mapping['layer_order'] = ( ! empty( $mapping['layer_order'] ) ? $mapping['layer_order'] : 0 );
$this->mappings[ $mapping['mapping_id'] ] = $mapping;
}
uasort( $this->mappings,array( $this, '_mapping_sort' ) );
if ( $mapping_id ) {
return $this->mappings[ $mapping_id ];
}
return $this->mappings;
}
/**
* Custom sort function for mappings
*
* Sort by:
* 1. post_type
* 2. layer_order, reversed (so that top layers appear on top)
* 3. mapping_id
*/
function _mapping_sort( $a, $b ) {
$post_type_order = strnatcmp( $a['post_type'], $b['post_type'] );
if ( $post_type_order !== 0 ) {
return $post_type_order;
}
$layer_order_order = $b['layer_order'] - $a['layer_order'];
if ( $layer_order_order !== 0 ) {
return $layer_order_order;
}
$mapping_id_order = $a['mapping_id'] - $b['mapping_id'];
return $mapping_id_order;
}
/**
* Get a setting, or all of the settings
*
* @param string $setting_field The specific setting to retrieve.
*/
function get_settings( $setting_field = null ) {
if ( empty( $this->settings ) ) {
$this->get_connections();
}
$defaults = array(
'cachetimeout' => 60,
);
$this->settings = array_merge( $defaults, $this->settings );
if ( ! empty( $setting_field ) ) {
return $this->settings[ $setting_field ];
}
return $this->settings;
}
/**
* Get the GeoJson for a given post and mapping
*/
function get_geojson_for_post() {
$postId = $_GET['post_id'];
list($connection, $mapping, $current_settings) = $this->get_post_info_by_post_id( $postId, $_GET['mapping_id'] );
if ( empty( $connection ) ) {
$mapping = $this->get_mappings( $_GET['mapping_id'] );
$connection = $this->get_connections( $mapping['connection_id'] );
$current_settings = array();
}
$overrides = $_GET['overrides'];
if ( empty( $overrides ) ) {
$overrides = array();
}
$current_settings = array_merge( $current_settings, $overrides );
$json = $connection->_get_geojson_for_post( $mapping, $current_settings );
$json = apply_filters( 'savvymapper_geojson', $json, $mapping );
// Leaflet doesn't like it if there's no zero index
if ( ! empty( $json['features'] ) ) {
$json['features'] = array_values( $json['features'] );
}
header( 'Content-Type: application/json' );
print json_encode( $json );
exit();
}
/**
* A convenience function used to find the connection, mapping and current_settings for
* the specified post ID and mapping_id
*
* @param event_id $post_id The post ID
* @param mapping_id $mapping_id The mapping
*
* @return An array with $connection, $mapping and $current_settings
*
* @note If $mapping_id == 'first', then the first mapping will be returned.
*
* @note In the event that the current post has no connection/mapping/settings (such as when a post is
* first created or if a mapping is added to a post type after a post has been created)
* then your function will need to handle the null/null/array() that's returned
* and instantiante or find the connection info some other way.
*/
function get_post_info_by_post_id( $post_id, $mapping_id = 'first' ) {
// Get the connection and mapping objects for the current post
$current_settings_str = get_post_meta( $post_id, 'savvymapper_post_meta', true );
if ( empty( $current_settings_str ) ) {
return array( null, null, array() );
}