pressbooks / pb-api Goto Github PK
View Code? Open in Web Editor NEWThe Pressbooks API.
The Pressbooks API.
Refactor the following code into this module, so that it works stand-alone, without any code inside Pressbooks core.
/hooks.php
$enable_network_api = \Pressbooks\Api\is_enabled();
if ( $enable_network_api ) {
add_filter( 'init', '\Pressbooks\Redirect\rewrite_rules_for_api', 1 ); // API V1
}
/inc/redirect/namespace.php
/**
* PB API v1
* Adding a rewrite rule for Book API
*
* @see https://github.com/pressbooks/pb-api
* @deprecated
*/
function rewrite_rules_for_api() {
add_rewrite_endpoint( 'api', EP_ROOT );
add_action( 'template_redirect', __NAMESPACE__ . '\do_api', 0 );
}
/**
* PB API v1
* Expects the pattern `api/v1/books/{id}`
*
* @see https://github.com/pressbooks/pb-api
* @deprecated
*/
function do_api() {
// Don't do anything and return if `api` isn't part of the URL
if ( ! array_key_exists( 'api', $GLOBALS['wp_query']->query_vars ) ) {
return;
}
// Support only GET requests for now
if ( 'GET' !== $_SERVER['REQUEST_METHOD'] ) {
\Pressbooks\Modules\Api_v1\Api::apiErrors( 'method' );
}
// Deal with the rest of the URL
$nouns = get_query_var( 'api' );
if ( '' === trim( $nouns, '/' ) || empty( $nouns ) ) {
\Pressbooks\Modules\Api_v1\Api::apiErrors( 'resource' );
}
// parse url, at minimum we need `v1` and `books`
$parts = explode( '/', $nouns );
// required 'v1'
$version = array_shift( $parts );
// required 'books'
$resource = array_shift( $parts );
// optional 'id'
$books_id = ( isset( $parts[0] ) ) ? $parts[0] : '';
$variations = [];
if ( 'v1' !== $version ) {
\Pressbooks\Modules\Api_v1\Api::apiErrors( 'version' );
}
// Filter user input
if ( is_array( $_GET ) ) {
$args = [
'titles' => [
'filter' => FILTER_SANITIZE_STRING,
'flags' => FILTER_FLAG_STRIP_HIGH,
],
'offset' => FILTER_SANITIZE_NUMBER_INT,
'limit' => FILTER_SANITIZE_NUMBER_INT,
'json' => FILTER_SANITIZE_NUMBER_INT,
'xml' => FILTER_SANITIZE_NUMBER_INT,
'subjects' => [
'filter' => FILTER_SANITIZE_STRING,
'flags' => FILTER_FLAG_STRIP_LOW,
],
'authors' => [
'filter' => FILTER_SANITIZE_STRING,
'flags' => FILTER_FLAG_STRIP_LOW,
],
'licenses' => [
'filter' => FILTER_SANITIZE_STRING,
'flags' => FILTER_FLAG_STRIP_HIGH,
],
'keywords' => [
'filter' => FILTER_SANITIZE_STRING,
'flags' => FILTER_FLAG_STRIP_LOW,
],
];
$variations = filter_input_array( INPUT_GET, $args, false );
if ( $variations ) {
// Trim whitespace
array_filter( $variations, __NAMESPACE__ . '\trim_value' );
}
}
switch ( $resource ) {
case 'books':
try {
new \Pressbooks\Modules\Api_v1\Books\BooksApi( $books_id, $variations );
} catch ( \Exception $e ) {
echo $e->getMessage();
}
break;
case 'docs':
$docs = [
PB_PLUGIN_DIR . 'vendor/pressbooks/pb-api/includes/modules/api_v1/docs/api-documentation.php', // Packaged
WP_CONTENT_DIR . '/../../vendor/pressbooks/pb-api/includes/modules/api_v1/docs/api-documentation.php', // Bedrock
WP_CONTENT_DIR . '/vendor/pressbooks/pb-api/includes/modules/api_v1/docs/api-documentation.php', // Maybe here?
];
foreach ( $docs as $path ) {
if ( file_exists( $path ) ) {
require( $path );
break;
}
}
break;
default:
\Pressbooks\Modules\Api_v1\Api::apiErrors( 'resource' );
break;
}
exit;
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.