Written by Anonymous
function get_navi_card_image_attributes($menu, $type = ET_DEFAULT){
$is_singular = false;
$url = $menu->url;
$object_id = $menu->object_id;
$object = $menu->object;
$is_large_image_use = is_widget_entry_card_large_image_use($type);
$thumb_size = $is_large_image_use ? THUMB320 : THUMB120;
//大きなサムネイル画像を使用する場合
$image_attributes = array();
$post_types = get_custum_post_types();
//投稿ページ・固定ページ・カスタム投稿ページ
if ($object == 'post' || $object == 'page' || in_array($object, $post_types)) {
$thumbnail_id = get_post_thumbnail_id($object_id); $image_attributes = wp_get_attachment_image_src($thumbnail_id, $thumb_size);
$is_singular = true;
}
// タクソノミー(カテゴリー、タグ、カスタムタクソノミー)
elseif (taxonomy_exists($object)) {
$image_url = get_term_meta($object_id, 'the_category_eye_catch_url', true);
if (!$image_url) {
$image_url = get_term_meta($object_id, 'the_tag_eye_catch_url', true);
}
$image_attributes = get_navi_card_image_url_attributes($image_url, $type);
}
// カスタムリンク
elseif ($object == 'custom') {
// URLから投稿・固定ページ・カスタム投稿ページ
$object_id = url_to_postid($url);
if ($object_id > 0) {
$thumbnail_id = get_post_thumbnail_id($object_id);
$image_attributes = wp_get_attachment_image_src($thumbnail_id, $thumb_size);
$is_singular = true;
}
// URLからタクソノミー
if (!$image_attributes) {
$tax_obj = url_to_taxonomy_object($url);
if ($tax_obj && isset($tax_obj->term_id)) {
$image_url = get_term_meta($tax_obj->term_id, 'the_category_eye_catch_url', true);
if (!$image_url) {
$image_url = get_term_meta($tax_obj->term_id, 'the_tag_eye_catch_url', true);
}
if ($image_url) {
$image_attributes = get_navi_card_image_url_attributes($image_url, $type);
}
}
}
}
//アイキャッチがない場合
if (!$image_attributes) {
$image_attributes = array();
if ($is_large_image_use) {
$image_attributes[0] = get_no_image_320x180_url($object_id, $is_singular);
$image_attributes[1] = THUMB320WIDTH_DEF;
$image_attributes[2] = THUMB320HEIGHT_DEF;
} else {
$image_attributes[0] = get_no_image_120x68_url($object_id, $is_singular);
$image_attributes[1] = THUMB120WIDTH_DEF;
$image_attributes[2] = THUMB120HEIGHT_DEF;
}
}
return $image_attributes;
}
// 内部URLからタクソノミーオブジェクトを取得
if ( !function_exists( 'url_to_taxonomy_object' ) ):
function url_to_taxonomy_object($url){
// 登録されているすべての公開タクソノミーを取得
$taxonomies = get_taxonomies(array('public' => true), 'objects');
foreach ($taxonomies as $tax_name => $tax_obj) {
// タクソノミーごとのベースURL(リライトスラッグ)を取得
$base = isset($tax_obj->rewrite['slug']) ? $tax_obj->rewrite['slug'] : $tax_name;
$quoted_base = preg_quote($base, '/');
// 正規表現でスラッグを抽出
$reg = '{/' . $quoted_base . '/([^/?]+)|[?&]' . $quoted_base . '=([^&]+)}';
if (preg_match($reg, $url, $m)) {
$slug = null;
if (!empty($m[1])) {
$slug = $m[1];
} elseif (!empty($m[2])) {
$slug = $m[2];
}
if ($slug) {
$term = get_term_by('slug', $slug, $tax_name);
if ($term && !is_wp_error($term)) {
return $term;
}
}
}
}
return null;
}
endif;