forked from wp-plugins/simple-google-maps-short-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-google-map-short-code.php
179 lines (144 loc) · 5.11 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
177
178
179
<?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.3.1
Author: Pippin Williamson
Author URI: http://pippinsplugins.com
Contributors: mordauk
*/
/**
* Loads the plugin textdomain
*
* @access private
* @since 1.2
* @return void
*/
function pw_map_textdomain() {
// Set filter for plugin's languages directory
$lang_dir = dirname( plugin_basename( __FILE__ ) ) . '/languages/';
$lang_dir = apply_filters( 'pw_map_languages_directory', $lang_dir );
// Load the translations
load_plugin_textdomain( 'simple-google-maps-short-code', false, $lang_dir );
}
add_action( 'init', 'pw_map_textdomain' );
/**
* 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',
'type' => 'ROADMAP', // or SATELLITE
'key' => ''
),
$atts
);
$address = $atts['address'];
wp_enqueue_script( 'google-maps-api', '//maps.google.com/maps/api/js?sensor=false&callback=initMap&key=' . sanitize_text_field( $atts['key'] ) );
if( $address ) :
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.<?php echo $atts['type']; ?>
}
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', 'simple-google-maps-short-code' );
endif;
}
add_shortcode( 'pw_map', 'pw_map_shortcode' );
/**
* 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 = apply_filters( 'pw_map_query_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.', 'simple-google-maps-short-code' );
} elseif( $data->status === 'INVALID_REQUEST' ) {
return __( 'Invalid request. Did you enter an address?', 'simple-google-maps-short-code' );
} else {
return __( 'Something went wrong while retrieving your map, please ensure you have entered the short code correctly.', 'simple-google-maps-short-code' );
}
} else {
return __( 'Unable to contact Google API service.', 'simple-google-maps-short-code' );
}
} 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' );