Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed Social Post #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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

Expand All @@ -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.

------
Expand Down
158 changes: 149 additions & 9 deletions TextformatterVideoEmbed.module
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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/'
);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -159,8 +164,8 @@ 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)
$regex = '#<p>\s*(https?://(?:www\.)?youtu(?:.be|be.com)+/(?:watch/?\?v=|v/)?([^\s&<\'"]+))(&[-_,.=&;a-zA-Z0-9]*)?.*?</p>#';
// 1: full URL | 2:video id | 3: query string (optional)
$regex = '#<p[^>]*>\s*(https?://(?:www\.)?youtu(?:.be|be.com)+/(?:watch/?\?v=|v/)?([^\s&<\'"]+))(&[-_,.=&;a-zA-Z0-9]*)?.*?</p>#';
if(!preg_match_all($regex, $str, $matches)) return;

foreach($matches[0] as $key => $line) {
Expand Down Expand Up @@ -196,22 +201,157 @@ class TextformatterVideoEmbed extends Textformatter implements ConfigurableModul

if(strpos($str, '://vimeo.com/') === false) return;

if(!preg_match_all('#<p>\s*(https?://vimeo.com/(\d+)).*?</p>#', $str, $matches)) return;
if(!preg_match_all('#<p[^>]*>\s*(https?://vimeo.com/(\d+)).*?</p>#', $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);
else $replaceLine = $matches[1][$key]; //Only replace the URL and keep the attributes assigned to the <p> tag (eg, align, style)

if($embedCode) $str = str_replace($replaceLine, $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('#<p[^>]*>\s*(https?://twitter.com/(.*)/(.*)).*?</p>#', $str, $matches)) return;

foreach($matches[0] as $key => $line) {

$replaceLine = $line;
$oembedURL =
"$this->https://publish.twitter.com/oembed?url=" . urlencode($matches[1][$key]) .
"&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 <p> tag (eg, align, style)

if($embedCode) $str = str_replace($replaceLine, $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('#<p[^>]*>\s*((https?:\/\/)?(www\.)?facebook.com\/(.*)/(videos|posts)?/(.*)).*?</p>#', $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('#<p[^>]*>\s*((https?:\/\/)?([\w\.]*)instagr(?:.am|am.com)+/p/(.*)/).*?</p>#', $str, $matches)) return;

foreach($matches[0] as $key => $line) {

$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 <p> tag (eg, align, style)

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('#<p[^>]*>\s*(https?://issuu.com/(.*)/(.*)).*?</p>#', $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 <p> tag (eg, align, style)

if($embedCode) $str = str_replace($replaceLine, $embedCode, $str);
}
}

/**
* Module configuration screen
*
Expand Down Expand Up @@ -276,7 +416,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 " .
")";

Expand Down