TemplateLayouts
1 month ago
AirTable.php
1 year ago
Boomplay.php
1 year ago
Calendly.php
2 years ago
Canva.php
1 year ago
FITE.php
1 year ago
GettyImages.php
9 months ago
Giphy.php
2 years ago
GitHub.php
3 years ago
GoogleCalendar.php
8 months ago
GoogleDocs.php
2 years ago
GoogleDrive.php
2 years ago
GoogleMaps.php
2 years ago
GooglePhotos.php
3 months ago
Gumroad.php
2 years ago
InstagramFeed.php
1 month ago
LinkedIn.php
2 years ago
Meetup.php
6 months ago
NRKRadio.php
2 years ago
OneDrive.php
11 months ago
OpenSea.php
9 months ago
SelfHosted.php
2 months ago
Spreaker.php
1 year ago
TikTok.php
2 years ago
Twitch.php
2 years ago
Wistia.php
3 months ago
Wrapper.php
3 years ago
X.php
2 years ago
Youtube.php
1 month ago
index.html
7 years ago
Wistia.php
308 lines
| 1 | <?php |
| 2 | |
| 3 | namespace EmbedPress\Providers; |
| 4 | |
| 5 | use Embera\Provider\ProviderAdapter; |
| 6 | use Embera\Provider\ProviderInterface; |
| 7 | use Embera\Url; |
| 8 | |
| 9 | (defined('ABSPATH') && defined('EMBEDPRESS_IS_LOADED')) or die("No direct script access allowed."); |
| 10 | |
| 11 | /** |
| 12 | * Wistia provider for EmbedPress. |
| 13 | * |
| 14 | * @package EmbedPress |
| 15 | * @subpackage EmbedPress/Providers |
| 16 | * @author EmbedPress |
| 17 | * @license GPLv3 or later |
| 18 | * @since 1.0.0 |
| 19 | */ |
| 20 | class Wistia extends ProviderAdapter implements ProviderInterface |
| 21 | { |
| 22 | protected static $hosts = [ |
| 23 | '*.wistia.com', |
| 24 | 'wistia.com' |
| 25 | ]; |
| 26 | |
| 27 | /** inline {@inheritdoc} */ |
| 28 | protected $allowedParams = [ |
| 29 | 'maxwidth', |
| 30 | 'maxheight', |
| 31 | 'wstarttime', |
| 32 | 'wautoplay', |
| 33 | 'scheme', |
| 34 | 'captions', |
| 35 | 'captions_default', |
| 36 | 'playbutton', |
| 37 | 'smallplaybutton', |
| 38 | 'playbar', |
| 39 | 'resumable', |
| 40 | 'wistiafocus', |
| 41 | 'volumecontrol', |
| 42 | 'volume', |
| 43 | 'rewind', |
| 44 | 'rewind_time', |
| 45 | 'wfullscreen', |
| 46 | 'always_show_controls', |
| 47 | ]; |
| 48 | |
| 49 | |
| 50 | /** inline {@inheritdoc} */ |
| 51 | protected $httpsSupport = true; |
| 52 | |
| 53 | public function getAllowedParams() |
| 54 | { |
| 55 | return $this->allowedParams; |
| 56 | } |
| 57 | |
| 58 | /** inline {@inheritdoc} */ |
| 59 | protected $responsiveSupport = true; |
| 60 | |
| 61 | public function __construct($url, array $config = []) |
| 62 | { |
| 63 | parent::__construct($url, $config); |
| 64 | add_filter('embedpress_render_dynamic_content', [$this, 'fakeDynamicResponse'], 10, 2); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Validates if the URL belongs to Wistia. |
| 69 | * |
| 70 | * @param Url $url |
| 71 | * @return bool |
| 72 | */ |
| 73 | public function validateUrl(Url $url) |
| 74 | { |
| 75 | $urlString = (string) $url; |
| 76 | |
| 77 | |
| 78 | return (bool) ( |
| 79 | preg_match('~(?:\w+\.)?wistia\.com/embed/(iframe|playlists)/([^/]+)~i', $urlString) || |
| 80 | preg_match('~(?:\w+\.)?wistia\.com/medias/([^/]+)~i', $urlString) |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | public function getVideoIDFromURL($url) |
| 85 | { |
| 86 | // https://fast.wistia.com/embed/medias/xf1edjzn92.jsonp |
| 87 | // https://ostraining-1.wistia.com/medias/xf1edjzn92 |
| 88 | preg_match('#\/medias\\\?\/([a-z0-9]+)\.?#i', $url, $matches); |
| 89 | |
| 90 | $id = false; |
| 91 | if (isset($matches[1])) { |
| 92 | $id = $matches[1]; |
| 93 | } |
| 94 | |
| 95 | return $id; |
| 96 | } |
| 97 | |
| 98 | private function getMergedOptions($providerOptions = []) |
| 99 | { |
| 100 | $wis_settings = get_option(EMBEDPRESS_PLG_NAME . ':wistia'); |
| 101 | if (!is_array($wis_settings)) { |
| 102 | $wis_settings = []; |
| 103 | } |
| 104 | |
| 105 | // Map: provider param => [admin settings key, default value] |
| 106 | $defaults_map = [ |
| 107 | 'wfullscreen' => ['display_fullscreen_button', 1], |
| 108 | 'playbar' => ['display_playbar', 1], |
| 109 | 'smallplaybutton' => ['small_play_button', 1], |
| 110 | 'playbutton' => [null, 1], |
| 111 | 'wautoplay' => ['autoplay', ''], |
| 112 | 'wstarttime' => ['start_time', 0], |
| 113 | 'scheme' => ['player_color', ''], |
| 114 | 'resumable' => ['plugin_resumable', ''], |
| 115 | 'wistiafocus' => ['plugin_focus', ''], |
| 116 | 'rewind' => ['plugin_rewind', ''], |
| 117 | 'rewind_time' => ['plugin_rewind_time', 10], |
| 118 | 'volumecontrol' => ['display_volume_control', 1], |
| 119 | 'volume' => ['volume', 100], |
| 120 | 'captions' => ['plugin_captions', ''], |
| 121 | 'captions_default' => ['plugin_captions_default', ''], |
| 122 | 'always_show_controls' => ['always_show_controls', ''], |
| 123 | ]; |
| 124 | |
| 125 | $merged = $providerOptions; |
| 126 | |
| 127 | foreach ($defaults_map as $paramKey => list($adminKey, $default)) { |
| 128 | if (!isset($merged[$paramKey]) || $merged[$paramKey] === '') { |
| 129 | if ($adminKey !== null && isset($wis_settings[$adminKey]) && $wis_settings[$adminKey] !== '') { |
| 130 | $merged[$paramKey] = $wis_settings[$adminKey]; |
| 131 | } else { |
| 132 | $merged[$paramKey] = $default; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return $merged; |
| 138 | } |
| 139 | |
| 140 | private function buildEmbedHtml($options) |
| 141 | { |
| 142 | $embedOptions = new \stdClass; |
| 143 | $embedOptions->videoFoam = false; |
| 144 | |
| 145 | // Fullscreen |
| 146 | $embedOptions->fullscreenButton = (bool) $options['wfullscreen']; |
| 147 | |
| 148 | // Playbar |
| 149 | $embedOptions->playbar = (bool) $options['playbar']; |
| 150 | |
| 151 | // Small play button |
| 152 | $embedOptions->smallPlayButton = (bool) $options['smallplaybutton']; |
| 153 | |
| 154 | // Big play button |
| 155 | $embedOptions->playButton = (bool) $options['playbutton']; |
| 156 | |
| 157 | // Autoplay |
| 158 | $embedOptions->autoPlay = (bool) $options['wautoplay']; |
| 159 | |
| 160 | // Volume control |
| 161 | $embedOptions->volumeControl = (bool) $options['volumecontrol']; |
| 162 | |
| 163 | // Volume (convert 0-100 to 0-1 float) |
| 164 | if (isset($options['volume'])) { |
| 165 | $embedOptions->volume = max(0, min(1, (int) $options['volume'] / 100)); |
| 166 | } |
| 167 | |
| 168 | // Start time |
| 169 | if (!empty($options['wstarttime'])) { |
| 170 | $embedOptions->time = (int) $options['wstarttime']; |
| 171 | } |
| 172 | |
| 173 | // Always show controls |
| 174 | $alwaysShowControls = !empty($options['always_show_controls']); |
| 175 | if ($alwaysShowControls) { |
| 176 | $embedOptions->controlsVisibleOnLoad = true; |
| 177 | } |
| 178 | |
| 179 | // Player color |
| 180 | if (!empty($options['scheme'])) { |
| 181 | $embedOptions->playerColor = $options['scheme']; |
| 182 | } |
| 183 | |
| 184 | // Plugins |
| 185 | $pluginsBaseURL = plugins_url( |
| 186 | 'assets/js/wistia/min', |
| 187 | dirname(__DIR__) . '/embedpress-Wistia.php' |
| 188 | ); |
| 189 | |
| 190 | $pluginList = []; |
| 191 | |
| 192 | // Resumable |
| 193 | $isResumableEnabled = !empty($options['resumable']); |
| 194 | if ($isResumableEnabled) { |
| 195 | $pluginList['resumable'] = [ |
| 196 | 'src' => $pluginsBaseURL . '/resumable.min.js', |
| 197 | 'async' => false |
| 198 | ]; |
| 199 | } |
| 200 | |
| 201 | // Autoplay + resumable fix |
| 202 | if ($embedOptions->autoPlay && $isResumableEnabled) { |
| 203 | $pluginList['fixautoplayresumable'] = [ |
| 204 | 'src' => $pluginsBaseURL . '/fixautoplayresumable.min.js' |
| 205 | ]; |
| 206 | } |
| 207 | |
| 208 | // Focus (dim the lights) |
| 209 | if (!empty($options['wistiafocus'])) { |
| 210 | $pluginList['dimthelights'] = [ |
| 211 | 'src' => $pluginsBaseURL . '/dimthelights.min.js', |
| 212 | 'autoDim' => true |
| 213 | ]; |
| 214 | $embedOptions->focus = true; |
| 215 | } |
| 216 | |
| 217 | // Captions |
| 218 | if (!empty($options['captions'])) { |
| 219 | $pluginList['captions-v1'] = [ |
| 220 | 'onByDefault' => !empty($options['captions_default']), |
| 221 | ]; |
| 222 | } |
| 223 | |
| 224 | // Rewind |
| 225 | if (!empty($options['rewind'])) { |
| 226 | $rewindTime = !empty($options['rewind_time']) ? (int) $options['rewind_time'] : 10; |
| 227 | $embedOptions->rewindTime = $rewindTime; |
| 228 | |
| 229 | $pluginList['rewind'] = [ |
| 230 | 'src' => $pluginsBaseURL . '/rewind.min.js' |
| 231 | ]; |
| 232 | } |
| 233 | |
| 234 | $embedOptions->plugin = $pluginList; |
| 235 | $embedOptions = json_encode($embedOptions); |
| 236 | |
| 237 | // Video ID |
| 238 | $videoId = $this->getVideoIDFromURL($options['url']); |
| 239 | $shortVideoId = substr($videoId, 0, 3); |
| 240 | |
| 241 | $class = [ |
| 242 | 'wistia_embed', |
| 243 | 'wistia_async_' . $videoId |
| 244 | ]; |
| 245 | |
| 246 | $width = $options['width'] ?? 640; |
| 247 | $height = $options['height'] ?? 360; |
| 248 | |
| 249 | $attribs = [ |
| 250 | sprintf('id="wistia_%s"', $videoId), |
| 251 | sprintf('class="%s"', implode(' ', $class)), |
| 252 | sprintf('style="width:%spx; height:%spx;"', $width, $height) |
| 253 | ]; |
| 254 | |
| 255 | $html = "<div class=\"embedpress-wrapper ose-wistia ose-uid-{$videoId} responsive we\">"; |
| 256 | $html .= '<script src="https://fast.wistia.com/assets/external/E-v1.js" async></script>'; |
| 257 | $html .= "<script>window._wq = window._wq || []; _wq.push({\"{$shortVideoId}\": {$embedOptions}});</script>"; |
| 258 | if ($alwaysShowControls) { |
| 259 | $html .= "<script>window._wq = window._wq || []; _wq.push({id: \"{$videoId}\", onReady: function(video){ video.requestControls('embedpress'); }});</script>"; |
| 260 | } |
| 261 | $html .= '<div ' . implode(' ', $attribs) . '></div>'; |
| 262 | $html .= '</div>'; |
| 263 | |
| 264 | return $html; |
| 265 | } |
| 266 | |
| 267 | public function enhance_wistia() |
| 268 | { |
| 269 | $options = $this->getMergedOptions($this->getParams()); |
| 270 | return $this->buildEmbedHtml($options); |
| 271 | } |
| 272 | |
| 273 | public function fakeDynamicResponse($embed, $options = []) |
| 274 | { |
| 275 | $options = $this->getMergedOptions($options); |
| 276 | return $this->buildEmbedHtml($options); |
| 277 | } |
| 278 | |
| 279 | |
| 280 | /** |
| 281 | * Generates a fake oEmbed response. |
| 282 | * |
| 283 | * @return array |
| 284 | */ |
| 285 | public function fakeResponse() |
| 286 | { |
| 287 | |
| 288 | return [ |
| 289 | 'type' => 'rich', |
| 290 | 'provider_name' => 'Wistia', |
| 291 | 'provider_url' => 'https://wistia.com', |
| 292 | 'title' => 'Wistia', |
| 293 | 'html' => $this->enhance_wistia(), |
| 294 | ]; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Fallback for modifyResponse, returns fakeResponse. |
| 299 | * |
| 300 | * @param array $response |
| 301 | * @return array |
| 302 | */ |
| 303 | public function modifyResponse(array $response = []) |
| 304 | { |
| 305 | return $this->fakeResponse(); |
| 306 | } |
| 307 | } |
| 308 |