Written by Anonymous
function cocoon_create_html_blog_card($arr) {
$url = $arr['url'] ?? '';
$target = $arr['target'] ?? '';
$rel = $arr['rel'] ?? '';
$title = $arr['title'] ?? '';
$thumbnail = $arr['thumbnail'] ?? '';
$snippet = $arr['snippet'] ?? '';
$site_logo_tag = $arr['site_logo_tag'] ?? '';
$date_tag = $arr['date_tag'] ?? '';
$cat = $arr['cat'] ?? '';
$class = $arr['class'] ?? get_additional_internal_blogcard_classes();
$type = $arr['type'] ?? 'internal';
$list = $type === 'internal' ?
'<ul class="article__data">
<li>' . $date_tag . '</li>
<li>' . $cat . '</li>
</ul>'
: '';
return
'<a href="' . esc_url($url) . '" title="' . esc_attr($title) . '" class="blog-card"' . $target . $rel . '>
<div class="article">
<div class="article__title">' . $title . '</div>
' . $list . '
<div class="article__eyecath-description-wrapper">
<div class="article__eye-catch">' . $thumbnail . '</div>
</div>
<div class="article__description">' . $snippet . '</div>
</div>
</a>';
}
// 内部ブログカード
function url_to_internal_blogcard_tag($url){
if ( !$url ) return;
$url = strip_tags($url);//URL
$id = url_to_postid( $url );//IDを取得(URLから投稿ID変換)
//内部ブログカード作成可能なURLかどうか
if ( !is_internal_blogcard_url($url) ) return;
//_v($url);
$no_image = get_no_image_160x90_url();
if (!$no_image) {
$no_image = get_site_screenshot_url($url);
}
$thumbnail = null;
$date_tag = null;
//投稿・固定ページの場合
if ($id) {
//global $post;
$post_data = get_post($id);
setup_postdata($post_data);
wp_reset_query();
$title = $post_data->post_title;//タイトルの取得
//メタディスクリプションの取得
$snippet = get_the_page_meta_description($id);
//投稿管理画面の抜粋を取得
if (!$snippet) {
$snippet = $post_data->post_excerpt;
}
//All in One SEO Packのメタディスクリプションを取得
if (!$snippet) {
$snippet = get_the_all_in_one_seo_pack_meta_description($id);
}
//記事本文の抜粋文を取得
if (!$snippet) {
$snippet = get_content_excerpt($post_data->post_content, get_entry_card_excerpt_max_length());
}
$snippet = preg_replace('/\n/', '', $snippet);
//日付表示
$date = null;
$post_date = mysql2date(get_site_date_format(), $post_data->post_date);
switch (get_internal_blogcard_date_type()) {
case 'post_date':
$date = $post_date;
break;
case 'up_date':
$date = mysql2date(get_site_date_format(), $post_data->post_modified);
if (!$date) {
$date = $post_date;
}
break;
}
if (is_internal_blogcard_date_visible()) {
//$date = '<div class="blogcard-post-date internal-blogcard-post-date">'.$date.'</div>';//日付の取得
//$date_tag = '<div class="blogcard-date internal-blogcard-date">'.$date.'</div>';
$date_tag = $date;
}
//サムネイルの取得(要160×90のサムネイル設定)
$thumbnail = get_the_post_thumbnail($id, get_internal_blogcard_thumbnail_size(), array('class' => 'blogcard-thumb-image internal-blogcard-thumb-image', 'alt' => ''));
} elseif (is_home_url($url)){
//トップページの場合
$title = get_front_page_title_caption();
$snippet = get_front_page_meta_description();
$image = get_ogp_home_image_url();
if (!empty($image)) {
$thumbnail = get_blogcard_thumbnail_image_tag($image);
}
} elseif ($cat = get_category_by_path($url, false)){
//カテゴリページの場合
$cat_id = $cat->cat_ID;
$title = get_the_category_title($cat_id);
$snippet = get_the_category_snippet($cat_id);
$image = get_the_category_eye_catch_url($cat_id);
if ($image) {
$thumbnail = get_blogcard_thumbnail_image_tag($image);
}
} elseif ($tag = url_to_tag_object($url)) {
$tag_id = $tag->term_id;
$title = get_the_tag_title($tag_id);
$snippet = get_the_tag_snippet($tag_id);
$image = get_the_tag_eye_catch_url($tag_id);
if ($image) {
$thumbnail = get_blogcard_thumbnail_image_tag($image);
}
}
//タイトルのフック
$title = apply_filters('cocoon_blogcard_title',$title);
$title = apply_filters('cocoon_internal_blogcard_title',$title);
//スニペットのフック
$snippet = apply_filters( 'cocoon_blogcard_snippet', $snippet );
$snippet = apply_filters( 'cocoon_internal_blogcard_snippet', $snippet );
//サムネイルが存在しない場合
if ( !$thumbnail ) {
$thumbnail = get_blogcard_thumbnail_image_tag($no_image);
}
//ブログカードのサムネイルを右側に
$additional_class = get_additional_internal_blogcard_classes();
//新しいタブで開く場合
$target = is_internal_blogcard_target_blank() ? ' target="_blank" rel="noopener"' : '';
//ファビコン
$favicon_tag =
'<div class="blogcard-favicon internal-blogcard-favicon">'.
get_original_image_tag('https://www.google.com/s2/favicons?domain='.get_the_site_domain(), 16, 16, 'blogcard-favicon-image internal-blogcard-favicon-image').
'</div>';
//サイトロゴ
$domain = get_domain_name(punycode_decode($url));
$site_logo_tag = '<div class="blogcard-domain internal-blogcard-domain">'.$domain.'</div>';
$site_logo_tag = '<div class="blogcard-site internal-blogcard-site">'.$favicon_tag.$site_logo_tag.'</div>';
// カテゴリー
$cat = '';
$categories = get_the_category();
if ($categories && is_single()) {
foreach($categories as $category){
$cat = $category->cat_name;
}
}
$args = [
'url' => $url,
'target' => $target,
'title' => $title,
'thumbnail' => $thumbnail,
'snippet' => $snippet,
'site_logo_tag' => $site_logo_tag,
'date_tag' => $date_tag,
'cat' => $cat,
'class' => $additional_class,
'type' => 'internal'
];
//取得した情報からブログカードのHTMLタグを作成
$tag = cocoon_create_html_blog_card($args);
return $tag;
}
function url_to_external_ogp_blogcard_tag($url){
if ( !$url ) return;
$url = strip_tags($url);//URL
if (preg_match('/.+(\.mp3|\.midi|\.mp4|\.mpeg|\.mpg|\.jpg|\.jpeg|\.png|\.gif|\.svg|\.pdf)$/i', $url, $m)) {
return;
}
$url = ampersand_urldecode($url);
$params = get_url_params($url);
$user_title = !empty($params['title']) ? $params['title'] : null;
$user_snippet = !empty($params['snippet']) ? $params['snippet'] : null;
//$url = add_query_arg(array('title' => null, 'snippet' => null), $url);
//_v($url);
$url_hash = TRANSIENT_BLOGCARD_PREFIX.md5( $url );
$error_title = $url; //エラーの場合はURLを表示
$title = $error_title;
$error_image = get_site_screenshot_url($url);
$image = $error_image;
$snippet = '';
$error_rel_nofollow = ' rel="nofollow"';
require_once get_template_directory().'/lib/open-graph.php';
//ブログカードキャッシュ更新モード、もしくはログインユーザー以外のときはキャッシュの取得
if ( !(is_external_blogcard_refresh_mode() && is_user_administrator()) ) {
//保存したキャッシュを取得
$ogp = get_transient( $url_hash );
}
if ( empty($ogp) ) {
$ogp = OpenGraphGetter::fetch( $url );
//_v($ogp);
if ( $ogp == false ) {
$ogp = 'error';
} else {
//キャッシュ画像の取得
$res = fetch_card_image($ogp->image, $url);
if ( $res ) {
$ogp->image = $res;
}
if ( isset( $ogp->title ) && $ogp->title )
$title = $ogp->title;//タイトルの取得
if ( isset( $ogp->description ) && $ogp->description )
$snippet = $ogp->description;//ディスクリプションの取得
if ( isset( $ogp->image ) && $ogp->image )
$image = $ogp->image;////画像URLの取得
$error_rel_nofollow = null;
}
set_transient( $url_hash, $ogp,
DAY_IN_SECONDS * intval(get_external_blogcard_cache_retention_period()) );
} elseif ( $ogp == 'error' ) {
//前回取得したとき404ページだったら何も出力しない
} else {
if ( isset( $ogp->title ) && $ogp->title )
$title = $ogp->title;//タイトルの取得
if ( isset( $ogp->description ) && $ogp->description )
$snippet = $ogp->description;//ディスクリプションの取得
if ( isset( $ogp->image ) && $ogp->image )
$image = $ogp->image;//画像URLの取得
$error_rel_nofollow = null;
}
//var_dump($image);
//ドメイン名を取得
$domain = get_domain_name(isset($ogp->url) ? punycode_decode($ogp->url) : punycode_decode($url));
//og:imageが相対パスのとき
if(!$image || (strpos($image, '//') === false) || (is_ssl() && (strpos($image, 'https:') === false))){ // //OGPのURL情報があるか
//相対パスの時はエラー用の画像を表示
$image = $error_image;
}
$title = strip_tags($title);
if ($user_title) {
$title = $user_title;
}
//タイトルのフック
$title = apply_filters('cocoon_blogcard_title',$title);
$title = apply_filters('cocoon_external_blogcard_title',$title);
$image = strip_tags($image);
$snippet = get_content_excerpt( $snippet, 160 );
$snippet = strip_tags($snippet);
if ($user_snippet) {
$snippet = $user_snippet;
}
$snippet = apply_filters( 'cocoon_blogcard_snippet', $snippet );
$snippet = apply_filters( 'cocoon_external_blogcard_snippet', $snippet );
//新しいタブで開く場合
$target = is_external_blogcard_target_blank() ? ' target="_blank"' : '';
$rel = '';
if (is_external_blogcard_target_blank()) {
$rel = ' rel="noopener"';
}
//コメント内でブログカード呼び出しが行われた際はnofollowをつける
global $comment; //コメント内以外で$commentを呼び出すとnullになる
if (is_external_blogcard_target_blank() && $comment) {
$rel = ' rel="nofollow noopener"';
}
//GoogleファビコンAPIを利用する
////www.google.com/s2/favicons?domain=nelog.jp
$favicon_tag = '<div class="blogcard-favicon external-blogcard-favicon">'.
get_original_image_tag('https://www.google.com/s2/favicons?domain='.$domain, 16, 16, 'blogcard-favicon-image external-blogcard-favicon-image').
'</div>';
//サイトロゴ
$site_logo_tag = '<div class="blogcard-domain external-blogcard-domain">'.$domain.'</div>';
$site_logo_tag = '<div class="blogcard-site external-blogcard-site">'.$favicon_tag.$site_logo_tag.'</div>';
//サムネイルを取得できた場合
$image = apply_filters('get_external_blogcard_thumbnail_url', $image);
if ( $image ) {
$thumbnail = get_original_image_tag($image, THUMB160WIDTH, THUMB160HEIGHT, 'blogcard-thumb-image external-blogcard-thumb-image');
}
$args = [
'url' => $url,
'target' => $target,
'rel' => $rel,
'title' => $title,
'thumbnail' => $thumbnail,
'snippet' => $snippet,
'site_logo_tag' => $site_logo_tag,
'class' => get_additional_external_blogcard_classes(),
'type' => 'external'
];
//取得した情報からブログカードのHTMLタグを作成
$tag = cocoon_create_html_blog_card($args);
return $tag;
}