Inpsyde Modularity Properties

If you are building a WordPress plugin, you typically need to reference specific values repeatedly—for example, the version or base path. The same is true for a theme or a library.

A pretty common approach is to define these as constants. There are boilerplates that suggest this approach.

If you have a "main" plugin class, you might have these "constants" as properties too.

This approach immediately introduces duplication because some of the values are typically also found in the Plugin Header, like the version. Hack together a release script and problem solved(?!).

The various Propreties classes of Inpsyde Modularity deal with this topic. They centralize the "constants" in one place, but it also conveniently sets them up.

Instead of defining typical constants one by one:

define('PLUGIN_NAME_VERSION', '1.0.0');
define('PLUGIN_NAME_PATH', __FILE__);

you can do this:

use Inpsyde\Modularity\Properties\PluginProperties;

$pluginPropreties = PluginProperties::new('/path/to/plugin-main-file.php');

and it will parse the plugin's header and set up the properties. You can then access the values by calling methods on the object:

$pluginPropreties->version(); // version of the plugin
$pluginPropreties->name(); // name of the plugin

If you are using Modularity for a theme, you have the ThemeProperties class:

$themeProperties = ThemeProperties::new('/path/to/theme-directory/');

For completeness, there are two more classes, the LibraryPropreties and the BasePropreties.

The BasePropreties doesn't parse any file; it expects you to pass some values manually to it. The LibraryProperties parses a composer.json file. More about these two somewhere else.

For more detailed information, check the documentation.

Next up the ...

YAML and the Norway problem | implenton

As a curiosity...

When it comes to booleans, in YAML you can use y, YES, on, True and other variations for the true value.

As you probably guessed, for the false value, you can use n, NO, off, etc.

But NO is also Norway's ISO 3166-1 alpha-2 country code. Oops. Edge case.

Of course, you can just escape it, as this article rightfully suggests.