Skip to content

Commit

Permalink
Gutenberg hack
Browse files Browse the repository at this point in the history
Temporary solution for: WordPress/gutenberg#8683
  • Loading branch information
dac514 committed Sep 27, 2018
1 parent 8c84b98 commit 3d40fb6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 17 deletions.
86 changes: 70 additions & 16 deletions inc/api/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Pressbooks\Api;

use function Pressbooks\Utility\str_starts_with;
use Pressbooks\Admin\Network\SharingAndPrivacyOptions;

/**
Expand Down Expand Up @@ -76,6 +77,49 @@ function init_book() {

// Batch endpoint
init_batch();

// Gutenberg hack
gutenberg_hack();
}

/**
* Gutenberg hack
*
* Expose post types and taxonomies in REST /wp/v2/ namespace. Hacky way of getting our post types working with WordPress
* without having to keep nagging them to fix their project for broader use cases.
*
* @see https://github.com/WordPress/gutenberg/issues/8683
*/
function gutenberg_hack() {
if ( current_user_can( 'edit_posts' ) === false ) {
// If the user cannot edit posts then don't turn on this hack.
// Our Public/Private mechanisms are ignored by WP_REST_Controller(s) and we don't want these exposed to the world.
return;
}

// TODO: Remove this once https://core.trac.wordpress.org/ticket/44864 is fixed.
if ( is_multisite() ) {
require_once( ABSPATH . 'wp-admin/includes/ms-admin-filters.php' );
require_once( ABSPATH . 'wp-admin/includes/ms.php' );
require_once( ABSPATH . 'wp-admin/includes/ms-deprecated.php' );
}

foreach ( get_post_types( [ 'show_in_rest' => true ], 'objects' ) as $post_type ) {
if ( $post_type->rest_controller_class === '\Pressbooks\Api\Endpoints\Controller\Posts' ) {
$controller = new \WP_REST_Posts_Controller( $post_type->name );
$controller->register_routes();
if ( post_type_supports( $post_type->name, 'revisions' ) ) {
$revisions_controller = new \WP_REST_Revisions_Controller( $post_type->name );
$revisions_controller->register_routes();
}
}
}
foreach ( get_taxonomies( [ 'show_in_rest' => true ], 'object' ) as $taxonomy ) {
if ( $taxonomy->rest_controller_class === '\Pressbooks\Api\Endpoints\Controller\Terms' ) {
$controller = new \WP_REST_Terms_Controller( $taxonomy->name );
$controller->register_routes();
}
}
}

/**
Expand Down Expand Up @@ -189,27 +233,37 @@ function batch_serve_request( $request ) {
/**
* Hide endpoints that don't work well with a book api
*
* @see https://github.com/WordPress/gutenberg/issues/8683
*
* @param array $endpoints
*
* @return array
*/
function hide_endpoints_from_book( $endpoints ) {

foreach ( $endpoints as $key => $val ) {
if (
( strpos( $key, '/wp/v2/posts' ) === 0 ) ||
( strpos( $key, '/wp/v2/pages' ) === 0 ) ||
( strpos( $key, '/wp/v2/tags' ) === 0 ) ||
( strpos( $key, '/wp/v2/categories' ) === 0 ) ||
( strpos( $key, '/wp/v2/front-matter-type' ) === 0 ) ||
( strpos( $key, '/wp/v2/chapter-type' ) === 0 ) ||
( strpos( $key, '/wp/v2/back-matter-type' ) === 0 ) ||
( strpos( $key, '/wp/v2/glossary-type' ) === 0 ) ||
( strpos( $key, '/wp/v2/license' ) === 0 ) ||
( strpos( $key, '/wp/v2/contributor' ) === 0 ) ||
( strpos( $key, '/wp/v2' ) === 0 && strpos( $key, '/revisions' ) !== false )
) {
unset( $endpoints[ $key ] );
// If the user cannot edit posts then hide certain WP endpoints.
// Our Public/Private mechanisms are ignored by WP_REST_Controller(s) and we don't want these exposed to the world.
if ( current_user_can( 'edit_posts' ) === false ) {
foreach ( $endpoints as $key => $val ) {
if ( str_starts_with( $key, '/wp/v2/media' ) ) {
// Don't touch media
continue;
}
if (
( str_starts_with( $key, '/wp/v2/posts' ) ) ||
( str_starts_with( $key, '/wp/v2/pages' ) ) ||
( str_starts_with( $key, '/wp/v2/tags' ) ) ||
( str_starts_with( $key, '/wp/v2/categories' ) ) ||
( str_starts_with( $key, '/wp/v2/front-matter-type' ) ) ||
( str_starts_with( $key, '/wp/v2/chapter-type' ) ) ||
( str_starts_with( $key, '/wp/v2/back-matter-type' ) ) ||
( str_starts_with( $key, '/wp/v2/glossary-type' ) ) ||
( str_starts_with( $key, '/wp/v2/license' ) ) ||
( str_starts_with( $key, '/wp/v2/contributor' ) ) ||
( str_starts_with( $key, '/wp/v2' ) && strpos( $key, '/revisions' ) !== false ) ||
( str_starts_with( $key, '/wp/v2' ) && strpos( $key, '/autosaves' ) !== false )
) {
unset( $endpoints[ $key ] );
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function test_BookEndpoints() {
$request = new \WP_REST_Request( 'GET', $endpoint );
$response = $server->dispatch( $request );
$status = $response->get_status();
$this->assertEquals( $status, 404 );
$this->assertEquals( 404, $status );
}
}

Expand Down

0 comments on commit 3d40fb6

Please sign in to comment.