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(); $contentRaw = $page->contentRaw();
$content = Text::pre2htmlentities($contentRaw); // Parse pre code with htmlentities $content = Text::pre2htmlentities($contentRaw); // Parse pre code with htmlentities
$content = $Parsedown->text($content); // Parse Markdown $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); $page->setField('content', $content, true);
// Pagebrake // Pagebrake

View File

@ -220,8 +220,8 @@ class Text {
public static function imgRel2Abs($string, $base) public static function imgRel2Abs($string, $base)
{ {
$pattern = "/<img([^>]*) src=\"([^http|https|ftp|\/\/][^\"]*)\"/"; $pattern = '/<img([^>]*)(src)=\"(?!https:)(?!http:)(?!\/\/)(.*?)\"(.*?)>/';
$replace = "<img\${1} src=\"".$base. "\${2}\""; $replace = "<img\${1} src=\"".$base."\${3}\" \${4}>";
return preg_replace($pattern, $replace, $string); return preg_replace($pattern, $replace, $string);
} }

View File

@ -2,25 +2,32 @@
class pluginOpenGraph extends Plugin { class pluginOpenGraph extends Plugin {
// Returns the first image from the HTML content public function init()
private function getImage($content)
{ {
$dom = new DOMDocument(); // Fields and default values for the database of this plugin
$dom->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$content); $this->dbFields = array(
$finder = new DomXPath($dom); 'defaultImage'=>''
);
}
$images = $finder->query("//img"); public function form()
{
global $Language;
if($images->length>0) { $html = '<div>';
// First image from the list $html .= '<label>'.$Language->get('Default image').'</label>';
$image = $images->item(0); $html .= '<select name="defaultImage">';
// Get value from attribute src
$imgSrc = $image->getAttribute('src'); $images = Filesystem::listFiles(PATH_UPLOADS);
// Returns the image src foreach ($images as $image) {
return $imgSrc; $base = basename($image);
$html .= '<option value="'.$base.'" '.(($this->getValue('defaultImage')==$base)?'selected':'').'>'.$base.'</option>';
} }
return false; $html .= '</select>';
$html .= '</div>';
return $html;
} }
public function siteHead() public function siteHead()
@ -41,7 +48,7 @@ class pluginOpenGraph extends Plugin {
'siteName' =>$Site->title() 'siteName' =>$Site->title()
); );
switch($WHERE_AM_I) { switch ($WHERE_AM_I) {
// The user filter by page // The user filter by page
case 'page': case 'page':
$og['type'] = 'article'; $og['type'] = 'article';
@ -73,11 +80,13 @@ class pluginOpenGraph extends Plugin {
$html .= '<meta property="og:siteName" content="'.$og['siteName'].'">'.PHP_EOL; $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 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 // Get the image from the content
$src = $this->getImage($content); $src = $this->getImage($content);
if($src!==false) { 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; 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 { 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() public function siteHead()
{ {
global $Url; global $Url;
@ -49,8 +77,10 @@ class pluginTwitterCards extends Plugin {
if( empty($data['image']) ) { if( empty($data['image']) ) {
// Get the image from the content // Get the image from the content
$src = $this->getImage($content); $src = $this->getImage($content);
if($src!==false) { if ($src!==false) {
$data['image'] = DOMAIN.$src; $og['image'] = $src;
} else {
$og['image'] = DOMAIN_UPLOADS.$this->getValue('defaultImage');
} }
} }