Opengraph and Twitter card now possible to select a default image, fixed regex on img abs
This commit is contained in:
parent
26c59cea76
commit
b8eec33515
|
@ -35,7 +35,7 @@ function buildPage($key) {
|
|||
$contentRaw = $page->contentRaw();
|
||||
$content = Text::pre2htmlentities($contentRaw); // Parse pre code with htmlentities
|
||||
$content = $Parsedown->text($content); // Parse Markdown
|
||||
$content = Text::imgRel2Abs($content, HTML_PATH_UPLOADS); // Parse img src relative to absolute.
|
||||
$content = Text::imgRel2Abs($content, DOMAIN_UPLOADS); // Parse img src relative to absolute (with domain)
|
||||
$page->setField('content', $content, true);
|
||||
|
||||
// Pagebrake
|
||||
|
|
|
@ -220,8 +220,8 @@ class Text {
|
|||
|
||||
public static function imgRel2Abs($string, $base)
|
||||
{
|
||||
$pattern = "/<img([^>]*) src=\"([^http|https|ftp|\/\/][^\"]*)\"/";
|
||||
$replace = "<img\${1} src=\"".$base. "\${2}\"";
|
||||
$pattern = '/<img([^>]*)(src)=\"(?!https:)(?!http:)(?!\/\/)(.*?)\"(.*?)>/';
|
||||
$replace = "<img\${1} src=\"".$base."\${3}\" \${4}>";
|
||||
|
||||
return preg_replace($pattern, $replace, $string);
|
||||
}
|
||||
|
|
|
@ -2,25 +2,32 @@
|
|||
|
||||
class pluginOpenGraph extends Plugin {
|
||||
|
||||
// Returns the first image from the HTML content
|
||||
private function getImage($content)
|
||||
public function init()
|
||||
{
|
||||
$dom = new DOMDocument();
|
||||
$dom->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$content);
|
||||
$finder = new DomXPath($dom);
|
||||
|
||||
$images = $finder->query("//img");
|
||||
|
||||
if($images->length>0) {
|
||||
// First image from the list
|
||||
$image = $images->item(0);
|
||||
// Get value from attribute src
|
||||
$imgSrc = $image->getAttribute('src');
|
||||
// Returns the image src
|
||||
return $imgSrc;
|
||||
// Fields and default values for the database of this plugin
|
||||
$this->dbFields = array(
|
||||
'defaultImage'=>''
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
public function form()
|
||||
{
|
||||
global $Language;
|
||||
|
||||
$html = '<div>';
|
||||
$html .= '<label>'.$Language->get('Default image').'</label>';
|
||||
$html .= '<select name="defaultImage">';
|
||||
|
||||
$images = Filesystem::listFiles(PATH_UPLOADS);
|
||||
foreach ($images as $image) {
|
||||
$base = basename($image);
|
||||
$html .= '<option value="'.$base.'" '.(($this->getValue('defaultImage')==$base)?'selected':'').'>'.$base.'</option>';
|
||||
}
|
||||
|
||||
$html .= '</select>';
|
||||
$html .= '</div>';
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
public function siteHead()
|
||||
|
@ -77,7 +84,9 @@ class pluginOpenGraph extends Plugin {
|
|||
// Get the image from the content
|
||||
$src = $this->getImage($content);
|
||||
if ($src!==false) {
|
||||
$og['image'] = DOMAIN.$src;
|
||||
$og['image'] = $src;
|
||||
} else {
|
||||
$og['image'] = DOMAIN_UPLOADS.$this->getValue('defaultImage');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -85,4 +94,25 @@ class pluginOpenGraph extends Plugin {
|
|||
|
||||
return $html;
|
||||
}
|
||||
|
||||
// Returns the first image from the HTML content
|
||||
private function getImage($content)
|
||||
{
|
||||
$dom = new DOMDocument();
|
||||
$dom->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$content);
|
||||
$finder = new DomXPath($dom);
|
||||
|
||||
$images = $finder->query("//img");
|
||||
|
||||
if($images->length>0) {
|
||||
// First image from the list
|
||||
$image = $images->item(0);
|
||||
// Get value from attribute src
|
||||
$imgSrc = $image->getAttribute('src');
|
||||
// Returns the image src
|
||||
return $imgSrc;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -2,6 +2,34 @@
|
|||
|
||||
class pluginTwitterCards extends Plugin {
|
||||
|
||||
public function init()
|
||||
{
|
||||
// Fields and default values for the database of this plugin
|
||||
$this->dbFields = array(
|
||||
'defaultImage'=>''
|
||||
);
|
||||
}
|
||||
|
||||
public function form()
|
||||
{
|
||||
global $Language;
|
||||
|
||||
$html = '<div>';
|
||||
$html .= '<label>'.$Language->get('Default image').'</label>';
|
||||
$html .= '<select name="defaultImage">';
|
||||
|
||||
$images = Filesystem::listFiles(PATH_UPLOADS);
|
||||
foreach ($images as $image) {
|
||||
$base = basename($image);
|
||||
$html .= '<option value="'.$base.'" '.(($this->getValue('defaultImage')==$base)?'selected':'').'>'.$base.'</option>';
|
||||
}
|
||||
|
||||
$html .= '</select>';
|
||||
$html .= '</div>';
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
public function siteHead()
|
||||
{
|
||||
global $Url;
|
||||
|
@ -50,7 +78,9 @@ class pluginTwitterCards extends Plugin {
|
|||
// Get the image from the content
|
||||
$src = $this->getImage($content);
|
||||
if ($src!==false) {
|
||||
$data['image'] = DOMAIN.$src;
|
||||
$og['image'] = $src;
|
||||
} else {
|
||||
$og['image'] = DOMAIN_UPLOADS.$this->getValue('defaultImage');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue