nr/fieldtypeoembed

收集和存储Oembed数据

1.1.6 2023-06-19 07:54 UTC

This package is auto-updated.

Last update: 2024-10-02 14:41:03 UTC


README

功能

从外部源存储、收集和更新oembed数据。此模块使用Félix Girault的出色PHP库Essence,并添加了一些Processwire魔法。它受到了Ryan的示例模块FieldtypeEvents和felixwahner的TextformatterOEmbed模块的启发。谢谢!

特性

  • 通过oembed端点和opengraph爬虫简单嵌入内容
  • 后端预览
  • 使用$pages->find()搜索oembed数据
  • 使用lazycron自动更新
  • 支持ProcessGraphQL(通过额外的模块GraphQLFieldtypeOembed
  • 使用钩子过滤结果

安装

  1. 将此模块的文件复制到/site/modules/FieldtypeOembed/
  2. 在/site/modules/FieldtypeOembed/目录中执行以下命令。
    composer install
  3. 在管理界面:模块 > 刷新。安装模块 > Oembed。
  4. 创建一个新的Oembed类型的字段,并给它起一个名字。在我们的示例中,我们简单地命名为“embed”。
  5. 将字段添加到一个模板中,并使用该模板编辑一个页面。在字段中输入一个(embed)-url并保存页面。

通过composer安装

  1. 在您的网站根目录中执行以下命令。
    composer require nr/fieldtypeoembed

配置

模块 > 配置 > FieldtypeOembed

Lazycron

设置Lazycron计划。缓存的过期时间可在字段设置中配置。

Lazycron

Essence的自定义提供者

您可以为Essence配置自己的OembedOpenGraph提供者。
如何添加自定义提供者(Essence Git)

{
   "getty": {
      "class": "OEmbed",
      "filter": "~gty\\.im/.+~i",
      "endpoint": "http://embed.gettyimages.com/oembed?url=:url"
   },
   "neuerituale": {
      "class": "OpenGraph",
      "filter": "~neuerituale\\.com.?~i"
   },
   "tiktok": {
      "class": "OEmbed",
      "filter": "~tiktok\\.com/.+~i",
      "endpoint": "https://www.tiktok.com/oembed?url=:url"
   },
   "InstagramOEmbed": {
      "class": "OEmbed",
      "filter": "~instagr(\\.am|am\\.com)/p/.+~i",
      "endpoint": "http://api.instagram.com/oembed?format=json&url=:url&hidecaption=true"
   },
}

您可以禁用所有提供者

{
	"23hq": false,
	"500px": false,
	"Animoto": false,
	"Aol": false,
	"App.net": false,
	"Bambuser": false,
	"Bandcamp": false,
	"Blip.tv": false,
	"Cacoo": false,
	"CanalPlus": false,
	"Chirb.it": false,
	"CircuitLab": false,
	"Clikthrough": false,
	"CollegeHumorOEmbed": false,
	"CollegeHumorOpenGraph": false,
	"Coub": false,
	"CrowdRanking": false,
	"DailyMile": false,
	"Dailymotion": false,
	"Deviantart": false,
	"Dipity": false,
	"Documentcloud": false,
	"Dotsub": false,
	"EdocrOEmbed": false,
	"EdocrTwitterCards": false,
	"FacebookPost": false,
	"FlickrOEmbed": false,
	"FlickrOpenGraph": false,
	"FunnyOrDie": false,
	"Gist": false,
	"Gmep": false,
	"HowCast": false,
	"Huffduffer": false,
	"Hulu": false,
	"Ifixit": false,
	"Ifttt": false,
	"Imgur": false,
	"InstagramOEmbed": false,
	"InstagramOpenGraph": false,
	"Jest": false,
	"Justin": false,
	"Kickstarter": false,
	"Meetup": false,
	"Mixcloud": false,
	"Mobypicture": false,
	"Nfb": false,
	"Official.fm": false,
	"Polldaddy": false,
	"PollEverywhere": false,
	"Prezi": false,
	"Qik": false,
	"Rdio": false,
	"Revision3": false,
	"Roomshare": false,
	"Sapo": false,
	"Screenr": false,
	"Scribd": false,
	"Shoudio": false,
	"Sketchfab": false,
	"SlideShare": false,
	"SoundCloud": false,
	"SpeakerDeck": false,
	"Spotify": false,
	"TedOEmbed": false,
	"TedOpenGraph": false,
	"Twitter": false,
	"Ustream": false,
	"Vhx": false,
	"Viddler": false,
	"Videojug": false,
	"Vimeo": false,
	"Vine": false,
	"Wistia": false,
	"WordPress": false,
	"Yfrog": false,
	"Youtube": false,
	"FacebookVideo": false
}

字段设置

字段 > embed > 详细信息 FieldtypeOembed扩展了FieldtypeURL(核心)。除了这些设置外,您还可以为oembed数据设置缓存时间。lazycron将更新数据。

Fieldsettings

字段预览

Fieldpreview

API

返回Oembed对象(WireData)

/** @var \ProcessWire\Oembed */
$page->embed

检查空值

/** @var boolean **/
$page->embed->empty

渲染

/** @var string return the html from oembed result */
"$page->embed"
$page->embed->html

结果钩子

如果您想过滤或更改oembed提供者的结果,请使用可钩方法,例如___filterProps()

在这个例子中,我们想要获取YouTube视频缩略图的高分辨率版本。

// some where in your ready.php
// Replace hqdefault preview thumbnail with maxresdefault

$this->addHook('FieldtypeOembed::filterProps', function (HookEvent $event) {
   $propsArray = $event->arguments(0);
   if(is_array($propsArray) && $propsArray['providerName'] === 'YouTube') {
      $maxResultUrl = str_replace('/hqdefault.jpg', '/maxresdefault.jpg', $propsArray['thumbnailUrl']);
      // test max result url
      if((new WireHttp())->status($maxResultUrl) === 200) $propsArray['thumbnailUrl'] = $maxResultUrl;
      $event->return = $propsArray;
   }
});

Oembed对象

// print_r($page->embed);

ProcessWire\Oembed Object
(
    [data] => Array
        (
            [empty] => false
            [url] => http://www.youtube.com/watch?v=dQw4w9WgXcQ
            [html] => '...'
            [type] => video
            [title] => Rick Astley - Never Gonna Give You Up (Video)
            [width] => 200
            [height] => 113
            [version] => 1.0
            [authorUrl] => https://www.youtube.com/user/RickAstleyVEVO
            [authorName] => RickAstleyVEVO
            [providerUrl] => https://www.youtube.com/
            [providerName] => YouTube
            [thumbnailUrl] => https://i.ytimg.com/vi/dQw4w9WgXcQ/hqdefault.jpg
            [thumbnailWidth] => 480
            [thumbnailHeight] => 360
        )
)

查找页面

您可以查询oembed结果字段

$pages->find('embed.providerName=YouTube');
$pages->find('embed.width>=200');

GraphQLFieldtypeOembed

您可以通过ProcessGraphQL查询此字段。
请安装附加模块GraphQLFieldtypeOembed

字段定义

myfield {
   empty: Boolean
   title: String
   authorName: String
   authorUrl: String
   type: String
   height: Int
   width: Int
   providerName: String
   providerUrl: String
   thumbnailHeight: Int
   thumbnailWidth: Int
   thumbnailUrl: String
   html: String
   url: String
}

待办事项

  • 删除extend urlfield!
  • 删除_oembed helper构造(脏)