stripe-php icon indicating copy to clipboard operation
stripe-php copied to clipboard

Update init.php to be safe when multiple wordpress plugin has this lib

Open karolymarton opened this issue 3 years ago • 3 comments

Describe the bug

Please load the classes only when not loaded already. I ran into an issue where multiple Wordpress plugin had this lib in its folders and WP shut down because of fatal error: plugins/membermouse/lib/stripe/lib/Stripe.php E_COMPILE_ERROR Cannot declare class Stripe\Stripe, because the name is already in use.

To Reproduce

  1. Install membermouse and learndash plugins at once onto your Wordpress.
  2. Experience the fatal error after plugin activate.

Expected behavior

If lib is already loaded, don't load it again.

Code snippets

No response

OS

ubuntu

PHP version

20.04

Library version

all

API version

N/A

Additional context

No response

karolymarton avatar Aug 30 '23 16:08 karolymarton

Hello @mdesign83, thanks for the report.

It's not standard practice for PHP libraries to conditionally define themselves (do you know any libraries that behave this way?) If multiple copies of stripe-php exist on your system, this is a problem that should be addressed explicitly. One of your plugins might depend on a newer version, and another plugin might depend on an older version, and if stripe-php silently just chooses to favor whichever one happened to be loaded first, this papers over the problem and could result in much more confusing errors down the line.

I recommend reaching out to the authors of the plugins you are trying to use to try and get them to conditionally load Stripe. The wordpress plugin has enough information to know which versions of stripe-php it is compatible with, and if it tries to load stripe but detects that stripe-php is already defined, it should be capable of checking the version and deciding whether it can use the already-defined one instead of loading its own. The wordpress plugin best practices makes it pretty clear that plugin authors are expected to check for existing implementations before declaring their own.

Conflicts between dependencies of wordpress plugins seems to be a pretty common problem in the Wordpress ecosystem. This StackOverflow thread might have some useful advice. Particularly, the second answer describes some "code prefixing" tools that you can use to eliminate conflicts like this and have multiple versions of your dependencies existing at the same time.

Closing this issue, as we are not going to take action in stripe-php.

richardm-stripe avatar Sep 01 '23 19:09 richardm-stripe

(There's some previous discussion of similar problems https://github.com/stripe/stripe-php/issues/54) -- although in those cases plugin authors do attempt to re-use a previously-defined Stripe instead of defining their own, but they face version incompatibility issues.

richardm-stripe avatar Sep 01 '23 19:09 richardm-stripe

Thanks for the detailed answer.

karolymarton avatar Sep 04 '23 19:09 karolymarton