prefix . 'wacc_attempts'; } public static function get( $hash ) { 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 cart_hash = %s', self::table(), $hash ), ARRAY_A ); } public static function claim( $hash, $operation, $request_hash ) { global $wpdb; // INSERT IGNORE is atomic under concurrent requests and does not replace a prior attempt. // 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( 'INSERT IGNORE INTO %i (cart_hash, operation_id, request_hash, state, result, expires_at) VALUES (%s, %s, %s, %s, %s, %d)', self::table(), $hash, $operation, $request_hash, 'pending', '', time() + 30 * DAY_IN_SECONDS ) ); } public static function release_preflight( $hash ) { 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->delete( self::table(), array( 'cart_hash' => $hash, 'state' => 'pending' ) ); } public static function finish( $hash, $result, $order_id = 0 ) { global $wpdb; $blob = self::seal( $result ); // 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' => $blob === false ? 'uncertain' : 'complete', 'result' => $blob === false ? '' : $blob, 'order_id' => $order_id ), array( 'cart_hash' => $hash ) ); } public static function seal( $value ) { $iv = random_bytes( 12 ); $tag = ''; $encrypted = openssl_encrypt( wp_json_encode( $value ), 'aes-256-gcm', hash( 'sha256', wp_salt( 'auth' ), true ), OPENSSL_RAW_DATA, $iv, $tag ); return $encrypted === false ? false : base64_encode( $iv . $tag . $encrypted ); } public static function open( $blob ) { $binary = base64_decode( $blob, true ); if ( $binary === false || strlen( $binary ) < 29 ) { return null; } $plain = openssl_decrypt( substr( $binary, 28 ), 'aes-256-gcm', hash( 'sha256', wp_salt( 'auth' ), true ), OPENSSL_RAW_DATA, substr( $binary, 0, 12 ), substr( $binary, 12, 16 ) ); return $plain === false ? null : json_decode( $plain, true ); } }