<?php

function edify_mobile_menu() {?>
	<div class="mobile-menu">
		<div class="top-close-menu"><i class="fa fa-times" aria-hidden="true"></i></div>
		 <?php edify_main_menu();?>
	</div>
<?php

}
 
/**
* edify_user_profile_fields
*/
 
function edify_user_profile_fields( $methods ) {
    $methods['designation'] = 'Designation';
    $methods['facebook_link'] = 'Facebook Link';
    $methods['twitter_link'] = 'Twitter Link';
    $methods['linkedin_link'] = 'Linkedin Link';
    $methods['youtube_link'] = 'Youtube Link';
    return $methods;
}
add_action( 'user_contactmethods', 'edify_user_profile_fields' );




/**
* edify_custom_exerpt
*/
 
function edify_custom_excerpt_length( $length ) {
    return 15;
}
add_filter( 'excerpt_length', 'edify_custom_excerpt_length', 999 );


function edify_excerpt_more( $more ) {
	return '';
}
add_filter('excerpt_more', 'edify_excerpt_more');


//Comment Field Order
add_filter( 'comment_form_fields', 'mo_comment_fields_custom_order' );
function mo_comment_fields_custom_order( $fields ) {
    $comment_field = $fields['comment'];
    $author_field = $fields['author'];
    $email_field = $fields['email'];
    // $url_field = $fields['url'];
    $cookies_field = $fields['cookies'];
    unset( $fields['comment'] );
    unset( $fields['author'] );
    unset( $fields['email'] );
    unset( $fields['url'] );
    unset( $fields['cookies'] );
    // the order of fields is the order below, change it as needed:
    $fields['author'] = $author_field;
    $fields['email'] = $email_field;
    // $fields['url'] = $url_field;
    $fields['comment'] = $comment_field;
    $fields['cookies'] = $cookies_field;
    // done ordering, now return the fields:
    return $fields;
}



/**
 * [edify_header_social_profiles description]
 * @return [type] [description]
 */
function edify_header_social_profiles() {
    $edify_topbar_fb_url = get_theme_mod( 'header_facebook_link', __( '#', 'edify' ) );
    $edify_topbar_twitter_url = get_theme_mod( 'header_twitter_link', __( '#', 'edify' ) );
    $edify_topbar_instagram_url = get_theme_mod( 'header_instagram_link', __( '#', 'edify' ) );
    $edify_topbar_linkedin_url = get_theme_mod( 'header_linkedin_link', __( '#', 'edify' ) );
    $edify_topbar_youtube_url = get_theme_mod( 'header_youtube_link', __( '#', 'edify' ) );
    ?>

	
<?php if ( !empty( $edify_topbar_fb_url ) ): ?>
<a target="_blank" href="<?php print esc_url( $edify_topbar_fb_url );?>"><i class="fa-brands fa-facebook-f"></i></a>
<?php endif;?>

<?php if ( !empty( $edify_topbar_twitter_url ) ): ?>
<a target="_blank" href="<?php print esc_url( $edify_topbar_twitter_url );?>"><i class="fa-brands fa-twitter"></i></a>
<?php endif;?>

<?php if ( !empty( $edify_topbar_instagram_url ) ): ?>
<a target="_blank" href="<?php print esc_url( $edify_topbar_instagram_url );?>"><i class="fa-brands fa-instagram"></i></a>
<?php endif;?>

<?php if ( !empty( $edify_topbar_linkedin_url ) ): ?>
<a target="_blank" href="<?php print esc_url( $edify_topbar_linkedin_url );?>"><i class="fab fa-linkedin"></i></a>
<?php endif;?>

<?php if ( !empty( $edify_topbar_youtube_url ) ): ?>
<a target="_blank" href="<?php print esc_url( $edify_topbar_youtube_url );?>"><i class="fab fa-youtube"></i></a>
<?php endif;?>

<?php
}



