-
Notifications
You must be signed in to change notification settings - Fork 0
/
wheresmybus.php
88 lines (73 loc) · 1.95 KB
/
wheresmybus.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
<?php
/**
* Plugin Name: Where's My Bus
* Plugin URI: https://github.com/buspreneurs/wheresmybus
* Description: StartupBus tracking for en route busses
* Version: 0.1
* Author: Gergely Imreh <[email protected]>
* Author URI: https://gergely.imreh.net
* License: BSD
*/
/* API endpoint */
add_action( 'init', 'add_endpoint');
function add_endpoint() {
add_rewrite_endpoint( 'tracker', EP_ROOT );
}
add_action( 'template_redirect', 'template_redirect' );
function template_redirect() {
global $wp_query;
if(!isset( $wp_query->query['tracker'] )) {
return $templates;
}
$out = array();
switch (get_query_var('tracker')) {
case 'config':
$out = create_config();
break;
case 'location':
$out['location'] = True;
break;
default:
$out['error'] = True;
break;
}
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($out);
}
/**
* Create configuration to return over the API,
* that the bus tracker app can use
*/
function create_config() {
global $post;
$args = array(
'post_type' => 'gavern_buses',
'post_status' => 'publish'
);
$query = new WP_Query( $args );
$out = array( 'endpoint' => get_bloginfo('url').'/tracker/location/' );
$buses = array();
while ( $query->have_posts() ) {
$query->the_post();
$bus = array("name"=>$post->post_title,"id"=>$post->ID);
$buses[] = $bus;
}
$out["buses"] = $buses;
wp_reset_postdata();
return $out;
}
/************/
/* Admin interface */
add_action( 'admin_menu', 'wheresmybus_menu' );
function wheresmybus_menu() {
add_options_page( 'Where\'s My Bus Options', 'Where\'s My Bus', 'manage_options', 'wheresmybus', 'wheresmybus_options' );
}
function wheresmybus_options() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
echo '<div class="wrap">';
echo '<p>Here is where the form would go if I actually had options.</p>';
echo '</div>';
}
?>