afragen / wp-dependency-installer

A lightweight class to add to WordPress plugins/themes to automatically install plugin dependencies.
MIT License
205 stars 32 forks source link

Allow config to be defined in composer.json #24

Closed seothemes closed 5 years ago

seothemes commented 6 years ago

It would be nice to have the option to place the config inside composer.json instead of a separate file. I was thinking it could go under the extra property. Something like this:

{
  "name": "project-name",
  "type": "library",
  "extra": {
    "wp-dependency-installer": [
      {
        "name": "Query Monitor",
        "host": "wordpress",
        "slug": "query-monitor/query-monitor.php",
        "uri": "https://wordpress.org/plugins/query-monitor/",
        "optional": false
      }
    ]
  }
}

If no wp-dependencies.json file is found, then check for the config in composer.json.

afragen commented 6 years ago

Is it really that much trouble to create an extra JSON file. Also, if there’s an error in the composer.json that doesn’t parse of something I’m not able to tell the user how to fix it.

From my (the developer) standpoint having one file that does one thing is simpler and safer.

seothemes commented 5 years ago

I agree - the main reason I wanted to remove the wp-dependencies.json file was to keep it out of sight of customers. I was trying to make a child theme as minimal as possible and stick to just style.css, functions.php and screenshot.png. The vendor directory is hidden in the WordPress admin so that's not an issue.

I found a better solution by using the register() method, and to avoid requiring a plugin path I called load_hooks() first, instead of run():

$config = apply_filters( 'prefix_plugin_dependencies', [
    [
        'name'     => 'One Click Demo Import',
        'host'     => 'wordpress',
        'slug'     => 'one-click-demo-import/one-click-demo-import.php',
        'uri'      => 'https://wordpress.org/plugins/one-click-demo-import/',
        'optional' => false,
    ],
] );

// Install and active dependencies.
\WP_Dependency_Installer::instance()->load_hooks();
\WP_Dependency_Installer::instance()->register( $config );
afragen commented 5 years ago

I’m so sorry I didn’t understand what you were trying to accomplish. We could’ve found the solution a lot quicker.

I built that functionality in just in case and haven’t really thought about it as I always use a wp-dependencies.json file.

I’m glad you found a solution.