<?php
add_filter( 'comment_form_default_fields', 'author_portfolio_comment_form' );
function author_portfolio_comment_form( $author_portfolio_comment_field ) {
	$author_portfolio_comment_field['author'] = '<input type="text" class="form-control" name="author" id="name-cmt" placeholder="' . esc_attr__( 'Your Name', 'author-portfolio' ) . '">';
	$author_portfolio_comment_field['email']  = '<input type="email" class="form-control" name="email" id="email-cmt" placeholder="' . esc_attr__( 'Your Email', 'author-portfolio' ) . '">';
	$author_portfolio_comment_field['url']    = '
                    <input type="text" class="form-control" name="url" id="website" placeholder="' . esc_attr__( 'Your Website', 'author-portfolio' ) . '">';
	return $author_portfolio_comment_field;
}
add_filter( 'comment_form_defaults', 'author_portfolio_comment_default_form' );
function author_portfolio_comment_default_form( $default_form ) {
    $default_form['comment_field']        = '<textarea class="form-control" name="comment" rows="7" placeholder="' . esc_attr__( 'Message goes here', 'author-portfolio' ) . '"></textarea>';
    $default_form['submit_button']        = '<button type="submit" class="btn btn-primary">' . esc_attr__( 'Post Comment', 'author-portfolio' ) . '</button>';
    $default_form['comment_notes_before'] = wp_nonce_field( 'author_portfolio_comment', 'author_portfolio_comment_nonce', true, false );
    $default_form['title_reply']          = esc_attr__( 'Leave A Comment', 'author-portfolio' );
    $default_form['title_reply_before']   = '<h4>';
    $default_form['title_reply_after']    = '</h4>';
    return $default_form;
}

function author_portfolio_comment_form_structure( $fields ) {
		$author_field = $fields['author'];
		$email_field = $fields['email'];
		if (function_exists('is_woocommerce') && !is_woocommerce()) {
			$url_field = $fields['url'];
		}
		$comment_field = $fields['comment'];
		$cookies_field = $fields['cookies'];
		unset( $fields['author'] );
		unset( $fields['email'] );
		unset( $fields['url'] );
		unset( $fields['comment'] );
		unset( $fields['cookies'] );
		$fields['author'] = $author_field;
		$fields['email'] = $email_field;
		if (function_exists('is_woocommerce') && !is_woocommerce()) {
			$fields['url'] = $url_field;
		}
		$fields['comment'] = $comment_field;
		$fields['cookies'] = $cookies_field;
	return $fields;
}

add_filter( 'comment_form_fields', 'author_portfolio_comment_form_structure' );

if ( ! function_exists( 'author_portfolio_comment_list' ) ) :
	/**
	 * Template for displaying comments and pingbacks.
	 *
	 * @since Shape 1.0
	 */
	function author_portfolio_comment_list( $comment, $args, $depth ) {
		$tag       = ( 'div' === $args['style'] ) ? 'div' : 'li';
		$add_below = ( 'div' === $args['style'] ) ? 'comment' : 'div-comment';
		$has_children_class = empty( $args['has_children'] ) ? '' : 'parent';

		echo sprintf( '<%1$s %2$s id="comment-%3$d">', esc_attr( $tag ), comment_class( $has_children_class, null, null, false ), get_comment_ID() );

		if ( 'div' === $args['style'] ) {
			echo '<div id="div-comment-' . get_comment_ID() . '" class="comment-body review-list">';
		}
		?>

		<div class="single-comment">
			<div class="commenter-image">
				<?php
				if ( 0 !== (int) $args['avatar_size'] ) {
					echo get_avatar( $comment, $args['avatar_size'], '', '', array( 'class' => 'lazyload', 'loading' => 'lazy' ) );
				}
				?>
			</div>

			<div class="commnenter-details">
				<?php if ( '0' === $comment->comment_approved ) : ?>
					<em><?php esc_html_e( 'Your comment is awaiting moderation.', 'author-portfolio' ); ?></em>
					<br />
				<?php endif; ?>

				<div class="comment-meta">
					<div class="comment-time">
						<p>
							<time datetime="<?php comment_time( 'c' ); ?>">
								<?php echo esc_html( get_comment_date() . ' ' . get_comment_time() ); ?>
							</time>
						</p>
					</div>
					<h4><?php echo wp_kses_post( get_comment_author_link() ); ?></h4>
				</div>

				<?php
				comment_text();
				if ( current_user_can( 'edit_comment', $comment->comment_ID ) && ( time() - strtotime( $comment->comment_date_gmt ) < 300 ) ) {
				    echo '<div class="edit-comment">';
				    edit_comment_link( esc_html__( 'Edit', 'author-portfolio' ), '', '' );
				    echo '</div>';
				}
				comment_reply_link(
					array_merge(
						$args,
						array(
							'depth'     => $depth,
							'max_depth' => $args['max_depth'],
							'before'    => '<div class="reply">',
							'after'     => '</div>',
						)
					)
				);
				?>
			</div>
		</div>

		<?php
		if ( 'div' === $args['style'] ) {
			echo '</div>';
		}

		echo "</$tag>";
	}
endif;

add_action( 'wp_insert_comment', 'author_portfolio_clear_comment_count_transient', 10, 2 );
function author_portfolio_clear_comment_count_transient( $comment_id, $comment ) {
    if ( $comment->comment_approved == 1 ) {
        delete_transient( 'author_portfolio_comment_count_' . $comment->comment_post_ID );
    }
}

add_filter( 'preprocess_comment', 'author_portfolio_sanitize_comment_data' );
function author_portfolio_sanitize_comment_data( $comment_data ) {
    $comment_data['comment_author'] = sanitize_text_field( $comment_data['comment_author'] );
    $comment_data['comment_author_email'] = sanitize_email( $comment_data['comment_author_email'] );
    $comment_data['comment_author_url'] = esc_url_raw( $comment_data['comment_author_url'] );
    $comment_data['comment_content'] = wp_kses_post( $comment_data['comment_content'] );
    return $comment_data;
}

add_action( 'pre_comment_on_post', 'author_portfolio_verify_comment_nonce' );
function author_portfolio_verify_comment_nonce() {
    if ( ! isset( $_POST['author_portfolio_comment_nonce'] ) || ! wp_verify_nonce( $_POST['author_portfolio_comment_nonce'], 'author_portfolio_comment' ) ) {
        wp_die( esc_html__( 'Security check failed. Please try again.', 'author-portfolio' ) );
    }
}