-
Notifications
You must be signed in to change notification settings - Fork 0
MR-31: Lite version of paywall #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ad56416
28c461e
df0ee89
d335a33
dc8d456
a403643
28a9230
e8a69d8
03db76c
e978111
03978ab
b8e2df8
70af3b5
3b40da0
e2a8617
e50f529
cfef9f6
4c5ec31
d1740cf
0e59044
01df5b4
efe0fb1
ad9e596
45faa2c
16f45d8
66ede34
1c29158
3ad49f9
ea5542b
7eca306
f9f1839
72122d0
e6f32c1
971da5f
77151a6
a249ed5
65e9fe5
7973d05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| jQuery(function ($) { | ||
| const $form = $('.memberful-paywall-builder__panel[data-panel="builder"]'); | ||
| const $modeInputs = $('input[name="memberful_paywall[mode]"]'); | ||
| const $panels = $('.memberful-paywall-builder__panel'); | ||
| const $preview = $('#memberful-paywall-preview'); | ||
|
|
||
| const preview = window.memberfulPaywallPreview || {}; | ||
| const DEBOUNCE_MS = 250; | ||
|
|
||
| let debounceTimer = null; | ||
| let requestSeq = 0; | ||
|
|
||
| function setColorFieldValue($field, color) { | ||
| const normalized = color ? color.toLowerCase() : ''; | ||
| const $presetSwatches = $field.find('.memberful-paywall-builder__swatch[data-color]'); | ||
| const $customSwatch = $field.find('.memberful-paywall-builder__swatch--custom'); | ||
| let presetSelected = false; | ||
|
|
||
| $field.find('.memberful-paywall-builder__color-input').val(normalized); | ||
|
|
||
| $presetSwatches.each(function () { | ||
| const $swatch = $(this); | ||
| const isSelected = normalized !== '' && ($swatch.attr('data-color') || '').toLowerCase() === normalized; | ||
| $swatch.toggleClass('is-selected', isSelected).attr('aria-pressed', isSelected ? 'true' : 'false'); | ||
| presetSelected = presetSelected || isSelected; | ||
| }); | ||
|
|
||
| $customSwatch.toggleClass('is-selected', normalized !== '' && !presetSelected); | ||
| if (normalized !== '' && !presetSelected) { | ||
| $customSwatch.css('--memberful-custom-swatch-color', normalized); | ||
| } else { | ||
| $customSwatch.css('--memberful-custom-swatch-color', ''); | ||
| } | ||
|
|
||
| $field.find('.memberful-paywall-builder__color-reset').prop('disabled', normalized === ''); | ||
| } | ||
|
|
||
| $('[data-color-field]').each(function () { | ||
| const $field = $(this); | ||
| setColorFieldValue($field, $field.find('.memberful-paywall-builder__color-input').val() || ''); | ||
| }); | ||
|
|
||
| function applyMode(mode) { | ||
| $panels.each(function () { | ||
| this.style.display = this.dataset.panel === mode ? '' : 'none'; | ||
| }); | ||
| } | ||
|
|
||
| $modeInputs.on('change', function () { | ||
| if (!this.checked) { | ||
| return; | ||
| } | ||
|
|
||
| applyMode(this.value); | ||
|
|
||
| if (this.value === 'builder') { | ||
| refreshPreview(); | ||
| } | ||
| }); | ||
|
|
||
| applyMode($modeInputs.filter(':checked').val() || 'builder'); | ||
|
|
||
| function collectConfig() { | ||
| return { | ||
| mode: $('input[name="memberful_paywall[mode]"]:checked').val() || 'builder', | ||
| layout: $('input[name="memberful_paywall[layout]"]:checked').val() || 'card', | ||
| heading: $('#memberful-paywall-heading').val() || '', | ||
| subheading: $('#memberful-paywall-subheading').val() || '', | ||
| features: $('#memberful-paywall-benefits .memberful-paywall-builder__benefit-input').map(function () { | ||
| return $(this).val(); | ||
| }).get(), | ||
| button_label: $('#memberful-paywall-button-label').val() || '', | ||
| subscribe_url: $('#memberful-paywall-subscribe-url').val() || '', | ||
| sign_in_url: $('#memberful-paywall-signin-url').val() || '', | ||
| brand_color: $('#memberful-paywall-brand-color').val() || '', | ||
| background_color: $('#memberful-paywall-background-color').val() || '', | ||
| button_shape: $('input[name="memberful_paywall[button_shape]"]:checked').val() || 'rounded', | ||
| }; | ||
| } | ||
|
|
||
| function refreshPreview() { | ||
| if (!preview.ajaxUrl || !preview.action || !preview.nonce || !$preview.length) { | ||
| return; | ||
| } | ||
|
|
||
| clearTimeout(debounceTimer); | ||
|
|
||
| const seq = ++requestSeq; | ||
| const body = new URLSearchParams(); | ||
| body.set('action', preview.action); | ||
| body.set('nonce', preview.nonce); | ||
|
|
||
| const config = collectConfig(); | ||
| Object.keys(config).forEach(function (key) { | ||
| const value = config[key]; | ||
| if (Array.isArray(value)) { | ||
| value.forEach(function (item) { | ||
| body.append('config[' + key + '][]', item); | ||
| }); | ||
| } else { | ||
| body.append('config[' + key + ']', value); | ||
| } | ||
| }); | ||
|
|
||
| fetch(preview.ajaxUrl, { | ||
| method: 'POST', | ||
| credentials: 'same-origin', | ||
| headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }, | ||
| body: body.toString(), | ||
| }) | ||
| .then(function (res) { | ||
| if (!res.ok) { | ||
| throw new Error('Request failed: ' + res.status); | ||
| } | ||
|
|
||
| return res.json(); | ||
| }) | ||
| .then(function (json) { | ||
| if (seq !== requestSeq) { | ||
| return; | ||
| } | ||
|
|
||
| if (json && json.success && json.data && json.data.html) { | ||
| $preview.attr('srcdoc', json.data.html); | ||
| } | ||
| }) | ||
| .catch(function (err) { | ||
| console.error('Paywall preview failed', err); | ||
| }); | ||
| } | ||
|
|
||
| function scheduleRefresh() { | ||
| clearTimeout(debounceTimer); | ||
| debounceTimer = setTimeout(refreshPreview, DEBOUNCE_MS); | ||
| } | ||
|
|
||
| $form.on('input', 'input[type="text"], input[type="url"], textarea', scheduleRefresh); | ||
| $form.on('change', 'input[type="radio"], select', refreshPreview); | ||
|
|
||
| $form.on('click', '.memberful-paywall-builder__swatch[data-color]', function (e) { | ||
| e.preventDefault(); | ||
|
|
||
| setColorFieldValue($(this).closest('[data-color-field]'), $(this).attr('data-color') || ''); | ||
| refreshPreview(); | ||
| }); | ||
|
|
||
| $form.on('input change', '.memberful-paywall-builder__swatch--custom input[type="color"]', function () { | ||
| setColorFieldValue($(this).closest('[data-color-field]'), this.value || ''); | ||
| scheduleRefresh(); | ||
| }); | ||
|
|
||
| $form.on('click', '.memberful-paywall-builder__color-reset', function (e) { | ||
| e.preventDefault(); | ||
|
|
||
| setColorFieldValue($(this).closest('[data-color-field]'), ''); | ||
| refreshPreview(); | ||
| }); | ||
|
|
||
| $form.on('click', '.memberful-paywall-builder__benefit-add', function (e) { | ||
| e.preventDefault(); | ||
|
|
||
| const template = document.getElementById('memberful-paywall-benefit-template'); | ||
| const list = document.getElementById('memberful-paywall-benefits'); | ||
| if (!template || !list) { | ||
| return; | ||
| } | ||
|
|
||
| list.appendChild(template.content.cloneNode(true)); | ||
| $(list).find('.memberful-paywall-builder__benefit-input').last().trigger('focus'); | ||
| refreshPreview(); | ||
| }); | ||
|
|
||
| $form.on('click', '.memberful-paywall-builder__benefit-remove', function (e) { | ||
| e.preventDefault(); | ||
| $(this).closest('.memberful-paywall-builder__benefit').remove(); | ||
| refreshPreview(); | ||
| }); | ||
|
|
||
| const $headingInput = $('#memberful-paywall-heading'); | ||
| const $headingCounter = $('[data-counter-for="memberful-paywall-heading"]'); | ||
| const headingMax = parseInt($headingCounter.attr('data-max'), 10) || 60; | ||
| $headingInput.on('input', function () { | ||
| $headingCounter.text(this.value.length + '/' + headingMax); | ||
| }); | ||
|
|
||
| if (($modeInputs.filter(':checked').val() || 'builder') === 'builder') { | ||
| refreshPreview(); | ||
| } | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -113,11 +113,14 @@ function memberful_wp_menu() { | |
|
|
||
| function memberful_wp_admin_enqueue_scripts() { | ||
| $screen = get_current_screen(); | ||
| $admin_css_path = MEMBERFUL_DIR . '/stylesheets/admin.css'; | ||
|
|
||
| if ( strpos( 'memberful', $screen->id ) !== null ) { | ||
| wp_enqueue_style( | ||
| 'memberful-admin', | ||
| plugins_url( 'stylesheets/admin.css' , dirname(__FILE__) ) | ||
| plugins_url( 'stylesheets/admin.css' , dirname(__FILE__) ), | ||
| array(), | ||
| file_exists( $admin_css_path ) ? filemtime( $admin_css_path ) : MEMBERFUL_VERSION | ||
| ); | ||
| wp_enqueue_script( | ||
| 'memberful-admin', | ||
|
|
@@ -127,6 +130,35 @@ function memberful_wp_admin_enqueue_scripts() { | |
| ); | ||
| } | ||
|
|
||
| if ( | ||
| 'memberful_options' === filter_input( INPUT_GET, 'page' ) | ||
| && 'global_marketing' === filter_input( INPUT_GET, 'subpage' ) | ||
| ) { | ||
| $paywall_css_path = MEMBERFUL_DIR . '/stylesheets/paywall.css'; | ||
| $paywall_builder_path = MEMBERFUL_DIR . '/js/build/paywall-builder.js'; | ||
|
|
||
| wp_enqueue_style( | ||
| 'memberful-paywall', | ||
| MEMBERFUL_URL . '/stylesheets/paywall.css', | ||
| array(), | ||
| file_exists( $paywall_css_path ) ? filemtime( $paywall_css_path ) : MEMBERFUL_VERSION | ||
| ); | ||
|
|
||
| wp_enqueue_script( | ||
| 'memberful-paywall-builder', | ||
| MEMBERFUL_URL . '/js/build/paywall-builder.js', | ||
| array('jquery'), | ||
| file_exists( $paywall_builder_path ) ? filemtime( $paywall_builder_path ) : MEMBERFUL_VERSION, | ||
| true | ||
| ); | ||
|
|
||
| wp_localize_script( | ||
| 'memberful-paywall-builder', | ||
| 'memberfulPaywallPreview', | ||
| Memberful_Paywall_Preview::script_args() | ||
| ); | ||
| } | ||
|
|
||
| wp_enqueue_script( | ||
| 'memberful-menu', | ||
| plugins_url( 'js/src/menu.js', dirname( __FILE__ ) ), | ||
|
|
@@ -697,8 +729,17 @@ function memberful_wp_global_marketing() { | |
| if ( isset( $_POST['memberful_use_global_marketing'] ) ) { | ||
| update_option( 'memberful_use_global_marketing', true ); | ||
| update_option( 'memberful_global_marketing_override', filter_input( INPUT_POST, 'memberful_global_marketing_override', FILTER_SANITIZE_NUMBER_INT ) ); | ||
| update_option( 'memberful_global_marketing_content', memberful_wp_kses_post( filter_input( INPUT_POST, 'memberful_global_marketing_content' ) ) ); | ||
| update_option( 'memberful_use_global_snippets', (int) isset($_POST['memberful_use_global_snippets'])); | ||
| update_option( 'memberful_use_global_snippets', (int) isset( $_POST['memberful_use_global_snippets'] ) ); | ||
|
|
||
| $paywall_input = filter_input( INPUT_POST, 'memberful_paywall', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); | ||
| if ( is_array( $paywall_input ) && ! empty( $paywall_input ) ) { | ||
| Memberful_Paywall_Config::save( $paywall_input ); | ||
| } | ||
|
|
||
| $config_mode = ( is_array( $paywall_input ) && isset( $paywall_input['mode'] ) && in_array( $paywall_input['mode'], Memberful_Paywall_Config::MODES, true ) ) ? $paywall_input['mode'] : 'builder'; | ||
| if ( 'custom_html' === $config_mode ) { | ||
| update_option( 'memberful_global_marketing_content', memberful_wp_kses_post( (string) filter_input( INPUT_POST, 'memberful_global_marketing_content' ) ) ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Custom HTML save mode fallbackMedium Severity When the posted Triggered by team rule: Code Review Guidelines Reviewed by Cursor Bugbot for commit 65e9fe5. Configure here. |
||
| } | ||
| } else { | ||
| update_option( 'memberful_use_global_marketing', false ); | ||
| } | ||
|
|
@@ -712,11 +753,12 @@ function memberful_wp_global_marketing() { | |
| memberful_wp_render( | ||
| 'global_marketing', | ||
| array( | ||
| 'use_global_marketing' => $use_global_marketing, | ||
| 'use_global_snippets' => $use_global_snippets, | ||
| 'global_marketing_content' => $global_marketing_content, | ||
| 'use_global_marketing' => $use_global_marketing, | ||
| 'use_global_snippets' => $use_global_snippets, | ||
| 'global_marketing_content' => $global_marketing_content, | ||
| 'global_marketing_override' => $global_marketing_override, | ||
| 'form_target' => memberful_wp_plugin_global_marketing_url() | ||
| 'paywall_config' => Memberful_Paywall_Config::get(), | ||
| 'form_target' => memberful_wp_plugin_global_marketing_url(), | ||
| ) | ||
| ); | ||
| } | ||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.