This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from xwp/feature/issue-29-rest-api
Add initial read-only REST API endpoints for snapshots
- Loading branch information
Showing
7 changed files
with
551 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
PATH_INCLUDES='*.* php js css tests' | ||
WPCS_GIT_TREE=develop | ||
ASSETS_DIR=wp-assets | ||
|
||
function after_wp_install { | ||
echo "Installing REST API..." | ||
svn export -q https://plugins.svn.wordpress.org/rest-api/trunk/ "$WP_CORE_DIR/src/wp-content/plugins/rest-api" | ||
} |
Submodule dev-lib
updated
6 files
+18 −0 | .editorconfig | |
+1 −0 | .eslintrc | |
+1 −1 | check-diff.sh | |
+4 −0 | phpunit-plugin-bootstrap.php | |
+11 −0 | readme.md | |
+1 −0 | svn-push |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
<?php | ||
/** | ||
* REST API Controller. | ||
* | ||
* @package CustomizeSnapshots | ||
*/ | ||
|
||
namespace CustomizeSnapshots; | ||
|
||
/** | ||
* REST API Controller Class | ||
* | ||
* @todo Add support for editing. Make sure Post_Type::save() is used. | ||
* @todo Add support for PATCH requests. | ||
* @todo Allow use of UUID instead of ID in routes. | ||
* @todo Disallow edits to slug. | ||
* | ||
* @package CustomizeSnapshots | ||
*/ | ||
class Snapshot_REST_API_Controller extends \WP_REST_Posts_Controller { | ||
|
||
/** | ||
* Post type instance. | ||
* | ||
* @var Post_Type | ||
*/ | ||
public $snapshot_post_type; | ||
|
||
/** | ||
* Snapshot_REST_API_Controller constructor. | ||
* | ||
* @throws Exception If the post type was not registered properly. | ||
* @param string $post_type Post type. | ||
*/ | ||
public function __construct( $post_type ) { | ||
$post_type_obj = get_post_type_object( $post_type ); | ||
if ( empty( $post_type_obj ) || empty( $post_type_obj->customize_snapshot_post_type_obj ) ) { | ||
throw new Exception( 'Missing customize_snapshot post type obj or arg for customize_snapshot_post_type_obj' ); | ||
} | ||
$this->snapshot_post_type = $post_type_obj->customize_snapshot_post_type_obj; | ||
parent::__construct( $post_type ); | ||
} | ||
|
||
/** | ||
* Get the Post's schema, conforming to JSON Schema. | ||
* | ||
* @return array | ||
*/ | ||
public function get_item_schema() { | ||
$schema = parent::get_item_schema(); | ||
$schema['properties']['content'] = array( | ||
'description' => __( 'Object mapping setting ID to an object of setting params, including value.', 'customize-snapshots' ), | ||
'type' => 'object', | ||
'context' => array( 'view', 'edit' ), | ||
); | ||
return $schema; | ||
} | ||
|
||
/** | ||
* Get the query params for collections of attachments. | ||
* | ||
* @return array | ||
*/ | ||
public function get_collection_params() { | ||
$params = parent::get_collection_params(); | ||
$params['author']['sanitize_callback'] = array( $this, 'parse_author_list' ); | ||
$params['author_exclude']['sanitize_callback'] = array( $this, 'parse_author_list' ); | ||
return $params; | ||
} | ||
|
||
/** | ||
* Parse comma-separated list of authors represented as IDs or usernames. | ||
* | ||
* @param string $author_list Authors. | ||
* @return array User IDs. | ||
*/ | ||
public function parse_author_list( $author_list ) { | ||
if ( empty( $author_list ) ) { | ||
return array(); | ||
} | ||
$authors = array(); | ||
foreach ( preg_split( '/\s*,\s*/', trim( $author_list ) ) as $author ) { | ||
if ( is_numeric( $author ) ) { | ||
$authors[] = intval( $author ); | ||
} else { | ||
$user = get_user_by( 'slug', sanitize_user( $author ) ); | ||
if ( $user ) { | ||
$authors[] = $user->ID; | ||
} else { | ||
$authors[] = -1; | ||
} | ||
} | ||
} | ||
return $authors; | ||
} | ||
|
||
/** | ||
* Check for fundamental customize capability to do anything with snapshots. | ||
* | ||
* @return bool|\WP_Error | ||
*/ | ||
protected function check_initial_access_permission() { | ||
if ( ! current_user_can( 'customize' ) ) { | ||
return new \WP_Error( 'rest_customize_unauthorized', __( 'Sorry, Customizer snapshots require proper authentication (the customize capability).', 'customize-snapshots' ), array( 'status' => rest_authorization_required_code() ) ); | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Check if a given request has basic access to read a snapshot. | ||
* | ||
* @param \WP_REST_Request $request Full details about the request. | ||
* @return \WP_Error|boolean | ||
*/ | ||
public function get_item_permissions_check( $request ) { | ||
$error = $this->check_initial_access_permission(); | ||
if ( is_wp_error( $error ) ) { | ||
return $error; | ||
} | ||
return parent::get_item_permissions_check( $request ); | ||
} | ||
|
||
/** | ||
* Check if a given request has basic access to read snapshots. | ||
* | ||
* @param \WP_REST_Request $request Full details about the request. | ||
* @return \WP_Error|boolean | ||
*/ | ||
public function get_items_permissions_check( $request ) { | ||
$error = $this->check_initial_access_permission(); | ||
if ( is_wp_error( $error ) ) { | ||
return $error; | ||
} | ||
return parent::get_items_permissions_check( $request ); | ||
} | ||
|
||
/** | ||
* Restrict read permission to whether the user can edit. | ||
* | ||
* @param \WP_Post $post Post object. | ||
* @return boolean Can we read it? | ||
*/ | ||
public function check_read_permission( $post ) { | ||
$post_type_obj = get_post_type_object( 'customize_snapshot' ); | ||
if ( ! current_user_can( $post_type_obj->cap->edit_post, $post->ID ) ) { | ||
return false; | ||
} | ||
return current_user_can( 'customize' ) && parent::check_read_permission( $post ); | ||
} | ||
|
||
/** | ||
* Prepare a single post output for response. | ||
* | ||
* @param \WP_Post $post Post object. | ||
* @param \WP_REST_Request $request Request object. | ||
* @return \WP_REST_Response $response Response. | ||
*/ | ||
public function prepare_item_for_response( $post, $request ) { | ||
$response = parent::prepare_item_for_response( $post, $request ); | ||
$response->data['content'] = $this->snapshot_post_type->get_post_content( $post ); | ||
return $response; | ||
} | ||
|
||
/** | ||
* Update one item from the collection. | ||
* | ||
* @param \WP_REST_Request $request Full data about the request. | ||
* @return \WP_Error|\WP_REST_Response | ||
*/ | ||
public function update_item( $request ) { | ||
unset( $request ); | ||
return new \WP_Error( 'invalid-method', sprintf( __( "Method '%s' not yet implemented.", 'customize-snapshots' ), __METHOD__ ), array( 'status' => 405 ) ); | ||
} | ||
|
||
/** | ||
* Delete one item from the collection. | ||
* | ||
* @param \WP_REST_Request $request Full data about the request. | ||
* @return \WP_Error|\WP_REST_Response | ||
*/ | ||
public function delete_item( $request ) { | ||
unset( $request ); | ||
return new \WP_Error( 'invalid-method', sprintf( __( "Method '%s' not yet implemented.", 'customize-snapshots' ), __METHOD__ ), array( 'status' => 405 ) ); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<phpunit | ||
bootstrap="dev-lib/phpunit-plugin-bootstrap.php" | ||
backupGlobals="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
> | ||
<php> | ||
<const name="WP_TEST_VIP_QUICKSTART_ACTIVATED_PLUGINS" value="jetpack/jetpack.php,media-explorer/media-explorer.php,writing-helper/writing-helper.php,mrss/mrss.php,wordpress-importer/wordpress-importer.php,keyring/keyring.php,polldaddy/polldaddy.php" /> | ||
<const name="WPCOM_VIP_DISABLE_REMOTE_REQUEST_ERROR_REPORTING" value="1" /> | ||
<const name="WP_TEST_ACTIVATED_PLUGINS" value="rest-api/plugin.php" /> | ||
</php> | ||
<testsuites> | ||
<testsuite> | ||
<directory prefix="test-" suffix=".php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="false"> | ||
<directory suffix=".php">./</directory> | ||
<exclude> | ||
<directory suffix=".php">./dev-lib</directory> | ||
<directory suffix=".php">./node_modules</directory> | ||
<directory suffix=".php">./tests</directory> | ||
<directory suffix=".php">./vendor</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.