<?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' );


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' );

// WP kses allowed tags
// ----------------------------------------------------------------------------------------
function edify_kses($raw)
{

    $allowed_tags = array(
        'a'                         => array(
            'class'   => array(),
            'href'    => array(),
            'rel'  => array(),
            'title'   => array(),
            'target' => array(),
        ),
        'abbr'                      => array(
            'title' => array(),
        ),
        'b'                         => array(),
        'blockquote'                => array(
            'cite' => array(),
        ),
        'cite'                      => array(
            'title' => array(),
        ),
        'code'                      => array(),
        'del'                    => array(
            'datetime'   => array(),
            'title'      => array(),
        ),
        'dd'                     => array(),
        'div'                    => array(
            'class'   => array(),
            'title'   => array(),
            'style'   => array(),
        ),
        'dl'                     => array(),
        'dt'                     => array(),
        'em'                     => array(),
        'h1'                     => array(),
        'h2'                     => array(),
        'h3'                     => array(),
        'h4'                     => array(),
        'h5'                     => array(),
        'h6'                     => array(),
        'i'                         => array(
            'class' => array(),
        ),
        'img'                    => array(
            'alt'  => array(),
            'class'   => array(),
            'height' => array(),
            'src'  => array(),
            'width'   => array(),
        ),
        'li'                     => array(
            'class' => array(),
        ),
        'ol'                     => array(
            'class' => array(),
        ),
        'p'                         => array(
            'class' => array(),
        ),
        'q'                         => array(
            'cite'    => array(),
            'title'   => array(),
        ),
        'span'                      => array(
            'class'   => array(),
            'title'   => array(),
            'style'   => array(),
        ),
        'iframe'                 => array(
            'width'         => array(),
            'height'     => array(),
            'scrolling'     => array(),
            'frameborder'   => array(),
            'allow'         => array(),
            'src'        => array(),
        ),
        'strike'                 => array(),
        'br'                     => array(),
        'strong'                 => array(),
        'data-wow-duration'            => array(),
        'data-wow-delay'            => array(),
        'data-wallpaper-options'       => array(),
        'data-stellar-background-ratio'   => array(),
        'ul'                     => array(
            'class' => array(),
        ),
        'svg' => array(
            'class' => true,
            'aria-hidden' => true,
            'aria-labelledby' => true,
            'role' => true,
            'xmlns' => true,
            'width' => true,
            'height' => true,
            'viewbox' => true, // <= Must be lower case!
        ),
        'g'     => array('fill' => true),
        'title' => array('title' => true),
        'path'  => array('d' => true, 'fill' => true,),
    );

    if (function_exists('wp_kses')) { // WP is here
        $allowed = wp_kses($raw, $allowed_tags);
    } else {
        $allowed = $raw;
    }

    return $allowed;
}