Files

28 lines
839 B
PHP
Raw Permalink Normal View History

<?php
declare( strict_types=1 );
namespace StreamGoals\Rules;
/** $wpdb-backed RuleStore over sg_rules. Requires live WordPress. */
final class WpRuleStore implements RuleStore {
private \wpdb $db;
public function __construct( ?\wpdb $db = null ) {
$this->db = $db ?? $GLOBALS['wpdb'];
}
public function enabledForGoal( int $goalId ): array {
$t = $this->db->prefix . 'sg_rules';
$rows = $this->db->get_results(
$this->db->prepare( "SELECT id, goal_id, type, config FROM {$t} WHERE goal_id = %d AND enabled = 1 ORDER BY sort ASC, id ASC", $goalId ),
ARRAY_A
);
$out = array();
foreach ( (array) $rows as $r ) {
$cfg = json_decode( (string) $r['config'], true );
$out[] = new RuleDef( (int) $r['id'], (int) $r['goal_id'], (string) $r['type'], is_array( $cfg ) ? $cfg : array() );
}
return $out;
}
}