Use site host in backup codes download title - #964
faithcoder wants to merge 2 commits into
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
🟡 Changes recommended
The new filter’s documented $user argument isn’t actually covered by the added test (it registers the filter for 1 accepted arg and doesn’t assert the user is passed).
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR updates the backup-codes download heading generated by the Backup Codes provider to avoid including a full home URL, and introduces a new filter to allow site owners to customize that heading.
Changes:
- Use
wp_parse_url( home_url(), PHP_URL_HOST )so the default download title includes only the site host (not scheme/path). - Add a
two_factor_backup_codes_download_titlefilter that receives the generated title and the targetWP_User. - Extend REST API tests to validate the host-only behavior and that the title can be filtered.
File summaries
| File | Description |
|---|---|
providers/class-two-factor-backup-codes.php |
Switches the default download title to use host-only and applies a new filter to allow customizing the title. |
tests/providers/class-two-factor-backup-codes-rest-api.php |
Adds REST API regression assertions for host-only title generation and basic filterability of the title. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| $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'] ) ); |
masteradhoc
left a comment
There was a problem hiding this comment.
Thank you very much for the PR @faithcoder! Here my comments for the PR. We'll await a further review of @georgestephanis and then give you a full feedback.
| /** | ||
| * 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 ); | ||
|
|
There was a problem hiding this comment.
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.
| /* translators: %s: the site's domain */ | ||
| __( 'Two-Factor Recovery Codes for %s', 'two-factor' ), | ||
| home_url( '/' ) | ||
| wp_parse_url( home_url(), PHP_URL_HOST ) |
There was a problem hiding this comment.
On a subdirectory multisite all sites share a host, so this labels every site's file identically — home_url( '/' ) at least differed by path.
What?
Use the site's host instead of its full URL in the heading of downloaded backup-code files, and make the heading filterable.
Fixes #957
Why?
The current heading includes the scheme and path from
home_url( '/' ), which makes the recovery-code file contain a directly usable target URL. The host alone still identifies the site while matching the existing translator comment and avoiding unnecessary URL details.How?
home_url()withwp_parse_url().two_factor_backup_codes_download_titlefilter, including the generated title and user as arguments.Use of AI Tools
AI assistance: Yes
Tool(s): Codex
Model(s): GPT-5
Used for: Investigating the issue, implementing the change, adding tests, and running validation. The resulting diff was reviewed before submission.
Testing Instructions
two_factor_backup_codes_download_title, generate codes again, and confirm the custom heading appears.Validation performed:
git diff --checkpassed.Changelog Entry