Skip to content

Commit

Permalink
Merge pull request #26 from BrianHenryIE/add/filters
Browse files Browse the repository at this point in the history
Add/filters
  • Loading branch information
BrianHenryIE committed Jul 11, 2024
2 parents 34f9093 + d258b67 commit fb14731
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .env.testing
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ TEST_SITE_TABLE_PREFIX="wp_"

TEST_DB_NAME="bh_wp_autologin_urls_tests"
TEST_DB_HOST="127.0.0.1"
TEST_DB_PORT="56968"
TEST_DB_PORT="33066"
TEST_DB_USER="root"
TEST_DB_PASSWORD="password"
TEST_TABLE_PREFIX="wp_au_tests_"
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/phpcbf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ jobs:
php-codesniffer:
runs-on: ubuntu-latest

permissions:
contents: write

services:
mysql:
image: mysql:8.0
Expand Down
3 changes: 3 additions & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"plugins": [
"."
]
},
"tests": {
"mysqlPort": 33066
}
},
"lifecycleScripts": {
Expand Down
62 changes: 46 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@playwright/test": "^1.32.0",
"@woocommerce/woocommerce-rest-api": "^1.0.1",
"@wordpress/e2e-test-utils-playwright": "^0.19.2",
"@wordpress/env": "^8.7.0",
"@wordpress/env": "^10.3.0",
"@wordpress/scripts": "^27.2.5",
"allure-playwright": "^2.15.1",
"dotenv": "^16.4.5"
Expand Down
41 changes: 31 additions & 10 deletions src/api/class-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ public function add_autologin_to_message( string $message, $user, ?int $expires_
*/
public function get_wp_user( $user ): ?WP_User {

/**
* Allow other plugins to provide the user object.
*
* @param null|int|string|WP_User $user A user id, email, login or user object.
*/
$user = apply_filters( 'bh_wp_autologin_urls_get_wp_user', $user );

if ( $user instanceof WP_User ) {
return $user;
}
Expand All @@ -124,6 +131,10 @@ public function get_wp_user( $user ): ?WP_User {
$user = absint( $user );
$user = get_user_by( 'ID', $user );

// But it is possible that the number is actually the username.
if ( false === $user ) {
$user = get_user_by( 'login', $user );
}
} elseif ( is_string( $user ) && is_email( $user ) ) {

// When a string which is an email is passed.
Expand Down Expand Up @@ -428,21 +439,16 @@ public function send_magic_link( string $username_or_email_address, ?string $url
'expires_in_friendly' => $expires_in_friendly,
);

$wp_user = get_user_by( 'login', $username_or_email_address );
$wp_user = $this->get_wp_user( $username_or_email_address );

if ( ! ( $wp_user instanceof WP_User ) ) {

$wp_user = get_user_by( 'email', $username_or_email_address );

if ( ! ( $wp_user instanceof WP_User ) ) {

// NB: Do not tell the user if the username exists.
$result['success'] = false;
// NB: Do not tell the user if the username exists.
$result['success'] = false;

$this->logger->debug( "No WP_User found for {$username_or_email_address}", array( 'result' => $result ) );
$this->logger->debug( "No WP_User found for {$username_or_email_address}", array( 'result' => $result ) );

return $result;
}
return $result;
}

$result['wp_user'] = $wp_user;
Expand All @@ -467,6 +473,21 @@ public function send_magic_link( string $username_or_email_address, ?string $url
// Add a marker for later logging use of the email.
$autologin_url = add_query_arg( array( 'magic' => 'true' ), $autologin_url );

/**
* Short-circuit email sending.
*
* @param bool $send_email Whether to send the email.
* @param string $autologin_url The URL which will log the user in.
* @param WP_User $wp_user The user to send the email to.
* @param int $expires_in The number of seconds the link will be valid for.
*/
$send_email = apply_filters( 'bh_wp_autologin_urls_send_magic_link_email', true, $autologin_url, $wp_user, $expires_in );

if ( ! $send_email ) {
$result['success'] = true;
return $result;
}

/**
* Allow overriding the email template.
*
Expand Down
19 changes: 10 additions & 9 deletions src/wp-includes/class-wp-mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,18 @@ public function __construct( API_Interface $api, Settings_Interface $settings )
*/
public function add_autologin_links_to_email( array $wp_mail_args ): array {

$to = $wp_mail_args['to'];

if ( is_array( $to ) && count( $to ) !== 1 ) {
return $wp_mail_args;
}

if ( is_array( $to ) ) {
$to = array_pop( $to );
switch ( true ) {
case is_string( $wp_mail_args['to'] ):
$to = $wp_mail_args['to'];
break;
case is_array( $wp_mail_args['to'] ) && count( $wp_mail_args['to'] ) === 1:
$to = $wp_mail_args['to'][ array_key_first( $wp_mail_args['to'] ) ];
break;
default:
return $wp_mail_args;
}

$user = get_user_by( 'email', $wp_mail_args['to'] );
$user = get_user_by( 'email', $to );

// If the email recipient does not have a user account on this site, return the message unchanged.
if ( ! $user ) {
Expand Down
3 changes: 0 additions & 3 deletions tests/e2e-pw/setup/initialize-external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,3 @@ npx wp-env run tests-cli ./setup/initialize-internal-tests.sh;


# npx wp-env run tests-cli "wp plugin activate woocommerce";

TEST_DB_PORT=$(docker ps -f ancestor=mariadb -f name=tests-mysql --format='{{.Ports}}' | sed -E 's/.*:(.*)->.*/\1/')
find . -depth \( -name '.env.testing' \) -exec sed -i '' "s/TEST_DB_PORT=\".*\"/TEST_DB_PORT=\"$TEST_DB_PORT\"/g" {} +
35 changes: 35 additions & 0 deletions tests/unit/wp-includes/class-wp-mail-unit-Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,39 @@ public function test_do_not_add_to_bcc(): void {

$this->assertEquals( $email_args, $filtered_email_args );
}

/**
* @covers ::add_autologin_links_to_email
*/
public function test_handles_array_of_wp_mail_args_to(): void {

$settings_mock = $this->createMock( Settings_Interface::class );

$api_mock = $this->makeEmpty(
API_Interface::class,
array(
'add_autologin_to_message' => Expected::never(),
)
);

$wp_mail = new WP_Mail( $api_mock, $settings_mock );

$email_args = array(
'to' => array( '[email protected]' ),
'subject' => 'to array test',
'message' => 'non-matching',
);

$user = $this->createMock( '\WP_User' );
$user->method( 'has_cap' )->with( 'administrator' )->willReturn( false );

\WP_Mock::userFunction( 'get_user_by' )
->with( 'email', '[email protected]' )
->once()
->andReturn( false );

$result = $wp_mail->add_autologin_links_to_email( $email_args );

$this->assertEquals( $email_args, $result );
}
}

0 comments on commit fb14731

Please sign in to comment.