/vendor/embed/embed/src/Providers/OpenGraph.php

https://github.com/FlusherDock1/Gutenberg · PHP · 226 lines · 146 code · 35 blank · 45 comment · 18 complexity · a1b1bf165a6d9b9842da5267789a1a79 MD5 · raw file

  1. <?php
  2. namespace Embed\Providers;
  3. use Embed\Adapters\Adapter;
  4. use Embed\Utils;
  5. /**
  6. * Provider to get the data from the Open Graph elements in the HTML
  7. */
  8. class OpenGraph extends Provider
  9. {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function __construct(Adapter $adapter)
  14. {
  15. parent::__construct($adapter);
  16. if (!($html = $adapter->getResponse()->getHtmlContent())) {
  17. return;
  18. }
  19. foreach ($html->getElementsByTagName('meta') as $meta) {
  20. $name = trim(strtolower($meta->getAttribute('property')));
  21. $value = $meta->getAttribute('content');
  22. if (empty($name)) {
  23. $name = trim(strtolower($meta->getAttribute('name')));
  24. }
  25. if (empty($name) || empty($value)) {
  26. continue;
  27. }
  28. if (strpos($name, 'og:') === 0) {
  29. $name = substr($name, 3);
  30. } elseif (
  31. strpos($name, 'article:') !== 0
  32. && strpos($name, 'image:') !== 0
  33. && strpos($name, 'video:') !== 0
  34. && strpos($name, 'book:') !== 0
  35. && strpos($name, 'music:') !== 0
  36. && strpos($name, 'profile:') !== 0
  37. ) {
  38. continue;
  39. }
  40. switch ($name) {
  41. case 'image':
  42. case 'image:url':
  43. case 'image:secure_url':
  44. $this->bag->add('images', $value);
  45. break;
  46. case 'article:tag':
  47. case 'video:tag':
  48. case 'book:tag':
  49. $this->bag->add('tags', $value);
  50. break;
  51. default:
  52. $this->bag->set($name, $value);
  53. }
  54. }
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getTitle()
  60. {
  61. return $this->bag->get('title');
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getDescription()
  67. {
  68. return $this->bag->get('description');
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getType()
  74. {
  75. $type = $this->bag->get('type');
  76. if (strpos($type, ':') !== false) {
  77. $type = substr(strrchr($type, ':'), 1);
  78. }
  79. switch ($type) {
  80. case 'video':
  81. case 'photo':
  82. case 'link':
  83. case 'rich':
  84. return $type;
  85. case 'article':
  86. return 'link';
  87. }
  88. if ($this->bag->has('video')) {
  89. return 'video';
  90. }
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function getCode()
  96. {
  97. $names = ['video:secure_url', 'video:url', 'video'];
  98. foreach ($names as $name) {
  99. if ($this->bag->has($name)) {
  100. $video = $this->normalizeUrl($this->bag->get($name));
  101. if (!($videoPath = parse_url($video, PHP_URL_PATH)) || !($type = pathinfo($videoPath, PATHINFO_EXTENSION))) {
  102. $type = $this->bag->get('video:type');
  103. }
  104. switch ($type) {
  105. case 'swf':
  106. case 'application/x-shockwave-flash':
  107. return Utils::flash($video, $this->getWidth(), $this->getHeight());
  108. case 'mp4':
  109. case 'ogg':
  110. case 'ogv':
  111. case 'webm':
  112. case 'application/mp4':
  113. case 'video/mp4':
  114. case 'video/ogg':
  115. case 'video/ogv':
  116. case 'video/webm':
  117. $images = $this->getImagesUrls();
  118. return Utils::videoHtml(current($images), $video, $this->getWidth(), $this->getHeight());
  119. case 'text/html':
  120. return Utils::iframe($video, $this->getWidth(), $this->getHeight());
  121. }
  122. }
  123. }
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function getUrl()
  129. {
  130. return $this->normalizeUrl($this->bag->get('url'));
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function getTags()
  136. {
  137. return (array) $this->bag->get('tags') ?: [];
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function getAuthorName()
  143. {
  144. return $this->bag->get('article:author') ?: $this->bag->get('book:author');
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function getProviderName()
  150. {
  151. return $this->bag->get('site_name');
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function getImagesUrls()
  157. {
  158. return $this->normalizeUrls($this->bag->get('images'));
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function getWidth()
  164. {
  165. return $this->bag->get('image:width') ?: $this->bag->get('video:width');
  166. }
  167. /**
  168. * {@inheritdoc}
  169. */
  170. public function getHeight()
  171. {
  172. return $this->bag->get('image:height') ?: $this->bag->get('video:height');
  173. }
  174. /**
  175. * {@inheritdoc}
  176. */
  177. public function getPublishedTime()
  178. {
  179. return $this->bag->get('article:published_time')
  180. ?: $this->bag->get('article:modified_time')
  181. ?: $this->bag->get('video:release_date')
  182. ?: $this->bag->get('music:release_date');
  183. }
  184. /**
  185. * {@inheritdoc}
  186. */
  187. public function getProviderUrl()
  188. {
  189. return $this->bag->get('website');
  190. }
  191. }