Add early click handling for WUBox and improve pre-commit hook#288
Add early click handling for WUBox and improve pre-commit hook#288superdav42 merged 1 commit intomainfrom
Conversation
## WUBox Early Click Handling
### Problem
Users clicking on `.wubox` elements before the script fully loads would experience:
- Click being ignored
- No visual feedback
- Need to click again after page loads
- Poor user experience on slow connections
### Solution
Implemented a two-phase early click capture system:
#### Phase 1: Inline Script (inc/class-scripts.php)
Added an inline script that runs **before** wubox.js loads:
```javascript
window.__wuboxEarlyClicks = [];
window.__wuboxEarlyClickHandler = function(e) {
if (window.__wuboxReady) return;
var t = e.target.closest('.wubox');
if (!t) return;
e.preventDefault();
e.stopPropagation();
t.style.cursor = 'wait';
window.__wuboxEarlyClicks.push(t);
};
document.addEventListener('click', window.__wuboxEarlyClickHandler, true);
```
**What it does:**
- Captures clicks on `.wubox` elements immediately
- Prevents default action and propagation
- Shows 'wait' cursor for visual feedback
- Queues clicked elements for later processing
#### Phase 2: Processing Queue (assets/js/wubox.js)
When wubox.js loads and DOMContentLoaded fires:
```javascript
window.__wuboxReady = true;
// Remove early click listener
if (window.__wuboxEarlyClickHandler) {
document.removeEventListener('click', window.__wuboxEarlyClickHandler, true);
delete window.__wuboxEarlyClickHandler;
}
// Process queued clicks
if (window.__wuboxEarlyClicks && window.__wuboxEarlyClicks.length > 0) {
const uniqueClicks = [...new Set(window.__wuboxEarlyClicks)];
uniqueClicks.forEach((target) => {
const caption = target.title || target.name || '';
const url = target.href || target.alt;
const imageGroup = target.rel || false;
target.style.cursor = '';
window.wubox.show(caption, url, imageGroup);
});
window.__wuboxEarlyClicks = [];
}
```
**What it does:**
- Marks wubox as ready
- Removes the early click listener (no longer needed)
- Deduplicates queued clicks
- Processes each unique click through wubox.show()
- Restores cursor to normal
### Benefits
✅ No more lost clicks on slow connections
✅ Immediate visual feedback (wait cursor)
✅ Automatic queue processing when ready
✅ Prevents duplicate processing
✅ Clean cleanup of event listeners
✅ Better user experience
### Files Changed
1. **inc/class-scripts.php** - Added inline early click handler
2. **assets/js/wubox.js** - Added queue processing on DOMContentLoaded
3. **assets/js/wubox.min.js** - Minified version updated
---
## Pre-Commit Hook Improvements
### File: .githooks/pre-commit
**Enhanced auto-fixing capabilities:**
#### Before:
- PHPCS found errors
- Showed error messages
- Commit failed
- Manual fixing required
#### After:
- PHPCS finds errors
- **Automatically runs PHPCBF** to fix them
- **Re-stages fixed files**
- **Re-validates** after fixes
- Only fails if errors remain after auto-fix
- Shows clear success/failure messages
**New Features:**
1. **Auto-fix attempt**: Runs PHPCBF on files with PHPCS errors
2. **Smart re-staging**: Automatically stages files that were fixed
3. **Re-validation**: Checks if errors were resolved
4. **Clear feedback**: Shows which files were fixed vs. which still have issues
5. **Better UX**: Green checkmarks for fixed files, red X for unfixable errors
**Code Example:**
```bash
if [ $HAS_PHPCS_ERRORS -ne 0 ]; then
echo "PHPCS found errors. Running PHPCBF to auto-fix..."
for FILE in $PHPCS_FAILED_FILES; do
vendor/bin/phpcbf "$FILE" || true
if vendor/bin/phpcs --colors "$FILE" 2>&1; then
echo "✓ Auto-fixed: $FILE"
git add "$FILE" # Re-stage the fixed file
else
echo "✗ Could not fully fix: $FILE"
fi
done
}
```
**Benefits:**
✅ Fewer manual interventions needed
✅ Faster commit workflow
✅ Automatic compliance with coding standards
✅ Better developer experience
✅ Clearer error reporting
---
## Impact Summary
### WUBox:
- 🚀 Better performance on slow connections
- 👆 No more lost clicks
- ⏳ Visual feedback during loading
- 🎯 Improved user experience
### Pre-Commit Hook:
- ⚡ Faster commit workflow
- 🔧 Automatic code standard fixes
- 📊 Clear feedback on what was fixed
- 🎨 Better developer experience
---
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
WalkthroughThree files were modified to enhance developer tooling and improve wubox click handling. The pre-commit hook now tracks PHPCS failures, auto-fixes them with PHPCBF, re-scans fixed files, and narrows PHPStan analysis to staged files only. An early-click queueing system was added to handle clicks on wubox elements before the script fully loads. Changes
Sequence DiagramsequenceDiagram
participant User
participant DOM as Document (early)
participant InlineScript as Inline Script<br/>(class-scripts.php)
participant WuboxJS as wubox.js
participant Window as Window Globals
Note over DOM,Window: Before wubox.js loads
User->>DOM: Click .wubox element
DOM->>InlineScript: Capture phase listener
InlineScript->>InlineScript: preventDefault()<br/>stopPropagation()
InlineScript->>InlineScript: Set cursor: wait
InlineScript->>Window: Queue target in<br/>__wuboxEarlyClicks
Note over DOM,Window: wubox.js loads
WuboxJS->>Window: DOMContentLoaded fires
WuboxJS->>Window: Mark wubox as ready
WuboxJS->>Window: Remove early listener
WuboxJS->>Window: Deduplicate queue
loop For each queued target
WuboxJS->>WuboxJS: Trigger show(target)
end
WuboxJS->>Window: Clear queue
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
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 |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
inc/class-scripts.php (1)
253-269: Consider adding a timeout fallback if wubox.js fails to load.If the main
wubox.jsscript fails to load (network error, blocked, etc.), the early click handler will remain attached indefinitely and clicked elements will be stuck withcursor: wait.Consider adding a timeout to restore normal behavior:
wp_add_inline_script( 'wubox', "(function(){ window.__wuboxEarlyClicks=[]; window.__wuboxEarlyClickHandler=function(e){ if(window.__wuboxReady)return; var t=e.target.closest('.wubox'); if(!t)return; e.preventDefault(); e.stopPropagation(); t.style.cursor='wait'; window.__wuboxEarlyClicks.push(t); }; document.addEventListener('click',window.__wuboxEarlyClickHandler,true); + // Fallback: remove handler after 30s if wubox never loads + setTimeout(function(){ + if(!window.__wuboxReady){ + document.removeEventListener('click',window.__wuboxEarlyClickHandler,true); + window.__wuboxEarlyClicks.forEach(function(t){t.style.cursor='';}); + window.__wuboxEarlyClicks=[]; + } + },30000); })();", 'before' );
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
assets/js/wubox.min.jsis excluded by!**/*.min.js
📒 Files selected for processing (3)
.githooks/pre-commit(2 hunks)assets/js/wubox.js(2 hunks)inc/class-scripts.php(1 hunks)
🔇 Additional comments (4)
assets/js/wubox.js (1)
447-472: LGTM on the early click processing logic.The implementation correctly:
- Marks wubox as ready to prevent further queuing
- Removes the early click handler to clean up
- Deduplicates queued elements by reference
- Restores cursor and triggers the modal
inc/class-scripts.php (1)
249-269: Good implementation of early click capture.The inline script correctly:
- Uses an IIFE to avoid polluting the global scope unnecessarily
- Attaches to the capture phase to intercept clicks before bubbling handlers
- Uses
closest('.wubox')for reliable element detection- Provides immediate visual feedback with cursor change
.githooks/pre-commit (2)
46-79: Good auto-fix workflow with re-staging.The workflow correctly:
- Attempts to auto-fix with PHPCBF
- Verifies fixes by re-running PHPCS
- Only stages files that are now clean
- Provides clear feedback on what was fixed vs. what requires manual intervention
96-104: LGTM on error handling and user feedback.The enhanced error messages provide clear guidance on:
- Which checks failed
- How to see remaining errors manually
- How to bypass if needed
| PHPCS_FAILED_FILES="" | ||
| for FILE in $STAGED_FILES; do | ||
| if [ -f "$FILE" ]; then | ||
| vendor/bin/phpcs --colors "$FILE" || HAS_PHPCS_ERRORS=1 | ||
| if ! vendor/bin/phpcs --colors "$FILE"; then | ||
| PHPCS_FAILED_FILES="$PHPCS_FAILED_FILES $FILE" | ||
| HAS_PHPCS_ERRORS=1 | ||
| fi | ||
| fi | ||
| done |
There was a problem hiding this comment.
Word splitting issue: filenames with spaces will break the loop.
The variable PHPCS_FAILED_FILES is built by string concatenation and later iterated with unquoted expansion. If any PHP filename contains spaces, it will be split incorrectly.
Consider using arrays instead:
HAS_PHPCS_ERRORS=0
-PHPCS_FAILED_FILES=""
+PHPCS_FAILED_FILES=()
for FILE in $STAGED_FILES; do
if [ -f "$FILE" ]; then
if ! vendor/bin/phpcs --colors "$FILE"; then
- PHPCS_FAILED_FILES="$PHPCS_FAILED_FILES $FILE"
+ PHPCS_FAILED_FILES+=("$FILE")
HAS_PHPCS_ERRORS=1
fi
fi
doneAnd update the loop at line 51:
- for FILE in $PHPCS_FAILED_FILES; do
+ for FILE in "${PHPCS_FAILED_FILES[@]}"; do📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| PHPCS_FAILED_FILES="" | |
| for FILE in $STAGED_FILES; do | |
| if [ -f "$FILE" ]; then | |
| vendor/bin/phpcs --colors "$FILE" || HAS_PHPCS_ERRORS=1 | |
| if ! vendor/bin/phpcs --colors "$FILE"; then | |
| PHPCS_FAILED_FILES="$PHPCS_FAILED_FILES $FILE" | |
| HAS_PHPCS_ERRORS=1 | |
| fi | |
| fi | |
| done | |
| PHPCS_FAILED_FILES=() | |
| for FILE in $STAGED_FILES; do | |
| if [ -f "$FILE" ]; then | |
| if ! vendor/bin/phpcs --colors "$FILE"; then | |
| PHPCS_FAILED_FILES+=("$FILE") | |
| HAS_PHPCS_ERRORS=1 | |
| fi | |
| fi | |
| done |
🤖 Prompt for AI Agents
.githooks/pre-commit around lines 36 to 44: the script builds PHPCS_FAILED_FILES
as a space-separated string which breaks on filenames with spaces; switch
PHPCS_FAILED_FILES to a bash array (initialize empty array), append failing
files with array syntax (e.g., PHPCS_FAILED_FILES+=("$FILE")), and later iterate
over the array using the proper indexed expansion (\"${PHPCS_FAILED_FILES[@]}\")
when processing at the later loop (around line 51).
| if (window.__wuboxEarlyClicks && window.__wuboxEarlyClicks.length > 0) { | ||
| // Remove duplicates - only keep unique elements | ||
| const uniqueClicks = [...new Set(window.__wuboxEarlyClicks)]; | ||
|
|
||
| uniqueClicks.forEach((target) => { | ||
| const caption = target.title || target.name || ''; | ||
| const url = target.href || target.alt; | ||
| const imageGroup = target.rel || false; | ||
| target.style.cursor = ''; | ||
| window.wubox.show(caption, url, imageGroup); | ||
| }); | ||
| window.__wuboxEarlyClicks = []; |
There was a problem hiding this comment.
Potential issue: Multiple modals may stack if multiple early clicks were captured.
If a user clicks multiple different .wubox elements before wubox loads, the forEach loop will call window.wubox.show() for each without waiting for the previous modal to close. This could result in stacked modals or visual glitches.
Consider processing only the first/last click, or ensuring previous modals are removed before showing the next:
uniqueClicks.forEach((target) => {
const caption = target.title || target.name || '';
const url = target.href || target.alt;
const imageGroup = target.rel || false;
- target.style.cursor = '';
+ target.style.cursor = '';
window.wubox.show(caption, url, imageGroup);
});Alternatively, if only a single modal should be shown, consider processing only the last clicked element:
if (uniqueClicks.length > 0) {
const target = uniqueClicks[uniqueClicks.length - 1];
// restore cursors for all
uniqueClicks.forEach(t => t.style.cursor = '');
const caption = target.title || target.name || '';
const url = target.href || target.alt;
const imageGroup = target.rel || false;
window.wubox.show(caption, url, imageGroup);
}🤖 Prompt for AI Agents
In assets/js/wubox.js around lines 459 to 470, multiple early clicks are
iterated and window.wubox.show() is called for each, which can stack modals;
instead pick a single click to show (e.g., the last item), restore cursor styles
for all captured targets, then call window.wubox.show() only once for that
selected target and finally clear window.__wuboxEarlyClicks; if you prefer to
allow sequential shows, ensure you close any existing modal before calling show
(call window.wubox.close() if available) to prevent visual stacking.
Add Early Click Handling for WUBox and Improve Pre-Commit Hook
🎯 Overview
This PR includes two important improvements:
🖱️ WUBox Early Click Handling
The Problem
Users clicking on
.wuboxelements before the script fully loads would experience:The Solution
Implemented a two-phase early click capture system:
Phase 1: Inline Script (Before wubox.js loads)
File:
inc/class-scripts.phpAdded an inline script that runs immediately before wubox.js loads:
What it does:
.wuboxelements immediatelyPhase 2: Processing Queue (When wubox.js loads)
File:
assets/js/wubox.jsWhen wubox.js loads and DOMContentLoaded fires:
What it does:
wubox.show()Benefits
✅ No more lost clicks on slow connections
✅ Immediate visual feedback with wait cursor
✅ Automatic queue processing when ready
✅ Prevents duplicate processing of same element
✅ Clean cleanup of event listeners
✅ Better user experience overall
Use Case
Perfect for:
🔧 Pre-Commit Hook Improvements
File:
.githooks/pre-commitEnhanced auto-fixing capabilities for better developer experience
Before This Change:
After This Change:
New Features
1. Auto-Fix Attempt
2. Smart Re-Staging
3. Clear Feedback
4. Re-Validation
After auto-fixing, re-runs PHPCS to check if all errors are resolved:
Benefits
✅ Fewer manual interventions needed
✅ Faster commit workflow - no manual PHPCBF runs
✅ Automatic compliance with coding standards
✅ Better developer experience - less context switching
✅ Clearer error reporting - know exactly what needs fixing
📁 Files Changed
WUBox Early Click Handling:
Pre-Commit Hook:
Stats: +99 additions, -11 deletions
🎬 Demo Scenarios
Scenario 1: WUBox on Slow Connection
Before:
After:
Scenario 2: Pre-Commit Hook
Before:
After:
✨ Impact Summary
WUBox:
Pre-Commit Hook:
🧪 Testing
Manual Testing - WUBox
.wuboxelementsManual Testing - Pre-Commit Hook
🤖 Generated with Claude Code
Co-Authored-By: Claude noreply@anthropic.com
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.