GithubHelp home page GithubHelp logo

pb-api's Issues

Move all pb-api code out of Pressbooks core, and into this repo

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;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.