Skip to content

Commit c7220f6

Browse files
author
xecdev
committed
Show Unlock Count on Front-end feature
1 parent 96682e5 commit c7220f6

5 files changed

Lines changed: 113 additions & 1 deletion

File tree

assets/css/paywall-styles.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,14 @@
1818

1919
.unlocked-indicator span {
2020
padding: 0 1rem; /* Space between text and lines */
21+
}
22+
23+
/* Show Unlock Count on Front-end */
24+
.pb-frontend-unlock-count {
25+
color: var(--pb-frontend-unlock-color, #0074C2);
26+
margin-bottom: 1em;
27+
text-align: center;
28+
font-family: inherit;
29+
font-size: 1.2em;
30+
font-style: italic;
2131
}

assets/js/paybutton-admin.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,21 @@ jQuery(document).ready(function($) {
1616
toggleColorFields();
1717
// On checkbox change
1818
unlockedCheckbox.on('change', toggleColorFields);
19+
20+
//NEW: Toggle the “Unlock Count Color” row visibility
21+
var enableUnlockCountCheckbox = $('#paybutton_enable_frontend_unlock_count');
22+
var frontendUnlockColorRow = $('#paybutton_frontend_unlock_color_row');
23+
24+
function toggleFrontendUnlockColorRow() {
25+
if ( enableUnlockCountCheckbox.is(':checked') ) {
26+
frontendUnlockColorRow.show();
27+
} else {
28+
frontendUnlockColorRow.hide();
29+
}
30+
}
31+
32+
// On page load
33+
toggleFrontendUnlockColorRow();
34+
// On checkbox change
35+
enableUnlockCountCheckbox.on('change', toggleFrontendUnlockColorRow);
1936
});

includes/class-paybutton-admin.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,17 @@ private function save_settings() {
295295
if ( isset( $_POST['paybutton_public_key'] ) ) {
296296
$public_key = sanitize_text_field( $_POST['paybutton_public_key'] );
297297
update_option( 'paybutton_public_key', $public_key );
298-
}
298+
}
299+
300+
//Front‐end unlock count toggle
301+
$enable_frontend_unlock_count = isset( $_POST['paybutton_enable_frontend_unlock_count'] ) ? '1' : '0';
302+
update_option( 'paybutton_enable_frontend_unlock_count', $enable_frontend_unlock_count );
303+
304+
//Front‐end unlock count text color
305+
$frontend_unlock_color = isset( $_POST['paybutton_frontend_unlock_color'] )
306+
? sanitize_hex_color( wp_unslash( $_POST['paybutton_frontend_unlock_color'] ) )
307+
: '#0074C2';
308+
update_option( 'paybutton_frontend_unlock_color', $frontend_unlock_color );
299309
}
300310

