Skip to content

Commit

Permalink
fix #8 外部リンクのブログカードを作る
Browse files Browse the repository at this point in the history
ブログカード作成のショートコードを追加。
SSL接続でエラーが出るサーバーあり。ogp画像は先方サーバ画像の読み込みなので、将来的には対応要?
  • Loading branch information
ultimate-ez committed Aug 28, 2018
1 parent 3fa8e5d commit a8c44d9
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 0 deletions.
115 changes: 115 additions & 0 deletions lib/ogp/Parser.php
@@ -0,0 +1,115 @@
<?php

/**
* @file
* Open Graph Protocol parser.
* Very simple open graph parser that parses open graph headers out of a given bit of php.
*
* Example:
*
* <code>
* $content = file_get_contents("https://www.youtube.com/watch?v=EIGGsZZWzZA");
*
* print_r(\ogp\Parser::parse($content));
* </code>
*
* @author Marcus Povey <marcus@marcus-povey.co.uk>
* @licence GPL2
*/

namespace ogp {

use DOMDocument;

class Parser {

/**
* Parse content into an array.
* @param $content html The HTML
* @return array
*/
public static function parse($content) {

$doc = new \DOMDocument();

// Fudge to handle a situation when an encoding isn't present
if (strpos($content, 'xml encoding=')===false)
$content = '<?xml encoding="utf-8" ?>' . $content;

@$doc->loadHTML($content);

$interested_in = ['og', 'fb', 'twitter']; // Open graph namespaces we're interested in (open graph + extensions)

$ogp = [];

// Open graph
$metas = $doc->getElementsByTagName('meta');
if (!empty($metas)) {
for ($n = 0; $n < $metas->length; $n++) {

$meta = $metas->item($n);

foreach (array('name', 'property') as $name) {
$meta_bits = explode(':', $meta->getAttribute($name));
if (in_array($meta_bits[0], $interested_in)) {

// If we're adding to an existing element, convert it to an array
if (isset($ogp[$meta->getAttribute($name)]) && (!is_array($ogp[$meta->getAttribute($name)])))
$ogp[$meta_bits[0]][$meta->getAttribute($name)] = array($ogp[$meta->getAttribute($name)], $meta->getAttribute('content'));
else if (isset($ogp[$meta->getAttribute($name)]) && (is_array($ogp[$meta->getAttribute($name)])))
$ogp[$meta_bits[0]][$meta->getAttribute($name)][] = $meta->getAttribute('content');
else
$ogp[$meta_bits[0]][$meta->getAttribute($name)] = $meta->getAttribute('content');
}
}
}
}

// OEmbed
$metas = $doc->getElementsByTagName('link');
if (!empty($metas)) {
for ($n = 0; $n < $metas->length; $n++) {

$meta = $metas->item($n);

if (strtolower($meta->getAttribute('rel')) == 'alternate') {

if (in_array(strtolower($meta->getAttribute('type')), ['application/json+oembed'])) {
$ogp['oembed']['jsonp'][] = $meta->getAttribute('href');
}
if (in_array(strtolower($meta->getAttribute('type')), ['text/json+oembed'])) {
$ogp['oembed']['json'][] = $meta->getAttribute('href');
}
if (in_array(strtolower($meta->getAttribute('type')), ['text/xml+oembed'])) {
$ogp['oembed']['xml'][] = $meta->getAttribute('href');
}
}
}
}

// Basics
foreach (['title'] as $basic) {
if (preg_match("#<$basic>(.*?)</$basic>#siu", $content, $matches))
$ogp[$basic] = trim($matches[1], " \n");
}
$metas = $doc->getElementsByTagName('meta');
if (!empty($metas)) {
for ($n = 0; $n < $metas->length; $n++) {

$meta = $metas->item($n);

if (strtolower($meta->getAttribute('name')) == 'description') {
$ogp['description'] = $meta->getAttribute('content');
}
if (strtolower($meta->getAttribute('name')) == 'keywords') {
$ogp['keywords'] = $meta->getAttribute('content');
}
}
}

return $ogp;
}

}

}
63 changes: 63 additions & 0 deletions lib/se-func-shortcode.php
Expand Up @@ -92,10 +92,73 @@ function sentry_bubble_handler( $atts, $content = null ){
}
}

function sentry_link_handler ( $atts ){
extract( shortcode_atts( array(
'url' => '',
) ,$atts ) );

$url = strip_tags($url);

// TODO: 自サイトのURLならIDで取得

if ( !$url ) return;

$ogp = get_transient( 'outlink-'.$url );

if ( $ogp === false ){

require_once('ogp/Parser.php');
$content = file_get_contents($url);
$ogp = \ogp\Parser::parse($content);

if ( !empty($ogp) ){
set_transient( 'outlink-'.$url, $ogp, DAY_IN_SECONDS );
}
}

$faviconUrl = "http://www.google.com/s2/favicons?domain=". $url;
$parse_url = parse_url( $url );

if ( empty($ogp) ){
return "<div class='outlink is-simple'><a href='" . $url . "' target='_blank' />" . $url . "</a></div>";
} else {
$outHTML ="";

$outHTML = '<div class="outlink is-blogcard">';
$outHTML .= '<a href="' . $ogp['og']['og:url'] . '" rel="bookmark" title="' . $ogp['og']['og:title'] . '">';
$outHTML .= '<div class="blogcard-content">';
$outHTML .= '<figure class="thumbnail">';
if ( $ogp['og']['og:image'] !== "" ){
$outHTML .= '<img src="' . $ogp['og']['og:image'] . '"/>';
}else{
$outHTML .= '<img src="' . get_default_image_uri(300). '" alt="no image" title="no image" width="300" height="300" />';
}
$outHTML .= '</figure>';
$outHTML .= '<div class="blogcard-body">';
$outHTML .= '<div class="blogcard-title">' . $ogp['og']['og:title'] . '</div>';
$outHTML .= '<div class="blogcard-description">' . mb_strimwidth( $ogp['og']['og:description'], 0, 160) . '...</div>';
$outHTML .= '</div>'; // .blogcard-body
$outHTML .= '</div>'; // .blogcard-content
$outHTML .= '<div class="blogcard-sitename">';
$outHTML .= '<img src="'. $faviconUrl .'" />';
if ( $ogp['og']['og:site_name'] ){
$outHTML .= $ogp['og']['og:site_name'];
} else {
$parse_url['scheme'] . '://' . $parse_url['host'];
}
$outHTML .= '</div>'; // .blogcard-sitename
$outHTML .= '</a>';
$outHTML .= '</div>';

return $outHTML;
}
}

add_shortcode( 'marker', 'marker_handler' );
add_shortcode( 'font', 'font_handler' );
add_shortcode( 'post', 'post_handler' );
add_shortcode( 'notification', 'notification_handler');
add_shortcode( 'ad', 'sentry_ad_handler' );
add_shortcode( 'bubble', 'sentry_bubble_handler');
add_shortcode( 'link', 'sentry_link_handler');
?>

0 comments on commit a8c44d9

Please sign in to comment.