Skip to content

Commit ffae98e

Browse files
obenlandpfefferle
andauthored
Embed: Accept Document and Image types (#1699)
Co-authored-by: Matthias Pfefferle <[email protected]>
1 parent 36bada3 commit ffae98e

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

includes/class-embed.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static function get_html_for_object( $activity_object, $inline_css = true
8181
$favorites = isset( $activity_object['likes']['totalItems'] ) ? (int) $activity_object['likes']['totalItems'] : null;
8282

8383
$audio = null;
84-
$images = null;
84+
$images = array();
8585
$video = null;
8686
if ( isset( $activity_object['image']['url'] ) ) {
8787
$images = array(
@@ -97,9 +97,8 @@ public static function get_html_for_object( $activity_object, $inline_css = true
9797

9898
switch ( $type ) {
9999
case 'image':
100-
$images = \wp_list_filter( $activity_object['attachment'], array( 'type' => 'Image' ) );
101-
$images = array_slice( $images, 0, 4 );
102-
break 2;
100+
$images[] = $attachment;
101+
break;
103102
case 'video':
104103
$video = $attachment;
105104
break 2;
@@ -108,6 +107,7 @@ public static function get_html_for_object( $activity_object, $inline_css = true
108107
break 2;
109108
}
110109
}
110+
$images = \array_slice( $images, 0, 4 );
111111
}
112112

113113
ob_start();

tests/includes/class-test-embed.php

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@
55
* @package ActivityPub
66
*/
77

8-
namespace ActivityPub;
8+
namespace Activitypub\Tests;
99

10-
use PHPUnit\Framework\TestCase;
10+
use Activitypub\Embed;
1111

1212
/**
1313
* Test the Embed class.
14+
*
15+
* @coversDefaultClass \ActivityPub\Embed
1416
*/
15-
class Test_Embed extends TestCase {
17+
class Test_Embed extends \WP_UnitTestCase {
1618
/**
1719
* Test the has_real_oembed method with a URL that has a real oEmbed.
20+
*
21+
* @covers ::has_real_oembed
1822
*/
1923
public function test_has_real_oembed_with_real_oembed() {
2024
// Define the filter function.
@@ -36,6 +40,8 @@ public function test_has_real_oembed_with_real_oembed() {
3640

3741
/**
3842
* Test the has_real_oembed method with a URL that doesn't have a real oEmbed.
43+
*
44+
* @covers ::has_real_oembed
3945
*/
4046
public function test_has_real_oembed_without_real_oembed() {
4147
// Add our filter.
@@ -52,6 +58,8 @@ public function test_has_real_oembed_without_real_oembed() {
5258

5359
/**
5460
* Test the maybe_use_activitypub_embed method when a result is already provided.
61+
*
62+
* @covers ::maybe_use_activitypub_embed
5563
*/
5664
public function test_maybe_use_activitypub_embed_with_result() {
5765
// Call the method with a non-null result.
@@ -62,6 +70,8 @@ public function test_maybe_use_activitypub_embed_with_result() {
6270

6371
/**
6472
* Test the maybe_use_activitypub_embed method when no result is provided but a real oEmbed is found.
73+
*
74+
* @covers ::maybe_use_activitypub_embed
6575
*/
6676
public function test_maybe_use_activitypub_embed_with_real_oembed() {
6777
// Create a test double for Embed that returns true for has_real_oembed.
@@ -80,6 +90,8 @@ public function test_maybe_use_activitypub_embed_with_real_oembed() {
8090

8191
/**
8292
* Test the handle_filtered_oembed_result method when HTML is already provided.
93+
*
94+
* @covers ::handle_filtered_oembed_result
8395
*/
8496
public function test_handle_filtered_oembed_result_with_html() {
8597
// Call the method with HTML already provided.
@@ -90,6 +102,8 @@ public function test_handle_filtered_oembed_result_with_html() {
90102

91103
/**
92104
* Test the handle_filtered_oembed_result method when the data type is not rich or video.
105+
*
106+
* @covers ::handle_filtered_oembed_result
93107
*/
94108
public function test_handle_filtered_oembed_result_with_non_rich_data() {
95109
// Call the method with a non-rich data type.
@@ -106,6 +120,8 @@ public function test_handle_filtered_oembed_result_with_non_rich_data() {
106120

107121
/**
108122
* Test the handle_filtered_oembed_result method when there's no HTML in the data.
123+
*
124+
* @covers ::handle_filtered_oembed_result
109125
*/
110126
public function test_handle_filtered_oembed_result_without_html() {
111127
// Call the method with no HTML in the data.
@@ -119,4 +135,36 @@ public function test_handle_filtered_oembed_result_without_html() {
119135

120136
$this->assertEquals( '', $result );
121137
}
138+
139+
/**
140+
* Test the get_html_for_object method.
141+
*
142+
* @covers ::get_html_for_object
143+
*/
144+
public function test_get_html_for_object() {
145+
// Create a test object.
146+
$object = array(
147+
'id' => 'https://example.com/post',
148+
'url' => 'https://example.com/post',
149+
'content' => 'This is a test post.',
150+
'attachment' => array(
151+
array(
152+
'type' => 'Document',
153+
'url' => 'https://example.com/image1.jpg',
154+
'mediaType' => 'image/jpeg',
155+
),
156+
array(
157+
'type' => 'Image',
158+
'url' => 'https://example.com/image2.jpg',
159+
'mediaType' => 'image/jpeg',
160+
),
161+
),
162+
);
163+
164+
// Call the method.
165+
$result = Embed::get_html_for_object( $object );
166+
167+
$this->assertStringContainsString( 'https://example.com/image1.jpg', $result );
168+
$this->assertStringContainsString( 'https://example.com/image2.jpg', $result );
169+
}
122170
}

0 commit comments

Comments
 (0)