Opengraph and Twitter card now possible to select a default image, fixed regex on img abs

This commit is contained in:
Diego Najar 2017-10-07 02:45:03 +02:00
parent 26c59cea76
commit b8eec33515
4 changed files with 83 additions and 23 deletions

View File

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

View File

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

View File

@ -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()
@ -41,7 +48,7 @@ class pluginOpenGraph extends Plugin {
'siteName' =>$Site->title()
);
switch($WHERE_AM_I) {
switch ($WHERE_AM_I) {
// The user filter by page
case 'page':
$og['type'] = 'article';
@ -73,11 +80,13 @@ class pluginOpenGraph extends Plugin {
$html .= '<meta property="og:siteName" content="'.$og['siteName'].'">'.PHP_EOL;
// If the page doesn't have a coverImage try to get an image from the HTML content
if( empty($og['image']) ) {
if (empty($og['image'])) {
// Get the image from the content
$src = $this->getImage($content);
if($src!==false) {
$og['image'] = DOMAIN.$src;
if ($src!==false) {
$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;
}
}

View File

@ -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;
@ -49,8 +77,10 @@ class pluginTwitterCards extends Plugin {
if( empty($data['image']) ) {
// Get the image from the content
$src = $this->getImage($content);
if($src!==false) {
$data['image'] = DOMAIN.$src;
if ($src!==false) {
$og['image'] = $src;
} else {
$og['image'] = DOMAIN_UPLOADS.$this->getValue('defaultImage');
}
}