-
Notifications
You must be signed in to change notification settings - Fork 6
/
address-geocoder.php
275 lines (218 loc) · 9.57 KB
/
address-geocoder.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
<?php
/*
Plugin Name: Address Geocoder
Description: Attach location information to posts
Version: 1.0.1
Contributors: martyspellerberg, mgibbs189
Author: Marty Spellerberg
Author URI: http://martyspellerberg.com
License: GPLv2+
*/
class Address_Geocoder
{
public $available_post_types = array();
public $options;
function __construct() {
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
}
/**
* Initialize
*/
function admin_init() {
// Class properties
$post_types = get_post_types();
$excluded = array( 'attachment', 'revision', 'nav_menu_item' );
$this->available_post_types = array_diff( $post_types, $excluded );
$this->options = get_option( 'address_geocoder_options' );
// Set some default options if none are already set
if( ! $this->options ) {
$this->options = $this->available_post_types;
}
// Actions
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
// Settings
register_setting( 'address_geocoder_options', 'address_geocoder_options', array( $this, 'validate_options' ) );
}
/**
* Register the menu item under "Settings"
*/
function admin_menu() {
add_options_page( 'Address Geocoder', 'Address Geocoder', 'manage_options', 'address-geocoder-options', array( $this, 'options_page' ) );
}
/**
* Enqueue admin scripts
*/
function admin_enqueue_scripts() {
// Load scripts only when necessary
if ( $this->is_geocoder_needed() ) {
$address_geocoder_options = get_option('address_geocoder_options');
$apikey = $address_geocoder_options['apikey'];
if ( ! empty( $apikey ) ) {
$mapsapi = '//maps.googleapis.com/maps/api/js?key=' . $apikey;
wp_register_script( 'googlemaps', $mapsapi );
wp_register_script( 'marty_geocode_js', plugins_url( '/address-geocoder.js', __FILE__ ) );
wp_enqueue_script( 'googlemaps' );
wp_enqueue_script( 'marty_geocode_js' );
}
}
}
/**
* Determine whether the geocoder metabox appears on the current page
*/
function is_geocoder_needed() {
$pagenow = isset( $GLOBALS['pagenow'] ) ? $GLOBALS['pagenow'] : '';
if ( 'post.php' == $pagenow ) {
$post_type = get_post_type( $_GET['post'] );
}
elseif ( 'post-new.php' == $pagenow ) {
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
}
if ( !empty( $post_type ) && ! empty( $this->options ) ) {
if ( in_array( $post_type, $this->available_post_types ) && 'exclude' != $this->options[ $post_type ] ) {
return true;
}
}
return false;
}
/**
* Add the "Geocoder" meta box to the appropriate pages
*/
function add_meta_boxes( $post_type ) {
if ( in_array( $post_type, $this->available_post_types ) && 'exclude' != $this->options[ $post_type ] ) {
$current_post_type = get_post_type_object( $post_type );
$metabox_title = empty( $this->options['meta-box-title'] ) ? $current_post_type->labels->singular_name . ' Location' : $this->options['meta-box-title'];
add_meta_box( 'martygeocoder', $metabox_title, array( $this, 'meta_box_html' ), $post_type, 'normal', 'high' );
}
}
/**
* Generate the meta box HTML
*/
function meta_box_html( $object, $box ) {
wp_nonce_field( 'save_latlng', 'geocoder_nonce' );
$address_geocoder_options = get_option('address_geocoder_options');
$apikey = $address_geocoder_options['apikey'];
if ( $apikey && $apikey != '' ): ?>
<div style="overflow:hidden; width:100%">
<div id="geocodepreview" style="float:right; width:240px; height:240px; border:1px solid #DFDFDF"></div>
<div style="margin-right:260px">
<p>
<label for="martygeocoderaddress">Address</label><br />
<input class="widefat" type="text" name="martygeocoderaddress" id="martygeocoderaddress" value="<?php echo esc_attr( get_post_meta( $object->ID, 'martygeocoderaddress', true ) ); ?>" />
</p>
<p>
<label for="martygeocoderlatlng">Lat/Lng</label><br />
<input class="widefat" type="text" name="martygeocoderlatlng" id="martygeocoderlatlng" value="<?php echo esc_attr( get_post_meta( $object->ID, 'martygeocoderlatlng', true ) ); ?>" />
</p>
<p>
<a id="geocode" class="button">Geocode Address</a>
</p>
</div>
</div>
<?php else : ?>
<p>A Google Maps API Key is required. <a href="options-general.php?page=address-geocoder-options">Go to Settings.</a></p>
<?php endif;
}
/**
* Generate the options page HTML
*/
function options_page() {
if ( ! isset( $_REQUEST['settings-updated'] ) ) {
$_REQUEST['settings-updated'] = false;
}
$apikey = empty( $this->options['apikey'] ) ? '' : $this->options['apikey'];
?>
<div class="wrap">
<h1><?php _e( 'Address Geocoder' ); ?></h1>
<form method="post" action="options.php">
<?php settings_fields( 'address_geocoder_options' ); ?>
<h3>Google Maps API Key</h3>
<p>Address Geocoder requires a Google Maps API Key to work. You can get a free API Key here: <a href="https://developers.google.com/maps/documentation/javascript/tutorial#api_key">Instructions for obtaining a key.</a></p>
<p><input type="input" id="geocoder-apikey" name="address_geocoder_options[apikey]" value="<?php echo esc_attr( $apikey ); ?>" /><br />
<label class="description" for="geocoder-apikey">Your API Key</label></p>
<h3>Show Metabox on Post Types</h3>
<?php foreach ( $this->options as $post_type => $status ) : ?>
<?php if ( $post_type != 'apikey') : ?>
<?php $checked = ( 'exclude' != $status ) ? ' checked="checked"' : ''; ?>
<p>
<input type="checkbox" id="geocoder-type-<?php echo $post_type; ?>" name="address_geocoder_options[<?php echo $post_type ?>]" value="enabled" <?php echo $checked; ?> />
<label class="description" for="geocoder-type-<?php echo $post_type; ?>"><?php echo $post_type; ?></label>
</p>
<?php endif; ?>
<?php endforeach; ?>
<h3>Meta Box Title</h3>
<p><input type="input" id="geocoder-meta-box-title" name="address_geocoder_options[meta-box-title]" value="<?php echo empty( $this->options['meta-box-title'] ) ? '' : esc_attr( $this->options['meta-box-title'] ); ?>" /><br>
<label class="description" for="geocoder-meta-box-title">Custom Meta Box Title</label></p>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( 'Save Options' ); ?>" />
</p>
</form>
</div>
<?php
}
/**
* Validate the options
*/
function validate_options( $input ) {
foreach ( $this->available_post_types as $post_type ) {
if ( ! isset( $input[ $post_type ] ) ) {
$input[ $post_type ] = 'exclude';
}
}
if ( isset( $input['meta-box-title'] ) ) {
$input['meta-box-title'] = sanitize_text_field( $input['meta-box-title'] );
}
return $input;
}
/**
* Attach geocode data to posts
*/
function save_post( $post_id, $post ) {
// Skip when nonce isn't present
if ( ! isset( $_POST['geocoder_nonce'] ) || ! wp_verify_nonce( $_POST['geocoder_nonce'], 'save_latlng' ) ) {
return $post_id;
}
// Ensure proper access rights
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
// Save address
$address_key = 'martygeocoderaddress';
$address = isset( $_POST[ $address_key ] ) ? sanitize_text_field( $_POST[ $address_key ] ) : '';
if ( empty( $address ) ) {
delete_post_meta( $post_id, $address_key );
}
else {
update_post_meta( $post_id, $address_key, $address );
}
// Save lat/lng
$latlng_key = 'martygeocoderlatlng';
$latlng = isset( $_POST[ $latlng_key ] ) ? sanitize_text_field( $_POST[ $latlng_key ] ) : '';
if ( empty( $latlng ) ) {
delete_post_meta( $post_id, $latlng_key );
}
else {
update_post_meta( $post_id, $latlng_key, $latlng );
}
}
}
$address_geocoder = new Address_Geocoder();
// Backwards compatibility
function get_geocode_latlng( $post_id ) {
return get_post_meta( $post_id, 'martygeocoderlatlng', true );
}
function get_geocode_lat( $post_id ) {
$latlng = get_post_meta( $post_id, 'martygeocoderlatlng', true );
$latlng = explode( ',', $latlng );
return substr( $latlng[0], 1 );
}
function get_geocode_lng( $post_id ) {
$latlng = get_post_meta( $post_id, 'martygeocoderlatlng', true );
$latlng = explode( ',', $latlng );
return substr( $latlng[1], 0, -1 );
}
function get_geocode_address( $post_id ) {
return get_post_meta( $post_id, 'martygeocoderaddress', true );
}