<?php
add_theme_support( 'menus' ); 
  add_theme_support( 'post-thumbnails' ); 
  add_theme_support( 'title-tag' ); 
  add_theme_support( 'woocommerce' ); 

  register_nav_menus( array(
    '1' => __('header-menu','escolha-saude')
  ));
    // Adding custom image sizes (name, width, height, crop)
    add_image_size( 'grande', 900, 9999);
    add_image_size( 'pequeno', 400, 9999);
    add_image_size( 'miniatura', 200, 9999);


add_filter( 'image_size_names_choose', 'custom_image_sizes_choose' );
function custom_image_sizes_choose( $sizes ) {
    $custom_sizes = array(
        'grande' => 'Grande',
        'pequeno' => 'Pequeno',
        'miniatura' => 'Miniatura'
    );
    return array_merge( $sizes, $custom_sizes );
}

// add_filter( 'queryloop_visual_attributes_picker', 'custom_ql_output' );
// function custom_ql_output($a) {
//   if(empty($text) || !isset($text)){
//     $text = $this->get_attribute_name( $attribute_attr, $term_raw );
//   }
//     return $a;
// }

/**
 * @snippet       Disable Variable Product Price Range
 * @how-to        Watch tutorial @ http://businessbloomer.com/?p=19055
 * @sourcecode    http://businessbloomer.com/disable-variable-product-price-range-woocommerce/
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 2.4.7
 */
 
add_filter( 'woocommerce_variable_sale_price_html', 'bbloomer_variation_price_format', 10, 2 );
 
add_filter( 'woocommerce_variable_price_html', 'bbloomer_variation_price_format', 10, 2 );
 
function bbloomer_variation_price_format( $price, $product ) {
 
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'A partir de: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
 
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'A partir de: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
 
if ( $price !== $saleprice ) {
$price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
}
return $price;
}


/**
 * Display the post content. Optinally allows post ID to be passed
 * @uses the_content()
 *
 * @param int $id Optional. Post ID.
 * @param string $more_link_text Optional. Content for when there is more text.
 * @param bool $stripteaser Optional. Strip teaser content before the more text. Default is false.
 */
function sh_the_content_by_id( $post_id=0, $more_link_text = null, $stripteaser = false ){
    global $post;
    $post = &get_post($post_id);
    setup_postdata( $post, $more_link_text, $stripteaser );
    the_content();
    wp_reset_postdata( $post );
}





























































function sidebar_widget_area() {

  if ( function_exists( 'register_sidebar' ) ) {
      register_sidebar(array(
        'name' => 'Sidebar Widgets',
        'id'   => 'sidebar-widgets',
        'description'   => 'These are widgets for the sidebar.',
        'before_widget' => '<div id="%1$s" class="widget col-sm-6 col-md-12 %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h4>',
        'after_title'   => '</h4>'
      ));
    }

}
add_action( 'widgets_init', 'sidebar_widget_area' );

remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );
add_filter( 'woocommerce_breadcrumb_defaults', 'jk_woocommerce_breadcrumbs' );
function jk_woocommerce_breadcrumbs() {
    return array(
            'delimiter'   => '<span class="breadcrumbs-sep"></span>',
            'wrap_before' => '<nav class="woocommerce-breadcrumb breadcrumbs" itemprop="breadcrumb">',
            'wrap_after'  => '</nav>',
            'before'      => '<span>',
            'after'       => '</span>',
            'home'        => _x( 'Home', 'breadcrumb', 'woocommerce' ),
        );
}
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20, 0 );
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30, 0 );
remove_action( 'woocommerce_after_shop_loop_item_title','woocommerce_template_loop_rating',5);
remove_action( 'woocommerce_after_shop_loop_item','woocommerce_template_loop_add_to_cart',10);

function woocommerce_template_loop_product_link_close_enter_button(){
  echo '<div class="button enter anim-fast">Ver mais</div>';
}
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_product_link_close_enter_button', 60 );

remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );

function woocommerce_template_loop_product_thumbnail_lazy(){
  global $post;

  if ( has_post_thumbnail() ) {
    $thumb_id = get_post_thumbnail_id($post->ID);
    $thumb_url = wp_get_attachment_image_src($thumb_id,'pequeno', true);
    ?>
    <div class="shop-archive-image anim-fast">
      <div class="image-wrapper anim-medium">
        <div class="aligner"></div>
        <img src="" data-src="<?php echo $thumb_url[0]; ?>" class="align-middle anim-medium lazy">
      </div>
      <div class="spinner anim-fast">
          <div class="bounce1"></div>
          <div class="bounce2"></div>
          <div class="bounce3"></div>
        </div>
    </div>
    <?php
  } elseif ( wc_placeholder_img_src() ) {
    echo wc_placeholder_img( 'shop_catalog' );
  }

}
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail_lazy', 10 );



