woocommerce / wc-smooth-generator

Smooth product, customer and order generation for WooCommerce
315 stars 49 forks source link

Check for autoload in various locations. #37

Closed jenkoian closed 4 years ago

jenkoian commented 4 years ago

Currently the assumption is that the vendor library will be installed relative to the main plugin file, i.e. $plugin_dir/vendor, however there are lots of instances where this may not be the case.

One common use case is for vendor to be relative to wp-content such as is the case when following a composer in wordpress recipe such as this one: https://composer.rarst.net/recipe/site-stack/ therefore it makes sense to check if the vendor and thus autoload file is in here first.

Taking this a bit further, there's a (mostly) unwritten convention which is to use an env var to mark the composer vendor dir (see discussion here: https://github.com/composer/composer/issues/2904) so it makes sense to check if that exists first.

There may also be cases where developers are storing vendors in the project root or one level above but not using WP_CONTENT_DIR constant so makes sense to check there too.

So this will check all of the above and can be summed up as thus:

Check for vendor directory in the following:

  1. COMPOSER_VENDOR_DIR.
  2. WP_CONTENT_DIR.
  3. Two directories up, assuming this gets installed in wp-content/plugins vendor may be in the project root.
  4. One directory up, assuming this gets installed in wp-content/plugins vendor may be in wp-content.
  5. Defaults to the current directory.
jenkoian commented 4 years ago

This also relates to https://github.com/woocommerce/wc-smooth-generator/pull/19

jenkoian commented 4 years ago

Any feedback on this? Alternatively make this library available as something installable via wp package install rather than a plugin??

rrennick commented 4 years ago

@jenkoian Thanks for submitting the PR. It is generally a discouraged practice to load PHP files outside of a plugin's directory unless that file is maintained by the plugin. The readme on the site-stack project has [nstructions for adding autoload.php to the site's configuration (under Autoload heading).

You could make smooth generator compatible with that setup by adding a file and a class exist check to the plugin load sequence.

rrennick commented 4 years ago

@jenkoian #40 implements my recommendation above so I'll close this PR.

jenkoian commented 4 years ago

That works, thanks!