Skip to content

Commit 3d5271b

Browse files
authored
Merge pull request #29 from stopfstedt/api
API endpoint for library hours.
2 parents 729396a + 9713aa4 commit 3d5271b

File tree

5 files changed

+120
-8
lines changed

5 files changed

+120
-8
lines changed

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,65 @@ The short code has the following configuration options.
3434

3535
`[wplibcalhours location="Parnassus Library" num_weeks=1]` - Prints the opening hours for the "Parnassus Library" for the next week (starting today).
3636

37+
## API
38+
39+
This plugin also exposes opening hours via a read-only, public API endpoint.
40+
41+
Requesting the path `/wp-admin/admin-ajax.php?action=wplibcalhours` on your site will yield
42+
a JSON-formatted payload comprised of dates and opening hours, keyed by their location names.
43+
44+
These lists are sorted in chronological order and contain day-of-the-week, date and the day's opening hours information.
45+
46+
Each list starts with the current date.
47+
48+
#### Example payload
49+
50+
Three locations, the current date is January 3rd.
51+
52+
```json
53+
{
54+
"Parnassus Library": [
55+
{
56+
"day": "Wed",
57+
"date": "Jan 3",
58+
"text": "7:45am - 10pm"
59+
},
60+
{
61+
"day": "Thu",
62+
"date": "Jan 4",
63+
"text": "7:45am - 10pm"
64+
}
65+
//...
66+
],
67+
"Mission Bay Library": [
68+
{
69+
"day": "Wed",
70+
"date": "Jan 3",
71+
"text": "9am - 6pm"
72+
},
73+
{
74+
"day": "Thu",
75+
"date": "Jan 4",
76+
"text": "9am - 6pm"
77+
}
78+
// ...
79+
],
80+
"Mission Bay Hub": [
81+
{
82+
"day": "Wed",
83+
"date": "Jan 3",
84+
"text": "24 hours"
85+
},
86+
{
87+
"day": "Thu",
88+
"date": "Jan 4",
89+
"text": "24 hours"
90+
}
91+
// ...
92+
]
93+
}
94+
```
95+
3796
## Styling
3897

