arraypress / wp-conditions
A flexible conditions/rules engine for WordPress with admin UI, REST API search, and extensible condition types.
Requires
- php: >=8.3
- arraypress/wp-abuseipdb: dev-main
- arraypress/wp-accept-language-utils: dev-main
- arraypress/wp-array-utils: dev-main
- arraypress/wp-composer-assets: dev-main
- arraypress/wp-countries: dev-main
- arraypress/wp-email-utils: dev-main
- arraypress/wp-ip-utils: dev-main
- arraypress/wp-ipinfo: dev-main
- arraypress/wp-ipqualityscore: dev-main
- arraypress/wp-proxycheck: dev-main
- arraypress/wp-referrer-utils: dev-main
- arraypress/wp-string-utils: dev-main
- arraypress/wp-user-agent-utils: dev-main
Requires (Dev)
- phpcompatibility/phpcompatibility-wp: ^2.1
- phpunit/phpunit: ^12.0
- squizlabs/php_codesniffer: ^3.13.5
- wp-coding-standards/wpcs: ^3.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Let a shop owner build a rule — "logged-in customers in the UK, on a weekend" — from an admin screen, and check it in code.
What it does
Anything with rules ends up needing this: a discount that applies to some carts, a fee that applies to some countries, a notice shown to some users. Hard-coding them means a release every time one changes; a settings field means parsing whatever the owner typed.
This gives the owner a builder — pick a condition, an operator, a value, and group them with any/all — and gives you one call that answers true or false.
Features
- Register a set of rules with its own admin screen, from one call
- Give owners built-in conditions — user role, country, day of week, cart total
- Add a condition of your own, with its own operators and values
- Group rules so any or all of them must match
- Search for posts, terms and users inside a rule, rather than typing ids
- Check a set from your own code, against whatever context you have
- Ask which rules matched, when the answer needs explaining
Installation
composer require arraypress/wp-conditions
Quick start
Register the set, and the screen that edits it:
add_action( 'init', function () { register_conditions( 'discount_rule', [ 'labels' => [ 'singular' => 'Discount Rule', 'plural' => 'Discount Rules', ], 'menu_icon' => 'dashicons-tag', 'conditions' => [ 'user_role', 'day_of_week', 'is_logged_in' ], ] ); } );
Then, at the point the rule matters:
if ( check_conditions( 'discount_rule', [ 'user_id' => get_current_user_id() ] ) ) { // ... }
check_conditions() is true when any rule set matches;
check_all_conditions() requires all of them.
Behind a proxy
The IP and country conditions read the visitor's address from the request.
Behind Cloudflare or a load balancer the address they see is the proxy's, and
the real one arrives in a header any client could also send — so forwarded
headers are believed only from proxies on a trusted list. Cloudflare's ranges
are on it by default; add your own with the ARRAYPRESS_TRUSTED_PROXIES
constant or the arraypress_trusted_proxies filter from wp-ip-utils.
Requirements
- PHP 8.3 or later
- WordPress 7.1 or later
License
GPL-2.0-or-later