Skip to content

Commit 43b6fd9

Browse files
xecdevxecdev
andauthored
make cookie ttl configurable and unlimited by default (#105)
* make cookie ttl configurable and unlimited by default * Change </br> to <br /> --------- Co-authored-by: xecdev <ecashinformer@gmail.com>
1 parent 2596c8e commit 43b6fd9

4 files changed

Lines changed: 97 additions & 10 deletions

File tree

assets/css/paybutton-admin.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,17 @@ table.widefat.fixed.striped td {
295295
.copy-overlay .overlay-text {
296296
text-align: center;
297297
font-weight:bold;
298+
}
299+
300+
.pre-box {
301+
background: #eaeaea;
302+
padding: 10px;
303+
border: 1px solid #ddd;
304+
}
305+
306+
.paybutton-guide {
307+
margin-top: 15px;
308+
background: #f7f7f7;
309+
padding: 15px;
310+
border-left: 4px solid #0073aa;
298311
}

includes/class-paybutton-admin.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ public function paywall_settings_page() {
224224
'blacklist' => get_option( 'paybutton_blacklist', array() ),
225225
//Public key
226226
'paybutton_public_key' => get_option( 'paybutton_public_key', '' ),
227+
// Login & content unlock cookie expiry (days)
228+
'paybutton_cookie_ttl_days' => get_option( 'paybutton_cookie_ttl_days', 0 ),
227229
);
228230
$this->load_admin_template( 'paywall-settings', $args );
229231
}
@@ -253,7 +255,7 @@ public function admin_notice_missing_required_inputs() {
253255

254256
/**
255257
* Save settings submitted via the Paywall Settings page.
256-
*/
258+
*/
257259
private function save_settings() {
258260
$address = sanitize_text_field( $_POST['paybutton_admin_wallet_address'] );
259261
$unit = sanitize_text_field( $_POST['unit'] );
@@ -292,6 +294,22 @@ private function save_settings() {
292294
// Default to #000000 for text
293295
update_option('paybutton_unlocked_indicator_color', $paybutton_unlocked_indicator_color ?: '#000000');
294296

297+
// NEW Login & Content Unlock Cookie Expiry (days)
298+
// 0 means "no automatic logout" (use long default TTL set in PayButton_State class)
299+
$paybutton_cookie_ttl_days = 0;
300+
301+
if ( isset( $_POST['paybutton_cookie_ttl_days'] ) ) {
302+
$raw_ttl = wp_unslash( $_POST['paybutton_cookie_ttl_days'] );
303+
// sanitize_text_field to strip tags etc, then cast to int
304+
$sanitized_ttl = sanitize_text_field( $raw_ttl );
305+
$paybutton_cookie_ttl_days = (int) $sanitized_ttl;
306+
307+
if ( $paybutton_cookie_ttl_days < 0 ) {
308+
$paybutton_cookie_ttl_days = 0;
309+
}
310+
}
311+
update_option( 'paybutton_cookie_ttl_days', $paybutton_cookie_ttl_days );
312+
295313
// Save the blacklist
296314
if ( isset( $_POST['paybutton_blacklist'] ) ) {
297315
$raw_blacklist = sanitize_text_field( $_POST['paybutton_blacklist'] );

includes/class-paybutton-state.php

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,40 @@
44
final 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,

templates/admin/paywall-settings.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,29 @@
183183
<tr>
184184
<th colspan="2"><h2>Advanced Settings</h2></th>
185185
</tr>
186+
<!-- Login & Content Unlock Cookie Expiry Setting -->
187+
<tr>
188+
<th scope="row">
189+
<label for="paybutton_cookie_ttl_days">
190+
Login &amp; Content Unlock Cookie Expiry (optional)
191+
</label>
192+
</th>
193+
<td>
194+
<input
195+
type="number"
196+
name="paybutton_cookie_ttl_days"
197+
id="paybutton_cookie_ttl_days"
198+
class="regular-text"
199+
min="0"
200+
step="1"
201+
value="<?php echo esc_attr( (int) $paybutton_cookie_ttl_days ); ?>"
202+
/>
203+
<p class="description">
204+
Controls how long login <code>paybutton_user_wallet_address</code> and unlocked content <code>paybutton_paid_content</code> cookies stay valid, in days.
205+
<br />Use <strong>0</strong> (default) to keep users logged in indefinitely.
206+
</p>
207+
</td>
208+
</tr>
186209
<!--blacklist Field -->
187210
<tr>
188211
<th scope="row"><label for="paybutton_blacklist">Blacklisted Addresses (optional)</label></th>
@@ -205,7 +228,7 @@
205228
Enter your PayButton public key to verify Payment Trigger requests.
206229
</p>
207230
<!-- User-Friendly Setup Guide -->
208-
<div class="paybutton-guide" style="margin-top: 15px; background: #f7f7f7; padding: 15px; border-left: 4px solid #0073aa;">
231+
<div class="paybutton-guide">
209232
<p><strong>Guide to Setup your PayButton Public Key:</strong></p>
210233
<p>
211234
1. Create an account on
@@ -222,11 +245,11 @@
222245
<p>
223246
4. In the <em>URL</em> field, paste the following:
224247
</p>
225-
<pre style="background: #eaeaea; padding: 10px; border: 1px solid #ddd;"><?php echo esc_url( admin_url( 'admin-ajax.php?action=payment_trigger' ) ); ?></pre>
248+
<pre class="pre-box"><?php echo esc_url( admin_url( 'admin-ajax.php?action=payment_trigger' ) ); ?></pre>
226249
<p>
227250
5. In the <em>Post Data</em> field, paste the following code as is:
228251
</p>
229-
<pre style="background: #eaeaea; padding: 10px; border: 1px solid #ddd;">
252+
<pre class="pre-box">
230253
{
231254
"signature": &lt;signature&gt;,
232255
"post_id": &lt;opReturn&gt;,

0 commit comments

Comments
 (0)