remove_action( 'woocommerce_sidebar','woocommerce_get_sidebar',10);
function woocommerce_get_sidebar_aba() {
  if(!is_product()){
    wc_get_template( 'global/sidebar.php' );
  }
}
add_action( 'woocommerce_sidebar','woocommerce_get_sidebar_aba',10);

// Dequeue prettyPhoto 
function prettyphoto_dequeue_script() {
  wp_dequeue_script('prettyPhoto');
  wp_dequeue_script('prettyPhoto-init');
}
add_action('wp_print_scripts', 'prettyphoto_dequeue_script', 100);

remove_action( 'woocommerce_single_product_summary','woocommerce_template_single_title',5);
remove_action( 'woocommerce_single_product_summary','woocommerce_template_single_rating',10);
remove_action( 'woocommerce_single_product_summary','woocommerce_template_single_add_to_cart',30);
remove_action( 'woocommerce_single_product_summary','woocommerce_template_single_excerpt',20);
remove_action( 'woocommerce_single_product_summary','woocommerce_template_single_meta',40);
add_action( 'woocommerce_single_product_summary','woocommerce_template_single_add_to_cart',15);

// remove_action('woocommerce_after_single_product_summary','woocommerce_output_product_data_tabs',10);

//remove_action('woocommerce_after_single_product_summary','woocommerce_upsell_display',15);
remove_action('woocommerce_after_single_product_summary','woocommerce_output_related_products',20);

 // add_action('woocommerce_after_single_product_summary','woocommerce_product_description_tab',25);
 // add_action('woocommerce_after_single_product_summary','woocommerce_product_additional_information_tab',30);


add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );
remove_action( 'woocommerce_shortcode_before_product_cat_loop', 'wc_print_notices', 10 );
remove_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 );
remove_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );
add_action( 'aba_show_message', 'wc_print_notices');
add_action( 'aba_show_message_mobile', 'wc_print_notices', 0);

function woo_custom_cart_button_text() {
 
        return __( 'Adicionar ao carrinho', 'woocommerce' );
 
}


add_filter( 'woocommerce_product_tabs', 'wcs_woo_remove_reviews_tab', 98 );
function wcs_woo_remove_reviews_tab($tabs) {
 unset($tabs['reviews']);
 return $tabs;
}

function wc_dropdown_variation_attribute_options_aba( $args = array() ) {
    $args = wp_parse_args( apply_filters( 'woocommerce_dropdown_variation_attribute_options_args', $args ), array(
      'options'          => false,
      'attribute'        => false,
      'product'          => false,
      'selected'         => false,
      'name'             => '',
      'id'               => '',
      'class'            => '',
      'show_option_none' => __( 'Choose an option', 'woocommerce' )
    ) );

    $options   = $args['options'];
    $product   = $args['product'];
    $attribute = $args['attribute'];
    $name      = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
    $id        = $args['id'] ? $args['id'] : sanitize_title( $attribute );
    $class     = $args['class'];

    if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
      $attributes = $product->get_variation_attributes();
      $options    = $attributes[ $attribute ];
    }
    $html = '<div data-id="' . esc_attr( $id ) . '" class="aba-select-attribute attribute_' . esc_attr( $id ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '">';

    // if ( $args['show_option_none'] ) {
    //   $html .= '<div class="attribute-choice" value="">' . esc_html( $args['show_option_none'] ) . '</div>';
    // }

    if( ! empty( $options ) ){
      if ( $product && taxonomy_exists( $attribute ) ) {
        // Get terms if this is a taxonomy - ordered. We need the names too.
        $terms = wc_get_product_terms( $product->id, $attribute, array( 'fields' => 'all' ) );

        foreach ( $terms as $term ) {
          if ( in_array( $term->slug, $options ) ) {
            $html .= '<div class="attribute-choice" data-value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>';
            if(esc_attr( $id ) == "pa_cor"){
              $html .= '<div class="cor-box" style="background-color:' . get_field('color_hex','pa_cor_' . $term->term_id) . ';"></div>';
            }elseif(esc_attr( $id ) == "pa_modelo"){
              $html .= '<div class="icon" style="background-image:url(' . get_field('icon_modelo','pa_modelo_' . $term->term_id) . ');"></div>';
            }
            $html .= '<div class="choice-name">' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) ) . '</div>';
            if(esc_attr( $id ) == "pa_modelo"){
              $html .= '<div class="radio-button"><div class="radio-button-inner"></div></div>';
            }
            $html .='</div>';
          }
        }
      }else{
        foreach ( $options as $option ) {
          // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
          $selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
          $html .= '<div class="attribute-choice" data-value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</div>';
        }
      }
    }

    $html .= '</div>';

    echo apply_filters( 'woocommerce_dropdown_variation_attribute_options_html', $html, $args );
  }

