Skip to content

Commit 8c1697e

Browse files
authored
Merge pull request #118 from PayButton/chore/new-settings-tab
Move Public Key to the new Settings tab
2 parents c6a6d82 + 7e757c5 commit 8c1697e

4 files changed

Lines changed: 168 additions & 66 deletions

File tree

includes/class-paybutton-activator.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ class PayButton_Activator {
1717
public static function activate() {
1818
self::create_tables();
1919
self::create_profile_page();
20-
// Set a flag to redirect the admin to the Paywall Settings page after activation
21-
update_option('paybutton_activation_redirect', true);
2220
//self::migrate_old_option();
2321
}
2422

includes/class-paybutton-admin.php

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public function __construct() {
1515
add_action( 'admin_menu', array( $this, 'add_admin_menus' ) );
1616
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
1717
add_action( 'admin_notices', array( $this, 'admin_notice_missing_required_inputs' ) );
18-
// Process form submissions early
19-
add_action( 'admin_init', array( $this, 'handle_save_settings' ) );
18+
// Handle settings save
19+
add_action( 'admin_init', array( $this, 'process_settings_forms' ) );
2020

2121
// New: Posts page PayButton Unlocks column
2222
add_filter( 'manage_edit-post_columns', [ $this, 'register_paybutton_unlocks_column' ] );
@@ -74,9 +74,26 @@ public function add_admin_menus() {
7474
'paybutton-paywall-content',
7575
array( $this, 'content_page' )
7676
);
77+
78+
add_submenu_page(
79+
'paybutton',
80+
'Settings',
81+
'Settings',
82+
'manage_options',
83+
'paybutton-settings',
84+
array( $this, 'settings_page' )
85+
);
7786
}
7887

79-
public function handle_save_settings() {
88+
/**
89+
* Process all settings form submissions.
90+
* Routes to appropriate handler based on which form was submitted.
91+
*/
92+
public function process_settings_forms() {
93+
if ( empty( $_POST ) ) {
94+
return;
95+
}
96+
// Handle Paywall Settings save
8097
if (
8198
isset( $_POST['paybutton_paywall_save_settings'] ) &&
8299
isset( $_POST['paybutton_settings_nonce'] ) &&
@@ -88,7 +105,33 @@ public function handle_save_settings() {
88105
wp_safe_redirect( admin_url( 'admin.php?page=paybutton-paywall&settings-updated=true' ) );
89106
exit;
90107
}
91-
}
108+
109+
// Handle Settings (public key) save
110+
if (
111+
isset( $_POST['paybutton_settings_save'] ) &&
112+
isset( $_POST['paybutton_settings_nonce'] ) &&
113+
wp_verify_nonce(
114+
sanitize_text_field( wp_unslash( $_POST['paybutton_settings_nonce'] ) ),
115+
'paybutton_settings_save'
116+
) &&
117+
current_user_can( 'manage_options' )
118+
) {
119+
if ( empty( $_POST['paybutton_public_key'] ) ) {
120+
wp_safe_redirect(
121+
admin_url( 'admin.php?page=paybutton-settings&settings-updated=false' )
122+
);
123+
exit;
124+
}
125+
$public_key = sanitize_text_field(
126+
wp_unslash( $_POST['paybutton_public_key'] )
127+
);
128+
update_option( 'paybutton_public_key', $public_key );
129+
wp_safe_redirect(
130+
admin_url( 'admin.php?page=paybutton-settings&settings-updated=true' )
131+
);
132+
exit;
133+
}
134+
}
92135

93136
/**
94137
* This function is hooked into the admin_enqueue_scripts action. It receives a
@@ -240,14 +283,25 @@ public function paywall_settings_page() {
240283
'logout_button_text_color' => get_option( 'paybutton_logout_button_text_color', '#FFFFFF' ),
241284
// blacklist
242285
'blacklist' => get_option( 'paybutton_blacklist', array() ),
243-
//Public key
244-
'paybutton_public_key' => get_option( 'paybutton_public_key', '' ),
245286
// Login & content unlock cookie expiry (days)
246287
'paybutton_cookie_ttl_days' => get_option( 'paybutton_cookie_ttl_days', 0 ),
247288
);
248289
$this->load_admin_template( 'paywall-settings', $args );
249290
}
250291

292+
public function settings_page() {
293+
if ( ! current_user_can( 'manage_options' ) ) {
294+
return;
295+
}
296+
297+
$args = array(
298+
'paybutton_public_key' => get_option( 'paybutton_public_key', '' ),
299+
'settings_saved' => isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] === 'true',
300+
);
301+
302+
$this->load_admin_template( 'settings', $args );
303+
}
304+
251305
public function admin_notice_missing_required_inputs() {
252306
if (isset($_GET['settings-updated']) && $_GET['settings-updated'] === 'true') {
253307
return;
@@ -266,7 +320,9 @@ public function admin_notice_missing_required_inputs() {
266320
$public_key = get_option('paybutton_public_key', '');
267321
if (empty($public_key)) {
268322
echo '<div class="notice notice-error">';
269-
echo '<p><strong>NOTICE:</strong> Please set your public key in <a href="' . esc_url(admin_url('admin.php?page=paybutton-paywall')) . '">Paywall Settings</a>.</p>';
323+
echo '<p><strong>NOTICE:</strong> Please set your public key in
324+
<a href="' . esc_url( admin_url('admin.php?page=paybutton-settings') ) . '">
325+
Settings</a>.</p>';
270326
echo '</div>';
271327
}
272328
}
@@ -334,11 +390,6 @@ private function save_settings() {
334390
$blacklist = array_map( 'trim', explode( ',', $raw_blacklist ) );
335391
update_option( 'paybutton_blacklist', $blacklist );
336392
}
337-
//Adding the new public key option
338-
if ( isset( $_POST['paybutton_public_key'] ) ) {
339-
$public_key = sanitize_text_field( $_POST['paybutton_public_key'] );
340-
update_option( 'paybutton_public_key', $public_key );
341-
}
342393

343394
//Front‐end unlock count toggle
344395
$enable_frontend_unlock_count = isset( $_POST['paybutton_enable_frontend_unlock_count'] ) ? '1' : '0';

templates/admin/paywall-settings.php

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -235,58 +235,6 @@ class="regular-text"
235235
<p class="description">Enter comma-separated wallet addresses to block from logging in via Cashtab.</p>
236236
</td>
237237
</tr>
238-
<!--NEW Public Key input field-->
239-
<tr>
240-
<th scope="row">
241-
<label for="paybutton_public_key">PayButton Public Key (required)</label>
242-
</th>
243-
<td>
244-
<input type="text" name="paybutton_public_key" id="paybutton_public_key" class="regular-text" value="<?php echo esc_attr( $paybutton_public_key ); ?>" required>
245-
<p class="description">
246-
Enter your PayButton public key to verify Payment Trigger requests.
247-
</p>
248-
<!-- User-Friendly Setup Guide -->
249-
<div class="paybutton-guide">
250-
<p><strong>Guide to Setup your PayButton Public Key:</strong></p>
251-
<p>
252-
1. Create an account on
253-
<a href="https://paybutton.org/signup" target="_blank" rel="noopener noreferrer">PayButton.org</a>
254-
and copy your public key from the <a href="https://paybutton.org/account" target="_blank" rel="noopener noreferrer">account page</a> and paste it in the Public Key field above.
255-
</p>
256-
<p>
257-
2. <a href="https://paybutton.org/buttons" target="_blank" rel="noopener noreferrer">Create a button</a>
258-
for your paywall receiving wallet address.
259-
</p>
260-
<p>
261-
3. Scroll down on the buttons page to the section <em>"When a Payment is Received..."</em>.
262-
</p>
263-
<p>
264-
4. In the <em>URL</em> field, paste the following:
265-
</p>
266-
<pre class="pre-box"><?php echo esc_url( admin_url( 'admin-ajax.php?action=payment_trigger' ) ); ?></pre>
267-
<p>
268-
5. In the <em>Post Data</em> field, paste the following code as is:
269-
</p>
270-
<pre class="pre-box">
271-
{
272-
"signature": &lt;signature&gt;,
273-
"post_id": &lt;opReturn&gt;,
274-
"tx_hash": &lt;txId&gt;,
275-
"tx_amount": &lt;amount&gt;,
276-
"tx_timestamp": &lt;timestamp&gt;,
277-
"user_address": &lt;inputAddresses&gt;,
278-
"value": &lt;value&gt;,
279-
"currency": &lt;currency&gt;
280-
}</pre>
281-
<p>
282-
6. Save your button settings after pasting these values, and you're all set!
283-
</p>
284-
<p>
285-
<strong>Note:</strong> Enabling this feature is required as it improves payment reliability, leveraging secure server-to-server messaging to record paywall and login transactions to your database.
286-
</p>
287-
</div>
288-
</td>
289-
</tr>
290238
</table>
291239
<p class="submit">
292240
<button type="submit" name="paybutton_paywall_save_settings" class="button button-primary">Save Changes</button>

templates/admin/settings.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<!-- File: templates/admin/settings.php -->
2+
<?php
3+
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4+
?>
5+
6+
<div class="wrap">
7+
<div class="pb-header">
8+
<img class="paybutton-logo" src="<?php echo esc_url( PAYBUTTON_PLUGIN_URL . 'assets/paybutton-logo.png' ); ?>" alt="PayButton Logo">
9+
</div>
10+
<h1>PayButton Settings</h1>
11+
<?php if ( ! empty( $settings_saved ) ) : ?>
12+
<div class="notice notice-success is-dismissible">
13+
<p>Settings saved successfully.</p>
14+
</div>
15+
<?php endif; ?>
16+
17+
<form method="post">
18+
<?php wp_nonce_field( 'paybutton_settings_save', 'paybutton_settings_nonce' ); ?>
19+
20+
<table class="form-table">
21+
22+
<!--NEW Public Key input field-->
23+
<tr>
24+
<th scope="row">
25+
<label for="paybutton_public_key">PayButton Public Key (required)</label>
26+
</th>
27+
<td>
28+
<input
29+
type="text"
30+
name="paybutton_public_key"
31+
id="paybutton_public_key"
32+
class="regular-text"
33+
value="<?php echo esc_attr( $paybutton_public_key ); ?>"
34+
required
35+
>
36+
<p class="description">
37+
Enter your PayButton public key to verify Payment Trigger requests.
38+
</p>
39+
40+
<!-- User-Friendly Setup Guide -->
41+
<div class="paybutton-guide">
42+
<p><strong>Guide to Setup your PayButton Public Key:</strong></p>
43+
<p>
44+
1. Create an account on
45+
<a href="https://paybutton.org/signup" target="_blank" rel="noopener noreferrer">
46+
PayButton.org
47+
</a>
48+
and copy your public key from the
49+
<a href="https://paybutton.org/account" target="_blank" rel="noopener noreferrer">
50+
account page
51+
</a>
52+
and paste it in the Public Key field above.
53+
</p>
54+
<p>
55+
2.
56+
<a href="https://paybutton.org/buttons" target="_blank" rel="noopener noreferrer">
57+
Create a button
58+
</a>
59+
for your paywall receiving wallet address.
60+
</p>
61+
<p>
62+
3. Scroll down on the buttons page to the section
63+
<em>"When a Payment is Received..."</em>.
64+
</p>
65+
<p>
66+
4. In the <em>URL</em> field, paste the following:
67+
</p>
68+
<pre class="pre-box"><?php echo esc_url( admin_url( 'admin-ajax.php?action=payment_trigger' ) ); ?></pre>
69+
<p>
70+
5. In the <em>Post Data</em> field, paste the following code as is:
71+
</p>
72+
<pre class="pre-box">
73+
{
74+
"signature": &lt;signature&gt;,
75+
"post_id": &lt;opReturn&gt;,
76+
"tx_hash": &lt;txId&gt;,
77+
"tx_amount": &lt;amount&gt;,
78+
"tx_timestamp": &lt;timestamp&gt;,
79+
"user_address": &lt;inputAddresses&gt;,
80+
"value": &lt;value&gt;,
81+
"currency": &lt;currency&gt;
82+
}</pre>
83+
<p>
84+
6. Save your button settings after pasting these values, and you're all set!
85+
</p>
86+
<p>
87+
<strong>Note:</strong> Enabling this feature is required as it improves payment reliability,
88+
leveraging secure server-to-server messaging to record paywall and login transactions to your database.
89+
</p>
90+
</div>
91+
</td>
92+
</tr>
93+
94+
</table>
95+
96+
<p class="submit">
97+
<button
98+
type="submit"
99+
name="paybutton_settings_save"
100+
class="button button-primary">
101+
Save Settings
102+
</button>
103+
</p>
104+
</form>
105+
</div>

0 commit comments

Comments
 (0)