<?php if ( !defined( 'ABSPATH' ) ) exit;//PHPの宣言と直接アクセスの拒否
/* セクションID haumenu_define_section_id_02_media() */
/* タブのスラグ haumenu_define_tab_slug_02_media() */
/* $option_name 設定名 haumenu_define_option_name_02_media() */
/* get_option GetOP02_media() */

/***** 親設定の有効／無効に合わせて子設定の表示方法を変更する *****/
/* 関数名・設定名は画面ごとに変えること（関数重複を防ぐ） */
/* add_action('admin_notices', 'current_pagehook');を追加してページ名を取得し、$page_fooksuffixの値を変える */
/* 連動マップ（ID => セレクター）の項目を編集する */

function haup_child_setting_view_control_02_media( $hook_suffix ) {
	//ページフックにより出力を判断
	$page_fooksuffix = 'ha-utilitiy_page_haumenu_page_media';

	if ( $page_fooksuffix !== $hook_suffix ) {
		return;
	}

	//メインスクリプトのハンドル
	$handle = 'haup-child-setting-control-js';

	// メインスクリプトが登録されていなければ終了(スクリプトの読み込みはmain-menu.phpで記述)
	if ( ! wp_script_is( $handle, 'registered' ) ) {
		return;
	}

	//設定名
	$set_name = haumenu_define_option_name_02_media();

	// 連動マップ（ID => セレクター）
	// 1つのIDに対して複数のCSSクラスを指定する（1つなら1行）
	$dependency_map = array(
		//$set_name . 'enable-set-revisions' => array(
			//'.set-revisions-num-wrapper', // リビジョン数
			//'.set-revisions-posttype-wrapper', // 指定する投稿タイプ
		//),

	);

	$inner_js = "";
	foreach ( $dependency_map as $id => $selectors ) {
		foreach ( (array) $selectors as $selector ) {
			// translators: %s stands for the CSS Class & ID.
			$inner_js .= sprintf(
				"haup_apply_dependency('%s', '%s');\n",
				esc_js( $id ),
				esc_js( $selector )
			);
		}
	}

	if ( $inner_js ) {
		$js_code = "if (window.haup_apply_dependency) {\n" . $inner_js . "}";
		// 第3引数を 'after' にすることで、本体JSの直後に出力
		wp_add_inline_script( $handle, $js_code, 'after' );
	}
}
add_action( 'admin_enqueue_scripts', 'haup_child_setting_view_control_02_media', 20 );


