Skip to content

Commit

Permalink
Merge pull request #29 from stopfstedt/api
Browse files Browse the repository at this point in the history
API endpoint for library hours.
  • Loading branch information
stopfstedt authored Jan 3, 2018
2 parents 729396a + 9713aa4 commit 3d5271b
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 8 deletions.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,65 @@ The short code has the following configuration options.

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

## API

This plugin also exposes opening hours via a read-only, public API endpoint.

Requesting the path `/wp-admin/admin-ajax.php?action=wplibcalhours` on your site will yield
a JSON-formatted payload comprised of dates and opening hours, keyed by their location names.

These lists are sorted in chronological order and contain day-of-the-week, date and the day's opening hours information.

Each list starts with the current date.

#### Example payload

Three locations, the current date is January 3rd.

```json
{
"Parnassus Library": [
{
"day": "Wed",
"date": "Jan 3",
"text": "7:45am - 10pm"
},
{
"day": "Thu",
"date": "Jan 4",
"text": "7:45am - 10pm"
}
//...
],
"Mission Bay Library": [
{
"day": "Wed",
"date": "Jan 3",
"text": "9am - 6pm"
},
{
"day": "Thu",
"date": "Jan 4",
"text": "9am - 6pm"
}
// ...
],
"Mission Bay Hub": [
{
"day": "Wed",
"date": "Jan 3",
"text": "24 hours"
},
{
"day": "Thu",
"date": "Jan 4",
"text": "24 hours"
}
// ...
]
}
```

## Styling

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.
Expand Down
23 changes: 20 additions & 3 deletions includes/class-wplibcalhours-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __construct( $plugin_name, $version, $url ) {
/**
* Returns the hours from a given location.
*
* @param string $location The name of the location.
* @param string $location The name of the location.
* @param boolean $ignore_cache Set to TRUE to bypass cache.
*
* @return array
Expand All @@ -78,6 +78,23 @@ public function __construct( $plugin_name, $version, $url ) {
* @since 1.0.0
*/
public function getHours( $location, $ignore_cache = false ) {
$data = $this->getRawData( $ignore_cache );

return $this->extractTimetableForLocation( $location, $data );
}

/**
* Returns the raw data as consumed from the LibCal API.
*
* @param bool $ignore_cache Set to TRUE to bypass cache.
*
* @return array
*
* @throws \Exception
*
* @since 1.1.0
*/
public function getRawData( $ignore_cache = false ) {
$data = false;
$transient_key = $this->plugin_name . '_data';
if ( ! $ignore_cache ) {
Expand All @@ -91,7 +108,7 @@ public function getHours( $location, $ignore_cache = false ) {
}
}

return $this->extractTimetableForLocation( $location, $data );
return $data;
}

/**
Expand Down Expand Up @@ -127,7 +144,7 @@ protected function fetchHoursFromAPI() {
* Extracts the hours for a given location from the given data set.
*
* @param string $location The name of the location.
* @param array $data The entire location/hours data set.
* @param array $data The entire location/hours data set.
*
* @return array The hours for the given location.
*
Expand Down
8 changes: 6 additions & 2 deletions includes/class-wplibcalhours.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class WpLibCalHours {
public function __construct() {

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

$this->load_dependencies();
$this->set_locale();
Expand Down Expand Up @@ -178,7 +178,8 @@ private function define_admin_hooks() {

$this->loader->add_action( 'admin_menu', $plugin_admin, 'add_options_page' );
$this->loader->add_action( 'admin_init', $plugin_admin, 'register_setting' );
$this->loader->add_action( 'update_option_' . self::PLUGIN_NAME . '_ignore_cache', $plugin_admin, 'update_option_ignore_cache', null, 0 );
$this->loader->add_action( 'update_option_' . self::PLUGIN_NAME . '_ignore_cache', $plugin_admin,
'update_option_ignore_cache', null, 0 );
$this->loader->add_filter( 'plugin_action_links_' . $this->plugin_basename, $plugin_admin, 'add_action_links' );
}

Expand All @@ -198,6 +199,9 @@ private function define_public_hooks() {
$this->loader->add_action( 'init', $plugin_public, 'register_shortcodes' );
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
$this->loader->add_action( 'wp_ajax_wplibcalhours', $plugin_public, 'api' );
$this->loader->add_action( 'wp_ajax_nopriv_wplibcalhours', $plugin_public, 'api' );

}

/**
Expand Down
36 changes: 34 additions & 2 deletions public/class-wplibcalhours-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class WpLibCalHours_Public {
*
* @since 1.0.0
*
* @param string $plugin_name The name of the plugin.
* @param string $version The version of this plugin.
* @param string $plugin_name The name of the plugin.
* @param string $version The version of this plugin.
* @param WpLibCalHours_Client $client The LibCal API client.
*/
public function __construct( $plugin_name, $version, WpLibCalHours_Client $client ) {
Expand Down Expand Up @@ -262,4 +262,36 @@ protected function extract_hours( array $weeks_raw_data ) {
return $days;
}

/**
* Sends a JSON-formatted response of all library location timetables.
*
* @since 1.1.0
*/
public function api() {
$ignore_cache = (boolean) get_option( 'wplibcalhours_ignore_cache' );
$now = date( 'Y-m-d' );
try {
$data = $this->client->getRawData( $ignore_cache );
$rhett = [];
foreach ( $data['locations'] as $location ) {
$rhett[ $location['name'] ] = [];
$timetable = $this->extract_hours( $location['weeks'] );
foreach ( $timetable as $date => $hours ) {
if ( strcmp( $now, $date ) > 0 ) {
continue;
}
$rhett[ $location['name'] ][] = [
'day' => date( 'D', strtotime( $date ) ),
'date' => date( 'M j', strtotime( $date ) ),
'text' => $hours,
];

}

}
} catch ( \Exception $e ) {
error_log( $e->getMessage() );
}
wp_send_json( $rhett );
}
}
2 changes: 1 addition & 1 deletion wplibcalhours.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Plugin Name: LibCal Hours for WordPress
* Plugin URI: https://github.com/ucsf-ckm/wplibcalhours
* Description: Embed LibCal hours for a given location into contents via short-code.
* Version: 1.0.1
* Version: 1.1.0
* Author: Stefan Topfstedt
* Author URI: https://github.com/stopfstedt
* License: MIT
Expand Down

0 comments on commit 3d5271b

Please sign in to comment.