// blog single social share
function edify_blog_social_share(){

    $finbest_singleblog_social = get_theme_mod( 'edify_social_share_switch', false );

    if(!empty($finbest_singleblog_social)) : ?>

    <span><?php echo esc_html__('Share:','edify'); ?></span>

    <a href="https://www.linkedin.com/shareArticle?url=<?php the_permalink(); ?>" target="_blank">
      <i class="fa-brands fa-linkedin-in"></i>
    </a>
    <a href="https://twitter.com/intent/tweet?url=<?php the_permalink(); ?>&text=<?php the_title(); ?>" target="_blank">
      <i class="fab fa-twitter"></i>
    </a>
    <a href="https://www.facebook.com/sharer/sharer.php?u=<?php the_permalink(); ?>" target="_blank">
      <i class="fab fa-facebook-f"></i>
    </a>

<?php endif ; 

}



// woocommerce mini cart content
add_filter('woocommerce_add_to_cart_fragments', function ($fragments) {
	ob_start();
	?>
	<div class="mini_shopping_cart_box">
		<?php woocommerce_mini_cart(); ?>
	</div>
	<?php $fragments['.mini_shopping_cart_box'] = ob_get_clean();
	return $fragments;
 });
 
 // woocommerce mini cart count icon
 if ( ! function_exists( 'sectox_header_add_to_cart_fragment' ) ) {
	function sectox_header_add_to_cart_fragment( $fragments ) {
		ob_start();
		?>
		<span class="cart__count" id="tp-cart-item">
			<?php echo esc_html( WC()->cart->cart_contents_count ); ?>
		</span>
		<?php
		$fragments['#tp-cart-item'] = ob_get_clean();
 
		return $fragments;
	}
 }
 add_filter( 'woocommerce_add_to_cart_fragments', 'sectox_header_add_to_cart_fragment' );



