ConfigPress

ConfigPress is the WordPress plugin with the main objective to allows developers to manage custom settings in an easy and flexible way. It is based on INI standard and is slightly extended to support section inheritance as well as conditional sections. Check the reference table below for more information on how to use ConfigPress.

ConfigPress can be used as a stand-alone application for your own system or another CMS. Download the source code from official WordPress repository. There are two main classes that perform the "heavy lifting": application/Reader.php is responsible for parsing INI string and application/Evaluator.php extends the INI standard and evaluate conditional sections. You are free to use any of these classes as long as you send me a quick message with two words Thank you or something like that.

Download ConfigPress

INI format allows to group your options in sections. The section name appears on a line by itself, in square brackets ([ ]). All keys after the section declaration are associated with that section. There is no explicit end of section delimiter; sections end at the next section declaration, or the end of the file. Sections may not be nested.

[section-name]
optionA = value
optionB = value

ConfigPress extends standard INI format and allows to inherit options from other section with colon (:) key. In the example below, section-b inherits optionA from section-a.

[section-a]
optionA = value

[section-b : section-a]
optionB = value

There is a way to define a logical condition as a part of section. All options related to conditional section will be available only when logical condition is not false.

; this conditional section returns true and option is available as section-a.option 
; (see alias for more information )
[10 + 5 >= 4 as section-a]
option = value

; this conditional section will return false if apache environment variable APPL_ENV is not dev
; (see variables for more information)
[$_ENV.APPL_ENV == dev]
option = value

; this conditional section will return true if today is between Sep 15th 2015 and Jan 12th 2016
; (see callback for more information)
[@time >= 1442289600 && @time <= 1452574800]
option = value

Below is the list of all possible operators ordered by precedence from highest to lowest. Parentheses may be used to force precedence, if necessary.

  • * , / - Multiplication, Division
  • +, - - Addition, Subtraction
  • ==, !=, ===, !==, <, >, <=, >=, <> - Equal, Not equal, Identical, Not identical, Less than, Greater than, Less than or equal to, Greater than or equal to, Not equal
  • &&, || - And, Or
  • as - condition's alias

You can include any globally accessible variable to your conditional section by specifying a variable’s name starting with dollar sign ($). ConfigPress will try to find it in $GLOBALS super global array. If your variable is inside an array or an object, use dot (.) and full path to it (e.g. $_SERVER.HTTP_REFERER).

; define aam section and all options within it if GET parameter page is equal to “aam”
[$_GET.page == aam as aam]
option = value

; if $GLOBALS[‘profile’][‘details’][‘username’] is set and equal to string “me”
[$profile.details.username == me]
option = value

In order to invoke a function or a class method, use ampersand sign (@) and a valid callback after it. The returned value from an executed callback function/method is used to evaluate a conditional section. If function or class method can’t be called, a false boolean value will be returned.

; section-a is available only when MyClass::testMethod returns number 5
[@MyClass::testMethod == 5 as section-a]
option = value

Alias is a convenient way to name your conditional or even non-conditional section.

; with an alias you can retrieve the option value with “environment.option”
[$_SERVER.HTTP_HOST == localhost as environment]
option = value 

; without an alias the only way to retrieve option value is to use full condition which is not that convenient  “@time == 1452574800.option”
[@time == 1452574800]
option = value

; non-conditional section can also use alias. Retrieve option value with “something.option”
[section-a as something]
option = value

In order to retrieve an option in your PHP script, use ConfigPress::get static method. It accepts two parameters: (string) $option for option name and (mixed) $default for default value. Both parameters are optional. If no $option is specified, the entire configuration set is returned.

if (class_exists('ConfigPress')){
    $option = ConfigPress::get('section.option', null);
} else {
    $option = null;
}