/*** 「-scaled」ファイルが生成されないようにする ***/
/* 項目の追加とデータの更新 */
function haumenu_init_field_disable_media_scaled(){
//項目の追加
	add_settings_field(
		'disable-media-scaled', // 設定名
		__('Do not generate "-scaled" files','hima-art-utility'), // 設定タイトル
		'haumenu_disable_media_scaled', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_02_media(), //表示させるタブのスラグ
		haumenu_define_section_id_02_media(),// $section 表示させるセクションのID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_disable_media_scaled' );

/* 入力フォーム */
function haumenu_disable_media_scaled() {
	$set_name = haumenu_define_option_name_02_media(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'disable-media-scaled'; //個別配列名

	//テーブルに値がなかった場合の措置（PHP8.x以降への対応）
	if(isset($options[$option_name])){
		$checked = $options[$option_name];
	}else{
		$checked = '';
	}
?>
<label>
	<input type="checkbox" id="<?php echo esc_html($set_name . $option_name ); ?>" name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="wppd-ui-toggle" <?php checked( $checked , 1 ); ?> value="1">
	<p><?php esc_html_e('Do not generate the "-scaled" file that is automatically generated when uploading images with large vertical and horizontal sizes, and maintain the image quality','hima-art-utility'); ?></p>
</label>
<p class="haup-admin-bk"><?php esc_html_e('Please note that by enabling this function, you may need to take into consideration the EXIF ​​information of the output image.','hima-art-utility'); ?></p>

<?php
}

/*** EXIF情報を削除する ***/
/* 項目の追加とデータの更新 */
function haumenu_init_field_delete_exif_while_upload(){
//項目の追加
	add_settings_field(
		'delete-exif-while-upload', // 設定名
		__('Delete EXIF Data While Upload','hima-art-utility'), // 設定タイトル
		'haumenu_delete_exif_while_upload', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_02_media(), //表示させるタブのスラグ
		haumenu_define_section_id_02_media(),// $section 表示させるセクションのID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_delete_exif_while_upload' );

/* 入力フォーム */
function haumenu_delete_exif_while_upload() {
	$set_name = haumenu_define_option_name_02_media(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'delete-exif-while-upload'; //個別配列名

	//テーブルに値がなかった場合の措置（PHP8.x以降への対応）
	if(isset($options[$option_name])){
		$checked = $options[$option_name];
	}else{
		$checked = '';
	}
?>
<label>
	<input type="checkbox" id="<?php echo esc_html($set_name . $option_name ); ?>" name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="wppd-ui-toggle" <?php checked( $checked , 1 ); ?> value="1">
	<p><?php esc_html_e('Removes original image EXIF information when uploading','hima-art-utility'); ?></p>
</label>
<details class="accordion"  style="margin: 5px 0px 0px 0px;overflow: hidden;">
<summary class="accordion__title" style="cursor: pointer;"><?php esc_html_e('View detailed description','hima-art-utility'); ?></summary>
<div class="accordion__content">
<h3><?php esc_html_e('Data deleted by this function','hima-art-utility'); ?></h3>
<p><?php esc_html_e('The EXIF information that is deleted varies depending on the image processing software that is enabled in your environment.','hima-art-utility'); ?></p>
<ol>
	<li><?php esc_html_e('GD: Completely discards all EXIF information','hima-art-utility'); ?></li>
	<li><?php esc_html_e('ImageMagick: Delete the following data items:','hima-art-utility'); ?></li>
	<ol>
		<li><?php esc_html_e('GPS: All About Location','hima-art-utility'); ?></li>
		<li><?php esc_html_e('SerialNumber: Camera serial number','hima-art-utility'); ?></li>
		<li><?php esc_html_e('Artist: Creator','hima-art-utility'); ?></li>
		<li><?php esc_html_e('Copyright: Copyright information','hima-art-utility'); ?></li>
		<li><?php esc_html_e('UserComment:User comment','hima-art-utility'); ?></li>
		<li><?php esc_html_e('ImageDescription: Image title and description','hima-art-utility'); ?></li>
	</ol>
</ol>
<h3><?php esc_html_e('Precautions when using this function','hima-art-utility'); ?></h3>
<h4><?php esc_html_e('Once deleted, it cannot be undone','hima-art-utility'); ?></h4>
	<p><?php esc_html_e('EXIF information deleted using this function cannot be restored. If you want to keep the original images, please make a backup.','hima-art-utility'); ?></p>
	<p><?php esc_html_e('Unless you specifically want to display shooting data (such as exposure information) on photo sharing sites or portfolio sites, we recommend deleting EXIF information when using original images in the following ways:','hima-art-utility'); ?></p>
	<ol>
		<li><?php esc_html_e('When the original, unprocessed files are directly called using themes or plugin functions','hima-art-utility'); ?></li>
		<li><?php esc_html_e('When using the "Lightbox" function of the image block','hima-art-utility'); ?></li>
		<li><?php esc_html_e('When the image block link destination is set to "Media File"','hima-art-utility'); ?></li>
		<li><?php esc_html_e('When "Full size" is specified for the image block size','hima-art-utility'); ?></li>
	</ol>
</div>
</details>
<?php
}

/*** アップロード時にファイル名をEXIFの撮影日時に変更 ***/
/* 項目の追加とデータの更新 */
function haumenu_init_field_rename_file_by_exif_on_upload(){
//項目の追加
	add_settings_field(
		'rename-file-by-exif-on-upload', // 設定名
		__('Convert file name to date and time','hima-art-utility'), // 設定タイトル
		'haumenu_rename_file_by_exif_on_upload', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_02_media(), //表示させるタブのスラグ
		haumenu_define_section_id_02_media(),// $section 表示させるセクションのID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_rename_file_by_exif_on_upload' );

/* 入力フォーム */
function haumenu_rename_file_by_exif_on_upload() {
	$set_name = haumenu_define_option_name_02_media(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'rename-file-by-exif-on-upload'; //個別配列名

	//テーブルに値がなかった場合の措置（PHP8.x以降への対応）
	if(isset($options[$option_name])){
		$checked = $options[$option_name];
	}else{
		$checked = '';
	}
?>
<label>
	<input type="checkbox" id="<?php echo esc_html($set_name . $option_name ); ?>" name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="wppd-ui-toggle" <?php checked( $checked , 1 ); ?> value="1">
	<p>
		<span><?php esc_html_e('Upload the image file and change the file name to date and time format.','hima-art-utility'); ?></span>
		<br>
		<span><?php esc_html_e('By converting to date and time, it will be easier to understand the file name when you download the image saved on the media later.','hima-art-utility'); ?></span>
	</p>
</label>
<p class="haup-admin-bk">
	<span><?php esc_html_e('For images with the same date and time, a sequential number such as "_1" or "_2" will be added to the end.','hima-art-utility'); ?></span>
	<br>
	<span><?php esc_html_e('If the image does not have date and time information, no processing will be done and the original file name will be used.','hima-art-utility'); ?></span>
	<br>
	<span class="haup-admin-font-red"><?php esc_html_e('*Please note that this function only automatically converts media whose MIME type is determined to be image when uploaded, and does not work for media that is recognized as other types.','hima-art-utility'); ?></span>
</p>
<?php
}