<?php
/**
	Functions which enhance the theme by hooking into WordPress
	@package WordPress
	@subpackage Uikit Starter
	@since 1.0.1
*/


/* Prepare
______________________________
*/

	// If this file is called directly,abort.
	if(!defined('WPINC')){die;}


/* Exec
______________________________
*/

	add_filter('comment_class','__hook_comment_class');
	/**
		@since 1.0.1
			Filters the returned CSS classes for the current comment.
		@link https://developer.wordpress.org/reference/hooks/comment_class/
	*/
	if(function_exists('__hook_comment_class') === FALSE) :
	function __hook_comment_class($classes)
	{
		// Reset array.
		// $classes = [];

		$classes[] = 'uk-comment';

		foreach($classes as $key => $class){
			if(strstr($class,'comment-author-')) :
				$classes[$key] = 'comment-author';
			endif;
		}// endforeach

		if(get_comment_author() !== get_the_author()){
			unset($classes['byuser']);
			$classes[] = 'guest';
		}// endif

		return $classes;

	}// Method
	endif;


	add_filter('wp_list_comments_args','__hook_wp_list_comments_args');
	/**
		@since 1.0.1
			Filters the arguments used in retrieving the comment list.
		@link https://developer.wordpress.org/reference/hooks/wp_list_comments_args/
	*/
	if(function_exists('__hook_wp_list_comments_args') === FALSE) :
	function __hook_wp_list_comments_args($r)
	{
		return apply_filters('uikit_starter_wp_list_comments_args',wp_parse_args(array(
			'style' => 'ol',
			'short_ping' => TRUE,
			//'per_page' => 3,
			'avatar_size' => 75,
			'reverse_top_level' => TRUE,
			'callback' => '__extra_get_comment_callback',
			'echo' => TRUE,
		),$r));

	}// Method
	endif;


	/**
		@since 1.0.1
			Callback function for wp_list_comments().
		@link https://developer.wordpress.org/reference/functions/wp_list_comments/
	*/
	if(function_exists('__extra_get_comment_callback') === FALSE) :
	function __extra_get_comment_callback($comment,$args,$depth)
	{
		$GLOBALS['comment'] = $comment;
		extract($args,EXTR_SKIP);

		/**
			@description
				Render the opening <li> tag.
		*/
		$comment_class = empty($args['has_children']) ? '' : 'parent';
		printf(
			'<li id="comment-%d" %s>',
			(int)get_comment_ID(),
			comment_class($comment_class,$comment,NULL,FALSE)
		);
		?>
		<div id = "div-comment-<?php echo get_comment_ID(); ?>" class = "uk-comment-body uk-margin comment-body">
			<div class = "uk-comment-title comment-author uk-clearfix">
				<?php
				if(get_option('show_avatars') == 1){
					if($args['avatar_size'] != 0) :
						?><div class = "uk-comment-avatar uk-margin uk-align-left">
							<?php echo get_avatar($comment,$args['avatar_size']); ?>
						</div><!-- .avatar --><?php
					endif;
				}// endif
				printf(__('<cite>%s</cite>','uikit-starter'),get_comment_author_link());

				if($comment->comment_approved == '0'){
					?><span class = "comment-awaiting-moderation"><?php echo __('Your comment is awaiting moderation.','uikit-starter'); ?></span>
				<?php }// endif ?>

				<div class = "uk-comment-meta comment-meta commentmetadata">
					<a href = "<?php echo htmlspecialchars(get_comment_link($comment->comment_ID)); ?>">
						<?php
						/* translators: 1: date, 2: time */
						printf( __('%1$s at %2$s','uikit-starter'),get_comment_date(),get_comment_time());
						?>
					</a>
					<?php edit_comment_link(__('(Edit)','uikit-starter'),'  <span uk-icon="icon: file-edit; ratio: 0.8"></span> ',''); ?>
				</div><!-- /.commentmetadata -->
			</div><!-- /.author -->

			<div class = "comment-text uk-padding-small">
				<?php comment_text(); ?>
			</div>

			<?php 
			$reply = get_comment_reply_link(array_merge($args,array(
				'depth' => $depth,
				'max_depth' => $args['max_depth']
			)));
			?>
			<?php if($reply != ''){ ?>
				<div class = "reply"><?php echo $reply; ?></div>
			<?php }// endif ?>

		</div><!-- .body -->

	<?php
	}// Method
	endif;


	add_filter('comment_form_defaults','__hook_comment_form_defaults');
	/**
		@since 1.0.1
			Filters the comment form default arguments.
		@link https://developer.wordpress.org/reference/hooks/comment_form_defaults/
	*/
	if(function_exists('__hook_comment_form_defaults') === FALSE) :
	function __hook_comment_form_defaults($defaults)
	{
		/**
			@description
				hide the default text before/after the form
		*/
		$defaults['comment_notes_before'] = '';
		$defaults['comment_notes_after'] = '';

		$defaults['comment_field'] = '<p><textarea id="comment" class="uk-width-1-1" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>';

		// $defaults['id_form'] = 'comment-form';
		// $defaults['id_submit'] = 'comment-submit';

		$defaults['class_submit'] = 'uk-button uk-width-1-1 uk-button-primary submit';

		// $defaults['title_reply_before'] = '<div id = "reply-title" class = "comment-reply-title">';
		// $defaults['title_reply_after'] = '</div>';
		// $defaults['submit_field'] = '<p class = "form-submit">%1$s %2$s</p>';

		return $defaults;

	}// Method
	endif;


	add_filter('comment_reply_link','__hook_comment_reply_link');
	/**
		@since 1.0.1
			Filters the comment reply link.
		@link https://developer.wordpress.org/reference/hooks/comment_reply_link/
	*/
	if(function_exists('__hook_comment_reply_link') === FALSE) :
	function __hook_comment_reply_link($html)
	{
		$html = preg_replace('~<a(.+?)class=\'(.+?)\'(.+?)~','<p class="uk-align-right uk-margin-right"><a\1class=\'\2 uk-button uk-button-small uk-button-secondary\'\3',$html);		$html = preg_replace('~</a>"~','</a></p>',$html);
		return $html;

	}// Method
	endif;


	add_filter('cancel_comment_reply_link','__hook_cancel_comment_reply_link',10,3);
	/**
		@since 1.0.1
			Filters the cancel comment reply link HTML.
		@param (string) $formatted_link
			The HTML-formatted cancel comment reply link.
		@param (string) $link
			Cancel comment reply link URL.
		@param (string) $text
			Cancel comment reply link text.
		@return (string)
	*/
	if(function_exists('__hook_cancel_comment_reply_link') === FALSE) :
	function __hook_cancel_comment_reply_link($formatted_link,$link,$text)
	{
		$style = isset($_GET['replytocom']) ? '' : 'display: none;';

		// phpcs:ignore WordPress.CSRF.NonceVerification.NoNonceVerification -- Used to determine inline style.
		// Automatically escaped.
		$formatted_link = '<span class="uk-margin-left"><a href="' . $link . '" id="cancel-comment-reply-link" class="uk-button uk-button-small uk-button-danger" style="' . $style . '" rel="nofollow">';
		$formatted_link .= $text;
		$formatted_link .= '</a></span>';

		return $formatted_link;

	}// Method
	endif;
