diff --git a/TextformatterVideoEmbed.module b/TextformatterVideoEmbed.module index 6e30bbe..f38e5a9 100644 --- a/TextformatterVideoEmbed.module +++ b/TextformatterVideoEmbed.module @@ -1,332 +1,364 @@ __('Video embed for YouTube/Vimeo', __FILE__), - 'version' => 111, - 'summary' => __('Enter a full YouTube or Vimeo URL by itself in any paragraph (example: http://www.youtube.com/watch?v=Wl4XiYadV_k) and this will automatically convert it to an embedded video. This formatter is intended to be run on trusted input. Recommended for use with TinyMCE textarea fields.', __FILE__), - 'author' => 'Ryan Cramer', - 'href' => 'http://modules.processwire.com/modules/textformatter-video-embed/' - ); - } - - const dbTableName = 'textformatter_video_embed'; - - /** - * Default configuration values - * - * @var array - * - */ - protected static $configDefaults = array( - 'maxWidth' => 640, - 'maxHeight' => 480, - 'responsive' => 0, - ); - - /** - * Data as used by the get/set functions - * - */ - protected $data = array(); - - /** - * Either http or https, depending on $config->https - * - */ - protected $http = ''; - - /** - * Set our configuration defaults - * - */ - public function __construct() { - foreach(self::$configDefaults as $key => $value) { - $this->set($key, $value); - } - $this->http = $this->config->https ? 'https' : 'http'; - } - - /** - * Given a service oembed URL and video ID, return the corresponding embed code. - * - * A cached version of the embed code will be used if possible. When not possible, - * it will be retrieved from the service's oembed URL, and then cached. - * - * @param string $oembedURL - * @param string $videoID - * @return string - * - */ - protected function getEmbedCode($oembedURL, $videoID) { - - $database = $this->wire('database'); - $table = self::dbTableName; - $query = $database->prepare("SELECT embed_code FROM $table WHERE video_id=:video_id"); - $query->bindValue(":video_id", $videoID); - $query->execute(); - - $embedCode = ''; - - if($query->rowCount()) { - - list($embedCode) = $query->fetch(\PDO::FETCH_NUM); - $query->closeCursor(); - - } else { - - $query->closeCursor(); - $http = new WireHttp(); - $data = $http->get($oembedURL); - if($data) $data = json_decode($data, true); - - if(is_array($data) && isset($data['html'])) { - $embedCode = $data['html']; - - $sql = "INSERT INTO $table SET " . - "video_id=:videoID, " . - "embed_code=:embedCode, " . - "created=NOW() "; - - $query = $database->prepare($sql); - $query->bindValue(":videoID", $videoID); - $query->bindValue(":embedCode", $embedCode); - $query->execute(); - } - } - - // account for possibility that stored embed code contains http version while requested on https - if($this->http == 'https') $embedCode = str_replace('http://', 'https://', $embedCode); - - return $embedCode; - } - - /** - * Make an iframe-based embed code responsive - * - * @param string $out - * @return string - * - */ - protected function makeResponsive($out) { - $out = str_ireplace('