Make WordPress Core

Changeset 63582


Ignore:
Timestamp:
09/10/2026 01:58:33 PM (5 weeks ago)
Author:
johnbillion
Message:

Application Passwords: Send email notification when an application password is added.

This introduces a filterable email notification so users get alerted when an application password is added to their account.

The email can be filtered via the wp_application_password_created_email filter and disabled via the wp_send_application_password_created_email filter.

Props prasadkarmalkar, johnbillion, audrasjb.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/9795

Fixes #63927

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/default-filters.php

    r63578 r63582  
    345345add_filter( 'rest_authentication_errors', 'rest_application_password_check_errors', 90 );
    346346add_filter( 'rest_authentication_errors', 'rest_cookie_check_errors', 100 );
     347
     348// Application password notifications.
     349add_action( 'wp_create_application_password', 'wp_application_password_created_notification', 10, 2 );
    347350
    348351// Actions.
  • trunk/src/wp-includes/functions.php

    r63522 r63582  
    94659465        return hash_equals( $hash, wp_fast_hash( $message ) );
    94669466}
     9467
     9468/**
     9469 * Sends an email to the user when a new application password is created.
     9470 *
     9471 * @since 7.2.0
     9472 *
     9473 * @param int   $user_id  The user ID.
     9474 * @param array $new_item The application password details.
     9475 */
     9476function wp_application_password_created_notification( $user_id, $new_item ) {
     9477        $send = true;
     9478
     9479        // Get current user data.
     9480        $user = get_userdata( $user_id );
     9481
     9482        if ( ! $user ) {
     9483                return;
     9484        }
     9485
     9486        if ( ! is_email( $user->user_email ) ) {
     9487                return;
     9488        }
     9489
     9490        // Validate that the application password has a name.
     9491        if ( empty( $new_item['name'] ) ) {
     9492                return;
     9493        }
     9494
     9495        /**
     9496         * Filters whether to send the application password created notification email.
     9497         *
     9498         * @since 7.2.0
     9499         *
     9500         * @param bool    $send  Whether to send the email notification.
     9501         * @param WP_User $user  The user object.
     9502         * @param array   $new_item The application password details.
     9503         */
     9504        $send = apply_filters( 'wp_send_application_password_created_email', $send, $user, $new_item );
     9505
     9506        if ( ! $send ) {
     9507                return;
     9508        }
     9509
     9510        /* translators: Do not translate USERNAME, APPLICATION_PASSWORD_NAME, SITENAME, SITEURL, EMAIL: those are placeholders. */
     9511        $application_password_create_text = __(
     9512                'Hi ###USERNAME###,
     9513
     9514A new application password was added to your account on ###SITENAME###. This password allows access to your account via the REST API.
     9515
     9516If you did not expect this, please contact the Site Administrator at
     9517###ADMIN_EMAIL###
     9518
     9519Application password name: ###APPLICATION_PASSWORD_NAME###
     9520Site: ###SITEURL###
     9521
     9522You can manage your application passwords in your account settings.
     9523
     9524This email has been sent to ###EMAIL###
     9525
     9526Regards,
     9527All at ###SITENAME###
     9528###SITEURL###'
     9529        );
     9530
     9531        $email = array(
     9532                'to'      => $user->user_email,
     9533                /* translators: Application password creation email subject. %s: Site title. */
     9534                'subject' => __( '[%s] Application Password Created' ),
     9535                'message' => $application_password_create_text,
     9536                'headers' => '',
     9537        );
     9538
     9539        // Get site name.
     9540        $site_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
     9541
     9542        /**
     9543         * Filters the contents of the email notification sent to a user when a new application password is created.
     9544         *
     9545         * @since 7.2.0
     9546         *
     9547         * @param array   $email {
     9548         *     Used to build wp_mail().
     9549         *
     9550         *     @type string $to      The email address of the intended recipient.
     9551         *     @type string $subject The subject of the email.
     9552         *     @type string $message The content of the email.
     9553         *         The following strings have a special meaning and will get replaced dynamically:
     9554         *          - `###USERNAME###`                  The user's display name.
     9555         *          - `###APPLICATION_PASSWORD_NAME###` The name of the application password.
     9556         *          - `###EMAIL###`                     The user's email address.
     9557         *          - `###SITENAME###`                  The name of the site.
     9558         *          - `###SITEURL###`                   The URL to the site.
     9559         *     @type string $headers Headers.
     9560         * }
     9561         * @param WP_User $user     The user object.
     9562         * @param array   $new_item The application password details.
     9563         */
     9564        $email = apply_filters( 'wp_application_password_created_email', $email, $user, $new_item );
     9565
     9566        $email['message'] = str_replace( '###USERNAME###', $user->display_name, $email['message'] );
     9567        $email['message'] = str_replace( '###APPLICATION_PASSWORD_NAME###', $new_item['name'], $email['message'] );
     9568        $email['message'] = str_replace( '###EMAIL###', $user->user_email, $email['message'] );
     9569        $email['message'] = str_replace( '###SITENAME###', $site_name, $email['message'] );
     9570        $email['message'] = str_replace( '###SITEURL###', home_url(), $email['message'] );
     9571
     9572        wp_mail(
     9573                $email['to'],
     9574                sprintf(
     9575                        $email['subject'],
     9576                        $site_name
     9577                ),
     9578                $email['message'],
     9579                $email['headers']
     9580        );
     9581}
Note: See TracChangeset for help on using the changeset viewer.