From 5f3397b18f91f3f61f9c143f64bc070b12cfb74d Mon Sep 17 00:00:00 2001
From: dignajar <dignajar@gmail.com>
Date: Fri, 8 Jan 2016 00:04:59 -0300
Subject: [PATCH] Opengraph with cover image

---
 content/sitemap.xml          |  0
 kernel/page.class.php        |  4 ++++
 kernel/post.class.php        |  4 ++++
 plugins/opengraph/plugin.php | 32 ++++++++++++++++++++++++++------
 4 files changed, 34 insertions(+), 6 deletions(-)
 delete mode 100644 content/sitemap.xml

diff --git a/content/sitemap.xml b/content/sitemap.xml
deleted file mode 100644
index e69de29b..00000000
diff --git a/kernel/page.class.php b/kernel/page.class.php
index e4456981..fdbf92df 100644
--- a/kernel/page.class.php
+++ b/kernel/page.class.php
@@ -115,6 +115,10 @@ class Page extends fileContent
 	{
 		$fileName = $this->getField('coverImage');
 
+		if(empty($fileName)) {
+			return false;
+		}
+
 		if($absolute) {
 			return HTML_PATH_UPLOADS.$fileName;
 		}
diff --git a/kernel/post.class.php b/kernel/post.class.php
index a0a4e3ad..c8789bc3 100644
--- a/kernel/post.class.php
+++ b/kernel/post.class.php
@@ -82,6 +82,10 @@ class Post extends fileContent
 	{
 		$fileName = $this->getField('coverImage');
 
+		if(empty($fileName)) {
+			return false;
+		}
+
 		if($absolute) {
 			return HTML_PATH_UPLOADS.$fileName;
 		}
diff --git a/plugins/opengraph/plugin.php b/plugins/opengraph/plugin.php
index 96c5c6a1..902fe798 100644
--- a/plugins/opengraph/plugin.php
+++ b/plugins/opengraph/plugin.php
@@ -2,17 +2,22 @@
 
 class pluginOpenGraph extends Plugin {
 
+	// Returns the first image that is in the 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);
 
+		/* DEPRECATED
 		$images = $finder->query("//img[contains(@class, 'bludit-img-opengraph')]");
 
 		if($images->length==0) {
 			$images = $finder->query("//img");
 		}
+		*/
+
+		$images = $finder->query("//img");
 
 		if($images->length>0)
 		{
@@ -49,6 +54,7 @@ class pluginOpenGraph extends Plugin {
 				$og['title']		= $Post->title().' | '.$og['title'];
 				$og['description']	= $Post->description();
 				$og['url']		= $Post->permalink(true);
+				$og['image'] 		= $Post->coverImage(false);
 
 				$content = $Post->content();
 				break;
@@ -58,12 +64,16 @@ class pluginOpenGraph extends Plugin {
 				$og['title']		= $Page->title().' | '.$og['title'];
 				$og['description']	= $Page->description();
 				$og['url']		= $Page->permalink(true);
+				$og['image'] 		= $Page->coverImage(false);
 
 				$content = $Page->content();
 				break;
 
 			default:
-				$content = isset($posts[0])?$posts[0]->content():'';
+				if(isset($posts[0])) {
+					$og['image'] = $posts[0]->coverImage(false);
+					$content = $posts[0]->content();
+				}
 				break;
 		}
 
@@ -75,11 +85,21 @@ class pluginOpenGraph extends Plugin {
 		$html .= '<meta property="og:url" content="'.$og['url'].'">'.PHP_EOL;
 		$html .= '<meta property="og:siteName" content="'.$og['siteName'].'">'.PHP_EOL;
 
-		// Get the image from the content
-		$src = $this->getImage( $content );
-		if($src!==false) {
-			$og['image'] = $Site->domain().$src;
-			$html .= '<meta property="og:image" content="'.$og['image'].'">'.PHP_EOL;
+		$domain = trim($Site->domain(),'/');
+		$urlImage = $domain.HTML_PATH_UPLOADS;
+
+		// If the post o page doesn't have a coverImage try to get it from the content
+		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;