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

function haup_ordered_and_change_comment_field( $fields ) {
//設定の変数
	if(isset( GetOP04_blockoption()['text-input-bottom-move'] )){
		$under_text_input = GetOP04_blockoption()['text-input-bottom-move'];
	}else{
		$under_text_input = '';
	}

//各ラベルを置き換える-入力があれば書き換え
	//「名前」の代替文字
	if(isset( GetOP04_blockoption()['change-comment-label-name'] )){
		$label_author = GetOP04_blockoption()['change-comment-label-name'];
	}else{
		$label_author = '';
	}

	//「メール」の代替文字
	if(isset( GetOP04_blockoption()['change-comment-label-email'] )){
		$label_email = GetOP04_blockoption()['change-comment-label-email'];
	}else{
		$label_email = '';
	}

	//「サイト」の代替文字
	if(isset( GetOP04_blockoption()['change-comment-label-website'] )){
		$label_url = GetOP04_blockoption()['change-comment-label-website'];
	}else{
		$label_url = '';
	}

	//「次回のコメント．．．」の代替文字
	if(isset( GetOP04_blockoption()['change-comment-label-cookie'] )){
		$label_cookies = GetOP04_blockoption()['change-comment-label-cookie'];
	}else{
		$label_cookies = '';
	}

	//「コメント」の代替文字
	if(isset( GetOP04_blockoption()['change-comment-label-comment'] )){
		$label_comment = GetOP04_blockoption()['change-comment-label-comment'];
	}else{
		$label_comment = '';
	}

	//必須の「※」の代替文字
	if(isset( GetOP04_blockoption()['change-comment-label-req'] )){
		$req_mark = GetOP04_blockoption()['change-comment-label-req'];
	}else{
		$req_mark = '';
	}

//変数があるかないかで処理を分岐
	//「名前」
		if($label_author ==''){
			$author_text = esc_html(__( 'Name','hima-art-utility' ));
		}else{
			$author_text = esc_html($label_author);
		}

	//「メール」
		if($label_email ==''){
			$email_text = esc_html(__( 'Email','hima-art-utility' ));
		}else{
			$email_text = esc_html($label_email);
		}

	//「サイト」
		if($label_url ==''){
			$url_text = esc_html(__( 'Website','hima-art-utility' ));
		}else{
			$url_text = esc_html($label_url);
		}

	//「次回のコメントで使用するためブラウザーに自分の名前、メールアドレス、サイトを保存する。」
		if($label_cookies ==''){
			$cookies_text = esc_html(__( 'Save my name, email, and website in this browser for the next time I comment.','hima-art-utility' ));
		}else{
			$cookies_text = esc_html($label_cookies);
		}

	//「コメント」
		if($label_comment ==''){
			$comment_text = esc_html(__( 'Comment','hima-art-utility' ));
		}else{
			$comment_text = esc_html($label_comment);
		}

	//「※」
		if($req_mark ==''){
			$req_mark_text = esc_html(__('*','hima-art-utility'));
		}else{
			$req_mark_text = esc_html($req_mark);
		}

//ラベルを書き換えたHTML
//WordPress6.3時点のテンプレート
$temp_author = '<p class="comment-form-author"><label for="author">'.$author_text.'<span class="required">'.$req_mark_text.'</span></label> <input id="author" name="author" type="text" value="" size="30" maxlength="245" autocomplete="name" required /></p>';
$temp_email = '<p class="comment-form-email"><label for="email">'.$email_text.'<span class="required">'.$req_mark_text.'</span></label> <input id="email" name="email" type="email" value="" size="30" maxlength="100" aria-describedby="email-notes" autocomplete="email" required /></p>';
$temp_url = '<p class="comment-form-url"><label for="url">'.$url_text.'</label> <input id="url" name="url" type="url" value="" size="30" maxlength="200" autocomplete="url" /></p>';
$temp_cookies = '<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes" /> <label for="wp-comment-cookies-consent">'.$cookies_text.'</label></p>';
$temp_comment = '<p class="comment-form-comment"><label for="comment">'.$comment_text.'<span class="required">'.$req_mark_text.'</span></label> <textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required></textarea></p>';

//一旦すべてのフィールドを削除
	unset($fields['author']);
	unset($fields['email']);
	unset($fields['url']);
	unset($fields['comment']);
	unset($fields['cookies']);

//変数各項目を再度呼び出し直す（行を入れ替えれば項目の表示順が変わる）
	if(empty($under_text_input) || $under_text_input == ''){
		$fields['comment'] = $temp_comment;//「テキスト入力」欄
	}
	$fields['author'] = $temp_author;//「名前」欄
	$fields['email'] = $temp_email;//「メールアドレス」欄
	$fields['url'] = $temp_url;//「サイト」欄
	if($under_text_input == '1'){
		$fields['comment'] = $temp_comment;//「テキスト入力」欄
	}
	$fields['cookies'] = $temp_cookies;//Cookie利用のチェック

	return $fields;
}
add_filter( 'comment_form_fields', 'haup_ordered_and_change_comment_field' );

