Skip to content
Open
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
12 changes: 11 additions & 1 deletion providers/class-two-factor-backup-codes.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,19 @@ public function rest_generate_codes( $request ) {
$title = sprintf(
/* translators: %s: the site's domain */
__( 'Two-Factor Recovery Codes for %s', 'two-factor' ),
home_url( '/' )
wp_parse_url( home_url(), PHP_URL_HOST )

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a subdirectory multisite all sites share a host, so this labels every site's file identically — home_url( '/' ) at least differed by path.

);

/**
* Filters the title in the backup codes download file.
*
* @since 0.17.0
*
* @param string $title Title for the backup codes download file.
* @param WP_User $user User for whom the backup codes were generated.
*/
$title = apply_filters( 'two_factor_backup_codes_download_title', $title, $user );

Comment on lines +364 to +373

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this filter is really needed. If it is added, applying it to the identifier before sprintf() rather than to the composed string avoids callbacks having to reimplement the translated wrapper.

// Generate download content.
$download_link = 'data:application/text;charset=utf-8,';
$download_link .= rawurlencode( "{$title}\r\n\r\n" );
Expand Down
30 changes: 30 additions & 0 deletions tests/providers/class-two-factor-backup-codes-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,36 @@ public function test_generate_code_and_validate_in_download_file() {
$this->assertCount( 10, $data['codes'] );
$this->assertTrue( self::$provider->validate_code( wp_get_current_user(), $data['codes'][0] ) );
$this->assertStringContainsString( $data['codes'][0], $data['download_link'] );
$this->assertStringContainsString( 'Two-Factor Recovery Codes for example.org', rawurldecode( $data['download_link'] ) );
$this->assertStringNotContainsString( home_url(), rawurldecode( $data['download_link'] ) );
}

/**
* Verify that the download title can be filtered.
*
* @covers Two_Factor_Backup_Codes::rest_generate_codes
*/
public function test_download_file_title_can_be_filtered() {
wp_set_current_user( self::$admin_id );
$filter = static function () {
return 'Custom recovery codes title';
};
add_filter( 'two_factor_backup_codes_download_title', $filter );

$request = new WP_REST_Request( 'POST', '/' . Two_Factor_Core::REST_NAMESPACE . '/generate-backup-codes' );
$request->set_body_params(
array(
'user_id' => self::$admin_id,
)
);

$response = rest_do_request( $request );
$data = $response->get_data();

remove_filter( 'two_factor_backup_codes_download_title', $filter );

$this->assertEquals( 200, $response->get_status() );
$this->assertStringContainsString( 'Custom recovery codes title', rawurldecode( $data['download_link'] ) );
}

/**
Expand Down
Loading