-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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: Undefined index "colors" warning #2982
fix: Undefined index "colors" warning #2982
Conversation
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.
Thanks for opening this PR. It fixes the bug described in #2198:
<b>Notice</b>: Undefined index: colors in <b>/srv/www/editor/htdocs/wp-content/plugins/gutenberg/lib/client-assets.php</b> on line <b>654</b><br />
but there is still another PHP notice
in this code that needs to be fixed:
<b>Notice</b>: Undefined index: wide-images in <b>/srv/www/wordpress-default/public_html/wp-content/plugins/lib/client-assets.php</b> on line <b>791</b><br>
I've updated my code to fix the second php notice |
lib/client-assets.php
Outdated
@@ -783,12 +783,12 @@ function gutenberg_editor_scripts_and_styles( $hook ) { | |||
$gutenberg_theme_support = get_theme_support( 'gutenberg' ); | |||
$color_palette = gutenberg_color_palette(); | |||
|
|||
if ( $gutenberg_theme_support && $gutenberg_theme_support[0]['colors'] ) { | |||
if ( $gutenberg_theme_support && ! empty( $gutenberg_theme_support[0]['colors'] ) ) { |
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'm wondering if empty
isn't powerful enough to make it possible to simplify it to:
if ( ! empty( $gutenberg_theme_support[0]['colors'] ) ) {
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.
Sorry, I'm a little lost. Can you explain further?
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.
Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.
You don't need to check if $gutenberg_theme_support
is truthy, because empty
does that, too. It makes this line shorter and works exactly the same.
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 just tested with the following code:
function mytheme_setup_theme_supported_features() {
add_theme_support( 'gutenberg', null );
}
add_action( 'after_setup_theme', 'mytheme_setup_theme_supported_features' );
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.
Yeah that's right, should I update the PR with this?
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.
If you like it, then go for it. Totally up to you :)
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.
Updated :)
That was quick, thanks. Testing your changes :) |
lib/client-assets.php
Outdated
$color_palette = $gutenberg_theme_support[0]['colors']; | ||
} | ||
|
||
$editor_settings = array( | ||
'wideImages' => $gutenberg_theme_support ? $gutenberg_theme_support[0]['wide-images'] : false, | ||
'wideImages' => ( $gutenberg_theme_support && ! empty( $gutenberg_theme_support[0]['wide-images'] ) ) ? $gutenberg_theme_support[0]['wide-images'] : false, |
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.
In this place it would get simplified to the following:
'wideImages' => ! empty( $gutenberg_theme_support[0]['wide-images'] ) ),
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 with the latest changes. I no longer see PHP notices. Nicely done @ayoolatj. I will leave it open to let you respond to my feedback. Let me know if you want to try to save some keystrokes as I suggested :)
Thanks for another update. It looks great now. 🎉 |
* master: (45 commits) Parser: Hide the core namespace in serialised data (WordPress#2950) fix: Undefined index warning (WordPress#2982) navigation via keydown PHPCS improvements (WordPress#2914) Update/html block description (WordPress#2917) Framework: Merge EditorSettingsProvider into EditorProvider Framework: Move the store initialization to a dedicated component Rotate header ellipsis to match block ellipsis add/459: Added support for ins, sub and sup Refresh nonce with heartbeat (WordPress#2790) Paste: add table support Bump version to 1.4.0. (WordPress#2954) Blocks: The custom classname should be persisted properly in the block's comment Editor: Fix the scroll position when reordering blocks Polish spacing in block ellipsis menu (WordPress#2955) Editor: HTML Editor per block (WordPress#2797) Show most frequently used blocks next to inserter (WordPress#2877) add/459: Added light grey background to selected inline boundaries Extend inline boundaries to other elements Move markdown fix to own plugin-compatibility file ...
* master: (45 commits) Parser: Hide the core namespace in serialised data (WordPress#2950) fix: Undefined index warning (WordPress#2982) navigation via keydown PHPCS improvements (WordPress#2914) Update/html block description (WordPress#2917) Framework: Merge EditorSettingsProvider into EditorProvider Framework: Move the store initialization to a dedicated component Rotate header ellipsis to match block ellipsis add/459: Added support for ins, sub and sup Refresh nonce with heartbeat (WordPress#2790) Paste: add table support Bump version to 1.4.0. (WordPress#2954) Blocks: The custom classname should be persisted properly in the block's comment Editor: Fix the scroll position when reordering blocks Polish spacing in block ellipsis menu (WordPress#2955) Editor: HTML Editor per block (WordPress#2797) Show most frequently used blocks next to inserter (WordPress#2877) add/459: Added light grey background to selected inline boundaries Extend inline boundaries to other elements Move markdown fix to own plugin-compatibility file ...
* master: (45 commits) Parser: Hide the core namespace in serialised data (WordPress#2950) fix: Undefined index warning (WordPress#2982) navigation via keydown PHPCS improvements (WordPress#2914) Update/html block description (WordPress#2917) Framework: Merge EditorSettingsProvider into EditorProvider Framework: Move the store initialization to a dedicated component Rotate header ellipsis to match block ellipsis add/459: Added support for ins, sub and sup Refresh nonce with heartbeat (WordPress#2790) Paste: add table support Bump version to 1.4.0. (WordPress#2954) Blocks: The custom classname should be persisted properly in the block's comment Editor: Fix the scroll position when reordering blocks Polish spacing in block ellipsis menu (WordPress#2955) Editor: HTML Editor per block (WordPress#2797) Show most frequently used blocks next to inserter (WordPress#2877) add/459: Added light grey background to selected inline boundaries Extend inline boundaries to other elements Move markdown fix to own plugin-compatibility file ...
Check that key exists and is not empty.
resolves: #2198