-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathso-mobile-map-widget.php
202 lines (170 loc) · 9.63 KB
/
so-mobile-map-widget.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
<?php /*
Plugin Name: Mobile Map Widget
Plugin URI: https://so-wp.com/plugin/mobile-map-widget
Description: This widget adds a mobile-optimised Google Static Maps Image with a colored pin centered on a destination of your choosing. Once clicked it opens the Google mobile maps website where you can fill in your Current Location if it is not already there. Then you can see the directions from your location to the destination as well as the map with the route of your choice. Optimised for mobile use. Google Static Maps API-key is required.
Version: 20182.4.2
Author: SO WP
Author URI: https://so-wp.com
Text Domain: so-mobile-map-widget
Domain Path: /languages
*/
/** Prevent direct access to files */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/** add plugin textdomain */
function so_mmw_init() {
load_plugin_textdomain( 'so-mobile-map-widget', false, basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'so_mmw_init' );
class SO_MobileMapWidget extends WP_Widget {
/**
* constructor
* @modified 2015.7.7 parent::WP_Widget() deprecated since WP 4.3
* more info: https://gist.github.com/chriscct7/d7d077afb01011b1839d
*
* @modified 2016.3.31 implement selective refresh support for Customizer (WP 4.5 feature)
* more info: https://make.wordpress.org/core/2016/03/22/implementing-selective-refresh-support-for-widgets/
*/
function __construct() {
$widget_ops = array(
'description' => __( 'This widget adds a mobile-optimised Google Static Map Image with a colored pin centered on a destination of your choosing.', 'so-mobile-map-widget' ),
// since 2016.3.31
'customize_selective_refresh' => true
);
parent::__construct( false, __( 'Mobile Map Widget', 'so-mobile-map-widget' ), $widget_ops );
}
/**
* fetch coordinates based on the address and add transient
* snippet copied from Google Maps Widget - https://www.googlemapswidget.com/
*
* since 2014.07.30
*/
static function get_coordinates( $address, $force_refresh = false ) {
$address_hash = md5( 'SO_MobileMapWidget' . $address );
if ( $force_refresh || ( $coordinates = get_transient( $address_hash ) ) === false ) {
$url = 'https://maps.googleapis.com/maps/api/geocode/xml?address=' . urlencode( $address );
$result = wp_remote_get( $url );
if ( ! is_wp_error( $result ) && $result['response']['code'] == 200) {
$data = new SimpleXMLElement( $result['body'] );
if ( $data->status == 'OK' ) {
$cache_value['lat'] = (string) $data->result->geometry->location->lat;
$cache_value['lng'] = (string) $data->result->geometry->location->lng;
$cache_value['address'] = (string) $data->result->formatted_address;
// cache coordinates for 3 months
set_transient( $address_hash, $cache_value, 3600*24*30*3 );
$data = $cache_value;
} elseif ( ! $data->status ) {
return false;
} else {
return false;
}
} else {
return false;
}
} else {
// data is cached, get it
$data = get_transient( $address_hash );
}
return $data;
} // get_coordinates
/** @see WP_Widget::widget */
function widget( $args, $instance ) {
extract( $args );
$address = '';
$coordinates = SO_MobileMapWidget::get_coordinates( $instance['address'] );
if ( $coordinates ) {
$address = $coordinates['lat'] . ',' . $coordinates['lng'];
}
// these are our widget options
$title = apply_filters( 'widget_title', $instance['title'] );
$address = isset( $instance['address'] ) ? esc_attr( $instance['address'] ) : '';
$color = isset( $instance['color'] ) ? esc_attr( $instance['color'] ) : '';
$zoom = isset( $instance['zoom'] ) ? esc_attr( $instance['zoom'] ) : '';
$width = isset( $instance['width'] ) ? esc_attr( $instance['width'] ) : '';
$height = isset( $instance['height'] ) ? esc_attr( $instance['height'] ) : '';
$apikey = isset( $instance['apikey'] ) ? esc_attr( $instance['apikey'] ) : '';
$description = isset( $instance['description'] ) ? esc_attr( $instance['description'] ) : '';
echo $before_widget;
if ( $title ) {
echo $before_title . $title . $after_title;
}
// 2014.07.30 sensor parameter no longer needed: developers.google.com/maps/documentation/staticmaps/#Moreinfo
echo '<div class="so_mmw_widget_content">';
echo '<a href="https://maps.google.com/maps?daddr=' . urlencode( $instance['address'] ) . '&markers=color:' . $color . '%7C' . urlencode( $instance['address'] ) . '" target="_blank"><img src="https://maps.googleapis.com/maps/api/staticmap?center=' . urlencode($instance['address']) . '&zoom=' . $zoom . '&size=' . $width . 'x' . $height . '&format=jpg&scale=2&markers=color:' . $color . '%7C' . urlencode( $instance['address'] );
if( $apikey && $apikey != '' )
echo '&key=' . $apikey;
echo '" width="' . $width . '" height="' . $height . '" alt="" /></a>';
if ( $description != '' ) {
echo '<div class="description">' . $description . '</div>';
}
echo '</div>';
echo $after_widget;
}
/** @see WP_Widget::update */
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['address'] = strip_tags( $new_instance['address'] );
$instance['color'] = strip_tags( $new_instance['color'] );
$instance['zoom'] = strip_tags( $new_instance['zoom'] );
$instance['width'] = strip_tags( $new_instance['width'] );
$instance['height'] = strip_tags( $new_instance['height'] );
$instance['apikey'] = strip_tags( $new_instance['apikey'] );
$instance['description'] = strip_tags( $new_instance['description'] );
return $instance;
}
/** @see WP_Widget::form */
function form( $instance ) {
/* Set up some default widget settings. */
$defaults = array(
'title' => 'Title (optional)',
'address' => 'Flamengos, Horta, Faial, Açores, Portugal',
'color' => 'orange',
'zoom' => 2,
'width' => 250,
'height' => 250,
'apikey' => '',
'description' => 'Description (optional)'
);
$instance = wp_parse_args( (array) $instance, $defaults );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title (optional):', 'so-mobile-map-widget' ); ?></label>
<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'address' ); ?>"><?php _e( 'Fill in the exact address of the location you want to show on the map; also takes coordinates, which you can check <a href="https://www.latlong.net/convert-address-to-lat-long.html" target="_blank">here</a>.', 'so-mobile-map-widget' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'address' ); ?>" name="<?php echo $this->get_field_name( 'address' ); ?>" type="text" value="<?php echo esc_attr( $instance['address'] ); ?>" /></p>
<p>
<label for="<?php echo $this->get_field_id( 'color' ); ?>"><?php _e( 'Color, choose from black, brown, green, purple, yellow, blue, gray, orange, red, white.', 'so-mobile-map-widget' ); ?></label>
<input type="text" name="<?php echo $this->get_field_name( 'color' ); ?>" value="<?php echo $instance['color']; ?>" class="widefat" id="<?php echo $this->get_field_id( 'color' ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'zoom' ); ?>"><?php _e( 'Zoom level, from 0 (world view) to 21 (streetview); from 15 and up seems to be good for locations in larger cities, but best to check and play around with it a bit.', 'so-mobile-map-widget' ); ?></label>
<input type="number" name="<?php echo $this->get_field_name( 'zoom' ); ?>" value="<?php echo $instance['zoom']; ?>" class="widefat" id="<?php echo $this->get_field_id( 'zoom' ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'width' ); ?>"><?php _e( 'Width in px (max 640):', 'so-mobile-map-widget' ); ?></label>
<input type="number" name="<?php echo $this->get_field_name( 'width' ); ?>" value="<?php echo $instance['width']; ?>" class="widefat" id="<?php echo $this->get_field_id( 'width' ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'height' ); ?>"><?php _e( 'Height in px (max 640):', 'so-mobile-map-widget' ); ?></label>
<input type="number" name="<?php echo $this->get_field_name( 'height' ); ?>" value="<?php echo $instance['height']; ?>" class="widefat" id="<?php echo $this->get_field_id( 'height' ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'apikey' ); ?>"><?php _e( 'Google Static Maps API Key (<strong>REQUIRED</strong>, get it <a href="https://cloud.google.com/maps-platform/#get-started" target="_blank">here</a>):', 'so-mobile-map-widget' ); ?></label>
<input type="text" name="<?php echo $this->get_field_name( 'apikey' ); ?>" value="<?php echo $instance['apikey']; ?>" class="widefat" id="<?php echo $this->get_field_id( 'apikey' ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'description' ); ?>"><?php _e( 'Description shows under map image (optional):', 'so-mobile-map-widget' ); ?></label>
<input type="textarea" name="<?php echo $this->get_field_name( 'description' ); ?>" value="<?php echo $instance['description']; ?>" class="widefat" id="<?php echo $this->get_field_id( 'description' ); ?>" />
</p>
<p>
<i><?php printf( __( 'If you like the Mobile Map Widget, please consider leaving a <a href="%1$s">review</a> or making a <a href="%2$s">donation</a>. Thanks!', 'so-mobile-map-widget'), 'https://wordpress.org/support/plugin/so-mobile-map-widget/reviews/?rate=5#new-post', 'https://so-wp.com/donations' ); ?></i>
</p>
<?php }
}
// Register widget
function so_mmw_init_widget () {
return register_widget( 'SO_MobileMapWidget' );
}
add_action ( 'widgets_init', 'so_mmw_init_widget' );