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

Gutenberg: Fix displaying validation warning and usage of PHP function #1612

Merged
merged 3 commits into from
Nov 16, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions assets/js/amp-block-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ var ampBlockValidation = ( function() { // eslint-disable-line no-unused-vars
* @return {void}
*/
handleValidationErrorsStateChange: function handleValidationErrorsStateChange() {
var currentPost, validationErrors, blockValidationErrors, noticeElement, noticeMessage, blockErrorCount, ampValidity;
var currentPost, validationErrors, blockValidationErrors, noticeOptions, noticeMessage, blockErrorCount, ampValidity;

if ( ! module.isAMPEnabled() ) {
if ( ! module.lastStates.noticesAreReset ) {
Expand Down Expand Up @@ -249,16 +249,20 @@ var ampBlockValidation = ( function() { // eslint-disable-line no-unused-vars
noticeMessage += wp.i18n.__( 'Non-accepted validation errors prevent AMP from being served, and the user will be redirected to the non-AMP version.', 'amp' );
}

noticeElement = wp.element.createElement( 'p', {}, [
noticeMessage + ' ',
ampValidity.review_link && wp.element.createElement(
'a',
{ key: 'review_link', href: ampValidity.review_link, target: '_blank' },
wp.i18n.__( 'Review issues', 'amp' )
)
] );
noticeOptions = {
id: 'amp-errors-notice'
};
if ( ampValidity.review_link ) {
noticeOptions.actions = [
{
label: wp.i18n.__( 'Review issues', 'amp' ),
url: ampValidity.review_link
}
];
}

module.validationWarningNoticeId = wp.data.dispatch( 'core/editor' ).createWarningNotice( noticeElement, { spokenMessage: noticeMessage } ).notice.id;
wp.data.dispatch( 'core/notices' ).createNotice( 'warning', noticeMessage, noticeOptions );
Copy link
Member

Choose a reason for hiding this comment

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

I'm getting an error here now when attempting to edit an existing post:

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@westonruter Weird, that would indicate as if core/notices wouldn't exist. Is this with the latest version of Gutenberg?

Copy link
Member

Choose a reason for hiding this comment

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

Humm. I'm not seeing the issue anymore. I opened an incognito window and everything. I'll report if I see it again.

module.validationWarningNoticeId = noticeOptions.id;
},

/**
Expand Down Expand Up @@ -303,7 +307,7 @@ var ampBlockValidation = ( function() { // eslint-disable-line no-unused-vars
*/
resetWarningNotice: function resetWarningNotice() {
if ( module.validationWarningNoticeId ) {
wp.data.dispatch( 'core/editor' ).removeNotice( module.validationWarningNoticeId );
wp.data.dispatch( 'core/notices' ).removeNotice( module.validationWarningNoticeId );
module.validationWarningNoticeId = null;
}
},
Expand Down
3 changes: 2 additions & 1 deletion includes/admin/class-amp-editor-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ public function enqueue_block_editor_assets() {
) ) )
);

$locale_data = function_exists( 'wp_get_jed_locale_data' ) ? wp_get_jed_locale_data( 'amp' ) : gutenberg_get_jed_locale_data( 'amp' );
wp_add_inline_script(
'wp-i18n',
'wp.i18n.setLocaleData( ' . wp_json_encode( gutenberg_get_jed_locale_data( 'amp' ) ) . ', "amp" );',
'wp.i18n.setLocaleData( ' . wp_json_encode( $locale_data ) . ', "amp" );',
'after'
);
}
Expand Down
5 changes: 1 addition & 4 deletions includes/admin/class-amp-post-meta-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ public function enqueue_admin_assets() {

/**
* Enqueues block assets.
* The name of gutenberg_get_jed_locale_data() may change, as the Gutenberg Core merge approaches.
*
* @since 1.0
*/
Expand All @@ -203,9 +202,7 @@ public function enqueue_block_assets() {
'errorMessages' => $error_messages,
);

if ( function_exists( 'gutenberg_get_jed_locale_data' ) ) {
$script_data['i18n'] = gutenberg_get_jed_locale_data( 'amp' );
}
$script_data['i18n'] = function_exists( 'wp_get_jed_locale_data' ) ? wp_get_jed_locale_data( 'amp' ) : gutenberg_get_jed_locale_data( 'amp' );

wp_add_inline_script(
self::BLOCK_ASSET_HANDLE,
Expand Down
2 changes: 1 addition & 1 deletion includes/validation/class-amp-validation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1916,7 +1916,7 @@ public static function enqueue_block_validation() {
);

$data = wp_json_encode( array(
'i18n' => gutenberg_get_jed_locale_data( 'amp' ),
'i18n' => function_exists( 'wp_get_jed_locale_data' ) ? wp_get_jed_locale_data( 'amp' ) : gutenberg_get_jed_locale_data( 'amp' ),
'ampValidityRestField' => self::VALIDITY_REST_FIELD_NAME,
'isCanonical' => amp_is_canonical(),
) );
Expand Down
2 changes: 1 addition & 1 deletion tests/test-class-amp-meta-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function test_enqueue_admin_assets() {
* @see AMP_Post_Meta_Box::enqueue_block_assets()
*/
public function test_enqueue_block_assets() {
if ( ! function_exists( 'gutenberg_get_jed_locale_data' ) ) {
if ( ! function_exists( 'wp_get_jed_locale_data' ) && ! function_exists( 'gutenberg_get_jed_locale_data' ) ) {
$this->markTestSkipped( 'Gutenberg is not available' );
}

Expand Down