WordPress.org

Making WordPress.org

Changeset 7523


Ignore:
Timestamp:
07/30/2018 05:17:43 AM (3 years ago)
Author:
dd32
Message:

Gutenberg: Override the REST API to provide gutenberg with some static content.
This is a hacky way to avoid having to hack capabilities.

See #3703.

Location:
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/gutenberg
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/gutenberg/functions.php

    r7522 r7523  
    1717        return;
    1818    }
    19 
    20     function frontenberg_edit_pages( $allcaps ) {
    21         $allcaps['edit_pages']           = true;
    22         $allcaps['edit_others_pages']    = true;
    23         $allcaps['edit_published_pages'] = true;
    24 
    25         return $allcaps;
    26     }
    27     add_filter( 'user_has_cap', 'frontenberg_edit_pages' );
    2819
    2920    show_admin_bar( true );
     
    6152    add_action( 'wp_enqueue_scripts', 'gutenberg_editor_scripts_and_styles' );
    6253
    63     if ( ! is_user_logged_in() ) {
    64         add_filter( 'wp_insert_post_empty_content', '__return_true', PHP_INT_MAX -1, 2 );
    65         add_filter( 'pre_insert_term', function( $t ) {return ''; });
    66     }
    67 
    6854    // Disable use XML-RPC
    6955    add_filter( 'xmlrpc_enabled', '__return_false' );
     
    136122}
    137123add_action( 'wp_ajax_nopriv_query-attachments', 'frontenberg_wp_ajax_nopriv_query_attachments' );
     124
     125// Override the WP API to give Gutenberg the REST API responses it wants.
     126function frontenberg_override_rest_api( $null, $wp_rest_server, $request ) {
     127    if ( is_admin() || preg_match( '!/wp-json/!', $_SERVER['REQUEST_URI'] ) ) {
     128        return $null; // Don't modify an actual API responses..
     129    }
     130
     131    if ( '/wp/v2/types' == $request->get_route() ) {
     132        // Minimal data required by Gutenberg for pages.
     133        return new WP_REST_Response( array(
     134            'page' => array(
     135                'rest_base' => 'pages',
     136            )
     137        ), 200 );
     138    }
     139
     140    if ( preg_match( '!^/wp/v2/pages/(\d+)$!', $request->get_route(), $m ) ) {
     141        $content = include __DIR__ . '/gutenfront-content.php';
     142
     143        return new WP_REST_Response( array(
     144            'id' => $m[1],
     145            'date' => gmdate( 'c' ), 'date_gmt' => gmdate( 'c' ),
     146            'modified' => gmdate( 'c' ), 'modified_gmt' => gmdate( 'c' ),
     147            'link' => 'https://wordpress.org/gutenberg/', 'guid' => array(),
     148            'parent' => 0, 'menu_order' => 0, 'author' => 0, 'featured_media' => 0,
     149            'comment_status' => 'closed', 'ping_status' => 'closed', 'template' => '', 'meta' => [], '_links' => [],
     150            'type' => 'page', 'slug' => '', 'status' => 'draft',
     151            'excerpt' => array( 'raw' => '' ),
     152            'title' => array(
     153                'raw' => $content['title'],
     154            ),
     155            'content' => array(
     156                'raw' => $content['content'],
     157            ),
     158        ), 200 );
     159    }
     160
     161    return $null;
     162}
     163add_filter( 'rest_pre_dispatch', 'frontenberg_override_rest_api', 10, 3 );
     164
    138165
    139166if ( ! function_exists( 'gutenbergtheme_setup' ) ) :
Note: See TracChangeset for help on using the changeset viewer.