purchase_repository = $purchase_repository; $this->secure_token = $secure_token; } public function user_has_access( int $article_id ): bool { $tokens = $this->get_cookie_tokens(); if ( empty( $tokens[ $article_id ] ) ) { return false; } $token_hash = $this->secure_token->hash( $tokens[ $article_id ] ); return null !== $this->purchase_repository->find_paid_by_token( $article_id, $token_hash ); } public function grant_access( int $article_id, string $raw_token ): void { $tokens = $this->get_cookie_tokens(); $tokens[ $article_id ] = $raw_token; setcookie( Constants::ACCESS_COOKIE_NAME, wp_json_encode( $tokens ), array( 'expires' => time() + YEAR_IN_SECONDS, 'path' => COOKIEPATH ?: '/', 'domain' => COOKIE_DOMAIN, 'secure' => is_ssl(), 'httponly' => true, 'samesite' => 'Lax', ) ); $_COOKIE[ Constants::ACCESS_COOKIE_NAME ] = wp_json_encode( $tokens ); } private function get_cookie_tokens(): array { if ( empty( $_COOKIE[ Constants::ACCESS_COOKIE_NAME ] ) ) { return array(); } $raw = sanitize_text_field( wp_unslash( $_COOKIE[ Constants::ACCESS_COOKIE_NAME ] ) ); $tokens = json_decode( $raw, true ); if ( ! is_array( $tokens ) ) { return array(); } return array_map( 'sanitize_text_field', $tokens ); } }