/**
 * @snippet       Plus Minus Quantity Buttons @ WooCommerce Single Product Page
 */
 
 add_action( 'woocommerce_before_quantity_input_field', 'bbloomer_display_quantity_minus' );
 
 function bbloomer_display_quantity_minus() {
	if ( ! is_product() ) return;
	echo '<button type="button" class="minus" >-</button>';
 }
  
 add_action( 'woocommerce_after_quantity_input_field', 'bbloomer_display_quantity_plus' );
  
 function bbloomer_display_quantity_plus() {
	if ( ! is_product() ) return;
	echo '<button type="button" class="plus" >+</button>';
 }
  
 add_action( 'woocommerce_before_single_product', 'bbloomer_add_cart_quantity_plus_minus' );
  
 function bbloomer_add_cart_quantity_plus_minus() {
	wc_enqueue_js( "
	   $('form.cart').on( 'click', 'button.plus, button.minus', function() {
			 var qty = $( this ).closest( 'form.cart' ).find( '.qty' );
			 var val   = parseFloat(qty.val());
			 var max = parseFloat(qty.attr( 'max' ));
			 var min = parseFloat(qty.attr( 'min' ));
			 var step = parseFloat(qty.attr( 'step' ));
			 if ( $( this ).is( '.plus' ) ) {
				if ( max && ( max <= val ) ) {
				   qty.val( max );
				} else {
				   qty.val( val + step );
				}
			 } else {
				if ( min && ( min >= val ) ) {
				   qty.val( min );
				} else if ( val > 1 ) {
				   qty.val( val - step );
				}
			 }
		  });
	" );
 }
 

 /**
 * Change number of related products output
 */ 
function woo_related_products_limit() {
	global $product;
	  
	  $args['posts_per_page'] = 6;
	  return $args;
  }

  add_filter( 'woocommerce_output_related_products_args', 'jk_related_products_args', 20 );
	function jk_related_products_args( $args ) {
	  $args['posts_per_page'] = 3; // 4 related products
	  $args['columns'] = 4; // arranged in 2 columns
	  return $args;
  }


/**
 * Registers an editor stylesheet for the theme.
 */
function edify_theme_add_editor_styles() {
    add_editor_style( 'style-editor.css' );
}
add_action( 'after_setup_theme', 'edify_theme_add_editor_styles' );


add_filter( 'kirki_telemetry', '__return_false' );



add_filter('loop_shop_columns','edify_loop_shop_columns');
function edify_loop_shop_columns(){
    return 3;
}


remove_action('woocommerce_before_shop_loop_item_title','woocommerce_show_product_loop_sale_flash',10);
remove_filter('woocommerce_after_shop_loop_item_title','woocommerce_template_loop_rating',5);
remove_filter('woocommerce_after_shop_loop_item_title','woocommerce_template_loop_price',10);
remove_filter('woocommerce_after_shop_loop_item','woocommerce_template_loop_add_to_cart',10);

add_action('woocommerce_before_shop_loop_item_title','woocommerce_template_loop_add_to_cart',10);

function simpleshop_product_add_to_cart_text($text){
	return '<i class="ri-shopping-bag-line"></i>';
}
add_filter('woocommerce_product_add_to_cart_text','simpleshop_product_add_to_cart_text');

function edify_before_shop_loop_item(){
	echo '<div class="product-wrap">';
}
add_action('woocommerce_before_shop_loop_item','edify_before_shop_loop_item');

function edify_before_shop_loop_item_title(){
	echo '</div><div class="woocommerce-product-title-wrap">';
}
add_action('woocommerce_before_shop_loop_item_title','edify_before_shop_loop_item_title');

add_action('woocommerce_after_shop_loop_item_title','woocommerce_show_product_loop_sale_flash',11);
add_action('woocommerce_after_shop_loop_item','woocommerce_template_loop_price');


function simpleshop_after_shop_loop_item_title(){
	echo '<a href="#" class="wish-list"><i class="fa fa-heart-o"></i></a></div>';
}
add_action('woocommerce_after_shop_loop_item_title','simpleshop_after_shop_loop_item_title');


add_filter( 'woocommerce_sale_flash', 'add_percentage_to_sale_badge', 20, 3 );

function add_percentage_to_sale_badge( $html, $post, $product ) {

  if( $product->is_type('variable')){
      $percentages = array();

      // Get all variation prices
      $prices = $product->get_variation_prices();

      // Loop through variation prices
      foreach( $prices['price'] as $key => $price ){
          // Only on sale variations
          if( $prices['regular_price'][$key] !== $price ){
              // Calculate and set in the array the percentage for each variation on sale
              $percentages[] = round( 100 - ( floatval($prices['sale_price'][$key]) / floatval($prices['regular_price'][$key]) * 100 ) );
          }
      }
      // We keep the highest value
      $percentage = max($percentages) . '%';

  } elseif( $product->is_type('grouped') ){
      $percentages = array();

      // Get all variation prices
      $children_ids = $product->get_children();

      // Loop through variation prices
      foreach( $children_ids as $child_id ){
          $child_product = wc_get_product($child_id);

          $regular_price = (float) $child_product->get_regular_price();
          $sale_price    = (float) $child_product->get_sale_price();

          if ( $sale_price != 0 || ! empty($sale_price) ) {
              // Calculate and set in the array the percentage for each child on sale
              $percentages[] = round(100 - ($sale_price / $regular_price * 100));
          }
      }
      // We keep the highest value
      $percentage = max($percentages) . '%';

  } else {
      $regular_price = (float) $product->get_regular_price();
      $sale_price    = (float) $product->get_sale_price();

      if ( $sale_price != 0 || ! empty($sale_price) ) {
          $percentage    = round(100 - ($sale_price / $regular_price * 100)) . '%';
      } else {
          return $html;
      }
  }
  return '<span class="onsale">' . esc_html__( '-', 'edify' ) . ' ' . $percentage . '</span>';
}


	/**
 * Add 'woocommerce-active' class to the body tag.
 *
 * @param  array $classes CSS classes applied to the body tag.
 * @return array $classes modified to include 'woocommerce-active' class.
 */
function edify_woocommerce_active_body_class( $classes ) {
	$classes[] = 'woocommerce-active';

	return $classes;
}
add_filter( 'body_class', 'edify_woocommerce_active_body_class' );



//  remove woocommerce sidebar from cart checkout

function remove_sidebar()
{
	if( is_account()|| is_checkout() || is_cart() || is_product()) { 
	 remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10);
   }
}
add_action('woocommerce_before_main_content', 'remove_sidebar' );
