Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dac514 opened this issue Oct 17, 2019 · 0 comments
Open

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

dac514 opened this issue Oct 17, 2019 · 0 comments
Labels
API & cloning related to our API or cloning routine dev tools related to tools/processes we might use in software development Epic help wanted

Comments

@dac514
Copy link

dac514 commented Oct 17, 2019

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;
}
dac514 added a commit to pressbooks/pressbooks that referenced this issue Oct 18, 2019
dac514 added a commit to pressbooks/pressbooks that referenced this issue Oct 18, 2019
+ Remove `/wp-json/pressbooks/v2/books/search`
+ Remove` /wp-json/pressbooks/v2/books` fulltext search

This is not good enough. We need to rethink how to do this. Possible hint: WP_Site_Query, WP_Network_Query, "Now after the filter `networks_pre_query` and `sites_pre_query run`, the code will exit straight away. This allows developers to completely hot-wire the network and site query, to load from another source, such as a different cache or Elastic search." 

Source: https://make.wordpress.org/core/2019/10/15/enhancements-to-the-network-sites-screen-in-wordpress-5-3/

+ Simplify and speed up the output of `/pressbooks/v2/books`
+ Remove pb-api (Api_v1) - TODO: pressbooks/pb-api#3
+ Fix: wp_blogmeta copy was stale, off-by-1-clear-cache
+ Store all book info as array and JSON
+ Add get() with auto-sync

Note: Instead of makling a `/v3` We're making the assumption that no one uses `/pressbooks/v2/books` and being bad open source citizens by removing the Network API `toc` node (because we don't want people using it). The `toc` node still exists in each Book API. We will be adding more schema,org compatible info into the metadata node in the upcoming weeks.
@SteelWagstaff SteelWagstaff added API & cloning related to our API or cloning routine dev tools related to tools/processes we might use in software development labels Nov 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API & cloning related to our API or cloning routine dev tools related to tools/processes we might use in software development Epic help wanted
Projects
None yet
Development

No branches or pull requests

2 participants