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

Meta Boxes: Only attempt apiRequest preload if path (fix meta box save breakage) #6130

Merged
merged 2 commits into from Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions editor/components/post-saved-state/index.js
Expand Up @@ -39,7 +39,7 @@ export class PostSavedState extends Component {
const { forceSavedMessage } = this.state;
if ( isSaving ) {
return (
<span className="editor-post-saved-state editor-post-saved-state__saving">
<span className="editor-post-saved-state is-saving">
<Dashicon icon="cloud" />
{ __( 'Saving' ) }
</span>
Expand All @@ -56,7 +56,7 @@ export class PostSavedState extends Component {

if ( forceSavedMessage || ( ! isNew && ! isDirty ) ) {
return (
<span className="editor-post-saved-state">
<span className="editor-post-saved-state is-saved">
<Dashicon icon="saved" />
{ __( 'Saved' ) }
</span>
Expand Down
8 changes: 4 additions & 4 deletions editor/components/post-saved-state/style.scss
Expand Up @@ -4,6 +4,10 @@
color: $dark-gray-500;
overflow: hidden;

&.is-saving {
animation: loading_fade .5s infinite;
}

.dashicon {
display: inline-block;
flex: 0 0 auto;
Expand All @@ -30,7 +34,3 @@
}
}
}

.editor-post-saved-state__saving {
animation: loading_fade .5s infinite;
}
17 changes: 11 additions & 6 deletions lib/compat.php
Expand Up @@ -124,12 +124,17 @@ function getStablePath( path ) {
// Add preloading support
var previousApiRequest = wp.apiRequest;
wp.apiRequest = function( request ) {
var method = request.method || 'GET';
var path = getStablePath( request.path );
if ( 'GET' === method && window._wpAPIDataPreload[ path ] ) {
var deferred = jQuery.Deferred();
deferred.resolve( window._wpAPIDataPreload[ path ].body );
return deferred.promise();
var method, path;

if ( typeof request.path === 'string' ) {
method = request.method || 'GET';
path = getStablePath( request.path );

if ( 'GET' === method && window._wpAPIDataPreload[ path ] ) {
var deferred = jQuery.Deferred();
deferred.resolve( window._wpAPIDataPreload[ path ].body );
return deferred.promise();
}
}

return previousApiRequest.call( previousApiRequest, request );
Expand Down
41 changes: 41 additions & 0 deletions test/e2e/specs/meta-boxes.test.js
@@ -0,0 +1,41 @@
/**
* Internal dependencies
*/
import '../support/bootstrap';
import { newPost, newDesktopBrowserPage } from '../support/utils';
import { activatePlugin, deactivatePlugin } from '../support/plugins';

describe( 'Meta boxes', () => {
beforeAll( async () => {
await newDesktopBrowserPage();
await activatePlugin( 'gutenberg-test-plugin-meta-box' );
await newPost();
} );

afterAll( async () => {
await newDesktopBrowserPage();
await deactivatePlugin( 'gutenberg-test-plugin-meta-box' );
} );

it( 'Should save the post', async () => {
// Save should not be an option for new empty post.
expect( await page.$( '.editor-post-save-draft' ) ).toBe( null );

// Add title to enable valid non-empty post save.
await page.type( '.editor-post-title__input', 'Hello Meta' );
expect( await page.$( '.editor-post-save-draft' ) ).not.toBe( null );

await Promise.all( [
// Transitions between three states "Saving..." -> "Saved" -> "Save
// Draft" (the button is always visible while meta are present).
page.waitForSelector( '.editor-post-saved-state.is-saving' ),
page.waitForSelector( '.editor-post-saved-state.is-saved' ),
page.waitForSelector( '.editor-post-save-draft' ),

// Keyboard shortcut Ctrl+S save.
page.keyboard.down( 'Meta' ),
page.keyboard.press( 'S' ),
page.keyboard.up( 'Meta' ),
] );
} );
} );
25 changes: 25 additions & 0 deletions test/e2e/test-plugins/meta-box.php
@@ -0,0 +1,25 @@
<?php

/**
* Plugin Name: Gutenberg Test Plugin, Meta Box
* Plugin URI: https://github.com/WordPress/gutenberg
* Author: Gutenberg Team
*
* @package gutenberg-test-meta-box
*/

function gutenberg_test_meta_box_render_meta_box() {
echo 'Hello World';
}

function gutenberg_test_meta_box_add_meta_box() {
add_meta_box(
'gutenberg-test-meta-box',
'Gutenberg Test Meta Box',
'gutenberg_test_meta_box_render_meta_box',
'post',
'normal',
'high'
);
}
add_action( 'add_meta_boxes', 'gutenberg_test_meta_box_add_meta_box' );
4 changes: 2 additions & 2 deletions test/e2e/test-plugins/templates.php
Expand Up @@ -10,7 +10,7 @@
/**
* Registers a book CPT with a template
*/
function register_book_type() {
function gutenberg_test_templates_register_book_type() {
$args = array(
'public' => true,
'label' => 'Books',
Expand Down Expand Up @@ -43,4 +43,4 @@ function register_book_type() {
register_post_type( 'book', $args );
}

add_action( 'init', 'register_book_type' );
add_action( 'init', 'gutenberg_test_templates_register_book_type' );