Written by Anonymous
if ( !function_exists( 'generate_author_box_tag' ) ):
function generate_author_box_tag($id = null, $label = null, $is_image_circle = 0){
// --- 1. データ取得・ロジック部分 ---
$user_id = get_the_author_meta( 'ID' );
if (!$user_id || (!is_singular() && !is_author($user_id))) {
$user_id = get_sns_default_follow_user();
}
if ($id && get_userdata( $id )) {
$user_id = $id;
}
$description = '';
$name = '';
if ($user_id) {
// プロフィール文の取得と加工
$description = get_the_author_description_text($user_id);
if (empty($description)) {
$description = get_the_author_meta('description', $user_id);
}
$description = wpautop($description);
$description = do_shortcode($description);
$description = apply_filters( 'the_author_box_description', $description, $user_id );
// 名前の取得と加工
$author_display_name = strip_tags(get_the_author_display_name($user_id));
if (!is_buddypress_page()) {
$profile_page_url = get_the_author_profile_page_url($user_id);
if ($profile_page_url) {
$name = '<a href="'.esc_url($profile_page_url).'">'.esc_html($author_display_name).'</a>';
} else {
$user = get_userdata( $user_id );
if ($user) {
$name = sprintf( '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
esc_url( get_author_posts_url( $user->ID, $user->user_nicename ) ),
esc_attr( sprintf( __( 'Posts by %s' ), $user->display_name ) ),
$user->display_name
);
} else {
$name = __( 'NO USER' );
}
}
} else {
$author_website_url = strip_tags(get_the_author_website_url($user_id));
$name = $author_display_name;
if ($author_website_url) {
$name = '<a href="'.esc_url($author_website_url).'" target="_blank" rel="nofollow noopener">'.esc_html($author_display_name).'</a>';
}
}
$name = apply_filters( 'the_author_box_name', $name, $user_id );
}
// テンプレートに渡す変数を配列にまとめる
$args = [
'user_id' => $user_id,
'label' => $label,
'is_image_circle' => $is_image_circle,
'avatar' => get_avatar( $user_id, 200 ),
'name' => $name,
'description' => $description,
'is_bp_exist' => is_buddypress_exist(),
'is_logged_in' => is_user_logged_in(),
];
get_template_part('tmp/author-box', null, $args);
}
endif;