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

Fix link back to editor from revisions screen #3511

Merged
merged 21 commits into from
Dec 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions editor/components/post-last-revision/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { connect } from 'react-redux';
/**
* Internal dependencies
*/
import { getCurrentPostLastRevisionId } from '../../selectors';
import { getCurrentPostLastRevisionId, getCurrentPostRevisionsCount } from '../../selectors';

function PostLastRevisionCheck( { lastRevisionId, children } ) {
if ( ! lastRevisionId ) {
export function PostLastRevisionCheck( { lastRevisionId, revisionsCount, children } ) {
if ( ! lastRevisionId || revisionsCount < 2 ) {
return null;
}

Expand All @@ -20,6 +20,7 @@ export default connect(
( state ) => {
return {
lastRevisionId: getCurrentPostLastRevisionId( state ),
revisionsCount: getCurrentPostRevisionsCount( state ),
};
}
)( PostLastRevisionCheck );
2 changes: 1 addition & 1 deletion editor/components/post-last-revision/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function LastRevision( { lastRevisionId, revisionsCount } ) {
return (
<PostLastRevisionCheck>
<IconButton
href={ getWPAdminURL( 'revision.php', { revision: lastRevisionId } ) }
href={ getWPAdminURL( 'revision.php', { revision: lastRevisionId, gutenberg: true } ) }
className="editor-post-last-revision__title"
icon="backup"
>
Expand Down
41 changes: 41 additions & 0 deletions editor/components/post-last-revision/test/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* External dependencies
*/
import { shallow } from 'enzyme';

/**
* Internal dependencies
*/
import { PostLastRevisionCheck } from '../check';

describe( 'PostLastRevisionCheck', () => {
it( 'should not render anything if the last revision ID is unknown', () => {
const wrapper = shallow(
<PostLastRevisionCheck revisionsCount={ 2 }>
Children
</PostLastRevisionCheck>
);

expect( wrapper.type() ).toBe( null );
} );

it( 'should not render anything if there is only one revision', () => {
const wrapper = shallow(
<PostLastRevisionCheck lastRevisionId={ 1 } revisionsCount={ 1 }>
Children
</PostLastRevisionCheck>
);

expect( wrapper.type() ).toBe( null );
} );

it( 'should render if there are two revisions', () => {
const wrapper = shallow(
<PostLastRevisionCheck lastRevisionId={ 1 } revisionsCount={ 2 }>
Children
</PostLastRevisionCheck>
);

expect( wrapper.text() ).toEqual( 'Children' );
} );
} );
36 changes: 30 additions & 6 deletions lib/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,18 +480,42 @@ function gutenberg_redirect_to_classic_editor_when_saving_posts( $url ) {
add_filter( 'redirect_post_location', 'gutenberg_redirect_to_classic_editor_when_saving_posts', 10, 1 );

/**
* Appends a query argument to the edit url to make sure it gets redirected to the classic editor.
* Appends a query argument to the edit url to make sure it is redirected to
* the editor from which the user navigated.
*
* @since 1.5.2
*
* @param string $url Edit url.
* @return string Edit url.
*/
function gutenberg_link_revisions_to_classic_editor( $url ) {
function gutenberg_revisions_link_to_editor( $url ) {
global $pagenow;
if ( 'revision.php' === $pagenow ) {
$url = add_query_arg( 'classic-editor', '', $url );
if ( 'revision.php' !== $pagenow || isset( $_REQUEST['gutenberg'] ) ) {
return $url;
}
return $url;

return add_query_arg( 'classic-editor', '', $url );
}
add_filter( 'get_edit_post_link', 'gutenberg_revisions_link_to_editor' );

/**
* Modifies revisions data to preserve Gutenberg argument used in determining
* where to redirect user returning to editor.
*
* @since 1.9.0
*
* @param array $revisions_data The bootstrapped data for the revisions screen.
* @return array Modified bootstrapped data for the revisions screen.
*/
function gutenberg_revisions_restore( $revisions_data ) {
if ( isset( $_REQUEST['gutenberg'] ) ) {
$revisions_data['restoreUrl'] = add_query_arg(
'gutenberg',
$_REQUEST['gutenberg'],
$revisions_data['restoreUrl']
);
}

return $revisions_data;
}
add_filter( 'get_edit_post_link', 'gutenberg_link_revisions_to_classic_editor' );
add_filter( 'wp_prepare_revision_for_js', 'gutenberg_revisions_restore' );
37 changes: 37 additions & 0 deletions phpunit/class-admin-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,41 @@ function test_add_gutenberg_post_state() {
$post_states = apply_filters( 'display_post_states', array(), get_post( self::$post_without_blocks ) );
$this->assertEquals( array(), $post_states );
}

/**
* Test that the revisions 'return to editor' links are set correctly for Classic & Gutenberg editors.
*
* @covers gutenberg_revisions_link_to_editor
*/
function test_gutenberg_revisions_link_to_editor() {
global $pagenow;

// Set up $pagenow so the filter will work.
$pagenow = 'revision.php';

// Test the filter when Gutenberg is the active editor.
$_REQUEST['gutenberg'] = '1';
$link = apply_filters( 'get_edit_post_link', 'http://test.com' );
$this->assertEquals( 'http://test.com', $link );

// Test the filter when Gutenberg is not the active editor.
unset( $_REQUEST['gutenberg'] );
$link = apply_filters( 'get_edit_post_link', 'http://test.com' );
$this->assertEquals( 'http://test.com?classic-editor', $link );
}

/**
* Test that the revisions 'restore this revision' button links correctly for Classic & Gutenberg editors.
*/
function test_gutenberg_revisions_restore() {
// Test the filter when Gutenberg is the active editor.
$_REQUEST['gutenberg'] = '1';
$link = apply_filters( 'wp_prepare_revision_for_js', array( 'restoreUrl' => 'http://test.com' ) );
$this->assertEquals( array( 'restoreUrl' => 'http://test.com?gutenberg=1' ), $link );

// Test the filter when Gutenberg is not the active editor.
unset( $_REQUEST['gutenberg'] );
$link = apply_filters( 'wp_prepare_revision_for_js', array( 'restoreUrl' => 'http://test.com' ) );
$this->assertEquals( array( 'restoreUrl' => 'http://test.com' ), $link );
}
}