44final class PayButton_State {
55
66 /**
7- * cookie names & session-only cookies
7+ * cookie names
88 */
99 const COOKIE_USER_ADDR = 'paybutton_user_wallet_address ' ;
1010 const COOKIE_CONTENT = 'paybutton_paid_content ' ;
11- const TTL = 604800 ; // one week
11+
12+ /**
13+ * Default cookie lifetime in seconds.
14+ * Used when the admin has not set a specific expiry.
15+ * Modern browsers may impose their own limits on cookie lifetimes often around 400 days.
16+ * https://httpwg.org/http-extensions/draft-ietf-httpbis-rfc6265bis.html#name-the-expires-attribute
17+ */
18+ const TTL = 31536000 ; // 1 year in seconds.
19+
20+ /**
21+ * Get effective cookie TTL (in seconds) based on settings.
22+ * - If "Login & Content Unlock Cookie Expiry (days)" is > 0,
23+ * use that value.
24+ * - If 0 or empty, fall back to the unlimited default (self::TTL).
25+ * @return int TTL in seconds
26+ */
27+ private static function get_ttl () {
28+ $ raw_days = get_option ( 'paybutton_cookie_ttl_days ' , 0 );
29+ $ days = (int ) $ raw_days ;
30+
31+ if ( $ days > 0 ) {
32+ if ( defined ( 'DAY_IN_SECONDS ' ) ) {
33+ return $ days * DAY_IN_SECONDS ;
34+ }
35+
36+ return $ days * 86400 ;
37+ }
38+
39+ return self ::TTL ;
40+ }
1241
1342 /**
1443 * Generate HMAC of a value using WP auth salt.
@@ -103,12 +132,14 @@ public static function set_address( $addr ) {
103132 return ; // nothing new → don’t send a Set-Cookie header, good for caching
104133 }
105134
135+ $ ttl = self ::get_ttl ();
136+
106137 if ( PHP_VERSION_ID >= 70300 ) {
107138 setcookie (
108139 self ::COOKIE_USER_ADDR ,
109140 $ cookieValue ,
110141 [
111- 'expires ' => time () + self :: TTL ,
142+ 'expires ' => time () + $ ttl ,
112143 'path ' => '/ ' ,
113144 'domain ' => COOKIE_DOMAIN ?: '' ,
114145 'secure ' => is_ssl (),
@@ -118,7 +149,7 @@ public static function set_address( $addr ) {
118149 );
119150 } else {
120151 //Fall back to a raw header with SameSite=Lax for older PHP versions
121- $ expiry = gmdate ( 'D, d-M-Y H:i:s T ' , time () + self :: TTL );
152+ $ expiry = gmdate ( 'D, d-M-Y H:i:s T ' , time () + $ ttl );
122153 $ header = sprintf (
123154 '%s=%s; Expires=%s; Path=%s; Domain=%s; %s; HttpOnly; SameSite=Lax ' ,
124155 self ::COOKIE_USER_ADDR ,
@@ -196,12 +227,14 @@ public static function add_article( $post_id ) {
196227 return ; // nothing new → don’t send a Set-Cookie header, good for caching
197228 }
198229
230+ $ ttl = self ::get_ttl ();
231+
199232 if ( PHP_VERSION_ID >= 70300 ) {
200233 setcookie (
201234 self ::COOKIE_CONTENT ,
202235 $ cookieValue ,
203236 [
204- 'expires ' => time () + self :: TTL ,
237+ 'expires ' => time () + $ ttl ,
205238 'path ' => '/ ' ,
206239 'domain ' => COOKIE_DOMAIN ?: '' ,
207240 'secure ' => is_ssl (),
@@ -211,7 +244,7 @@ public static function add_article( $post_id ) {
211244 );
212245 } else {
213246 //Fall back to a raw header with SameSite=Lax for older PHP versions
214- $ expiry = gmdate ( 'D, d-M-Y H:i:s T ' , time () + self :: TTL );
247+ $ expiry = gmdate ( 'D, d-M-Y H:i:s T ' , time () + $ ttl );
215248 $ header = sprintf (
216249 '%s=%s; Expires=%s; Path=%s; Domain=%s; %s; HttpOnly; SameSite=Lax ' ,
217250 self ::COOKIE_CONTENT ,
0 commit comments