DOM helper on plugins
This commit is contained in:
parent
20151c7f05
commit
df978c0a75
|
@ -4,6 +4,8 @@ class DOM {
|
||||||
|
|
||||||
public static function getFirstImage($content)
|
public static function getFirstImage($content)
|
||||||
{
|
{
|
||||||
|
// Disable warning
|
||||||
|
libxml_use_internal_errors();
|
||||||
$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);
|
||||||
|
|
|
@ -89,7 +89,7 @@ class pluginOpenGraph extends Plugin {
|
||||||
// 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 = DOM::getFirstImage($content);
|
||||||
if ($src!==false) {
|
if ($src!==false) {
|
||||||
$og['image'] = $src;
|
$og['image'] = $src;
|
||||||
} else {
|
} else {
|
||||||
|
@ -103,24 +103,4 @@ 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -83,7 +83,7 @@ class pluginTwitterCards extends Plugin {
|
||||||
// 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($data['image']) ) {
|
if( empty($data['image']) ) {
|
||||||
// Get the image from the content
|
// Get the image from the content
|
||||||
$src = $this->getImage($content);
|
$src = DOM::getFirstImage($content);
|
||||||
if ($src!==false) {
|
if ($src!==false) {
|
||||||
$data['image'] = $src;
|
$data['image'] = $src;
|
||||||
} else {
|
} else {
|
||||||
|
@ -96,25 +96,4 @@ class pluginTwitterCards extends Plugin {
|
||||||
$html .= '<meta property="twitter:image" content="'.$data['image'].'">'.PHP_EOL;
|
$html .= '<meta property="twitter:image" content="'.$data['image'].'">'.PHP_EOL;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue