From 361329cba674ca03b1bc19196f648fb5c4f77c39 Mon Sep 17 00:00:00 2001 From: Lex Sanchez Date: Mon, 22 Jan 2018 23:27:24 -0500 Subject: [PATCH 1/3] Now you can embed publications from the main social networks (Facebook, Twitter, Instagram) or Issuu Presentations. V.121 --- README.md | 21 +++-- TextformatterVideoEmbed.module | 138 +++++++++++++++++++++++++++++++-- 2 files changed, 149 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e4ce7d9..9eedfdd 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,18 @@ -# Video embed for YouTube and Vimeo +# Video or Social Post Embed -ProcessWire Textformatter module that enables translation of YouTube or Vimeo URLs to full embed codes, resulting in a viewable video in textarea fields you apply it to. +ProcessWire Textformatter module that enables translation of YouTube, Vimeo, Instagram, Facebook, Twitter or Issuu URLs to full embed codes, resulting in a viewable video or social post in textarea fields you apply it to. ## How to install - Copy the TextformatterVideoEmbed.module file to your /site/modules/ directory (or place it in /site/modules/TextformatterVideoEmbed/). -- Click *check for new modules* in ProcessWire Admin Modules screen. Click *install* for the module labeled: "Video embed for YouTube/Vimeo". +- Click *check for new modules* in ProcessWire Admin Modules screen. Click *install* for the module labeled: "Video or Social Post Embed". - Now you will be on the module config screen. Please make note of the config options and set as you see fit. ## How to use -- Edit your *body* field in Setup > Fields (or whatever field(s) you will be placing videos in). On the *details* tab, find the *Text Formatters* field and select "Video embed for YouTube/Vimeo". Save. +- Edit your *body* field in Setup > Fields (or whatever field(s) you will be placing videos in). On the *details* tab, find the *Text Formatters* field and select "Video or Social Post Embed". Save. - Edit a page using the field you edited and paste in YouTube and/or Vimeo video URLs each on their own paragraph. @@ -29,6 +29,17 @@ How it might look in your editor (like TinyMCE): > And here is a great video I watched earlier this week: > > http://vimeo.com/18280328 +> +> Embed social post and Issuu Presentation: +> +> https://www.instagram.com/p/BeA8p9AhA3b/ +> +> https://twitter.com/rpc_radio/status/954315569838411776 +> +> https://www.facebook.com/TNTLA/videos/10155549598252758/ +> +> https://issuu.com/madsoundsmagazine/docs/mad_sounds_issue_no._17_issuu + ## How it works @@ -44,7 +55,7 @@ If you change these max width / max height settings you may also want to check t ### Using with Markdown, Textile or other LML -This text formatter is looking for a YouTube or Vimeo video URL surrounded by paragraph tags. As a result, if you are using Markdown or Textile (or something else like it) you want that text formatter to run before this one. +This text formatter is looking for a YouTube, Vimeo, Instagram, Facebook, Twitter or Issuu video URL surrounded by paragraph tags. As a result, if you are using Markdown or Textile (or something else like it) you want that text formatter to run before this one. That ensures that the expected paragraph tags will be present when TextformatterVideoEmbed runs. You can control the order that text formatters are run in by drag/drop sorting in the field editor. ------ diff --git a/TextformatterVideoEmbed.module b/TextformatterVideoEmbed.module index 6e30bbe..f042acd 100644 --- a/TextformatterVideoEmbed.module +++ b/TextformatterVideoEmbed.module @@ -3,7 +3,7 @@ /** * ProcessWire Video Embedding Textformatter * - * Looks for Youtube or Vimeo URLs and automatically converts them to embeds + * Looks for Youtube, Vimeo, Instagram, Facebook, Twitter or Issuu URLs and automatically converts them to embeds * * Copyright (C) 2016 by Ryan Cramer * Licensed under MPL 2.0 @@ -19,9 +19,9 @@ class TextformatterVideoEmbed extends Textformatter implements ConfigurableModul public static function getModuleInfo() { return array( - 'title' => __('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__), + 'title' => __('Video or Social Post Embed', __FILE__), + 'version' => 121, + 'summary' => __('Enter a full YouTube, Vimeo, Instagram, Facebook, Twitter or Issuu 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 or social post. 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/' ); @@ -94,6 +94,7 @@ class TextformatterVideoEmbed extends Textformatter implements ConfigurableModul $query->closeCursor(); $http = new WireHttp(); + $http->setHeader('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); $data = $http->get($oembedURL); if($data) $data = json_decode($data, true); @@ -144,6 +145,10 @@ class TextformatterVideoEmbed extends Textformatter implements ConfigurableModul public function format(&$str) { $this->embedYoutube($str); $this->embedVimeo($str); + $this->embedIssuu($str); + $this->embedTwitter($str); + $this->embedFacebook($str); + $this->embedInstagram($str); } /** @@ -159,7 +164,7 @@ class TextformatterVideoEmbed extends Textformatter implements ConfigurableModul && strpos($str, '://www.youtube.com/v/') === false && strpos($str, '://youtu.be/') === false) return; - // 1: full URL 2:video id 3: query string (optional) + // 1: full URL | 2:video id | 3: query string (optional) $regex = '#

