jchristopher / attachments

[WordPress Plugin] Attachments allows you to simply append any number of items from your WordPress Media Library to Posts, Pages, and Custom Post Types
wordpress.org/extend/plugins/attachments/
GNU General Public License v2.0
241 stars 78 forks source link

PHP 7.1.1 Warning: Illegal String Offset #173

Closed wunc closed 6 years ago

wunc commented 7 years ago

Getting the following errors on PHP 7.1.1:

Illegal string offset 'file' in /var/www/cleanroom/web/app/plugins/attachments/classes/class.attachments.php on line 1329 Warning: Illegal string offset 'file' in /var/www/cleanroom/web/app/plugins/attachments/classes/class.attachments.php on line 1330 Warning: Illegal string offset 'file' in /var/www/cleanroom/web/app/plugins/attachments/classes/class.attachments.php on line 1335

In PHP 7.1.1, empty strings are not auto-converted to arrays when attempting to assign a value to a string-based key:

$attachment_meta = '';
$attachment_meta['file'] = 'filename';

will convert $attachment_meta to an array and properly assign the key in earlier versions of PHP, but will result in a warning and unexpected behavior in PHP 7.1.1.

If there is no attachment metadata, wp_get_attachment_metadata( $attachment->id ) will return and empty string, which results in the above errors.

One solution would be to make sure you are getting an array back from wp_get_attachment_metadata() by doing explicit type-conversion to array on line 1325.