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


function haup_init_editor_panel_js_hide_featured_image(){
	$script_name = 'haup-panel-js-hide-featured-image';

	//投稿タイプを「投稿」に指定し、haup_is_single_filterで追加できるようにする
	$posttypes = 'post,page';
	$posttypes = apply_filters('haup_is_single_filter',$posttypes);
	$posttypes = apply_filters('haup_is_single_hide_fimage_filter',$posttypes);
	//20250902 explodeだけでは空文字などがあった場合に不具合が出る可能性があるので修正
	$post_types_array = array_map('trim', explode(',', $posttypes));
	$post_types_array = array_filter($post_types_array);

	wp_enqueue_script(
		$script_name,
		plugin_dir_url( __FILE__ ) .'editor-panel.js',
		[ 'wp-plugins', 'wp-edit-post', 'wp-element', 'wp-components', 'wp-data', 'wp-i18n' ],
		haup_plugin_version(),
		array( 
			'strategy'  => 'defer',
			'in_footer' => true,// Note: This is the default value.
		)
	);

	//スクリプトに渡すための文字列と翻訳可能文字列を紐づけて配列にする
	$localize_val = array(
		'title' => __('Featured Image visiblity','hima-art-utility'),
		'label' => __('Hide Featured Image','hima-art-utility'),
		'help' => __('If you check it, the featured image will not be displayed on individual pages.','hima-art-utility'),
		'post_types' => $post_types_array, //適用させる投稿タイプを配列で渡す
	);

	//wp_localize_scriptを使ってスクリプトへ配列を渡す
	wp_localize_script( $script_name , 'HaupLocalizeTextFimage', $localize_val);
}
add_action('enqueue_block_editor_assets','haup_init_editor_panel_js_hide_featured_image');

/********** アイキャッチ画像非表示の場合、スタイルをインラインで出力 **********/
function haup_hide_featured_image_style_css_init(){
if(isset( GetOP01_blocktheme()['fimage-hide-class'] )){
	$custom_class = GetOP01_blocktheme()['fimage-hide-class'];
}else{
	$custom_class = '';
}

if(empty( GetOP01_blocktheme()['fimage-hide-class'] )){
$custom_css_pre = hau_sanitize_css("
/* HAUP Hide Featured Image */
body.single .wp-block-post-featured-image img,
body.page .wp-block-post-featured-image img
{
	display:none;
}
");
}else{
$custom_css_pre = hau_sanitize_css("
/* HAUP Hide Featured Image */
.".$custom_class."
{
	display:none;
}
");
}

//タブを除去
$custom_css_deltab = preg_replace('/\t+/', '', $custom_css_pre);
//改行を除去
$custom_css = str_replace(PHP_EOL, '', $custom_css_deltab);

if(get_post_meta(get_the_ID(),'hide_fimage',true) == 1){
wp_add_inline_style( 'haup-front-and-blocks-style', $custom_css );//インラインでCSS出力
}
}
add_action('wp_enqueue_scripts','haup_hide_featured_image_style_css_init');