\s*(https?://(?:www\.)?youtu(?:.be|be.com)+/(?:watch/?\?v=|v/)?([^\s&<\'"]+))(&[-_,.=&;a-zA-Z0-9]*)?.*?

#'; if(!preg_match_all($regex, $str, $matches)) return; @@ -212,6 +217,129 @@ class TextformatterVideoEmbed extends Textformatter implements ConfigurableModul } } + /** + * Check for Issuu URLS and embed when found + * + * @var string $str + * + */ + protected function embedIssuu(&$str) { + + if(strpos($str, '://issuu.com/') === false) return; + + if(!preg_match_all('#

\s*(https?://issuu.com/(.*)/(.*)).*?

#', $str, $matches)) return; + + foreach($matches[0] as $key => $line) { + + $oembedURL = + "$this->https://issuu.com/oembed?url=" . urlencode($matches[1][$key]) . + "&maxwidth={$this->maxWidth}&maxheight={$this->maxHeight}&format=json"; + + $videoID = $matches[3][$key]; + $embedCode = $this->getEmbedCode($oembedURL, $videoID); + if($this->responsive) $embedCode = $this->makeResponsive($embedCode); + + if($embedCode) $str = str_replace($line, $embedCode, $str); + } + } + + /** + * Check for Twitter URLS and embed when found + * + * @var string $str + * + */ + protected function embedTwitter(&$str) { + + if(strpos($str, '://twitter.com/') === false) return; + + if(!preg_match_all('#

\s*(https?://twitter.com/(.*)/(.*)).*?

#', $str, $matches)) return; + + foreach($matches[0] as $key => $line) { + + $oembedURL = + "$this->https://publish.twitter.com/oembed?url=" . urlencode($matches[1][$key]) . + "&maxwidth={$this->maxWidth}&maxheight={$this->maxHeight}&lang=es"; + + $videoID = 'tw-'.$matches[3][$key]; + $embedCode = $this->getEmbedCode($oembedURL, $videoID); + if($this->responsive) $embedCode = $this->makeResponsive($embedCode); + + if($embedCode) $str = str_replace($line, $embedCode, $str); + } + } + + /** + * Check for Facebook URLS and embed when found + * + * @var string $str + * + */ + protected function embedFacebook(&$str) { + + $pType = ''; + + if(strpos($str, '://www.facebook.com/') === false) return; + + if(!preg_match_all('#

\s*((https?:\/\/)?(www\.)?facebook.com\/(.*)/(videos|posts)?/(.*)).*?

#', $str, $matches)) return; + + foreach($matches[0] as $key => $line) { + + $pType = ($matches[5][$key]=='videos' ? 'video':($matches[5][$key]=='posts' ? 'post':'')); + + $oembedURL = + "$this->https://www.facebook.com/plugins/{$pType}/oembed.json/?url=" . urlencode($matches[1][$key]) . + "&maxwidth={$this->maxWidth}" ; + + $videoID = 'fb-'.$this->wire('sanitizer')->name($matches[6][$key]); + $embedCode = $this->getEmbedCode($oembedURL, $videoID); + + //if($this->responsive) $embedCode = $this->makeResponsive($embedCode); + + if($embedCode) $str = str_replace($line, $embedCode, $str); + } + + } + + /** + * Check for Instagram URLS and embed when found + * + * @var string $str + * + */ + protected function embedInstagram(&$str) { + + /**** + * Instagram URLs + * URL scheme: http://instagram.com/p/* + URL scheme: http://instagr.am/p/* + URL scheme: http://www.instagram.com/p/* + URL scheme: http://www.instagr.am/p/* + URL scheme: https://instagram.com/p/* + URL scheme: https://instagr.am/p/* + URL scheme: https://www.instagram.com/p/* + URL scheme: https://www.instagr.am/p/* + */ + + if(strpos($str, 'instagram.com/') === false + && strpos($str, 'instagr.am/') === false) return; + + if(!preg_match_all('#

\s*((https?:\/\/)?([\w\.]*)instagr(?:.am|am.com)+/p/(.*)/).*?

#', $str, $matches)) return; + + foreach($matches[0] as $key => $line) { + + $oembedURL = + "$this->https://api.instagram.com/oembed?url=" . urlencode($matches[1][$key]) . + "&maxwidth={$this->maxWidth}&maxheight={$this->maxHeight}"; + + $videoID = 'ig-'.$matches[4][$key]; + $embedCode = $this->getEmbedCode($oembedURL, $videoID); + if($this->responsive) $embedCode = $this->makeResponsive($embedCode); + + if($embedCode) $str = str_replace($line, $embedCode, $str); + } + } + /** * Module configuration screen * From 3a2614756a2ac85702a0650b2dfca08f64fadcca Mon Sep 17 00:00:00 2001 From: Lex Sanchez Date: Mon, 22 Jan 2018 23:54:14 -0500 Subject: [PATCH 2/3] A change is made to the format of the embed_code column of the textformatter_video_embed table from varchar(1024) to text; This is done so that the publications of social networks are not cut when they are cached in the DB. --- TextformatterVideoEmbed.module | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TextformatterVideoEmbed.module b/TextformatterVideoEmbed.module index f042acd..b6cc5cb 100644 --- a/TextformatterVideoEmbed.module +++ b/TextformatterVideoEmbed.module @@ -404,7 +404,7 @@ class TextformatterVideoEmbed extends Textformatter implements ConfigurableModul $sql = "CREATE TABLE " . self::dbTableName . " (" . "video_id VARCHAR(128) NOT NULL PRIMARY KEY, " . - "embed_code VARCHAR(1024) NOT NULL DEFAULT '', " . + "embed_code text NOT NULL DEFAULT '', " . "created TIMESTAMP NOT NULL " . ")"; From 3b864b7a91d456b97ad4048d6e9e760a19ec1553 Mon Sep 17 00:00:00 2001 From: Lex Sanchez Date: Wed, 24 Jan 2018 11:47:06 -0500 Subject: [PATCH 3/3] When the responsive option is not active, just replace the URL and keep the attributes assigned to the p tag (eg, align, style) --- TextformatterVideoEmbed.module | 86 +++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/TextformatterVideoEmbed.module b/TextformatterVideoEmbed.module index b6cc5cb..d5b9b47 100644 --- a/TextformatterVideoEmbed.module +++ b/TextformatterVideoEmbed.module @@ -165,7 +165,7 @@ class TextformatterVideoEmbed extends Textformatter implements ConfigurableModul && strpos($str, '://youtu.be/') === false) return; // 1: full URL | 2:video id | 3: query string (optional) - $regex = '#

\s*(https?://(?:www\.)?youtu(?:.be|be.com)+/(?:watch/?\?v=|v/)?([^\s&<\'"]+))(&[-_,.=&;a-zA-Z0-9]*)?.*?

#'; + $regex = '#]*>\s*(https?://(?:www\.)?youtu(?:.be|be.com)+/(?:watch/?\?v=|v/)?([^\s&<\'"]+))(&[-_,.=&;a-zA-Z0-9]*)?.*?

#'; if(!preg_match_all($regex, $str, $matches)) return; foreach($matches[0] as $key => $line) { @@ -201,45 +201,22 @@ class TextformatterVideoEmbed extends Textformatter implements ConfigurableModul if(strpos($str, '://vimeo.com/') === false) return; - if(!preg_match_all('#

\s*(https?://vimeo.com/(\d+)).*?

#', $str, $matches)) return; + if(!preg_match_all('#]*>\s*(https?://vimeo.com/(\d+)).*?

#', $str, $matches)) return; foreach($matches[0] as $key => $line) { - $oembedURL = + $replaceLine = $line; + $oembedURL = "$this->http://vimeo.com/api/oembed.json?url=" . urlencode($matches[1][$key]) . "&maxwidth={$this->maxWidth}&maxheight={$this->maxHeight}"; $videoID = $matches[2][$key]; $embedCode = $this->getEmbedCode($oembedURL, $videoID); - if($this->responsive) $embedCode = $this->makeResponsive($embedCode); - - if($embedCode) $str = str_replace($line, $embedCode, $str); - } - } - /** - * Check for Issuu URLS and embed when found - * - * @var string $str - * - */ - protected function embedIssuu(&$str) { - - if(strpos($str, '://issuu.com/') === false) return; - - if(!preg_match_all('#

\s*(https?://issuu.com/(.*)/(.*)).*?

#', $str, $matches)) return; - - foreach($matches[0] as $key => $line) { - - $oembedURL = - "$this->https://issuu.com/oembed?url=" . urlencode($matches[1][$key]) . - "&maxwidth={$this->maxWidth}&maxheight={$this->maxHeight}&format=json"; - - $videoID = $matches[3][$key]; - $embedCode = $this->getEmbedCode($oembedURL, $videoID); if($this->responsive) $embedCode = $this->makeResponsive($embedCode); + else $replaceLine = $matches[1][$key]; //Only replace the URL and keep the attributes assigned to the

tag (eg, align, style) - if($embedCode) $str = str_replace($line, $embedCode, $str); + if($embedCode) $str = str_replace($replaceLine, $embedCode, $str); } } @@ -253,19 +230,22 @@ class TextformatterVideoEmbed extends Textformatter implements ConfigurableModul if(strpos($str, '://twitter.com/') === false) return; - if(!preg_match_all('#

\s*(https?://twitter.com/(.*)/(.*)).*?

#', $str, $matches)) return; + if(!preg_match_all('#]*>\s*(https?://twitter.com/(.*)/(.*)).*?

#', $str, $matches)) return; foreach($matches[0] as $key => $line) { - $oembedURL = + $replaceLine = $line; + $oembedURL = "$this->https://publish.twitter.com/oembed?url=" . urlencode($matches[1][$key]) . - "&maxwidth={$this->maxWidth}&maxheight={$this->maxHeight}&lang=es"; + "&maxwidth={$this->maxWidth}&maxheight={$this->maxHeight}"; $videoID = 'tw-'.$matches[3][$key]; $embedCode = $this->getEmbedCode($oembedURL, $videoID); + if($this->responsive) $embedCode = $this->makeResponsive($embedCode); + else $replaceLine = $matches[1][$key]; //Only replace the URL and keep the attributes assigned to the

tag (eg, align, style) - if($embedCode) $str = str_replace($line, $embedCode, $str); + if($embedCode) $str = str_replace($replaceLine, $embedCode, $str); } } @@ -281,7 +261,7 @@ class TextformatterVideoEmbed extends Textformatter implements ConfigurableModul if(strpos($str, '://www.facebook.com/') === false) return; - if(!preg_match_all('#

\s*((https?:\/\/)?(www\.)?facebook.com\/(.*)/(videos|posts)?/(.*)).*?

#', $str, $matches)) return; + if(!preg_match_all('#]*>\s*((https?:\/\/)?(www\.)?facebook.com\/(.*)/(videos|posts)?/(.*)).*?

#', $str, $matches)) return; foreach($matches[0] as $key => $line) { @@ -324,19 +304,51 @@ class TextformatterVideoEmbed extends Textformatter implements ConfigurableModul if(strpos($str, 'instagram.com/') === false && strpos($str, 'instagr.am/') === false) return; - if(!preg_match_all('#

\s*((https?:\/\/)?([\w\.]*)instagr(?:.am|am.com)+/p/(.*)/).*?

#', $str, $matches)) return; + if(!preg_match_all('#]*>\s*((https?:\/\/)?([\w\.]*)instagr(?:.am|am.com)+/p/(.*)/).*?

#', $str, $matches)) return; foreach($matches[0] as $key => $line) { - $oembedURL = + $replaceLine = $line; + $oembedURL = "$this->https://api.instagram.com/oembed?url=" . urlencode($matches[1][$key]) . "&maxwidth={$this->maxWidth}&maxheight={$this->maxHeight}"; $videoID = 'ig-'.$matches[4][$key]; $embedCode = $this->getEmbedCode($oembedURL, $videoID); + if($this->responsive) $embedCode = $this->makeResponsive($embedCode); + else $replaceLine = $matches[1][$key]; //Only replace the URL and keep the attributes assigned to the

tag (eg, align, style) - if($embedCode) $str = str_replace($line, $embedCode, $str); + if($embedCode) $str = str_replace($replaceLine, $embedCode, $str); + } + } + + /** + * Check for Issuu URLS and embed when found + * + * @var string $str + * + */ + protected function embedIssuu(&$str) { + + if(strpos($str, '://issuu.com/') === false) return; + + if(!preg_match_all('#]*>\s*(https?://issuu.com/(.*)/(.*)).*?

#', $str, $matches)) return; + + foreach($matches[0] as $key => $line) { + + $replaceLine = $line; + $oembedURL = + "$this->https://issuu.com/oembed?url=" . urlencode($matches[1][$key]) . + "&maxwidth={$this->maxWidth}&maxheight={$this->maxHeight}&format=json"; + + $videoID = $matches[3][$key]; + $embedCode = $this->getEmbedCode($oembedURL, $videoID); + + if($this->responsive) $embedCode = $this->makeResponsive($embedCode); + else $replaceLine = $matches[1][$key]; //Only replace the URL and keep the attributes assigned to the

tag (eg, align, style) + + if($embedCode) $str = str_replace($replaceLine, $embedCode, $str); } }