prefix . 'wacc_carts'; } public static function maybe_install() { if ( get_option( 'wacc_schema' ) !== self::SCHEMA ) { self::install(); } if ( ! wp_next_scheduled( 'wacc_cleanup' ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'hourly', 'wacc_cleanup' ); } } public static function install() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $table = self::table(); $rates = $wpdb->prefix . 'wacc_rates'; $attempts = $wpdb->prefix . 'wacc_attempts'; $collate = $wpdb->get_charset_collate(); dbDelta( "CREATE TABLE $table ( id varchar(36) NOT NULL, secret_hash varchar(64) NOT NULL, items longtext NOT NULL, revision bigint unsigned NOT NULL DEFAULT 1, expires_at bigint unsigned NOT NULL, state varchar(12) NOT NULL DEFAULT 'active', handoff_hash varchar(64) DEFAULT NULL, handoff_expires bigint unsigned NOT NULL DEFAULT 0, PRIMARY KEY (id), UNIQUE KEY handoff_hash (handoff_hash), KEY expires_at (expires_at) ) $collate;" ); dbDelta( "CREATE TABLE $rates ( bucket varchar(64) NOT NULL, hits int unsigned NOT NULL DEFAULT 1, expires_at bigint unsigned NOT NULL, PRIMARY KEY (bucket), KEY expires_at (expires_at) ) $collate;" ); dbDelta( "CREATE TABLE $attempts ( cart_hash varchar(64) NOT NULL, operation_id varchar(36) NOT NULL, request_hash varchar(64) NOT NULL, state varchar(12) NOT NULL DEFAULT 'pending', result longtext NOT NULL, order_id bigint unsigned NOT NULL DEFAULT 0, expires_at bigint unsigned NOT NULL, PRIMARY KEY (cart_hash), KEY expires_at (expires_at) ) $collate;" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin-owned transactional tables require fresh state; atomic claims and counters must bypass caches. if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) ) === $table && $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $rates ) ) ) === $rates && $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $attempts ) ) ) === $attempts ) { update_option( 'wacc_schema', self::SCHEMA, false ); } } public static function cleanup() { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin-owned transactional tables require fresh state; atomic claims and counters must bypass caches. $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE expires_at < %d', self::table(), time() ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin-owned transactional tables require fresh state; atomic claims and counters must bypass caches. $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE expires_at < %d', $wpdb->prefix . 'wacc_rates', time() ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin-owned transactional tables require fresh state; atomic claims and counters must bypass caches. $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE expires_at < %d', $wpdb->prefix . 'wacc_attempts', time() ) ); } public static function token() { return bin2hex( random_bytes( 32 ) ); } public static function create() { global $wpdb; $id = wp_generate_uuid4(); $token = self::token(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin-owned transactional tables require fresh state; atomic claims and counters must bypass caches. $ok = $wpdb->insert( self::table(), array( 'id' => $id, 'secret_hash' => hash( 'sha256', $token ), 'items' => '[]', 'revision' => 1, 'expires_at' => time() + self::TTL, 'state' => 'active' ) ); if ( ! $ok ) { return new \WP_Error( 'wacc_storage', 'Shopping storage is unavailable.', array( 'status' => 503 ) ); } return array( 'cart' => self::get( $id ), 'token' => $token ); } public static function get( $id ) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin-owned transactional tables require fresh state; atomic claims and counters must bypass caches. return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %s AND expires_at > %d', self::table(), $id, time() ), ARRAY_A ); } public static function authenticate( $id, $header ) { if ( ! is_string( $header ) || ! preg_match( '/^Bearer ([a-f0-9]{64})$/D', $header, $matches ) ) { return false; } $cart = self::get( $id ); return $cart && hash_equals( $cart['secret_hash'], hash( 'sha256', $matches[1] ) ) ? $cart : false; } /** Compare-and-swap prevents lost updates and invalidates outstanding handoffs. */ public static function replace_items( $cart, array $items ) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin-owned transactional tables require fresh state; atomic claims and counters must bypass caches. return 1 === $wpdb->query( $wpdb->prepare( "UPDATE %i SET items = %s, revision = revision + 1, handoff_hash = NULL, handoff_expires = 0 WHERE id = %s AND revision = %d AND state = 'active' AND expires_at > %d", self::table(), wp_json_encode( $items ), $cart['id'], $cart['revision'], time() ) ); } public static function issue_handoff( $cart ) { global $wpdb; $token = self::token(); $expires = min( (int) $cart['expires_at'], time() + self::HANDOFF_TTL ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin-owned transactional tables require fresh state; atomic claims and counters must bypass caches. $ok = $wpdb->query( $wpdb->prepare( "UPDATE %i SET handoff_hash = %s, handoff_expires = %d WHERE id = %s AND revision = %d AND state = 'active' AND expires_at > %d", self::table(), hash( 'sha256', $token ), $expires, $cart['id'], $cart['revision'], time() ) ); return $ok === 1 ? array( 'token' => $token, 'expires_at' => $expires ) : false; } public static function for_handoff( $token ) { global $wpdb; if ( ! is_string( $token ) || ! preg_match( '/^[a-f0-9]{64}$/D', $token ) ) { return null; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin-owned transactional tables require fresh state; atomic claims and counters must bypass caches. return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM %i WHERE handoff_hash = %s AND state = 'active' AND expires_at > %d AND handoff_expires > %d", self::table(), hash( 'sha256', $token ), time(), time() ), ARRAY_A ); } public static function claim( $cart ) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin-owned transactional tables require fresh state; atomic claims and counters must bypass caches. return 1 === $wpdb->query( $wpdb->prepare( "UPDATE %i SET state = 'claimed' WHERE id = %s AND revision = %d AND handoff_hash = %s AND state = 'active' AND expires_at > %d AND handoff_expires > %d", self::table(), $cart['id'], $cart['revision'], $cart['handoff_hash'], time(), time() ) ); } public static function finish( $id, $success ) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin-owned transactional tables require fresh state; atomic claims and counters must bypass caches. $wpdb->update( self::table(), array( 'state' => $success ? 'consumed' : 'active', 'handoff_hash' => null, 'handoff_expires' => 0 ), array( 'id' => $id, 'state' => 'claimed' ) ); } /** Atomic fixed-window throttle, keyed by a salted hash of REMOTE_ADDR only. */ public static function rate_limit( $scope, $limit ) { global $wpdb; $now = time(); $address = isset( $_SERVER['REMOTE_ADDR'] ) && is_string( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; $address = filter_var( $address, FILTER_VALIDATE_IP ) !== false ? $address : 'unknown'; $bucket = hash_hmac( 'sha256', $scope . '|' . $address . '|' . (int) floor( $now / 60 ), wp_salt( 'auth' ) ); $table = $wpdb->prefix . 'wacc_rates'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin-owned transactional tables require fresh state; atomic claims and counters must bypass caches. $ok = $wpdb->query( $wpdb->prepare( 'INSERT INTO %i (bucket, hits, expires_at) VALUES (%s, 1, %d) ON DUPLICATE KEY UPDATE hits = hits + 1', $table, $bucket, $now + 120 ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin-owned transactional tables require fresh state; atomic claims and counters must bypass caches. return $ok !== false && (int) $wpdb->get_var( $wpdb->prepare( 'SELECT hits FROM %i WHERE bucket = %s', $table, $bucket ) ) <= $limit; } }