From b88ffc949de43b0b3ed25dfae73596eddf3850de Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Sun, 3 Jan 2016 22:26:54 -0700 Subject: [PATCH 1/4] make default constructor more resilient --- js/load.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/load.js b/js/load.js index 298648c..cf12e7f 100644 --- a/js/load.js +++ b/js/load.js @@ -474,7 +474,7 @@ * @todo required arguments */ _.each( modelInstance.defaults, function( theDefault, index ) { - if ( _.isUndefined( theDefault['default'] ) ) { + if ( null === theDefault || _.isUndefined( theDefault['default'] ) ) { modelInstance.defaults[ index ] = null; } else { modelInstance.defaults[ index ] = theDefault['default']; From 309ccaabec6fe31883cb946c85335db5b9cfbfdd Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Sun, 3 Jan 2016 22:27:14 -0700 Subject: [PATCH 2/4] fix a constructor error --- js/load.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/load.js b/js/load.js index cf12e7f..557e3df 100644 --- a/js/load.js +++ b/js/load.js @@ -358,7 +358,7 @@ * @return {Object} user A backbone model representing the author user. */ getAuthorUser: function() { - var user, authorId, embeddeds, attributes, + var user, authorId, embeddeds, attributes; authorId = this.get( 'author' ); embeddeds = this.get( '_embedded' ) || {}; From 428df39fbb1de87f6836d8870d47267019d8c96c Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Sun, 3 Jan 2016 22:27:34 -0700 Subject: [PATCH 3/4] Set the cache and cookie after loading localized schema --- js/load.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/js/load.js b/js/load.js index 557e3df..7b75788 100644 --- a/js/load.js +++ b/js/load.js @@ -39,6 +39,13 @@ // Use schema supplied as model attribute. model.schemaModel.set( model.schemaModel.parse( model.get( 'schema' ) ) ); + + // Store a copy of the schema model in the session cache if available. + if ( ! _.isUndefined( sessionStorage ) ) { + sessionStorage.setItem( 'wp-api-schema-model' + model.get( 'apiRoot' ) + model.get( 'versionString' ), JSON.stringify( model.schemaModel ) ); + document.cookie = 'api-schema-hash=' + wpApiSettings.schemaHash; + } + } else if ( ! _.isUndefined( sessionStorage ) && sessionStorage.getItem( 'wp-api-schema-model' + model.get( 'apiRoot' ) + model.get( 'versionString' ) ) ) { // Used a cached copy of the schema model if available. From 10c4c96848aded97a5a8fa0fca86e6aec6e82b4f Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Sun, 3 Jan 2016 22:28:02 -0700 Subject: [PATCH 4/4] Only localize the schema if the clients doesn't have the data yet --- client-js.php | 55 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/client-js.php b/client-js.php index 213d200..274ffc2 100644 --- a/client-js.php +++ b/client-js.php @@ -13,31 +13,50 @@ function json_api_client_js() { wp_register_script( 'wp-api', $src, array( 'jquery', 'underscore', 'backbone' ), '1.0', true ); } - /** - * @var \WP_REST_Server $wp_rest_server - */ - global $wp_rest_server; - if ( empty( $wp_rest_server ) ) { - /** This filter is documented in wp-includes/rest-api.php */ - $wp_rest_server_class = apply_filters( 'wp_rest_server_class', 'WP_REST_Server' ); - $wp_rest_server = new $wp_rest_server_class(); - - /** This filter is documented in wp-includes/rest-api.php */ - do_action( 'rest_api_init', $wp_rest_server ); + + $client_has_schema = false; + if ( false !== ( $schema_hash = get_transient( 'api-schema-hash-cache' ) ) ) { + if ( isset( $_COOKIE['api-schema-hash'] ) && $schema_hash == $_COOKIE['api-schema-hash'] ) { + $client_has_schema = true; + } } - $schema_request = new WP_REST_Request( 'GET', '/wp/v2' ); - $schema_response = $wp_rest_server->dispatch( $schema_request ); $schema = null; - if ( ! $schema_response->is_error() ) { - $schema = $schema_response->get_data(); + + if ( ! $client_has_schema ) { + + /** + * @var \WP_REST_Server $wp_rest_server + */ + global $wp_rest_server; + if ( empty( $wp_rest_server ) ) { + /** This filter is documented in wp-includes/rest-api.php */ + $wp_rest_server_class = apply_filters( 'wp_rest_server_class', 'WP_REST_Server' ); + $wp_rest_server = new $wp_rest_server_class(); + + /** This filter is documented in wp-includes/rest-api.php */ + do_action( 'rest_api_init', $wp_rest_server ); + } + + $schema_request = new WP_REST_Request( 'GET', '/wp/v2' ); + $schema_response = $wp_rest_server->dispatch( $schema_request ); + + // Check to see if the request contains a schema hash cookie matching the cached hash. + if ( ! $schema_response->is_error() ) { + $schema = $schema_response->get_data(); + } + $schema_hash = md5( json_encode( $schema ) ); + + // Cache the schema hash for up to an hour. + set_transient( 'api-schema-hash-cache', $schema_hash, 60 * MINUTE_IN_SECONDS ); } $settings = array( - 'root' => esc_url_raw( get_rest_url() ), - 'nonce' => wp_create_nonce( 'wp_rest' ), + 'root' => esc_url_raw( get_rest_url() ), + 'nonce' => wp_create_nonce( 'wp_rest' ), 'versionString' => 'wp/v2/', - 'schema' => $schema, + 'schema' => $schema, + 'schemaHash' => $schema_hash, ); wp_localize_script( 'wp-api', 'wpApiSettings', $settings );