/*** ラベルのみを変更するもの ***/
function haup_set_comment_form_defaults_label( $defaults ) {
//「〇〇（作者）にコメントを残す」の部分は変更されない模様なので含めていない(20230816現在)
//title_reply_to

//ラベルの代替文字列
	//「コメントを残す」
	if(isset( GetOP04_blockoption()['change-comment-label-title-reply'] )){
		$title_reply_label = GetOP04_blockoption()['change-comment-label-title-reply'];
	}else{
		$title_reply_label = '';
	}

	//「コメントをキャンセル」
	if(isset( GetOP04_blockoption()['change-comment-label-cancel-reply-link'] )){
		$cancel_reply_link_label = GetOP04_blockoption()['change-comment-label-cancel-reply-link'];
	}else{
		$cancel_reply_link_label = '';
	}

	//「コメントを送信」
	if(isset( GetOP04_blockoption()['change-comment-label-submit'] )){
		$submit_label = GetOP04_blockoption()['change-comment-label-submit'];
	}else{
		$submit_label = '';
	}

//ラベルの有無で文字列を整形
	//「コメントを残す」
	if($title_reply_label == ''){
		$title_reply_text = esc_html(__( 'Leave a Reply','hima-art-utility' ));
	}else{
		$title_reply_text = esc_html($title_reply_label);
	}
//「コメントをキャンセル」
	if($cancel_reply_link_label == ''){
		$cancel_reply_link_text = esc_html(__( 'Cancel reply','hima-art-utility' ));
	}else{
		$cancel_reply_link_text = esc_html($cancel_reply_link_label);
	}
	//「コメントを送信」
	if($submit_label == ''){
		$submit_text = esc_html(__( 'Post Comment','hima-art-utility' ));
	}else{
		$submit_text = esc_html($submit_label);
	}

//書き換え処理
	//「コメントを残す」
		$defaults['title_reply'] = $title_reply_text;
	//「コメントをキャンセル」
		$defaults['cancel_reply_link'] = $cancel_reply_link_text;
	//「コメントを送信」
		$defaults['label_submit'] = $submit_text;

	return $defaults;
}
add_filter( 'comment_form_defaults', 'haup_set_comment_form_defaults_label' );

function haup_change_comment_notes_before_defaults_html( $defaults ) {

if(isset( GetOP04_blockoption()['change-comment-label-req'] )){
		$req_mark = GetOP04_blockoption()['change-comment-label-req'];
	}else{
		$req_mark = '';
	}

	if($req_mark ==''){
		$req_mark_text = esc_html(__('*','hima-art-utility'));
	}else{
		$req_mark_text = esc_html($req_mark);
	}

if(isset( GetOP04_blockoption()['message-line-break'] )){
	$br = GetOP04_blockoption()['message-line-break'];
}else{
	$br = '';
}

	if(!empty($br)){
		$br_text = hau_sanitize_html_tags('<br>');
	}else{
		$br_text = '';
	}

if(isset( GetOP04_blockoption()['change-comment-label-mail-unpublic'] )){
	$change_text_before = GetOP04_blockoption()['change-comment-label-mail-unpublic'];
}else{
	$change_text_before = '';
}

	if(!empty($change_text_before)){
		$change_before = esc_html($change_text_before);
	}else{
		$change_before = esc_html(__( 'Your email address will not be published.','hima-art-utility' ));
	}

if(isset( GetOP04_blockoption()['change-comment-label-req-message'] )){
	$change_text_after = GetOP04_blockoption()['change-comment-label-req-message'];
}else{
	$change_text_after = '';
}

if(!empty($change_text_after)){
	$change_after = hau_sanitize_html_tags('<span class="required">'.$req_mark_text.'</span><span>'.$change_text_after.'</span>');
}else{
	$change_after = wp_required_field_message();
}

//「メールアドレスが公開されることはありません。 ※ が付いている欄は必須項目です」を変更
	$defaults['comment_notes_before'] = '<p class="comment-notes"><span id="email-notes">'.$change_before.'</span>'.$br_text.'<span class="required-field-message">'.$change_after.'</p>';

	return $defaults;
}
add_filter( 'comment_form_defaults', 'haup_change_comment_notes_before_defaults_html' );

