generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from tonysm/tm/custom-content-attachments-with…
…out-sgid Support custom content attachments that dont require an sgid
- Loading branch information
Showing
13 changed files
with
461 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
namespace Workbench\App\Models\Opengraph; | ||
|
||
use DOMElement; | ||
use Tonysm\RichTextLaravel\Attachables\AttachableContract; | ||
|
||
class OpengraphEmbed implements AttachableContract | ||
{ | ||
use OpengraphEmbed\Fetching; | ||
|
||
const ATTRIBUTES = ['title', 'url', 'image', 'description']; | ||
|
||
const CONTENT_TYPE = 'application/vnd.rich-text-laravel.opengraph-embed'; | ||
|
||
public static function fromNode(DOMElement $node): ?OpengraphEmbed | ||
{ | ||
if ($node->hasAttribute('content-type') && $node->getAttribute('content-type') === static::CONTENT_TYPE) { | ||
return new OpengraphEmbed(...static::attributesFromNode($node)); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static function tryFromAttributes(array $attributes) | ||
{ | ||
if (validator($attributes, [ | ||
'title' => ['required'], | ||
'url' => ['required'], | ||
'description' => ['required'], | ||
'image' => ['sometimes', 'required', 'url'], | ||
])->fails()) { | ||
return null; | ||
} | ||
|
||
return new static( | ||
$attributes['url'], | ||
$attributes['image'] ?? null, | ||
$attributes['title'], | ||
$attributes['description'], | ||
); | ||
} | ||
|
||
private static function attributesFromNode(DOMElement $node): array | ||
{ | ||
return [ | ||
'href' => $node->getAttribute('href'), | ||
'url' => $node->getAttribute('url'), | ||
'filename' => $node->getAttribute('filename'), | ||
'description' => $node->getAttribute('caption'), | ||
]; | ||
} | ||
|
||
public function __construct( | ||
public $href, | ||
public $url, | ||
public $filename, | ||
public $description, | ||
) { | ||
} | ||
|
||
public function toRichTextAttributes(array $attributes): array | ||
{ | ||
return collect($attributes) | ||
->replace([ | ||
'content_type' => $this->richTextContentType(), | ||
'previewable' => true, | ||
]) | ||
->filter() | ||
->all(); | ||
} | ||
|
||
public function equalsToAttachable(AttachableContract $attachable): bool | ||
{ | ||
return $this->richTextRender() === $attachable->richTextRender(); | ||
} | ||
|
||
public function richTextRender(array $options = []): string | ||
{ | ||
return view('rich-text-laravel.attachables.opengraph_embed', [ | ||
'attachable' => $this, | ||
])->render(); | ||
} | ||
|
||
public function richTextAsPlainText(?string $caption = null): string | ||
{ | ||
return ''; | ||
} | ||
|
||
public function richTextContentType(): string | ||
{ | ||
return static::CONTENT_TYPE; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'href' => $this->href, | ||
'url' => $this->url, | ||
'filename' => $this->filename, | ||
'description' => $this->description, | ||
'contentType' => $this->richTextContentType(), | ||
'content' => $this->richTextRender(), | ||
]; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
workbench/app/Models/Opengraph/OpengraphEmbed/Fetching.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Workbench\App\Models\Opengraph\OpengraphEmbed; | ||
|
||
use DOMDocument; | ||
use DOMXPath; | ||
use Illuminate\Support\Facades\Http; | ||
use Tonysm\RichTextLaravel\HtmlConversion; | ||
use Workbench\App\Models\Opengraph\OpengraphEmbed; | ||
|
||
trait Fetching | ||
{ | ||
public static function createFromUrl(string $url): ?OpengraphEmbed | ||
{ | ||
$attributes = static::extractAttributesFromDocument(static::fetchDocument($url)); | ||
|
||
return OpengraphEmbed::tryFromAttributes($attributes); | ||
} | ||
|
||
private static function fetchDocument(string $url) | ||
{ | ||
return HtmlConversion::document( | ||
Http::withUserAgent('curl/7.81.0') | ||
->maxRedirects(10) | ||
->get(static::replaceTwitterDomainFromTweetUrls($url)) | ||
->throw() | ||
->body() | ||
); | ||
} | ||
|
||
private static function replaceTwitterDomainFromTweetUrls(string $url) | ||
{ | ||
$domains = [ | ||
'www.x.com', | ||
'www.twitter.com', | ||
'twitter.com', | ||
'x.com', | ||
]; | ||
|
||
$host = parse_url($url)['host']; | ||
|
||
return in_array($host, $domains, strict: true) | ||
? str_replace($host, 'fxtwitter.com', $url) | ||
: $url; | ||
} | ||
|
||
private static function extractAttributesFromDocument(DOMDocument $document): array | ||
{ | ||
$xpath = new DOMXPath($document); | ||
$openGraphTags = $xpath->query('//meta[starts-with(@property, "og:") or starts-with(@name, "og:")]'); | ||
$attributes = []; | ||
|
||
foreach ($openGraphTags as $tag) { | ||
if (! $tag->hasAttribute('content')) { | ||
continue; | ||
} | ||
|
||
$key = str_replace('og:', '', $tag->hasAttribute('property') ? $tag->getAttribute('property') : $tag->getAttribute('name')); | ||
|
||
if (! in_array($key, OpengraphEmbed::ATTRIBUTES, true)) { | ||
continue; | ||
} | ||
|
||
$attributes[$key] = $tag->getAttribute('content'); | ||
} | ||
|
||
return $attributes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.