-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions-badge.php
More file actions
607 lines (533 loc) · 20.5 KB
/
functions-badge.php
File metadata and controls
607 lines (533 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
<?php
/**
* ============================================
* STATIC COMPILATION STATUS BADGE
* Next.js style indicator for logged-in users
* ============================================
*
* BEHAVIOR:
* - Default ON in development/localhost environments (where it's most useful)
* - Default OFF in production environments (cleaner for clients)
* - Per-user preference overrides defaults
* - Uses existing ?live parameter for mode switching
* - Admin bar toggle for quick enable/disable
* - User profile setting for permanent preference
*
* DETECTION:
* - Development: wp_get_environment_type() === 'development'
* - Development: WP_DEBUG === true
* - Development: localhost, 127.0.0.1, *.local domains
*
* CONTROLS:
* - Admin Bar: Click status → "Show/Hide Badge" + mode switching
* - User Profile: Users → Your Profile → "Static Compilation Badge"
* - Mode Switching: Uses existing ?live=0 (live) or ?live=1 (static) parameters
* - Filter: apply_filters('client_badge_default_enabled', $default, $is_local)
*/
/**
* Check if badge should be displayed
*/
function client_should_show_badge() {
// Only show to logged-in users
if (!is_user_logged_in()) {
return false;
}
// Only on singular pages/posts
if (!is_singular(['page', 'post'])) {
return false;
}
// Check user preference (stored in user meta)
$user_preference = get_user_meta(get_current_user_id(), 'client_badge_enabled', true);
// If user has set a preference, respect it
if ($user_preference !== '') {
return (bool) $user_preference;
}
// Default behavior: ON in development (where it's most useful), OFF in production
$is_local = client_is_local_dev();
// Allow theme/plugin to override default behavior
$default_enabled = apply_filters('client_badge_default_enabled', $is_local, $is_local);
return $default_enabled;
}
/**
* Display compilation status badge on frontend
*/
function client_compilation_badge() {
// Check if badge should be shown
if (!client_should_show_badge()) {
return;
}
$post_id = get_the_ID();
$compiled_time = get_post_meta($post_id, '_client_compiled_time', true);
$stats = get_post_meta($post_id, '_client_compiled_stats', true);
$has_modules = have_rows('modules', $post_id);
// Determine status
$is_live = isset($_GET['live']) && $_GET['live'] === '0';
$force_compiled = isset($_GET['live']) && $_GET['live'] === '1';
// Check if local/dev
$is_local = client_is_local_dev();
// Determine mode based on URL parameters and environment
if ($is_live) {
// Explicitly forced to dynamic rendering (?live=0)
$mode = 'development';
} else if ($force_compiled) {
// Explicitly forced to compiled mode (?live=1)
$mode = !empty($compiled_time) ? 'live' : 'no-compile';
} else if ($is_local) {
// Local dev: default to development (dynamic rendering)
$mode = 'development';
} else {
// Production: default to live if compiled content exists
$mode = !empty($compiled_time) ? 'live' : 'no-compile';
}
?>
<div class="client-badge" data-mode="<?php echo esc_attr($mode); ?>">
<div class="client-badge__inner">
<div class="client-badge__status">
<span class="client-badge__indicator"></span>
<span class="client-badge__mode"><?php
if ($mode === 'live') {
echo 'Live';
} else if ($mode === 'development') {
echo 'Development';
} else {
echo 'No Modules';
}
?></span>
</div>
<?php if ($stats): ?>
<div class="client-badge__stats">
<span class="client-badge__stat">
<strong><?php echo $stats['static']; ?></strong> static modules
</span>
<span class="client-badge__stat">
<strong><?php echo $stats['dynamic']; ?></strong> dynamic modules
</span>
<?php if ($mode === 'live'): ?>
<span class="client-badge__stat">
<strong><?php echo $stats['compile_time']; ?>s</strong> compile time
</span>
<?php endif; ?>
</div>
<?php if ($mode === 'live' && $compiled_time): ?>
<div class="client-badge__time">
Compiled <?php echo human_time_diff(strtotime($compiled_time), current_time('timestamp')); ?> ago
</div>
<?php endif; ?>
<?php elseif ($has_modules): ?>
<div class="client-badge__stats">
<span class="client-badge__stat">
<strong><?php echo $mode === 'live' ? 'Live' : 'No'; ?></strong> compilation data
</span>
<span class="client-badge__stat">
Modules rendering <?php echo $mode === 'live' ? 'dynamically' : 'without stats'; ?>
</span>
</div>
<?php endif; ?>
<?php if ($has_modules): ?>
<div class="client-badge__actions">
<?php
// Determine the correct URL for toggling modes
if ($mode === 'live') {
// Currently live (compiled), switch to development
$toggle_url = add_query_arg('live', '0');
$toggle_text = 'Development';
} else if ($mode === 'development') {
// Currently development (dynamic), switch to live
$toggle_url = add_query_arg('live', '1');
$toggle_text = 'View Live';
} else {
// No modules, try to switch to live
$toggle_url = add_query_arg('live', '1');
$toggle_text = 'View Live';
}
?>
<a href="<?php echo esc_url($toggle_url); ?>" class="client-badge__button">
<?php echo esc_html($toggle_text); ?>
</a>
<?php if (current_user_can('edit_post', $post_id)): ?>
<a href="<?php echo get_edit_post_link($post_id); ?>" class="client-badge__button">
Edit
</a>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<button class="client-badge__toggle" aria-label="Toggle compilation info">
<div class="client-badge__icon"></div>
</button>
</div>
<style>
.client-badge {
position: fixed;
bottom: 20px;
right: 20px;
z-index: 999999;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
font-size: 13px;
line-height: 1.4;
}
.client-badge__toggle {
display: flex;
align-items: center;
justify-content: center;
width: 36px;
height: 36px;
border-radius: 24px;
border: none;
cursor: pointer;
transition: all 0.2s ease;
box-shadow: 0 4px 8px rgba(0,0,0,0.15);
background: #171717;
border: 1.5px solid #424242;
}
.client-badge[data-mode="live"] .client-badge__toggle {
background: #171717;
}
.client-badge[data-mode="development"] .client-badge__toggle {
background: #171717;
}
.client-badge[data-mode="no-compile"] .client-badge__toggle {
background: #171717;
}
.client-badge__toggle:hover {
transform: scale(1.05);
box-shadow: 0 6px 16px rgba(0,0,0,0.2);
}
/* SVG icon with exact colors using CSS mask */
.client-badge__icon {
width: 21px;
height: 21px;
mask: url('<?php echo get_stylesheet_directory_uri(); ?>/img/c2.svg') no-repeat center;
mask-size: contain;
-webkit-mask: url('<?php echo get_stylesheet_directory_uri(); ?>/img/c2.svg') no-repeat center;
-webkit-mask-size: contain;
}
.client-badge[data-mode="live"] .client-badge__icon {
background-color:rgb(82, 255, 96);
}
.client-badge[data-mode="development"] .client-badge__icon {
background-color: #4079cc;
}
.client-badge[data-mode="no-compile"] .client-badge__icon {
background-color: #ec583f;
}
.client-badge__inner {
position: absolute;
bottom: 60px;
right: 0;
background: #fff;
border-radius: 12px;
box-shadow: 0 8px 24px rgba(0,0,0,0.15);
padding: 16px;
min-width: 280px;
opacity: 0;
visibility: hidden;
transform: translateY(10px);
transition: all 0.2s ease;
pointer-events: none;
}
.client-badge.is-open .client-badge__inner {
opacity: 1;
visibility: visible;
transform: translateY(0);
pointer-events: auto;
}
.client-badge__status {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 12px;
padding-bottom: 12px;
border-bottom: 1px solid #e5e7eb;
}
.client-badge__indicator {
width: 8px;
height: 8px;
border-radius: 50%;
animation: pulse 2s ease-in-out infinite;
}
.client-badge[data-mode="live"] .client-badge__indicator {
background: #76f880;
}
.client-badge[data-mode="development"] .client-badge__indicator {
background: #4079cc;
}
.client-badge[data-mode="no-compile"] .client-badge__indicator {
background: #ec583f;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.client-badge__mode {
font-weight: 600;
font-size: 14px;
color: #111827;
}
.client-badge__stats {
display: flex;
flex-direction: column;
gap: 6px;
margin-bottom: 8px;
}
.client-badge__stat {
color: #6b7280;
font-size: 12px;
}
.client-badge__stat strong {
color: #111827;
font-weight: 600;
}
.client-badge__time {
color: #9ca3af;
font-size: 11px;
margin-bottom: 12px;
}
.client-badge__actions {
display: flex;
gap: 8px;
padding-top: 12px;
border-top: 1px solid #e5e7eb;
}
.client-badge__button {
flex: 1;
padding: 6px 12px;
background: #f3f4f6;
color: #374151;
text-decoration: none;
border-radius: 6px;
text-align: center;
font-size: 12px;
font-weight: 500;
transition: all 0.2s ease;
}
.client-badge__button:hover {
background: #e5e7eb;
color: #111827;
}
/* Mobile responsive */
@media (max-width: 768px) {
.client-badge {
bottom: 80px;
right: 16px;
}
.client-badge__toggle {
width: 40px;
height: 40px;
}
.client-badge__inner {
min-width: 260px;
right: -8px;
}
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
const badge = document.querySelector('.client-badge');
const toggle = document.querySelector('.client-badge__toggle');
if (!badge || !toggle) return;
toggle.addEventListener('click', function(e) {
e.stopPropagation();
badge.classList.toggle('is-open');
});
// Close when clicking outside
document.addEventListener('click', function(e) {
if (!badge.contains(e.target)) {
badge.classList.remove('is-open');
}
});
// Close on escape
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
badge.classList.remove('is-open');
}
});
});
</script>
<?php
}
// Hook into wp_footer
add_action('wp_footer', 'client_compilation_badge');
/**
* Add compilation status to admin bar
*/
function client_admin_bar_status($wp_admin_bar) {
if (!is_user_logged_in() || !is_singular(['page', 'post'])) {
return;
}
$post_id = get_the_ID();
$stats = get_post_meta($post_id, '_client_compiled_stats', true);
$compiled_time = get_post_meta($post_id, '_client_compiled_time', true);
$badge_enabled = client_should_show_badge();
// Determine admin bar mode (simplified logic)
$is_live_param = isset($_GET['live']) && $_GET['live'] === '0';
$force_compiled = isset($_GET['live']) && $_GET['live'] === '1';
if ($is_live_param) {
$mode = 'Development';
$color = '#4079cc';
} else if ($force_compiled || (!client_is_local_dev() && !empty($compiled_time))) {
$mode = 'Live';
$color = '#76f880';
} else {
$mode = 'Development';
$color = '#4079cc';
}
$title = '<span style="color: ' . $color . ';">●</span> ' . $mode;
if ($stats) {
if ($mode === 'Development') {
// In development mode, show that modules are rendering live
$total_modules = $stats['static'] + $stats['dynamic'];
$title .= ' (' . $total_modules . 'd)';
} else {
// In live mode, show the compiled breakdown
$title .= ' (' . $stats['static'] . 's/' . $stats['dynamic'] . 'd)';
}
}
// Main status node
$wp_admin_bar->add_node([
'id' => 'client-status',
'title' => $title,
'href' => '#',
'meta' => [
'title' => 'Static Compilation Status',
'onclick' => $badge_enabled ? 'document.querySelector(".client-badge__toggle")?.click(); return false;' : ''
]
]);
// Mode switching using existing ?live parameter
if (have_rows('modules', $post_id)) {
// Determine current mode based on same logic as badge
$is_live_param = isset($_GET['live']) && $_GET['live'] === '0';
$force_compiled_param = isset($_GET['live']) && $_GET['live'] === '1';
if ($mode === 'Live') {
$wp_admin_bar->add_node([
'parent' => 'client-status',
'id' => 'client-switch-dev',
'title' => '🔵 Switch to Development',
'href' => add_query_arg('live', '0'),
'meta' => ['title' => 'Switch to development rendering mode']
]);
} else {
$wp_admin_bar->add_node([
'parent' => 'client-status',
'id' => 'client-switch-live',
'title' => '🟢 Switch to Live',
'href' => add_query_arg('live', '1'),
'meta' => ['title' => 'Switch to live compilation mode']
]);
}
}
// Add rebuild static option for admins
if (current_user_can('manage_options')) {
$rebuild_url = wp_nonce_url(admin_url('admin-post.php?action=client_rebuild_all'), 'client_rebuild_all');
$wp_admin_bar->add_node([
'parent' => 'client-status',
'id' => 'client-rebuild-static',
'title' => '🔄 Rebuild Static',
'href' => $rebuild_url,
'meta' => ['title' => 'Rebuild all static compiled content']
]);
}
}
add_action('admin_bar_menu', 'client_admin_bar_status', 100);
/**
* Add badge setting to user profile
*/
function client_badge_user_profile_fields($user) {
if (!current_user_can('edit_user', $user->ID)) {
return;
}
$badge_enabled = get_user_meta($user->ID, 'client_badge_enabled', true);
$is_local = client_is_local_dev();
$default_text = $is_local ? 'ON (development)' : 'OFF (production)';
?>
<h3>Static Compilation Badge</h3>
<table class="form-table">
<tr>
<th><label for="client_badge_enabled">Show Compilation Badge</label></th>
<td>
<select name="client_badge_enabled" id="client_badge_enabled">
<option value="" <?php selected($badge_enabled, ''); ?>>Default (<?php echo $default_text; ?>)</option>
<option value="1" <?php selected($badge_enabled, '1'); ?>>Always Show</option>
<option value="0" <?php selected($badge_enabled, '0'); ?>>Always Hide</option>
</select>
<p class="description">
Controls the Next.js style compilation status badge on pages/posts.<br>
<strong>Mode Switching:</strong> Use existing <code>?live=0</code> (live) or <code>?live=1</code> (static) parameters to switch rendering modes.
</p>
</td>
</tr>
</table>
<?php
}
add_action('show_user_profile', 'client_badge_user_profile_fields');
add_action('edit_user_profile', 'client_badge_user_profile_fields');
/**
* Save badge setting from user profile
*/
function client_badge_save_user_profile_fields($user_id) {
if (!current_user_can('edit_user', $user_id)) {
return;
}
if (isset($_POST['client_badge_enabled'])) {
$value = sanitize_text_field($_POST['client_badge_enabled']);
if (in_array($value, ['', '0', '1'])) {
update_user_meta($user_id, 'client_badge_enabled', $value);
}
}
}
add_action('personal_options_update', 'client_badge_save_user_profile_fields');
add_action('edit_user_profile_update', 'client_badge_save_user_profile_fields');
/**
* Add keyboard shortcut hint (Optional - Ctrl+Shift+S)
*/
function client_badge_keyboard_hint() {
if (!is_user_logged_in() || !is_singular(['page', 'post'])) {
return;
}
?>
<script>
// Keyboard shortcut: Ctrl+Shift+S to toggle badge
document.addEventListener('keydown', function(e) {
if (e.ctrlKey && e.shiftKey && e.key === 'S') {
e.preventDefault();
const badge = document.querySelector('.client-badge');
if (badge) {
badge.classList.toggle('is-open');
}
}
});
</script>
<?php
}
add_action('wp_footer', 'client_badge_keyboard_hint');
/**
* ============================================
* REBUILD STATIC FUNCTIONALITY
* Moved from functions.php to keep badge-related code together
* ============================================
*/
/**
* Admin-post handler for rebuild all
*/
function client_handle_rebuild_all() {
if (!current_user_can('manage_options')) {
wp_die(__('Insufficient permissions.'));
}
check_admin_referer('client_rebuild_all');
// Use existing functions from main functions.php
client_enqueue_all_rebuild();
client_schedule_rebuild_cron();
$redirect = wp_get_referer() ?: admin_url();
wp_safe_redirect(add_query_arg('client_rebuild', 'queued', $redirect));
exit;
}
add_action('admin_post_client_rebuild_all', 'client_handle_rebuild_all');
/**
* Admin notice after queuing rebuild
*/
function client_admin_rebuild_notice() {
if (!current_user_can('manage_options')) return;
if (!isset($_GET['client_rebuild']) || $_GET['client_rebuild'] !== 'queued') return;
echo '<div class="notice notice-success is-dismissible"><p>Rebuild queued. Batches will run in the background.</p></div>';
}
add_action('admin_notices', 'client_admin_rebuild_notice');