This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
simple-google-map-short-code.php
176 lines (137 loc) · 4.67 KB
/
simple-google-map-short-code.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
<?php
/*
Plugin Name: Simple Google Maps Short Code
Plugin URL: http://pippinsplugins.com/simple-google-maps-short-code
Description: Adds a simple Google Maps short code
Version: 1.1.2
Author: Pippin Williamson
Author URI: http://pippinsplugins.com
Contributors: mordauk
*/
/**
* Displays the map
*
* @access private
* @since 1.0
* @return void
*/
function pw_map_shortcode( $atts ) {
$atts = shortcode_atts(
array(
'address' => false,
'width' => '100%',
'height' => '400px',
'enablescrollwheel' => 'true',
'zoom' => 15,
'disablecontrols' => 'false'
),
$atts
);
$address = $atts['address'];
if( $address ) if( $address && wp_script_is( 'google-maps-api', 'registered' ) ) :
wp_print_scripts( 'google-maps-api' );
$coordinates = pw_map_get_coordinates( $address );
if( !is_array( $coordinates ) )
return;
$map_id = uniqid( 'pw_map_' ); // generate a unique ID for this map
ob_start(); ?>
<div class="pw_map_canvas" id="<?php echo esc_attr( $map_id ); ?>" style="height: <?php echo esc_attr( $atts['height'] ); ?>; width: <?php echo esc_attr( $atts['width'] ); ?>"></div>
<script type="text/javascript">
var map_<?php echo $map_id; ?>;
function pw_run_map_<?php echo $map_id ; ?>(){
var location = new google.maps.LatLng("<?php echo $coordinates['lat']; ?>", "<?php echo $coordinates['lng']; ?>");
var map_options = {
zoom: <?php echo $atts['zoom']; ?>,
center: location,
scrollwheel: <?php echo 'true' === strtolower( $atts['enablescrollwheel'] ) ? '1' : '0'; ?>,
disableDefaultUI: <?php echo 'true' === strtolower( $atts['disablecontrols'] ) ? '1' : '0'; ?>,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map_<?php echo $map_id ; ?> = new google.maps.Map(document.getElementById("<?php echo $map_id ; ?>"), map_options);
var marker = new google.maps.Marker({
position: location,
map: map_<?php echo $map_id ; ?>
});
}
pw_run_map_<?php echo $map_id ; ?>();
</script>
<?php
return ob_get_clean();
else :
return __( 'This Google Map cannot be loaded because the maps API does not appear to be loaded', 'pw-maps' );
endif;
}
add_shortcode( 'pw_map', 'pw_map_shortcode' );
/**
* Loads Google Map API
*
* @access private
* @since 1.0
* @return void
*/
function pw_map_load_scripts() {
wp_register_script( 'google-maps-api', '//maps.google.com/maps/api/js?sensor=false' );
}
add_action( 'wp_enqueue_scripts', 'pw_map_load_scripts' );
/**
* Retrieve coordinates for an address
*
* Coordinates are cached using transients and a hash of the address
*
* @access private
* @since 1.0
* @return void
*/
function pw_map_get_coordinates( $address, $force_refresh = false ) {
$address_hash = md5( $address );
$coordinates = get_transient( $address_hash );
if ($force_refresh || $coordinates === false) {
$args = array( 'address' => urlencode( $address ), 'sensor' => 'false' );
$url = add_query_arg( $args, 'http://maps.googleapis.com/maps/api/geocode/json' );
$response = wp_remote_get( $url );
if( is_wp_error( $response ) )
return;
$data = wp_remote_retrieve_body( $response );
if( is_wp_error( $data ) )
return;
if ( $response['response']['code'] == 200 ) {
$data = json_decode( $data );
if ( $data->status === 'OK' ) {
$coordinates = $data->results[0]->geometry->location;
$cache_value['lat'] = $coordinates->lat;
$cache_value['lng'] = $coordinates->lng;
$cache_value['address'] = (string) $data->results[0]->formatted_address;
// cache coordinates for 3 months
set_transient($address_hash, $cache_value, 3600*24*30*3);
$data = $cache_value;
} elseif ( $data->status === 'ZERO_RESULTS' ) {
return __( 'No location found for the entered address.', 'pw-maps' );
} elseif( $data->status === 'INVALID_REQUEST' ) {
return __( 'Invalid request. Did you enter an address?', 'pw-maps' );
} else {
return __( 'Something went wrong while retrieving your map, please ensure you have entered the short code correctly.', 'pw-maps' );
}
} else {
return __( 'Unable to contact Google API service.', 'pw-maps' );
}
} else {
// return cached results
$data = $coordinates;
}
return $data;
}
/**
* Fixes a problem with responsive themes
*
* @access private
* @since 1.0.1
* @return void
*/
function pw_map_css() {
echo '<style type="text/css">/* =Responsive Map fix
-------------------------------------------------------------- */
.pw_map_canvas img {
max-width: none;
}</style>';
}
add_action( 'wp_head', 'pw_map_css' );