<?php
/**
 * Comments
 *
 * @package Multi
 */

if ( ! defined( 'ABSPATH' ) ) {
	die;
}
/**
 * Customizes comment list markup
 *
 * @param object  $comment Comment object.
 * @param array   $args Arguments customizing the output.
 * @param integer $depth Comment nesting depth.
 */
function multimagazine_comment( $comment, $args, $depth ) {
	?>
	<li <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent', $comment->comment_ID ); ?> id="comment-<?php echo esc_attr( $comment->comment_ID ); ?>">
	<div id="div-comment-<?php echo esc_attr( $comment->comment_ID ); ?>">
		<div class="comment-author vcard">
			<?php
			if ( 0 !== absint( $args['avatar_size'] ) ) {
				echo wp_kses_post( get_avatar( $comment, $args['avatar_size'] ) );
			}
			?>
			<cite class="fn"><?php echo wp_kses_post( get_comment_author_link( $comment->comment_ID ) ); ?></cite>
			<p class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>"><?php echo esc_html( get_comment_date() ); ?> <?php esc_html_e( 'at', 'multimagazine' ); ?> <?php echo esc_html( get_comment_time() ); ?></a>
			<?php
			comment_reply_link(
				array_merge(
					$args,
					array(
						'add_below' => 'div-comment',
						'depth'     => $depth,
						'max_depth' => $args['max_depth'],
						'before'    => '<span class="sep">|</span>',
					)
				)
			);
			?>
			</p>
		</div>
		<?php if ( ! $comment->comment_approved ) : ?>
			<p class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'multimagazine' ); ?></p>
		<?php endif; ?>
		<div class="comment-body"><?php comment_text(); ?></div>
	</div>
	<?php
}

/**
 * Reorders fields in the comment form
 *
 * @param array $fields Form fields.
 */
function multimagazine_comment_fields_custom_order( $fields ) {
	$cookies_opt_in_enabled = ! empty( $fields['cookies'] );
	if ( $cookies_opt_in_enabled ) {
		$cookie_field = $fields['cookies'];
	}
	if ( $cookies_opt_in_enabled ) {
		unset( $fields['cookies'] );
	}

	$fields['comment'] = '<p class="comment-form-comment"><label for="comment">' . esc_html__( 'Comment', 'multimagazine' ) . '</label> <textarea id="comment" name="comment" cols="45" rows="5" maxlength="65525" required></textarea></p>';

	if ( $cookies_opt_in_enabled ) {
		$fields['cookies'] = $cookie_field;
	}
	return $fields;
}
add_filter( 'comment_form_fields', 'multimagazine_comment_fields_custom_order' );

/**
 * Custom markup of the submit button
 *
 * @param object $submit_field Submit field object supplied by WP.
 */
function multimagazine_submit_button( $submit_field ) {
	$new_submit = str_replace(
		'<input name="submit" type="submit" id="submit" class="submit" value="Post Comment">',
		'<button name="submit" type="submit" id="submit" class="button submit" value="Post Comment">' . __( 'Post comment', 'multimagazine' ) . '</button>',
		$submit_field
	);

	return $new_submit;
}
add_filter( 'comment_form_submit_field', 'multimagazine_submit_button' );
