AttLii / image-blur

Wordpress plugin that generates blurred versions of uploaded images
21 stars 1 forks source link

Requires autoload if class does not exist #24

Closed lightningspirit closed 2 years ago

lightningspirit commented 2 years ago

This commit fixes an issue when using this plugin with a root autoload but the plugin was installed via zip upload (not using the composer at the root of the project).

AttLii commented 2 years ago

23

I'll test this once I get some instructions to the issue

AttLii commented 2 years ago

Hey, I'll close this issue since I haven't heard from you back in a while.

This plugin is meant to be included in mu-plugins or plugins folder, like other plugins. I haven't come across any Wordpress installations where plugins would be installed in root autoload, which makes this change serve only small portion of Wordpress installations. Maybe I am wrong on this.

However, there is a bug that your PR would fix, which happens when someone accidentally includes this plugin in both mu-plugins and plugins. It would still cause double processing, since both locations would instantiate the main class. I'll make a separate PR about it.

closes #23

kasparsd commented 2 years ago

I haven't come across any Wordpress installations where plugins would be installed in root autoload

This plugin appears to include the composer.json file which allows users to require this package as a dependency in their projects. This is often used in enterprise WordPress setups where everything including the WordPress core is added as a Composer dependency. Here is a great explanation of such setups https://composer.rarst.net/recipe/site-stack/

By setting the package type to wordpress-plugin here:

https://github.com/AttLii/image-blur/blob/f268f2fb2c4915032d896a14bb9cf715481b177c/composer.json#L3

it is implying that the plugin supports the composer/installers package which is able to place wordpress-plugin under wp-content/plugins or any other location within the projects directory structure.

In such scenarios Composer autoloader from vendor/autoload.php is included somewhere in wp-config.php for example so the vendor directory underwp-content/plugins/image-blur/vendor is never created and the include here would fail

https://github.com/AttLii/image-blur/blob/f268f2fb2c4915032d896a14bb9cf715481b177c/image-blur.php#L21

because vendor/autoload.php wouldn't exist on such setups.

Therefore, it is best practice to load the "local" vendor/autoload.php only if the class resolved by that autoloader isn't available (and hasn't been loaded by the project-level autoloader).

AttLii commented 2 years ago

Hey, thanks for explanation!

I still don't understand why requiring this plugin would remove the vendor-folder. I tested the gist found in your link and example plugins don't really line up with the problem we have, since none of the plugins that are hosted on wpackagist use their own autoloader. Also requiring my plugin at the root doesn't remove the vendor-folder found in my release.

Also, composer/installers isn't in image-blur dependencies, should it?