Skip to content

Commit

Permalink
Retain backcompat in cases of multiple og:image tags
Browse files Browse the repository at this point in the history
  • Loading branch information
AramZS committed Jul 12, 2016
1 parent 31e6e60 commit 6d71e1c
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions OpenGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,15 @@ static private function _parse($HTML) {
foreach ($tags AS $tag) {
if ($tag->hasAttribute('property') && strpos($tag->getAttribute('property'), 'og:') === 0) {
$key = strtr(substr($tag->getAttribute('property'), 3), '-', '_');

if(isset($key)){
if(!is_array($page->_values[$key])){
$temp = $page->_values[$key];
$page->_values[$key] = array($temp);
}
$page->_values[$key][] = $tag->getAttribute('content');
}else{
$page->_values[$key] = $tag->getAttribute('content');
}

if( array_key_exists($key, $page->_values) ){
if ( !array_key_exists($key.'_additional', $page->_values) ){
$page->_values[$key.'_additional'] = array();
}
$page->_values[$key.'_additional'][] = $tag->getAttribute('content');
}else{
$page->_values[$key] = $tag->getAttribute('content');
}
}

//Added this if loop to retrieve description values from sites like the New York Times who have malformed it.
Expand All @@ -137,7 +136,14 @@ static private function _parse($HTML) {
if ($tag->hasAttribute('name') &&
strpos($tag->getAttribute('name'), 'twitter:') === 0) {
$key = strtr($tag->getAttribute('name'), '-:', '__');
$page->_values[$key] = $tag->getAttribute('content');
if( array_key_exists($key, $page->_values) ){
if (!array_key_exists($key.'_additional', $page->_values)){
$page->_values[$key.'_additional'] = array();
}
$page->_values[$key.'_additional'][] = $tag->getAttribute('content');
} else {
$page->_values[$key] = $tag->getAttribute('content');
}
}
}
//Based on modifications at https://github.com/bashofmann/opengraph/blob/master/src/OpenGraph/OpenGraph.php
Expand Down

1 comment on commit 6d71e1c

@AramZS
Copy link
Owner Author

@AramZS AramZS commented on 6d71e1c Jul 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the final step to resolve
scottmac#37
and
scottmac#33

Thanks for the contributions @GregersBoye and @bruce133

Please sign in to comment.