Skip to content
Snippets Groups Projects
Commit ddf814b6 authored by Andriy Tkachuk's avatar Andriy Tkachuk Committed by Andriy Tkachuk
Browse files

Issue #3107939 by loon: Duplicates of og properties caused a lot of errors

parent d48b6537
No related branches found
No related tags found
No related merge requests found
......@@ -2,20 +2,20 @@
/**
* Function to find all opengraph (and related) tags from a uri.
*
*
* Returns a (multidimensional) array on success, or an empty one on failure.
*/
function opengraph_filter_find_tags($uri, $reset = FALSE) {
$tags = &drupal_static(__FUNCTION__);
$cache_key = md5($uri);
if (!$reset) {
// Try to find results in static cache (memory).
if (!empty($tags) && isset($tags[$cache_key])) {
return $tags[$cache_key];
}
// Try to find results in cache table (database).
if ($cache = cache_get('tags:' . $cache_key, 'cache_opengraph_filter')) {
// Save result in static cache
......@@ -23,26 +23,27 @@ function opengraph_filter_find_tags($uri, $reset = FALSE) {
return $tags[$cache_key];
}
}
// We really do need to query the web, since we have no cached version.
// Let us start with an empty array.
$tags[$cache_key] = array(
'request_uri' => $uri,
);
if ($html = opengraph_filter_get_html($uri, $reset)) {
// Preg Query for title.
$search = '/<title>(.*)<\/title>/Ui';
if (preg_match($search, $html, $found)) {
$tags[$cache_key]['title'] = filter_xss(decode_entities($found[1]));
}
// Preg Query for correct metatags.
$search = '/<meta[^>]+(?<attr_1>name|property|value|content)\s?=\s?([\'"]+)(?<value_1>.*)?([\'"]+).*(?<attr_2>name|property|value|content)\s?=\s?([\'"]+)(?<value_2>.*)?([\'"]+).*>/Ui';
if (preg_match_all($search, $html, $found, PREG_PATTERN_ORDER)) {
$num_found = count($found[0]);
$properties = array();
for ($i = 0; $i < $num_found; $i++) {
if (in_array($found['attr_1'][$i], ['name', 'property']) && in_array($found['attr_2'][$i], ['value', 'content'])) {
$key_id = 'value_1';
......@@ -57,12 +58,17 @@ function opengraph_filter_find_tags($uri, $reset = FALSE) {
}
$found[$key_id][$i] = preg_replace('/[^a-z0-9:\._-]/', '', strtolower(trim($found[$key_id][$i])));
$found[$value_id][$i] = filter_xss(decode_entities(trim($found[$value_id][$i])));
// Ignore if either the tag name or the tag value is empty.
if (!drupal_strlen($found[$key_id][$i]) || !drupal_strlen($found[$value_id][$i])) {
continue;
}
// Skip duplicates of properties.
if (isset($properties[$found[$key_id][$i]])) {
continue;
}
$properties[$found[$key_id][$i]] = $found[$value_id][$i];
// Magic to put the found tags in a multidimensional array.
$these_tags = &$tags[$cache_key];
$keys = preg_split('/[\.|:]/', $found[$key_id][$i]);
......@@ -90,7 +96,7 @@ function opengraph_filter_find_tags($uri, $reset = FALSE) {
} else {
$these_tags[$key] = $found[$value_id][$i];
}
} else {
// We need to move one level deeper in our array.
......@@ -101,7 +107,7 @@ function opengraph_filter_find_tags($uri, $reset = FALSE) {
$these_tags[$key] = array($key => $these_tags[$key]);
}
$these_tags = &$these_tags[$key];
}
}
}
unset($these_tags);
}
......@@ -111,7 +117,7 @@ function opengraph_filter_find_tags($uri, $reset = FALSE) {
// Write results back to the database cache.
cache_set('tags:' . $cache_key, $tags[$cache_key], 'cache_opengraph_filter', (REQUEST_TIME + 3600));
return $tags[$cache_key];
}
......@@ -272,7 +278,7 @@ function _opengraph_filter_processor($text, $filter) {
$chunk_type = 'tag';
}
else {
// Only process this tag if there are no unclosed $ignore_tags.
if ($open_tag == '') {
// Check whether this tag is contained in $ignore_tags.
......@@ -335,13 +341,15 @@ function opengraph_filter_theme() {
}
function theme_opengraph_filter_image(&$variables) {
$return = '<span><a href="' . $variables['url'] . '" ';
$return .= 'title="' . check_plain($variables['title']) . '">';
if (is_array($variables['image'])) {
$src = $variables['image']['image'];
$height = NULL;
if (isset($variables['image']['width']) && isset($variables['image']['height'])) {
if (isset($variables['image']['width'], $variables['image']['height'])
&& is_numeric($variables['image']['width'])
&& is_numeric($variables['image']['height'])) {
$width = 80;
$height = floor(($width / $variables['image']['width']) * $variables['image']['height']);
}
......@@ -359,12 +367,12 @@ function theme_opengraph_filter_image(&$variables) {
function theme_opengraph_filter_images(&$variables) {
$return = '';
/*
foreach($variables['image'] as $image) {
$return .= theme('opengraph_image', array('image' => $image));
}
*
*
* For now we just return the first image.
* TODO: include a basic slider (don't forget to include the JS also!)
*/
......@@ -373,16 +381,16 @@ function theme_opengraph_filter_images(&$variables) {
}
function template_preprocess_opengraph_filter(&$variables) {
if (!count($variables['metatags']) && !empty($variables['uri'])) {
$tags = opengraph_filter_find_tags($variables['uri']);
if (count($tags) > 1) {
$variables['metatags'] = $tags;
}
}
$keys = array('title', 'description', 'image', 'url', 'type');
// Set default
foreach ($keys as $key) {
if (isset($variables['metatags'][$key])) {
......@@ -404,13 +412,13 @@ function template_preprocess_opengraph_filter(&$variables) {
}
}
}
// We need to show at least something!
// If tags is empty we give back an empty template in the tpl.php
if (!isset($variables['tags']['title']) && !isset($variables['tags']['description'])) {
$variables['tags'] = array();
}
// Detect multiple images.
if (isset($variables['tags']['image']) &&
is_array($variables['tags']['image']) &&
......@@ -435,14 +443,14 @@ function template_preprocess_opengraph_filter(&$variables) {
$domain['host'] = drupal_substr($domain['host'], 4);
}
$variables['classes_array'][] = str_replace('.', '-', $domain['host']);
if (!empty($variables['tags']['image'])) {
$theme_images = array(
'image' => $variables['tags']['image'],
'url' => $variables['tags']['url'],
'title' => isset($variables['tags']['title']) ? $variables['tags']['title'] : '',
);
if ($variables['multiple_images']) {
$variables['image'] = theme('opengraph_filter_images', $theme_images);
}
......@@ -455,7 +463,7 @@ function template_preprocess_opengraph_filter(&$variables) {
/**
* Implements hook_init().
*
*
* Really hate to do this, but filter output is cached, so we cannot add JS/CSS on the fly; when needed.
*/
function opengraph_filter_init() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment