Skip to content

Try: Show a success message when restoring a revision - #20349

Closed
johnwatkins0 wants to merge 6 commits into
trunkfrom
try/19226-restore-revision-success-message
Closed

johnwatkins0 wants to merge 6 commits into
trunkfrom
try/19226-restore-revision-success-message

Conversation

@johnwatkins0

@johnwatkins0 johnwatkins0 commented Feb 21, 2020 •

Copy link
Copy Markdown
Member

Description

This would resolve #19226 by showing a success message when a post is restored from a revision:

Screen Shot 2020-02-20 at 8 13 30 PM

The challenge with this task is that restoring revisions is not part of what the editor does; revisions are a different UI on a separate page, and the revision is restored server-side before sending the user back to the editor. At this point the only indication that a post is a revision is this URL parameter:

Screen Shot 2020-02-20 at 8 18 48 PM

So I see two possible approaches:

  1. Read that URL parameter in the editor and trigger the notice on load
  2. When the post is restored from a revision, pass the notice from the server via initializeEditor, and use this to trigger the notice on load

I went with 2, but this could be adapted for 1. I didn't see any instances of URL parameters being read currently in the editor, so I figured a decision had been made at an earlier point not to do that. We do have the tools to do it, though. Perhaps the BrowserURL component could be given exclusive unofficial permission to read URL parameters; it could read the URL parameter, remove it, and dispatch a bit of state to trigger the notice. I'd love to try that if people are in favor of it.

As this stands now, I'd need to also submit (roughly) the following update to edit-form-blocks.php in core. The update could be merged here without the core update in place.

$init_script = <<<JS
( function() {
	window._wpLoadBlockEditor = new Promise( function( resolve ) {
		wp.domReady( function() {
			resolve( wp.editPost.initializeEditor( 'editor', "%s", %d, %s, %s, %s ) );
		} );
	} );
} )();
JS;

$script = sprintf(
	$init_script,
	$post->post_type,
	$post->ID,
	wp_json_encode( $editor_settings ),
	wp_json_encode( $initial_edits ),
	wp_json_encode(
		isset( $_GET['revision'] )
			? sprintf(
				__( '%s restored to revision from %s.' ),
				get_post_type_object( get_post_type( get_post() ) )->labels->singular_name,
				wp_post_revision_title( (int) $_GET['revision'], false )
			)
			: ''
	)
);
wp_add_inline_script( 'wp-edit-post', $script );

Alternative approaches

Aside from maybe handling this entirely in the plugin, I think we could generalize this if we think being able to show other types of snackbar messages on pageload might be useful.

How has this been tested?

  • This can be tested by making the above change in your local WP installation, or in the plugin you can hardcode some string for revisionNotice in edit-post/src/editor.js.
  • Added two unit tests, and everything passed.
  • Tested locally in the browser.

Types of changes

New feature that will not break existing functionality.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@github-actions

github-actions Bot commented Feb 21, 2020 •

Copy link
Copy Markdown

Size Change: +106 B (0%)

Total Size: 825 kB

Filename Size Change
build/edit-post/index.js 28.2 kB +52 B (0%)
build/editor/index.js 44.4 kB +54 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.02 kB 0 B
build/annotations/index.js 3.62 kB 0 B
build/api-fetch/index.js 4.08 kB 0 B
build/autop/index.js 2.82 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 6.6 kB 0 B
build/block-directory/style-rtl.css 760 B 0 B
build/block-directory/style.css 761 B 0 B
build/block-editor/index.js 107 kB 0 B
build/block-editor/style-rtl.css 10.2 kB 0 B
build/block-editor/style.css 10.2 kB 0 B
build/block-library/editor-rtl.css 7.08 kB 0 B
build/block-library/editor.css 7.08 kB 0 B
build/block-library/index.js 115 kB 0 B
build/block-library/style-rtl.css 7.22 kB 0 B
build/block-library/style.css 7.23 kB 0 B
build/block-library/theme-rtl.css 683 B 0 B
build/block-library/theme.css 685 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 48.1 kB 0 B
build/components/index.js 179 kB 0 B
build/components/style-rtl.css 16.9 kB 0 B
build/components/style.css 16.9 kB 0 B
build/compose/index.js 6.66 kB 0 B
build/core-data/index.js 11.4 kB 0 B
build/data-controls/index.js 1.29 kB 0 B
build/data/index.js 8.44 kB 0 B
build/date/index.js 5.47 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 3.1 kB 0 B
build/edit-navigation/index.js 4.05 kB 0 B
build/edit-navigation/style-rtl.css 485 B 0 B
build/edit-navigation/style.css 485 B 0 B
build/edit-post/style-rtl.css 12.2 kB 0 B
build/edit-post/style.css 12.2 kB 0 B
build/edit-site/index.js 11.4 kB 0 B
build/edit-site/style-rtl.css 5.18 kB 0 B
build/edit-site/style.css 5.18 kB 0 B
build/edit-widgets/index.js 7.77 kB 0 B
build/edit-widgets/style-rtl.css 4.67 kB 0 B
build/edit-widgets/style.css 4.66 kB 0 B
build/editor/editor-styles-rtl.css 428 B 0 B
build/editor/editor-styles.css 431 B 0 B
build/editor/style-rtl.css 5.07 kB 0 B
build/editor/style.css 5.08 kB 0 B
build/element/index.js 4.65 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/index.js 7.63 kB 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/hooks/index.js 2.13 kB 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/is-shallow-equal/index.js 710 B 0 B
build/keyboard-shortcuts/index.js 2.51 kB 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/index.js 3.13 kB 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/media-utils/index.js 5.29 kB 0 B
build/notices/index.js 1.79 kB 0 B
build/nux/index.js 3.4 kB 0 B
build/nux/style-rtl.css 616 B 0 B
build/nux/style.css 613 B 0 B
build/plugins/index.js 2.56 kB 0 B
build/primitives/index.js 1.5 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/rich-text/index.js 14.8 kB 0 B
build/server-side-render/index.js 2.67 kB 0 B
build/shortcode/index.js 1.69 kB 0 B
build/token-list/index.js 1.28 kB 0 B
build/url/index.js 4.02 kB 0 B
build/viewport/index.js 1.84 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.18 kB 0 B

compressed-size-action

@johnwatkins0 johnwatkins0 self-assigned this Feb 21, 2020
@johnwatkins0 johnwatkins0 added the [Type] Enhancement A suggestion for improvement. label Feb 21, 2020
@johnwatkins0
johnwatkins0 force-pushed the try/19226-restore-revision-success-message branch from 9301f39 to e55b740 Compare May 3, 2020 15:59
"@wordpress/keycodes": "file:../keycodes",
"@wordpress/priority-queue": "file:../priority-queue",
"@wordpress/rich-text": "file:../rich-text",
"@wordpress/shortcode": "file:../shortcode",

@johnwatkins0 johnwatkins0 May 3, 2020 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging latest master, got this ESLint error for a file in block-editor:

42:1 error '@wordpress/shortcode' should be listed in the project's dependencies. Run 'npm i -S @wordpress/shortcode' to add it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Restoring a revision doesn't display a success message

2 participants