diff --git a/composer.json b/composer.json index 604548b23..0a9ffcd8a 100644 --- a/composer.json +++ b/composer.json @@ -44,6 +44,6 @@ "test": "phpunit --exclude-group ''" }, "extra": { - "version": "2.17.4-dev" + "version": "2.18.0-dev" } } diff --git a/docs/Internals/API_changes.md b/docs/Internals/API_changes.md index 2f9b0a210..d33afd3f2 100644 --- a/docs/Internals/API_changes.md +++ b/docs/Internals/API_changes.md @@ -1,6 +1,20 @@ See also [general changes](Changes.md). +## 2.18.0 + +The following classes have moved namespace and are (silently) deprecated: + + - s9e\TextFormatter\Plugins\MediaEmbed\Configurator\AbstractConfigurableHostHelper + - s9e\TextFormatter\Plugins\MediaEmbed\Configurator\MastodonHelper + - s9e\TextFormatter\Plugins\MediaEmbed\Configurator\XenForoHelper + +The preferred way to access helpers is through the plugin's configurator. For example: + + - `$configurator->MediaEmbed->getSiteHelper('mastodon')` + - `$configurator->MediaEmbed->getSiteHelper('xenforo')` + + ## 2.2.0 The following methods are silently deprecated and will be removed in the next major release: diff --git a/docs/Plugins/MediaEmbed/Federated_sites.md b/docs/Plugins/MediaEmbed/Federated_sites.md index 935d343b3..a206ded6a 100644 --- a/docs/Plugins/MediaEmbed/Federated_sites.md +++ b/docs/Plugins/MediaEmbed/Federated_sites.md @@ -7,11 +7,8 @@ The default Mastodon media site can be customized with additional hosts. This ca ```php $configurator = new s9e\TextFormatter\Configurator; -// Add the Mastodon media site -$configurator->MediaEmbed->add('mastodon'); - -// Use MastodonHelper to add 'infosec.exchange' as a supported instance -$mastodonHelper = new s9e\TextFormatter\Plugins\MediaEmbed\Configurator\MastodonHelper($configurator); +// Use the Mastodon helper to add 'infosec.exchange' as a supported instance +$mastodonHelper = $configurator->MediaEmbed->getSiteHelper('mastodon'); $mastodonHelper->addHost('infosec.exchange'); // Get an instance of the parser and the renderer @@ -36,11 +33,8 @@ While not technically a federated platform, XenForo 2.3+ allows embedding conten ```php $configurator = new s9e\TextFormatter\Configurator; -// Add the Mastodon media site -$configurator->MediaEmbed->add('xenforo'); - -// Use XenForoHelper to add 'xenforo.com' as an authorized source -$xenforoHelper = new s9e\TextFormatter\Plugins\MediaEmbed\Configurator\XenForoHelper($configurator); +// Use the XenForo helper to add 'xenforo.com' as an authorized source +$xenforoHelper = $configurator->MediaEmbed->getSiteHelper('xenforo'); $xenforoHelper->addHost('xenforo.com'); // Get an instance of the parser and the renderer diff --git a/docs/testdox.txt b/docs/testdox.txt index 64cf2b461..439715f54 100644 --- a/docs/testdox.txt +++ b/docs/testdox.txt @@ -6757,9 +6757,13 @@ Xml File Definition Collection (s9e\TextFormatter\Tests\Plugins\MediaEmbed\Confi [x] Other default attribute values are left as strings [x] Attributes' "required" property is cast to bool -Mastodon Helper (s9e\TextFormatter\Tests\Plugins\MediaEmbed\Configurator\MastodonHelper) +Mastodon Helper (s9e\TextFormatter\Tests\Plugins\MediaEmbed\Configurator\SiteHelpers\MastodonHelper) [x] addHost() normalizes the host [x] addHost() adds the Mastodon media site if it's not enabled yet + [x] setHosts() resets previously allowed hosts + +Xen Foro Helper (s9e\TextFormatter\Tests\Plugins\MediaEmbed\Configurator\SiteHelpers\XenForoHelper) + [x] addHost() adds the XenForo media site if it's not enabled yet Template Builder (s9e\TextFormatter\Tests\Plugins\MediaEmbed\Configurator\TemplateBuilder) [x] getTemplate() returns an empty string by default @@ -6814,6 +6818,7 @@ Configurator (s9e\TextFormatter\Tests\Plugins\MediaEmbed\Configurator) [x] asConfig() returns a an array containing a "regexp" element by default, if any site was added [x] asConfig() creates a regexp if a site has a "host" [x] asConfig() returns '://' as quickMatch + [x] getSiteHelper('mastodon') returns the Mastodon helper Parser (s9e\TextFormatter\Tests\Plugins\MediaEmbed\Parser) [x] The MEDIA tag can be effectively disabled diff --git a/src/Plugins/MediaEmbed/Configurator.php b/src/Plugins/MediaEmbed/Configurator.php index 33547c3ef..91019a1c5 100644 --- a/src/Plugins/MediaEmbed/Configurator.php +++ b/src/Plugins/MediaEmbed/Configurator.php @@ -15,6 +15,7 @@ use s9e\TextFormatter\Configurator\JavaScript\Dictionary; use s9e\TextFormatter\Plugins\ConfiguratorBase; use s9e\TextFormatter\Plugins\MediaEmbed\Configurator\Collections\CachedDefinitionCollection; +use s9e\TextFormatter\Plugins\MediaEmbed\Configurator\SiteHelpers\AbstractSiteHelper; use s9e\TextFormatter\Plugins\MediaEmbed\Configurator\TemplateBuilder; class Configurator extends ConfiguratorBase @@ -136,6 +137,13 @@ public function add($siteId, ?array $siteConfig = null) return $tag; } + public function getSiteHelper(string $siteId): AbstractSiteHelper + { + $className = $this->defaultSites->get($siteId)['helper']; + + return new $className($this->configurator); + } + /** * Return the list of configured sites * diff --git a/src/Plugins/MediaEmbed/Configurator/AbstractConfigurableHostHelper.php b/src/Plugins/MediaEmbed/Configurator/AbstractConfigurableHostHelper.php index e9f934e29..a1c882f7d 100644 --- a/src/Plugins/MediaEmbed/Configurator/AbstractConfigurableHostHelper.php +++ b/src/Plugins/MediaEmbed/Configurator/AbstractConfigurableHostHelper.php @@ -7,32 +7,8 @@ */ namespace s9e\TextFormatter\Plugins\MediaEmbed\Configurator; -use function strtolower; -use s9e\TextFormatter\Configurator; +use s9e\TextFormatter\Plugins\MediaEmbed\Configurator\SiteHelpers\AbstractConfigurableHostHelper as ParentClass; -abstract class AbstractConfigurableHostHelper +abstract class AbstractConfigurableHostHelper extends ParentClass { - /** - * @var Configurator - */ - protected Configurator $configurator; - - public function __construct(Configurator $configurator) - { - $this->configurator = $configurator; - } - - public function addHost(string $host): void - { - $siteId = $this->getSiteId(); - if (!isset($this->configurator->registeredVars['MediaEmbed.sites'][$siteId])) - { - $this->configurator->MediaEmbed->add($siteId); - } - - $host = strtolower($host); - $this->configurator->registeredVars['MediaEmbed.hosts'][$host] = $siteId; - } - - abstract protected function getSiteId(): string; } \ No newline at end of file diff --git a/src/Plugins/MediaEmbed/Configurator/Collections/CachedDefinitionCollection.php b/src/Plugins/MediaEmbed/Configurator/Collections/CachedDefinitionCollection.php index ad8cfca1b..cf1b54005 100644 --- a/src/Plugins/MediaEmbed/Configurator/Collections/CachedDefinitionCollection.php +++ b/src/Plugins/MediaEmbed/Configurator/Collections/CachedDefinitionCollection.php @@ -71,7 +71,7 @@ class CachedDefinitionCollection extends SiteDefinitionCollection 'liveleak'=>['attributes'=>[],'example'=>'https://www.liveleak.com/view?t=yIcw_1520190567','extract'=>['!liveleak\\.com/(?:e/|view\\?i=)(?\'id\'\\w+)!'],'homepage'=>'https://www.liveleak.com/','host'=>['liveleak.com'],'iframe'=>['src'=>'//www.liveleak.com/e/{@id}'],'name'=>'Liveleak','scrape'=>[['extract'=>['!liveleak\\.com/e/(?\'id\'\\w+)!'],'match'=>['!liveleak\\.com/view\\?t=!']]],'tags'=>['videos']], 'livestream'=>['attributes'=>[],'example'=>['https://new.livestream.com/jbtvlive/musicmarathon','https://livestream.com/ccscsl/USChessChampionships/videos/83267610','https://livestre.am/58XNV'],'extract'=>['!livestream\\.com/accounts/(?\'account_id\'\\d+)/events/(?\'event_id\'\\d+)!','!/videos/(?\'video_id\'\\d+)!','!original\\.livestream\\.com/(?\'channel\'\\w+)/video\\?clipId=(?\'clip_id\'[-\\w]+)!'],'homepage'=>'https://new.livestream.com/','host'=>['livestre.am','livestream.com'],'iframe'=>['src'=>'//cdn.livestream.com/embed/?layout=4&autoplay=false&clip=livestream.com/accounts//events//videos//player?autoPlay=false'],'name'=>'Livestream','scrape'=>[['extract'=>['!accounts/(?\'account_id\'\\d+)/events/(?\'event_id\'\\d+)!'],'match'=>['@livestream\\.com/(?!accounts/\\d+/events/\\d)@']],['extract'=>['!//original\\.livestream\\.com/(?\'channel\'\\w+)/video/(?\'clip_id\'[-\\w]+)!'],'match'=>['!livestre.am!']]],'tags'=>['livestreaming','videos']], 'mailru'=>['attributes'=>[],'example'=>['https://my.mail.ru/corp/auto/video/testdrive/34.html','https://my.mail.ru/mail/alenka1957/video/1/7.html'],'extract'=>[],'homepage'=>'https://my.mail.ru/','host'=>['my.mail.ru'],'iframe'=>['src'=>'https://my.mail.ru/video/embed/{@id}'],'name'=>'Mail.Ru','scrape'=>[['extract'=>['!"itemId": ?"?(?\'id\'\\d+)!'],'match'=>['!my\\.mail\\.ru/\\w+/\\w+/video/\\w+/\\d!']]],'tags'=>['.ru']], - 'mastodon'=>['attributes'=>['host'=>['required'=>true]],'example'=>'https://mastodon.social/@HackerNewsBot/100181134752056592','extract'=>['#//(?\'host\'[-.\\w]+)/(?:web/)?(?:@|users/)(?\'name\'\\w+)/(?:posts/|statuses/)?(?\'id\'\\d+)#'],'homepage'=>'https://mastodon.social/','host'=>['mastodon.social'],'iframe'=>['data-s9e-livepreview-ignore-attrs'=>'style','height'=>300,'onload'=>'let c=new MessageChannel;c.port1.onmessage=e=>this.style.height=e.data+\'px\';this.contentWindow.postMessage(\'s9e:init\',\'*\',[c.port2])','src'=>'https://s9e.github.io/iframe/2/mastodon.min.html#@/','width'=>550],'name'=>'Mastodon','oembed'=>['endpoint'=>'https://mastodon.social/api/oembed','scheme'=>'https://mastodon.social/@{@name}/{@id}'],'scrape'=>[['extract'=>['#"url":"https://(?\'host\'[-.\\w]+)/@(?\'name\'\\w+)/(?\'id\'\\d+)"#'],'match'=>['#^(?\'origin\'https://[^/]+)/(?:web/)?(?:@\\w+@[-.\\w]+|statuses)/(?\'id\'\\d+)#'],'url'=>'{@origin}/api/v1/statuses/{@id}']],'tags'=>['social']], + 'mastodon'=>['attributes'=>['host'=>['required'=>true]],'example'=>'https://mastodon.social/@HackerNewsBot/100181134752056592','extract'=>['#//(?\'host\'[-.\\w]+)/(?:web/)?(?:@|users/)(?\'name\'\\w+)/(?:posts/|statuses/)?(?\'id\'\\d+)#'],'helper'=>'s9e\\TextFormatter\\Plugins\\MediaEmbed\\Configurator\\SiteHelpers\\MastodonHelper','homepage'=>'https://mastodon.social/','host'=>['mastodon.social'],'iframe'=>['data-s9e-livepreview-ignore-attrs'=>'style','height'=>300,'onload'=>'let c=new MessageChannel;c.port1.onmessage=e=>this.style.height=e.data+\'px\';this.contentWindow.postMessage(\'s9e:init\',\'*\',[c.port2])','src'=>'https://s9e.github.io/iframe/2/mastodon.min.html#@/','width'=>550],'name'=>'Mastodon','oembed'=>['endpoint'=>'https://mastodon.social/api/oembed','scheme'=>'https://mastodon.social/@{@name}/{@id}'],'scrape'=>[['extract'=>['#"url":"https://(?\'host\'[-.\\w]+)/@(?\'name\'\\w+)/(?\'id\'\\d+)"#'],'match'=>['#^(?\'origin\'https://[^/]+)/(?:web/)?(?:@\\w+@[-.\\w]+|statuses)/(?\'id\'\\d+)#'],'url'=>'{@origin}/api/v1/statuses/{@id}']],'tags'=>['social']], 'medium'=>['attributes'=>[],'example'=>'https://medium.com/@donnydonny/team-internet-is-about-to-win-net-neutrality-and-they-didnt-need-googles-help-e7e2cf9b8a95','extract'=>['#medium\\.com/(?:s/\\w+/|@?[-\\w]+/)?(?:[%\\w]+-)*(?\'id\'[0-9a-f]+)(?![%\\w])#'],'homepage'=>'https://medium.com/','host'=>['medium.com'],'iframe'=>['data-s9e-livepreview-ignore-attrs'=>'style','height'=>316,'max-width'=>900,'onload'=>'let c=new MessageChannel;c.port1.onmessage=e=>this.style.height=e.data+\'px\';this.contentWindow.postMessage(\'s9e:init\',\'*\',[c.port2])','src'=>'https://s9e.github.io/iframe/2/medium.min.html#{@id}','width'=>'100%'],'name'=>'Medium','scrape'=>[],'tags'=>['blogging']], 'megaphone'=>['amp'=>['custom-element'=>'amp-megaphone','src'=>'https://cdn.ampproject.org/v0/amp-megaphone-0.1.js','template'=>''],'attributes'=>[],'example'=>['https://cms.megaphone.fm/channel/lockedonheat?selected=LKN8165322853','https://player.megaphone.fm/LKN8165322853'],'extract'=>['@megaphone\\.fm/.*?\\?(?:e|selected)=(?\'id\'\\w+)@','@(?:dcs|player|traffic)\\.megaphone\\.fm/(?\'id\'\\w+)@','@megaphone\\.link/(?\'id\'\\w+)@'],'homepage'=>'https://megaphone.fm/','host'=>['megaphone.fm','megaphone.link'],'iframe'=>['height'=>200,'max-width'=>900,'src'=>'https://player.megaphone.fm/?light=true','width'=>'100%'],'name'=>'Megaphone','scrape'=>[],'tags'=>['podcasts']], 'metacafe'=>['attributes'=>[],'example'=>'https://www.metacafe.com/watch/10785282/chocolate_treasure_chest_epic_meal_time/','extract'=>['!metacafe\\.com/watch/(?\'id\'\\d+)!'],'homepage'=>'https://www.metacafe.com/','host'=>['metacafe.com'],'iframe'=>['src'=>'//www.metacafe.com/embed/{@id}/'],'name'=>'Metacafe','scrape'=>[],'tags'=>['videos']], @@ -139,7 +139,7 @@ class CachedDefinitionCollection extends SiteDefinitionCollection 'wsj'=>['attributes'=>[],'example'=>['https://www.wsj.com/video/nba-players-primp-with-pedicures/9E476D54-6A60-4F3F-ABC1-411014552DE6.html','https://live.wsj.com/#!09FB2B3B-583E-4284-99D8-FEF6C23BE4E2','https://live.wsj.com/video/seahawks-qb-russell-wilson-on-super-bowl-win/9B3DF790-9D20-442C-B564-51524B06FD26.html'],'extract'=>['@wsj\\.com/[^#]*#!(?\'id\'[-0-9A-F]{36})@','@wsj\\.com/video/[^/]+/(?\'id\'[-0-9A-F]{36})@'],'homepage'=>'https://www.wsj.com/video/','host'=>['wsj.com'],'iframe'=>['src'=>'//video-api.wsj.com/api-video/player/iframe.html?guid={@id}'],'name'=>'The Wall Street Journal Online','scrape'=>[['extract'=>['@wsj\\.com/video/[^/]+/(?\'id\'[-0-9A-F]{36})@'],'match'=>['@on\\.wsj\\.com/\\w@']]],'tags'=>['news']], 'xboxclips'=>['attributes'=>[],'example'=>'https://gameclips.io/boulderBaby5568/035a50fa-2d54-4820-aa44-f0f43a873308','extract'=>['@(?:gameclips\\.io|xboxclips\\.com)/(?!game/)(?\'user\'[^/]+)/(?!screenshots/)(?\'id\'[-0-9a-f]+)@'],'homepage'=>'https://gameclips.io/','host'=>['gameclips.io','xboxclips.com'],'iframe'=>['height'=>315,'src'=>'//gameclips.io/{@user}/{@id}/embed','width'=>560],'name'=>'GameClips.io','scrape'=>[],'tags'=>['gaming']], 'xboxdvr'=>['attributes'=>[],'example'=>'https://gamerdvr.com/gamer/LOXITANE/video/12463958','extract'=>['!(?:gamer|xbox)dvr\\.com/gamer/(?\'user\'[^/]+)/video/(?\'id\'\\d+)!'],'homepage'=>'https://gamerdvr.com/','host'=>['gamerdvr.com','xboxdvr.com'],'iframe'=>['src'=>'//gamerdvr.com/gamer/{@user}/video/{@id}/embed'],'name'=>'Gamer DVR','scrape'=>[],'tags'=>['gaming']], - 'xenforo'=>['attributes'=>['content_id'=>['filterChain'=>['#identifier'],'required'=>false],'post_id'=>['filterChain'=>['#uint'],'required'=>false],'profile_post_id'=>['filterChain'=>['#uint'],'required'=>false],'resource_id'=>['filterChain'=>['#uint'],'required'=>false],'thread_id'=>['filterChain'=>['#uint'],'required'=>false],'url'=>['filterChain'=>['#url'],'required'=>true],'xfmg_album_id'=>['filterChain'=>['#uint'],'required'=>false]],'example'=>'https://xenforo.com/community/threads/embed-your-content-anywhere.217381/','extract'=>['!^(?\'url\'https://.*?/)media/albums/(?:[-\\w]+\\.)?(?\'xfmg_album_id\'\\d+)!','!^(?\'url\'https://.*?/)(?:members/[-.\\w]+/#profile-post-|profile-posts/)(?\'profile_post_id\'\\d+)!','!^(?\'url\'https://.*?/)resources/(?:[-\\w]+\\.)?(?\'resource_id\'\\d+)!','!^(?\'url\'https://.*?/)threads/(?:[-\\w]+\\.)?(?\'thread_id\'\\d+)/(?:page-\\d+)?#?(?:post-(?\'post_id\'\\d+))?!','!^(?\'url\'https://.*?/)embed\\.php\\?content=(?\'content_id\'[-\\w]+)!'],'host'=>[],'iframe'=>['data-s9e-livepreview-ignore-attrs'=>'style','height'=>300,'onload'=>'let c=new MessageChannel;c.port1.onmessage=e=>this.style.height=e.data+\'px\';this.contentWindow.postMessage(\'s9e:init\',\'*\',[c.port2])','src'=>'https://s9e.github.io/iframe/2/xenforo.min.html#profile-posts/resources/media/albums/threads//post-','width'=>'100%'],'name'=>'XenForo','scrape'=>[],'tags'=>['social']], + 'xenforo'=>['attributes'=>['content_id'=>['filterChain'=>['#identifier'],'required'=>false],'post_id'=>['filterChain'=>['#uint'],'required'=>false],'profile_post_id'=>['filterChain'=>['#uint'],'required'=>false],'resource_id'=>['filterChain'=>['#uint'],'required'=>false],'thread_id'=>['filterChain'=>['#uint'],'required'=>false],'url'=>['filterChain'=>['#url'],'required'=>true],'xfmg_album_id'=>['filterChain'=>['#uint'],'required'=>false]],'example'=>'https://xenforo.com/community/threads/embed-your-content-anywhere.217381/','extract'=>['!^(?\'url\'https://.*?/)media/albums/(?:[-\\w]+\\.)?(?\'xfmg_album_id\'\\d+)!','!^(?\'url\'https://.*?/)(?:members/[-.\\w]+/#profile-post-|profile-posts/)(?\'profile_post_id\'\\d+)!','!^(?\'url\'https://.*?/)resources/(?:[-\\w]+\\.)?(?\'resource_id\'\\d+)!','!^(?\'url\'https://.*?/)threads/(?:[-\\w]+\\.)?(?\'thread_id\'\\d+)/(?:page-\\d+)?#?(?:post-(?\'post_id\'\\d+))?!','!^(?\'url\'https://.*?/)embed\\.php\\?content=(?\'content_id\'[-\\w]+)!'],'helper'=>'s9e\\TextFormatter\\Plugins\\MediaEmbed\\Configurator\\SiteHelpers\\XenForoHelper','host'=>[],'iframe'=>['data-s9e-livepreview-ignore-attrs'=>'style','height'=>300,'onload'=>'let c=new MessageChannel;c.port1.onmessage=e=>this.style.height=e.data+\'px\';this.contentWindow.postMessage(\'s9e:init\',\'*\',[c.port2])','src'=>'https://s9e.github.io/iframe/2/xenforo.min.html#profile-posts/resources/media/albums/threads//post-','width'=>'100%'],'name'=>'XenForo','scrape'=>[],'tags'=>['social']], 'youku'=>['attributes'=>[],'example'=>'https://v.youku.com/v_show/id_XMzY0NTMyMTgxMg==.html','extract'=>['!youku\\.com/v(?:_show|ideo)/id_(?\'id\'\\w+=*)!'],'homepage'=>'https://www.youku.com/','host'=>['youku.com'],'iframe'=>['src'=>'//player.youku.com/embed/{@id}'],'name'=>'Youku','scrape'=>[],'tags'=>['.cn']], 'youmaker'=>['attributes'=>[],'example'=>['https://www.youmaker.com/v/EnDXOWo8OOvQ','https://www.youmaker.com/video/b3ce8457-2cbe-4661-84ec-829fa8fe0754'],'extract'=>['!youmaker\\.com/(?:embed|v(?:ideo)?)/(?\'id\'[-a-z0-9]+)!i'],'homepage'=>'https://www.youmaker.com/','host'=>['youmaker.com'],'iframe'=>['src'=>'https://www.youmaker.com/embed/{@id}'],'name'=>'YouMaker','scrape'=>[],'tags'=>['videos']], 'youtube'=>['amp'=>['custom-element'=>'amp-youtube','src'=>'https://cdn.ampproject.org/v0/amp-youtube-0.1.js','template'=>''],'attributes'=>['id'=>['filterChain'=>['#identifier'],'required'=>false],'t'=>['filterChain'=>['#timestamp']]],'example'=>['https://www.youtube.com/watch?v=-cEzsCAzTak','https://youtu.be/-cEzsCAzTak','https://www.youtube.com/watch?feature=player_detailpage&v=jofNR_WkoCE#t=40','https://www.youtube.com/watch?v=pC35x6iIPmo&list=PLOU2XLYxmsIIxJrlMIY5vYXAFcO5g83gA'],'extract'=>['!youtube\\.com/(?:watch.*?v=|(?:embed|live|shorts|v)/|attribution_link.*?v%3D)(?\'id\'[-\\w]+)!','!youtube-nocookie\\.com/embed/(?\'id\'[-\\w]+)!','!youtu\\.be/(?\'id\'[-\\w]+)!','@[#&?]t(?:ime_continue)?=(?\'t\'\\d[\\dhms]*)@','![&?]list=(?\'list\'[-\\w]+)!'],'homepage'=>'https://www.youtube.com/','host'=>['youtube-nocookie.com','youtube.com','youtu.be'],'iframe'=>['src'=>'https://www.youtube.com/embed/?clip=&clipt=?list=&?start=','style'=>['background'=>'url(https://i.ytimg.com/vi/{@id}/hqdefault.jpg) 50% 50% / cover']],'name'=>'YouTube','oembed'=>['endpoint'=>'https://www.youtube.com/oembed','scheme'=>'https://www.youtube.com/watch?v={@id}'],'scrape'=>[['extract'=>['@/embed/(?\'id\'[-\\w]+)\\?clip=(?\'clip\'[-\\w]+)&clipt=(?\'clipt\'[-\\w]+)@'],'match'=>['@youtube\\.com/clip/.@']]],'source'=>'https://support.google.com/youtube/bin/answer.py?hl=en&answer=171780','tags'=>['livestreaming','videos']] diff --git a/src/Plugins/MediaEmbed/Configurator/MastodonHelper.php b/src/Plugins/MediaEmbed/Configurator/MastodonHelper.php index 3f8f9fd41..910a2a317 100644 --- a/src/Plugins/MediaEmbed/Configurator/MastodonHelper.php +++ b/src/Plugins/MediaEmbed/Configurator/MastodonHelper.php @@ -7,10 +7,8 @@ */ namespace s9e\TextFormatter\Plugins\MediaEmbed\Configurator; -class MastodonHelper extends AbstractConfigurableHostHelper +use s9e\TextFormatter\Plugins\MediaEmbed\Configurator\SiteHelpers\MastodonHelper as ParentClass; + +class MastodonHelper extends ParentClass { - protected function getSiteId(): string - { - return 'mastodon'; - } } \ No newline at end of file diff --git a/src/Plugins/MediaEmbed/Configurator/SiteHelpers/AbstractConfigurableHostHelper.php b/src/Plugins/MediaEmbed/Configurator/SiteHelpers/AbstractConfigurableHostHelper.php new file mode 100644 index 000000000..64a9ee8a5 --- /dev/null +++ b/src/Plugins/MediaEmbed/Configurator/SiteHelpers/AbstractConfigurableHostHelper.php @@ -0,0 +1,57 @@ +addHosts([$host]); + } + + public function addHosts(array $hosts): void + { + $siteId = $this->getSiteId(); + if (!isset($this->configurator->registeredVars['MediaEmbed.sites'][$siteId])) + { + $this->configurator->MediaEmbed->add($siteId); + } + + foreach ($hosts as $host) + { + $host = strtolower($host); + $this->configurator->registeredVars['MediaEmbed.hosts'][$host] = $siteId; + } + } + + abstract protected function getSiteId(): string; + + public function setHosts(array $hosts): void + { + $siteId = $this->getSiteId(); + if (!isset($this->configurator->registeredVars['MediaEmbed.sites'][$siteId])) + { + $this->configurator->MediaEmbed->add($siteId); + } + + // Remove previously set hosts for this site + $unsetHosts = array_keys( + (array) $this->configurator->registeredVars['MediaEmbed.hosts'], + $siteId, + true + ); + foreach ($unsetHosts as $host) + { + unset($this->configurator->registeredVars['MediaEmbed.hosts'][$host]); + } + + $this->addHosts($hosts); + } +} \ No newline at end of file diff --git a/src/Plugins/MediaEmbed/Configurator/SiteHelpers/AbstractSiteHelper.php b/src/Plugins/MediaEmbed/Configurator/SiteHelpers/AbstractSiteHelper.php new file mode 100644 index 000000000..3a0c7dc0a --- /dev/null +++ b/src/Plugins/MediaEmbed/Configurator/SiteHelpers/AbstractSiteHelper.php @@ -0,0 +1,17 @@ + + https://mastodon.social/@HackerNewsBot/100181134752056592 + https://xenforo.com/community/threads/embed-your-content-anywhere.217381/ social diff --git a/tests/Plugins/MediaEmbed/Configurator/MastodonHelperTest.php b/tests/Plugins/MediaEmbed/Configurator/SiteHelpers/MastodonHelperTest.php similarity index 55% rename from tests/Plugins/MediaEmbed/Configurator/MastodonHelperTest.php rename to tests/Plugins/MediaEmbed/Configurator/SiteHelpers/MastodonHelperTest.php index 3f81860f0..1dc475d68 100644 --- a/tests/Plugins/MediaEmbed/Configurator/MastodonHelperTest.php +++ b/tests/Plugins/MediaEmbed/Configurator/SiteHelpers/MastodonHelperTest.php @@ -1,12 +1,14 @@ assertXmlStringEqualsXmlString($expected, $actual); } + + /** + * @testdox setHosts() resets previously allowed hosts + */ + public function testSetHost() + { + $mastodonHelper = new MastodonHelper($this->configurator); + $mastodonHelper->setHosts(['test.local']); + + $parser = $this->getParser(); + $actual = $parser->parse('https://mastodon.social/@HackerNewsBot/100181134752056592 https://test.local/@user/123'); + $expected = 'https://mastodon.social/@HackerNewsBot/100181134752056592 https://test.local/@user/123'; + + $this->assertXmlStringEqualsXmlString($expected, $actual); + } } \ No newline at end of file diff --git a/tests/Plugins/MediaEmbed/Configurator/SiteHelpers/XenForoHelperTest.php b/tests/Plugins/MediaEmbed/Configurator/SiteHelpers/XenForoHelperTest.php new file mode 100644 index 000000000..501e3f4e3 --- /dev/null +++ b/tests/Plugins/MediaEmbed/Configurator/SiteHelpers/XenForoHelperTest.php @@ -0,0 +1,30 @@ +configurator); + $xenforoHelper->addHost('xenforo.com'); + + $parser = $this->getParser(); + $actual = $parser->parse('https://xenforo.com/community/threads/embed.217381/'); + $expected = 'https://xenforo.com/community/threads/embed.217381/'; + + $this->assertXmlStringEqualsXmlString($expected, $actual); + } +} \ No newline at end of file diff --git a/tests/Plugins/MediaEmbed/ConfiguratorTest.php b/tests/Plugins/MediaEmbed/ConfiguratorTest.php index 854a2d7ee..2f2040dc2 100644 --- a/tests/Plugins/MediaEmbed/ConfiguratorTest.php +++ b/tests/Plugins/MediaEmbed/ConfiguratorTest.php @@ -4,6 +4,7 @@ use Exception; use s9e\TextFormatter\Configurator\Items\AttributePreprocessor; +use s9e\TextFormatter\Plugins\MediaEmbed\Configurator\SiteHelpers\MastodonHelper; use s9e\TextFormatter\Tests\Test; /** @@ -497,4 +498,15 @@ public function testAsConfigQuickMatchHost() $this->assertSame('://', $config['quickMatch']); } + + /** + * @testdox getSiteHelper('mastodon') returns the Mastodon helper + */ + public function testGetSiteHelper() + { + $this->assertInstanceOf( + MastodonHelper::class, + $this->configurator->MediaEmbed->getSiteHelper('mastodon') + ); + } } \ No newline at end of file