3998
Please see this [Wiki page](https://github.com/ucsf-ckm/wplibcalhours/wiki/Styling-The-Output) for ideas on how to customize the styles of this plugin's generated markup.

includes/class-wplibcalhours-client.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function __construct( $plugin_name, $version, $url ) {
6868
/**
6969
* Returns the hours from a given location.
7070
*
71-
* @param string $location The name of the location.
71+
* @param string $location The name of the location.
7272
* @param boolean $ignore_cache Set to TRUE to bypass cache.
7373
*
7474
* @return array
@@ -78,6 +78,23 @@ public function __construct( $plugin_name, $version, $url ) {
7878
* @since 1.0.0
7979
*/
8080
public function getHours( $location, $ignore_cache = false ) {
81+
$data = $this->getRawData( $ignore_cache );
82+
83+
return $this->extractTimetableForLocation( $location, $data );
84+
}
85+
86+
/**
87+
* Returns the raw data as consumed from the LibCal API.
88+
*
89+
* @param bool $ignore_cache Set to TRUE to bypass cache.
90+
*
91+
* @return array
92+
*
93+
* @throws \Exception
94+
*
95+
* @since 1.1.0
96+
*/
97+
public function getRawData( $ignore_cache = false ) {
8198
$data = false;
8299
$transient_key = $this->plugin_name . '_data';
83100
if ( ! $ignore_cache ) {
@@ -91,7 +108,7 @@ public function getHours( $location, $ignore_cache = false ) {
91108
}
92109
}
93110

94-
return $this->extractTimetableForLocation( $location, $data );
111+
return $data;
95112
}
96113

97114
/**
@@ -127,7 +144,7 @@ protected function fetchHoursFromAPI() {
127144
* Extracts the hours for a given location from the given data set.
128145
*
129146
* @param string $location The name of the location.
130-
* @param array $data The entire location/hours data set.
147+
* @param array $data The entire location/hours data set.
131148
*
132149
* @return array The hours for the given location.
133150
*

includes/class-wplibcalhours.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class WpLibCalHours {
8787
public function __construct() {
8888

8989
$this->plugin_basename = plugin_basename( plugin_dir_path( __DIR__ ) . self::PLUGIN_NAME . '.php' );
90-
$this->version = '1.0.0';
90+
$this->version = '1.1.0';
9191

9292
$this->load_dependencies();
9393
$this->set_locale();
@@ -178,7 +178,8 @@ private function define_admin_hooks() {
178178

179179
$this->loader->add_action( 'admin_menu', $plugin_admin, 'add_options_page' );
180180
$this->loader->add_action( 'admin_init', $plugin_admin, 'register_setting' );
181-
$this->loader->add_action( 'update_option_' . self::PLUGIN_NAME . '_ignore_cache', $plugin_admin, 'update_option_ignore_cache', null, 0 );
181+
$this->loader->add_action( 'update_option_' . self::PLUGIN_NAME . '_ignore_cache', $plugin_admin,
182+
'update_option_ignore_cache', null, 0 );
182183
$this->loader->add_filter( 'plugin_action_links_' . $this->plugin_basename, $plugin_admin, 'add_action_links' );
183184
}
184185

@@ -198,6 +199,9 @@ private function define_public_hooks() {
198199
$this->loader->add_action( 'init', $plugin_public, 'register_shortcodes' );
199200
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
200201
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
202+
$this->loader->add_action( 'wp_ajax_wplibcalhours', $plugin_public, 'api' );
203+
$this->loader->add_action( 'wp_ajax_nopriv_wplibcalhours', $plugin_public, 'api' );
204+
201205
}
202206

203207
/**

public/class-wplibcalhours-public.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ class WpLibCalHours_Public {
5959
*
6060
* @since 1.0.0
6161
*
62-
* @param string $plugin_name The name of the plugin.
63-
* @param string $version The version of this plugin.
62+
* @param string $plugin_name The name of the plugin.
63+
* @param string $version The version of this plugin.
6464
* @param WpLibCalHours_Client $client The LibCal API client.
6565
*/
6666
public function __construct( $plugin_name, $version, WpLibCalHours_Client $client ) {
@@ -262,4 +262,36 @@ protected function extract_hours( array $weeks_raw_data ) {
262262
return $days;
263263
}
264264

265+
/**
266+
* Sends a JSON-formatted response of all library location timetables.
267+
*
268+
* @since 1.1.0
269+
*/
270+
public function api() {
271+
$ignore_cache = (boolean) get_option( 'wplibcalhours_ignore_cache' );
272+
$now = date( 'Y-m-d' );
273+
try {
274+
$data = $this->client->getRawData( $ignore_cache );
275+
$rhett = [];
276+
foreach ( $data['locations'] as $location ) {
277+
$rhett[ $location['name'] ] = [];
278+
$timetable = $this->extract_hours( $location['weeks'] );
279+
foreach ( $timetable as $date => $hours ) {
280+
if ( strcmp( $now, $date ) > 0 ) {
281+
continue;
282+
}
283+
$rhett[ $location['name'] ][] = [
284+
'day' => date( 'D', strtotime( $date ) ),
285+
'date' => date( 'M j', strtotime( $date ) ),
286+
'text' => $hours,
287+
];
288+
289+
}
290+
291+
}
292+
} catch ( \Exception $e ) {
293+
error_log( $e->getMessage() );
294+
}
295+
wp_send_json( $rhett );
296+
}
265297
}

wplibcalhours.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* Plugin Name: LibCal Hours for WordPress
1717
* Plugin URI: https://github.com/ucsf-ckm/wplibcalhours
1818
* Description: Embed LibCal hours for a given location into contents via short-code.
19-
* Version: 1.0.1
19+
* Version: 1.1.0
2020
* Author: Stefan Topfstedt
2121
* Author URI: https://github.com/stopfstedt
2222
* License: MIT

0 commit comments

Comments
 (0)