-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore-init.php
More file actions
721 lines (626 loc) · 24.2 KB
/
Copy pathcore-init.php
File metadata and controls
721 lines (626 loc) · 24.2 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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
<?php
/**
* This file initializes all BB Core components.
*
* @link https://wbcomdesigns.com/contact/
* @since 1.0.0
* @package BP_Birthdays
*/
// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! defined( 'WPINC' ) ) {
die;
}
// Define Our Constants.
define( 'BB_CORE_INC', __DIR__ . '/assets/inc/' );
define( 'BB_CORE_IMG', plugins_url( 'assets/img/', __FILE__ ) );
define( 'BB_CORE_CSS', plugins_url( 'assets/css/', __FILE__ ) );
define( 'BB_CORE_JS', plugins_url( 'assets/js/', __FILE__ ) );
/**
* Global flag to track if assets are loaded
*
* @global bool $bb_assets_loaded
*/
$bb_assets_loaded = false;
/**
* Register CSS with enhanced smart loading.
*/
function bb_register_core_css() {
global $bb_assets_loaded;
// Prevent duplicate loading.
if ( $bb_assets_loaded || wp_style_is( 'bb-core', 'enqueued' ) ) {
return;
}
// Load on appropriate pages.
if ( bb_should_load_assets() ) {
/*
* Serve the minified build unless SCRIPT_DEBUG is on.
*
* grunt builds bb-core.min.css but nothing ever enqueued it, so every
* visitor was served the unminified source (10780 B against 6893 B) and
* the minified bundle shipped in the zip as dead weight. Keep this in
* step with the cssmin/uglify targets in gruntfile.js: if a target is
* added or removed, this suffix has to follow.
*/
$bb_suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.css' : '.min.css';
wp_enqueue_style(
'bb-core',
BB_CORE_CSS . 'bb-core' . $bb_suffix,
array(),
BIRTHDAY_WIDGET_VERSION,
'all'
);
$bb_assets_loaded = true;
}
}
add_action( 'wp_enqueue_scripts', 'bb_register_core_css', 10 );
/**
* Register JS with enhanced smart loading.
*/
function bb_register_core_js() {
global $bb_assets_loaded;
// Prevent duplicate loading.
if ( wp_script_is( 'bb-core', 'enqueued' ) ) {
return;
}
// Load on appropriate pages.
if ( bb_should_load_assets() ) {
// See the note in bb_register_core_css(): the minified build was shipped
// but never enqueued (29168 B against 14307 B).
$bb_suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.js' : '.min.js';
wp_enqueue_script(
'bb-core',
BB_CORE_JS . 'bb-core' . $bb_suffix,
array( 'jquery' ),
BIRTHDAY_WIDGET_VERSION,
true
);
// Enhanced localization.
// Determine confetti setting from saved options (don't rely on admin class being loaded).
$confetti_enabled = false;
$bb_opts = get_option( 'bp_birthdays_settings', array() );
if ( is_array( $bb_opts ) && isset( $bb_opts['confetti_enabled'] ) ) {
$confetti_enabled = (bool) $bb_opts['confetti_enabled'];
}
wp_localize_script(
'bb-core',
'bbBirthdays',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'bb_birthdays_nonce' ),
'plugin_url' => plugins_url( '', __FILE__ ),
'version' => BIRTHDAY_WIDGET_VERSION,
'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG,
// Every key bb-core.js reads via bbBirthdays.strings.<key>
// MUST be seeded here with a __() value: a key the JS reads
// but PHP never seeds renders its English fallback on every
// locale forever (docs/standards/i18n.md trap 1).
'strings' => array(
'loading' => __( 'Loading...', 'buddypress-birthdays' ),
'error' => __( 'Error occurred', 'buddypress-birthdays' ),
'send_wishes' => __( 'Send my wishes', 'buddypress-birthdays' ),
'wishes_sent' => __( 'Birthday wishes sent!', 'buddypress-birthdays' ),
'wishes_error' => __( 'Unable to send wishes at this time.', 'buddypress-birthdays' ),
'happy_birthday' => __( 'Happy Birthday!', 'buddypress-birthdays' ),
'no_birthdays' => __( 'No upcoming birthdays', 'buddypress-birthdays' ),
'today' => __( 'Today', 'buddypress-birthdays' ),
'tomorrow' => __( 'Tomorrow', 'buddypress-birthdays' ),
// Accessible name applied to every send-wishes button by
// bb-core.js initAccessibility(). Mirrors the title
// attribute the widget renders server-side.
'send_wishes_aria' => __( 'Send birthday wishes', 'buddypress-birthdays' ),
// Landmark label applied to each birthday widget region.
'widget_aria_label' => __( 'Birthday notifications', 'buddypress-birthdays' ),
// NOTE: 'notifications_on' was dropped in 2.5.0 alongside the
// browser-notification subsystem in bb-core.js (the permission
// prompt had no caller that ever sent a notification). Do not
// re-add the string without re-adding a real consumer.
),
'settings' => array(
'animation_speed' => apply_filters( 'bb_birthdays_animation_speed', 300 ),
'tooltip_delay' => apply_filters( 'bb_birthdays_tooltip_delay', 300 ),
'cache_duration' => apply_filters( 'bb_birthdays_cache_duration', 1800 ), // 30 minutes.
// Frontend toggle for confetti animation (from admin settings).
'confetti_enabled' => $confetti_enabled,
),
)
);
}
}
add_action( 'wp_enqueue_scripts', 'bb_register_core_js', 10 );
/**
* Force load assets - for widgets displayed in footer or dynamic content
*/
function bb_force_load_assets() {
global $bb_assets_loaded;
if ( ! $bb_assets_loaded ) {
bb_register_core_css();
bb_register_core_js();
}
}
/**
* Enhanced asset loading check - runs in footer for late-loaded widgets
*/
function bb_footer_asset_check() {
// Check if widget is active but assets weren't loaded.
if ( is_active_widget( false, false, 'widget_buddypress_birthdays' ) ) {
if ( ! wp_style_is( 'bb-core', 'enqueued' ) && ! wp_style_is( 'bb-core', 'done' ) ) {
// Force load assets in footer.
bb_force_load_assets();
}
}
// Also check for shortcode in dynamic content.
global $post;
if ( $post && has_shortcode( $post->post_content, 'bp_birthdays' ) ) {
if ( ! wp_style_is( 'bb-core', 'enqueued' ) && ! wp_style_is( 'bb-core', 'done' ) ) {
bb_force_load_assets();
}
}
}
add_action( 'wp_footer', 'bb_footer_asset_check', 5 );
/**
* Check if assets should be loaded - Enhanced for BuddyPress and all scenarios.
*
* @return bool Whether assets should be loaded.
*/
function bb_should_load_assets() {
// Always load if widget is active (most important check).
if ( is_active_widget( false, false, 'widget_buddypress_birthdays' ) ) {
return true;
}
// Load in admin/customizer.
if ( is_admin() || is_customize_preview() ) {
return true;
}
// Check for shortcode in current post content.
global $post;
if ( $post && has_shortcode( $post->post_content, 'bp_birthdays' ) ) {
return true;
}
// Load on all BuddyPress pages.
if ( function_exists( 'bp_is_directory' ) && bp_is_directory() ) {
return true;
}
if ( function_exists( 'bp_is_user' ) && bp_is_user() ) {
return true;
}
if ( function_exists( 'bp_is_group' ) && bp_is_group() ) {
return true;
}
if ( function_exists( 'bp_is_activity_component' ) && bp_is_activity_component() ) {
return true;
}
// Load on BuddyBoss pages.
if ( function_exists( 'buddyboss_theme' ) ) {
return true;
}
// Load if Youzify is active (they often use widgets everywhere).
if ( function_exists( 'youzify' ) || defined( 'YOUZIFY_VERSION' ) ) {
return true;
}
// Load on common pages where widgets might appear.
if ( is_home() || is_front_page() || is_page() || is_single() || is_archive() ) {
return true;
}
// Load if we're in a widget area context.
if ( bb_check_widget_areas() ) {
return true;
}
// Allow themes/plugins to force loading.
return apply_filters( 'bb_core_load_assets', false );
}
/**
* Check if birthday widget is present in any widget area
*
* @return bool Whether widget is found in any widget area.
*/
function bb_check_widget_areas() {
// Ensure function is available (may not be during early loading).
if ( ! function_exists( 'wp_get_sidebars_widgets' ) ) {
return false;
}
// phpcs:ignore Generic.PHP.ForbiddenFunctions.Found -- Required for widget detection.
$sidebars_widgets = wp_get_sidebars_widgets();
if ( ! is_array( $sidebars_widgets ) ) {
return false;
}
foreach ( $sidebars_widgets as $sidebar_id => $widgets ) {
if ( is_array( $widgets ) ) {
foreach ( $widgets as $widget ) {
if ( false !== strpos( $widget, 'widget_buddypress_birthdays' ) ) {
return true;
}
}
}
}
return false;
}
/**
* Load the Widget File.
*/
if ( file_exists( BB_CORE_INC . 'buddypress-birthdays-widget.php' ) ) {
require_once BB_CORE_INC . 'buddypress-birthdays-widget.php';
}
/**
* Shortcode support with automatic asset loading
*
* @param array $atts Shortcode attributes.
* @return string Shortcode output.
*/
function bb_birthdays_shortcode( $atts ) {
// Force load assets when shortcode is used.
bb_force_load_assets();
$atts = shortcode_atts(
array(
'title' => __( 'Upcoming Birthdays', 'buddypress-birthdays' ),
'limit' => 5,
'show_age' => 'yes',
'show_message_button' => 'yes',
'date_format' => 'F d',
'range_limit' => 'no_limit',
'show_birthdays_of' => 'all',
'display_name_type' => 'user_name',
'emoji' => 'balloon',
'field_name' => '',
'birthdays_per_page' => 10,
),
$atts,
'bp_birthdays'
);
// Check if widget class exists.
if ( ! class_exists( 'Widget_Buddypress_Birthdays' ) ) {
return '<p>' . esc_html__( 'Birthday widget not available.', 'buddypress-birthdays' ) . '</p>';
}
// If field_name is not provided or empty, find the first available date field.
if ( empty( $atts['field_name'] ) ) {
global $wpdb;
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$atts['field_name'] = $wpdb->get_var(
"SELECT id FROM {$wpdb->prefix}bp_xprofile_fields WHERE type IN ('datebox', 'birthdate') LIMIT 1"
);
}
// Create widget instance.
$widget = new Widget_Buddypress_Birthdays();
// Convert shortcode atts to widget instance format.
$instance = array(
'title' => $atts['title'],
'birthdays_to_display' => (int) $atts['limit'],
'display_age' => $atts['show_age'],
'birthday_send_message' => $atts['show_message_button'],
'birthday_date_format' => $atts['date_format'],
'birthdays_range_limit' => $atts['range_limit'],
'show_birthdays_of' => $atts['show_birthdays_of'],
'display_name_type' => $atts['display_name_type'],
'emoji' => $atts['emoji'],
'birthday_field_name' => (int) $atts['field_name'],
'birthdays_per_page' => (int) $atts['birthdays_per_page'],
);
$args = array(
'before_widget' => '<div class="bp-birthdays-shortcode widget_bp_birthdays">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
);
ob_start();
$widget->widget( $args, $instance );
return ob_get_clean();
}
add_shortcode( 'bp_birthdays', 'bb_birthdays_shortcode' );
/**
* Enhanced debug function to check widget loading (for development).
*/
function bb_debug_widget_loading() {
$debug_birthdays = filter_input( INPUT_GET, 'debug_birthdays', FILTER_SANITIZE_NUMBER_INT );
if ( defined( 'WP_DEBUG' ) && WP_DEBUG && current_user_can( 'manage_options' ) && $debug_birthdays ) {
echo '<div style="background: #f0f0f0; padding: 15px; margin: 10px; border: 1px solid #ddd; border-radius: 5px; font-family: monospace; font-size: 12px;">';
echo '<strong style="color: #333; font-size: 14px;">🎂 Birthday Widget Debug Info:</strong><br><br>';
// Widget status.
echo '<strong>Widget Status:</strong><br>';
echo ' Active: ' . ( is_active_widget( false, false, 'widget_buddypress_birthdays' ) ? '<span style="color: green;">YES</span>' : '<span style="color: red;">NO</span>' ) . '<br>';
echo ' Class Exists: ' . ( class_exists( 'Widget_Buddypress_Birthdays' ) ? '<span style="color: green;">YES</span>' : '<span style="color: red;">NO</span>' ) . '<br><br>';
// BuddyPress status.
echo '<strong>BuddyPress Status:</strong><br>';
echo ' Active: ' . ( function_exists( 'bp_is_active' ) ? '<span style="color: green;">YES</span>' : '<span style="color: red;">NO</span>' ) . '<br>';
echo ' Version: ' . ( defined( 'BP_VERSION' ) ? esc_html( BP_VERSION ) : 'N/A' ) . '<br><br>';
// Page context.
echo '<strong>Page Context:</strong><br>';
echo ' Current: ';
if ( is_home() ) {
echo 'Home';
} elseif ( is_front_page() ) {
echo 'Front Page';
} elseif ( is_page() ) {
echo 'Page';
} elseif ( is_single() ) {
echo 'Single Post';
} elseif ( function_exists( 'bp_is_user' ) && bp_is_user() ) {
echo 'BP User Profile';
} elseif ( function_exists( 'bp_is_directory' ) && bp_is_directory() ) {
echo 'BP Directory';
} else {
echo 'Other';
}
echo '<br><br>';
// Asset loading status.
echo '<strong>Asset Loading:</strong><br>';
echo ' Should Load: ' . ( bb_should_load_assets() ? '<span style="color: green;">YES</span>' : '<span style="color: red;">NO</span>' ) . '<br>';
echo ' CSS Loaded: ' . ( wp_style_is( 'bb-core', 'done' ) ? '<span style="color: green;">YES</span>' : ( wp_style_is( 'bb-core', 'enqueued' ) ? '<span style="color: orange;">QUEUED</span>' : '<span style="color: red;">NO</span>' ) ) . '<br>';
echo ' JS Loaded: ' . ( wp_script_is( 'bb-core', 'done' ) ? '<span style="color: green;">YES</span>' : ( wp_script_is( 'bb-core', 'enqueued' ) ? '<span style="color: orange;">QUEUED</span>' : '<span style="color: red;">NO</span>' ) ) . '<br><br>';
// Widget locations.
echo '<strong>Widget Locations:</strong><br>';
// phpcs:ignore Generic.PHP.ForbiddenFunctions.Found -- Required for widget detection.
$sidebars = function_exists( 'wp_get_sidebars_widgets' ) ? wp_get_sidebars_widgets() : array();
$found_widgets = array();
foreach ( $sidebars as $sidebar_id => $widgets ) {
if ( ! empty( $widgets ) && is_array( $widgets ) ) {
foreach ( $widgets as $widget ) {
if ( false !== strpos( $widget, 'widget_buddypress_birthdays' ) ) {
$found_widgets[] = $sidebar_id;
}
}
}
}
if ( ! empty( $found_widgets ) ) {
echo ' Found in: <span style="color: green;">' . esc_html( implode( ', ', $found_widgets ) ) . '</span><br>';
} else {
echo ' <span style="color: red;">No widgets found in any sidebar</span><br>';
}
// Shortcode check.
global $post;
if ( $post && has_shortcode( $post->post_content, 'bp_birthdays' ) ) {
echo ' Shortcode: <span style="color: green;">FOUND in current post</span><br>';
} else {
echo ' Shortcode: <span style="color: gray;">Not found</span><br>';
}
echo '<br><strong>URLs:</strong><br>';
echo ' CSS: ' . esc_url( BB_CORE_CSS . 'bb-core.css' ) . '<br>';
echo ' JS: ' . esc_url( BB_CORE_JS . 'bb-core.js' ) . '<br>';
echo '<br><small style="color: #666;">Add ?debug_birthdays=1 to any URL to see this debug info</small>';
echo '</div>';
}
}
add_action( 'wp_footer', 'bb_debug_widget_loading', 999 );
/**
* AJAX handler for birthday-related actions
*/
function bb_birthdays_ajax_handler() {
// Verify nonce.
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
if ( ! wp_verify_nonce( $nonce, 'bb_birthdays_nonce' ) ) {
wp_die( esc_html__( 'Security check failed', 'buddypress-birthdays' ) );
}
$action = isset( $_POST['birthday_action'] ) ? sanitize_key( wp_unslash( $_POST['birthday_action'] ) ) : '';
switch ( $action ) {
case 'refresh_widget':
// Only allow logged-in users with read capability to refresh cache.
if ( ! is_user_logged_in() || ! current_user_can( 'read' ) ) {
wp_send_json_error( __( 'Authentication required', 'buddypress-birthdays' ) );
break;
}
// Clear birthday cache.
bb_clear_birthday_caches();
wp_send_json_success( array( 'message' => __( 'Widget refreshed', 'buddypress-birthdays' ) ) );
break;
case 'mark_wished':
// Mark that user has been wished. Fired (fire-and-forget) by
// bb-core.js recordWish() when a member clicks the send-wishes
// button, before the browser navigates to the compose screen.
$user_id = isset( $_POST['user_id'] ) ? absint( wp_unslash( $_POST['user_id'] ) ) : 0;
$current_user_id = get_current_user_id();
// Recording a wish requires a logged-in actor.
if ( ! $current_user_id ) {
wp_send_json_error( __( 'Authentication required', 'buddypress-birthdays' ) );
break;
}
// A valid target birthday user id must be supplied.
if ( ! $user_id ) {
wp_send_json_error( __( 'Invalid request: missing user_id', 'buddypress-birthdays' ) );
break;
}
if ( $user_id && $current_user_id ) {
$wished_users = get_user_meta( $current_user_id, 'bb_birthday_wished_users', true );
if ( ! is_array( $wished_users ) ) {
$wished_users = array();
}
$today = wp_date( 'Y-m-d' );
if ( ! isset( $wished_users[ $today ] ) ) {
$wished_users[ $today ] = array();
}
if ( ! in_array( $user_id, array_map( 'absint', $wished_users[ $today ] ), true ) ) {
$wished_users[ $today ][] = $user_id;
update_user_meta( $current_user_id, 'bb_birthday_wished_users', $wished_users );
}
wp_send_json_success( array( 'message' => __( 'Wish recorded', 'buddypress-birthdays' ) ) );
}
wp_send_json_error( __( 'Authentication required', 'buddypress-birthdays' ) );
break;
default:
wp_send_json_error( __( 'Invalid action', 'buddypress-birthdays' ) );
}
}
add_action( 'wp_ajax_bb_birthdays_action', 'bb_birthdays_ajax_handler' );
add_action( 'wp_ajax_nopriv_bb_birthdays_action', 'bb_birthdays_ajax_handler' );
/**
* Clean up old wished users data (runs daily)
*/
function bb_cleanup_old_wishes() {
global $wpdb;
// Remove wish data older than 7 days.
$old_date = wp_date( 'Y-m-d', strtotime( '-7 days' ) );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- One-time cleanup query, caching not beneficial.
$users = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, meta_value FROM {$wpdb->usermeta} WHERE meta_key = %s", 'bb_birthday_wished_users' ) );
foreach ( $users as $user ) {
$wished_data = maybe_unserialize( $user->meta_value );
if ( is_array( $wished_data ) ) {
$cleaned_data = array();
foreach ( $wished_data as $date => $user_ids ) {
if ( $date >= $old_date ) {
$cleaned_data[ $date ] = $user_ids;
}
}
if ( empty( $cleaned_data ) ) {
delete_user_meta( $user->user_id, 'bb_birthday_wished_users' );
} else {
update_user_meta( $user->user_id, 'bb_birthday_wished_users', $cleaned_data );
}
}
}
}
// Schedule daily cleanup.
if ( ! wp_next_scheduled( 'bb_cleanup_old_wishes' ) ) {
wp_schedule_event( time(), 'daily', 'bb_cleanup_old_wishes' );
}
add_action( 'bb_cleanup_old_wishes', 'bb_cleanup_old_wishes' );
/**
* Clear all birthday widget caches.
*
* This function clears object cache for the birthday widget
* to ensure the widget displays fresh data.
*
* @since 2.0.0
*/
function bb_clear_birthday_caches() {
// Clear object cache for birthday cache group.
if ( function_exists( 'wp_cache_flush_group' ) ) {
wp_cache_flush_group( 'bp_birthdays' );
}
// Note: We no longer use transients for birthday caching.
// Object cache is lighter weight and more efficient for large sites.
}
/**
* Clear birthday caches when xprofile field is updated.
*
* @param BP_XProfile_ProfileData $field_data The field data object.
*/
function bb_clear_cache_on_xprofile_update( $field_data ) {
bb_clear_birthday_caches();
}
add_action( 'xprofile_data_after_save', 'bb_clear_cache_on_xprofile_update' );
/**
* Clear birthday caches when a friendship is accepted.
*
* @param int $id Friendship ID.
* @param int $initiator_user_id User ID of the initiator.
* @param int $friend_user_id User ID of the friend.
*/
function bb_clear_cache_on_friendship_accepted( $id, $initiator_user_id, $friend_user_id ) {
bb_clear_birthday_caches();
}
add_action( 'friends_friendship_accepted', 'bb_clear_cache_on_friendship_accepted', 10, 3 );
/**
* Clear birthday caches when a friendship is deleted.
*
* @param int $id Friendship ID.
* @param int $initiator_user_id User ID of the initiator.
* @param int $friend_user_id User ID of the friend.
*/
function bb_clear_cache_on_friendship_deleted( $id, $initiator_user_id, $friend_user_id ) {
bb_clear_birthday_caches();
}
add_action( 'friends_friendship_deleted', 'bb_clear_cache_on_friendship_deleted', 10, 3 );
/**
* Clear birthday caches when a friendship is withdrawn.
*
* @param int $friendship_id Friendship ID.
*/
function bb_clear_cache_on_friendship_withdrawn( $friendship_id ) {
bb_clear_birthday_caches();
}
add_action( 'friends_friendship_withdrawn', 'bb_clear_cache_on_friendship_withdrawn' );
/**
* Clear birthday caches when a user is deleted.
*
* @param int $user_id The user ID being deleted.
*/
function bb_clear_cache_on_user_deleted( $user_id ) {
bb_clear_birthday_caches();
}
add_action( 'delete_user', 'bb_clear_cache_on_user_deleted' );
add_action( 'wpmu_delete_user', 'bb_clear_cache_on_user_deleted' );
/**
* Clear birthday caches when a user is registered.
*
* @param int $user_id The user ID being registered.
*/
function bb_clear_cache_on_user_registered( $user_id ) {
bb_clear_birthday_caches();
}
add_action( 'user_register', 'bb_clear_cache_on_user_registered' );
/**
* Clear birthday caches when follow/unfollow happens (BP Follow plugin).
*
* @param BP_Follow $follow The follow object.
*/
function bb_clear_cache_on_follow_change( $follow ) {
bb_clear_birthday_caches();
}
add_action( 'bp_follow_start_following', 'bb_clear_cache_on_follow_change' );
add_action( 'bp_follow_stop_following', 'bb_clear_cache_on_follow_change' );
/**
* Clear birthday caches daily via cron to ensure fresh data.
*/
function bb_daily_cache_clear() {
bb_clear_birthday_caches();
}
add_action( 'bb_cleanup_old_wishes', 'bb_daily_cache_clear' );
/**
* Render the per-user birthday privacy opt-out on the member's
* BuddyPress "Settings > General" screen.
*
* GDPR: birthday month/day is personal data. This gives every member a
* self-service switch to hide their birthday from every plugin surface
* (widget, shortcode, activity, notifications and greeting emails). The
* enforcement lives in BP_Birthdays_Helpers::is_user_opted_out().
*
* @since 2.5.0
*/
function bb_render_member_privacy_field() {
if ( ! is_user_logged_in() || ! function_exists( 'bp_displayed_user_id' ) || ! class_exists( 'BP_Birthdays_Helpers' ) ) {
return;
}
$user_id = bp_displayed_user_id();
if ( ! $user_id ) {
$user_id = get_current_user_id();
}
$is_hidden = ( 'yes' === get_user_meta( $user_id, BP_Birthdays_Helpers::OPTOUT_META_KEY, true ) );
?>
<label for="bb-birthday-hidden" class="bb-birthday-privacy-label">
<input type="checkbox" name="bb_birthday_hidden" id="bb-birthday-hidden" value="yes" <?php checked( $is_hidden ); ?> />
<?php esc_html_e( 'Hide my birthday everywhere on this site (widgets, activity, notifications and emails).', 'buddypress-birthdays' ); ?>
</label>
<?php
}
add_action( 'bp_core_general_settings_before_submit', 'bb_render_member_privacy_field' );
/**
* Persist the member's birthday privacy opt-out when the BuddyPress
* "Settings > General" form is saved.
*
* Fires after BuddyPress has already verified the bp_settings_general nonce and
* saved the core fields; the nonce is re-checked here for defence in depth.
*
* @since 2.5.0
*/
function bb_save_member_privacy_field() {
if ( ! function_exists( 'bp_displayed_user_id' ) || ! class_exists( 'BP_Birthdays_Helpers' ) ) {
return;
}
$nonce = isset( $_POST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) : '';
if ( ! wp_verify_nonce( $nonce, 'bp_settings_general' ) ) {
return;
}
$user_id = bp_displayed_user_id();
if ( ! $user_id ) {
return;
}
$hide = ( isset( $_POST['bb_birthday_hidden'] ) && 'yes' === sanitize_text_field( wp_unslash( $_POST['bb_birthday_hidden'] ) ) );
if ( $hide ) {
update_user_meta( $user_id, BP_Birthdays_Helpers::OPTOUT_META_KEY, 'yes' );
} else {
delete_user_meta( $user_id, BP_Birthdays_Helpers::OPTOUT_META_KEY );
}
// A visibility change must invalidate the cached birthday lists.
bb_clear_birthday_caches();
}
add_action( 'bp_core_general_settings_after_save', 'bb_save_member_privacy_field' );