wpbones / flags
Flags for WP Bones
Requires
- php: >=7.4
- symfony/yaml: ^5.0
README
Flags for WP Bones is a PHP package designed for the WP Bones framework, allowing you to enable or disable features in plugins using YAML configuration files. This approach simplifies feature management and makes the plugin more flexible and easy to configure, even for non-technical users.
Key features
- Enable and Disable Features: Using flags, you can easily activate or deactivate specific plugin features.
- YAML Configuration: YAML files are easy to read and modify, and can be used to configure various plugin options.
- Flexibility: The path and name of the YAML file can be customized through the plugin configuration.
- Reusability: The same YAML file can be used across different plugins, improving code consistency and maintenance.
Installation
You can install third party packages by using:
php bones require wpbones/flags
I advise to use this command instead of composer require
because doing this an automatic renaming will done.
You can use composer to install this package:
composer require wpbones/flags
You may also to add "wpbones/flags": "~0.7"
in the composer.json
file of your plugin:
"require": { "php": ">=7.4.0", "wpbones/wpbones": "~1.5", "wpbones/flags": "~0.7" },
and run
composer install
YAML file example
# The version of the file is 1.0.0 version: "1.0.0" example: # Enable or disable the Example feature enabled: true # Throttle request time in minutes # By setting this value to 0, the feature will be disabled throttle: 5 # Request timeout timeout: 0
You can find more information about the YAML syntax in the Symfony documentation.
YAML file configuration path
You can creare your own YAML file everywhere in your plugin, but I suggest to create it in the config
directory of your plugin.
The default path and filename is:
config/flags.yaml
Set the flags path in the plugin configuration
You can set the path and filename in the plugin configuration by adding the following line in the config/plugin.php
file of your plugin:
<?php if (!defined('ABSPATH')) { exit(); } return [ /* |-------------------------------------------------------------------------- | Logging Configuration |-------------------------------------------------------------------------- | | Here you may configure the log settings for your plugin. | | Available Settings: "single", "daily", "errorlog". | | Set to false or 'none' to stop logging. | */ 'log' => 'errorlog', 'log_level' => 'debug', /* |-------------------------------------------------------------------------- | Flags package path Configuration |-------------------------------------------------------------------------- | | Here you may configure the flags path for your plugin. | */ 'flags' => [ 'path' => 'config/flags.yaml', ], ...
Basic usage
You can use the wpbones_flags
helper function to get the value of a flag:
wpbones_flags()->get('example.enabled', false);
The first parameter is the flag name, and the second parameter is the default value if the flag is not found.
You may also use the class directly:
use WpBones\Flags\Flags; $flags = new Flags(); $flags->get('example.enabled', false);
or by using the static method:
use WpBones\Flags\Flags; Flags::get('example.enabled', false);
Set the flags path by method
You may also set/change the path by using:
wpbones_flags('config/flags.yaml')->get('logger.enabled', false);
or the fluent method withPath
:
wpbones_flags()->withPath('config/flags.yaml')->get('logger.enabled', false);
by using the class directly:
use WpBones\Flags\Flags; $flags = new Flags(); $flags->withPath('config/flags.yaml')->get('logger.enabled', false);
or by using the static method:
use WpBones\Flags\Flags; Flags::withPath('config/flags.yaml')->get('logger.enabled', false);