<?php
/**
 * This file contains the general functions used by the theme.
 * 
 * @author Pix & Hue
 */

/*****************************************************************************
 * Title Tag
****************************************************************************/
add_action( 'after_setup_theme', 'rilee_title_setup' );

if ( !function_exists('rilee_title_setup') )  {

	function rilee_title_setup() {

		add_theme_support( 'title-tag' );
	}
}

/*****************************************************************************
 * Google Font Functions
****************************************************************************/
if ( !function_exists('rilee_google_fonts_url') )  {

	function rilee_google_fonts_url() {

		$fonts_url = '';

		 /*
   		 Translators: If there are characters in your language that are not supported
   		 by chosen font(s), translate this to 'off'. Do not translate into your own language.
    	*/
   		 if ( 'off' !== _x( 'on', 'Google font: on or off', 'rilee' ) ) {
        	$fonts_url = add_query_arg( 'family', urlencode( 'PT Mono:400|Josefin Sans:300,300i,400,700&display=swap' ), "//fonts.googleapis.com/css?" );
   		 }
   		 
   		return esc_url_raw($fonts_url);
	}
}		

//Enqueue Fonts
add_action( 'wp_enqueue_scripts', 'rilee_enqueue_google_fonts' );

if ( !function_exists('rilee_enqueue_google_fonts') )  {
	
	function rilee_enqueue_google_fonts() {
		
		wp_enqueue_style( 'rilee-google-fonts', rilee_google_fonts_url(), array(), null );
	}
}	

/*****************************************************************************
 * WordPress Customizer - Sanitize Select
****************************************************************************/
if ( !function_exists('rilee_customizer_sanitize_select') )  {

	function rilee_customizer_sanitize_select($input, $setting) {

	    $input = sanitize_key($input);
	    $choices = $setting->manager->get_control($setting->id)->choices;

	    return (array_key_exists($input, $choices) ? $input : $setting->default);
	}
}	

/*****************************************************************************
 * Modify Comments Form - Add Placeholders & Remove Labels from Fields 
****************************************************************************/	
add_filter('comment_form_default_fields','rilee_mod_comment_form');	

if ( !function_exists('rilee_mod_comment_form') )  {
	
	function rilee_mod_comment_form($fields){

	    $fields['author'] = '<input placeholder="' . esc_html__( 'name', 'rilee' ) . '" id="author" name="author" type="text">';
	    $fields['email'] = '<input type="email" placeholder="' . esc_html__( 'email', 'rilee' ) . '"  id="email" name="email">';
	    $fields['url'] = '<input type="text" id="url" name="url" placeholder="' . esc_html__( 'website', 'rilee' ) . '">';

	    return $fields;
	}
}

/*****************************************************************************
 * Comment Layout
****************************************************************************/
if ( !function_exists('rilee_post_comments') )  {

	function rilee_post_comments($comment, $args, $depth) {

		$GLOBALS['comment'] = $comment; 

		 ?>

		<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">

			<div class="rilee_thecomment">

				<div class="rilee-comment-author-avatar">
					<?php echo get_avatar($comment, 80); ?>
				</div>
				<div class="rilee-comment-text">
					<h6 class="rilee-comment-author"><?php echo get_comment_author_link(); ?></h6>
					
					<?php if( $comment->comment_parent ) { ?>
    					<span class="rilee-comment-parent"> &gt; <?php comment_author( $comment->comment_parent ); ?></span>
    				<?php } ?>
    				
					<div class="rilee-comment-date"><?php printf(esc_html__('%1$s', 'rilee'), get_comment_date()) ?></div>

					<?php if ($comment->comment_approved == '0') { ?>
						<span class="rilee-comment-approval"><em><?php esc_html_e('Comment awaiting approval', 'rilee'); ?></em></span>
					<?php } ?>
					
					
					<?php comment_text(); ?>
					
					<div class="rilee-comment-footer">
						<span class="rilee-reply">
							<?php comment_reply_link(array_merge( $args, array('reply_text' => esc_html__('Reply', 'rilee'), 'depth' => $depth, 'max_depth' => $args['max_depth'])), $comment->comment_ID); ?>
						</span>
						<span class="rilee-edit"><?php edit_comment_link(esc_html__('Edit', 'rilee')); ?></span>
					</div>	

				</div>	
			</div>	
		</li>	
		<?php
	}
}	

/************************************************************************
 	* Modify WooCommerce Review Form
*************************************************************************/
add_filter( 'woocommerce_product_review_comment_form_args', 'rilee_woo_mod_review_form' );

if (!function_exists('rilee_woo_mod_review_form')) {

	function rilee_woo_mod_review_form( $review_form ) {

		$commenter = wp_get_current_commenter();

    	// Shown to non-logged in users above name / email
    	$review_form['fields'] = array(
    		'author' => '<p class="comment-form-author">' . '<input id="author" name="author" type="text" placeholder="' . esc_html__( 'author *', 'rilee' ) . 
    		            '" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30" aria-required="true" required /></p>',
			'email' => '<p class="comment-form-email">' . '<input id="email" name="email" type="email" placeholder="' . esc_html__( 'email *', 'rilee' ) . 
			           '" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30" aria-required="true" required /></p>',			
			);

    	if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' ) {
			$review_form['comment_field'] = '<p class="comment-form-rating"><label for="rating">' . esc_html__( 'Your Rating', 'rilee' ) .'</label><select name="rating" id="rating" aria-required="true" required>
				<option value="">' . esc_html__( 'Rate&hellip;', 'rilee' ) . '</option>
				<option value="5">' . esc_html__( 'Perfect', 'rilee' ) . '</option>
				<option value="4">' . esc_html__( 'Good', 'rilee' ) . '</option>
				<option value="3">' . esc_html__( 'Average', 'rilee' ) . '</option>
				<option value="2">' . esc_html__( 'Not that bad', 'rilee' ) . '</option>
				<option value="1">' . esc_html__( 'Very Poor', 'rilee' ) . '</option>
			</select></p>';
		}   

		$review_form['comment_field'] .= '<p class="comment-form-comment"><textarea id="comment" name="comment" placeholder="' . esc_html__( 'YOUR REVIEW *', 'rilee' ) . 
    	                                '" cols="45" rows="8" aria-required="true" required></textarea></p>';                             
   
    	return $review_form;
	}
} 

/*****************************************************************************
 * Footer - Custom Copyright Text
****************************************************************************/
add_action( 'wp_footer', 'rilee_footer_copyright_text' );

if ( !function_exists('rilee_footer_copyright_text') )  {

	function rilee_footer_copyright_text() { 

		$copyright_text = get_theme_mod('rilee_footer_custom_copyright'); 

		if(!isset($copyright_text) || trim($copyright_text) != '') { ?>

			<div class="pixandhue-copyright">
				<p><?php echo wp_kses_post($copyright_text . ' <span class="ph-marketing">Design by <a target="_blank" href="' . esc_url('https://pixandhue.com') .'">Pix & Hue.</a></span>'); ?></p>	
			</div>	

			<?php

		} else { ?>

			<div class="pixandhue-copyright">
				<p>Copyright &copy; <?php echo date("Y") . ' ' . get_bloginfo(); ?>. All Rights Reserved. <span class="ph-marketing">Design by <a target="_blank" href="<?php echo esc_url('https://pixandhue.com'); ?>">Pix &amp; Hue.</a></span></p>
				</p>	
			</div>	

			<?php
		}		
	}
}