-
Notifications
You must be signed in to change notification settings - Fork 16
/
geo-mashup.php
2093 lines (1843 loc) · 62.8 KB
/
geo-mashup.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
/*
Plugin Name: Geo Mashup
Plugin URI: https://wordpress.org/plugins/geo-mashup/
Description: Save location for posts and pages, or even users and comments. Display these locations on Google, Leaflet, and OSM maps. Make WordPress into your GeoCMS.
Version: 1.13.14
Author: Dylan Kuhn
Text Domain: GeoMashup
Domain Path: /lang
Author URI: http://www.cyberhobo.net/
Minimum WordPress Version Required: 3.0
License: GPL2+
*/
/* Copyright 2015 Dylan Kuhn (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2 or later, as
published by the Free Software Foundation.
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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* The main Geo Mashup plugin file loaded by WordPress.
*
* @package GeoMashup
*/
if ( !class_exists( 'GeoMashup' ) ) {
/** @noinspection AutoloadingIssuesInspection */
/**
* The Geo Mashup static class.
*
* Used primarily for namespace, with methods called using the scope operator,
* like echo GeoMashup::map();
*
* @package GeoMashup
* @since 1.0
* @access public
* @static
*/
class GeoMashup {
/**
* Whether to add the click-to-load map script.
*
* @since 1.4
* @var bool
*/
private static $add_loader_script = false;
/**
* The basename of the Geo Mashup Search plugin when deactivated.
*
* @since 1.5
* @var string
*/
private static $deactivate_geo_search_basename = '';
/**
* Freemius integration.
*
* @since 1.10
* @var Freemius
*/
private static $freemius;
/**
* Load Geo Mashup.
*
* Initializations that can be done before init().
*
* @since 1.2
*/
public static function load() {
self::load_constants();
self::load_dependencies();
self::load_hooks();
}
/**
* Test to see if the current request is for the Geo Mashup options page.
*
* @since 1.4
*
* @return bool Whether this is a an options page request.
*/
public static function is_options_page() {
// We may need this before $pagenow is set, but maybe this method won't always work?
return ( is_admin() and isset($_GET['page']) and GEO_MASHUP_PLUGIN_NAME === $_GET['page'] );
}
/**
* WordPress init action.
*
* init {@link http://codex.wordpress.org/Plugin_API/Action_Reference#Advanced_Actions action},
* called by WordPress.
*
* @since 1.2
*/
public static function init() {
if ( GEO_MASHUP_DB_VERSION !== GeoMashupDB::installed_version() ) {
// We're active but not installed - try once more to install
GeoMashupDB::install();
}
self::load_styles();
self::load_scripts();
}
/**
* Load relevant dependencies.
*
* @since 1.2
*/
private static function load_dependencies() {
global $geo_mashup_options;
include_once GEO_MASHUP_DIR_PATH . '/vendor/autoload.php';
include_once GEO_MASHUP_DIR_PATH . '/geo-mashup-options.php';
include_once GEO_MASHUP_DIR_PATH . '/gm-location-query.php';
include_once GEO_MASHUP_DIR_PATH . '/post-query.php';
include_once GEO_MASHUP_DIR_PATH . '/geo-mashup-db.php';
include_once GEO_MASHUP_DIR_PATH . '/geo-mashup-ui-managers.php';
if ( ! defined( 'GEO_MASHUP_UNIT_TESTING' ) &&
! defined( 'GEO_MASHUP_FREEMIUS_OPT_OUT') &&
is_admin() ) {
include_once GEO_MASHUP_DIR_PATH . '/freemius.php';
self::$freemius = new GeoMashupFreemius();
self::$freemius->load();
}
if ( $geo_mashup_options->get( 'overall', 'enable_geo_search' ) === 'true' ) {
include_once GEO_MASHUP_DIR_PATH . '/geo-mashup-search.php';
}
if ( function_exists( 'register_rest_field' ) ) {
include_once GEO_MASHUP_DIR_PATH . '/rest-api.php';
}
}
/**
* Load available integrations.
*
* @since 1.10.0
*/
public static function load_integrations() {
load_plugin_textdomain( 'GeoMashup', false, GEO_MASHUP_DIRECTORY . '/lang' );
if ( defined( 'ICL_LANGUAGE_CODE' ) ) {
include_once GEO_MASHUP_DIR_PATH . '/wpml.php';
add_action( 'wpml_loaded', array( 'GeoMashupWPML', 'load' ) );
}
if ( defined( 'SNAZZY_VERSION_NUMBER' ) || defined( 'SNAZZY_MAPS_VERSION_NUMBER' ) ) {
include_once GEO_MASHUP_DIR_PATH . '/snazzy-maps.php'; GeoMashupSnazzyMaps::load();
}
}
/**
* Load relevant hooks.
*
* @since 1.2
*/
private static function load_hooks() {
global $geo_mashup_options;
add_action( 'init', array( __CLASS__, 'init' ) );
add_action( 'wp_scheduled_delete', array( __CLASS__, 'action_wp_scheduled_delete' ) );
add_action( 'plugins_loaded', array( __CLASS__, 'dependent_init' ), -1 );
add_action( 'plugins_loaded', array( __CLASS__, 'load_integrations' ) );
add_action( 'wp_ajax_geo_mashup_query', array( __CLASS__, 'geo_query') );
add_action( 'wp_ajax_nopriv_geo_mashup_query', array( __CLASS__, 'geo_query') );
add_action( 'wp_ajax_geo_mashup_kml_attachments', array( __CLASS__, 'ajax_kml_attachments') );
add_action( 'wp_ajax_nopriv_geo_mashup_kml_attachments', array( __CLASS__, 'ajax_kml_attachments') );
add_action( 'wp_ajax_geo_mashup_suggest_custom_keys', array( 'GeoMashupDB', 'post_meta_key_suggest' ) );
add_action( 'rest_api_init', array( 'GeoMashupRestAPI', 'init' ));
register_activation_hook( __FILE__, array( __CLASS__, 'activation_hook' ) );
if (is_admin()) {
// To add Geo Mashup settings page
add_action('admin_menu', array(__CLASS__, 'admin_menu'));
// To make important announcements
add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) );
// To add plugin listing links
add_filter( 'plugin_action_links', array( __CLASS__, 'plugin_action_links' ), 10, 2 );
add_filter( 'plugin_row_meta', array( __CLASS__, 'plugin_row_meta' ), 10, 2 );
// Enqueue widget assets in admin
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'widget_scripts'));
} else {
// This is a non-admin request
if ($geo_mashup_options->get('overall','add_category_links') === 'true') {
// To add map links to a category list - flaky, requires non-empty category description
add_filter('list_cats', array(__CLASS__, 'list_cats'), 10, 2);
}
// To output location meta tags in the page head
add_action('wp_head', array(__CLASS__, 'wp_head'));
// To add footer output (like scripts)
add_action( 'wp_footer', array( __CLASS__, 'wp_footer' ) );
// To allow shortcodes in the text widget
if ( ! has_filter( 'widget_text', 'do_shortcode' ) ) {
add_filter( 'widget_text', 'do_shortcode', 11 );
}
// To add the GeoRSS namespace to feeds (not available for RSS 0.92)
add_action('rss2_ns', array(__CLASS__, 'rss_ns_buffer'), 1);
add_action('atom_ns', array(__CLASS__, 'rss_ns_buffer'), 1);
add_action('rss2_ns', array(__CLASS__, 'rss_ns'), 99);
add_action('atom_ns', array(__CLASS__, 'rss_ns'), 99);
// To add GeoRSS location to feeds
add_action('rss2_item', array(__CLASS__, 'rss_item'));
add_action('atom_entry', array(__CLASS__, 'rss_item'));
// To add custom renderings
add_filter( 'query_vars', array( __CLASS__, 'query_vars' ) );
add_action( 'template_redirect', array( __CLASS__, 'template_redirect' ) );
include_once GEO_MASHUP_DIR_PATH . '/shortcodes.php';
}
}
/**
* Define Geo Mashup constants.
*
* @since 1.2
*/
private static function load_constants() {
define('GEO_MASHUP_PLUGIN_NAME', plugin_basename(__FILE__));
/** @noinspection DirectoryConstantCanBeUsedInspection */
define('GEO_MASHUP_DIR_PATH', __DIR__);
define('GEO_MASHUP_DIRECTORY', dirname( GEO_MASHUP_PLUGIN_NAME ) );
define('GEO_MASHUP_URL_PATH', trim( plugin_dir_url( __FILE__ ), '/' ) );
define('GEO_MASHUP_MAX_ZOOM', 20);
define('GEO_MASHUP_VERSION', '1.13.14');
define('GEO_MASHUP_DB_VERSION', '1.3');
}
/**
* Load relevant scripts.
*
* @since 1.2
*/
private static function load_scripts() {
if ( self::is_options_page() ) {
wp_enqueue_script( 'jquery-ui-tabs' );
wp_enqueue_script( 'suggest' );
if ( isset( $_POST['geo_mashup_run_tests'] ) ){
self::register_script( 'qunit', 'js/qunit.js', array( 'jquery' ), GEO_MASHUP_VERSION, true );
self::register_script( 'qunit-close-enough', 'js/qunit-close-enough.js', array( 'qunit' ), GEO_MASHUP_VERSION, true );
self::register_script( 'geo-mashup-tests', 'js/qunit-tests.js', array( 'qunit-close-enough' ), GEO_MASHUP_VERSION, true );
wp_enqueue_script( 'geo-mashup-tests' );
include_once GEO_MASHUP_DIR_PATH . '/tests.php';
}
}
if (is_admin()){
self::register_script( 'geo-mashup-widget','js/widget.js', array( 'jquery' ), GEO_MASHUP_VERSION, true );
}
}
/**
* Load relevant styles.
*
* @since 1.2
*/
private static function load_styles() {
if ( self::is_options_page() ) {
self::register_style( 'jquery-smoothness', 'css/jquery-ui.1.7.smoothness.css', array(), GEO_MASHUP_VERSION, 'screen' );
wp_enqueue_style( 'jquery-smoothness' );
if ( isset( $_POST['geo_mashup_run_tests'] ) ){
self::register_style( 'qunit', 'css/qunit.css', array(), GEO_MASHUP_VERSION, 'screen' );
wp_enqueue_style( 'qunit' );
}
}
}
/**
* WordPress hook to perform activation tasks.
*
* @since 1.4
*/
public static function activation_hook() {
global $geo_mashup_options;
GeoMashupDB::install();
if ( '' === $geo_mashup_options->get( 'overall', 'version' ) &&
'' !== $geo_mashup_options->get( 'overall', 'google_key' )
) {
// Upgrading from a pre-1.4 version - don't set the default provider to Google v3
$geo_mashup_options->set_valid_options(
array(
'overall' => array(
'map_api' => 'google',
'version' => GEO_MASHUP_VERSION
)
)
);
$geo_mashup_options->save();
}
}
/**
* WordPress action to supply an init action for plugins that would like to use Geo Mashup APIs.
*
* @since 1.4
* @uses do_action() geo_mashup_init Fired when Geo Mashup is loaded and ready.
*/
public static function dependent_init() {
global $geo_mashup_options;
do_action( 'geo_mashup_init' );
if ( defined( 'GeoMashupSearch::VERSION' ) && class_exists( 'GeoMashupSearch' ) ) {
// The old search plugin is active - enable native geo search and flag for deactivation
/** @noinspection PhpUndefinedMethodInspection */
self::$deactivate_geo_search_basename = GeoMashupSearch::get_instance()->basename;
$geo_mashup_options->set_valid_options(
array(
'overall' => array(
'enable_geo_search' => 'true',
)
)
);
$geo_mashup_options->save();
}
}
/**
* WordPress action to remove expired Geo Mashup transients.
*
* @since 1.4.6
* @uses apply_filters() geo_mashup_disable_scheduled_delete A way to disable the scheduled delete.
*/
public static function action_wp_scheduled_delete() {
global $wpdb, $_wp_using_ext_object_cache;
if ( $_wp_using_ext_object_cache ||
defined( 'GEO_MASHUP_DISABLE_SCHEDULED_DELETE' ) ||
apply_filters( 'geo_mashup_disable_scheduled_delete', false )
) {
return;
}
$time = time();
$expired = $wpdb->get_col( "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE '_transient_timeout_gm%' AND option_value < {$time};" );
foreach( $expired as $transient ) {
$key = str_replace('_transient_timeout_', '', $transient);
delete_transient($key);
}
$wpdb->query( "OPTIMIZE TABLE {$wpdb->options}" );
}
/**
* Register the Geo Mashup script appropriate for the request.
*
* @since 1.4
*
* @param string $handle Global tag for the script.
* @param string $src Path to the script from the root directory of Geo Mashup.
* @param array|string $deps Array of dependency handles.
* @param string|bool $ver Script version.
* @param bool $in_footer Whether the script can be loaded in the footer.
*/
public static function register_script(
$handle,
$src,
$deps = array(),
$ver = false,
$in_footer = false
) {
// Use the minified version if SCRIPT_DEBUG is not set and it exists
if ( ( !defined( 'SCRIPT_DEBUG' ) || !SCRIPT_DEBUG ) && '.js' === substr( $src, -3 ) ) {
$min_src = substr( $src, 0, -3 ) . '.min.js';
if ( is_readable( GEO_MASHUP_DIR_PATH . '/' . $min_src ) ) {
$src = $min_src;
}
}
wp_register_script(
$handle,
plugins_url( $src, __FILE__ ),
$deps,
$ver,
$in_footer );
}
/**
* Register the Geo Mashup style appropriate for the request.
*
* @since 1.4
*
* @param string $handle Global tag for the style.
* @param string $src Path to the stylesheet from the root directory of Geo Mashup.
* @param array|string $deps Array of dependency handles.
* @param string|bool $ver Script version.
* @param string $media Stylesheet media target.
*/
public static function register_style(
$handle,
$src,
$deps = array(),
$ver = false,
$media = 'all'
) {
// Use the minified version if SCRIPT_DEBUG is not set and it exists
if ( ( !defined( 'SCRIPT_DEBUG' ) || !SCRIPT_DEBUG ) && '.css' === substr( $src, -4 ) ) {
$min_src = substr( $src, 0, -4 ) . '.min.css';
if ( is_readable( GEO_MASHUP_DIR_PATH . '/' . $min_src ) ) {
$src = $min_src;
}
}
wp_register_style( $handle, plugins_url( $src, __FILE__ ), $deps, $ver, $media );
}
/**
* WordPress action to add things like scripts to the footer.
*
* @since 1.4
*/
public static function wp_footer() {
if ( self::$add_loader_script ) {
self::register_script(
'geo-mashup-loader',
'js/loader.js',
array(),
GEO_MASHUP_VERSION,
true );
wp_print_scripts( 'geo-mashup-loader' );
}
}
/**
* WordPress filter to add Geo Mashup query variables.
*
* @since 1.3
* @param array $public_query_vars
* @return array
*/
public static function query_vars( $public_query_vars ) {
$public_query_vars[] = 'geo_mashup_content';
return $public_query_vars;
}
/**
* Locate a Geo Mashup template.
*
* Geo Mashup looks for templates given a certain base name. Given a base
* name of 'info-window', it will return the first of:
* 'geo-mashup-info-window.php' in the active theme directory
* 'info-window.php' in the geo-mashup-custom plugin directory
* 'default-templates/info-window.php' in the geo-mashup plugin directory
*
* @since 1.4
*
* @param string $template_base The base name of the template.
* @return string The file path of the template found.
*/
public static function locate_template( $template_base ) {
global $geo_mashup_custom;
$template = locate_template( array("geo-mashup-$template_base.php") );
/** @noinspection PhpUndefinedMethodInspection */
if ( empty( $template ) &&
$geo_mashup_custom !== null &&
$geo_mashup_custom->file_url( $template_base . '.php' )
) {
$template = path_join( $geo_mashup_custom->dir_path, $template_base . '.php' );
}
if ( empty( $template ) || !is_readable( $template ) ) {
$template = path_join( GEO_MASHUP_DIR_PATH, "default-templates/$template_base.php" );
}
if ( empty( $template ) || !is_readable( $template ) ) {
// If all else fails, just use the default info window template
$template = path_join( GEO_MASHUP_DIR_PATH, 'default-templates/info-window.php' );
}
return $template;
}
/**
* WordPress action to deliver templated Geo Mashup content.
*
* @since 1.3
*
* @uses geo_query
* @uses render_map
*/
public static function template_redirect() {
$geo_mashup_content = get_query_var( 'geo_mashup_content' );
if ( ! empty( $geo_mashup_content ) ) {
// The parameter's purpose is to get us here, we can remove it now
unset( $_GET['geo_mashup_content'] );
// Call the function corresponding to the content request
// This provides some security, as only implemented methods will be executed
$method = str_replace( '-', '_', $geo_mashup_content );
$callable = array( __CLASS__, $method );
if ( is_callable( $callable ) ) {
call_user_func( $callable );
} else {
/** @noinspection ForgottenDebugOutputInspection */
error_log( 'Undefined Geo Mashup content requested: ' . $method );
status_header( 404 );
echo 'Not Found';
}
exit();
}
}
/**
* Process an AJAX geo query.
*
* @since 1.3
* @uses geo-query.php
*/
public static function geo_query() {
require_once 'geo-query.php';
exit();
}
/**
* Process an iframe map request.
*
* @since 1.3
* @uses render-map.php
*/
private static function render_map() {
require_once 'render-map.php';
GeoMashupRenderMap::render_map();
exit();
}
/**
* WordPress action to perform an ajax edit operation and echo results.
*
* @since 1.3
*/
public static function ajax_edit() {
check_ajax_referer( 'geo-mashup-edit', 'geo_mashup_nonce' );
unset( $_GET['_wpnonce'] );
$status = array( 'request' => 'ajax-edit', 'code' => 200 );
if ( isset( $_POST['geo_mashup_object_id'] ) ) {
$status['object_id'] = $_POST['geo_mashup_object_id'];
} else {
$status['code'] = 400;
$status['message'] = __( 'No object id posted.', 'GeoMashup' );
$status['object_id'] = '?';
}
/** @todo add an option for a user capability check here? */
if ( ! empty( $_POST['geo_mashup_ui_manager'] ) && 200 === $status['code'] ) {
$ui_manager = GeoMashupUIManager::get_instance( $_POST['geo_mashup_ui_manager'] );
$result = $ui_manager->save_posted_object_location( $status['object_id'] );
if ( is_wp_error( $result ) ) {
$status['code'] = 500;
$status['message'] = $result->get_error_message();
}
}
if ( 200 === $status['code'] ) {
if ( ! empty( $_REQUEST['geo_mashup_update_location'] ) ) {
$status['message'] = __( 'Location updated.', 'GeoMashup' );
} else if ( ! empty( $_REQUEST['geo_mashup_delete_location'] ) ) {
$status['message'] = __( 'Location deleted.', 'GeoMashup' );
} else if ( ! empty( $_REQUEST['geo_mashup_add_location'] ) ) {
$status['message'] = __( 'Location added.', 'GeoMashup' );
}
}
echo json_encode( array( 'status' => $status ) );
exit();
}
/**
* Toggle limiting of query_posts to located posts only,
* with Geo Mashup query extensions.
*
* When enabled, only posts with locations will be returned from
* WordPress query_posts() and related functions. Also adds Geo Mashup
* public query variables.
*
* Caution - what if a future Geo Mashup incorporates multiple locations per object?
*
* @since 1.3
* @deprecated Use the geo_mashup_query query var
*
* @param bool $yes_or_no Whether to activate the join or not.
*/
public static function join_post_queries( $yes_or_no ) {
GeoMashupDB::join_post_queries( $yes_or_no );
}
/**
* Helper to turn a string of key-value pairs into an associative array.
*
* @since 1.0
*
* @param string $glue1 Pair separator.
* @param string $glue2 Key/value separator.
* @param string $str String to explode.
* @return array The associative array.
*/
public static function explode_assoc($glue1, $glue2, $str) {
$array2=explode($glue2, $str);
$array3=array();
foreach($array2 as $val) {
$pos=strpos($val,$glue1);
$key=substr($val,0,$pos);
$array3[$key] =substr($val,$pos+1,strlen($val));
}
return $array3;
}
/**
* Helper to turn an associative array into a string of key-value pairs.
*
* @since 1.0
*
* @param string $inner_glue Key/value separator.
* @param string $outer_glue Pair separator.
* @param array $array Array to implode.
* @param mixed $skip_empty Whether to include empty values in output.
* @param mixed $urlencoded Whether to URL encode the output.
* @return string The imploded string.
*/
public static function implode_assoc($inner_glue, $outer_glue, $array, $skip_empty=false, $urlencoded=false) {
$output = array();
foreach($array as $key=>$item) {
if (!$skip_empty || $item !== null) {
if ($urlencoded) {
$output[] = preg_replace(
'/\s/',
' ',
$key.$inner_glue.urlencode($item)
);
} else {
$output[] = preg_replace('/\s/', ' ', $key.$inner_glue.$item);
}
}
}
return implode($outer_glue, $output);
}
/**
* Guess the best language code for the current context.
*
* Takes some plugins and common practices into account.
*
* @since 1.4
*
* @return string Language code.
*/
public static function get_language_code() {
if ( isset( $_GET['lang'] ) ) {
// A language override technique is to use this querystring parameter
$language_code = $_GET['lang'];
} else if ( function_exists( 'qtrans_getLanguage' ) ) {
// qTranslate integration
$language_code = qtrans_getLanguage();
} else {
$language_code = get_locale();
}
return apply_filters( 'geo_mashup_get_language_code', $language_code );
}
/**
* Get post types available for location searches.
*
* This includes post types included via the located_post_types option,
* as well as post types registered with exclude_from_search set to
* <em>false</em>.
*
* @since 1.4.5
*
* @return array Array of post type strings.
*/
public static function get_searchable_post_types() {
global $geo_mashup_options;
$located_types = $geo_mashup_options->get( 'overall', 'located_post_types' );
$searchable_types = array_keys( get_post_types( array('exclude_from_search' => false) ) );
return array_unique( array_merge( $located_types, $searchable_types ) );
}
/**
* Get an array of URLs of KML or KMZ attachments for a post.
*
* @since 1.1
*
* @param int $post_id
* @return array Array of URL strings.
*/
public static function get_kml_attachment_urls($post_id) {
if ( empty( $post_id ) ) {
return array();
}
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_mime_type' => array(
'application/vnd.google-earth.kml+xml',
'application/vnd.google-earth.kmz',
'application/octet-stream'
),
'post_parent' => $post_id
);
$attachments = get_posts($args);
$urls = array();
if ($attachments) {
foreach ($attachments as $attachment) {
$url = wp_get_attachment_url( $attachment->ID );
// Backwards compatibility: include KML attachments with the incorrect octet-stream mime type
if ( 'application/octet-stream' !== $attachment->post_mime_type ||
'kml' === substr( $url, -3 )
) {
$urls[] = $url;
}
}
}
return $urls;
}
/**
* Echo a JSONP array of URLs of KML or KMZ attachments for posts.
*
* since 1.4
*/
public static function ajax_kml_attachments() {
$urls = array(array());
if ( !empty( $_REQUEST['post_ids'] ) ) {
$post_ids = array_map( 'intval', explode( ',', $_REQUEST['post_ids'] ) );
foreach( $post_ids as $post_id ) {
$urls[] = self::get_kml_attachment_urls( $post_id );
}
}
/** @noinspection ArgumentUnpackingCanBeUsedInspection */
$urls = call_user_func_array( 'array_merge', $urls );
$json = json_encode( $urls );
if ( isset( $_REQUEST['callback'] ) ) {
$json = esc_js( $_REQUEST['callback'] ) . '(' . $json . ')';
}
echo $json;
exit();
}
/**
* WordPress action to add relevant geo meta tags to the document head.
*
* wp_head {@link http://codex.wordpress.org/Plugin_API/Action_Reference#TemplateActions action},
* called by WordPress.
*
* @since 1.0
*/
public static function wp_head() {
global $wp_query;
if (is_single())
{
$loc = GeoMashupDB::get_object_location( 'post', $wp_query->post->ID );
if (!empty($loc)) {
$title = esc_html(
convert_chars(strip_tags(get_bloginfo('name')).' - '.$wp_query->post->post_title)
);
echo '<meta name="ICBM" content="' . esc_attr( $loc->lat . ', ' . $loc->lng ) . '" />' . "\n";
echo '<meta name="dcterms.title" content="' . esc_attr( $title ) . '" />' . "\n";
echo '<meta name="geo.position" content="' . esc_attr( $loc->lat . ';' . $loc->lng ) . '" />' . "\n";
}
}
}
/**
* Query object locations and return JSON.
*
* Offers customization per object location via a filter, geo_mashup_locations_json_object.
*
* @since 1.2
* @uses GeoMashupDB::get_object_locations()
* @uses apply_filters() the_title Filter post titles.
* @uses apply_filters() geo_mashup_locations_json_object Filter each location associative array before conversion to JSON.
* @global array $geo_mashup_options
*
* @param string|array $query_args Query variables for GeoMashupDB::get_object_locations().
* @param string $format (optional) 'JSON' (default) or ARRAY_A
* @return string|array Queried object locations JSON ( { "object" : [...] } ).
*/
public static function get_locations_json( $query_args, $format = 'JSON' ) {
$default_args = array( 'object_name' => 'post' );
$query_args = wp_parse_args( $query_args, $default_args );
$json_objects = array();
$objects = GeoMashupDB::get_object_locations( $query_args );
if ( $objects ) {
foreach ($objects as $object) {
$obj = self::augment_map_object_location( $query_args['object_name'], $object );
if ($obj) {
$json_objects[] = $obj;
}
}
}
if ( ARRAY_A === $format ) {
return array( 'objects' => $json_objects );
}
return json_encode( array( 'objects' => $json_objects ) );
}
/**
* Augment an object location for display.
*
* Adds term data, object type, author, and label.
* @since 1.5
*
* @param string $object_name The object type, e.g. 'post', 'user', etc.
* @param object $object_location The object location data.
* @return array The augmented object location data.
*/
private static function augment_map_object_location( $object_name, $object_location ) {
global $geo_mashup_options;
$term_ids_by_taxonomy = array();
$author_name = '';
if ( 'post' === $object_name ) {
// Filter the title
$object_location->label = sanitize_text_field( apply_filters( 'the_title', $object_location->label, $object_location->object_id ) );
// Add terms
if ( defined( 'GEO_MASHUP_DISABLE_CATEGORIES' ) && GEO_MASHUP_DISABLE_CATEGORIES ) {
$include_taxonomies = array();
} else {
$include_taxonomies = $geo_mashup_options->get( 'overall', 'include_taxonomies' );
}
foreach( $include_taxonomies as $include_taxonomy ) {
$term_ids_by_taxonomy[$include_taxonomy] = array();
// Not using wp_get_object_terms(), which doesn't allow for persistent caching
$tax_terms = get_the_terms( $object_location->object_id, $include_taxonomy );
if ( $tax_terms ) {
// terms are sometimes indexed in order, sometimes by id, so wp_list_pluck() doesn't work
foreach ( $tax_terms as $term ) {
$term_ids_by_taxonomy[$include_taxonomy][] = $term->term_id;
}
}
}
// Add post author name
if ( defined( 'GEO_MASHUP_DISABLE_AUTHOR_NAME' ) && GEO_MASHUP_DISABLE_AUTHOR_NAME ) {
$author = null;
} else {
$author = get_userdata( $object_location->post_author );
}
$author_name = empty( $author ) ? '' : $author->display_name;
}
$augmented_object = array(
'object_name' => $object_name,
'object_id' => $object_location->object_id,
// We should be able to use real UTF-8 characters in titles
// Helps with the spelling-out of entities in tooltips
'title' => html_entity_decode( $object_location->label, ENT_COMPAT, 'UTF-8' ),
'lat' => $object_location->lat,
'lng' => $object_location->lng,
'author_name' => $author_name,
'terms' => $term_ids_by_taxonomy,
);
// Allow companion plugins to add data with legacy filter name
return apply_filters( 'geo_mashup_locations_json_object', $augmented_object, $object_location );
}
/**
* Build the data for a javascript map.
*
* Parameters are used both to retrieve data and as options to
* eventually pass to the javascript.
*
* @since 1.4
* @uses GeoMashup::get_locations_json()
*
* @global array $geo_mashup_options
* @global object $geo_mashup_custom
* @param array $query Query parameters
* @return array|WP_Error Map data ready to be rendered, or a message
*/
public static function build_map_data( $query ) {
global $geo_mashup_options, $geo_mashup_custom;
$defaults = array(
'map_api' => $geo_mashup_options->get( 'overall', 'map_api' )
);
$query = wp_parse_args( $query, $defaults );
$object_id = isset( $query['object_id'] ) ? $query['object_id'] : 0;
unset( $query['object_id'] );
$map_data = $query + array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'siteurl' => home_url( '/' ), // qTranslate doesn't work with get_option( 'home' )
'url_path' => GEO_MASHUP_URL_PATH,
'template_url_path' => get_stylesheet_directory_uri()
);
if ( $geo_mashup_custom !== null ) {
$map_data['custom_url_path'] = $geo_mashup_custom->url_path;
}
$map_content = isset( $query['map_content'] ) ? $query['map_content'] : null;
$object_name = isset( $query['object_name'] ) ? $query['object_name'] : 'post';
if ( $map_content === 'single') {
$object_location = GeoMashupDB::get_object_location( $object_name, $object_id );
if ( !empty( $object_location ) ) {
$augmented_location = self::augment_map_object_location( $object_name, $object_location );
$map_data['object_data'] = array( 'objects' => array( $augmented_location ) );
}
$options = $geo_mashup_options->get( 'single_map' );
$map_data = array_merge ( $options, $map_data );
if ( 'post' === $object_name ) {
$kml_urls = self::get_kml_attachment_urls( $object_id );
if (count($kml_urls)>0) {
$map_data['load_kml'] = array_pop( $kml_urls );
}
}
} else { // $map_content != 'single'
$map_data['context_object_id'] = $object_id;
if ( $map_content === 'contextual' ) {
$options = $geo_mashup_options->get( 'context_map' );
// If desired we could make these real options
$options['auto_info_open'] = 'false';
} else { // $map_content == 'global'
$options = $geo_mashup_options->get( 'global_map' );
// Term options handled during render
unset( $options['term_options'] );
if ( empty( $query['show_future'] ) ) {
$query['show_future'] = $options['show_future'];
}
if ( $map_content === null ) {
$options['map_content'] = 'global';
}
}
// Determine which taxonomies to include, if any
if ( defined( 'GEO_MASHUP_DISABLE_CATEGORIES' ) && GEO_MASHUP_DISABLE_CATEGORIES ) {
$options['include_taxonomies'] = array();
} else {
$options['include_taxonomies'] = $geo_mashup_options->get(
'overall',
'include_taxonomies'
);
}
if ( isset( $options['add_google_bar'] ) && 'true' === $options['add_google_bar'] ) {
$options['adsense_code'] = $geo_mashup_options->get( 'overall', 'adsense_code' );
}
// We have a lot map control parameters that don't effect the locations query,
// but only the relevant ones are used
$map_data['object_data'] = self::get_locations_json( $query, ARRAY_A );