Make WordPress Core

Changeset 50230


Ignore:
Timestamp:
02/05/2021 03:48:59 PM (6 years ago)
Author:
SergeyBiryukov
Message:

Privacy: Rename the $send_confirmation_email parameter of wp_create_user_request() to $status, for clarity.

Follow-up to [50159], [50165].

Props xkon, TimothyBlynJacobs.
Fixes #52430. See #43890.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/privacy-tools.php

    r50165 r50230  
    112112                                $username_or_email_address = sanitize_text_field( wp_unslash( $_POST['username_or_email_for_privacy_request'] ) );
    113113                                $email_address             = '';
    114                                 $send_confirmation_email   = true;
     114                                $status                    = 'pending';
    115115
    116116                                if ( ! isset( $_POST['send_confirmation_email'] ) ) {
    117                                         $send_confirmation_email = false;
     117                                        $status = 'confirmed';
    118118                                }
    119119
    … …  
    147147                                }
    148148
    149                                 $request_id = wp_create_user_request( $email_address, $action_type, array(), $send_confirmation_email );
     149                                $request_id = wp_create_user_request( $email_address, $action_type, array(), $status );
     150                                $message    = '';
    150151
    151152                                if ( is_wp_error( $request_id ) ) {
     153                                        $message = $request_id->get_error_message();
     154                                } elseif ( ! $request_id ) {
     155                                        $message = __( 'Unable to initiate confirmation request.' );
     156                                }
     157
     158                                if ( $message ) {
    152159                                        add_settings_error(
    153160                                                'username_or_email_for_privacy_request',
    154161                                                'username_or_email_for_privacy_request',
    155                                                 $request_id->get_error_message(),
    156                                                 'error'
    157                                         );
    158                                         break;
    159                                 } elseif ( ! $request_id ) {
    160                                         add_settings_error(
    161                                                 'username_or_email_for_privacy_request',
    162                                                 'username_or_email_for_privacy_request',
    163                                                 __( 'Unable to initiate confirmation request.' ),
     162                                                $message,
    164163                                                'error'
    165164                                        );
    … …  
    167166                                }
    168167
    169                                 if ( $send_confirmation_email ) {
     168                                if ( 'pending' === $status ) {
    170169                                        wp_send_user_request( $request_id );
     170
     171                                        $message = __( 'Confirmation request initiated successfully.' );
     172                                } elseif ( 'confirmed' === $status ) {
     173                                        $message = __( 'Request added successfully.' );
    171174                                }
    172175
    173                                 add_settings_error(
    174                                         'username_or_email_for_privacy_request',
    175                                         'username_or_email_for_privacy_request',
    176                                         __( 'Confirmation request initiated successfully.' ),
    177                                         'success'
    178                                 );
    179                                 break;
     176                                if ( $message ) {
     177                                        add_settings_error(
     178                                                'username_or_email_for_privacy_request',
     179                                                'username_or_email_for_privacy_request',
     180                                                $message,
     181                                                'success'
     182                                        );
     183                                        break;
     184                                }
    180185                }
    181186        }
  • trunk/src/wp-includes/user.php

    r50165 r50230  
    39373937 *
    39383938 * @since 4.9.6
    3939  * @since 5.7.0 Added the `$send_confirmation_email` parameter.
     3939 * @since 5.7.0 Added the `$status` parameter.
    39403940 *
    39413941 * @param string $email_address           User email address. This can be the address of a registered
    … …  
    39443944 * @param array  $request_data            Misc data you want to send with the verification request and pass
    39453945 *                                        to the actions once the request is confirmed.
    3946  * @param bool   $send_confirmation_email Optional. If false, the request status is set to 'Completed' directly.
    3947  *                                        Default true.
     3946 * @param string $status                  Optional request status (pending or confirmed). Default 'pending'.
    39483947 * @return int|WP_Error                   Returns the request ID if successful, or a WP_Error object on failure.
    39493948 */
    3950 function wp_create_user_request( $email_address = '', $action_name = '', $request_data = array(), $send_confirmation_email = true ) {
     3949function wp_create_user_request( $email_address = '', $action_name = '', $request_data = array(), $status = 'pending' ) {
    39513950        $email_address = sanitize_email( $email_address );
    39523951        $action_name   = sanitize_key( $action_name );
    … …  
    39583957        if ( ! in_array( $action_name, _wp_privacy_action_request_types(), true ) ) {
    39593958                return new WP_Error( 'invalid_action', __( 'Invalid action name.' ) );
     3959        }
     3960
     3961        if ( ! in_array( $status, array( 'pending', 'confirmed' ), true ) ) {
     3962                return new WP_Error( 'invalid_status', __( 'Invalid request status.' ) );
    39603963        }
    39613964
    … …  
    39813984        }
    39823985
    3983         if ( false !== $send_confirmation_email ) {
    3984                 $status = 'request-pending';
    3985         } else {
    3986                 $status = 'request-completed';
    3987         }
    3988 
    39893986        $request_id = wp_insert_post(
    39903987                array(
    … …  
    39933990                        'post_title'    => $email_address,
    39943991                        'post_content'  => wp_json_encode( $request_data ),
    3995                         'post_status'   => $status,
     3992                        'post_status'   => 'request-' . $status,
    39963993                        'post_type'     => 'user_request',
    39973994                        'post_date'     => current_time( 'mysql', false ),
  • trunk/tests/phpunit/tests/privacy/wpCreateUserRequest.php

    r50160 r50230  
    311311
    312312        /**
    313          * Test that the request has a Pending status if a confirmation email is sent.
     313         * Test that the request has a Pending status by default.
    314314         *
    315315         * @ticket 43890
    316316         */
    317         public function test_pending_status_with_default_wp_create_user_request_params() {
     317        public function test_wp_create_user_request_default_pending_status() {
    318318                $actual = wp_create_user_request( self::$non_registered_user_email, 'export_personal_data' );
    319319                $post   = get_post( $actual );
    … …  
    323323
    324324        /**
    325          * Test that the request has a Pending status if the $send_confirmation_email param is true.
     325         * Test that the request has a Pending status if the $status param is 'pending'.
    326326         *
    327327         * @ticket 43890
    328328         */
    329         public function test_pending_status_with_true_send_confirmation_email() {
    330                 $request_data            = array();
    331                 $send_confirmation_email = true;
    332 
    333                 $actual = wp_create_user_request( self::$non_registered_user_email, 'export_personal_data', $request_data, $send_confirmation_email );
     329        public function test_wp_create_user_request_pending_status() {
     330                $actual = wp_create_user_request( self::$non_registered_user_email, 'export_personal_data', array(), 'pending' );
    334331                $post   = get_post( $actual );
    335332
    … …  
    338335
    339336        /**
    340          * Test that the request has a Completed status if the $send_confirmation_email param is false.
     337         * Test that the request has a Confirmed status if the $status param is 'confirmed'.
    341338         *
    342339         * @ticket 43890
    343340         */
    344         public function test_pending_status_with_false_send_confirmation_email() {
    345                 $request_data            = array();
    346                 $send_confirmation_email = false;
    347 
    348                 $actual = wp_create_user_request( self::$non_registered_user_email, 'export_personal_data', $request_data, $send_confirmation_email );
     341        public function test_wp_create_user_request_confirmed_status() {
     342                $actual = wp_create_user_request( self::$non_registered_user_email, 'export_personal_data', array(), 'confirmed' );
    349343                $post   = get_post( $actual );
    350344
    351                 $this->assertSame( 'request-completed', $post->post_status );
     345                $this->assertSame( 'request-confirmed', $post->post_status );
     346        }
     347
     348        /**
     349         * Test that the request returns a WP_Error if $status isn't 'pending' or 'confirmed'.
     350         *
     351         * @ticket 43890
     352         */
     353        public function test_wp_create_user_request_wp_error_status() {
     354                $actual = wp_create_user_request( self::$non_registered_user_email, 'export_personal_data', array(), 'wrong-status' );
     355
     356                $this->assertWPError( $actual );
    352357        }
    353358}
Note: See TracChangeset for help on using the changeset viewer.