Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 @@
}

/**
* Registers the fields in the language_section

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".
*
* Returns the number of added fields
*
Expand Down Expand Up @@ -338,7 +338,7 @@

return count( $map );
}

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".
/**
* Shows the select-form-field 'blog_language'
*/
Expand Down Expand Up @@ -378,27 +378,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
23 changes: 19 additions & 4 deletions includes/MslsBlogCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,20 +301,35 @@ 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 = 0 ): array {
$args = array(
'blog_id' => $this->current_blog_id,
'orderby' => 'registered',
'fields' => $fields,
'number' => $number,
'count_total' => false,
);

if ( $number > 1 ) {
$args['number'] = $number;

$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 ) ) ) );
}
}
Comment thread
lloc marked this conversation as resolved.
Outdated

$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
23 changes: 23 additions & 0 deletions tests/phpunit/TestMslsBlogCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ final class TestMslsBlogCollection extends MslsUnitTestCase {
protected function setUp(): void {
parent::setUp(); // TODO: Change the autogenerated stub

Functions\when( 'count_users' )->justReturn( array( 'total_users' => 3210 ) );

$options = \Mockery::mock( MslsOptions::class );
$options->shouldReceive( 'get_order' )->andReturn( 'description' );
$options->shouldReceive( 'is_excluded' )->andReturn( false );
Expand Down Expand Up @@ -199,6 +201,27 @@ public function test_get_users(): void {
$this->assertIsArray( $obj->get_users() );
}

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

$max_users = 100;

$obj = new MslsBlogCollection();

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 {
Functions\expect( 'get_site_option' )->once()->andReturn( array() );

Expand Down
Loading