-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
981 lines (826 loc) · 24.9 KB
/
Copy pathfunctions.php
File metadata and controls
981 lines (826 loc) · 24.9 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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
<?php
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
/**
* CB Is Get Request
*
* Checks if the current request is a GET request
*
* @since 1.0.0
* @return bool True if GET request, false otherwise
*
*/
function cb_is_get_request() {
return (bool) ( 'GET' === strtoupper( $_SERVER['REQUEST_METHOD'] ) );
}
/**
* CB Is Post Request
*
* Checks if the current request is a POST request
*
* @since 1.0.0
* @return bool True if POST request, false otherwise
*/
function cb_is_post_request() {
return (bool) ( 'POST' === strtoupper( $_SERVER['REQUEST_METHOD'] ) );
}
/**
* CB Is Patch Request
*
* Checks if the current request is a PATCH request
*
* @return bool True if PATCH request, false otherwise
*
* @package ConfettiBits
* @since 2.3.0
*/
function cb_is_patch_request() {
return (bool) ( 'PATCH' === strtoupper( $_SERVER['REQUEST_METHOD'] ) );
}
/**
* CB Is Delete Request
*
* Checks if the current request is a DELETE request
*
* @return bool True if DELETE request, false otherwise
*
* @package ConfettiBits
* @since 2.3.0
*/
function cb_is_delete_request() {
return (bool) ( 'DELETE' === strtoupper( $_SERVER['REQUEST_METHOD'] ) );
}
if ( ! function_exists( 'confetti_bits_admin_enqueue_script' ) ) {
function confetti_bits_admin_enqueue_script() {
wp_enqueue_style( 'confetti-bits-admin-css', plugin_dir_url( __FILE__ ) . 'style.css' );
}
add_action( 'admin_enqueue_scripts', 'confetti_bits_admin_enqueue_script' );
}
/**
* Initializes our notifications class.
*
* So we can get those sweet, sweet noties.
*
* @package ConfettiBits\Core
* @since 1.3.0
*/
function cb_core_notifications_init() {
if ( class_exists( 'CB_Notifications_Component' ) ) {
CB_Notifications_Component::instance();
}
}
add_action( 'cb_init', 'cb_core_notifications_init', 10 );
/**
* Returns form markup that allows privileged users to manually
* input the birthday and anniversary dates for other users.
*
* @param object WP_User The current instance of WP_User
* @package ConfettiBits\Core
* @since 3.0.0
*/
function cb_user_birthday_anniversary_fields( $user ) {
if( !current_user_can('add_users') ) {
return;
}
?>
<h3>Birthday & Work Anniversary</h3>
<table class="form-table">
<tr>
<th><label for="cb_birthday">Birthday</label></th>
<td>
<input type="date" class="regular-text" name="cb_birthday" required value="<?php echo esc_attr( xprofile_get_field_data( 51, $user->ID, 'comma' ) ); ?>" id="cb_birthday" /><br />
<span class="description"></span>
</td>
</tr>
<tr>
<th><label for="cb_anniversary">Work Anniversary</label></th>
<td>
<input type="date" class="regular-text" name="cb_anniversary" required value="<?php echo esc_attr( xprofile_get_field_data( 52, $user->ID, 'comma' ) ); ?>" id="cb_anniversary" /><br />
<span class="description"></span>
</td>
</tr>
</table>
<?php
}
add_action( 'user_new_form', 'cb_user_birthday_anniversary_fields' );
function cb_save_user_birthday_anniversary_fields($user_id, $notify) {
if( !current_user_can('add_users')
) {
return false;
}
$c = !empty( $_POST['cb_birthday'] ) ? date( 'Y-m-d H:i:s', strtotime( $_POST['cb_birthday'] ) ) : null;
$d = !empty( $_POST['cb_anniversary']) ? date( 'Y-m-d H:i:s', strtotime( $_POST['cb_anniversary'] ) ) : null;
xprofile_set_field_data( 51, $user_id, $c );
xprofile_set_field_data( 52, $user_id, $d );
}
add_action( 'edit_user_created_user', 'cb_save_user_birthday_anniversary_fields', 10, 2 );
/**
* CB Core Set Reset Date Globals
*
* Sets a few internal globals using the DateTimeImmutable class
* so that we can reference these spending/earning cycles throughout
* the app without running these calculations all the time.
*
* A breakdown of what this does:
*
* - There are two cycles: an earning cycle and a spending cycle
*
* - The reset date refers to the earning cycle. That is when
* users start over with a zero'd out bank of confetti bits.
* So the earning cycle "ends" on the reset date.
*
* - The earning cycle "starts" one year prior to that, on the
* same date. Please don't set it to February 29th, I did not
* account for that when I built this forsaken system.
*
* - The spending cycle is offset by 1 month after the earning
* cycle. So the spending cycle "ends" 1 month after the earning
* cycle does. That means that the spending cycle "starts" one month
* after the earning cycle does as well.
*
* - There are situations where a user may want to look back
* at a previous cycle, so we account for those here as well.
* We only need the dates that those cycles started, because
* they ended when the current cycles started.
*
* @package ConfettiBits\Core
* @since 2.3.0
*
*/
function cb_core_set_reset_date_globals() {
$cb = Confetti_Bits();
$reset_date = get_option('cb_reset_date');
if ( !$reset_date ) {
return;
}
$date = new DateTimeImmutable($reset_date);
$today = new DateTimeImmutable();
if ( $today > $date ) {
$date = cb_core_auto_reset();
}
$cb->earn_start = $date->modify('-1 year')->format('Y-m-d H:i:s');
$cb->earn_end = $date->format('Y-m-d H:i:s');
$cb->spend_start = $date->modify('-1 year + 1 month')->format('Y-m-d H:i:s');
$cb->spend_end = $date->modify('+ 1 month')->format('Y-m-d H:i:s');
$cb->prev_earn_start = $date->modify('-2 years')->format('Y-m-d H:i:s');
$cb->prev_spend_start = $date->modify('-2 years + 1 month')->format('Y-m-d H:i:s');
}
add_action( 'cb_setup_globals', 'cb_core_set_reset_date_globals' );
/**
* Sets the amount for spot bonuses in our core class for easy access.
*
* Make sure to set this value in the DB via admin settings, or else
* we may never experience the bliss of automation.
*
* @package ConfettiBits\Core
* @since 3.0.0
*/
function cb_core_set_spot_bonus_global() {
$cb = Confetti_Bits();
$spot_bonus_amount = get_option('cb_core_spot_bonus_amount');
if ( !$spot_bonus_amount ) {
return;
}
$cb->spot_bonus_amount = intval($spot_bonus_amount);
}
add_action( 'cb_setup_globals', 'cb_core_set_spot_bonus_global' );
/**
* Automatically increments the reset date by 1 year.
*
* @returns DateTimeImmutable The DateTimeImmutable object, with the new reset
* date already locked and loaded.
*
* @package ConfettiBits\Core
* @since 3.0.0
*/
function cb_core_auto_reset() {
$reset_date = get_option('cb_reset_date');
$date = new DateTimeImmutable($reset_date);
$new_reset_date = $date->modify('+1 year')->format('Y-m-d');
update_option( 'cb_reset_date', $new_reset_date );
return new DateTimeImmutable($new_reset_date);
}
/**
* CB Core Current Date
*
* Returns the current date and time in the given
* format. Defaults to MySQL format in the site's
* timezone.
*
*
* @param bool $offset Whether to use the site's
* UTC offset setting. Default true.
*
* @param string $format The desired datetime format.
* Default MySQL - 'Y-m-d H:i:s'
*
* @return string The formatted datetime.
*
* @package ConfettiBits\Core
* @since 2.3.0
*/
function cb_core_current_date( $offset = false, $format = "Y-m-d H:i:s" ) {
$tz = $offset ? wp_timezone() : null;
$date = new DateTimeImmutable("now", $tz);
return $date->format($format);
}
if (!function_exists('str_starts_with')) {
/**
* Str Starts With
*
* PHP 8 Polyfill for str_starts_with
*
* @package ConfettiBits
* @since 2.3.0
*
* @param string $haystack The string to search.
* @param string $needle The substring to search for at the beginning.
*
* @return bool Whether the string starts with the given substring.
*/
function str_starts_with($haystack = '', $needle = '') {
return (string)$needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0;
}
}
if (!function_exists('str_ends_with')) {
/**
* Str Ends With
*
* PHP 8 Polyfill for str_ends_with
*
* @package ConfettiBits
* @since 2.3.0
*
* @param string $haystack The string to search.
* @param string $needle The substring to search for at the end.
*
* @return bool Whether the string ends with the given substring.
*/
function str_ends_with($haystack, $needle) {
return $needle !== '' && substr($haystack, -strlen($needle)) === (string)$needle;
}
}
if (!function_exists('str_contains')) {
/**
* Str Contains
*
* PHP 8 Polyfill for str_contains
*
* @package ConfettiBits
* @since 2.3.0
*
* @param string $haystack The string to search.
* @param string $needle The substring to search for.
*
* @return bool Whether the string contains the given substring.
*/
function str_contains($haystack, $needle) {
return $needle !== '' && mb_strpos($haystack, $needle) !== false;
}
}
/**
* CB Flush Rewrite Rules
*
* Flushes the rewrite rules after we update a
* plugin or theme, so our pages stop disappearing.
*
* @package ConfettiBits
* @since 2.3.0
*/
function cb_flush_rewrite_rules() {
flush_rewrite_rules();
}
add_action( 'after_plugin_or_theme_update', 'cb_flush_rewrite_rules' );
/**
* Gets PATCH data from an HTTP PATCH request.
*
* Retrieves data from a PATCH request and returns it as an
* associative array.
*
* @return array The PATCH request body as an associative array.
*
* @package ConfettiBits
* @since 2.3.0
*/
function cb_get_patch_data() {
if ( !cb_is_patch_request() ) {
return;
}
return json_decode(file_get_contents('php://input'), true);
}
/**
* Gets DELETE data from an HTTP DELETE request.
*
* Retrieves data from a DELETE request and returns it as an
* associative array.
*
* @return array The DELETE request body as an associative array.
*
* @package ConfettiBits\Core
* @since 2.3.0
*/
function cb_get_delete_data() {
if ( !cb_is_delete_request() ) {
return;
}
return json_decode(file_get_contents('php://input'), true);
}
/**
* Gets a list of transactions for nonexistent users.
*
* Get a list of users that are present in the
* confetti_bits_transactions table, but not in the
* wp_users table. Returns an array of transactions where
* the sender or recipient isn't on the platform anymore.
*
* @return array An associative array of transaction data.
*
* @package ConfettiBits\Core
* @since 2.3.0
*/
function cb_core_get_missing_users() {
global $wpdb;
$cb = Confetti_Bits();
$users = "{$wpdb->prefix}users";
$select = 'SELECT t.*';
$from = "FROM {$cb->transactions->table_name} t";
$left_join1 = "LEFT JOIN {$users} u1 ON t.sender_id = u1.id";
$left_join2 = "LEFT JOIN {$users} u2 ON t.recipient_id = u2.id";
$where = "WHERE u1.id IS NULL OR u2.id IS NULL";
$sql = "{$select} {$from} {$left_join1} {$left_join2} {$where}";
return $wpdb->get_results( $sql, "ARRAY_A" );
}
/**
* Gets the user display name.
*
* Attempts to get a display_name for the given user_id. If that
* comes up empty, searches for the first_name. If that also
* comes up empty, searches for the nickname. If there's no
* display name to be found, it gives us a lovely bunch of
* abject nothingness.
*
* @param int $user_id The ID for the user whose name we want.
* Default current user_id.
*
* @return string The display_name on success, empty on failure.
*
* @package ConfettiBits\Core
* @since 2.3.0
*/
function cb_core_get_user_display_name( $user_id = 0 ) {
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
$display_name = get_the_author_meta('display_name', $user_id );
if ( empty( $display_name ) ) {
$display_name = get_the_author_meta( 'first_name', $user_id );
}
if ( empty( $display_name ) ) {
$display_name = get_the_author_meta( 'nickname', $user_id );
}
if ( empty( $display_name ) ) {
$display_name = "Unknown Member";
}
return $display_name;
}
/**
* Returns the email address for the given user.
*
* @param int $user_id The ID of the user. Default current user.
*
* @return string The user's email address, if one exists.
*
* @package ConfettiBits\Core
* @since 3.0.0
*/
function cb_core_get_user_email( $user_id = 0 ) {
if ( $user_id === 0 ) {
$user_id = get_current_user_id();
}
$user = get_userdata($user_id);
return $user->user_email;
}
/**
* Checks if the parameter is a multi-dimensional array.
*
* @param array $arr The array to check.
*
* @return bool Whether the array is multi-dimensional.
*
* @package ConfettiBits\Core
* @since 1.3.0
*/
function cb_core_is_multi_array(array $arr) {
rsort($arr);
return (isset($arr[0]) && is_array($arr[0]));
}
/**
* Sends out a sitewide notice.
*
* Use this to send out non-critical updates that are
* intended to be informative or nice to know, such as
* an upcoming or recent update, new feature, etc.
*
* @package ConfettiBits\Core
* @since 1.2.0
*/
function cb_core_send_sitewide_notice() {
if (
!cb_is_confetti_bits_component() ||
!cb_is_post_request() ||
empty( $_POST['cb_sitewide_notice_heading'] ) ||
empty( $_POST['cb_sitewide_notice_body'] )
) {
return;
}
$redirect_to = Confetti_Bits()->page;
$feedback = ['type' => 'error', 'text' => ''];
$username = cb_core_get_user_display_name(intval($_POST['cb_sitewide_notice_user_id']));
$subject = trim($_POST['cb_sitewide_notice_heading']);
$message = trim($_POST['cb_sitewide_notice_body']) . " - {$username}";
$notice = messages_send_notice($subject, $message);
if ($notice) {
$feedback['type'] = 'success';
$feedback['text'] = 'Sitewide notice was successfully posted.';
} else {
$feedback['text'] = 'Failed to send sitewide notice.';
}
bp_core_add_message($feedback['text'], $feedback['type']);
bp_core_redirect($redirect_to);
}
add_action('cb_actions', 'cb_core_send_sitewide_notice');
/**
* Returns the number of days remaining until the reset date.
*
* @return int The number of days remaining before the reset.
*
* @package ConfettiBits\Core
* @since 3.0.0
*/
function cb_core_get_doomsday_clock() {
$cb = Confetti_Bits();
$current_date = new DateTimeImmutable();
$reset_date = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $cb->earn_end);
$interval = $current_date->diff($reset_date);
return $interval->days;
}
/**
* Washes away the sins of bad actors.
*
* Use this to aggressively scrub input strings.
* I doubt that there are going to be any
* elite hackers playing injecting nonsense
* into this app, but this is good practice
* for other kinds of sanitization that we might
* want to do later.
*
* @param string $input An input string
* @return string A kinder, gentler string.
*
* @package ConfettiBits\Core
* @since 3.0.0
*/
function cb_core_sanitize_string( $input = '' ) {
$input = trim($input);
$input = htmlentities($input, ENT_QUOTES, 'UTF-8');
$input = addslashes($input);
$input = preg_replace(
[ '/\\\\/', '/\0/', '/\n/', '/\r/', '/\'/', '/"/', '/\x1a/' ],
[ '\\\\\\\\', '\\\\0', '\\\\n', '\\\\r', "\\'", '\\"', '\\\\Z' ],
$input
);
return $input;
}
/**
* Injects our own config settings into PHPMailer.
*
* @param PHPMailer $phpmailer A PHPMailer object.
*
* @package ConfettiBits\Core
* @since 3.0.0
*/
function cb_core_smtp_init( $phpmailer ) {
$phpmailer->Host = SMTP_HOST;
$phpmailer->SMTPAuth = SMTP_AUTH;
$phpmailer->Port = SMTP_PORT;
$phpmailer->SMTPSecure = SMTP_SECURE;
$phpmailer->Username = SMTP_USER;
$phpmailer->Password = SMTP_PASS;
$phpmailer->From = SMTP_FROM;
$phpmailer->FromName = SMTP_NAME;
$phpmailer->isSMTP();
}
add_action( 'phpmailer_init', 'cb_core_smtp_init' );
/**
* Adds a menu item for our settings.
*
* @package ConfettiBits\Core
* @subpackage Templates
* @since 3.0.0
*/
function cb_core_admin_menu() {
add_options_page(
'Confetti Bits', // Page title
'Confetti Bits', // Menu title
'manage_options', // Capability required to access the page
'cb-core-admin-settings', // Menu slug
'cb_core_admin_page'// Callback function to display the page content
);
}
add_action('admin_menu', 'cb_core_admin_menu');
/**
* Formats markup for our admin settings page.
*
* @TODO: Add a form here, friend.
*
* @return string The formatted page markup.
*
* @package ConfettiBits\Templates
* @since 3.0.0
*/
function cb_templates_get_admin_page() {
$heading = cb_templates_get_heading('Confetti Bits Settings', 1);
return cb_templates_container([
'classes' => ['wrap'],
'output' => $heading
]);
}
/**
* Outputs markup for our admin settings page.
*
* @see cb_templates_get_admin_page()
*
* @package ConfettiBits\Templates
* @since 3.0.0
*/
function cb_core_admin_page() {
echo '<div class="wrap">';
cb_templates_heading('Confetti Bits Settings', 1);
echo '<form method="POST" action="options.php">';
settings_fields('cb_core_admin_settings');
do_settings_sections('cb-core-admin-settings');
submit_button();
echo '</form>';
echo '</div>';
}
function cb_core_admin_settings_init() {
// Register the settings
register_setting(
'cb_core_admin_settings', // Option group (used in settings_fields)
'cb_reset_date', // Option name (used in the database)
'cb_core_admin_settings_sanitize'// Sanitization callback function
);
register_setting(
'cb_core_admin_settings', // Option group (used in settings_fields)
'cb_core_volunteer_amount', // Option name (used in the database)
'cb_core_admin_settings_sanitize'// Sanitization callback function
);
register_setting(
'cb_core_admin_settings', // Option group (used in settings_fields)
'cb_core_spot_bonus_amount', // Option name (used in the database)
'cb_core_admin_settings_sanitize'// Sanitization callback function
);
// Add a section and fields for your settings
add_settings_section(
'cb_core_admin_settings_section',
'Confetti Bits Core Settings',
'cb_core_admin_settings_section_callback',
'cb-core-admin-settings'
);
add_settings_field(
'cb_reset_date',
'Cycle Reset Date',
'cb_core_admin_reset_date_setting',
'cb-core-admin-settings',
'cb_core_admin_settings_section'
);
add_settings_field(
'cb_core_volunteer_amount',
'Amount for Volunteer Hours',
'cb_core_admin_volunteer_setting',
'cb-core-admin-settings',
'cb_core_admin_settings_section'
);
add_settings_field(
'cb_core_spot_bonus_amount',
'Amount for Spot Bonuses',
'cb_core_admin_spot_bonus_setting',
'cb-core-admin-settings',
'cb_core_admin_settings_section'
);
}
add_action('admin_init', 'cb_core_admin_settings_init');
/**
* Will eventually be used to sanitize user input in the
* admin menu.
*
* @param mixed $input The input passed via post.
* @return string $input The "sanitized" input.
*
* @package ConfettiBits\Core
* @subpackage Templates
* @since 3.0.0
*/
function cb_core_admin_settings_sanitize($input) {
return $input;
}
add_action('admin_init', 'cb_core_admin_settings_sanitize');
/**
* Returns the content for our main settings section.
*
* @return string Content.
*
* @package ConfettiBits\Core
* @subpackage Templates
* @since 3.0.0
*/
function cb_core_admin_settings_section_callback() {
// You can add section description here if needed
echo "
<p>Here is where we house all of our global settings used throughout the app.</p>
<p>This will include things such as the yearly reset date, the amounts for certain actions, and other
settings as we add them.</p>
";
}
/**
* Outputs the setting field for the reset date.
*
* @package Core
* @since 3.0.0
*/
function cb_core_admin_reset_date_setting() {
$option = get_option('cb_reset_date');
$value = !empty($option) ? date('Y-m-d', strtotime($option) ) : '';
echo '<input type="date" name="cb_reset_date" value="' . esc_attr($value) . '" />';
}
/**
* Outputs the setting field for the volunteer amount per hour.
*
* @package ConfettiBits\Core
* @subpackage Templates
* @since 3.0.0
*/
function cb_core_admin_volunteer_setting() {
$option = get_option('cb_core_volunteer_amount');
$value = isset($option) ? intval($option) : '';
echo '<input type="text" name="cb_core_volunteer_amount" value="' . esc_attr($value) . '" />';
}
/**
* Outputs the setting field for the volunteer amount per hour.
*
* @package ConfettiBits\Core
* @subpackage Templates
* @since 3.0.0
*/
function cb_core_admin_spot_bonus_setting() {
$option = get_option('cb_core_spot_bonus_amount');
$value = isset($option) ? intval($option) : '';
echo '<input type="text" name="cb_core_spot_bonus_amount" value="' . esc_attr($value) . '" />';
}
/**
* Replaces our default script tags with modules.
*/
function cb_core_convert_scripts_to_modules($tag, $handle, $src) {
$modules = ['cb_core', 'cb_staffing_admin', 'cb_events_admin', 'cb_events', 'cb_core_modules', 'cb_volunteers', 'cb_settings'];
if ( in_array( $handle, $modules ) ) {
$src = esc_url($src);
$tag = "<script type='module' src='{$src}' defer></script>";
}
return $tag;
}
add_filter('script_loader_tag', 'cb_core_convert_scripts_to_modules', 10, 3);
/**
* Deletes all Confetti Bits data associated with deleted user.
*
* @param int $id The ID of the user that's being deleted.
* @param int $reassign An optional user ID to reassign items to.
* @param WP_User $user The instance of WP_User associated with the deleted user.
*
* @package ConfettiBits\Core
* @since 3.0.0
*/
function cb_core_delete_user_data( $id, $reassign, $user ) {
global $wpdb;
$cb = Confetti_Bits();
if ( ! is_numeric( $id ) ) {
return false;
}
$id = (int) $id;
$user = new WP_User( $id );
if ( ! $user->exists() ) {
return false;
}
if ( null !== $reassign ) {
$reassign = (int) $reassign;
}
if ( null === $reassign ) {
$wpdb->delete($cb->participation->table_name, ['applicant_id' => $id ], ['%d']);
$wpdb->delete($cb->transactions->table_name, ['recipient_id' => $id ], ['%d']);
$wpdb->delete($cb->transactions->table_name, ['sender_id' => $id ], ['%d']);
$wpdb->delete($cb->requests->table_name, ['applicant_id' => $id ], ['%d']);
$wpdb->delete($cb->requests->table_name, ['admin_id' => $id ], ['%d']);
}
}
add_action('delete_user', 'cb_core_delete_user_data', 10, 3);
function cb_core_do_spot_bonuses() {
$spot_bonus = new CB_Transactions_Spot_Bonus();
$transaction = new CB_Transactions_Transaction();
$date = new DateTime('now', new DateTimeZone('UTC'));
$recipient_list = [];
$spot_bonus_get_args = [
'select' => '*',
'where' => [
'date_query' => [
'column' => 'spot_bonus_date',
'before' => [
'day' => $date->format('d'),
'month' => $date->format('m'),
'year' => $date->format('Y'),
],
'after' => [
'day' => $date->format('d'),
'month' => $date->format('m'),
'year' => $date->format('Y') - 1,
],
'inclusive' => true,
],
'transaction_id' => null,
]
];
$bonuses_for_today = $spot_bonus->get_spot_bonuses($spot_bonus_get_args);
if ( ! empty( $bonuses_for_today ) && is_array( $bonuses_for_today ) ) {
foreach ( $bonuses_for_today as $bonus ) {
if ( !is_null( $bonus['transaction_id'] ) ) {
continue;
}
$recipient_id = intval($bonus['recipient_id']);
$sender_id = intval($bonus['sender_id']);
array_push($recipient_list, cb_core_get_user_display_name($recipient_id));
$bonus_date = new DateTime($bonus['spot_bonus_date'], new DateTimeZone('UTC'));
$transaction->item_id = $sender_id;
$transaction->secondary_item_id = $recipient_id;
$transaction->sender_id = $sender_id;
$transaction->recipient_id = $recipient_id;
$transaction->amount = get_option('cb_core_spot_bonus_amount');
$transaction->log_entry = "Spot Bonus winner {$bonus_date->format('m/d/Y')}";
$transaction->date_sent = $date->format('Y-m-d H:i:s');
$transaction->component_name = "transactions";
$transaction->component_action = "cb_transactions_spot_bonus";
$save = $transaction->send_bits();
$spot_bonus->update(['transaction_id' => $save], ['id' => $bonus['id']]);
}
}
$message = !empty($recipient_list) ?
'Here\'s a list of people who got their bits: <ul>' . implode('', array_map(cb_core_listify($recipient_list) ) ) . '</ul>'
: "<p>We didn't find anyone waiting on their bits.</p> <p><b>Hooray!</b></p>";
wp_mail('dustin@celebrationtitlegroup.com', 'Cron Job Report', "<h4>Spot bonuses were sent out!</h4> {$message}", ['Content-Type: text/html; charset=UTF-8']);
}
add_action('cb_core_run_spot_bonuses', 'cb_core_do_spot_bonuses');
function cb_core_listify( $string ) {
return "<li>{$string}</li>";
}
function cb_core_schedule_events() {
if (! wp_next_scheduled ( 'cb_core_run_spot_bonuses' )) {
wp_schedule_event(time(), 'daily', 'cb_core_run_spot_bonuses');
}
}
add_action('cb_actions', 'cb_core_schedule_events');
add_filter( 'cron_schedules', function ( $schedules ) {
$schedules['per_minute'] = array(
'interval' => 60,
'display' => __( 'One Minute' )
);
return $schedules;
} );
function cb_core_utc_to_local( $date_string = '' ) {
$site_tz = wp_timezone();
$date = new DateTime($date_string, new DateTimeZone('UTC'));
$date->setTimezone($site_tz);
return $date;
}
/**
* Adds an ordinal suffix to a given integer.
*
* @param int|string $int An integer.
*
* @return string The same integer, but with a little spice.
*
* @package Core
* @since 3.0.0
*/
function cb_core_ordinal_suffix( $int ) {
$int = intval($int);
if ($int >= 11 && $int <= 13) {
return "{$int}th";
}
if ( $int % 10 === 1) {
return "{$int}st";
}
if ( $int % 10 === 2 ) {
return "{$int}nd";
}
if ( $int % 10 === 3 ) {
return "{$int}rd";
}
return "{$int}th";
}