Skip to content

Commit

Permalink
Remove audio & video methods from post
Browse files Browse the repository at this point in the history
Those methods do not really make sense. Since `Post` is kind of a DTO object, it should not extract html content from `post_content`.
  • Loading branch information
nlemoine committed May 26, 2023
1 parent 3d6bb88 commit 0847681
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 112 deletions.
44 changes: 0 additions & 44 deletions src/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -1892,50 +1892,6 @@ public function gallery($html = true)
return apply_filters('get_post_gallery', $gallery, $this->ID, $galleries);
}

/**
* Returns audio tags embedded in the post’s content.
*
* @api
* @example
* ```twig
* {{ post.audio }}
* ```
* @return bool|array A list of found HTML embeds.
*/
public function audio()
{
$audio = false;

// Only get audio from the content if a playlist isn’t present.
if (!str_contains($this->content(), 'wp-playlist-script')) {
$audio = get_media_embedded_in_content($this->content(), ['audio']);
}

return $audio;
}

/**
* Returns video tags embedded in the post’s content.
*
* @api
* @example
* ```twig
* {{ post.video }}
* ```
* @return bool|array A list of found HTML embeds.
*/
public function video()
{
$video = false;

// Only get video from the content if a playlist isn't present.
if (!str_contains($this->content(), 'wp-playlist-script')) {
$video = get_media_embedded_in_content($this->content(), ['video', 'object', 'embed', 'iframe']);
}

return $video;
}

protected function get_entity_name()
{
return 'post';
Expand Down
68 changes: 0 additions & 68 deletions tests/test-timber-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -1075,74 +1075,6 @@ public function testPostWithoutGallery()
$this->assertSame(false, $post->gallery());
}

public function testPostWithoutAudio()
{
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);

$this->assertEquals([], $post->audio());
}

public function testPostWithAudio()
{
$quote = 'Named must your fear be before banish it you can.';
$quote .= '[embed]http://www.noiseaddicts.com/samples_1w72b820/280.mp3[/embed]';
$quote .= "No, try not. Do or do not. There is no try.";

$pid = $this->factory->post->create([
'post_content' => $quote,
]);
$post = Timber::get_post($pid);
$expected = 'http://www.noiseaddicts.com/samples_1w72b820/280.mp3';

$this->assertStringContainsString($expected, $post->audio()[0]);
$this->assertStringStartsWith('<audio', $post->audio()[0]);
}

public function testPostWithAudioCustomField()
{
$quote = 'Named must your fear be before banish it you can.';
$quote .= '[embed]http://www.noiseaddicts.com/samples_1w72b820/280.mp3[/embed]';
$quote .= "No, try not. Do or do not. There is no try.";

$pid = $this->factory->post->create([
'post_content' => $quote,
]);
update_post_meta($pid, 'audio', 'foo');
$expected = 'http://www.noiseaddicts.com/samples_1w72b820/280.mp3';
$post = Timber::get_post($pid);
$this->assertStringContainsString($expected, $post->audio()[0]);
$this->assertStringStartsWith('<audio', $post->audio()[0]);
}

public function testPostWithoutVideo()
{
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);

$this->assertEquals([], $post->video());
}

public function testPostWithVideo()
{
$quote = 'Named must your fear be before banish it you can.';
$quote .= '[embed]https://www.youtube.com/watch?v=Jf37RalsnEs[/embed]';
$quote .= "No, try not. Do or do not. There is no try.";

$pid = $this->factory->post->create([
'post_content' => $quote,
]);
$post = Timber::get_post($pid);

$video = $post->video();
if (is_array($video)) {
$video = array_shift($video);
}
$expected = '/<iframe [^>]+ src="https:\/\/www\.youtube\.com\/embed\/Jf37RalsnEs\?feature=oembed" [^>]+>/i';
$this->assertMatchesRegularExpression($expected, $video);
;
}

public function testPathAndLinkWithPort()
{
/* setUp */
Expand Down

0 comments on commit 0847681

Please sign in to comment.