inpsyde / modularity

A PSR-11 implementation for WordPress Plugins, Themes or Libraries.
https://inpsyde.github.io/modularity/
GNU General Public License v2.0
44 stars 4 forks source link

Allow to retrieve custom fields from plugin header #13

Closed strangerkir closed 3 years ago

strangerkir commented 3 years ago

Is your feature request related to a problem? Please describe. I want to get the WC requires at least field value, that is not provided by properties. I tried to extend PluginProperties and overwrite HEADERS constant, but it's not worked because it's called in the constructor as self::HEADERS , so the constant is used from the PluginProperties class, not from the extending one.

Describe the solution you'd like It would be great to have an option to get value of any of WP plugin header fields with Properties::get(). Probably, just merge everything returned by the get_plugin_data() function, as discussed in Slack.

Describe alternatives you've considered Another option is to allow classes extending the PluginProperties to overwrite the parsed headers list. For example, use the protected method headers() instead of a class constant. Or, it should be possible to override that constant if it's used as static::HEADERS instead of self::HEADERS.

Additional context This is how it's expected to work. Plugin header:

/**

Usage of properties:

    $pluginProperties->get('WC requires at least'); //5.0

or if this property was configured in custom HEADERS constant:

Usage of properties:

    $pluginProperties->get('wcRequiresAtLeast'); //5.0
Chrico commented 3 years ago

I've implemented some improvements for supporting non-default Plugin headers here https://github.com/inpsyde/modularity/commit/6a0283d9114e317038eff8af82ce29adb837c970 . The new logic will only map internally default Plugin Headers to generalize them to Properties. Custom Headers will not be touched and stay as they are added.

You can access for example following Headers:

/**
 * Author: Inpsyde GmbH
 * AuthorURI: https://www.inpsyde.com
 *
 * WC requires at least: 2.2
 * WC tested up to: 2.3
 */ 

like following:

$properties = Inpsyde\Modularity\Properties\PluginProperties::new(__FILE__);

// Default Headers
$properties->author();  // Inpsyde GmbH
$properties->get(Properties::PROP_AUTHOR); // same as above

$properties->authorUri();  // https://www.inpsyde.com
$properties->get(Properties::PROP_AUTHOR_URI); // same as above

// Custom Headers
$properties->get('WC requires at least'); // 2.2
$properties->get('WC tested up to'); // 2.3
Chrico commented 3 years ago

released with version 1.3.0. Please close if this issue is consider for you as solved @strangerkir . :)

strangerkir commented 3 years ago

Please close if this issue is consider for you as solved

Sorry, just noticed your comment. Yep, everything is fine. Thanks!