Skip to content

Commit

Permalink
Merge branch 'trunk' into implement/844
Browse files Browse the repository at this point in the history
  • Loading branch information
pfefferle authored Dec 19, 2024
2 parents 1215118 + b2e14e9 commit 7438c7d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* `@mentions` in the JSON representation of the reply

### Improved

* Direct Messages: Improve HTML to e-mail text conversion

## [4.5.1] - 2024-12-18

### Improved
Expand Down
9 changes: 8 additions & 1 deletion includes/class-mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,17 @@ public static function direct_message( $activity, $user_id ) {
$email = $user->user_email;
}

$content = \html_entity_decode(
\wp_strip_all_tags(
str_replace( '</p>', PHP_EOL . PHP_EOL, $activity['object']['content'] )
),
ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401
);

/* translators: 1: Blog name, 2 Actor name */
$subject = \sprintf( \esc_html__( '[%1$s] Direct Message from: %2$s', 'activitypub' ), \esc_html( get_option( 'blogname' ) ), \esc_html( $actor['name'] ) );
/* translators: 1: Blog name, 2: Actor name */
$message = \sprintf( \esc_html__( 'New Direct Message: %2$s', 'activitypub' ), \esc_html( get_option( 'blogname' ) ), \wp_strip_all_tags( $activity['object']['content'] ) ) . "\r\n\r\n";
$message = \sprintf( \esc_html__( 'New Direct Message: %2$s', 'activitypub' ), \esc_html( get_option( 'blogname' ) ), $content ) . "\r\n\r\n";
/* translators: Actor name */
$message .= \sprintf( \esc_html__( 'From: %s', 'activitypub' ), \esc_html( $actor['name'] ) ) . "\r\n";
/* translators: Actor URL */
Expand Down
4 changes: 4 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ For reasons of data protection, it is not possible to see the followers of other

== Changelog ==

= Unreleased =

* Improved: HTML to e-mail text conversion

= 4.5.1 =

* Added: `@mentions` in the JSON representation of the reply
Expand Down
66 changes: 66 additions & 0 deletions tests/includes/class-test-mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,70 @@ function ( $args ) {
remove_all_filters( 'wp_mail' );
wp_delete_user( $user_id );
}

/**
* Data provider for direct message notification text.
*
* @return array
*/
public function direct_message_text_provider() {
return array(
'HTML entities' => array(
json_decode( '"<p>Interesting story from <span class=\"h-card\" translate=\"no\"><a href=\"https:\/\/example.com\/@test\" class=\"u-url mention\">@<span>test<\/span><\/a><\/span> about people who don&#39;t own their own domain.<\/p><p>&quot;This is not a new issue, of course, but Service\u2019s implementation shows limitations.&quot;<\/p>"' ),
'Interesting story from @test about people who don\'t own their own domain.' . PHP_EOL . PHP_EOL . '"This is not a new issue, of course, but Service’s implementation shows limitations."',
),
'invalid HTML' => array(
json_decode( '"<ptest"' ),
'',
),
);
}

/**
* Test direct message notification text.
*
* @param string $text Text to test.
* @param string $expected Expected result.
*
* @covers ::direct_message
* @dataProvider direct_message_text_provider
*/
public function test_direct_message_text( $text, $expected ) {
$user_id = self::$user_id;

$activity = array(
'actor' => 'https://example.com/author',
'object' => array(
'content' => $text,
),
);

// Mock remote metadata.
add_filter(
'pre_get_remote_metadata_by_actor',
function () {
return array(
'name' => 'Test Sender',
'url' => 'https://example.com/author',
);
}
);

// Capture email.
add_filter(
'wp_mail',
function ( $args ) use ( $expected, $user_id ) {
$this->assertStringContainsString( $expected, $args['message'] );
$this->assertEquals( get_user_by( 'id', $user_id )->user_email, $args['to'] );
return $args;
}
);

Mailer::direct_message( $activity, $user_id );

// Clean up.
remove_all_filters( 'pre_get_remote_metadata_by_actor' );
remove_all_filters( 'wp_mail' );
wp_delete_user( $user_id );
}
}

0 comments on commit 7438c7d

Please sign in to comment.