Opengraph with cover image

This commit is contained in:
dignajar 2016-01-08 00:04:59 -03:00
parent 652bc7353f
commit 5f3397b18f
4 changed files with 34 additions and 6 deletions

View File

View File

@ -115,6 +115,10 @@ class Page extends fileContent
{ {
$fileName = $this->getField('coverImage'); $fileName = $this->getField('coverImage');
if(empty($fileName)) {
return false;
}
if($absolute) { if($absolute) {
return HTML_PATH_UPLOADS.$fileName; return HTML_PATH_UPLOADS.$fileName;
} }

View File

@ -82,6 +82,10 @@ class Post extends fileContent
{ {
$fileName = $this->getField('coverImage'); $fileName = $this->getField('coverImage');
if(empty($fileName)) {
return false;
}
if($absolute) { if($absolute) {
return HTML_PATH_UPLOADS.$fileName; return HTML_PATH_UPLOADS.$fileName;
} }

View File

@ -2,17 +2,22 @@
class pluginOpenGraph extends Plugin { class pluginOpenGraph extends Plugin {
// Returns the first image that is in the content
private function getImage($content) private function getImage($content)
{ {
$dom = new DOMDocument(); $dom = new DOMDocument();
$dom->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$content); $dom->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$content);
$finder = new DomXPath($dom); $finder = new DomXPath($dom);
/* DEPRECATED
$images = $finder->query("//img[contains(@class, 'bludit-img-opengraph')]"); $images = $finder->query("//img[contains(@class, 'bludit-img-opengraph')]");
if($images->length==0) { if($images->length==0) {
$images = $finder->query("//img"); $images = $finder->query("//img");
} }
*/
$images = $finder->query("//img");
if($images->length>0) if($images->length>0)
{ {
@ -49,6 +54,7 @@ class pluginOpenGraph extends Plugin {
$og['title'] = $Post->title().' | '.$og['title']; $og['title'] = $Post->title().' | '.$og['title'];
$og['description'] = $Post->description(); $og['description'] = $Post->description();
$og['url'] = $Post->permalink(true); $og['url'] = $Post->permalink(true);
$og['image'] = $Post->coverImage(false);
$content = $Post->content(); $content = $Post->content();
break; break;
@ -58,12 +64,16 @@ class pluginOpenGraph extends Plugin {
$og['title'] = $Page->title().' | '.$og['title']; $og['title'] = $Page->title().' | '.$og['title'];
$og['description'] = $Page->description(); $og['description'] = $Page->description();
$og['url'] = $Page->permalink(true); $og['url'] = $Page->permalink(true);
$og['image'] = $Page->coverImage(false);
$content = $Page->content(); $content = $Page->content();
break; break;
default: default:
$content = isset($posts[0])?$posts[0]->content():''; if(isset($posts[0])) {
$og['image'] = $posts[0]->coverImage(false);
$content = $posts[0]->content();
}
break; break;
} }
@ -75,11 +85,21 @@ class pluginOpenGraph extends Plugin {
$html .= '<meta property="og:url" content="'.$og['url'].'">'.PHP_EOL; $html .= '<meta property="og:url" content="'.$og['url'].'">'.PHP_EOL;
$html .= '<meta property="og:siteName" content="'.$og['siteName'].'">'.PHP_EOL; $html .= '<meta property="og:siteName" content="'.$og['siteName'].'">'.PHP_EOL;
// Get the image from the content $domain = trim($Site->domain(),'/');
$src = $this->getImage( $content ); $urlImage = $domain.HTML_PATH_UPLOADS;
if($src!==false) {
$og['image'] = $Site->domain().$src; // If the post o page doesn't have a coverImage try to get it from the content
$html .= '<meta property="og:image" content="'.$og['image'].'">'.PHP_EOL; if($og['image']===false)
{
// Get the image from the content
$src = $this->getImage( $content );
if($src!==false) {
$html .= '<meta property="og:image" content="'.$urlImage.$og['image'].'">'.PHP_EOL;
}
}
else
{
$html .= '<meta property="og:image" content="'.$urlImage.$og['image'].'">'.PHP_EOL;
} }
return $html; return $html;