36 lines
1.0 KiB
PHP
36 lines
1.0 KiB
PHP
<?php
|
|||
|
|
declare( strict_types=1 );
|
||
|
|
|
||
|
|
namespace StreamGoals\Core;
|
||
|
|
|
||
|
|
use StreamGoals\Rules\StateRepository;
|
||
|
|
|
||
|
|
/** $wpdb-backed sg_state access (goal_id, skey, svalue). Requires live WordPress. */
|
||
|
|
final class StateStore implements StateRepository {
|
||
|
|
|
||
|
|
private \wpdb $db;
|
||
|
|
|
||
|
|
public function __construct( ?\wpdb $db = null ) {
|
||
|
|
$this->db = $db ?? $GLOBALS['wpdb'];
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getInt( int $goalId, string $key, int $default = 0 ): int {
|
||
|
|
$t = $this->db->prefix . 'sg_state';
|
||
|
|
$v = $this->db->get_var( $this->db->prepare( "SELECT svalue FROM {$t} WHERE goal_id = %d AND skey = %s", $goalId, $key ) );
|
||
|
|
return null === $v ? $default : (int) $v;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function setInt( int $goalId, string $key, int $value ): void {
|
||
|
|
$t = $this->db->prefix . 'sg_state';
|
||
|
|
$this->db->query(
|
||
|
|
$this->db->prepare(
|
||
|
|
"INSERT INTO {$t} (goal_id, skey, svalue, updated_at) VALUES (%d, %s, %s, %s)
|
||
|
|
ON DUPLICATE KEY UPDATE svalue = VALUES(svalue), updated_at = VALUES(updated_at)",
|
||
|
|
$goalId,
|
||
|
|
$key,
|
||
|
|
(string) $value,
|
||
|
|
gmdate( 'Y-m-d H:i:s' )
|
||
|
|
)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|