opengraph_meta.common.inc in Open Graph meta tags 7.2
File
opengraph_meta.common.inc
View source
<?php
class OpenGraphMeta {
private static $instance = NULL;
protected $thumbnailStyle;
public function __construct($thumbnail_style = NULL) {
$default = module_exists('media') ? 'media_thumbnail' : 'thumbnail';
$this->thumbnailStyle = $thumbnail_style ?: variable_get('opengraph_meta_thumbnail_style', $default);
}
public static function instance() {
if (empty(self::$instance)) {
self::$instance = new OpenGraphMeta();
}
return self::$instance;
}
public function harvestImagesFromNode($node) {
$ret = $this
->extractImageFields((array) $node);
$body = $this
->getNodeBody($node);
if (!empty($body)) {
libxml_use_internal_errors(TRUE);
$doc = new DOMDocument();
$doc
->loadHTML($body);
$list = $doc
->getElementsByTagName('img');
for ($i = 0; $list->length > $i; ++$i) {
$item = $list
->item($i);
if ($item
->hasAttribute('src')) {
$url = $item
->getAttribute('src');
if (!empty($url)) {
$thumb_url = image_style_url($this->thumbnailStyle, $url);
$ret[$url] = array(
'title' => $url,
'alt' => $url,
'url' => $thumb_url,
);
}
}
}
libxml_use_internal_errors(FALSE);
}
return $ret;
}
public function getNodeBody($node) {
$body = '';
$lang = field_language('node', $node, 'body');
$lang = $lang ? $lang : LANGUAGE_NONE;
if (!empty($node->body[$lang]['0']['value'])) {
$body = $node->body[$lang]['0']['value'];
}
return $body;
}
public function extractImageFields($fields) {
$result = [];
if (is_array($fields)) {
if (!empty($fields['filemime']) && FALSE !== stripos($fields['filemime'], 'image') && !empty($fields['uri'])) {
$url = $fields['uri'];
$thumb_url = image_style_url($this->thumbnailStyle, $fields['uri']);
$result[$url] = array(
'title' => !empty($fields['title']) ? $fields['title'] : $url,
'alt' => !empty($fields['alt']) ? $fields['alt'] : $url,
'url' => $thumb_url,
);
}
else {
foreach ($fields as $cv) {
$result += $this
->extractImageFields($cv);
}
}
}
return $result;
}
}
class OpengraphImageMetaTag extends DrupalTextMetaTag {
public function getValue(array $options = array()) {
$value = parent::getValue($options);
$new_urls = [];
foreach (explode(',', $value) as $p) {
$new_urls[] = url(file_create_url(ltrim($p, '/')), array(
'absolute' => TRUE,
));
}
return implode(',', $new_urls);
}
}