Skip to content

Commit

Permalink
Now can add title and description from head meta even if no Open Grap…
Browse files Browse the repository at this point in the history
…h tags exist.
  • Loading branch information
AramZS committed Jun 13, 2012
1 parent f8dcc99 commit 8137872
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions OpenGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Original can be found at https://github.com/scottmac/opengraph/blob/master/OpenGraph.php
*/

class OpenGraph implements Iterator
Expand Down Expand Up @@ -72,22 +75,37 @@ static private function _parse($HTML) {

$page = new self();

$nonOgDescription = null;

foreach ($tags AS $tag) {
if ($tag->hasAttribute('property') &&
strpos($tag->getAttribute('property'), 'og:') === 0) {
$key = strtr(substr($tag->getAttribute('property'), 3), '-', '_');
$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.
if ($tag ->hasAttribute('value') && $tag->hasAttribute('property') &&
strpos($tag->getAttribute('property'), 'og:') === 0) {
$key = strtr(substr($tag->getAttribute('property'), 3), '-', '_');
$page->_values[$key] = $tag->getAttribute('value');
}
//Based on modifications at https://github.com/bashofmann/opengraph/blob/master/src/OpenGraph/OpenGraph.php
if ($tag->hasAttribute('name') && $tag->getAttribute('name') === 'description') {
$nonOgDescription = $tag->getAttribute('content');
}

}
//Based on modifications at https://github.com/bashofmann/opengraph/blob/master/src/OpenGraph/OpenGraph.php
if (!isset($page->_values['title'])) {
$titles = $doc->getElementsByTagName('title');
if ($titles->length > 0) {
$page->_values['title'] = $titles->item(0)->textContent;
}
}
if (!isset($page->_values['description']) && $nonOgDescription) {
$page->_values['description'] = $nonOgDescription;
}

if (empty($page->_values)) { return false; }

Expand Down Expand Up @@ -160,4 +178,4 @@ public function current() { return current($this->_values); }
public function key() { return key($this->_values); }
public function next() { next($this->_values); ++$this->_position; }
public function valid() { return $this->_position < sizeof($this->_values); }
}
}

0 comments on commit 8137872

Please sign in to comment.