/*** 「コメントを残す」の上に追加するコンテンツ ***/
//comment_form_before
function haup_action_comment_form_before() {
	if(isset( GetOP04_blockoption()['add-comment-form-before'] )){
		$content = GetOP04_blockoption()['add-comment-form-before'];
	}else{
		$content = '';
	}

	if(!empty($content)){
	echo wp_kses_post($content);

	}

}
add_action( 'comment_form_before', 'haup_action_comment_form_before' );

/*** 「コメントを残す」の下に追加するコンテンツ ***/
//comment_form_top
function haup_action_comment_form_top() {
	if(isset( GetOP04_blockoption()['add-comment-form-top'] )){
		$content = GetOP04_blockoption()['add-comment-form-top'];
	}else{
		$content = '';
	}

	if(!empty($content)){
	echo wp_kses_post($content);
	}
}
add_action( 'comment_form_top', 'haup_action_comment_form_top' );

/*** 「次回のコメント．．」の下に追加するコンテンツ ***/
//comment_form_before_fields
function haup_action_comment_form_before_fields() {
	if(isset( GetOP04_blockoption()['add-comment-form-before-fields'] )){
		$content = GetOP04_blockoption()['add-comment-form-before-fields'];
	}else{
		$content = '';
	}

	if(!empty($content)){
	echo wp_kses_post($content);
	}
}
add_action( 'comment_form_before_fields', 'haup_action_comment_form_before_fields' );

/*** 「次回のコメント．．」の下へ追加するコンテンツ ***/
//comment_form_after_fields
function haup_action_comment_form_after_fields() {
	if(isset( GetOP04_blockoption()['add-comment-form-after-fields'] )){
		$content = GetOP04_blockoption()['add-comment-form-after-fields'];
	}else{
		$content = '';
	}

	if(!empty($content)){
	echo wp_kses_post($content);
	}
}
add_action( 'comment_form_after_fields', 'haup_action_comment_form_after_fields' );

/*** コメントフォームの下へ追加するコンテンツ ***/
//comment_form_after
function haup_action_comment_form_after() {
	if(isset( GetOP04_blockoption()['add-comment-form-after'] )){
		$content = GetOP04_blockoption()['add-comment-form-after'];
	}else{
		$content = '';
	}

	if(!empty($content)){
	echo wp_kses_post($content);
	}
}
add_action( 'comment_form_after', 'haup_action_comment_form_after' );

/*** 「〇〇としてログインしています」の下へ追加するコンテンツ ***/
//comment_form_logged_in_after
function haup_action_comment_form_logged_in_after() {
	if(isset( GetOP04_blockoption()['add-comment-form-logged-in-after'] )){
		$content = GetOP04_blockoption()['add-comment-form-logged-in-after'];
	}else{
		$content = '';
	}

	if(!empty($content)){
	echo wp_kses_post($content);
	}
}
add_action( 'comment_form_logged_in_after', 'haup_action_comment_form_logged_in_after' );

/*** 「ログインが必要です」の下へ追加するコンテンツ ***/
//comment_form_must_log_in_after
function haup_action_comment_form_must_log_in_after() {
	if(isset( GetOP04_blockoption()['add-comment-form-must-log-in-after'] )){
		$content = GetOP04_blockoption()['add-comment-form-must-log-in-after'];
	}else{
		$content = '';
	}

	if(!empty($content)){
	echo wp_kses_post($content);
	}
}
add_action( 'comment_form_must_log_in_after', 'haup_action_comment_form_must_log_in_after' );

/*** コメントが閉じているときに表示するコンテンツ ***/
//comments_closed
function haup_action_comments_closed() {
	if(isset( GetOP04_blockoption()['add-comments-closed'] )){
		$content = GetOP04_blockoption()['add-comments-closed'];
	}else{
		$content = '';
	}

	if(!empty($content)){
	echo wp_kses_post($content);
	}
}
add_action( 'comment_form_comments_closed', 'haup_action_comments_closed' );