<?php if ( !defined( 'ABSPATH' ) ) exit;//PHPの宣言と直接アクセスの拒否
/***** カスタムフィールドの登録 *****/

/* コンテンツに広告があるメッセージを非表示にする */
function haup_register_post_meta_hide_ad_info(){
	//投稿タイプを「投稿」に指定し、haup_is_single_filterで追加できるようにする
	$posttypes = 'post,page';
	$posttypes = apply_filters('haup_is_single_filter',$posttypes);
	$posttypes = apply_filters('haup_is_single_ad_info_filter',$posttypes);
	$post_types_array = array_map('trim', explode(',', $posttypes));
	$post_types_array = array_filter($post_types_array);

	//メタキーを指定
	$meta_key = 'hide_ad_info';

	foreach ( $post_types_array as $post_type ) {
		register_post_meta( 
			$post_type, 
			$meta_key, 
			[
				'show_in_rest' => true,
				'single' => true,
				'type' => 'string', //JS側でtrue/falseの場合はboolean、1/空白の場合はstring
				'auth_callback' => function() {
					return current_user_can( 'edit_posts' );
				},
			]
		);
	}
}
add_action('init','haup_register_post_meta_hide_ad_info');

/* アイキャッチ画像を非表示にする */
function haup_register_post_meta_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);
	$post_types_array = array_map('trim', explode(',', $posttypes));
	$post_types_array = array_filter($post_types_array);

	//メタキーを指定
	$meta_key = 'hide_fimage';

	foreach ( $post_types_array as $post_type ) {
		register_post_meta( 
			$post_type, 
			$meta_key, 
			[
				'show_in_rest' => true,
				'single' => true,
				'type' => 'string', //JS側でtrue/falseの場合はboolean、1/空白の場合はstring
				'auth_callback' => function() {
					return current_user_can( 'edit_posts' );
				},
			]
		);
	}
}
add_action('init','haup_register_post_meta_hide_featured_image');

/*** 更新日のコントロール ***/
/* 更新の判断 */
function haup_register_post_meta_post_moddate_control(){
	//投稿タイプを「投稿」に指定し、haup_is_single_filterで追加できるようにする
	$posttypes = 'post,page';
	$posttypes = apply_filters('haup_is_single_filter',$posttypes);
	$posttypes = apply_filters('haup_is_single_update_moddate_filter',$posttypes);
	$post_types_array = array_map('trim', explode(',', $posttypes));
	$post_types_array = array_filter($post_types_array);

	//メタキーを指定
	$meta_key = 'update_mode';

	foreach ( $post_types_array as $post_type ) {
		register_post_meta( 
			$post_type, 
			$meta_key, 
			[
				'show_in_rest' => true,
				'single' => true,
				'type' => 'string',
				'auth_callback' => function() {
					return current_user_can( 'edit_posts' );
				},
			]
		);
	}
}
add_action('init','haup_register_post_meta_post_moddate_control');

/* 更新日の一時保存 */
function haup_register_post_meta_post_moddate_control_lm(){
	//投稿タイプを「投稿」に指定し、haup_is_single_filterで追加できるようにする
	$posttypes = 'post,page';
	$posttypes = apply_filters('haup_is_single_filter',$posttypes);
	$posttypes = apply_filters('haup_is_single_update_moddate_filter',$posttypes);
	$post_types_array = array_map('trim', explode(',', $posttypes));
	$post_types_array = array_filter($post_types_array);

	//メタキーを指定
	$meta_key = 'last_modified';

	foreach ( $post_types_array as $post_type ) {
		register_post_meta( 
			$post_type, 
			$meta_key, 
			[
				'show_in_rest' => true,
				'single' => true,
				'type' => 'string',
				'auth_callback' => function() {
					return current_user_can( 'edit_posts' );
				},
			]
		);
	}
}
add_action('init','haup_register_post_meta_post_moddate_control_lm');

/*** SEO ***/
function haup_register_post_meta_seo_fields(){
		//投稿タイプを「投稿」に指定し、haup_is_single_filterで追加できるようにする
	$posttypes = 'post,page';
	$posttypes = apply_filters('haup_is_single_filter',$posttypes);
	$posttypes = apply_filters('haup_is_single_seo_fields_filter',$posttypes);
	$post_types_array = array_map('trim', explode(',', $posttypes));
	$post_types_array = array_filter($post_types_array);

	//メタキーを指定
	$noindex_key = 'is_noindex'; //noindex
	$nofollow_key = 'is_nofollow'; //nofollow
	$title_key = 'seo_title'; //title
	$keyword_key = 'meta_keywords'; //meta_keywords
	$description_key = 'meta_description'; //meta_description

	//Noindexの登録
	foreach ( $post_types_array as $post_type ) {
		register_post_meta( 
			$post_type, 
			$noindex_key, 
			[
				'show_in_rest' => true,
				'single' => true,
				'type' => 'string', //JS側でtrue/falseの場合はboolean、1/空白の場合はstring
				'auth_callback' => function() {
					return current_user_can( 'edit_posts' );
				},
			]
		);
	}

	//Nofollowの登録
	foreach ( $post_types_array as $post_type ) {
		register_post_meta( 
			$post_type, 
			$nofollow_key, 
			[
				'show_in_rest' => true,
				'single' => true,
				'type' => 'string', //JS側でtrue/falseの場合はboolean、1/空白の場合はstring
				'auth_callback' => function() {
					return current_user_can( 'edit_posts' );
				},
			]
		);
	}

	//titleの登録
	foreach ( $post_types_array as $post_type ) {
		register_post_meta( 
			$post_type, 
			$title_key, 
			[
				'show_in_rest' => true,
				'single' => true,
				'type' => 'string', 
				'auth_callback' => function() {
					return current_user_can( 'edit_posts' );
				},
			]
		);
	}

	//descriptionの登録
	foreach ( $post_types_array as $post_type ) {
		register_post_meta( 
			$post_type, 
			$description_key, 
			[
				'show_in_rest' => true,
				'single' => true,
				'type' => 'string', 
				'auth_callback' => function() {
					return current_user_can( 'edit_posts' );
				},
			]
		);
	}

	//keywordsの登録
	foreach ( $post_types_array as $post_type ) {
		register_post_meta( 
			$post_type, 
			$keyword_key, 
			[
				'show_in_rest' => true,
				'single' => true,
				'type' => 'string', 
				'auth_callback' => function() {
					return current_user_can( 'edit_posts' );
				},
			]
		);
	}
}
add_action('init','haup_register_post_meta_seo_fields');

/*** カテゴリーの一択 ***/
function haup_register_post_meta_category_radio(){
	//投稿タイプを「投稿」に指定し、haup_is_single_filterで追加できるようにする
	$posttypes = 'post';
	$posttypes = apply_filters('haup_is_single_filter',$posttypes);
	$post_types_array = array_map('trim', explode(',', $posttypes));
	$post_types_array = array_filter($post_types_array);

	//メタキーを指定
	$meta_key = '_haup_temp_category_id';

	foreach ( $post_types_array as $post_type ) {
		register_post_meta( 
			$post_type, 
			$meta_key, 
			[
				'show_in_rest' => true,
				'single' => true,
				'type' => 'integer',
				'auth_callback' => function() {
					return current_user_can( 'edit_posts' );
				},
			]
		);
	}
}
add_action('init','haup_register_post_meta_category_radio');