<?php
/**
 * Functions for comments
 *
 * @package wpstream-theme
 */
function wpstream_theme_reply() {

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}

add_action( 'wp_enqueue_scripts', 'wpstream_theme_reply' );


// Comments.
if ( ! function_exists( 'wpstream_theme_comment' ) ) {
	/**
	 * Template for comments and pingbacks.
	 *
	 * Used as a callback by wp_list_comments() for displaying the comments.
	 *
	 * @param object $comment The current comment object.
	 * @param array  $args     An array of arguments.
	 * @param int    $depth    Depth of comment.
	 */
	function wpstream_theme_comment( $comment, $args, $depth ) {

		if ( 'pingback' === $comment->comment_type || 'trackback' === $comment->comment_type ) : ?>

			<li class="alert alert-info" id="comment-<?php comment_ID(); ?>" <?php comment_class( 'media' ); ?>>
			<div class="comment-body">
				<?php esc_html_e( 'Pingback:', 'hello-wpstream' ); ?><?php comment_author_link(); ?><?php edit_comment_link( esc_html__( 'Edit', 'hello-wpstream' ), '<span class="edit-link">', '</span>' ); ?>
			</div>

		<?php else : ?>

			<li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>

				<article id="div-comment-<?php comment_ID(); ?>" class="comment-body mb-4 d-flex">

					<div class="flex-shrink-0 me-3">
						<?php
						if ( 0 != $args['avatar_size'] ) {
							echo get_avatar( $comment, $args['avatar_size'], '', '', array( 'class' => 'img-thumbnail rounded-circle' ) );
						}
						?>
					</div>

					<div class="comment-content">
						<div class="card">
							<div class="card-body">

								<div class="mt-0"><?php printf( __( '%s <span class="says d-none">says:</span>', 'hello-wpstream' ), sprintf( '<h3 class="h5">%s</h3>', get_comment_author_link() ) ); ?>
								</div>

								<p class="small comment-meta text-secondary">
									<time datetime="<?php comment_time( 'c' ); ?>">
										<?php printf( _x( '%1$s at %2$s', '1: date, 2: time', 'hello-wpstream' ), get_comment_date(), get_comment_time() ); ?>
									</time>
									<?php edit_comment_link( __( 'Edit', 'hello-wpstream' ), '<span class="edit-link">', '</span>' ); ?>
								</p>

								<?php if ( '0' == $comment->comment_approved ) : ?>
									<p class="comment-awaiting-moderation alert alert-info"><?php _e( 'Your comment is awaiting moderation.', 'hello-wpstream' ); ?></p>
								<?php endif; ?>

								<?php comment_text(); ?>

								<?php
								comment_reply_link(
									array_merge(
										$args,
										array(
											'add_below' => 'div-comment',
											'depth'     => $depth,
											'max_depth' => $args['max_depth'],
											'before'    => '<p class="reply comment-reply">',
											'after'     => '</p>',
										)
									)
								);
								?>
							</div> <!-- card-body -->
						</div><!-- card -->
					</div><!-- .comment-content -->

				</article><!-- .comment-body -->
			</li><!-- #comment -->

			<?php
		endif;
	}
} // ends check for wpstream_theme_comment()


// h2 Reply Title
add_filter( 'comment_form_defaults', 'custom_reply_title' );
function custom_reply_title( $defaults ) {
	$defaults['title_reply_before'] = '<h2 id="reply-title" class="mb-30">';
	$defaults['title_reply_after']  = '</h2>';
	return $defaults;
}

// h2 Reply Title End


// Comment Cookie Checkbox
function wpstream_change_comment_form_cookies_consent( $defaults ) {
	$commenter = wp_get_current_commenter();
	$req       = get_option( 'require_name_email' );
	$html5     = true;

	// Define attributes in HTML5 or XHTML syntax.
	$required_attribute = ( $html5 ? ' required' : ' required="required"' );

	$defaults['comment_notes_before'] = '';

	$user = wp_get_current_user();
	if ( $user->exists() ) {
		$defaults['class_container'] .= ' wpstream-comment-form-login';
		$user_url                     = wpstream_get_author_profile_image_url_by_author_id( $user->ID, 48 );
		$defaults['comment_field']    = sprintf(
			'<p class="comment-form-comment">%s %s</p>',
			sprintf(
				'<img class="rounded-circle" src="%s" alt="%s">',
				esc_url( $user_url ),
				esc_attr( $user->display_name )
			),
			'<textarea id="comment" name="comment" cols="45" rows="3" maxlength="65525"' . $required_attribute . ' placeholder="' . esc_attr__( 'Add a comment', 'hello-wpstream' ) . '"></textarea>'
		);
	} else {
		$defaults['comment_field'] = sprintf(
			'<p class="comment-form-comment">%s %s</p>',
			sprintf(
				'<label for="comment">%s</label>',
				_x( 'Leave a comment', 'noun', 'hello-wpstream' ),
			),
			'<textarea id="comment" name="comment" cols="45" rows="3" maxlength="65525"' . $required_attribute . ' placeholder="' . esc_attr__( 'Add a comment', 'hello-wpstream' ) . '"></textarea>'
		);
	}

	$defaults['fields']['author'] = sprintf(
		'<p class="comment-form-author">%s %s</p>',
		sprintf(
			'<label for="author">%s</label>',
			__( 'Name', 'hello-wpstream' )
		),
		sprintf(
			'<input id="author" name="author" type="text" value="%s" size="30" maxlength="245" autocomplete="name" %s placeholder="%s"/>',
			esc_attr( $commenter['comment_author'] ),
			( $req ? $required_attribute : '' ),
			esc_attr__( 'Your name', 'hello-wpstream' )
		)
	);

	$defaults['fields']['email'] = sprintf(
		'<p class="comment-form-email">%s %s</p>',
		sprintf(
			'<label for="email">%s</label>',
			__( 'Email', 'hello-wpstream' ),
		),
		sprintf(
			'<input id="email" name="email" %s value="%s" size="30" maxlength="100" aria-describedby="email-notes" autocomplete="email" %s placeholder="%s"/>',
			( $html5 ? 'type="email"' : 'type="text"' ),
			esc_attr( $commenter['comment_author_email'] ),
			( $req ? $required_attribute : '' ),
			esc_attr__( 'Email address', 'hello-wpstream' )
		)
	);

	$defaults['fields']['url'] = sprintf(
		'<p class="comment-form-url">%s %s</p>',
		sprintf(
			'<label for="url">%s</label>',
			__( 'Website', 'hello-wpstream' )
		),
		sprintf(
			'<input id="url" name="url" %s value="%s" size="30" maxlength="200" autocomplete="url" placeholder="%s" />',
			( $html5 ? 'type="url"' : 'type="text"' ),
			esc_attr( $commenter['comment_author_url'] ),
			esc_attr__( 'URL', 'hello-wpstream' )
		)
	);

	$defaults['label_submit'] = esc_html__( 'Comment', 'hello-wpstream' );
	// remove logged_in_as
	$defaults['logged_in_as'] = '';

	return $defaults;
}

add_filter( 'comment_form_defaults', 'wpstream_change_comment_form_cookies_consent' );
// Comment Cookie Checkbox End


// Open comment author link in new tab
add_filter( 'get_comment_author_link', 'wpstream_open_comment_author_link_in_new_window' );
function wpstream_open_comment_author_link_in_new_window( $author_link ) {
	return str_replace( '<a', "<a target='_blank'", $author_link );
}
// Open comment author link in new tab End
