Skip to content

Commit

Permalink
Add Podcast Support (#386)
Browse files Browse the repository at this point in the history
* Podcast test

* wip

* Add audio player to podcast posts

* Use the enclosure's type and length if available

* Only add the audio player if the post content doesn't have it already
  • Loading branch information
akirk authored Nov 5, 2024
1 parent d3e0e98 commit c2b2c6f
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
12 changes: 12 additions & 0 deletions feed-parsers/class-feed-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ public function __set( $key, $value ) {
$value = $this->validate_date( $value, 'invalid-updated-date' );
break;

case 'enclosure':
if ( ! is_array( $value ) ) {
return new \WP_Error( 'invalid-enclosure', 'This value cannot be stored in a enclosure.' );
}

if ( ! isset( $value['url'] ) || ! $this->validate_url( $value['url'], 'invalid-enclosure-url' ) ) {
return new \WP_Error( 'invalid-enclosure-url', 'The enclosure URL is invalid.' );
}

$this->data['meta']['enclosure'] = $value;
break;

// Internal.
case '_feed_rule_delete':
$value = boolval( $value );
Expand Down
45 changes: 45 additions & 0 deletions feed-parsers/class-feed-parser-simplepie.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function __construct( Feed $friends_feed ) {

\add_filter( 'friends_get_comments', array( $this, 'get_comments' ), 10, 4 );
\add_filter( 'friends_no_comments_feed_available', array( $this, 'no_comments_feed_available' ), 10, 2 );
\add_filter( 'friends_modify_feed_item', array( $this, 'podcast_support' ), 10, 4 );
}

/**
Expand Down Expand Up @@ -331,6 +332,20 @@ public function process_items( $items, $url ) {
}
}

foreach ( $item->get_enclosures() as $enclosure ) {
if ( ! isset( $enclosure->link ) ) {
continue;
}

$feed_item->enclosure = array_filter(
array(
'url' => $enclosure->get_link(),
'type' => $enclosure->get_type(),
'length' => $enclosure->get_length(),
)
);
}

if ( is_object( $item->get_author() ) ) {
$feed_item->author = \wp_strip_all_tags( $item->get_author()->name );
}
Expand Down Expand Up @@ -403,4 +418,34 @@ public function get_comments( $comments, $post_id, User $friend_user = null, Use

return array_merge( $comments, $items );
}

public function podcast_support( $item, $user_feed, $friend_user, $post_id ) {
if (
isset( $item->enclosure['url'] )
&& false === stripos( $item->post_content, '<audio' )
&& false === stripos( $item->post_content, $item->enclosure['url'] )
) {
$audio_block = '<!-- wp:audio -->';
$audio_block .= PHP_EOL;
$audio_block .= '<figure class="wp-block-audio"><audio controls src="';
$audio_block .= esc_url( $item->enclosure['url'] );
if ( isset( $item->enclosure['type'] ) ) {
$audio_block .= '" type="';
$audio_block .= esc_attr( $item->enclosure['type'] );
}
if ( isset( $item->enclosure['length'] ) ) {
$audio_block .= '" length="';
$audio_block .= esc_attr( $item->enclosure['length'] );
}
$audio_block .= '"></audio></figure>';
$audio_block .= PHP_EOL;
$audio_block .= '<!-- /wp:audio -->';
$audio_block .= PHP_EOL;
$audio_block .= PHP_EOL;

$item->post_content = $audio_block . $item->post_content;

}
return $item;
}
}
14 changes: 14 additions & 0 deletions tests/data/podcast-feed.rss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>podcast.local</title>
<link>http://podcast.local</link>
<description>My podcast</description>
<item>
<title>First Episode</title>
<link>https://podcast.local/2022/10/episode-1/</link>
<description>This is a public post.</description>
<enclosure url="http://podcast.local/wp-content/uploads/2024/10/first-episode.mp3" length="123456" type="audio/mpeg" />
</item>
</channel>
</rss>
4 changes: 2 additions & 2 deletions tests/test-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public function test_friend_mentions() {
$object->get_tag()
);

$this->assertContains( \get_rest_url( null, '/activitypub/1.0/users/1/followers' ), $object->get_to() );
$this->assertContains( \get_rest_url( null, '/activitypub/1.0/users/1/followers' ), $object->get_cc() );
$this->assertContains( $this->actor, $object->get_cc() );

remove_all_filters( 'activitypub_from_post_object' );
Expand Down Expand Up @@ -313,7 +313,7 @@ public function test_dont_override_activitypub_mentions() {
$object->get_tag()
);

$this->assertContains( \get_rest_url( null, '/activitypub/1.0/users/1/followers' ), $object->get_to() );
$this->assertContains( \get_rest_url( null, '/activitypub/1.0/users/1/followers' ), $object->get_cc() );
$this->assertNotContains( $this->actor, $object->get_cc() );

remove_all_filters( 'activitypub_from_post_object' );
Expand Down
14 changes: 14 additions & 0 deletions tests/test-feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,4 +664,18 @@ public function test_global_retention_count() {
$count = wp_count_posts( Friends::CPT );
$this->assertEquals( 5, $count->publish );
}

public function test_podcast() {
$podcast = __DIR__ . '/data/podcast-feed.rss';
$feed_parsing_test = $this->feed_parsing_test( $podcast );

$new_items = $feed_parsing_test->current();
$this->assertCount( 1, $new_items );
$post_id = $new_items[0];

$post = get_post( $post_id );

$this->assertEquals( 'https://podcast.local/2022/10/episode-1/', $post->guid );
$this->assertStringContainsString( 'first-episode.mp3', $post->post_content );
}
}

0 comments on commit c2b2c6f

Please sign in to comment.