<?php
/**
 * This file is the copy of `Divi/includes/builder/comments_template.php`.
 *
 * This file is called by the CommentContentTrait to render the comments section.
 *
 * @package ET_Builder
 * @since ??
 */

/**
 * Comments template.
 */
if ( post_password_required() ) : ?>

<p class="nocomments container">
	<?php esc_html_e( 'This post is password protected. Enter the password to view comments.', 'et_builder_5' ); ?></p>
	<?php
	return;
	endif;

	global $et_comments_header_level, $et_comments_form_title_level, $et_comments_wrap_classes;

	$et_comments_header_level_processed = isset( $et_comments_header_level ) && '' !== $et_comments_header_level ? et_pb_process_header_level( $et_comments_header_level, 'h1' ) : 'h1';
	$et_comments_wrap_class_attr        = ! empty( $et_comments_wrap_classes ) ? ' class="' . esc_attr( $et_comments_wrap_classes ) . '"' : '';
?>

<?php
if ( empty( $comments_by_type ) ) {
	$comments_by_type = separate_comments( $comments );
}
?>

<section id="comment-wrap"<?php echo et_core_esc_previously( $et_comments_wrap_class_attr ); ?>>
	<<?php echo et_core_intentionally_unescaped( $et_comments_header_level_processed, 'fixed_string' ); ?> id="comments"
		class="page_title">
		<?php comments_number( esc_html__( '0 Comments', 'et_builder_5' ), esc_html__( '1 Comment', 'et_builder_5' ), '% ' . esc_html__( 'Comments', 'et_builder_5' ) ); ?>
	</<?php echo et_core_intentionally_unescaped( $et_comments_header_level_processed, 'fixed_string' ); ?>>
	<?php if ( have_comments() ) : ?>
		<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // Are there comments to navigate through? ?>
	<div class="comment_navigation_top clearfix">
		<div class="nav-previous">
			<?php previous_comments_link( et_get_safe_localization( __( '<span class="meta-nav">&larr;</span> Older Comments', 'et_builder_5' ) ) ); ?>
		</div>
		<div class="nav-next">
			<?php next_comments_link( et_get_safe_localization( __( 'Newer Comments <span class="meta-nav">&rarr;</span>', 'et_builder_5' ) ) ); ?>
		</div>
	</div>
	<?php endif; // check for comment navigation. ?>

		<?php if ( ! empty( $comments_by_type['comment'] ) ) : ?>
	<ol class="commentlist clearfix">
			<?php
				wp_list_comments(
					[
						'type'     => 'comment',
						'callback' => 'et_custom_comments_display',
					]
				);
			?>
	</ol>
	<?php endif; ?>

		<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // Are there comments to navigate through? ?>
	<div class="comment_navigation_bottom clearfix">
		<div class="nav-previous">
			<?php previous_comments_link( et_get_safe_localization( __( '<span class="meta-nav">&larr;</span> Older Comments', 'et_builder_5' ) ) ); ?>
		</div>
		<div class="nav-next">
			<?php next_comments_link( et_get_safe_localization( __( 'Newer Comments <span class="meta-nav">&rarr;</span>', 'et_builder_5' ) ) ); ?>
		</div>
	</div>
	<?php endif; // check for comment navigation. ?>

		<?php if ( ! empty( $comments_by_type['pings'] ) ) : ?>
	<div id="trackbacks">
		<h3 id="trackbacks-title"><?php esc_html_e( 'Trackbacks/Pingbacks', 'et_builder_5' ); ?></h3>
		<ol class="pinglist">
			<?php wp_list_comments( 'type=pings&callback=et_list_pings' ); ?>
		</ol>
	</div>
	<?php endif; ?>
	<?php else : // this is displayed if there are no comments so far. ?>
	<div id="comment-section" class="nocomments">
		<?php if ( 'open' === $post->comment_status ) : // phpcs:ignore ET.Comments.Todo.TodoFound -- Legacy TODO: May not be tracked in GitHub issues yet. Preserve for future tracking/removal. // TODO feat(D5, Refactor) Re-evaluate this `if else` block in future as we have not put any code here so we can remove it by any chance. ?>

		<?php else : // comments are closed. ?>

		<?php endif; ?>
	</div>
	<?php endif; ?>
	<?php if ( 'open' === $post->comment_status ) : ?>
		<?php
			// Comment submit content title level.
			$et_comments_form_title_level_processed = isset( $et_comments_form_title_level ) && '' !== $et_comments_form_title_level ? et_pb_process_header_level( $et_comments_form_title_level, 'h3' ) : 'h3';
			$et_comments_form_title_level_escaped   = et_core_intentionally_unescaped( $et_comments_form_title_level_processed, 'fixed_string' );

			comment_form(
				[
					'label_submit'        => esc_attr__( 'Submit Comment', 'et_builder_5' ),
					'title_reply'         => '<span>' . esc_attr__( 'Submit a Comment', 'et_builder_5' ) . '</span>',
					'title_reply_to'      => esc_attr__( 'Leave a Reply to %s', 'et_builder_5' ),
					'title_reply_before'  => '<' . $et_comments_form_title_level_escaped . ' id="reply-title" class="comment-reply-title">',
					'title_reply_after'   => '</' . $et_comments_form_title_level_escaped . '>',
					'class_submit'        => 'submit et_pb_button',
					'submit_field'        => '<p class="form-submit et_pb_button_wrapper">%1$s %2$s</p>',
					'comment_notes_after' => '',
					'id_submit'           => 'et_pb_submit',
				]
			);
		?>
	<?php else : // phpcs:ignore ET.Comments.Todo.TodoFound -- Legacy TODO: May not be tracked in GitHub issues yet. Preserve for future tracking/removal. // TODO feat(D5, Refactor) Re-evaluate this `else` in future as we have not put any code here so we can remove it by any chance. ?>

	<?php endif; // if you delete this the sky will fall on your head. ?>
</section>
