From a8c44d9f4f4bf9baad76764e8ee53e6881a82dba Mon Sep 17 00:00:00 2001 From: Masashi Ezaki Date: Wed, 29 Aug 2018 01:05:10 +0900 Subject: [PATCH] =?UTF-8?q?fix=20#8=20=E5=A4=96=E9=83=A8=E3=83=AA=E3=83=B3?= =?UTF-8?q?=E3=82=AF=E3=81=AE=E3=83=96=E3=83=AD=E3=82=B0=E3=82=AB=E3=83=BC?= =?UTF-8?q?=E3=83=89=E3=82=92=E4=BD=9C=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ブログカード作成のショートコードを追加。 SSL接続でエラーが出るサーバーあり。ogp画像は先方サーバ画像の読み込みなので、将来的には対応要? --- lib/ogp/Parser.php | 115 ++++++++++++++++++++++++++++++++++++++ lib/se-func-shortcode.php | 63 +++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100755 lib/ogp/Parser.php diff --git a/lib/ogp/Parser.php b/lib/ogp/Parser.php new file mode 100755 index 0000000..4ba760b --- /dev/null +++ b/lib/ogp/Parser.php @@ -0,0 +1,115 @@ + + * $content = file_get_contents("https://www.youtube.com/watch?v=EIGGsZZWzZA"); + * + * print_r(\ogp\Parser::parse($content)); + * + * + * @author Marcus Povey + * @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 = '' . $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>(.*?)#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; + } + + } + +} diff --git a/lib/se-func-shortcode.php b/lib/se-func-shortcode.php index 962e096..ccfa57d 100644 --- a/lib/se-func-shortcode.php +++ b/lib/se-func-shortcode.php @@ -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 ""; + } else { + $outHTML =""; + + $outHTML = ''; + + 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'); ?>