Conversation
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughThis PR introduces a sticky header AJAX refresh after content unlock and standardizes variable naming across PHP classes and templates. After successful unlock, the client-side code marks the user as logged in and fetches a server-rendered sticky header. Backend enhancements include a new AJAX handler for sticky header retrieval and extended database queries to populate wallet addresses during payment verification. Changes
Sequence DiagramsequenceDiagram
participant Client as Browser Client
participant JS as paywalled-content.js
participant Server as WordPress Server
participant AJAX as AJAX Handler
participant Template as Sticky Header Template
Client->>Server: POST Unlock Payment
Server->>Server: Process Payment & Verify
Server->>Client: Success Response
JS->>JS: Mark User as Logged In (isLoggedIn)
JS->>AJAX: AJAX POST paybutton_get_sticky_header
AJAX->>AJAX: Verify Nonce
AJAX->>Template: Render Sticky Header with Wallet Address
Template->>AJAX: Return HTML
AJAX->>Client: Success Response with HTML
JS->>Client: Replace Sticky Header DOM Element
Note over Client: Header Updated Without Page Reload
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Suggested labels
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
assets/js/paywalled-content.js (1)
76-91: Consider adding error handling for the AJAX call.The sticky header refresh functionality works well, but there's no error handling if the AJAX request fails. Users won't receive feedback if the header fails to update.
Apply this diff to add error handling:
jQuery.post( PaywallAjax.ajaxUrl, { action: 'paybutton_get_sticky_header', security: PaywallAjax.nonce }, function(resp) { if (resp && resp.success && resp.data && resp.data.html) { var $header = jQuery('#cashtab-sticky-header'); if ($header.length) { // Replace the whole header with the freshly rendered one $header.replaceWith(resp.data.html); } } } - ); + ).fail(function() { + console.warn('Failed to refresh sticky header. The page may need to be reloaded.'); + });
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (10)
assets/js/paywalled-content.js(1 hunks)includes/class-paybutton-admin.php(1 hunks)includes/class-paybutton-ajax.php(5 hunks)includes/class-paybutton-public.php(2 hunks)templates/admin/content.php(1 hunks)templates/admin/customers.php(2 hunks)templates/admin/paybutton-generator.php(2 hunks)templates/admin/paywall-settings.php(1 hunks)templates/public/profile.php(1 hunks)templates/public/sticky-header.php(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
assets/js/paywalled-content.js (1)
assets/js/paybutton-paywall-cashtab-login.js (1)
isLoggedIn(2-2)
includes/class-paybutton-public.php (1)
includes/class-paybutton-state.php (2)
PayButton_State(4-279)get_address(139-150)
includes/class-paybutton-ajax.php (1)
includes/class-paybutton-state.php (2)
get_address(139-150)set_address(97-134)
🪛 PHPMD (2.15.0)
includes/class-paybutton-ajax.php
625-625: Avoid unused local variables such as '$paybutton_user_wallet_address'. (undefined)
(UnusedLocalVariable)
🔇 Additional comments (18)
includes/class-paybutton-public.php (2)
166-169: LGTM! Consistent variable naming.The rename from
user_wallet_addresstopaybutton_user_wallet_addressimproves clarity and reduces potential naming conflicts. The template data key is correctly updated to match.
283-298: LGTM! Consistent variable naming in profile shortcode.The variable renames (
paybutton_user_wallet_address,paybutton_rows) align with the naming conventions introduced across the PR. The database query and template data passing are updated consistently.templates/admin/paybutton-generator.php (1)
6-25: LGTM! Consistent variable naming.The rename to
paybutton_admin_wallet_addressaligns with the naming standardization across the codebase.includes/class-paybutton-admin.php (1)
207-207: LGTM! Template data key updated.The key rename to
paybutton_admin_wallet_addresscorrectly aligns with the template variable changes in paywall-settings.php.templates/admin/paywall-settings.php (1)
20-21: LGTM! Template variable updated.The template correctly uses the renamed
paybutton_admin_wallet_addressvariable, matching the data key passed from the admin class.assets/js/paywalled-content.js (1)
70-74: LGTM! Good defensive programming.The check for
typeof isLoggedIn !== 'undefined'before setting it prevents potential reference errors if the login script hasn't loaded.templates/public/sticky-header.php (2)
6-10: LGTM! Consistent variable naming.The template correctly uses
paybutton_admin_wallet_addressfor the admin wallet check, aligning with the naming standardization.
42-42: LGTM! Consistent variable naming.The conditional correctly uses
paybutton_user_wallet_addressto determine whether to show login or logged-in UI.templates/admin/content.php (1)
40-55: LGTM! Consistent variable naming in loop.The foreach loop variable rename to
paybutton_rowand introduction ofpaybutton_permalinkimprove clarity. All array access is updated consistently.templates/public/profile.php (2)
9-10: LGTM! Consistent variable naming.The wallet address display correctly uses
paybutton_user_wallet_address, aligning with the naming standardization.
14-22: LGTM! Consistent variable naming in content list.The template correctly uses
paybutton_rowsandpaybutton_rowfor the unlocked content list, with proper object property access ($paybutton_row->post_id).templates/admin/customers.php (2)
24-59: LGTM! Variable naming refactor maintains functionality.The refactoring from
$rowto$paybutton_rowwith consistent prefixing is clean and maintains all existing functionality. The object-style property access is appropriate for the data structure being iterated.
101-137: LGTM! Array-style access refactor is correct.The refactoring correctly uses array-style access (
$paybutton_row['key']) for the customers data structure, which differs appropriately from the object-style access in the first loop. All data access patterns are consistent and properly escaped.includes/class-paybutton-ajax.php (5)
53-56: LGTM! Proper AJAX hook registration.The new sticky header endpoint is correctly registered for both authenticated and unauthenticated users, following the same pattern as other AJAX handlers in this class.
314-314: LGTM! Query extended to support auto-login.The query now selects
pb_paywall_user_wallet_addressin addition toid, which is necessary for the auto-login feature implemented below. This is a minimal and appropriate change.
345-349: LGTM! Auto-login logic is secure and well-implemented.The auto-login feature correctly extracts the wallet address from the server-verified database row and sets it in the user's state only if they're not already logged in. The security is sound because:
- The row was inserted by the cryptographically signed webhook in
payment_trigger()- The
unlock_tokenwas validated inajax_validate_unlock_tx()- The wallet address matches the one provided in the AJAX request (verified in the WHERE clause)
This ensures the wallet address originates from a legitimate payment transaction, not user input.
410-410: LGTM! Appropriate phpcs ignore for core WordPress hook.The phpcs ignore comment is correctly used for the
the_contentfilter, which is a core WordPress hook. The inline explanation makes the intent clear.
616-634: The static analysis hint is a false positive; the variable is used in the template, but current indentation is already correct.The
get_sticky_header()method is functionally sound:
- Nonce verification is proper
- Template existence check with appropriate error handling
- The
$paybutton_user_wallet_addressvariable is correctly used in the included template (templates/public/sticky-header.php, line 42), confirming the PHPMD warning is a false positiveThe method body is already properly indented (8 spaces) consistent with WordPress coding standards, so no indentation changes are needed. The file also has a trailing newline after the closing brace.
This PR implements #96. Added a new AJAX endpoint to log in a user who unlocks content automatically.
Test plan:
Summary by CodeRabbit
New Features
Improvements
✏️ Tip: You can customize this high-level summary in your review settings.