function show_page_content(){
  if(get_field('show_info_content')){
    $info_pages = get_field('pag_info_single_prod');
    ?>
    <div class="panel entry-content wc-tab" id="tab-page-<?php echo esc_attr( $info_page_id ); ?>">
      <h2>INFORMAÇÕES</h2>
      <?php
      $counter = 1;
      $colors = ['#38B7D4','#71D9F1','#52C4DE','#2DA7C3','#52C7E2'];
      $colorscounter = 0;
      foreach ($info_pages as $info_page_id) {
        $info_page_title = get_the_title($info_page_id);
        ?>

          <div class="info-block <?php echo $counter % 2 == 0 ? "no-margin":""; ?>" style="background-color:<?php echo $colors[$colorscounter]; ?>">
            <div class="aligner"></div>
            <div class="align-middle">
              <div class="info-page-title">
                <?php echo $info_page_title; ?>
              </div>
              <?php
              if(get_field("info_opt",$info_page_id) == "img_info"){
                ?>
                <a href="<?php echo get_field("info_img_c",$info_page_id); ?>" data-rel="lightbox">
                  <div class="button white-border anim-fast">Saber mais</div>
                </a>
                <?php
              }else{
                ?>
                <a href="<?php the_permalink($info_page_id); ?>" target="_blank">
                  <div class="button white-border anim-fast">Saber mais</div>
                </a>
                <?php
              }
              ?>
            </div>
          </div>
          
        <?php
        if($colorscounter == 4){
          $colorscounter = 1;
        }else{
          $colorscounter++;
        }
        $counter++;
      }
      ?>
    </div>
    <?php
  }
  if(get_field('show_brand_content')){
    $brand_pages = get_field('pag_brand_single_prod');
    foreach ($brand_pages as $brand_page_id) {
      $brand_page_title = get_the_title($brand_page_id);
      $content_post = get_post($info_page_id);
      $content = $content_post->post_content;
      $content = apply_filters('the_content', $content);
      $content = str_replace(']]>', ']]&gt;', $content);
      ?>
      <div class="panel entry-content wc-tab" id="tab-page-<?php echo esc_attr( $brand_page_id ); ?>">
        <h2><?php echo $brand_page_title; ?></h2>
        <div class="text">
        <?php sh_the_content_by_id($brand_page_id); ?>
        </div>
      </div>
      <?php
    }
  }
}

add_action('woocommerce_aba_after_tabs','show_page_content');




function mobile_catandsearch(){
  ?>
  <div class="cat-search-mobile">
    <div class="show-cat-list button">Ver por categorias</div>
    <?php
    get_product_search_form();
    ?>
  </div>
  <?php
}
add_action( 'woocommerce_archive_description','mobile_catandsearch');


/**
 * Hides the product's weight and dimension in teh single product page.
 */
add_filter( 'wc_product_enable_dimensions_display', '__return_false' );



/**
 * WooCommerce Extra Feature
 * --------------------------
 *
 * Change number of related products on product page
 * Set your own value for 'posts_per_page'
 *
 */ 
function woo_related_products_limit() {
  global $product;
  
  $args['posts_per_page'] = 3;
  return $args;
}
add_filter( 'woocommerce_output_related_products_args', 'jk_related_products_args' );
  function jk_related_products_args( $args ) {
  $args['posts_per_page'] = 3; // 4 related products
  $args['columns'] = 3; // arranged in 2 columns
  return $args;
}


function skyverge_change_attribute_list_display( $output, $attributes, $values ) {
 
    return wpautop( wptexturize( implode( ';<br />', $values ) ) );
}
add_filter( 'woocommerce_attribute', 'skyverge_change_attribute_list_display', 10, 3 );



define( 'WC_MAX_LINKED_VARIATIONS', 249 );




























































function custom_excerpt_length( $length ) {
  return 15;
}
function new_excerpt_more( $more ) {
  return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
// retrieves the attachment ID from the file URL
function pippin_get_image_id($image_url) {
	global $wpdb;
	$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url )); 
        return $attachment[0]; 
}



function botao_func( $atts, $content = "" ){
  // Attributes
  extract( 
    shortcode_atts(
      array(
        'imagem' => 'none',
        'colunas' => '1'
      ), 
      $atts
    )
  );




  $output = "<div class='button anim-fast'>".$content."</div>";


  return $output;

}
function content($limit) {
  $content = explode(' ', get_the_content(), $limit);
  if (count($content)>=$limit) {
    array_pop($content);
    $content = implode(" ",$content).'...';
  } else {
    $content = implode(" ",$content);
  } 
  $content = preg_replace('/\[.+\]/','', $content);
  $content = apply_filters('the_content', $content); 
  $content = str_replace(']]>', ']]&gt;', $content);
  return $content;
}
function register_shortcodes(){
  add_shortcode( 'botao', 'botao_func' );
}
add_action( 'init', 'register_shortcodes');
?>