Make WordPress Core


Ignore:
Timestamp:
08/03/2017 09:40:02 PM (8 years ago)
Author:
flixos90
Message:

Multisite: Introduce a can_add_user_to_blog filter to prevent adding a user to a site.

Under certain circumstances, it can be necessary that a user should not be added to a site, beyond the restrictions that WordPress core applies. With the new can_add_user_to_blog filter, plugin developers can run custom checks and return an error in case of a failure, that will prevent the user from being added.

The user-facing parts and the REST API route that interact with add_user_to_blog() have been adjusted accordingly to provide appropriate error feedback when a user could not be added to a site. Furthermore, two existing error feedback messages in the site admin's "New User" screen have been adjusted to properly show inside an error notice instead of a success notice.

Props jmdodd.
Fixes #41101.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/user/multisite.php

    r41171 r41225  
    396396
    397397        $this->assertWPError( $result );
     398    }
     399
     400    /**
     401     * @ticket 41101
     402     */
     403    public function test_should_fail_can_add_user_to_blog_filter() {
     404        $site_id = self::factory()->blog->create();
     405        $user_id = self::factory()->user->create();
     406
     407        add_filter( 'can_add_user_to_blog', '__return_false' );
     408        $result = add_user_to_blog( $site_id, $user_id, 'subscriber' );
     409
     410        $this->assertWPError( $result );
     411    }
     412
     413    /**
     414     * @ticket 41101
     415     */
     416    public function test_should_succeed_can_add_user_to_blog_filter() {
     417        $site_id = self::factory()->blog->create();
     418        $user_id = self::factory()->user->create();
     419
     420        add_filter( 'can_add_user_to_blog', '__return_true' );
     421        $result = add_user_to_blog( $site_id, $user_id, 'subscriber' );
     422
     423        $this->assertTrue( $result );
    398424    }
    399425
Note: See TracChangeset for help on using the changeset viewer.