Giphy.php
8 years ago
GoogleDocs.php
8 years ago
GoogleMaps.php
8 years ago
Twitch.php
8 years ago
index.html
9 years ago
Twitch.php
125 lines
| 1 | <?php |
| 2 | namespace EmbedPress\Providers; |
| 3 | |
| 4 | use Embera\Adapters\Service as EmberaService; |
| 5 | |
| 6 | (defined('ABSPATH') && defined('EMBEDPRESS_IS_LOADED')) or die("No direct script access allowed."); |
| 7 | |
| 8 | /** |
| 9 | * Entity responsible to support Twitch embeds. |
| 10 | * |
| 11 | * @package EmbedPress |
| 12 | * @subpackage EmbedPress/Providers |
| 13 | * @author EmbedPress <help@embedpress.com> |
| 14 | * @copyright Copyright (C) 2018 EmbedPress. All rights reserved. |
| 15 | * @license GPLv2 or later |
| 16 | * @since 1.5.0 |
| 17 | */ |
| 18 | class Twitch extends EmberaService |
| 19 | { |
| 20 | /** |
| 21 | * The regex which identifies Twitch URLs. |
| 22 | * |
| 23 | * @since 1.5.0 |
| 24 | * @access private |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | private $urlRegexPattern = '/http[s]?:\/\/(?:www\.|clips\.)twitch\.tv\/([0-9a-zA-Z\-\_]+)\/?(chat\/?$|[0-9a-z\-\_]*)?/'; |
| 29 | |
| 30 | /** |
| 31 | * Method that verifies if the embed URL belongs to Twitch. |
| 32 | * |
| 33 | * @since 1.5.0 |
| 34 | * |
| 35 | * @return boolean |
| 36 | */ |
| 37 | public function validateUrl() |
| 38 | { |
| 39 | return preg_match($this->urlRegexPattern, $this->url); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Return the type of the embed based on the URL. |
| 44 | * |
| 45 | * @param $url |
| 46 | * |
| 47 | * @return string |
| 48 | */ |
| 49 | protected function getType($url) |
| 50 | { |
| 51 | if ( stristr($url, 'clips.twitch.tv') ) { |
| 52 | return 'clip'; |
| 53 | } |
| 54 | |
| 55 | if ( stristr($url, '/videos/') ) { |
| 56 | return 'video'; |
| 57 | } |
| 58 | |
| 59 | if ( preg_match('#/chat$#', $url) ) { |
| 60 | return 'chat'; |
| 61 | } |
| 62 | |
| 63 | return 'channel'; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * This method fakes an Oembed response. |
| 68 | * |
| 69 | * @since 1.5.0 |
| 70 | * |
| 71 | * @return array |
| 72 | */ |
| 73 | public function fakeResponse() |
| 74 | { |
| 75 | $url = $this->getUrl(); |
| 76 | $providerUrl = 'https://twitch.tv'; |
| 77 | $html = ''; |
| 78 | $src = ''; |
| 79 | |
| 80 | if (preg_match("{$this->urlRegexPattern}i", $url, $matches)) { |
| 81 | $channelName = $matches[1]; |
| 82 | |
| 83 | $type = $this->getType($url); |
| 84 | |
| 85 | // Clip, channel, chat, collection, or video? |
| 86 | switch ($type) { |
| 87 | case 'clip': |
| 88 | $src = 'https://clips.twitch.tv/embed?clip=' . $channelName . '&autoplay=false'; |
| 89 | $attrs = 'scrolling="no" frameborder="0" allowfullscreen="true"'; |
| 90 | break; |
| 91 | |
| 92 | case 'video': |
| 93 | $channelName = $matches[2]; |
| 94 | $src = 'https://player.twitch.tv/?video=' . $channelName; |
| 95 | $attrs = 'scrolling="no" frameborder="0" allowfullscreen="true"'; |
| 96 | break; |
| 97 | |
| 98 | case 'channel': |
| 99 | $src = 'https://player.twitch.tv/?channel=' . $channelName; |
| 100 | $attrs = 'scrolling="no" frameborder="0" allowfullscreen="true"'; |
| 101 | break; |
| 102 | |
| 103 | case 'chat': |
| 104 | $src = 'http://www.twitch.tv/embed/' . $channelName . '/chat'; |
| 105 | $attrs = 'scrolling="yes" frameborder="0" allowfullscreen="true" id="' . $channelName . '"'; |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | $html = '<iframe src="' . $src . '" height="{height}" width="{width}" ' . $attrs . '></iframe>'; |
| 110 | |
| 111 | $response = array( |
| 112 | 'type' => $type, |
| 113 | 'provider_name' => 'Twitch', |
| 114 | 'provider_url' => $providerUrl, |
| 115 | 'url' => $url, |
| 116 | 'html' => $html |
| 117 | ); |
| 118 | } else { |
| 119 | $response = array(); |
| 120 | } |
| 121 | |
| 122 | return $response; |
| 123 | } |
| 124 | } |
| 125 |