-
Notifications
You must be signed in to change notification settings - Fork 0
/
toggle-maps.php
476 lines (418 loc) · 17.1 KB
/
toggle-maps.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
<?php
/*
Plugin Name: Toggle Maps
Author: Kim Spasaro
Version: 1.0.0
*/
function toggle_maps_register_scripts(){
wp_register_style( 'toggle_maps', plugins_url('/toggle-maps.css', __FILE__));
if(get_option('toggle_maps_google_key'))
wp_register_script('google_maps', 'http://maps.googleapis.com/maps/api/js?key='.get_option('toggle_maps_google_key'));
else
wp_register_script('google_maps', 'http://maps.googleapis.com/maps/api/js');
wp_register_script( 'toggle_maps', plugins_url('/toggle-maps.js', __FILE__));
}
add_action('wp_enqueue_scripts', 'toggle_maps_register_scripts');
// Create toggle maps shortcode and enqueue scripts
function toggle_maps_handler($atts){
$post = get_post($atts['id']);
// Check if we're mapping a single location
if($post->post_type == 'toggle_maps_location')
$single = true;
else if($post->post_type == 'toggle_maps_map')
$single = false;
else
return;
$terms = wp_get_post_terms($post->ID, 'toggle_maps_category');
$term_slugs = [];
$term_names = [];
foreach($terms as $term){
$term_slugs[$term->name] = $term->slug;
$term_names[$term->slug] = $term->name;
}
get_option('toggle_maps_port') ? $port = get_option('toggle_maps_port') : $port = null;
get_option('toggle_maps_path') ? $path = get_option('toggle_maps_path') : $path = null;
$map_vals = array(
'single' => $single,
'id' => $post->ID,
'terms' => $terms,
'termSlugs' => $term_slugs,
'termNames' => $term_names,
'port' => $port,
'path' => $path
);
// Localize toggle maps script
wp_enqueue_script('jquery');
wp_enqueue_style( 'toggle_maps');
wp_enqueue_script('google_maps');
wp_enqueue_script( 'toggle_maps');
wp_localize_script( 'toggle_maps', 'toggleMaps', $map_vals );
return '<div id="toggle-map"></div>';
}
add_shortcode('toggle-maps', 'toggle_maps_handler');
// Create toggle maps shortcode and enqueue scripts
function toggle_maps_list_handler($atts){
$post = get_post($atts['id']);
// Check if we're mapping a single location
if($post->post_type != 'toggle_maps_map')
return;
$terms = wp_get_post_terms($post->ID, 'toggle_maps_category');
ob_start();
?>
<div class="toggle-maps-list">
<?php foreach($terms as $term): ?>
<?php $tax_query = array(array('taxonomy' => 'toggle_maps_category', 'terms' => $term->term_id)); ?>
<?php $query = new WP_Query(array('tax_query' => $tax_query, 'post_type' => 'toggle_maps_location')); ?>
<h2><?php echo $term->name; ?></h2>
<?php while($query->have_posts()): $query->the_post(); ?>
<div class="toggle-maps-location">
<a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
<?php if(get_post_meta(get_the_ID(), 'toggle_maps_address', true)): ?>
<p><?php echo get_post_meta(get_the_ID(), 'toggle_maps_address', true); ?><p>
<?php endif; ?>
<?php if(get_post_meta(get_the_ID(), 'toggle_maps_phone', true)): ?>
<p><?php echo get_post_meta(get_the_ID(), 'toggle_maps_phone', true); ?></p>
<?php endif; ?>
<?php if(get_post_meta(get_the_ID(), 'toggle_maps_website_link', true)): ?>
<p><a href="<?php echo get_post_meta(get_the_ID(), 'toggle_maps_website_link', true); ?>"><?php the_title(); ?> Website</a></p>
<?php endif; ?>
</div>
<?php endwhile; ?>
<?php endforeach; ?>
</div>
<?php
return ob_get_clean();
}
add_shortcode('toggle-maps-list', 'toggle_maps_list_handler');
// Create toggle map post type
function create_toggle_maps_maps() {
register_post_type( 'toggle_maps_map',
array(
'labels' => array(
'name' => __( 'Toggle Maps' ),
'singular_name' => __( 'Toggle Map' )
),
'public' => true,
'has_archive' => true,
'show_in_rest' => true
)
);
}
add_action( 'init', 'create_toggle_maps_maps' );
// Create toggle location post type
function create_toggle_maps_locations() {
$rewrite = array('slug' => 'locations');
if(get_option('toggle_maps_rewrite'))
$rewrite = array('slug' => get_option('toggle_maps_rewrite'));
register_post_type( 'toggle_maps_location',
array(
'labels' => array(
'name' => __( 'Toggle Locations' ),
'singular_name' => __( 'Toggle Location' )
),
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'rewrite' => $rewrite,
)
);
}
add_action( 'init', 'create_toggle_maps_locations' );
function toggle_maps_admin_style () {
global $post_type;
if( 'toggle_maps_location' == $post_type )
wp_enqueue_style('toggle_maps', plugins_url('/toggle-maps-admin.css', __FILE__ ));
}
add_action( 'admin_print_scripts-post-new.php', 'toggle_maps_admin_style' );
add_action( 'admin_print_scripts-post.php', 'toggle_maps_admin_style' );
// Create custom plugin settings menu
function toggle_maps_create_menu() {
add_submenu_page(
'options-general.php',
'Toggle Maps',
'Toggle Maps',
'administrator',
'toggle_maps_options_page',
'toggle_maps_options_page'
);
//add_menu_page('Toggle Maps Settings', 'Toggle Maps Settings', 'administrator', __FILE__, 'toggle_maps_options_page' , plugins_url('/images/icon.png', __FILE__) );
add_action( 'admin_init', 'toggle_maps_register_options' );
}
add_action('admin_menu', 'toggle_maps_create_menu');
// Register settings
function toggle_maps_register_options() {
register_setting( 'toggle_maps_options_group', 'toggle_maps_google_key' );
register_setting( 'toggle_maps_options_group', 'toggle_maps_port' );
register_setting( 'toggle_maps_options_group', 'toggle_maps_path' );
register_setting( 'toggle_maps_options_group', 'toggle_maps_rewrite' );
}
// Create options page
function toggle_maps_options_page() {
?>
<div class="wrap">
<h2>Toggle Maps Options</h2>
<form method="post" action="options.php">
<?php settings_fields( 'toggle_maps_options_group' ); ?>
<?php do_settings_sections( 'toggle_maps_options_group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Google API Key</th>
<td><input type="text" class="large-text" name="toggle_maps_google_key" value="<?php echo esc_attr( get_option('toggle_maps_google_key') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Locations Slug Rewrite</th>
<td><input type="text" class="large-text" name="toggle_maps_rewrite" value="<?php echo esc_attr( get_option('toggle_maps_rewrite') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Port</th>
<td><input type="text" class="large-text" name="toggle_maps_port" value="<?php echo esc_attr( get_option('toggle_maps_port') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Path</th>
<td><input type="text" class="large-text" name="toggle_maps_path" value="<?php echo esc_attr( get_option('toggle_maps_path') ); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php }
// Create toggle location category taxonomy
function create_toggle_maps_categories(){
$labels = array(
'name' => _x( 'Toggle Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Toggle Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Toggle Categories' ),
'all_items' => __( 'All Toggle Categories' ),
'parent_item' => __( 'Parent Toggle Category' ),
'parent_item_colon' => __( 'Parent Toggle Category:' ),
'edit_item' => __( 'Edit Toggle Category' ),
'update_item' => __( 'Update Toggle Category' ),
'add_new_item' => __( 'Add New Toggle Category' ),
'new_item_name' => __( 'New Toggle Category Name' ),
'menu_name' => __( 'Toggle Category' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'map-categories' ),
);
register_taxonomy( 'toggle_maps_category', array('toggle_maps_map', 'toggle_maps_location'), $args );
}
add_action('init', 'create_toggle_maps_categories');
// Add the meta fields for locations
function add_toggle_maps_location_meta () {
add_meta_box ( 'toggle-maps-location-meta', 'Toggle Maps', 'toggle_maps_location_meta_callback', 'toggle_maps_location', 'normal');
}
add_action ( 'add_meta_boxes', 'add_toggle_maps_location_meta' );
// Add meta fields for maps
function add_toggle_maps_map_meta () {
add_meta_box ( 'toggle-maps-map-meta', 'Toggle Maps', 'toggle_maps_map_meta_callback', 'toggle_maps_map', 'normal');
}
add_action ( 'add_meta_boxes', 'add_toggle_maps_map_meta' );
// Initialize meta boxes with their saved values
function toggle_maps_location_meta_callback () {
global $post;
$address = get_post_meta($post->ID, 'toggle_maps_address', true);
echo '<label>Address</label>';
echo '<input type="text" name="toggle_maps_address" value="' . $address . '" class="widefat" />';
$phone = get_post_meta($post->ID, 'toggle_maps_phone', true);
echo '<label>Phone</label>';
echo '<input type="text" name="toggle_maps_phone" value="' . $phone . '" class="widefat" />';
$website_link = get_post_meta($post->ID, 'toggle_maps_website_link', true);
echo '<label>Website Link</label>';
echo '<input type="text" name="toggle_maps_website_link" value="' . $website_link . '" class="widefat" />';
$latitude = get_post_meta($post->ID, 'toggle_maps_latitude', true);
echo '<label>Latitude</label>';
echo '<input type="text" name="toggle_maps_latitude" value="' . $latitude . '" class="widefat" />';
$longitude = get_post_meta($post->ID, 'toggle_maps_longitude', true);
echo '<label>Longitude</label>';
echo '<input type="text" name="toggle_maps_longitude" value="' . $longitude . '" class="widefat" />';
$zoom = get_post_meta($post->ID, 'toggle_maps_zoom', true);
echo '<label>Zoom (default 14)</label>';
echo '<input type="text" name="toggle_maps_zoom" value="' . $zoom . '" class="widefat" />';
echo '<label>Shortcode</label>';
echo '<p>[toggle-maps id="'.$post->ID.'"]</p>';
}
// Initialize meta boxes with their saved values
function toggle_maps_map_meta_callback () {
global $post;
$latitude = get_post_meta($post->ID, 'toggle_maps_latitude', true);
echo '<label>Center Latitude</label>';
echo '<input type="text" name="toggle_maps_latitude" value="' . $latitude . '" class="widefat" />';
$longitude = get_post_meta($post->ID, 'toggle_maps_longitude', true);
echo '<label>Center Longitude</label>';
echo '<input type="text" name="toggle_maps_longitude" value="' . $longitude . '" class="widefat" />';
$zoom = get_post_meta($post->ID, 'toggle_maps_zoom', true);
echo '<label>Zoom</label>';
echo '<input type="text" name="toggle_maps_zoom" value="' . $zoom . '" class="widefat" />';
echo '<label>Map Shortcode</label>';
echo '<p>[toggle-maps id="'.$post->ID.'"]</p>';
echo '<label>List Shortcode</label>';
echo '<p>[toggle-maps-list id="'.$post->ID.'"]</p>';
}
// Save toggle maps meta
function toggle_maps_save_meta() {
global $post;
if($post->post_type == 'toggle_maps_location'){
$meta['toggle_maps_address'] = $_POST['toggle_maps_address'];
$meta['toggle_maps_phone'] = $_POST['toggle_maps_phone'];
$meta['toggle_maps_website_link'] = $_POST['toggle_maps_website_link'];
$meta['toggle_maps_latitude'] = $_POST['toggle_maps_latitude'];
$meta['toggle_maps_longitude'] = $_POST['toggle_maps_longitude'];
$meta['toggle_maps_zoom'] = $_POST['toggle_maps_zoom'];
if(!$meta['toggle_maps_zoom'])
$meta['toggle_maps_zoom'] = 14;
foreach ($meta as $key => $value) {
if(get_post_meta($post->ID, $key, FALSE))
update_post_meta($post->ID, $key, $value);
else
add_post_meta($post->ID, $key, $value);
if(!$value)
delete_post_meta($post->ID, $key);
}
}
else if($post->post_type == 'toggle_maps_map'){
$meta['toggle_maps_latitude'] = $_POST['toggle_maps_latitude'];
$meta['toggle_maps_longitude'] = $_POST['toggle_maps_longitude'];
$meta['toggle_maps_zoom'] = $_POST['toggle_maps_zoom'];
foreach ($meta as $key => $value) {
if(get_post_meta($post->ID, $key, FALSE))
update_post_meta($post->ID, $key, $value);
else
add_post_meta($post->ID, $key, $value);
if(!$value)
delete_post_meta($post->ID, $key);
}
}
}
add_action('save_post', 'toggle_maps_save_meta');
// Add toggle maps meta to API
function add_toggle_maps_data() {
// Location meta
register_api_field(
'toggle_maps_location',
'toggle_maps_address',
array(
'get_callback' => 'toggle_maps_get_field',
'update_callback' => null,
'schema' => array(
'description' => 'Location address',
'type' => 'string',
'context' => array('view', 'edit')
)
)
);
register_api_field(
'toggle_maps_location',
'toggle_maps_phone',
array(
'get_callback' => 'toggle_maps_get_field',
'update_callback' => null,
'schema' => array(
'description' => 'Location phone',
'type' => 'string',
'context' => array('view', 'edit')
)
)
);
register_api_field(
'toggle_maps_location',
'toggle_maps_website_link',
array(
'get_callback' => 'toggle_maps_get_field',
'update_callback' => null,
'schema' => array(
'description' => 'Location website',
'type' => 'string',
'context' => array('view', 'edit')
)
)
);
register_api_field(
'toggle_maps_location',
'toggle_maps_latitude',
array(
'get_callback' => 'toggle_maps_get_field',
'update_callback' => null,
'schema' => array(
'description' => 'Location latitude',
'type' => 'string',
'context' => array('view', 'edit')
)
)
);
register_api_field(
'toggle_maps_location',
'toggle_maps_longitude',
array(
'get_callback' => 'toggle_maps_get_field',
'update_callback' => null,
'schema' => array(
'description' => 'Location longitude',
'type' => 'string',
'context' => array('view', 'edit')
)
)
);
register_api_field(
'toggle_maps_location',
'toggle_maps_zoom',
array(
'get_callback' => 'toggle_maps_get_field',
'update_callback' => null,
'schema' => array(
'description' => 'Location zoom',
'type' => 'string',
'context' => array('view', 'edit')
)
)
);
// Maps meta
register_api_field(
'toggle_maps_map',
'toggle_maps_latitude',
array(
'get_callback' => 'toggle_maps_get_field',
'update_callback' => null,
'schema' => array(
'description' => 'Map center latitude',
'type' => 'string',
'context' => array('view', 'edit')
)
)
);
register_api_field(
'toggle_maps_map',
'toggle_maps_longitude',
array(
'get_callback' => 'toggle_maps_get_field',
'update_callback' => null,
'schema' => array(
'description' => 'Map center longitude',
'type' => 'string',
'context' => array('view', 'edit')
)
)
);
register_api_field(
'toggle_maps_map',
'toggle_maps_zoom',
array(
'get_callback' => 'toggle_maps_get_field',
'update_callback' => null,
'schema' => array(
'description' => 'Map zoom',
'type' => 'string',
'context' => array('view', 'edit')
)
)
);
}
add_action('rest_api_init', 'add_toggle_maps_data');
// Callback to display meta in API
function toggle_maps_get_field($post, $field_name, $request) {
return get_post_meta($post['id'], $field_name, true);
}