PluginProbe
WP Approve User / 3
WP Approve User v3
13 12 trunk 1.0 1.1.0 1.1.1 10 11 2.0.0 2.1.0 2.1.1 2.2.0 2.2.1 2.2.2 2.2.3 2.4.0 3 4 5 6 7 8 9
wp-approve-user / wp-approve-user.php

wp-approve-user.php in WP Approve User 3, at wp-approve-user.php

1,092 lines 28.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: WP Approve User
4 * Plugin URI: http://en.wp.obenland.it/wp-approve-user/#utm_source=wordpress&utm_medium=plugin&utm_campaign=wp-approve-user
5 * Description: Adds action links to user table to approve or unapprove user registrations.
6 * Version: 3
7 * Author: Konstantin Obenland
8 * Author URI: http://en.wp.obenland.it/#utm_source=wordpress&utm_medium=plugin&utm_campaign=wp-approve-user
9 * Text Domain: wp-approve-user
10 * Domain Path: /lang
11 * License: GPLv2
12 *
13 * @package WP Approve User
14 */
15
16 if ( ! get_option( 'users_can_register' ) ) {
17 /**
18 * Whitelists all users.
19 *
20 * @author Konstantin Obenland
21 * @since 2.2.0 - 30.03.2013
22 *
23 * @param bool $new New option value.
24 */
25 function wpau_whitelist_users( $new ) {
26 if ( $new ) {
27 $user_ids = get_users( array(
28 'blog_id' => '',
29 'fields' => 'ID',
30 ) );
31
32 foreach ( $user_ids as $user_id ) {
33 update_user_meta( $user_id, 'wp-approve-user', true );
34 update_user_meta( $user_id, 'wp-approve-user-mail-sent', true );
35 }
36 }
37 }
38 add_filter( 'pre_update_option_users_can_register', 'wpau_whitelist_users' );
39 return;
40 }
41
42
43 if ( ! class_exists( 'Obenland_Wp_Plugins_V4' ) ) {
44 require_once 'obenland-wp-plugins.php';
45 }
46
47 /**
48 * Class Obenland_Wp_Approve_User.
49 */
50 class Obenland_Wp_Approve_User extends Obenland_Wp_Plugins_V4 {
51
52 /**
53 * Class instance.
54 *
55 * @since 1.1.0 - 12.02.2012
56 * @access public
57 * @static
58 *
59 * @var Obenland_Wp_Approve_User
60 */
61 public static $instance;
62
63 /**
64 * The plugin options.
65 *
66 * @author Konstantin Obenland
67 * @since 2.0.0 - 31.03.2012
68 * @access protected
69 *
70 * @var array
71 */
72 protected $options;
73
74 /**
75 * Users flagged as unapproved.
76 *
77 * @author Konstantin Obenland
78 * @since 2.2.0 - 30.03.2013
79 * @access protected
80 *
81 * @var array
82 */
83 protected $unapproved_users = array();
84
85
86 /**
87 * Constructor
88 *
89 * @author Konstantin Obenland
90 * @since 1.0 - 29.01.2012
91 * @access public
92 *
93 * @return Obenland_Wp_Approve_User
94 */
95 public function __construct() {
96 parent::__construct( array(
97 'textdomain' => 'wp-approve-user',
98 'plugin_path' => __FILE__,
99 'donate_link_id' => 'G65Y5CM3HVRNY',
100 ) );
101
102 self::$instance = $this;
103 $this->options = wp_parse_args(
104 get_option( $this->textdomain, array() ),
105 $this->default_options()
106 );
107
108 if ( is_admin() ) {
109 $this->unapproved_users = get_users( array(
110 'meta_key' => 'wp-approve-user',
111 'meta_value' => false,
112 ) );
113 }
114
115 load_plugin_textdomain( 'wp-approve-user', false, 'wp-approve-user/lang' );
116
117 $this->hook( 'plugins_loaded' );
118 }
119
120
121 /**
122 * Approves all existing users.
123 *
124 * @author Konstantin Obenland
125 * @since 1.0 - 29.01.2012
126 * @access public
127 * @static
128 *
129 * @return void
130 */
131 public function activation() {
132 $user_ids = get_users( array(
133 'blog_id' => '',
134 'fields' => 'ID',
135 ) );
136
137 foreach ( $user_ids as $user_id ) {
138 update_user_meta( $user_id, 'wp-approve-user', true );
139 update_user_meta( $user_id, 'wp-approve-user-mail-sent', true );
140 }
141 }
142
143
144 /**
145 * Hooks in all the hooks :)
146 *
147 * @author Konstantin Obenland
148 * @since 1.1 - 12.02.2012
149 * @access public
150 *
151 * @return void
152 */
153 public function plugins_loaded() {
154 $this->hook( 'user_row_actions' );
155 $this->hook( 'ms_user_row_actions', 'user_row_actions' );
156 $this->hook( 'wp_authenticate_user' );
157 $this->hook( 'user_register' );
158 $this->hook( 'shake_error_codes' );
159 $this->hook( 'admin_menu' );
160 $this->hook( 'network_admin_menu', 'admin_menu' );
161
162 $this->hook( 'admin_print_scripts-users.php' );
163 $this->hook( 'admin_print_scripts-site-users.php', 'admin_print_scripts_users_php' );
164 $this->hook( 'admin_print_styles-settings_page_wp-approve-user' );
165
166 $this->hook( 'load-users.php', 'map_action2' );
167 $this->hook( 'load-site-users.php', 'map_action2' );
168 $this->hook( 'admin_action_wpau_approve' );
169 $this->hook( 'admin_action_wpau_bulk_approve' );
170 $this->hook( 'admin_action_wpau_unapprove' );
171 $this->hook( 'admin_action_wpau_bulk_unapprove' );
172 $this->hook( 'admin_action_wpau_update' );
173
174 $this->hook( 'wpau_approve' );
175 $this->hook( 'delete_user' );
176 $this->hook( 'admin_init' );
177
178 if ( is_admin() ) {
179 $this->hook( 'views_users' );
180 $this->hook( 'views_users-network', 'views_users' );
181 $this->hook( 'views_site-users-network', 'views_users' );
182 $this->hook( 'pre_user_query' );
183 }
184 }
185
186
187 /**
188 * Enqueues the script
189 *
190 * @author Konstantin Obenland
191 * @since 1.1 - 12.02.2012
192 * @access public
193 *
194 * @return void
195 */
196 public function admin_print_scripts_users_php() {
197 $plugin_data = get_plugin_data( __FILE__, false, false );
198 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
199
200 wp_enqueue_script(
201 $this->textdomain,
202 plugins_url( "/js/{$this->textdomain}{$suffix}.js", __FILE__ ),
203 array( 'jquery' ),
204 $plugin_data['Version'],
205 true
206 );
207
208 wp_localize_script(
209 $this->textdomain,
210 'wp_approve_user',
211 array(
212 'approve' => __( 'Approve', 'wp-approve-user' ),
213 'unapprove' => __( 'Unapprove', 'wp-approve-user' ),
214 )
215 );
216 }
217
218
219 /**
220 * Enqueues the style on the settings page
221 *
222 * @author Konstantin Obenland
223 * @since 2.0.0 - 10.04.2012
224 * @access public
225 *
226 * @return void
227 */
228 public function admin_print_styles_settings_page_wp_approve_user() {
229 $plugin_data = get_plugin_data( __FILE__, false, false );
230 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
231
232 wp_enqueue_style(
233 $this->textdomain,
234 plugins_url( "/css/settings-page{$suffix}.css", __FILE__ ),
235 array(),
236 $plugin_data['Version']
237 );
238 }
239
240
241 /**
242 * Adds a link to a list view of unapproved users.
243 *
244 * @author Konstantin Obenland
245 * @since 2.2.0 - 30.03.2013
246 * @access public
247 *
248 * @param array $views List of registered user list views.
249 *
250 * @return array
251 */
252 public function views_users( $views ) {
253 if ( $this->unapproved_users ) {
254 // phpcs:ignore WordPress.CSRF.NonceVerification.NoNonceVerification
255 $site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
256 $url = 'site-users-network' === get_current_screen()->id ? add_query_arg( array( 'id' => $site_id ), 'site-users.php' ) : 'users.php';
257
258 $views['unapproved'] = sprintf(
259 '<a href="%1$s" class="%2$s">%3$s <span class="count">(%4$s)</span></a>',
260 esc_url( add_query_arg( array( 'role' => 'wpau_unapproved' ), $url ) ),
261 // phpcs:ignore WordPress.CSRF.NonceVerification.NoNonceVerification
262 'wpau_unapproved' === $this->get_role() ? 'current' : '',
263 esc_html__( 'Unapproved', 'wp-approve-users' ),
264 count( $this->unapproved_users )
265 );
266 }
267
268 return $views;
269 }
270
271
272 /**
273 * Resets the user query to handle request for unapproved users only.
274 *
275 * @author Konstantin Obenland
276 * @since 2.2.0 - 30.03.2013
277 * @access public
278 *
279 * @param WP_User_Query $query User query object.
280 *
281 * @return void
282 */
283 public function pre_user_query( $query ) {
284 // phpcs:ignore WordPress.CSRF.NonceVerification.NoNonceVerification, WordPress.VIP.ValidatedSanitizedInput
285 $role = empty( $query->query_vars['role'] ) && isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : $query->query_vars['role'];
286
287 if ( 'wpau_unapproved' === $role ) {
288 unset( $query->query_vars['meta_query'] );
289 $query->query_vars['role'] = '';
290 $query->query_vars['meta_key'] = 'wp-approve-user';
291 $query->query_vars['meta_value'] = false;
292
293 remove_filter( 'pre_user_query', array( $this, 'pre_user_query' ) );
294 $query->prepare_query();
295 }
296 }
297
298
299 /**
300 * Adds the plugin's row actions to the existing ones.
301 *
302 * @author Konstantin Obenland
303 * @since 1.0 - 29.01.2012
304 * @access public
305 *
306 * @param array $actions User action links.
307 * @param WP_User $user_object User object.
308 *
309 * @return array
310 */
311 public function user_row_actions( $actions, $user_object ) {
312 if ( get_current_user_id() !== $user_object->ID && current_user_can( 'edit_user', $user_object->ID ) ) {
313 // phpcs:ignore WordPress.CSRF.NonceVerification.NoNonceVerification
314 $site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
315 $url = 'site-users-network' === get_current_screen()->id ? add_query_arg( array( 'id' => $site_id ), 'site-users.php' ) : 'users.php';
316
317 if ( get_user_meta( $user_object->ID, 'wp-approve-user', true ) ) {
318 $url = wp_nonce_url( add_query_arg( array(
319 'action' => 'wpau_unapprove',
320 'user' => $user_object->ID,
321 ), $url ), 'wpau-unapprove-users' );
322
323 $actions['wpau-unapprove'] = sprintf( '<a class="submitunapprove" href="%1$s">%2$s</a>', esc_url( $url ), esc_html__( 'Unapprove', 'wp-approve-user' ) );
324
325 } else {
326 $url = wp_nonce_url( add_query_arg( array(
327 'action' => 'wpau_approve',
328 'user' => $user_object->ID,
329 'role' => $this->get_role(),
330 ), $url ), 'wpau-approve-users' );
331
332 $actions['wpau-approve'] = sprintf( '<a class="submitapprove" href="%1$s">%2$s</a>', esc_url( $url ), esc_html__( 'Approve', 'wp-approve-user' ) );
333 }
334 }
335
336 return $actions;
337 }
338
339
340 /**
341 * Checks whether the user is approved. Throws error if not.
342 *
343 * @author Konstantin Obenland
344 * @since 1.0 - 29.01.2012
345 * @access public
346 *
347 * @param WP_User|WP_Error $userdata User object.
348 *
349 * @return WP_User|WP_Error
350 */
351 public function wp_authenticate_user( $userdata ) {
352 if ( ! is_wp_error( $userdata ) && ! get_user_meta( $userdata->ID, 'wp-approve-user', true ) && get_bloginfo( 'admin_email' ) !== $userdata->user_email ) {
353 $userdata = new WP_Error(
354 'wpau_confirmation_error',
355 wp_kses_post( __( '<strong>ERROR:</strong> Your account has to be confirmed by an administrator before you can login.', 'wp-approve-user' ) )
356 );
357 }
358
359 return $userdata;
360 }
361
362
363 /**
364 * Updates user_meta to approve user when created by an Administrator.
365 *
366 * @author Konstantin Obenland
367 * @since 1.1 - 12.02.2012
368 * @access public
369 *
370 * @param int $id User ID.
371 *
372 * @return void
373 */
374 public function user_register( $id ) {
375 update_user_meta( $id, 'wp-approve-user', current_user_can( 'create_users' ) );
376 }
377
378 /**
379 * Calls actions that depend on the `action` parameter.
380 *
381 * @author Konstantin Obenland
382 * @since 3 - 21.12.2017
383 * @access public
384 */
385 public function map_action2() {
386 // phpcs:disable WordPress.CSRF.NonceVerification.NoNonceVerification, WordPress.VIP.ValidatedSanitizedInput
387
388 if ( ! empty( $_REQUEST['action2'] ) && false !== stripos( $_REQUEST['action2'], 'wpau_' ) ) {
389 do_action( "admin_action_{$_REQUEST['action2']}" );
390 }
391
392 // phpcs:enable WordPress.CSRF.NonceVerification.NoNonceVerification, WordPress.VIP.ValidatedSanitizedInput
393 }
394
395
396 /**
397 * Updates user_meta to approve user.
398 *
399 * @author Konstantin Obenland
400 * @since 1.1 - 12.02.2012
401 * @access public
402 *
403 * @return void
404 */
405 public function admin_action_wpau_approve() {
406 check_admin_referer( 'wpau-approve-users' );
407 $this->approve();
408 }
409
410
411 /**
412 * Bulkupdates user_meta to approve user.
413 *
414 * @author Konstantin Obenland
415 * @since 1.1 - 12.02.2012
416 * @access public
417 *
418 * @return void
419 */
420 public function admin_action_wpau_bulk_approve() {
421 $action = 'users-network' === get_current_screen()->id ? 'bulk-users-network' : 'bulk-users';
422 check_admin_referer( $action );
423
424 $this->set_up_role_context();
425 $this->approve();
426 }
427
428
429 /**
430 * Updates user_meta to unapprove user.
431 *
432 * @author Konstantin Obenland
433 * @since 1.1 - 12.02.2012
434 * @access public
435 *
436 * @return void
437 */
438 public function admin_action_wpau_unapprove() {
439 check_admin_referer( 'wpau-unapprove-users' );
440 $this->unapprove();
441 }
442
443
444 /**
445 * Bulkupdates user_meta to unapprove user.
446 *
447 * @author Konstantin Obenland
448 * @since 1.1 - 12.02.2012
449 * @access public
450 *
451 * @return void
452 */
453 public function admin_action_wpau_bulk_unapprove() {
454 $action = 'users-network' === get_current_screen()->id ? 'bulk-users-network' : 'bulk-users';
455 check_admin_referer( $action );
456
457 $this->set_up_role_context();
458 $this->unapprove();
459 }
460
461
462 /**
463 * Adds the update message to the admin notices queue.
464 *
465 * @author Konstantin Obenland
466 * @since 1.1 - 12.02.2012
467 * @access public
468 *
469 * @return void
470 */
471 public function admin_action_wpau_update() {
472 // phpcs:disable WordPress.VIP.ValidatedSanitizedInput, WordPress.CSRF.NonceVerification.NoNonceVerification
473 $count = absint( $_REQUEST['count'] );
474
475 switch ( $_REQUEST['update'] ) {
476 case 'wpau-approved':
477 /* translators: Number of users. */
478 $message = esc_html( _n( '%d User approved.', '%d users approved.', $count, 'wp-approve-user' ) );
479 break;
480
481 case 'wpau-unapproved':
482 /* translators: Number of users. */
483 $message = esc_html( _n( '%d User unapproved.', '%d users unapproved.', $count, 'wp-approve-user' ) );
484 break;
485
486 default:
487 $message = apply_filters( 'wpau_update_message_handler', '', $_REQUEST['update'] );
488 }
489
490 if ( isset( $message ) ) {
491 add_settings_error(
492 $this->textdomain,
493 esc_attr( $_REQUEST['update'] ),
494 sprintf( $message, $count ),
495 'updated'
496 );
497
498 $this->hook( 'all_admin_notices' );
499 }
500
501 // Prevent other admin action handlers from trying to handle our action.
502 $_REQUEST['action'] = -1;
503
504 // phpcs:enable WordPress.VIP.ValidatedSanitizedInput, WordPress.CSRF.NonceVerification.NoNonceVerification
505 }
506
507
508 /**
509 * Adds our error code to make the login form shake :)
510 *
511 * @author Konstantin Obenland
512 * @since 1.0 - 29.01.2012
513 * @access public
514 *
515 * @param array $shake_error_codes Error codes that trigger form shaking.
516 *
517 * @return array Shake error codes
518 */
519 public function shake_error_codes( $shake_error_codes ) {
520 $shake_error_codes[] = 'wpau_confirmation_error';
521
522 return $shake_error_codes;
523 }
524
525
526 /**
527 * Enhances the User menu item to reflect the amount of unapproved users.
528 *
529 * @author Konstantin Obenland
530 * @since 1.1 - 12.02.2012
531 * @access public
532 *
533 * @return void
534 */
535 public function admin_menu() {
536 if ( current_user_can( 'list_users' ) && version_compare( get_bloginfo( 'version' ), '3.2', '>=' ) ) {
537 global $menu;
538
539 foreach ( $menu as $key => $menu_item ) {
540 if ( array_search( 'users.php', $menu_item, true ) ) {
541
542 // No need for number formatting, count() always returns an integer.
543 $awaiting_mod = count( $this->unapproved_users );
544
545 // phpcs:ignore WordPress.Variables.GlobalVariables.OverrideProhibited
546 $menu[ $key ][0] .= " <span class='update-plugins count-{$awaiting_mod}'><span class='plugin-count'>{$awaiting_mod}</span></span>";
547
548 break; // Bail on success.
549 }
550 }
551 }
552
553 add_options_page(
554 esc_html__( 'Approve User', 'wp-approve-user' ), // Page Title.
555 esc_html__( 'Approve User', 'wp-approve-user' ), // Menu Title.
556 'promote_users', // Capability.
557 $this->textdomain, // Menu Slug.
558 array( $this, 'settings_page' ) // Function.
559 );
560 }
561
562
563 /**
564 * Registers the plugins' settings.
565 *
566 * @author Konstantin Obenland
567 * @since 1.0.0 - 02.03.2012
568 * @access public
569 *
570 * return void
571 */
572 public function admin_init() {
573 register_setting(
574 $this->textdomain,
575 'wp-approve-user',
576 array( &$this, 'sanitize' )
577 );
578
579 add_settings_section(
580 $this->textdomain,
581 esc_html__( 'Email contents', 'wp-approve-user' ),
582 array( $this, 'section_description_cb' ),
583 $this->textdomain
584 );
585
586 add_settings_field(
587 'wp-approve-user[send-approve-email]',
588 esc_html__( 'Send Approve Email', 'wp-approve-user' ),
589 array( $this, 'checkbox_cb' ),
590 $this->textdomain,
591 $this->textdomain,
592 array(
593 'name' => 'wpau-send-approve-email',
594 'description' => __( 'Send email on approval.', 'wp-approve-user' ),
595 )
596 );
597
598 add_settings_field(
599 'wp-approve-user[approve-email]',
600 esc_html__( 'Approve Email', 'wp-approve-user' ),
601 array( $this, 'textarea_cb' ),
602 $this->textdomain,
603 $this->textdomain,
604 array(
605 'label_for' => 'wpau-approve-email',
606 'name' => 'wpau-approve-email',
607 'setting' => 'wpau-send-approve-email',
608 )
609 );
610
611 add_settings_field(
612 'wp-approve-user[send-unapprove-email]',
613 esc_html__( 'Send Unapprove Email', 'wp-approve-user' ),
614 array( $this, 'checkbox_cb' ),
615 $this->textdomain,
616 $this->textdomain,
617 array(
618 'name' => 'wpau-send-unapprove-email',
619 'description' => __( 'Send email on unapproval.', 'wp-approve-user' ),
620 )
621 );
622 add_settings_field(
623 'wp-approve-user[unapprove-email]',
624 esc_html__( 'Unapprove Email', 'wp-approve-user' ),
625 array( $this, 'textarea_cb' ),
626 $this->textdomain,
627 $this->textdomain,
628 array(
629 'label_for' => 'wpau-unapprove-email',
630 'name' => 'wpau-unapprove-email',
631 'setting' => 'wpau-send-unapprove-email',
632 )
633 );
634 }
635
636
637 /**
638 * Displays the options page.
639 *
640 * @author Konstantin Obenland
641 * @since 2.0.0 - 31.03.2012
642 * @access public
643 *
644 * @return void
645 */
646 public function settings_page() {
647 ?>
648 <div class="wrap">
649 <?php screen_icon(); ?>
650 <h2><?php esc_html_e( 'Approve User Settings', 'wp-approve-user' ); ?></h2>
651
652 <div id="poststuff">
653 <div id="post-body" class="obenland-wp columns-2">
654 <div id="post-body-content">
655 <form method="post" action="options.php">
656 <?php
657 settings_fields( $this->textdomain );
658 do_settings_sections( $this->textdomain );
659 submit_button();
660 ?>
661 </form>
662 </div>
663 <div id="postbox-container-1">
664 <div id="side-info-column">
665 <?php do_action( 'obenland_side_info_column' ); ?>
666 </div>
667 </div>
668 </div>
669 </div>
670 </div>
671 <?php
672 }
673
674
675 /**
676 * Prints the section description.
677 *
678 * @author Konstantin Obenland
679 * @since 2.0.0 - 31.03.2012
680 * @access public
681 *
682 * @return void
683 */
684 public function section_description_cb() {
685 $tags = array( 'USERNAME', 'BLOG_TITLE', 'BLOG_URL', 'LOGINLINK' );
686 if ( is_multisite() ) {
687 $tags[] = 'SITE_NAME';
688 }
689
690 printf(
691 /* translators: Placeholders. */
692 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' ),
693 sprintf( '<code>%s</code>', implode( '</code>, <code>', $tags ) ) // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
694 );
695 }
696
697
698 /**
699 * Populates the setting field.
700 *
701 * @author Konstantin Obenland
702 * @since 2.0.0 - 31.03.2012
703 * @access public
704 *
705 * @param array $option Settings option.
706 *
707 * @return void
708 */
709 public function checkbox_cb( $option ) {
710 $option = (object) $option;
711 ?>
712 <label for="<?php echo esc_attr( sanitize_title_with_dashes( $option->name ) ); ?>">
713 <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 ] ); ?> />
714 <?php echo esc_html( $option->description ); ?>
715 </label><br />
716 <?php
717 }
718
719
720 /**
721 * Populates the setting field.
722 *
723 * @author Konstantin Obenland
724 * @since 2.0.0 - 31.03.2012
725 * @access public
726 *
727 * @param array $option Settings option.
728 *
729 * @return void
730 */
731 public function textarea_cb( $option ) {
732 $option = (object) $option;
733 ?>
734 <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" >
735 <?php echo esc_textarea( $this->options[ $option->name ] ); ?>
736 </textarea>
737 <?php
738 }
739
740
741 /**
742 * Sanitizes the settings input.
743 *
744 * @author Konstantin Obenland
745 * @since 2.0.0 - 31.03.2012
746 * @access public
747 *
748 * @param array $input Form input.
749 *
750 * @return array The sanitized settings
751 */
752 public function sanitize( $input ) {
753 return array(
754 'wpau-send-approve-email' => (bool) isset( $input['wpau-send-approve-email'] ),
755 'wpau-send-unapprove-email' => (bool) isset( $input['wpau-send-unapprove-email'] ),
756 'wpau-approve-email' => isset( $input['wpau-approve-email'] ) ? trim( $input['wpau-approve-email'] ) : '',
757 'wpau-unapprove-email' => isset( $input['wpau-unapprove-email'] ) ? trim( $input['wpau-unapprove-email'] ) : '',
758 );
759 }
760
761
762 /**
763 * Sends the approval email.
764 *
765 * @author Konstantin Obenland
766 * @since 2.0.0 - 31.03.2012
767 * @access public
768 *
769 * @param int $user_id User ID.
770 *
771 * @return void
772 */
773 public function wpau_approve( $user_id ) {
774 // Check user meta if mail has been sent already.
775 if ( $this->options['wpau-send-approve-email'] && ! get_user_meta( $user_id, 'wp-approve-user-mail-sent', true ) ) {
776
777 $user = new WP_User( $user_id );
778 $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
779
780 // Send mail.
781 $sent = wp_mail(
782 $user->user_email,
783 /* translators: Blog name. */
784 sprintf( esc_html_x( '[%s] Registration approved', 'Blogname', 'wp-approve-user' ), $blogname ),
785 $this->populate_message( $this->options['wpau-approve-email'], $user )
786 );
787
788 if ( $sent ) {
789 update_user_meta( $user_id, 'wp-approve-user-mail-sent', true );
790 }
791 }
792 }
793
794
795 /**
796 * Sends the rejection email.
797 *
798 * @author Konstantin Obenland
799 * @since 2.0.0 - 31.03.2012
800 * @access public
801 *
802 * @param int $user_id User ID.
803 *
804 * @return void
805 */
806 public function delete_user( $user_id ) {
807 if ( $this->options['wpau-send-unapprove-email'] ) {
808 $user = new WP_User( $user_id );
809 $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
810
811 // Send mail.
812 wp_mail(
813 $user->user_email,
814 /* translators: Blog name. */
815 sprintf( esc_html_x( '[%s] Registration unapproved', 'Blogname', 'wp-approve-user' ), $blogname ),
816 $this->populate_message( $this->options['wpau-unapprove-email'], $user )
817 );
818
819 // No need to delete user_meta, since this user will be GONE.
820 }
821 }
822
823
824 /**
825 * Display all messages registered to this plugin.
826 *
827 * @author Konstantin Obenland
828 * @since 2.0.0 - 30.03.2012
829 * @access public
830 *
831 * @return void
832 */
833 public function all_admin_notices() {
834 settings_errors( $this->textdomain );
835 }
836
837
838 /**
839 * Updates user_meta to approve user.
840 *
841 * @author Konstantin Obenland
842 * @since 1.1 - 12.02.2012
843 * @access protected
844 *
845 * @return void
846 */
847 protected function approve() {
848 list( $user_ids, $url ) = $this->check_user();
849
850 foreach ( (array) $user_ids as $id ) {
851 $id = (int) $id;
852
853 if ( ! current_user_can( 'edit_user', $id ) ) {
854 wp_die( esc_html__( 'You can&#8217;t edit that user.' ), '', array(
855 'back_link' => true,
856 ) );
857 }
858
859 update_user_meta( $id, 'wp-approve-user', true );
860 do_action( 'wpau_approve', $id );
861 }
862
863 wp_safe_redirect( add_query_arg( array(
864 'action' => 'wpau_update',
865 'update' => 'wpau-approved',
866 'count' => count( $user_ids ),
867 'role' => $this->get_role(),
868 ), $url ) );
869 exit();
870 }
871
872
873 /**
874 * Updates user_meta to unapprove user.
875 *
876 * @author Konstantin Obenland
877 * @since 1.1 - 12.02.2012
878 * @access protected
879 *
880 * @return void
881 */
882 protected function unapprove() {
883 list( $user_ids, $url ) = $this->check_user();
884
885 foreach ( (array) $user_ids as $id ) {
886 $id = (int) $id;
887
888 if ( ! current_user_can( 'edit_user', $id ) ) {
889 wp_die( esc_html__( 'You can&#8217;t edit that user.' ), '', array(
890 'back_link' => true,
891 ) );
892 }
893
894 update_user_meta( $id, 'wp-approve-user', false );
895 do_action( 'wpau_unapprove', $id );
896 }
897
898 wp_safe_redirect( add_query_arg( array(
899 'action' => 'wpau_update',
900 'update' => 'wpau-unapproved',
901 'count' => count( $user_ids ),
902 'role' => $this->get_role(),
903 ), $url ) );
904 exit();
905 }
906
907
908 /**
909 * Checks permissions and assembles User IDs.
910 *
911 * @author Konstantin Obenland
912 * @since 2.0.0 - 15.03.2012
913 * @access protected
914 *
915 * @return array User IDs and URL
916 */
917 protected function check_user() {
918 // phpcs:disable WordPress.CSRF.NonceVerification.NoNonceVerification
919
920 $screen_id = get_current_screen()->id;
921 $users_key = 'user';
922
923 if ( false !== stripos( current_action(), 'wpau_bulk_' ) ) {
924 $users_key = 'users-network' === $screen_id ? 'allusers' : 'users';
925 }
926
927 $site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
928 $url = 'site-users-network' === $screen_id ? add_query_arg( array( 'id' => $site_id ), 'site-users.php' ) : 'users.php';
929
930 if ( empty( $_REQUEST[ $users_key ] ) && empty( $_REQUEST[ $users_key ] ) ) {
931 wp_safe_redirect( $url );
932 exit();
933 }
934
935 if ( ! current_user_can( 'promote_users' ) ) {
936 wp_die( esc_html__( 'You can&#8217;t unapprove users.', 'wp-approve-user' ), '', array(
937 'back_link' => true,
938 ) );
939 }
940
941 $user_ids = array_map( 'intval', (array) $_REQUEST[ $users_key ] );
942 $user_ids = array_diff( $user_ids, array( get_user_by( 'email', get_bloginfo( 'admin_email' ) )->ID ) );
943
944 return array( $user_ids, $url );
945
946 // phpcs:enable WordPress.CSRF.NonceVerification.NoNonceVerification
947 }
948
949
950 /**
951 * Replaces all the placeholders with their content.
952 *
953 * @author Konstantin Obenland
954 * @since 2.0.0 - 15.03.2012
955 * @access protected
956 *
957 * @param string $message Email body.
958 * @param WP_User $user User object.
959 *
960 * @return string
961 */
962 protected function populate_message( $message, $user ) {
963 $title = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
964
965 $message = str_replace( 'BLOG_TITLE', $title, $message );
966 $message = str_replace( 'BLOG_URL', home_url(), $message );
967 $message = str_replace( 'LOGINLINK', wp_login_url(), $message );
968 $message = str_replace( 'USERNAME', $user->user_nicename, $message );
969
970 if ( is_multisite() ) {
971 $message = str_replace( 'SITE_NAME', $GLOBALS['current_site']->site_name, $message );
972 }
973
974 return $message;
975 }
976
977
978 /**
979 * Returns the default options.
980 *
981 * @author Konstantin Obenland
982 * @since 2.0.0 - 15.03.2012
983 * @access protected
984 *
985 * @return array
986 */
987 protected function default_options() {
988 $options = array(
989 'wpau-send-approve-email' => false,
990 'wpau-approve-email' => '
991 Hi USERNAME,
992 Your registration for BLOG_TITLE has now been approved.
993
994 You can log in, using your username and password that you created when registering for our website, at the following URL: LOGINLINK
995
996 If you have any questions, or problems, then please do not hesitate to contact us.
997
998 Name,
999 Company,
1000 Contact details',
1001 'wpau-send-unapprove-email' => false,
1002 'wpau-unapprove-email' => '',
1003 );
1004
1005 return apply_filters( 'wpau_default_options', $options );
1006 }
1007
1008 /**
1009 * Sets the role context on bulk actions.
1010 *
1011 * On bulk actions the role parameter is not passed, since we're using a form
1012 * to submit information. The information is only available through the
1013 * `_wp_http_referer` parameter, so we get it from there and make it available
1014 * for the request.
1015 *
1016 * @author Konstantin Obenland
1017 * @since 3 - 04.09.2014
1018 * @access protected
1019 */
1020 protected function set_up_role_context() {
1021 // phpcs:disable WordPress.CSRF.NonceVerification.NoNonceVerification, WordPress.VIP.ValidatedSanitizedInput
1022
1023 if ( empty( $_REQUEST['role'] ) && ! empty( $_REQUEST['_wp_http_referer'] ) ) {
1024 $referrer = parse_url( $_REQUEST['_wp_http_referer'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
1025
1026 if ( ! empty( $referrer['query'] ) ) {
1027 $args = wp_parse_args( $referrer['query'] );
1028
1029 if ( ! empty( $args['role'] ) ) {
1030 $_REQUEST['role'] = $args['role'];
1031 }
1032 }
1033 }
1034 }
1035
1036 /**
1037 * Returns the current role.
1038 *
1039 * If the user list is in the context of a specific role, this function makes
1040 * sure that the requested role is valid. By returning `false` otherwise, we
1041 * make sure that parameter gets removed from the activation link.
1042 *
1043 * @author Konstantin Obenland
1044 * @since 3 - 04.09.2014
1045 * @access protected
1046 *
1047 * @return string|bool The role key if set, false otherwise.
1048 */
1049 protected function get_role() {
1050 $roles = array_keys( get_editable_roles() );
1051 $roles[] = 'wpau_unapproved';
1052 $role = false;
1053
1054 if ( isset( $_REQUEST['role'] ) && in_array( $_REQUEST['role'], $roles, true ) ) {
1055 $role = $_REQUEST['role'];
1056 }
1057
1058 return $role;
1059
1060 // phpcs:enable WordPress.CSRF.NonceVerification.NoNonceVerification, WordPress.VIP.ValidatedSanitizedInput
1061 }
1062
1063 /**
1064 * Re-runs the activation hook when registration is activated.
1065 *
1066 * If the plugin is activated and user registration is disabled, the plugin
1067 * activation hook never gets added, let alone fired. This a secondary
1068 * measure to make sure all existing users are approved on activation.
1069 *
1070 * @author Konstantin Obenland
1071 * @deprecated 2.3.0 - 13.08.2013
1072 * @access public
1073 *
1074 * @param string $old Old settings value.
1075 * @param int $new New settings value.
1076 *
1077 * @return void
1078 */
1079 public function update_option_users_can_register( $old, $new ) {
1080 _deprecated_function( __FUNCTION__, '2.3' );
1081 }
1082 } // End of class Obenland_Wp_Approve_User
1083
1084
1085 new Obenland_Wp_Approve_User();
1086
1087
1088 register_activation_hook( __FILE__, array(
1089 Obenland_Wp_Approve_User::$instance,
1090 'activation',
1091 ) );
1092