<?php if ( !defined( 'ABSPATH' ) ) exit;//PHPの宣言と直接アクセスの拒否

/***** メディアにEXIF情報を表示 *****/
/* ユーザー定義 メディア一覧にカラム追加 */
function haup_exif_media_columns( $posts_columns ) {
	$posts_columns['haup_ph_exif_date'] = __( 'Shooting Date', 'hima-art-utility' );
	$posts_columns['haup_ph_used_camera'] = __( 'Camera', 'hima-art-utility' );
	$posts_columns['haup_ph_iso'] = __( 'ISO', 'hima-art-utility' );

	return $posts_columns;
}
add_filter( 'manage_media_columns', 'haup_exif_media_columns' );

/* ユーザー定義 サイズを表示 */
function haup_exif_media_custom_column( $column_name, $post_id ) {

	$meta = wp_get_attachment_metadata($post_id);

//カメラ
	if ( $column_name == 'haup_ph_used_camera') {
		if ( isset($meta['image_meta']['camera']) ) {
			$used_camera = $meta['image_meta']['camera'];

			if(!empty($used_camera)){
				$data = $used_camera;
			} else {
				$data = __( 'No Data', 'hima-art-utility' );
			}
		} else {
			$data = __( 'No Data', 'hima-art-utility' );
		}
	}

	//撮影日
	if ( $column_name == 'haup_ph_exif_date') {
		if ( isset($meta['image_meta']['camera']) ) {
			$unix_time = $meta['image_meta']['created_timestamp'];

			if(!empty($unix_time)){
				$data = date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $unix_time );
			}else{
				$data = __( 'No Data', 'hima-art-utility' );
			}
		} else {
			$data = __( 'No Data', 'hima-art-utility' );
		}
	}

	//iso
	if ( $column_name == 'haup_ph_iso') {
		if ( isset($meta['image_meta']['camera']) ) {
			$iso = $meta['image_meta']['iso'];

			if(!empty($iso)){
					$data = $iso;
			}else{
				$data = __( 'No Data', 'hima-art-utility' );
			}
		} else {
			$data = __( 'No Data', 'hima-art-utility' );
		}
	}


if(!empty($data)){
	echo esc_html($data);
}else{
	echo '';
}
}

add_action( 'manage_media_custom_column', 'haup_exif_media_custom_column', 10, 2 );

/* 管理画面上のスタイル */
function haup_exif_admin_media_column_style_init(){
$custom_css_pre = "
/* HAUP Media Exif Column Width */
.column-haup_ph_exif_date,
.column-haup_ph_used_camera,
.column-haup_ph_iso
{width:10%;}
";

	//スタイルコードを改行なしで整形する
	$custom_css = str_replace(PHP_EOL, '', $custom_css_pre);

	wp_add_inline_style('haup-only-admin-style', $custom_css );

}
add_action('admin_enqueue_scripts','haup_exif_admin_media_column_style_init');
