Complete API Reference
This comprehensive API reference documents all public classes, methods, interfaces, and functions in MilliRules.
Table of Contents#Copied!
Core Classes#Copied!
MilliRules#Copied!
Main entry point for initializing and executing rules.
Namespace: MilliRules
Methods
init(?array $package_names = null, ?array $packages = null): array
Initialize MilliRules and load packages.
Parameters:
$package_names(array|null): Array of package names to load (null = auto-load all available)$packages(array|null): Array of PackageInterface instances to register (null = register defaults)
Returns: array - Array of loaded package names
Example:
1// Auto-load available packages
2MilliRules::init();
3
4// Load specific packages by name
5MilliRules::init(['PHP', 'WP']);
6
7// Load with custom package instances
8$custom = new CustomPackage();
9MilliRules::init(null, [$custom]);
execute_rules(?array $allowed_packages = null, array $context = []): array
Execute all registered rules.
Parameters:
$allowed_packages(array|null): Array of package names to use (null = all loaded)$context(array): Additional context data (merges with auto-built context)
Returns: array - Execution result with statistics
1[
2 'rules_processed' => int, // Total rules evaluated
3 'rules_skipped' => int, // Rules skipped
4 'rules_matched' => int, // Rules with matching conditions
5 'actions_executed' => int, // Actions executed
6 'context' => array, // Execution context
7]
Example:
1// Execute all rules
2$result = MilliRules::execute_rules();
3
4// Execute only PHP rules
5$result = MilliRules::execute_rules(['PHP']);
6
7// Execute with custom context
8$result = MilliRules::execute_rules(null, ['custom_data' => 'value']);
get_loaded_packages(): array
Get names of all loaded packages.
Returns: array - Array of package names
Example:
1$packages = MilliRules::get_loaded_packages();
2// ['PHP', 'WP']
build_context(): array
Build context from all loaded packages.
Returns: array - Aggregated context data
Example:
1$context = MilliRules::build_context();
2/*
3[
4 'request' => [...],
5 'wp' => [...],
6]
7*/
Rules#Copied!
Fluent interface for creating and registering rules.
Namespace: MilliRules
snake_case or camelCase. For example, ->when_all() and ->whenAll() are equivalent, as are ->set_conditions() and ->setConditions(). This applies to all builder methods on Rules, ConditionBuilder, and ActionBuilder. The documentation uses snake_case throughout, but use whichever style fits your project.Methods
create(string $id, ?string $type = null): Rules
Create a new rule.
Parameters:
$id(string): Unique rule identifier$type(string|null): Rule type ('php'or'wp'), auto-detected if null
Returns: Rules - Rule builder instance
Example:
1$rule = Rules::create('my_rule');
2$rule = Rules::create('wp_rule', 'wp');
title(string $title): Rules
Set rule title.
Parameters:
$title(string): Human-readable title
Returns: Rules - Fluent interface
Example:
1Rules::create('my_rule')
2 ->title('My Custom Rule')
3 ->register();
order(int $order): Rules
Set execution order.
Parameters:
$order(int): Order value (lower = executes first)
Returns: Rules - Fluent interface
Example:
1Rules::create('my_rule')
2 ->order(10) // Execute at priority 10
3 ->register();
enabled(bool $enabled): Rules
Enable or disable rule.
Parameters:
$enabled(bool): Whether rule is enabled
Returns: Rules - Fluent interface
Example:
1Rules::create('my_rule')
2 ->enabled(false) // Disable rule
3 ->register();
when(): ConditionBuilder
Start building conditions with match_all logic.
Returns: ConditionBuilder - Condition builder instance
Example:
1Rules::create('my_rule')
2 ->when()
3 ->request_url('/api/*')
4 ->request_method('GET')
5 ->then()
6 ->custom('action')
7 ->register();
when_all(): ConditionBuilder
Start building conditions with ALL logic (AND).
Returns: ConditionBuilder
Example:
1Rules::create('my_rule')
2 ->when_all()
3 ->condition1()
4 ->condition2()
5 ->then()->custom('action')
6 ->register();
when_any(): ConditionBuilder
Start building conditions with ANY logic (OR).
Returns: ConditionBuilder
Example:
1Rules::create('my_rule')
2 ->when_any()
3 ->condition1()
4 ->condition2()
5 ->then()->custom('action')
6 ->register();
when_none(): ConditionBuilder
Start building conditions with NONE logic (NOT).
Returns: ConditionBuilder
Example:
1Rules::create('my_rule')
2 ->when_none()
3 ->condition1()
4 ->condition2()
5 ->then()->custom('action')
6 ->register();
then(?array $actions = null): ActionBuilder
Start building actions.
Parameters:
$actions(array|null): Array of action configurations (optional)
Returns: ActionBuilder - Action builder instance
Example:
1Rules::create('my_rule')
2 ->when()->request_url('*')
3 ->then()
4 ->custom('action1')
5 ->custom('action2')
6 ->register();
on(string $hook, int $priority = 10): Rules
Register rule on WordPress hook.
Parameters:
$hook(string): WordPress hook name$priority(int): Hook priority
Returns: Rules - Fluent interface
Example:
1Rules::create('my_rule', 'wp')
2 ->on('init', 10)
3 ->when()->is_user_logged_in()
4 ->then()->custom('action')
5 ->register();
register(): void
Register rule with MilliRules.
If a rule with the same ID already exists, it will be replaced.
Returns: void
Example:
1Rules::create('my_rule')
2 ->when()->request_url('*')
3 ->then()->custom('action')
4 ->register(); // Must call to activate rule
5
6// Registering again with same ID replaces the rule
7Rules::create('my_rule')
8 ->when()->request_url('/api/*')
9 ->then()->custom('different_action')
10 ->register(); // Replaces previous 'my_rule'
unregister(string $rule_id): bool
Remove a rule by its ID.
Parameters:
$rule_id(string): The ID of the rule to remove
Returns: bool - True if rule was found and removed, false otherwise
Example:
1// Remove a rule
2$removed = Rules::unregister('my_rule');
3
4// Check if removal was successful
5if ($removed) {
6 error_log('Rule was removed');
7} else {
8 error_log('Rule not found');
9}
10
11// Use case: Child theme disabling parent rule
12Rules::unregister('parent_theme_cache_rule');
13
14// Use case: Environment-specific disabling
15if (wp_get_environment_type() === 'production') {
16 Rules::unregister('debug_logging_rule');
17}
register_condition(string $type, callable $callback): void
Register custom condition callback.
Parameters:
$type(string): Condition type identifier$callback(callable): Callback functionfunction($args, Context $context): bool
Returns: void
Example:
1Rules::register_condition('is_weekend', function(Context $context) {
2 return date('N') >= 6;
3});
register_action(string $type, callable $callback): void
Register custom action callback.
Parameters:
$type(string): Action type identifier$callback(callable): Callback functionfunction($args, Context $context): void
Returns: void
Example:
1Rules::register_action('log', function($args, Context $context) {
2 error_log($args['value'] ?? '');
3});
register_placeholder(string $category, callable $resolver): void
Register custom placeholder resolver.
Parameters:
$category(string): Placeholder category$resolver(callable): Resolver functionfunction($context, $parts): string
Returns: void
Example:
1Rules::register_placeholder('custom', function($context, $parts) {
2 return $context['custom'][$parts[0]] ?? '';
3});
Builders#Copied!
ConditionBuilder#Copied!
Fluent builder for rule conditions.
Namespace: MilliRules
Methods
match_all(): ConditionBuilder
Use AND logic for conditions.
Returns: ConditionBuilder
match_any(): ConditionBuilder
Use OR logic for conditions.
Returns: ConditionBuilder
match_none(): ConditionBuilder
Use NOT logic for conditions.
Returns: ConditionBuilder
custom(string $type, $arg = null): ConditionBuilder
Add custom condition.
Parameters:
$type(string): Condition type$arg(mixed): Condition argument (value, config array, etc.)
Returns: ConditionBuilder
Example:
1->when()
2 ->custom('is_weekend')
3 ->custom('time_range', ['start' => 9, 'end' => 17])
add_namespace(string $namespace): ConditionBuilder
Add condition namespace for class resolution.
Parameters:
$namespace(string): Fully qualified namespace
Returns: ConditionBuilder
__call(string $method, array $args): mixed
Magic method for dynamic condition creation.
Converts method calls to condition types:
request_url()→RequestUrlConditionis_user_logged_in()→IsUserLoggedInCondition
ActionBuilder#Copied!
Fluent builder for rule actions.
Namespace: MilliRules
Methods
custom(string $type, $arg = null): ActionBuilder
Add custom action.
Parameters:
$type(string): Action type$arg(mixed): Action argument (config array, value, etc.)
Returns: ActionBuilder
Example:
1->then()
2 ->custom('log', ['value' => 'message'])
3 ->custom('send_email', ['to' => ''])
lock(): ActionBuilder
Mark the last action as locked.
Locked actions prevent subsequent actions of the same type from executing in later rules. The first matching locked action wins. This is useful for preventing cache TTL values or security settings from being overridden by lower-priority rules.
Returns: ActionBuilder
Example:
1// Rule 1 (order: 10) - Disable cache for logged-in users
2Rules::create('no-cache-logged-in')->order(10)
3 ->when()->is_user_logged_in()
4 ->then()->do_cache(false)->lock() // Lock the cache setting
5 ->register();
6
7// Rule 2 (order: 20) - This cache action will be IGNORED
8Rules::create('cache-api')->order(20)
9 ->when()->request_url('/api/*')
10 ->then()->do_cache(true) // Blocked - cache already locked
11 ->register();
12
13// Lock multiple actions independently
14->then()
15 ->do_cache(false)->lock() // Locks do_cache
16 ->do_log('User logged in') // Not locked, still executes
17 ->do_set_header('X-Cache', 'MISS') // Not locked, still executes
Key Points:
- Locks are per action type, not per rule
- Only affects actions of the same type
- Different action types can still execute
- Lock only applies if the rule's conditions match
- Locks reset on each rule execution
add_namespace(string $namespace): ActionBuilder
Add action namespace for class resolution.
Parameters:
$namespace(string): Fully qualified namespace
Returns: ActionBuilder
__call(string $method, array $args): mixed
Magic method for dynamic action creation.
Interfaces#Copied!
PackageInterface#Copied!
Interface for all packages.
Namespace: MilliRulesInterfaces
Methods
get_name(): string
Get unique package name.
Returns: string - Package name
get_namespaces(): array
Get condition and action namespaces.
Returns: array - Array of namespace strings
is_available(): bool
Check if package is available in current environment.
Returns: bool - True if available
get_required_packages(): array
Get required package names.
Returns: array - Array of package names
build_context(): array
Build context data for this package.
Returns: array - Context data
get_placeholder_resolver(array $context)
Get placeholder resolver for this package.
Parameters:
$context(array): Execution context
Returns: callable|null - Resolver function or null
register_rule(array $rule, array $metadata): void
Register rule with package.
Parameters:
$rule(array): Rule configuration$metadata(array): Rule metadata
Returns: void
execute_rules(array $rules, array $context): array
Execute rules for this package.
Parameters:
$rules(array): Rules to execute$context(array): Execution context
Returns: array - Execution result
ConditionInterface#Copied!
Interface for all conditions.
Namespace: MilliRulesInterfaces
Methods
matches(array $context): bool
Check if condition matches.
Parameters:
$context(array): Execution context
Returns: bool - True if matches
get_type(): string
Get condition type identifier.
Returns: string - Condition type
ActionInterface#Copied!
Interface for all actions.
Namespace: MilliRulesInterfaces
Methods
execute(Context $context): void
Execute action.
Parameters:
$context(array): Execution context
Returns: void
get_type(): string
Get action type identifier.
Returns: string - Action type
Base Classes#Copied!
BasePackage#Copied!
Abstract base class for packages.
Namespace: MilliRulesPackages
Provides default implementations for most PackageInterface methods.
Must Override:
get_name(): stringget_namespaces(): arrayis_available(): bool
Can Override:
get_required_packages(): array- Defaults to[]build_context(): array- Defaults to[]get_placeholder_resolver()- Defaults tonull
BaseCondition#Copied!
Abstract base class for conditions.
Namespace: MilliRulesConditions
Provides operator support and comparison logic.
Methods
__construct(array $config, Context $context)
Constructor.
Parameters:
$config(array): Condition configuration$context(array): Execution context
matches(array $context): bool
Check if condition matches (implemented).
Parameters:
$context(array): Execution context
Returns: bool
abstract protected function get_actual_value(Context $context)
Get actual value to compare (must implement).
Parameters:
$context(array): Execution context
Returns: mixed - Actual value
static public function compare_values($actual, $expected, string $operator = '='): bool
Compare values using operator.
Parameters:
$actual(mixed): Actual value$expected(mixed): Expected value$operator(string): Comparison operator
Returns: bool - True if comparison matches
BaseAction#Copied!
Abstract base class for actions.
Namespace: MilliRulesActions
Provides placeholder resolution.
Methods
__construct(array $config, Context $context)
Constructor.
Parameters:
$config(array): Action configuration$context(array): Execution context
protected function resolve_value(string $value): string
Resolve placeholders in value.
Parameters:
$value(string): Value with placeholders
Returns: string - Resolved value
abstract public function execute(Context $context): void
Execute action (must implement).
Parameters:
$context(array): Execution context
Returns: void
PackageManager#Copied!
Static manager for packages.
Namespace: MilliRules
Methods
static register_package(PackageInterface $package): void
Register package.
Parameters:
$package(PackageInterface): Package instance
Returns: void
static load_packages(?array $package_names = null): array
Load packages by name.
Parameters:
$package_names(array|null): Package names (null = load all available)
Returns: array - Loaded package names
static get_loaded_packages(): array
Get loaded package instances.
Returns: array - Array of PackageInterface instances
static get_loaded_package_names(): array
Get loaded package names.
Returns: array - Array of package names
static get_package(string $name): ?PackageInterface
Get package by name.
Parameters:
$name(string): Package name
Returns: PackageInterface|null - Package instance or null
static is_package_loaded(string $name): bool
Check if package is loaded.
Parameters:
$name(string): Package name
Returns: bool - True if loaded
static has_packages(): bool
Check if any packages are loaded.
Returns: bool - True if packages exist
static build_context(): array
Build context from all loaded packages.
Returns: array - Aggregated context
RuleEngine#Copied!
Rule execution engine.
Namespace: MilliRules
Methods
execute(array $rules, array $context, ?array $allowed_packages = null): array
Execute rules.
Parameters:
$rules(array): Rules to execute$context(array): Execution context$allowed_packages(array|null): Allowed package names
Returns: array - Execution result
static register_namespace(string $type, string $namespace): void
Register namespace for class resolution.
Parameters:
$type(string): Type ('condition'or'action')$namespace(string): Namespace string
Returns: void
Next Steps#Copied!
- Real-World Examples - See API usage in complete examples
- Getting Started - Basic usage guide
- Building Rules - Fluent API guide
Ready for complete examples? Continue to Real-World Examples to see the API in action with full working code.