forked from obenland/wp-approve-user
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwp-approve-user.php
967 lines (810 loc) · 23.9 KB
/
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
<?php
/** wp-approve-user.php
*
* Plugin Name: WP Approve User
* Plugin URI: http://en.wp.obenland.it/wp-approve-user/#utm_source=wordpress&utm_medium=plugin&utm_campaign=wp-approve-user
* Description: Adds action links to user table to approve or unapprove user registrations.
* Version: 2.2.0
* Author: Konstantin Obenland
* Author URI: http://en.wp.obenland.it/#utm_source=wordpress&utm_medium=plugin&utm_campaign=wp-approve-user
* Text Domain: wp-approve-user
* Domain Path: /lang
* License: GPLv2
*/
if ( ! get_option( 'users_can_register' ) ) {
return;
}
if ( ! class_exists( 'Obenland_Wp_Plugins_v300' ) ) {
require_once( 'obenland-wp-plugins.php' );
}
class Obenland_Wp_Approve_User extends Obenland_Wp_Plugins_v300 {
///////////////////////////////////////////////////////////////////////////
// PROPERTIES, PUBLIC
///////////////////////////////////////////////////////////////////////////
/**
*
* @since 1.1.0 - 12.02.2012
* @access public
* @static
*
* @var Obenland_Wp_Approve_User
*/
public static $instance;
///////////////////////////////////////////////////////////////////////////
// PROPERTIES, PROTECTED
///////////////////////////////////////////////////////////////////////////
/**
* The plugin options.
*
*
* @author Konstantin Obenland
* @since 2.0.0 - 31.03.2012
* @access protected
*
* @var array
*/
protected $options;
/**
* Users flagged as unapproved.
*
* @author Konstantin Obenland
* @since 2.2.0 - 30.03.2013
* @access protected
*
* @var array
*/
protected $unapproved_users = array();
///////////////////////////////////////////////////////////////////////////
// METHODS, PUBLIC
///////////////////////////////////////////////////////////////////////////
/**
* Constructor
*
* @author Konstantin Obenland
* @since 1.0 - 29.01.2012
* @access public
*
* @return Obenland_Wp_Approve_User
*/
public function __construct() {
parent::__construct( array(
'textdomain' => 'wp-approve-user',
'plugin_path' => __FILE__,
'donate_link_id' => 'G65Y5CM3HVRNY',
));
self::$instance = $this;
$this->options = wp_parse_args(
get_option( $this->textdomain, array() ),
$this->default_options()
);
if ( is_admin() ) {
$this->unapproved_users = get_users( array(
'meta_key' => 'wp-approve-user',
'meta_value' => false,
) );
}
load_plugin_textdomain( 'wp-approve-user' , false, 'wp-approve-user/lang' );
$this->hook( 'plugins_loaded' );
}
/**
* Approves all existing users.
*
* @author Konstantin Obenland
* @since 1.0 - 29.01.2012
* @access public
* @static
*
* @return void
*/
public function activation() {
$user_ids = get_users( array(
'blog_id' => '',
'fields' => 'ID'
) );
foreach ( $user_ids as $user_id ) {
update_user_meta( $user_id, 'wp-approve-user', true );
update_user_meta( $user_id, 'wp-approve-user-mail-sent', true );
}
}
/**
* Hooks in all the hooks :)
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access public
*
* @return void
*/
public function plugins_loaded() {
$this->hook( 'update_option_users_can_register' );
$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( 'shake_error_codes' );
$this->hook( 'admin_menu' );
$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( '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( 'pre_user_query' );
}
}
/**
* Enqueues the script
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access public
*
* @return void
*/
public function admin_print_scripts_users_php() {
$plugin_data = get_plugin_data( __FILE__, false, false );
$suffix = ( defined( 'SCRIPT_DEBUG' ) AND 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
*
* @return void
*/
public function admin_print_styles_settings_page_wp_approve_user() {
$plugin_data = get_plugin_data( __FILE__, false, false );
$suffix = ( defined( 'SCRIPT_DEBUG' ) AND SCRIPT_DEBUG ) ? '' : '.min';
wp_enqueue_style(
$this->textdomain,
plugins_url( "/css/settings-page{$suffix}.css", __FILE__ ),
array(),
$plugin_data['Version']
);
}
/**
* Re-runs the activation hook when registration is activated.
*
* If the plugin is activated and user registration is disabled, the plugin
* activation hook never gets added, let alone fired. This a secondary
* measure to make sure all existing users are approved on activation.
*
* @author Konstantin Obenland
* @since 2.2.0 - 30.03.2013
* @access public
*
* @param string $old Old settings value.
* @param int $new New settings value.
*
* @return void
*/
public function update_option_users_can_register( $old, $new ) {
if ( $new )
$this->activation();
}
/**
* @author Konstantin Obenland
* @since 2.2.0 - 30.03.2013
* @access public
*
* @param array $views
*
* @return array
*/
public function views_users( $views ) {
if ( $this->unapproved_users ) {
$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';
$views['unapproved'] = sprintf( '<a href="%1$s" class="%2$s">%3$s <span class="count">(%4$s)</span></a>',
add_query_arg( array( 'role' => 'wpau_unapproved' ), $url ),
( isset( $_REQUEST['role'] ) AND 'wpau_unapproved' == $_REQUEST['role'] ) ? 'current' : '',
__( 'Unapproved', 'wp-approve-users' ),
count( $this->unapproved_users )
);
}
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
*
* @return void
*/
public function pre_user_query( $query ) {
if ( 'wpau_unapproved' == $query->query_vars['role'] ) {
unset( $query->query_vars['meta_query'][0] );
$query->set( 'role', '' );
$query->set( 'meta_key', 'wp-approve-user' );
$query->set( 'meta_value', false );
$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
* @param WP_User $user_object
*
* @return array
*/
public function user_row_actions( $actions, $user_object ) {
if ( ( get_current_user_id() != $user_object->ID ) AND current_user_can( 'edit_user', $user_object->ID ) ) {
$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 ( get_user_meta( $user_object->ID, 'wp-approve-user', true ) ) {
$url = wp_nonce_url( add_query_arg( array(
'action' => 'wpau_unapprove',
'user' => $user_object->ID,
), $url ), 'wpau-unapprove-users' );
$actions['wpau-unapprove'] = sprintf( '<a class="submitunapprove" href="%1$s">%2$s</a>', $url, __( 'Unapprove', 'wp-approve-user' ) );
} else {
$url = wp_nonce_url( add_query_arg( array(
'action' => 'wpau_approve',
'user' => $user_object->ID,
'role' => isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '',
), $url ), 'wpau-approve-users' );
$actions['wpau-approve'] = sprintf( '<a class="submitapprove" href="%1$s">%2$s</a>', $url, __( 'Approve', '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
*
* @return WP_User|WP_Error
*/
public function wp_authenticate_user( $userdata ) {
if ( ! is_wp_error( $userdata ) AND ! get_user_meta( $userdata->ID, 'wp-approve-user', true ) AND $userdata->user_email != get_bloginfo( 'admin_email' ) ) {
$userdata = new WP_Error(
'wpau_confirmation_error',
__('<strong>ERROR:</strong> Your account has to be confirmed by an administrator before you can login.', 'wp-approve-user')
);
}
return $userdata;
}
/**
* Updates user_meta to approve user when created by an Administrator.
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access public
*
* @return void
*/
public function user_register( $id ) {
update_user_meta( $id, 'wp-approve-user', current_user_can( 'create_users' ) );
}
/**
* Updates user_meta to approve user.
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access public
*
* @return void
*/
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
*
* @return void
*/
public function admin_action_wpau_bulk_approve() {
check_admin_referer( 'bulk-users' );
$this->approve();
}
/**
* Updates user_meta to unapprove user.
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access public
*
* @return void
*/
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
*
* @return void
*/
public function admin_action_wpau_bulk_unapprove() {
check_admin_referer( 'bulk-users' );
$this->unapprove();
}
/**
* Adds the update message to the admin notices queue
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access public
*
* @return void
*/
public function admin_action_wpau_update() {
switch( $_REQUEST['update'] ) {
case 'wpau-approved':
$message = _n( 'User approved.', '%d users approved.', $_REQUEST['count'], 'wp-approve-user' );
break;
case 'wpau-unapproved':
$message = _n( 'User unapproved.', '%d users unapproved.', $_REQUEST['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, $_REQUEST['count'] ),
'updated'
);
$this->hook( 'all_admin_notices' );
}
// Prevent other admin action handlers from trying to handle our action.
$_REQUEST['action'] = -1;
}
/**
* Adds our error code to make the login form shake :)
*
* @author Konstantin Obenland
* @since 1.0 - 29.01.2012
* @access public
*
* @return void
*/
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
*
* @return void
*/
public function admin_menu() {
if ( current_user_can( 'list_users' ) AND version_compare( get_bloginfo( 'version' ), '3.2', '>=' ) ) {
global $menu;
foreach ( $menu as $key => $menu_item ) {
if ( array_search( 'users.php', $menu_item ) ) {
// No need for number formatting, count() always returns an integer
$awaiting_mod = count( $this->unapproved_users );
$menu[ $key ][0] .= " <span class='update-plugins count-{$awaiting_mod}'><span class='plugin-count'>{$awaiting_mod}</span></span>";
break; // Bail on success
}
}
}
add_options_page(
__( 'Approve User', 'wp-approve-user' ), // Page Title
__( '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
*
* return void
*/
public function admin_init() {
register_setting(
$this->textdomain,
'wp-approve-user',
array( &$this, 'sanitize' )
);
add_settings_section(
$this->textdomain,
__( 'Email contents', 'wp-approve-user' ),
array( &$this, 'section_description_cb' ),
$this->textdomain
);
add_settings_field(
'wp-approve-user[send-approve-email]',
__( '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]',
__( '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]',
__( '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]',
__( '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
*
* @return void
*/
public function settings_page() {
?>
<div class="wrap">
<?php screen_icon(); ?>
<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 do_action( 'obenland_side_info_column' ); ?>
</div>
</div>
</div>
</div>
</div>
<?php
}
/**
* Prints the section description.
*
* @author Konstantin Obenland
* @since 2.0.0 - 31.03.2012
* @access public
*
* @return void
*/
public function section_description_cb() {
$tags = array( 'USERNAME', 'BLOG_TITLE', 'BLOG_URL', 'LOGINLINK' );
if ( is_multisite() ) {
$tags[] = 'SITE_NAME';
}
printf(
_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 ) )
);
}
/**
* Populates the setting field.
*
* @author Konstantin Obenland
* @since 2.0.0 - 31.03.2012
* @access public
*
* @return void
*/
public function checkbox_cb( $option ) {
$option = (object) $option; ?>
<label for="<?php echo sanitize_title_with_dashes( $option->name ); ?>">
<input type="checkbox" name="wp-approve-user[<?php echo esc_attr( $option->name ); ?>]" id="<?php echo 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
*
* @return void
*/
public function textarea_cb( $option ) {
$option = (object) $option;
?>
<textarea id="<?php echo 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
*
* @return array The sanitized settings
*/
public function sanitize( $input ) {
$output = array();
$output['wpau-send-approve-email'] = (bool) isset( $input['wpau-send-approve-email'] );
$output['wpau-send-unapprove-email'] = (bool) isset( $input['wpau-send-unapprove-email'] );
$output['wpau-approve-email'] = isset( $input['wpau-approve-email'] ) ? trim( $input['wpau-approve-email'] ) : '';
$output['wpau-unapprove-email'] = isset( $input['wpau-unapprove-email'] ) ? trim( $input['wpau-unapprove-email'] ) : '';
return $output;
}
/**
* Sends the approval email.
*
* @author Konstantin Obenland
* @since 2.0.0 - 31.03.2012
* @access public
*
* @param int $user_id
*
* @return void
*/
public function wpau_approve( $user_id ) {
// Check user meta if mail has been sent already.
if ( $this->options['wpau-send-approve-email'] AND ! 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,
sprintf( _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
*
* @return void
*/
public function delete_user( $user_id ) {
if ( $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,
sprintf( _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
*
* @return void
*/
public function all_admin_notices() {
settings_errors( $this->textdomain );
}
///////////////////////////////////////////////////////////////////////////
// METHODS, PROTECTED
///////////////////////////////////////////////////////////////////////////
/**
* Updates user_meta to approve user.
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access protected
*
* @return void
*/
protected function approve() {
list( $userids, $url ) = $this->check_user();
foreach ( (array) $userids as $id ) {
$id = (int) $id;
if ( ! current_user_can( 'edit_user', $id ) )
wp_die( __( 'You can’t edit that user.' ) );
update_user_meta( $id, 'wp-approve-user', true );
do_action( 'wpau_approve', $id );
}
wp_redirect( add_query_arg( array(
'action' => 'wpau_update',
'update' => 'wpau-approved',
'count' => count( $userids ),
'role' => isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '',
), $url ) );
exit();
}
/**
* Updates user_meta to unapprove user.
*
* @author Konstantin Obenland
* @since 1.1 - 12.02.2012
* @access protected
*
* @return void
*/
protected function unapprove() {
list( $userids, $url ) = $this->check_user();
foreach ( (array) $userids as $id ) {
$id = (int) $id;
if ( ! current_user_can( 'edit_user', $id ) )
wp_die( __( 'You can’t edit that user.' ) );
update_user_meta( $id, 'wp-approve-user', false );
do_action( 'wpau_unapprove', $id );
}
wp_redirect( add_query_arg( array(
'action' => 'wpau_update',
'update' => 'wpau-unapproved',
'count' => count( $userids ),
), $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() {
$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 ( empty( $_REQUEST['users'] ) AND empty( $_REQUEST['user'] ) ) {
wp_redirect( $url );
exit();
}
if ( ! current_user_can( 'promote_users' ) ) {
wp_die( __( 'You can’t unapprove users.', 'wp-approve-user' ) );
}
$userids = ( empty( $_REQUEST['users'] ) ) ? array( intval( $_REQUEST['user'] ) ) : (array) $_REQUEST['users'];
$userids = array_diff( $userids, array( get_user_by( 'email', get_bloginfo( 'admin_email' ) )->ID ) );
return array( $userids, $url );
}
/**
* Replaces all the placeholders with their content.
*
* @author Konstantin Obenland
* @since 2.0.0 - 15.03.2012
* @access protected
*
* @param string $message
* @param WP_User $user
*
* @return string
*/
protected function populate_message( $message, $user ) {
$title = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$message = str_replace( 'BLOG_TITLE', $title, $message );
$message = str_replace( 'BLOG_URL', home_url(), $message );
$message = str_replace( 'LOGINLINK', wp_login_url(), $message );
$message = str_replace( 'USERNAME', $user->user_nicename, $message );
if ( is_multisite() ) {
global $current_site;
$message = str_replace( 'SITE_NAME', $current_site->site_name, $message );
}
return $message;
}
/**
* Returns the default options.
*
* @author Konstantin Obenland
* @since 2.0.0 - 15.03.2012
* @access protected
*
* @return array
*/
protected function default_options() {
$options = array(
'wpau-send-approve-email' => false,
'wpau-approve-email' => 'Hi USERNAME,
Your registration for BLOG_TITLE has now been approved.
You can log in, using your username and password that you created when registering for our website, at the following URL: LOGINLINK
If you have any questions, or problems, then please do not hesitate to contact us.
Name,
Company,
Contact details',
'wpau-send-unapprove-email' => false,
'wpau-unapprove-email' => ''
);
return apply_filters( 'wpau_default_options', $options );
}
} // End of class Obenland_Wp_Approve_User
new Obenland_Wp_Approve_User;
register_activation_hook( __FILE__, array(
Obenland_Wp_Approve_User::$instance,
'activation'
));
/* End of file wp-approve-user.php */
/* Location: ./wp-content/plugins/wp-approve-user/wp-approve-user.php */