Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions includes/MslsAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
*
* @since 1.0
*/
do_action( self::MSLS_REGISTER_ACTION, __CLASS__ );

Check warning on line 242 in includes/MslsAdmin.php

View workflow job for this annotation

GitHub Actions / test

WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound

Hook names invoked by a theme/plugin should start with the theme/plugin prefix. Found: "self::MSLS_REGISTER_ACTION".
}

/**
Expand Down Expand Up @@ -338,7 +338,7 @@
*
* @since 2.4.4
*/
do_action( self::MSLS_ACTION_PREFIX . $section, __CLASS__, $section );

Check warning on line 341 in includes/MslsAdmin.php

View workflow job for this annotation

GitHub Actions / test

WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound

Hook names invoked by a theme/plugin should start with the theme/plugin prefix. Found: "self::MSLS_ACTION_PREFIX . $section".

return count( $map );
}
Expand Down Expand Up @@ -382,27 +382,17 @@
* Shows the select-form-field 'reference_user'
*/
public function reference_user(): void {
$users = array();
$max_users = (int) apply_filters( 'msls_max_reference_users_count', self::MAX_REFERENCE_USERS );

foreach ( (array) apply_filters( 'msls_reference_users', $this->collection->get_users() ) as $user ) {
$users[ $user->ID ] = $user->user_nicename;
}

if ( count( $users ) > self::MAX_REFERENCE_USERS ) {
$users = array_slice( $users, 0, self::MAX_REFERENCE_USERS, true );
$users_collection = $this->collection->get_users( array( 'ID', 'user_nicename' ), $max_users );

/* translators: %s: maximum number of users */
$format = __(
'Multisite Language Switcher: Collection for reference user has been truncated because it exceeded the maximum of %d users. Please, use the hook "msls_reference_users" to filter the result before!',
'multisite-language-switcher'
);

// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
trigger_error( esc_html( sprintf( $format, strval( self::MAX_REFERENCE_USERS ) ) ) );
}
$reference_users = (array) apply_filters(
'msls_reference_users',
wp_list_pluck( $users_collection, 'user_nicename', 'ID' )
);

// phpcs:ignore WordPress.Security.EscapeOutput
echo ( new Select( 'reference_user', $users, strval( $this->options->reference_user ) ) )->render();
echo ( new Select( 'reference_user', $reference_users, strval( $this->options->reference_user ) ) )->render();
}

/**
Expand Down
31 changes: 21 additions & 10 deletions includes/MslsBlogCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,9 @@ public static function get_configured_blog_description( int $blog_id, $descripti
* @return object[]|\stdClass[]
*/
public function get_blogs_of_reference_user( MslsOptions $options ) {
$reference_user = $options->has_value( 'reference_user' ) ? $options->reference_user : current(
$this->get_users(
'ID',
1
)
);
$reference_user = $options->has_value( 'reference_user' ) ?
$options->reference_user :
current( $this->get_users( 'ID', 1 ) );

$blogs = get_blogs_of_user( $reference_user );
foreach ( $blogs as $key => $blog ) {
Expand Down Expand Up @@ -305,20 +302,34 @@ public function get_filtered( bool $filter = false ): array {
/**
* Gets the registered users of the current blog
*
* @param string $fields
* @param int|string $number
* @param string|string[] $fields
* @param int $number
*
* @return array<string, int>
*/
public function get_users( $fields = 'all', $number = '' ) {
public function get_users( $fields = 'all', int $number = MslsAdmin::MAX_REFERENCE_USERS ): array {
$args = array(
'blog_id' => $this->current_blog_id,
'orderby' => 'registered',
'fields' => $fields,
'number' => $number,
'number' => $number > 0 ? $number : MslsAdmin::MAX_REFERENCE_USERS,
'count_total' => false,
);

if ( $number !== 1 ) { // Check total users only if not fetching a single user
$user_count = count_users();
if ( isset( $user_count['total_users'] ) && $user_count['total_users'] > $number ) {
/* translators: %s: maximum number of users */
$format = __(
'Multisite Language Switcher: The user list has been limited to %d users.',
'multisite-language-switcher'
);

// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
trigger_error( esc_html( sprintf( $format, strval( $number ) ) ) );
}
}

$args = (array) apply_filters( 'msls_get_users', $args );

return get_users( $args );
Expand Down
30 changes: 2 additions & 28 deletions tests/phpunit/TestMslsAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,35 +116,9 @@ public function test_admin_display(): void {
$obj->admin_display();
}

public function test_reference_user(): void {
$users = array();
$too_much = MslsAdmin::MAX_REFERENCE_USERS + 1;
for ( $i = 1; $i <= $too_much; $i++ ) {
$users[] = (object) array(
'ID' => $i,
'user_nicename' => 'realloc',
);
}

$obj = $this->MslsAdminFactory( $users );

set_error_handler(
static function ( $errno, $errstr ) {
restore_error_handler();
throw new \Exception( $errstr, $errno );
},
E_ALL
);

$this->expectException( \Exception::class );
$this->expectExceptionMessage(
'Multisite Language Switcher: Collection for reference user has been truncated because it exceeded the maximum of 100 users. Please, use the hook "msls_reference_users" to filter the result before!'
);

$obj->reference_user();
}

public function test_reference_user_over_max(): void {
$users = array( 1 => 'realloc' );
Functions\expect( 'wp_list_pluck' )->once()->andReturn( $users );
$obj = $this->MslsAdminFactory();

$this->expectOutputRegex( '/^<select id="reference_user" name="msls\[reference_user\]">.*$/' );
Expand Down
37 changes: 35 additions & 2 deletions tests/phpunit/TestMslsBlogCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@

final class TestMslsBlogCollection extends MslsUnitTestCase {

const TOTAL_USERS = 3210;

protected function setUp(): void {
parent::setUp(); // TODO: Change the autogenerated stub

Functions\when( 'count_users' )->justReturn( array( 'total_users' => self::TOTAL_USERS ) );

$options = \Mockery::mock( MslsOptions::class );
$options->shouldReceive( 'get_order' )->andReturn( 'description' );
$options->shouldReceive( 'is_excluded' )->andReturn( false );
Expand Down Expand Up @@ -191,12 +195,41 @@ public function test_get_filtered(): void {
$this->assertIsArray( $obj->get_filtered( true ) );
}

public function test_get_users(): void {
public function test_get_users_single(): void {
Functions\expect( 'get_site_option' )->once()->andReturn( array() );

$obj = new MslsBlogCollection();

$this->assertIsArray( $obj->get_users( array( 'ID' ), 1 ) );
}

public function test_get_users_massive(): void {
Functions\expect( 'get_site_option' )->once()->andReturn( array() );

$obj = new MslsBlogCollection();

$this->assertIsArray( $obj->get_users( array( 'ID' ), self::TOTAL_USERS ) );
}

public function test_get_users_max() {
Functions\expect( 'get_site_option' )->once()->andReturn( array() );

$max_users = 100;

$obj = new MslsBlogCollection();

$this->assertIsArray( $obj->get_users() );
set_error_handler(
static function ( $errno, $errstr ) {
restore_error_handler();
throw new \Exception( $errstr, $errno );
},
E_ALL
);

$this->expectException( \Exception::class );
$this->expectExceptionMessage( "Multisite Language Switcher: The user list has been limited to {$max_users} users." );

$obj->get_users( 'all', $max_users );
}

public function test_get_current_blog(): void {
Expand Down
Loading