-
Notifications
You must be signed in to change notification settings - Fork 9
/
class-obenland-wp-approve-user.php
1192 lines (1045 loc) · 33 KB
/
class-obenland-wp-approve-user.php
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
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Obenland_Wp_Approve_User file.
*
* @package wp-approve-user
*/
/**
* Class Obenland_Wp_Approve_User.
*/
class Obenland_Wp_Approve_User extends Obenland_Wp_Plugins_V5 {
/**
* Class instance.
*
* @since 1.1.0 - 12.02.2012
* @access public
* @static
*
* @var Obenland_Wp_Approve_User
*/
public static $instance;
/**
* The plugin options.
*
* @author Konstantin Obenland
* @since 2.0.0 - 31.03.2012
* @access protected
*
* @var array
*/
protected $options;
/**
* Users flagged as pending.
*
* @since 12
*
* @var int
*/
protected $pending_count = 0;
/**
* Users flagged as unapproved.
*
* @author Konstantin Obenland
* @since 2.2.0 - 30.03.2013
* @access protected
*
* @var array
*/
protected $unapproved_users = array();
/**
* Number of unapproved users.
*
* @since 12
*
* @var int
*/
protected $unapproved_count = 0;
/**
* Constructor
*
* @author Konstantin Obenland
* @since 1.0 - 29.01.2012
* @access public
*/
public function __construct() {
parent::__construct(
array(
'textdomain' => 'wp-approve-user',
'plugin_path' => __DIR__ . '/wp-approve-user.php',
'donate_link_id' => 'G65Y5CM3HVRNY',
)
);
self::$instance = $this;
$this->options = wp_parse_args(
get_option( $this->textdomain, array() ),
$this->default_options()
);
if ( is_admin() ) {
/**
* Get all users where wp-approve-user meta value is false or doesn't exist.
*/
$args = array(
'fields' => 'ID',
'meta_key' => 'wp-approve-user', //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => 'pending', //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
);
if ( is_multisite() ) {
$args['blog_id'] = is_network_admin() ? 0 : get_current_blog_id();
}
$this->pending_count = count( get_users( $args ) );
$args['meta_value'] = 'unapproved'; //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
$this->unapproved_count = count( get_users( $args ) );
}
load_plugin_textdomain( 'wp-approve-user', false, 'wp-approve-user/lang' );
$this->hook( 'plugins_loaded' );
}
/**
* Singleton.
*
* @return Obenland_Wp_Approve_User
*/
public static function get_instance() {
if ( ! static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Hooks in all the hooks :)
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access public
*/
public function plugins_loaded() {
$this->hook( 'user_row_actions' );
$this->hook( 'ms_user_row_actions', 'user_row_actions' );
$this->hook( 'wp_authenticate_user' );
$this->hook( 'user_register' );
$this->hook( 'register_new_user', 0 );
$this->hook( 'wp_login_errors' );
$this->hook( 'shake_error_codes' );
$this->hook( 'admin_print_scripts-users.php' );
$this->hook( 'admin_print_scripts-site-users.php', 'admin_print_scripts_users_php' );
$this->hook( 'admin_print_styles-settings_page_wp-approve-user' );
$this->hook( 'load-users.php', 'map_action2' );
$this->hook( 'load-site-users.php', 'map_action2' );
$this->hook( 'admin_action_wpau_approve' );
$this->hook( 'admin_action_wpau_bulk_approve' );
$this->hook( 'admin_action_wpau_unapprove' );
$this->hook( 'admin_action_wpau_bulk_unapprove' );
$this->hook( 'admin_action_wpau_update' );
$this->hook( 'wpau_approve' );
$this->hook( 'delete_user' );
$this->hook( 'admin_init' );
if ( is_admin() ) {
$this->hook( 'views_users' );
$this->hook( 'views_users-network', 'views_users' );
$this->hook( 'views_site-users-network', 'views_users' );
$this->hook( 'pre_user_query' );
}
if ( is_multisite() ) {
$this->hook( 'network_admin_menu', 'admin_menu' );
} else {
$this->hook( 'admin_menu' );
}
}
/**
* Enqueues the script
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access public
*/
public function admin_print_scripts_users_php() {
$plugin_data = get_plugin_data( __FILE__, false, false );
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_enqueue_script(
$this->textdomain,
plugins_url( "/js/{$this->textdomain}{$suffix}.js", __FILE__ ),
array( 'jquery' ),
$plugin_data['Version'],
true
);
wp_localize_script(
$this->textdomain,
'wp_approve_user',
array(
'approve' => __( 'Approve', 'wp-approve-user' ),
'unapprove' => __( 'Unapprove', 'wp-approve-user' ),
)
);
}
/**
* Enqueues the style on the settings page
*
* @author Konstantin Obenland
* @since 2.0.0 - 10.04.2012
* @access public
*/
public function admin_print_styles_settings_page_wp_approve_user() {
$plugin_data = get_plugin_data( __FILE__, false, false );
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_enqueue_style(
$this->textdomain,
plugins_url( "/css/settings-page{$suffix}.css", __FILE__ ),
array(),
$plugin_data['Version']
);
}
/**
* Adds a link to a list view of unapproved users.
*
* @author Konstantin Obenland
* @since 2.2.0 - 30.03.2013
* @access public
*
* @param array $views List of registered user list views.
* @return array
*/
public function views_users( $views ) {
// phpcs:ignore WordPress.Security.NonceVerification
$site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
$url = 'site-users-network' === get_current_screen()->id ? add_query_arg( array( 'id' => $site_id ), 'site-users.php' ) : 'users.php';
if ( $this->pending_count ) {
$views['pending'] = sprintf(
'<a href="%1$s" class="%2$s">%3$s <span class="count">(%4$s)</span></a>',
esc_url( add_query_arg( array( 'role' => 'wpau_pending' ), $url ) ),
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
'wpau_pending' === $this->get_role() ? 'current' : '',
esc_html__( 'Pending', 'wp-approve-users' ),
$this->pending_count
);
}
if ( $this->unapproved_count ) {
$views['unapproved'] = sprintf(
'<a href="%1$s" class="%2$s">%3$s <span class="count">(%4$s)</span></a>',
esc_url( add_query_arg( array( 'role' => 'wpau_unapproved' ), $url ) ),
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
'wpau_unapproved' === $this->get_role() ? 'current' : '',
esc_html__( 'Unapproved', 'wp-approve-users' ),
$this->unapproved_count
);
}
return $views;
}
/**
* Resets the user query to handle request for unapproved users only.
*
* @author Konstantin Obenland
* @since 2.2.0 - 30.03.2013
* @access public
*
* @param WP_User_Query $query User query object.
*/
public function pre_user_query( $query ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
$role = empty( $query->query_vars['role'] ) && isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : $query->query_vars['role'];
if ( 'wpau_pending' === $role ) {
unset( $query->query_vars['meta_query'] );
$query->query_vars['role'] = '';
$query->query_vars['meta_key'] = 'wp-approve-user'; //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
$query->query_vars['meta_value'] = 'pending'; //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
remove_filter( 'pre_user_query', array( $this, 'pre_user_query' ) );
$query->prepare_query();
}
if ( 'wpau_unapproved' === $role ) {
unset( $query->query_vars['meta_query'] );
$query->query_vars['role'] = '';
$query->query_vars['meta_key'] = 'wp-approve-user'; //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
$query->query_vars['meta_value'] = 'unapproved'; //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
remove_filter( 'pre_user_query', array( $this, 'pre_user_query' ) );
$query->prepare_query();
}
}
/**
* Adds the plugin's row actions to the existing ones.
*
* @author Konstantin Obenland
* @since 1.0 - 29.01.2012
* @access public
*
* @param array $actions User action links.
* @param WP_User $user_object User object.
* @return array
*/
public function user_row_actions( $actions, $user_object ) {
if ( get_current_user_id() !== $user_object->ID && current_user_can( 'edit_user', $user_object->ID ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
$url = 'site-users-network' === get_current_screen()->id ? add_query_arg( array( 'id' => $site_id ), 'site-users.php' ) : 'users.php';
$status = get_user_meta( $user_object->ID, 'wp-approve-user', true );
if ( 'approved' !== $status ) {
$unapprove_url = wp_nonce_url(
add_query_arg(
array(
'action' => 'wpau_approve',
'user' => $user_object->ID,
'role' => $this->get_role(),
),
$url
),
'wpau-approve-users'
);
$actions['wpau-approve'] = sprintf( '<a class="submitapprove" href="%1$s">%2$s</a>', esc_url( $unapprove_url ), esc_html__( 'Approve', 'wp-approve-user' ) );
}
if ( 'unapproved' !== $status ) {
$approve_url = wp_nonce_url(
add_query_arg(
array(
'action' => 'wpau_unapprove',
'user' => $user_object->ID,
'role' => $this->get_role(),
),
$url
),
'wpau-unapprove-users'
);
$actions['wpau-unapprove'] = sprintf( '<a class="submitunapprove" href="%1$s">%2$s</a>', esc_url( $approve_url ), esc_html__( 'Unapprove', 'wp-approve-user' ) );
}
}
return $actions;
}
/**
* Checks whether the user is approved. Throws error if not.
*
* @author Konstantin Obenland
* @since 1.0 - 29.01.2012
* @access public
*
* @param WP_User|WP_Error $userdata User object.
* @return WP_User|WP_Error
*/
public function wp_authenticate_user( $userdata ) {
if ( is_wp_error( $userdata ) ) {
return $userdata;
}
if ( get_bloginfo( 'admin_email' ) === $userdata->user_email ) {
return $userdata;
}
if ( is_super_admin( $userdata->ID ) ) {
return $userdata;
}
if ( 'approved' === get_user_meta( $userdata->ID, 'wp-approve-user', true ) ) {
return $userdata;
}
return new WP_Error(
'wpau_confirmation_error',
wp_kses_post( __( '<strong>ERROR:</strong> Your account has to be confirmed by an administrator before you can log in.', 'wp-approve-user' ) )
);
}
/**
* Updates user_meta to approve user when created by an Administrator.
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access public
*
* @param int $id User ID.
*/
public function user_register( $id ) {
$status = current_user_can( 'create_users' ) ? 'approved' : 'pending';
update_user_meta( $id, 'wp-approve-user', $status );
update_user_meta( $id, 'wp-approve-user-new-registration', true );
}
/**
* Fires after a new user registration has been recorded.
*
* Prevents WordPress to send the new user notification email, if the user has to be approved first.
* Still notifies the admin about the new user registration.
*
* @author Konstantin Obenland
* @since 6 - 04.03.2019
* @access public
*
* @param int $user_id ID of the newly registered user.
*/
public function register_new_user( $user_id ) {
if ( 'pending' === get_user_meta( $user_id, 'wp-approve-user', true ) ) {
remove_action( 'register_new_user', 'wp_send_new_user_notifications' );
add_action( 'register_new_user', 'wp_new_user_notification' );
}
}
/**
* Calls actions that depend on the `action` parameter.
*
* @author Konstantin Obenland
* @since 3 - 21.12.2017
* @access public
*/
public function map_action2() {
// phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
if ( ! empty( $_REQUEST['action2'] ) && false !== stripos( $_REQUEST['action2'], 'wpau_' ) ) {
do_action( "admin_action_{$_REQUEST['action2']}" );
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
wp_add_inline_style( 'list-tables', '.wp-list-table.users tbody th, .wp-list-table.users tbody td { box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.1); } #the-list .submitapprove { color:#007017; } #the-list .submitunapprove { color:#996800; }' );
}
/**
* Updates user_meta to approve user.
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access public
*/
public function admin_action_wpau_approve() {
check_admin_referer( 'wpau-approve-users' );
$this->approve();
}
/**
* Bulkupdates user_meta to approve user.
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access public
*/
public function admin_action_wpau_bulk_approve() {
$action = 'users-network' === get_current_screen()->id ? 'bulk-users-network' : 'bulk-users';
check_admin_referer( $action );
$this->set_up_role_context();
$this->approve();
}
/**
* Updates user_meta to unapprove user.
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access public
*/
public function admin_action_wpau_unapprove() {
check_admin_referer( 'wpau-unapprove-users' );
$this->unapprove();
}
/**
* Bulkupdates user_meta to unapprove user.
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access public
*/
public function admin_action_wpau_bulk_unapprove() {
$action = 'users-network' === get_current_screen()->id ? 'bulk-users-network' : 'bulk-users';
check_admin_referer( $action );
$this->set_up_role_context();
$this->unapprove();
}
/**
* Adds the update message to the admin notices queue.
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access public
*/
public function admin_action_wpau_update() {
// phpcs:disable WordPress.Security.ValidatedSanitizedInput, WordPress.Security.NonceVerification.Recommended
if ( empty( $_REQUEST['update'] ) ) {
return;
}
$count = absint( $_REQUEST['count'] );
switch ( $_REQUEST['update'] ) {
case 'wpau-approved':
/* translators: Number of users. */
$message = esc_html( _n( '%d User approved.', '%d users approved.', $count, 'wp-approve-user' ) );
break;
case 'wpau-unapproved':
/* translators: Number of users. */
$message = esc_html( _n( '%d User unapproved.', '%d users unapproved.', $count, 'wp-approve-user' ) );
break;
default:
$message = apply_filters( 'wpau_update_message_handler', '', $_REQUEST['update'] );
}
if ( isset( $message ) ) {
add_settings_error(
$this->textdomain,
esc_attr( $_REQUEST['update'] ),
sprintf( $message, $count ),
'updated'
);
$this->hook( 'all_admin_notices' );
}
// Prevent other admin action handlers from trying to handle our action.
$_REQUEST['action'] = -1;
// phpcs:enable WordPress.Security.ValidatedSanitizedInput, WordPress.Security.NonceVerification.Recommended
}
/**
* Filters the login page errors.
*
* @author Konstantin Obenland
* @since 6 - 04.03.2019
* @access public
*
* @param WP_Error $errors WP Error object.
* @return WP_Error
*/
public function wp_login_errors( $errors ) {
if ( in_array( 'registered', $errors->get_error_codes(), true ) ) {
$message = __( 'Registration complete. You will receive an email once your registration was confirmed by an administrator.', 'wp-approve-user' );
$errors->remove( 'registered' );
$errors->add( 'registered', $message, 'message' );
}
return $errors;
}
/**
* Adds our error code to make the login form shake :)
*
* @author Konstantin Obenland
* @since 1.0 - 29.01.2012
* @access public
*
* @param array $shake_error_codes Error codes that trigger form shaking.
* @return array Shake error codes
*/
public function shake_error_codes( $shake_error_codes ) {
$shake_error_codes[] = 'wpau_confirmation_error';
return $shake_error_codes;
}
/**
* Enhances the User menu item to reflect the amount of unapproved users.
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access public
*/
public function admin_menu() {
if ( current_user_can( 'list_users' ) && version_compare( get_bloginfo( 'version' ), '3.2', '>=' ) ) {
global $menu;
foreach ( $menu as $key => $menu_item ) {
if ( array_search( 'users.php', $menu_item, true ) ) {
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$menu[ $key ][0] .= sprintf( ' <span class="update-plugins count-%1$s"><span class="plugin-count">%1$s</span></span>', $this->pending_count );
break; // Bail on success.
}
}
}
add_submenu_page(
is_multisite() ? 'settings.php' : 'options-general.php',
esc_html__( 'Approve User', 'wp-approve-user' ), // Page Title.
esc_html__( 'Approve User', 'wp-approve-user' ), // Menu Title.
'promote_users', // Capability.
$this->textdomain, // Menu Slug.
array( $this, 'settings_page' ) // Function.
);
}
/**
* Registers the plugins' settings.
*
* @author Konstantin Obenland
* @since 1.0.0 - 02.03.2012
* @access public
*/
public function admin_init() {
register_setting(
$this->textdomain,
'wp-approve-user',
array( &$this, 'sanitize' )
);
add_settings_section(
$this->textdomain,
esc_html__( 'Email contents', 'wp-approve-user' ),
array( $this, 'section_description_cb' ),
$this->textdomain
);
add_settings_field(
'wp-approve-user[send-approve-email]',
esc_html__( 'Send Approve Email', 'wp-approve-user' ),
array( $this, 'checkbox_cb' ),
$this->textdomain,
$this->textdomain,
array(
'name' => 'wpau-send-approve-email',
'description' => __( 'Send email on approval.', 'wp-approve-user' ),
)
);
add_settings_field(
'wp-approve-user[approve-email]',
esc_html__( 'Approve Email', 'wp-approve-user' ),
array( $this, 'textarea_cb' ),
$this->textdomain,
$this->textdomain,
array(
'label_for' => 'wpau-approve-email',
'name' => 'wpau-approve-email',
'setting' => 'wpau-send-approve-email',
)
);
add_settings_field(
'wp-approve-user[send-unapprove-email]',
esc_html__( 'Send Unapprove Email', 'wp-approve-user' ),
array( $this, 'checkbox_cb' ),
$this->textdomain,
$this->textdomain,
array(
'name' => 'wpau-send-unapprove-email',
'description' => __( 'Send email on unapproval.', 'wp-approve-user' ),
)
);
add_settings_field(
'wp-approve-user[unapprove-email]',
esc_html__( 'Unapprove Email', 'wp-approve-user' ),
array( $this, 'textarea_cb' ),
$this->textdomain,
$this->textdomain,
array(
'label_for' => 'wpau-unapprove-email',
'name' => 'wpau-unapprove-email',
'setting' => 'wpau-send-unapprove-email',
)
);
}
/**
* Displays the options page.
*
* @author Konstantin Obenland
* @since 2.0.0 - 31.03.2012
* @access public
*/
public function settings_page() {
?>
<div class="wrap">
<h2><?php esc_html_e( 'Approve User Settings', 'wp-approve-user' ); ?></h2>
<div id="poststuff">
<div id="post-body" class="obenland-wp columns-2">
<div id="post-body-content">
<form method="post" action="options.php">
<?php
settings_fields( $this->textdomain );
do_settings_sections( $this->textdomain );
submit_button();
?>
</form>
</div>
<div id="postbox-container-1">
<div id="side-info-column">
<?php
$this->donate_box();
$this->feed_box();
?>
</div>
</div>
</div>
</div>
</div>
<?php
}
/**
* Prints the section description.
*
* @author Konstantin Obenland
* @since 2.0.0 - 31.03.2012
* @access public
*/
public function section_description_cb() {
$tags = array( 'USERNAME', 'BLOG_TITLE', 'BLOG_URL', 'LOGINLINK' );
if ( is_multisite() ) {
$tags[] = 'SITE_NAME';
}
printf(
/* translators: Placeholders. */
esc_html_x( 'To take advantage of dynamic data, you can use the following placeholders: %s. Username will be the user login in most cases.', 'Placeholders', 'wp-approve-user' ),
sprintf( '<code>%s</code>', implode( '</code>, <code>', $tags ) ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
);
}
/**
* Populates the setting field.
*
* @author Konstantin Obenland
* @since 2.0.0 - 31.03.2012
* @access public
*
* @param array $option Settings option.
*/
public function checkbox_cb( $option ) {
$option = (object) $option;
?>
<label for="<?php echo esc_attr( sanitize_title_with_dashes( $option->name ) ); ?>">
<input type="checkbox" name="wp-approve-user[<?php echo esc_attr( $option->name ); ?>]" id="<?php echo esc_attr( sanitize_title_with_dashes( $option->name ) ); ?>" value="1" <?php checked( $this->options[ $option->name ] ); ?> />
<?php echo esc_html( $option->description ); ?>
</label><br />
<?php
}
/**
* Populates the setting field.
*
* @author Konstantin Obenland
* @since 2.0.0 - 31.03.2012
* @access public
*
* @param array $option Settings option.
*/
public function textarea_cb( $option ) {
$option = (object) $option;
?>
<textarea id="<?php echo esc_attr( sanitize_title_with_dashes( $option->name ) ); ?>" class="large-text code" name="wp-approve-user[<?php echo esc_attr( $option->name ); ?>]" rows="10" cols="50" ><?php echo esc_textarea( $this->options[ $option->name ] ); ?></textarea>
<?php
}
/**
* Sanitizes the settings input.
*
* @author Konstantin Obenland
* @since 2.0.0 - 31.03.2012
* @access public
*
* @param array $input Form input.
*
* @return array The sanitized settings
*/
public function sanitize( $input ) {
return array(
'wpau-send-approve-email' => isset( $input['wpau-send-approve-email'] ),
'wpau-send-unapprove-email' => isset( $input['wpau-send-unapprove-email'] ),
'wpau-approve-email' => isset( $input['wpau-approve-email'] ) ? trim( $input['wpau-approve-email'] ) : '',
'wpau-unapprove-email' => isset( $input['wpau-unapprove-email'] ) ? trim( $input['wpau-unapprove-email'] ) : '',
);
}
/**
* Sends the approval email.
*
* @author Konstantin Obenland
* @since 2.0.0 - 31.03.2012
* @access public
*
* @param int $user_id User ID.
*/
public function wpau_approve( $user_id ) {
if ( get_user_meta( $user_id, 'wp-approve-user-new-registration', true ) ) {
wp_new_user_notification( $user_id, null, 'user' );
delete_user_meta( $user_id, 'wp-approve-user-new-registration' );
}
// Check user meta if mail has been sent already.
if ( $this->options['wpau-send-approve-email'] && ! get_user_meta( $user_id, 'wp-approve-user-mail-sent', true ) ) {
$user = new WP_User( $user_id );
$blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
// Send mail.
$sent = wp_mail(
$user->user_email,
/* translators: Blog name. */
sprintf( esc_html_x( '[%s] Registration approved', 'Blogname', 'wp-approve-user' ), $blogname ),
$this->populate_message( $this->options['wpau-approve-email'], $user )
);
if ( $sent ) {
update_user_meta( $user_id, 'wp-approve-user-mail-sent', true );
}
}
}
/**
* Sends the rejection email.
*
* @author Konstantin Obenland
* @since 2.0.0 - 31.03.2012
* @access public
*
* @param int $user_id User ID.
*/
public function delete_user( $user_id ) {
$is_new_registration = get_user_meta( $user_id, 'wp-approve-user-new-registration', true );
$is_unapproved = 'unapproved' === get_user_meta( $user_id, 'wp-approve-user', true );
if ( $is_new_registration && $is_unapproved && $this->options['wpau-send-unapprove-email'] ) {
$user = new WP_User( $user_id );
$blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
// Send mail.
wp_mail(
$user->user_email,
/* translators: Blog name. */
sprintf( esc_html_x( '[%s] Registration unapproved', 'Blogname', 'wp-approve-user' ), $blogname ),
$this->populate_message( $this->options['wpau-unapprove-email'], $user )
);
// No need to delete user_meta, since this user will be GONE.
}
}
/**
* Display all messages registered to this plugin.
*
* @author Konstantin Obenland
* @since 2.0.0 - 30.03.2012
* @access public
*/
public function all_admin_notices() {
settings_errors( $this->textdomain );
}
/**
* Updates user_meta to approve user.
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access protected
*/
protected function approve() {
list( $user_ids, $url ) = $this->check_user();
foreach ( (array) $user_ids as $id ) {
$id = (int) $id;
if ( ! current_user_can( 'edit_user', $id ) ) {
wp_die(
esc_html__( 'You can’t edit that user.' ),
'',
array(
'back_link' => true,
)
);
}
update_user_meta( $id, 'wp-approve-user', 'approved' );
/**
* Fires after a user has been approved.
*
* @since 1.1.0
*
* @param int $id User ID.
*/
do_action( 'wpau_approve', $id );
}
$role = $this->get_role();
$count = count( $user_ids );
$has_remaining = $this->has_remaining_users( $role, $count );
$query_args = array(
'action' => 'wpau_update',
'update' => 'wpau-approved',
'count' => $count,
);
if ( $has_remaining ) {
$query_args['role'] = $role;
}
wp_safe_redirect( add_query_arg( $query_args, $url ) );
exit();
}
/**
* Updates user_meta to unapprove user.
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access protected
*/
protected function unapprove() {
list( $user_ids, $url ) = $this->check_user();
foreach ( (array) $user_ids as $id ) {
$id = (int) $id;
if ( ! current_user_can( 'edit_user', $id ) ) {
wp_die(
esc_html__( 'You can’t edit that user.' ),
'',
array(
'back_link' => true,
)
);
}
update_user_meta( $id, 'wp-approve-user', 'unapproved' );
WP_Session_Tokens::get_instance( $id )->destroy_all();
/**
* Fires after a user has been unapproved.
*
* @since 1.1.0
*
* @param int $id User ID.
*/
do_action( 'wpau_unapprove', $id );
}
$role = $this->get_role();
$count = count( $user_ids );
$has_remaining = $this->has_remaining_users( $role, $count );
$query_args = array(
'action' => 'wpau_update',
'update' => 'wpau-unapproved',
'count' => $count,
);
// Special case: If someone unapproves all unapproved users, we want to stay on the unapproved list.
if ( $has_remaining || 'wpau_unapproved' === $role ) {
$query_args['role'] = $role;
}
wp_safe_redirect( add_query_arg( $query_args, $url ) );
exit();
}
/**
* Checks permissions and assembles User IDs.
*
* @author Konstantin Obenland
* @since 2.0.0 - 15.03.2012
* @access protected
*
* @return array User IDs and URL
*/
protected function check_user() {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$screen_id = get_current_screen()->id;
$users_key = 'user';
if ( false !== stripos( current_action(), 'wpau_bulk_' ) ) {
$users_key = 'users-network' === $screen_id ? 'allusers' : 'users';
}
$site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
$url = 'site-users-network' === $screen_id ? add_query_arg( array( 'id' => $site_id ), 'site-users.php' ) : 'users.php';
if ( empty( $_REQUEST[ $users_key ] ) ) {
wp_safe_redirect( $url );
exit();
}
if ( ! current_user_can( 'promote_users' ) ) {
wp_die(
esc_html__( 'You can’t unapprove users.', 'wp-approve-user' ),
'',
array(
'back_link' => true,
)
);
}