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

/* https://increment-log.com/wordpress-user-profile-add-meta-field/ */

// プロフィールに構造化データ用のフィールドを追加する
function haup_add_profile_for_schema_fields( $user ) {
$user_work = get_the_author_meta( 'ha-user-work', $user->ID );
$x_id = get_the_author_meta( 'ha-user-x-url', $user->ID );
$facebook_id = get_the_author_meta( 'ha-user-facebook-url', $user->ID );
$insta_id = get_the_author_meta( 'ha-user-insta-url', $user->ID );

?>
<div class="ha-admin-profile-pannel-wrapper">
<h2><?php esc_html_e('Data for structured data output','hima-art-utility' ); ?></h2>
<table class="schema-userdata-table">
	<tr>
		<th style="text-align:left;">
			<label for="ha-user-work">
				<?php esc_html_e( 'Occupation or position','hima-art-utility' ); ?>
			</label>
		</th>
		<td>
			<input type="text" name="ha-user-work" id="ha-user-work" value="<?php echo esc_html($user_work); ?>" placeholder="<?php esc_html_e( 'Please enter your title or occupation','hima-art-utility' ); ?>" size="50" />
		</td>
	</tr>
	<tr>
		<th style="text-align:left;">
			<label for="ha-user-x-url">
				<?php esc_html_e( 'X ID','hima-art-utility' ); ?>
			</label>
		</th>
		<td>
		<input type="text" name="ha-user-x-url" id="ha-user-x-url" value="<?php echo esc_html( $x_id); ?>" placeholder="<?php esc_html_e( 'Please enter the Xs ID','hima-art-utility' ); ?>" size="100" />
		</td>
	</tr>
	<tr>
		<th style="text-align:left;">
			<label for="ha-user-facebook-url">
				<?php esc_html_e( 'Facebook ID','hima-art-utility' ); ?>
			</label>
		</th>
	<td>
		<input type="text" name="ha-user-facebook-url" id="ha-user-facebook-url" value="<?php echo esc_html( $facebook_id ); ?>" placeholder="<?php esc_html_e( 'Please enter your Facebook ID','hima-art-utility' ); ?>"  size="100" />
	</td>
	</tr>
	<tr>
		<th style="text-align:left;">
			<label for="ha-user-insta-url">
				<?php esc_html_e( 'Instagram ID','hima-art-utility' ); ?>
			</label>
		</th>
	<td>
		<input type="text" name="ha-user-insta-url" id="ha-user-insta-url" value="<?php echo esc_html( $insta_id ); ?>" placeholder="<?php esc_html_e( 'Please enter your Instagram ID','hima-art-utility' ); ?>"  size="100" />
	</td>
	</tr>
</table>
</div>
	<?php wp_nonce_field( 'haup_add_user_meta_nonce_action', '_haup_add_user_meta_nonce' ) ?>
<?php
}

add_action( 'show_user_profile', 'haup_add_profile_for_schema_fields' );
add_action( 'edit_user_profile', 'haup_add_profile_for_schema_fields' );

/***** 構造化データ用のフィールドを保存する *****/
function haup_update_profile_schema_fields( $user_id ) {
	// 現在のユーザーに[$user_id]を編集する権限があることを確認
	if ( ! current_user_can( 'edit_user', $user_id ) ) {
		return false;
	}

// nonceの検証
	if ( !isset( $_POST['_haup_add_user_meta_nonce'] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash($_POST['_haup_add_user_meta_nonce'])), 'haup_add_user_meta_nonce_action' ) ) {
		return $user_id; // nonceが検証に失敗した場合は何もしない
	}

	// 追加した任意の各フィールドを保存用に配列化（カスタムフィールドのキー名と値）
	if(isset( $_POST['ha-user-work'] )){
		$ha_user_work = sanitize_text_field(wp_unslash($_POST['ha-user-work']));
	}

	if(isset( $_POST['ha-user-x-url'] )){
		$ha_user_x_url = sanitize_text_field(wp_unslash($_POST['ha-user-x-url']));
	}

	if(isset( $_POST['ha-user-facebook-url'] )){
		$ha_user_fb_url = sanitize_text_field(wp_unslash($_POST['ha-user-facebook-url']));
	}

	if(isset( $_POST['ha-user-insta-url'] )){
		$ha_user_insta_url = sanitize_text_field(wp_unslash($_POST['ha-user-insta-url']));
	}

	$meta_keys = array(
		'ha-user-work' => sanitize_text_field(wp_unslash( $ha_user_work )),
		'ha-user-x-url' => sanitize_text_field(wp_unslash( $ha_user_x_url )),
		'ha-user-facebook-url' => sanitize_text_field(wp_unslash( $ha_user_fb_url  )),
		'ha-user-insta-url' => sanitize_text_field(wp_unslash( $ha_user_insta_url ))
	);

	// [$user_id]のユーザーメタを作成または更新
	foreach( $meta_keys as $key => $value ) {
		update_user_meta( $user_id, $key, $value );
	}

	// 1つの場合は以下の1行で完了
		# update_user_meta( $user_id, 'text-sample', $_POST['text-sample'] );

	return true;
}

add_action( 'personal_options_update', 'haup_update_profile_schema_fields' );
add_action( 'edit_user_profile_update', 'haup_update_profile_schema_fields' );