ldc-jeusselin/app/Models/Articles.php
2021-06-30 12:32:51 +02:00

57 lines
2.7 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\App;
class Articles extends Model
{
protected $contentRender;
public function category()
{
return $this->belongsTo(ArticlesCategories::class, 'category_id');
}
/**
* Mutator method to set priceHt's value
*/
public function setContentRenderAttribute($value)
{
$this->contentRender = $value;
}
public function getContentRenderAttribute() {
if(isset($this->contentRender)) {
return $this->contentRender;
} else {
//youtube
$content = preg_replace('/<oembed.+?url="https?:\/\/www\.youtube\.com\/watch\?v=([a-zA-Z0-9_-]{11})"><\/oembed>/','<iframe width="560" height="315" src="https://www.youtube.com/embed/${1}" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>',$this->content);
//vimeo
$content = preg_replace('/<oembed.+?url="https?:\/\/vimeo\.com\/([a-zA-Z0-9_-]{9})"><\/oembed>/','<iframe src="https://player.vimeo.com/video/${1}" width="640" height="480" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen></iframe>',$content);
//twitter
//tweet oembed
//match '/<figure class="media"><oembed.+?url="https?:\/\/twitter\.com\/([a-zA-Z0-9_-].*)\?.*"><\/oembed><\/figure>/'
//generate 'https://publish.twitter.com/oembed?url=https://twitter.com/${1}
if (!App::environment("local")) {
$tweets = preg_match_all('/<figure class="media"><oembed.+?url="https?:\/\/twitter\.com\/([a-zA-Z0-9\/_-]*)(?:\?[^"])*"><\/oembed><\/figure>/',$content,$tweet_urls);
foreach ($tweet_urls[1] as $onetweet) {
$oembed = 'https://publish.twitter.com/oembed?url=https://twitter.com/' . $onetweet;
$response = Http::get($oembed);
$html_for_tweet = $response->json()['html'];
$pattern = '/<figure class="media"><oembed.+?url="https?:\/\/twitter\.com\/'.preg_quote($onetweet,'/').'(?:\?[^"])*"><\/oembed><\/figure>/';
$content = preg_replace($pattern,$html_for_tweet,$content);
}
} else {
$content = preg_replace('/<figure class="media"><oembed.+?url="https?:\/\/twitter\.com\/([a-zA-Z0-9\/_-]*)(?:\?[^"])*"><\/oembed><\/figure>/','<a href="https://twitter.com/${1}" target="_twitter">BEHIND PROXY RENDER : Tweet</a>',$content);
}
//$this->contentRender = $content;
return $this->contentRender = $content;
}
}
}