-
Notifications
You must be signed in to change notification settings - Fork 384
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
Ensure validation error sources
property is always iterable
#6846
Conversation
@westonruter As noted in the original issue, I was not able to reproduce this error locally. So the backend part that you suggested is added as-is as I had no way to test it out. When it comes to the JS part, I wrote some unit tests in order to make sure the issue is fixed. |
sources
property is always iterable
Plugin builds for 87a0424 are ready 🛎️!
|
$item['validation_errors'] = array_map( | ||
static function ( $validation_error ) { | ||
if ( ! isset( $validation_error['sources'] ) || ! is_array( $validation_error['sources'] ) ) { | ||
$validation_error['sources'] = []; | ||
} | ||
|
||
return $validation_error; | ||
}, | ||
wp_list_pluck( $data, 'data' ) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change isn't needed due to the JS changes, right? Maybe we should leave the PHP changes as-is until we can identify the underlying cause. So only fix the symptoms until we can fix the underlying cause.
So that would mean reverting the PHP changes in this PR but leaving the JS changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I'll revert the backend changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be worthwhile to retain the PHP commit and its revert commit so that we can refer back to it when we return to identifying the underlying cause? I'm not bothered by such commits in the history 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
987ec15
to
fda65f2
Compare
assets/src/block-validation/components/error/get-error-source-title.js
Outdated
Show resolved
Hide resolved
assets/src/block-validation/components/error/test/get-error-source-title.js
Show resolved
Hide resolved
Rebasing onto |
assets/src/block-validation/components/error/get-error-source-title.js
Outdated
Show resolved
Hide resolved
This reverts commit 50e557b.
Co-authored-by: Weston Ruter <[email protected]>
4723f40
to
60d170a
Compare
Co-authored-by: Piotr Delawski <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this by manually forcing null
sources via:
--- a/includes/validation/class-amp-validation-manager.php
+++ b/includes/validation/class-amp-validation-manager.php
@@ -790,7 +790,7 @@ public static function add_validation_error( array $error, array $data = [] ) {
$node = $data['node'];
}
- if ( self::is_validate_request() ) {
+ if ( self::is_validate_request() && 0 ) {
if ( ! empty( $error['sources'] ) ) {
$sources = $error['sources'];
} elseif ( $node ) {
And:
--- a/includes/validation/class-amp-validation-manager.php
+++ b/includes/validation/class-amp-validation-manager.php
@@ -1591,7 +1591,7 @@ public static function decorate_filter_source( $value ) {
if ( isset( self::$current_hook_source_stack[ current_filter() ] ) ) {
$sources = self::$current_hook_source_stack[ current_filter() ];
array_pop( $sources ); // Remove self.
- $source['sources'] = $sources;
+ $source['sources'] = null;
}
return implode(
'',
Before the changes in this PR I got an error on the Settings screen:
But after the changes in this PR, I no longer got any errors on:
- Settings screen
- Onboarding Wizard
- Plugin activation
Summary
Fixes #6827
In some specific cases, the
sources
property returned with a validation error may not be an array (it isnull
instead). However, in the Site Scan feature we're relying on a fact thatsources
is iterable. When it's not the case, an error is thrown in WP admin.This PR fixes this issue in the following ways:
sources
property is an array from the very beginning (as suggested in Error:s.sources
is not iterable #6827 (comment) and the next comment).sources
is a non-empty array before attempting to iterate over it.Checklist