cabrerahector / wordpress-popular-posts

WordPress Popular Posts - A highly customizable WordPress widget that displays your most popular posts.
https://wordpress.org/plugins/wordpress-popular-posts/
GNU General Public License v2.0
280 stars 82 forks source link

PHP error in WordPressPopularPosts\Image::get() when using an array in the crop image sizes #380

Closed davidbru closed 6 months ago

davidbru commented 6 months ago

Describe the problem

My theme uses an array for the crop value. See the following two examples large vs td_218x150.

  ["large"]=>
  array(3) {
    ["width"]=>
    string(4) "1024"
    ["height"]=>
    string(4) "1024"
    ["crop"]=>
    bool(false)
  }
  ["td_218x150"]=>
  array(3) {
    ["width"]=>
    int(218)
    ["height"]=>
    int(150)
    ["crop"]=>
    array(2) {
      [0]=>
      string(6) "center"
      [1]=>
      string(3) "top"
    }
  }

According to https://developer.wordpress.org/reference/functions/add_image_size/ this is ok. But since updating from version 5.4.2 to 6.3.4 the frontend throws the following error:

[27-Dec-2023 11:30:48 UTC] PHP Fatal error:  Uncaught TypeError: Argument 4 passed to WordPressPopularPosts\Image::get() must be of the type bool, array given, called in /var/www/html/wp-content/plugins/wordpress-popular-posts/src/Output.php on line 547 and defined in /var/www/html/wp-content/plugins/wordpress-popular-posts/src/Image.php:114
Stack trace:
#0 /var/www/html/wp-content/plugins/wordpress-popular-posts/src/Output.php(547): WordPressPopularPosts\Image->get(835944, Array, 'featured', Array, 'predefined')
#1 /var/www/html/wp-content/plugins/wordpress-popular-posts/src/Output.php(346): WordPressPopularPosts\Output->get_thumbnail(835944)
#2 /var/www/html/wp-content/plugins/wordpress-popular-posts/src/Output.php(271): WordPressPopularPosts\Output->render_post(Object(stdClass), 1)
#3 /var/www/html/wp-content/plugins/wordpress-popular-posts/src/Rest/WidgetEndpoint.php(125): WordPressPopularPosts\Output->build_output()
#4 /var/www/html/wp-content/plugins/wordpress-popular-posts/src/Rest/WidgetEndpoint.php(80): WordPressPopularPosts\Rest\WidgetEndpoint in /var/www/html/wp-content/plugins/wordpress-popular-posts/src/Image.php on line 114

As far as I can tell the plugin expects a boolean value, and stumbles over the array definition.

Steps to reproduce it

  1. Register an image size with an array definition as the crop value
  2. Install WPP 5.4.2 --> Everything works fine in the frontend
  3. Update to WPP 6.3.4 --> Frontend does not work anymore.

Expected behavior

The plugin should not throw an error.

Screenshots

Environment

Additional context

cabrerahector commented 6 months ago

Hi @davidbru,

Interesting. Thanks for the heads up (and for the very detailed report), I'll look into it ASAP.

davidbru commented 6 months ago

@cabrerahector No problem. For now I reverted back to the old version (which works just fine).

cabrerahector commented 6 months ago

Hey @davidbru,

This commit fixed the issue on my end. You can either wait for the official update to be released via wordpress.org (which should happen some time early January) or if you prefer to not wait then update the plugin, disable it, manually add that change to src/Image.php, and then enable the plugin again.

davidbru commented 6 months ago

Hay @cabrerahector, thanks for the quick info. With your bugfix everything works fine. Cheers, David