ID ) ) { return new WP_Error( 'wp360_lg_pending', '' . esc_html__( 'Error:', '360-orbit-login-guard' ) . ' ' . esc_html__( 'Your account is waiting for approval by an administrator. You will receive an email as soon as you can sign in.', '360-orbit-login-guard' ) ); } return $user; } /** * Hinweis auf der Anmeldeseite direkt nach der Registrierung. * * @param string $message Bisherige Meldung. */ public static function registered_message( $message ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nur eine Meldung. if ( isset( $_GET['checkemail'] ) && 'registered' === $_GET['checkemail'] ) { $message .= '

' . esc_html__( 'An administrator checks new accounts before they can be used. You will receive an email as soon as yours is approved.', '360-orbit-login-guard' ) . '

'; } return $message; } /** * Mail an den Administrator: mit Link zur Freigabe. * * @param array $email Mail von WordPress. * @param WP_User $user Neues Konto. */ public static function admin_email( $email, $user ) { if ( ! $user instanceof WP_User || ! self::is_pending( $user->ID ) ) { return $email; } $email['subject'] = sprintf( /* translators: %s: site name */ __( '[%s] New registration waiting for approval', '360-orbit-login-guard' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) ); $email['message'] = sprintf( /* translators: 1: user name, 2: email address, 3: link to the list of waiting accounts */ __( "A new account was registered and is waiting for your approval.\n\nUser name: %1\$s\nEmail: %2\$s\n\nApprove or reject it here:\n%3\$s", '360-orbit-login-guard' ), $user->user_login, $user->user_email, admin_url( 'users.php?wp360_lg_pending=1' ) ); return $email; } /** * Gibt ein Konto frei und benachrichtigt den Benutzer. * * @param int $user_id Konto. */ public static function approve( $user_id ) { if ( ! self::is_pending( $user_id ) ) { return; } delete_user_meta( (int) $user_id, self::META ); $user = get_userdata( (int) $user_id ); if ( ! $user ) { return; } wp_mail( $user->user_email, sprintf( /* translators: %s: site name */ __( '[%s] Your account has been approved', '360-orbit-login-guard' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) ), sprintf( /* translators: 1: user name, 2: login address */ __( "Hello %1\$s,\n\nyour account has been approved. You can sign in now:\n%2\$s\n\nIf you have not set a password yet, use “Lost your password?” on that page.", '360-orbit-login-guard' ), $user->display_name, wp_login_url() ) ); } /** * Lehnt ein wartendes Konto ab: Es wird geloescht. * * @param int $user_id Konto. */ public static function reject( $user_id ) { if ( ! self::is_pending( $user_id ) || get_current_user_id() === (int) $user_id ) { return; } require_once ABSPATH . 'wp-admin/includes/user.php'; wp_delete_user( (int) $user_id ); } public static function count_pending() { $query = new WP_User_Query( array( 'meta_key' => self::META, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- nur im Backend. 'count_total' => true, 'number' => 1, 'fields' => 'ID', ) ); return (int) $query->get_total(); } /* * --------------------------------------------------------------------- * Benutzerliste * --------------------------------------------------------------------- */ /** * Ansicht „Warten auf Freigabe“ ueber der Benutzerliste. * * @param string[] $views Ansichten. */ public static function users_view( $views ) { $count = self::count_pending(); if ( ! $count ) { return $views; } $current = isset( $_GET['wp360_lg_pending'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nur die Ansicht. $views['wp360_lg_pending'] = sprintf( '%s (%s)', esc_url( admin_url( 'users.php?wp360_lg_pending=1' ) ), $current ? ' class="current" aria-current="page"' : '', esc_html__( 'Waiting for approval', '360-orbit-login-guard' ), esc_html( number_format_i18n( $count ) ) ); return $views; } /** * @param WP_User_Query $query Abfrage der Benutzerliste. */ public static function filter_users( $query ) { global $pagenow; if ( ! is_admin() || 'users.php' !== $pagenow || ! isset( $_GET['wp360_lg_pending'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nur die Ansicht. return; } $query->set( 'meta_key', self::META ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- nur im Backend. } private static function action_url( $do, $user_id ) { return wp_nonce_url( admin_url( 'admin-post.php?action=' . self::ACTION . '&do=' . $do . '&user=' . (int) $user_id ), self::ACTION . '_' . (int) $user_id ); } /** * @param string[] $actions Zeilenaktionen. * @param WP_User $user Konto der Zeile. */ public static function row_actions( $actions, $user ) { if ( ! current_user_can( 'edit_users' ) || ! self::is_pending( $user->ID ) ) { return $actions; } $actions['wp360_lg_approve'] = '' . esc_html__( 'Approve', '360-orbit-login-guard' ) . ''; if ( current_user_can( 'delete_users' ) ) { $actions['wp360_lg_reject'] = '' . esc_html__( 'Reject and delete', '360-orbit-login-guard' ) . ''; } return $actions; } /** * @param string[] $columns Spalten. */ public static function add_column( $columns ) { if ( self::count_pending() ) { $columns['wp360_lg_status'] = __( 'Approval', '360-orbit-login-guard' ); } return $columns; } /** * @param string $output Bisherige Ausgabe. * @param string $column Spalte. * @param int $user_id Konto. */ public static function render_column( $output, $column, $user_id ) { if ( 'wp360_lg_status' !== $column ) { return $output; } $since = (int) get_user_meta( (int) $user_id, self::META, true ); return $since /* translators: %s: date */ ? '' . esc_html( sprintf( __( 'Waiting since %s', '360-orbit-login-guard' ), wp_date( get_option( 'date_format' ), $since ) ) ) . '' : esc_html__( 'Approved', '360-orbit-login-guard' ); } /** * @param string[] $actions Mehrfachaktionen. */ public static function bulk_actions( $actions ) { if ( current_user_can( 'edit_users' ) && self::count_pending() ) { $actions['wp360_lg_approve'] = __( 'Approve registration', '360-orbit-login-guard' ); } return $actions; } /** * @param string $redirect Ziel. * @param string $action Aktion. * @param int[] $user_ids Konten. */ public static function handle_bulk( $redirect, $action, $user_ids ) { if ( 'wp360_lg_approve' !== $action || ! current_user_can( 'edit_users' ) ) { return $redirect; } $done = 0; foreach ( (array) $user_ids as $user_id ) { if ( self::is_pending( (int) $user_id ) && current_user_can( 'edit_user', (int) $user_id ) ) { self::approve( (int) $user_id ); ++$done; } } return add_query_arg( 'wp360_lg_approved', $done, $redirect ); } public static function handle_single() { $user_id = isset( $_GET['user'] ) ? absint( $_GET['user'] ) : 0; $do = isset( $_GET['do'] ) ? sanitize_key( wp_unslash( $_GET['do'] ) ) : ''; check_admin_referer( self::ACTION . '_' . $user_id ); $cap = 'reject' === $do ? 'delete_user' : 'edit_user'; if ( ! $user_id || ! current_user_can( $cap, $user_id ) ) { wp_die( esc_html__( 'You do not have permission to do this.', '360-orbit-login-guard' ), '', array( 'response' => 403 ) ); } if ( 'reject' === $do ) { self::reject( $user_id ); $arg = 'wp360_lg_rejected'; } else { self::approve( $user_id ); $arg = 'wp360_lg_approved'; } wp_safe_redirect( add_query_arg( $arg, 1, admin_url( 'users.php' ) ) ); exit; } /** Bestaetigung nach Freigabe oder Ablehnung, Hinweis auf wartende Konten. */ public static function admin_notice() { $screen = get_current_screen(); if ( ! $screen || 'users' !== $screen->id || ! current_user_can( 'edit_users' ) ) { return; } // phpcs:disable WordPress.Security.NonceVerification.Recommended -- nur Meldungen. if ( isset( $_GET['wp360_lg_approved'] ) ) { $count = absint( $_GET['wp360_lg_approved'] ); /* translators: %d: number of accounts */ echo '

' . esc_html( sprintf( _n( '%d account approved. The user has been informed by email.', '%d accounts approved. The users have been informed by email.', $count, '360-orbit-login-guard' ), $count ) ) . '

'; } elseif ( isset( $_GET['wp360_lg_rejected'] ) ) { echo '

' . esc_html__( 'Registration rejected, the account has been deleted.', '360-orbit-login-guard' ) . '

'; } // phpcs:enable } }