Improves Opengraph

This commit is contained in:
dignajar 2015-11-28 19:46:23 -03:00
parent 8d8cf62de8
commit 78c2a85fd7
1 changed files with 27 additions and 11 deletions

View File

@ -7,18 +7,21 @@ class pluginOpenGraph extends Plugin {
$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);
$classname = "bludit-img-opengraph";
$images = $finder->query("//img[contains(@class, '$classname')]"); $images = $finder->query("//img[contains(@class, 'bludit-img-opengraph')]");
if($images->length==0) {
$images = $finder->query("//img");
}
if($images->length>0) if($images->length>0)
{ {
// First image from the list // First image from the list
$image = $images->item(0); $image = $images->item(0);
// Get value from attribute src // Get value from attribute src
$coverImage = $image->getAttribute('src'); $imgSrc = $image->getAttribute('src');
// Returns the image src
return $coverImage; return $imgSrc;
} }
return false; return false;
@ -27,7 +30,7 @@ class pluginOpenGraph extends Plugin {
public function siteHead() public function siteHead()
{ {
global $Url, $Site; global $Url, $Site;
global $Post, $Page; global $Post, $Page, $posts;
$og = array( $og = array(
'locale' =>$Site->locale(), 'locale' =>$Site->locale(),
@ -46,14 +49,21 @@ 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'] = $Site->domain().$this->getImage($Post->content());
$content = $Post->content();
break; break;
case 'page': case 'page':
$og['type'] = 'article'; $og['type'] = 'article';
$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'] = $Site->domain().$this->getImage($Page->content());
$content = $Page->content();
break;
default:
$content = isset($posts[0])?$posts[0]->content():'';
break; break;
} }
@ -62,10 +72,16 @@ class pluginOpenGraph extends Plugin {
$html .= '<meta property="og:type" content="'.$og['type'].'">'.PHP_EOL; $html .= '<meta property="og:type" content="'.$og['type'].'">'.PHP_EOL;
$html .= '<meta property="og:title" content="'.$og['title'].'">'.PHP_EOL; $html .= '<meta property="og:title" content="'.$og['title'].'">'.PHP_EOL;
$html .= '<meta property="og:description" content="'.$og['description'].'">'.PHP_EOL; $html .= '<meta property="og:description" content="'.$og['description'].'">'.PHP_EOL;
$html .= '<meta property="og:image" content="'.$og['image'].'">'.PHP_EOL;
$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
$src = $this->getImage( $content );
if($src!==false) {
$og['image'] = $Site->domain().$src;
$html .= '<meta property="og:image" content="'.$og['image'].'">'.PHP_EOL;
}
return $html; return $html;
} }
} }