Php Warning log with snippet

Hello,
I hope you’re fine.
I’ve a lot of php warning in my debug.log from the snippet plugin file when I activate debug log. Here is one log:

PHP Warning: Trying to access array offset on value of type null in wp-content/plugins/snippet/functions/class-snippet-meta-box.php on line 235

There are the same log for the following lines of the file class-snippet-meta-box.php :
`

235
232
248
252
256
260
264
267
269
275
279
318
320
326
330
333
335
339
341
289
293
297
301
304
306
`
Could you please say what to do to don’t have these errors anymore ?

Thank you in advance.

Hello @Jloupf,

Thanks for writing to us.

As per your given error, it seems that the error is coming from the snippet plugin, I would suggest you please update the PHP version to 8.0 or latest PHP version and recheck it again. You can contact your hosting provider to upgrade the PHP version. Please note you must take a backup of your files and database before upgrading it.

Hope it helps
Thanks

Hi there,

We are using PHP 8.1 and are experiencing the same issue using Snippet version 2.1.2 and WP version 6.5.5

image
the ternary results in a null value that is not being handled; the code after this assumes that $metabox_value is an array.
You just need to handle the null value (exit early or wrap references to $metabox_value with null/empty checks.

Hi Alison,

I have checked it in my local environment but didn’t find any issue you described here. Can you please provide any screenshots marked with the issue or any video that helps us recognize the problem and help us to replicate it?

I would also suggest you create a new thread and share the details on it.

Thanks

Hi @tristup,

The error is caused by insufficient checks for $metabox_value.
In the screenshot I shared previously, you can see that the default value is null.
However, just after that, there is some code that will check if $metabox_value is empty which is good.
The code:

<?php
if ( empty ( $metabox_value) && array_key_exists( 'default_value', $metabox ) ) {
  $metabox_value = $this->get_default_value( $metabox['default_value'], $post_id );
}

The problem with this is that the new default value for $metabox_value will only be set if array_key_exists( 'default_value', $metabox ) is also true.

In the case of @Jloupf and myself, array_key_exists( 'default_value', $metabox ) is false which means the code block above does not happen and $metabox_value stays as null.
This is because $metabox does not contain default_value.
Here is what I see when I var_dump($metabox):

array(6) {
  ["id"]=> string(37) "_snippet_article_alternative_headline",
  ["name"]=> string(19) "alternativeHeadline",
  ["label"]=> string(20) "Alternative Headline",
  ["description"]=> string(38) "A secondary title of the CreativeWork.",
  ["schema_type"]=> string(4) "Text",
  ["type"]=> string(4) "text"
}

You can see that the default_value key is missing which is why the conditional statement fails to run.

All of the following code assumes that $metabox_value is not null and does not perform any checks to handle a possible empty or null value.

take this code in the same renderMeta method for example:

you can see here there are no checks if $metabox_value is an array or not, it just assumes it is which is why there are PHP errors.

The simple fix is to update the conditional statement that sets a new default value for $metabox_value to ensure it sets an appropriate value, or just check if they are empty before output/reference.

If you are leveraging PHP 8 features, you can change
value="<?php echo esc_attr( $metabox_value['name'] ); ?>" class="postbox">
to
value="<?php echo esc_attr( $metabox_value['name'] ?? '' ); ?>" class="postbox">
to solve the problem.

Please take the time to read Tco_Snippet_Meta_Box::renderMeta and understand the logic for the default values on $metabox_value and how the code is prone to error.

Thanks

1 Like

Hello @prakash_s,

Thank you for your answer.
PHP 8 is already installed on my server.

Hi @AlisonARU,

thank you for your investigation.
I’ve followed your advice and added ?? '' after each $metabox_value['value'] in the lines specified in logs.
No more logs.
Thank you

Hi @Jloupf,
My comments were more for @tristup so they can fix the actual problem within their plugin.
If the plugin is updated your changes will be discarded unless they fix the root cause of the problem.
Glad it helps for a temporary workaround, though.

Hey @Jloupf,

You are most welcome. We’re glad the patch given by @Tristup works out for you.

@AlisonARU:
We have already added this to our issue tracker. The plugin will be updated soon.

Thanks.

Hi @ruenel,

Many thanks. I look forward to the updates.

Hi Alison,

You are most welcome. Please keep eye on the updates.