Skip to content

Commit 125bb7f

Browse files
committed
Guard iframe rendering for oEmbed-only providers
1 parent cc60a23 commit 125bb7f

5 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎docs/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,8 @@ $html = $mediaEmbed->oEmbedHtml($mediaObject);
610610
echo $html;
611611
```
612612

613+
Calling the iframe-specific `MediaObject::getEmbedCode()` or `getEmbedSrc()` methods for an oEmbed-only provider throws `EmbedCodeUnavailableException` with this alternative. This avoids silently returning unusable markup.
614+
613615
Custom oEmbed-only providers can be registered inline without modifying the bundled provider file:
614616

615617
```php
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MediaEmbed\Exception;
6+
7+
/**
8+
* Exception thrown when a provider has no static iframe renderer.
9+
*/
10+
class EmbedCodeUnavailableException extends MediaEmbedException {
11+
12+
/**
13+
* @param string $providerSlug Provider slug.
14+
*/
15+
public function __construct(string $providerSlug) {
16+
parent::__construct(sprintf(
17+
'Provider "%s" has no iframe embed. Use MediaEmbed::oEmbedHtml() for oEmbed-only providers.',
18+
$providerSlug,
19+
));
20+
}
21+
22+
}

‎src/Object/MediaObject.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace MediaEmbed\Object;
66

77
use InvalidArgumentException;
8+
use MediaEmbed\Exception\EmbedCodeUnavailableException;
89
use MediaEmbed\Template\TemplateResolver;
910

1011
/**
@@ -549,6 +550,9 @@ public function isSourceResolved(): bool {
549550
* @return string The unescaped src URL
550551
*/
551552
public function getEmbedSrc(): string {
553+
if (empty($this->stub['iframe-player'])) {
554+
throw new EmbedCodeUnavailableException($this->slug());
555+
}
552556
$source = $this->templateResolver->resolve($this->stub['iframe-player'], $this->match);
553557

554558
return $this->appendQueryParams($source);

‎tests/MediaEmbedTest.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MediaEmbed\Test;
44

55
use InvalidArgumentException;
6+
use MediaEmbed\Exception\EmbedCodeUnavailableException;
67
use MediaEmbed\Exception\InvalidUrlException;
78
use MediaEmbed\Exception\ProviderConfigException;
89
use MediaEmbed\Http\HttpClientInterface;
@@ -617,6 +618,16 @@ public function testOEmbedHtmlReturnsNullWithoutHtml(): void {
617618
$this->assertNull($MediaEmbed->oEmbedHtml($Object));
618619
}
619620

621+
public function testOEmbedOnlyProviderRejectsIframeRenderingWithActionableException(): void {
622+
$MediaEmbed = new MediaEmbed();
623+
$Object = $MediaEmbed->parseUrl('https://bsky.app/profile/bsky.app/post/3mbhel6ij7s2y');
624+
$this->assertInstanceOf(MediaObject::class, $Object);
625+
626+
$this->expectException(EmbedCodeUnavailableException::class);
627+
$this->expectExceptionMessage('Use MediaEmbed::oEmbedHtml()');
628+
$Object->getEmbedCode();
629+
}
630+
620631
public function testOEmbedReturnsNullWithoutRegisteredEndpoint(): void {
621632
$MediaEmbed = new MediaEmbed();
622633
$Object = $MediaEmbed->parseUrl('https://my.matterport.com/show/?m=Zh14WDtkjdC');

‎tests/ProviderFixtureTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public function get(string $url, array $options = []): ?string {
4444
$this->assertInstanceOf(MediaObject::class, $object);
4545
$this->assertSame($fixture['slug'], $object->slug());
4646
$this->assertSame($fixture['id'], $object->id());
47+
$this->assertTrue(
48+
isset($fixture['embedSrc']) || isset($fixture['oembedEndpoint']),
49+
'Provider fixtures must verify an iframe source or oEmbed endpoint.',
50+
);
4751
if (isset($fixture['embedSrc'])) {
4852
$this->assertSame($fixture['embedSrc'], $object->getEmbedSrc());
4953
}

0 commit comments

Comments
 (0)