Skip to content

Commit

Permalink
facebook completed
Browse files Browse the repository at this point in the history
  • Loading branch information
supun-io committed Oct 23, 2024
1 parent 7daa014 commit 67783fb
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 10 deletions.
35 changes: 33 additions & 2 deletions src/Embed/Platforms/FacebookVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,44 @@
namespace Hyvor\Unfold\Embed\Platforms;

use Hyvor\Unfold\Embed\EmbedParserAbstract;
use Hyvor\Unfold\Embed\EmbedParserCustomInterface;
use Hyvor\Unfold\Embed\PlatformHelpers\FacebookHelper;

class FacebookVideo extends EmbedParserAbstract
class FacebookVideo extends EmbedParserAbstract implements EmbedParserCustomInterface
{
public const PRIORITY = 3;

public function regex()
{
return [];
return [
/**
* The following regex are derived from the FacebookVideo oembed schema
* "https://www.facebook.com/* /videos/*",
* "https://www.facebook.com/video.php?id=*",
* "https://www.facebook.com/video.php?v=*"
*/

// with username
'https?://(www|m|business)\.facebook\.com/([^/]+)/videos/([^/]+)',

// video.php with id or v
'https?://(www|m|business)\.facebook\.com/video\.php\?[^/]*(id|v)=([^&]+)',

// watch with v
'https?://(www|m|business)\.facebook\.com/watch/?\?[^/]*v=([^&]+)',

// reel
'https?://(www|m|business)\.facebook\.com/reel/([^/]+)',
];
}

public function getEmbedHtml(array $matches): string
{
$sdk = FacebookHelper::sdkScript();
$url = $this->url;
return <<<HTML
$sdk
<div class="fb-video" data-href="$url" data-width="500" data-show-text="true"></div>
HTML;
}
}
24 changes: 19 additions & 5 deletions tests/Unit/Embed/Platforms/FacebookPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,27 @@
use Hyvor\Unfold\Embed\Platforms\FacebookPage;
use Hyvor\Unfold\Embed\Platforms\FacebookPost;

it('matches a facebook page', function () {
$url = 'https://www.facebook.com/GMANetwork/about';
//it('matches a facebook page', function () {
// $url = 'https://www.facebook.com/GMANetwork/about';
// $parser = new FacebookPage($url);
//
// $response = $parser->parse($parser->match());
// var_dump($response->html);
// expect($response->html)->toBeString();
//});

it('embeds facebook page', function () {
$url = 'https://www.facebook.com/geonarah';

$parser = new FacebookPage($url);
$match = $parser->match();
$response = $parser->parse($match);

$response = $parser->parse($parser->match());
var_dump($response->html);
expect($response->html)->toBeString();
$html = $response->html;
expect($html)->toContain('<div class="fb-page" data-href="' . $url);
expect($html)->toContain(
'<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v21.0">'
);
});

it('embed class prioritizes facebook post before page', function () {
Expand Down
8 changes: 5 additions & 3 deletions tests/Unit/Embed/Platforms/FacebookPostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
$match = $parser->match();
$response = $parser->parse($match);

var_dump($response->html);
expect(true)->toBeTrue();
//dd($response->html);
$html = $response->html;
expect($html)->toContain('<div class="fb-post" data-href="' . $url);
expect($html)->toContain(
'<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v21.0">'
);
});

it('matches', function ($url) {
Expand Down
48 changes: 48 additions & 0 deletions tests/Unit/Embed/Platforms/FacebookVideoTest.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,51 @@
<?php

namespace Unit\Parsers;

use Hyvor\Unfold\Embed\Platforms\FacebookVideo;

//it('manual', function () {
// $url = 'https://www.facebook.com/reel/416122534780426';
// $facebook = new FacebookVideo($url);
// $embed = $facebook->getEmbedHtml($facebook->match());
//
// dd($embed);
//});

it('embeds facebook videos', function () {
$url = 'https://www.facebook.com/username/videos/123456789';

$parser = new FacebookVideo($url);
$match = $parser->match();
$response = $parser->parse($match);

$html = $response->html;
expect($html)->toContain('<div class="fb-video" data-href="' . $url);
expect($html)->toContain(
'<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v21.0">'
);
});

it('matches', function ($url) {
$facebook = new FacebookVideo($url);
expect($facebook->match())->toBeArray();
})->with([
// with username
'https://www.facebook.com/username/videos/123456789',

// video.php with id
'https://www.facebook.com/video.php?id=123456789',
'https://www.facebook.com/video.php?s=1&id=123456789',

// video.php with v
'https://www.facebook.com/video.php?v=123456789',
'https://www.facebook.com/video.php?s=1&v=123456789',

// watch
'https://www.facebook.com/watch/?v=123456789',
'https://www.facebook.com/watch/?s=1&v=123456789',
'https://www.facebook.com/watch?v=123456789',

// reel
'https://www.facebook.com/reel/123456789',
]);

0 comments on commit 67783fb

Please sign in to comment.