301311
/**

includes/class-paybutton-public.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public function enqueue_public_assets() {
4545
// Read the admin-chosen color for the unlocked content indicator from options
4646
$pb_indicator_color = get_option('paybutton_unlocked_indicator_color', '#000000');
4747

48+
// Read the admin-chosen color for the frontend unlock label
49+
$frontend_label_color = esc_attr( get_option( 'paybutton_frontend_unlock_color', '#0074C2' ) );
50+
4851
// Add inline CSS variables.
4952
$custom_css = "
5053
:root {
@@ -55,6 +58,7 @@ public function enqueue_public_assets() {
5558
--logout-button-bg-color: " . esc_attr( get_option('paybutton_logout_button_bg_color', '#d9534f') ) . ";
5659
--logout-button-text-color: " . esc_attr( get_option('paybutton_logout_button_text_color', '#fff') ) . ";
5760
--pb-unlocked-indicator-color: {$pb_indicator_color};
61+
--pb-frontend-unlock-color: {$frontend_label_color};
5862
}
5963
";
6064
wp_add_inline_style( 'paybutton-sticky-header', esc_attr( $custom_css ) );
@@ -222,8 +226,41 @@ public function paybutton_paywall_shortcode( $atts, $content = null ) {
222226
),
223227
'opReturn' => (string) $post_id //This is a hack to give the PB server the post ID to send it back to WP's DB
224228
);
229+
230+
//NEW: If the admin enabled “Show Unlock Count on Front‐end,” and this post is NOT yet unlocked then display unlock count on the front end.
231+
$unlock_label_html = '';
232+
233+
if ( '1' === get_option( 'paybutton_enable_frontend_unlock_count', '0' ) ) {
234+
global $wpdb;
235+
$unlock_table_name = $wpdb->prefix . 'paybutton_paywall_unlocked';
236+
237+
$unlock_count = (int) $wpdb->get_var( $wpdb->prepare(
238+
"SELECT COUNT(*) FROM {$unlock_table_name} WHERE post_id = %d",
239+
$post_id
240+
) );
241+
242+
if ( $unlock_count < 1 ) {
243+
$unlock_text = '🔓 Be the first to unlock this content!';
244+
} elseif ( $unlock_count === 1 ) {
245+
$unlock_text = '🔥 1 unlock and counting...';
246+
} else {
247+
$unlock_text = "🔥 {$unlock_count} unlocks and counting...";
248+
}
249+
250+
// Build the <p> into a variable, but do not echo yet:
251+
$unlock_label_html = '<p class="pb-frontend-unlock-count">'
252+
. esc_html( $unlock_text )
253+
. '</p>';
254+
}
255+
225256
ob_start(); //When ob_start() is called, PHP begins buffering all subsequent output instead of printing it to the browser.
226257
?>
258+
259+
<?php
260+
//Print the unlock‐count HTML (if enabled) before the PayButton container.
261+
echo $unlock_label_html;
262+
?>
263+
227264
<div id="paybutton-container-<?php echo esc_attr( $post_id ); ?>" class="paybutton-container" data-config="<?php echo esc_attr( json_encode( $config ) ); ?>" style="text-align: center;"></div>
228265
<?php
229266
return ob_get_clean(); // ob_get_clean() Returns the HTML string to WordPress so it is inserted properly.

templates/admin/paywall-settings.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,44 @@
9595
</td>
9696
</tr>
9797
</tbody>
98+
<!-- Show Unlock Count on Front‐end -->
99+
<tr>
100+
<th scope="row">Show Unlock Count on Front‐end</th>
101+
<td>
102+
<label>
103+
<input
104+
type="checkbox"
105+
name="paybutton_enable_frontend_unlock_count"
106+
id="paybutton_enable_frontend_unlock_count"
107+
value="1"
108+
<?php checked( get_option( 'paybutton_enable_frontend_unlock_count', '0' ), '1' ); ?>
109+
>
110+
<span>Enable unlock‐count label above PayButton on public posts</span>
111+
</label>
112+
</td>
113+
</tr>
114+
115+
<!-- Unlock Count Color Picker (hidden until above box is checked) -->
116+
<tr id="paybutton_frontend_unlock_color_row">
117+
<th scope="row">
118+
<label for="paybutton_frontend_unlock_color">Unlock Count Label Color</label>
119+
</th>
120+
<td>
121+
<input
122+
type="color"
123+
name="paybutton_frontend_unlock_color"
124+
id="paybutton_frontend_unlock_color"
125+
value="<?php echo esc_attr( get_option( 'paybutton_frontend_unlock_color', '#0074C2' ) ); ?>"
126+
>
127+
<button type="button"
128+
onclick="document.getElementById('paybutton_frontend_unlock_color').value = '#0074C2';">
129+
Reset
130+
</button>
131+
<p class="description">
132+
Pick the hex color for the unlock count label.
133+
</p>
134+
</td>
135+
</tr>
98136
<!-- Sticky Header Settings -->
99137
<tr>
100138
<th colspan="2"><h2>Sticky Header Settings</h2></th>

0 commit comments

Comments
 (0)