<?php
/* Woocommerce extensions
------------------------------------------------------------------------------- */

// Theme init priorities:
// 9 - register other filters (for installer, etc.)
if ( ! function_exists( 'law_office_woocommerce_extensions_theme_setup9' ) ) {
	add_action( 'after_setup_theme', 'law_office_woocommerce_extensions_theme_setup9', 9 );
	function law_office_woocommerce_extensions_theme_setup9() {
		if ( law_office_exists_woocommerce() ) {
			// Frontend styles and scripts
			add_action( 'wp_enqueue_scripts', 'law_office_woocommerce_extensions_frontend_scripts', 1100 );
			add_action( 'trx_addons_action_load_scripts_front_woocommerce_extensions', 'law_office_woocommerce_extensions_frontend_scripts', 10, 1 );
			add_action( 'wp_enqueue_scripts', 'law_office_woocommerce_extensions_frontend_scripts_responsive', 2000 );
			add_action( 'trx_addons_action_load_scripts_front_woocommerce_extensions', 'law_office_woocommerce_extensions_frontend_scripts_responsive', 10, 1 );
			add_filter( 'law_office_filter_merge_styles', 'law_office_woocommerce_extensions_merge_styles' );
			add_filter( 'law_office_filter_merge_styles_responsive', 'law_office_woocommerce_extensions_merge_styles_responsive' );
			add_filter( 'law_office_filter_merge_scripts', 'law_office_woocommerce_extensions_merge_scripts' );
		}
	}
}

// Enqueue WooCommerce extensions styles
if ( ! function_exists( 'law_office_woocommerce_extensions_frontend_scripts' ) ) {
	//Handler of the add_action( 'wp_enqueue_scripts', 'law_office_woocommerce_extensions_frontend_scripts', 1100 );
	//Handler of the add_action( 'trx_addons_action_load_scripts_front_woocommerce_extensions', 'law_office_woocommerce_extensions_frontend_scripts', 10, 1 );
	function law_office_woocommerce_extensions_frontend_scripts( $force = false ) {
		static $loaded = false;
		if ( ! $loaded && (
			current_action() == 'wp_enqueue_scripts' && law_office_need_frontend_scripts( 'woocommerce' )
			||
			current_action() != 'wp_enqueue_scripts' && $force === true
			)
		) {
			$loaded = true;
			$law_office_url = law_office_get_file_url( 'plugins/woocommerce/woocommerce-extensions.css' );
			if ( '' != $law_office_url ) {
				wp_enqueue_style( 'law-office-woocommerce-extensions', $law_office_url, array(), null );
			}
			$law_office_url = law_office_get_file_url( 'plugins/woocommerce/woocommerce-extensions.js' );
			if ( '' != $law_office_url ) {
				wp_enqueue_script( 'law-office-woocommerce-extensions', $law_office_url, array( 'jquery' ), null, true );
			}
		}
	}
}

// Enqueue WooCommerce extensions responsive styles
if ( ! function_exists( 'law_office_woocommerce_extensions_frontend_scripts_responsive' ) ) {
	//Handler of the add_action( 'wp_enqueue_scripts', 'law_office_woocommerce_extensions_frontend_scripts_responsive', 2000 );
	//Handler of the add_action( 'trx_addons_action_load_scripts_front_woocommerce_extensions', 'law_office_woocommerce_extensions_frontend_scripts_responsive', 10, 1 );
	function law_office_woocommerce_extensions_frontend_scripts_responsive( $force = false ) {
		static $loaded = false;
		if ( ! $loaded && (
			current_action() == 'wp_enqueue_scripts' && law_office_need_frontend_scripts( 'woocommerce' )
			||
			current_action() != 'wp_enqueue_scripts' && $force === true
			)
		) {
			$loaded = true;
			$law_office_url = law_office_get_file_url( 'plugins/woocommerce/woocommerce-extensions-responsive.css' );
			if ( '' != $law_office_url ) {
				wp_enqueue_style( 'law-office-woocommerce-extensions-responsive', $law_office_url, array(), null, law_office_media_for_load_css_responsive( 'woocommerce-extensions' ) );
			}
		}
	}
}

// Merge custom styles
if ( ! function_exists( 'law_office_woocommerce_extensions_merge_styles' ) ) {
	//Handler of the add_filter('law_office_filter_merge_styles', 'law_office_woocommerce_extensions_merge_styles');
	function law_office_woocommerce_extensions_merge_styles( $list ) {
		$list[ 'plugins/woocommerce/woocommerce-extensions.css' ] = false;
		return $list;
	}
}

// Merge responsive styles
if ( ! function_exists( 'law_office_woocommerce_extensions_merge_styles_responsive' ) ) {
	//Handler of the add_filter('law_office_filter_merge_styles_responsive', 'law_office_woocommerce_extensions_merge_styles_responsive');
	function law_office_woocommerce_extensions_merge_styles_responsive( $list ) {
		$list[ 'plugins/woocommerce/woocommerce-extensions-responsive.css' ] = false;
		return $list;
	}
}

// Merge custom scripts
if ( ! function_exists( 'law_office_woocommerce_extensions_merge_scripts' ) ) {
	//Handler of the add_filter('law_office_filter_merge_scripts', 'law_office_woocommerce_extensions_merge_scripts');
	function law_office_woocommerce_extensions_merge_scripts( $list ) {
		$list[ 'plugins/woocommerce/woocommerce-extensions.js' ] = false;
		return $list;
	}
}



/* Common functions
------------------------------------------------------------------- */

// Check if taxomony is a Woocommerce product's attribute
if ( ! function_exists( 'law_office_woocommerce_extensions_attrib_get_type' ) ) {
	function law_office_woocommerce_extensions_attrib_get_type( $taxonomy ) {
		$type = '';
		if ( law_office_exists_woocommerce() ) {
			$attribute_taxonomies = wc_get_attribute_taxonomies();
			if ( ! empty( $attribute_taxonomies ) ) {
				foreach ( $attribute_taxonomies as $attribute ) {
					if ( wc_attribute_taxonomy_name( $attribute->attribute_name ) == $taxonomy ) {
						$type = $attribute->attribute_type;
						break;
					}
				}
			}
		}
		return $type;
	}
}



/* Move breadcrumbs above title
------------------------------------------------------------------- */
if ( ! function_exists( 'law_office_woocommerce_extensions_move_breadcrumbs_before_title' ) ) {
	add_action( 'init', 'law_office_woocommerce_extensions_move_breadcrumbs_before_title' );
	function law_office_woocommerce_extensions_move_breadcrumbs_before_title() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'move_breadcrumbs' )
		) {
			remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );
			add_action( 'woocommerce_single_product_summary', 'woocommerce_breadcrumb', 3 );	// Priority 5 used for the title
		}
	}
}



/* Add parameter 'Single Product Gallery Style'
------------------------------------------------------------------- */

// Theme init priorities:
// 3 - add/remove Theme Options elements
if ( ! function_exists( 'law_office_woocommerce_extensions_product_gallery_style_theme_setup3' ) ) {
	add_action( 'after_setup_theme', 'law_office_woocommerce_extensions_product_gallery_style_theme_setup3', 3 );
	function law_office_woocommerce_extensions_product_gallery_style_theme_setup3() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'gallery_style' )
		) {
			// Add 'single_product_gallery_style' to the theme-specific options
			law_office_storage_set_array_before( 'options', 'single_product_gallery_thumbs', apply_filters( 'law_office_filter_woocommerce_extensions_gallery_style_args', array(
				'single_product_gallery_style'  => array(
					'title'      => esc_html__( 'Gallery style', 'law-office' ),
					'desc'       => wp_kses_data( __( 'Select the style of the gallery on the single product page.', 'law-office' ) ),
					'override'   => array(
						'mode'    => 'product',
						'section' => esc_html__( 'Content', 'law-office' ),
					),
					'options'    => apply_filters(
													'law_office_filter_woocommerce_single_product_gallery_style',
													array(
														'default'   => esc_html__( 'Default', 'law-office' ),
														'fullwidth' => esc_html__( 'Fullwidth', 'law-office' ),
														'cascade'   => esc_html__( 'Cascade', 'law-office' ),
														'grid'      => esc_html__( 'Grid', 'law-office' ),
													)
												),
					'std'        => 'default',
					'type'       => 'select',
				),
			) ) );

			// Add condition to the 'single_product_gallery_thumbs'
			law_office_storage_set_array2( 'options', 'single_product_gallery_thumbs', 'dependency', array(
				'single_product_gallery_style' => array( 'default', 'fullwidth' ),
			) );

		}
	}
}

// Disable slider and zoom for 'fullwidth' (only zoom), 'cascade' and 'grid'
if ( ! function_exists( 'law_office_woocommerce_extensions_product_gallery_style_theme_support' ) ) {
	add_action( 'wp_enqueue_scripts', 'law_office_woocommerce_extensions_product_gallery_style_theme_support', 9 );
	function law_office_woocommerce_extensions_product_gallery_style_theme_support() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'gallery_style' )
		) {
			$style = law_office_get_theme_option( 'single_product_gallery_style' );
			// Disable 'zoom'
			if ( in_array( $style, array( 'fullwidth', 'cascade', 'grid' ) ) ) {
				remove_theme_support( 'wc-product-gallery-zoom' );
			}
			// Disable 'slider'
			if ( in_array( $style, array( 'cascade', 'grid' ) ) ) {
				remove_theme_support( 'wc-product-gallery-slider' );
			}
			// Load 'masonry' script
			if ( 'grid' == $style && is_product() ) {
				law_office_load_masonry_scripts();
			}
		}
	}
}

// Add plugin-specific classes to the product
if ( ! function_exists( 'law_office_woocommerce_extensions_product_gallery_style_add_wrapper_classes' ) ) {
	add_filter( 'law_office_filter_single_product_wrapper_class', 'law_office_woocommerce_extensions_product_gallery_style_add_wrapper_classes' );
	function law_office_woocommerce_extensions_product_gallery_style_add_wrapper_classes( $classes ) {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'gallery_style' )
			&& is_product()
		) {
			$classes[] = 'single_product_gallery_style_' . esc_attr( law_office_get_theme_option( 'single_product_gallery_style' ) );
		}
		return $classes;
	}
}

// Increase thumbnail size for some gallery's styles
if ( ! function_exists( 'law_office_woocommerce_extensions_product_gallery_style_thumbnail_size' ) ) {
	add_filter( 'woocommerce_gallery_thumbnail_size', 'law_office_woocommerce_extensions_product_gallery_style_thumbnail_size' );
	function law_office_woocommerce_extensions_product_gallery_style_thumbnail_size( $sizes = array() ) {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'gallery_style' ) ) {
			$style = law_office_get_theme_option( 'single_product_gallery_style' );
			if ( in_array( $style, array( 'cascade', 'grid' ) ) ) {
				$gallery_thumbnail = wc_get_image_size( 'woocommerce_single' );
				$sizes = array( (int) $gallery_thumbnail['width'], (int) $gallery_thumbnail['height'] );
			}
		}
		return $sizes;
	}
}

// Increase main image size for some gallery's styles
if ( ! function_exists( 'law_office_woocommerce_extensions_product_gallery_style_image_size' ) ) {
	add_filter( 'woocommerce_gallery_image_size', 'law_office_woocommerce_extensions_product_gallery_style_image_size' );
	function law_office_woocommerce_extensions_product_gallery_style_image_size( $sizes = array() ) {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'gallery_style' ) ) {
			$style = law_office_get_theme_option( 'single_product_gallery_style' );
			if ( $style == 'fullwidth' ) {
				$sizes = 'full';
			}
		}
		return $sizes;
	}
}


// Disable slider for some gallery's styles
if ( ! function_exists( 'law_office_woocommerce_extensions_product_gallery_style_disable_slider' ) ) {
	add_filter( 'woocommerce_single_product_flexslider_enabled', 'law_office_woocommerce_extensions_product_gallery_style_disable_slider' );
	function law_office_woocommerce_extensions_product_gallery_style_disable_slider( $enabled = true ) {
		return $enabled
					&& ( ! apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'gallery_style' )
						||
						! in_array( law_office_get_theme_option( 'single_product_gallery_style' ), array( 'cascade', 'grid' ) )
						);
	}
}


// Remove 'single_product_gallery_thumbs_left' from the body 
if ( ! function_exists( 'law_office_woocommerce_extensions_product_gallery_style_remove_body_classes' ) ) {
	add_filter( 'body_class', 'law_office_woocommerce_extensions_product_gallery_style_remove_body_classes', 20 );
	function law_office_woocommerce_extensions_product_gallery_style_remove_body_classes( $classes ) {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'gallery_style' ) ) {
			$style = law_office_get_theme_option( 'single_product_gallery_style' );
			if ( in_array( $style, array( 'cascade', 'grid' ) ) ) {
				if ( law_office_exists_woocommerce() && is_product() ) {
					$classes = law_office_array_delete_by_value( $classes, 'single_product_gallery_thumbs_left' );
				}
			}
		}
		return $classes;
	}
}



/* Add parameter 'Single product gallery lightbox on/off'
------------------------------------------------------------------- */

// Theme init priorities:
// 3 - add/remove Theme Options elements
if ( ! function_exists( 'law_office_woocommerce_extensions_product_gallery_lightbox_theme_setup3' ) ) {
	add_action( 'after_setup_theme', 'law_office_woocommerce_extensions_product_gallery_lightbox_theme_setup3', 3 );
	function law_office_woocommerce_extensions_product_gallery_lightbox_theme_setup3() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'gallery_lightbox' )
		) {
			// Add 'single_product_additional_info' to the theme-specific options
			law_office_storage_set_array_after( 'options', 'single_product_gallery_thumbs', apply_filters( 'law_office_filter_woocommerce_extensions_gallery_lightbox_args', array(
				'single_product_gallery_lightbox' => array(
					'title'   => esc_html__( 'Allow lightbox with large image', 'law-office' ),
					'desc'    => wp_kses_data( __( "Allow the lightbox with a large image in the single product page.", 'law-office' ) ),
					'std'     => 1,
					'type'    => 'switch',
				),
			) ) );
		}
	}
}

// Disable image lightbox
if ( ! function_exists( 'law_office_woocommerce_extensions_product_gallery_lightbox_theme_support' ) ) {
	add_action( 'wp_enqueue_scripts', 'law_office_woocommerce_extensions_product_gallery_lightbox_theme_support', 9 );
	function law_office_woocommerce_extensions_product_gallery_lightbox_theme_support() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'gallery_lightbox' )
			&& is_product()
		) {
			if ( (int) law_office_get_theme_option( 'single_product_gallery_lightbox' ) == 0 ) {
				remove_theme_support( 'wc-product-gallery-lightbox' );
			}
		}
	}
}

// Remove link from gallery image layout if lightbox is disabled
if ( ! function_exists( 'law_office_woocommerce_extensions_product_gallery_lightbox_remove_links' ) ) {
	add_filter( 'woocommerce_single_product_image_thumbnail_html', 'law_office_woocommerce_extensions_product_gallery_lightbox_remove_links', 9 );
	function law_office_woocommerce_extensions_product_gallery_lightbox_remove_links( $html = '', $image_id = 0 ) {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'gallery_lightbox' ) ) {
			if ( (int) law_office_get_theme_option( 'single_product_gallery_lightbox' ) == 0 && strpos( $html, '<a ' ) !== false ) {
				$html = preg_replace( '/(.*)(<a [^>]*>)(.*)(<\/a>)(.*)/', '$1$3$5', $html );
			}
		}
		return $html;
	}
}



/* Add parameter 'Single product gallery zoom on/off'
------------------------------------------------------------------- */

// Theme init priorities:
// 3 - add/remove Theme Options elements
if ( ! function_exists( 'law_office_woocommerce_extensions_product_gallery_zoom_theme_setup3' ) ) {
	add_action( 'after_setup_theme', 'law_office_woocommerce_extensions_product_gallery_zoom_theme_setup3', 3 );
	function law_office_woocommerce_extensions_product_gallery_zoom_theme_setup3() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'gallery_zoom' )
		) {
			// Add 'single_product_additional_info' to the theme-specific options
			law_office_storage_set_array_after( 'options', 'single_product_gallery_thumbs', apply_filters( 'law_office_filter_woocommerce_extensions_gallery_zoom_args', array(
				'single_product_gallery_zoom' => array(
					'title'   => esc_html__( 'Allow zoom on the large image', 'law-office' ),
					'desc'    => wp_kses_data( __( "Allow the zoom on the large image in the single product page.", 'law-office' ) ),
					'std'     => 1,
					'type'    => 'switch',
				),
			) ) );
		}
	}
}

// Disable image zoom
if ( ! function_exists( 'law_office_woocommerce_extensions_product_gallery_zoom_theme_support' ) ) {
	add_action( 'wp_enqueue_scripts', 'law_office_woocommerce_extensions_product_gallery_zoom_theme_support', 9 );
	function law_office_woocommerce_extensions_product_gallery_zoom_theme_support() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'gallery_zoom' )
			&& is_product()
		) {
			if ( (int) law_office_get_theme_option( 'single_product_gallery_zoom' ) == 0 ) {
				remove_theme_support( 'wc-product-gallery-zoom' );
			}
		}
	}
}



/* Add parameter 'Single Product Details Style'
------------------------------------------------------------------- */

// Theme init priorities:
// 3 - add/remove Theme Options elements
if ( ! function_exists( 'law_office_woocommerce_extensions_product_details_style_theme_setup3' ) ) {
	add_action( 'after_setup_theme', 'law_office_woocommerce_extensions_product_details_style_theme_setup3', 3 );
	function law_office_woocommerce_extensions_product_details_style_theme_setup3() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'details_style' )
		) {
			// Add 'single_product_details_style' to the theme-specific options
			law_office_storage_set_array_before( 'options', 'show_related_posts_shop', apply_filters( 'law_office_filter_woocommerce_extensions_details_style_args', array(
				'single_product_details_style'   => array(
					'title'      => esc_html__( 'Details style', 'law-office' ),
					'desc'       => wp_kses_data( __( 'Select the style of the section "Details" on the single product page.', 'law-office' ) ),
					'override'   => array(
						'mode'    => 'product',
						'section' => esc_html__( 'Content', 'law-office' ),
					),
					'options'    => apply_filters(
													'law_office_filter_woocommerce_single_product_details_styles',
													array(
														'default'   => esc_html__( 'Default (Tabs)', 'law-office' ),
														'stacked'   => esc_html__( 'Stacked', 'law-office' ),
														'accordion' => esc_html__( 'Accordion', 'law-office' ),
													)
												),
					'std'        => 'default',
					'type'       => 'select',
				),
			) ) );
		}
	}
}

// Add plugin-specific classes to the product
if ( ! function_exists( 'law_office_woocommerce_extensions_product_details_style_add_wrapper_classes' ) ) {
	add_filter( 'law_office_filter_single_product_wrapper_class', 'law_office_woocommerce_extensions_product_details_style_add_wrapper_classes' );
	function law_office_woocommerce_extensions_product_details_style_add_wrapper_classes( $classes ) {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'details_style' )
			&& is_product()
		) {
			$classes[] = 'single_product_details_style_' . esc_attr( law_office_get_theme_option( 'single_product_details_style' ) );
		}
		return $classes;
	}
}


// Enqueue accordion script if description style is equal to 'Accordion'
if ( ! function_exists( 'law_office_woocommerce_extensions_product_details_style_accordion_enqueue_script' ) ) {
	add_action( 'wp_enqueue_scripts', 'law_office_woocommerce_extensions_product_details_style_accordion_enqueue_script', 1100 );
	function law_office_woocommerce_extensions_product_details_style_accordion_enqueue_script() {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'details_style' )
			&& law_office_get_theme_option( 'single_product_details_style' ) == 'accordion'
			&& law_office_exists_woocommerce()
			&& is_product()
		) {
			wp_enqueue_script( 'jquery-ui-accordion', false, array( 'jquery', 'jquery-ui-core' ), null, true );
		}
	}
}


// Remove tabs if description style is equal to 'Stacked'
if ( ! function_exists( 'law_office_woocommerce_extensions_product_details_style_stacked_remove_product_tabs' ) ) {
	add_filter( 'woocommerce_product_tabs', 'law_office_woocommerce_extensions_product_details_style_stacked_remove_product_tabs', 1000 );
	function law_office_woocommerce_extensions_product_details_style_stacked_remove_product_tabs( $tabs = array() ) {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'details_style' )
			&& law_office_get_theme_option( 'single_product_details_style' ) == 'stacked'
			&& is_array( $tabs )
		) {
			// Move additional info to top
			if ( isset( $tabs['additional_information'] ) ) {
				$tabs_new = array( 'additional_information' => $tabs['additional_information'] );
				unset( $tabs['additional_information'] );
				$tabs = array_merge( $tabs_new, $tabs );
			}
			// Display blocks
			do_action( 'law_office_action_before_product_stacks', $tabs );
			?><div class="woocommerce-stacks"><?php
				foreach ( $tabs as $k => $tab ) {
					if ( isset( $tab['callback'] ) ) {
						do_action( 'law_office_action_before_product_stack', $k, $tab );
						?><div class="woocommerce-stack woocommerce-stack-<?php echo esc_attr( $k ); ?>"><?php
							call_user_func( $tab['callback'], $k, $tab );
						?></div><?php
						do_action( 'law_office_action_after_product_stack', $k, $tab );
					}
				}
			?></div><?php
			do_action( 'law_office_action_after_product_stacks', $tabs );
			// Remove all tabs to prevent display
			$tabs = array();
		}
		return $tabs;
	}
}


// Remove tabs if description style is equal to 'Accordion'
if ( ! function_exists( 'law_office_woocommerce_extensions_product_details_style_accordion_remove_product_tabs' ) ) {
	add_filter( 'woocommerce_product_tabs', 'law_office_woocommerce_extensions_product_details_style_accordion_remove_product_tabs', 1000 );
	function law_office_woocommerce_extensions_product_details_style_accordion_remove_product_tabs( $tabs = array() ) {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'details_style' )
			&& law_office_get_theme_option( 'single_product_details_style' ) == 'accordion'
			&& is_array( $tabs )
		) {
			// Move additional info to top
			if ( isset( $tabs['additional_information'] ) ) {
				$tabs_new = array( 'additional_information' => $tabs['additional_information'] );
				unset( $tabs['additional_information'] );
				$tabs = array_merge( $tabs_new, $tabs );
			}
			// Display blocks
			do_action( 'law_office_action_before_product_accordion', $tabs );
			?><div class="law_office_accordion woocommerce-accordion"><?php
				foreach ( $tabs as $k => $tab ) {
					if ( isset( $tab['callback'] ) ) {
						do_action( 'law_office_action_before_product_accordion_title', $k, $tab );
						?><h3 class="law_office_accordion_title woocommerce-accordion-title woocommerce-accordion-title-<?php echo esc_attr( $k ); ?>"><?php
							echo esc_html( $tab['title'] );
						?></h3><?php
						do_action( 'law_office_action_after_product_accordion_title', $k, $tab );
						do_action( 'law_office_action_before_product_accordion_content', $k, $tab );
						?><div class="law_office_accordion_content woocommerce-accordion-content woocommerce-accordion-content-<?php echo esc_attr( $k ); ?>"><?php
							call_user_func( $tab['callback'], $k, $tab );
						?></div><?php
						do_action( 'law_office_action_after_product_accordion_content', $k, $tab );
					}
				}
			?></div><?php
			do_action( 'law_office_action_after_product_accordion', $tabs );
			// Remove all tabs to prevent display
			$tabs = array();
		}
		return $tabs;
	}
}


/* Add parameter 'Single Product Details position'
------------------------------------------------------------------- */

// Theme init priorities:
// 3 - add/remove Theme Options elements
if ( ! function_exists( 'law_office_woocommerce_extensions_product_details_position_theme_setup3' ) ) {
	add_action( 'after_setup_theme', 'law_office_woocommerce_extensions_product_details_position_theme_setup3', 3 );
	function law_office_woocommerce_extensions_product_details_position_theme_setup3() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'details_position' )
		) {
			// Add 'single_product_details_position' to the theme-specific options
			law_office_storage_set_array_before( 'options', 'show_related_posts_shop', apply_filters( 'law_office_filter_woocommerce_extensions_details_position_args', array(
				'single_product_details_position' => array(
					'title'   => esc_html__( 'Details position', 'law-office' ),
					'desc'    => wp_kses_data( __( "Select a position of the single product details.", 'law-office' ) ),
					'override'   => array(
						'mode'    => 'product',
						'section' => esc_html__( 'Content', 'law-office' ),
					),
					'options' => apply_filters( 'law_office_filter_woocommerce_extensions_details_position', array(
						'default'       => esc_html__( 'Default', 'law-office' ),
						'under_gallery' => esc_html__( 'Under the gallery', 'law-office' ),
						'under_summary' => esc_html__( 'Under the summary', 'law-office' ),
					) ),
					'std'     => 'default',
					'type'    => 'select',
				),
			) ) );
		}
	}
}

// Add plugin-specific classes to the product
if ( ! function_exists( 'law_office_woocommerce_extensions_product_details_position_add_wrapper_classes' ) ) {
	add_filter( 'law_office_filter_single_product_wrapper_class', 'law_office_woocommerce_extensions_product_details_position_add_wrapper_classes' );
	function law_office_woocommerce_extensions_product_details_position_add_wrapper_classes( $classes ) {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'details_position' )
			&& is_product()
		) {
			$classes[] = 'single_product_details_position_' . esc_attr( law_office_get_theme_option( 'single_product_details_position' ) );
		}
		return $classes;
	}
}



/* Add parameter 'Single Product Summary Sticky'
------------------------------------------------------------------- */

// Theme init priorities:
// 3 - add/remove Theme Options elements
if ( ! function_exists( 'law_office_woocommerce_extensions_product_summary_sticky_theme_setup3' ) ) {
	add_action( 'after_setup_theme', 'law_office_woocommerce_extensions_product_summary_sticky_theme_setup3', 3 );
	function law_office_woocommerce_extensions_product_summary_sticky_theme_setup3() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'product_summary_sticky' )
		) {
			// Add 'single_product_summary_sticky' to the theme-specific options
			law_office_storage_set_array_before( 'options', 'show_related_posts_shop', apply_filters( 'law_office_filter_woocommerce_extensions_summary_sticky_args', array(
				'single_product_summary_sticky' => array(
					'title'      => esc_html__( 'Summary sticky', 'law-office' ),
					'desc'       => wp_kses_data( __( 'Make "Summary" sticky on the single product page.', 'law-office' ) ),
					'override'   => array(
						'mode'    => 'product',
						'section' => esc_html__( 'Content', 'law-office' ),
					),
					'dependency' => array(
						'single_product_details_position' => array( 'under_gallery' ),
					),
					'std'        => 0,
					'type'       => 'switch',
				),
			) ) );
		}
	}
}

// Add plugin-specific classes to the product
if ( ! function_exists( 'law_office_woocommerce_extensions_product_summary_sticky_add_wrapper_classes' ) ) {
	add_filter( 'law_office_filter_single_product_wrapper_class', 'law_office_woocommerce_extensions_product_summary_sticky_add_wrapper_classes' );
	function law_office_woocommerce_extensions_product_summary_sticky_add_wrapper_classes( $classes ) {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'product_summary_sticky' )
			&& law_office_get_theme_option( 'single_product_summary_sticky' ) == 1
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'details_position' )
			&& law_office_get_theme_option( 'single_product_details_position' ) == 'under_gallery'
			&& is_product()
		) {
			$classes[] = 'single_product_summary_sticky';
		}
		return $classes;
	}
}

// Move related products outside the article
if ( ! function_exists( 'law_office_woocommerce_extensions_product_summary_sticky_before_main_content' ) ) {
	add_action( 'woocommerce_before_main_content', 'law_office_woocommerce_extensions_product_summary_sticky_before_main_content' );
	function law_office_woocommerce_extensions_product_summary_sticky_before_main_content() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'product_summary_sticky' )
			&& law_office_get_theme_option( 'single_product_summary_sticky' ) == 1
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'details_position' )
			&& law_office_get_theme_option( 'single_product_details_position' ) == 'under_gallery'
			&& is_product()
		) {
			remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
			add_action( 'woocommerce_after_main_content', 'woocommerce_output_related_products', 20 );
			add_action( 'woocommerce_after_single_product_summary', 'law_office_woocommerce_extensions_product_summary_sticky_add_holder', 20 );
		}
	}
}

// Add holder instead related products inside the article
if ( ! function_exists( 'law_office_woocommerce_extensions_product_summary_sticky_add_holder' ) ) {
	function law_office_woocommerce_extensions_product_summary_sticky_add_holder() {
		?><div class="single_product_summary_sticky_related_holder"></div><?php
	}
}



/* Move 'Additional information' under the product's description
------------------------------------------------------------------- */

// Theme init priorities:
// 3 - add/remove Theme Options elements
if ( ! function_exists( 'law_office_woocommerce_extensions_additional_info_theme_setup3' ) ) {
	add_action( 'after_setup_theme', 'law_office_woocommerce_extensions_additional_info_theme_setup3', 3 );
	function law_office_woocommerce_extensions_additional_info_theme_setup3() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'additional_info' )
		) {
			// Add 'single_product_additional_info' to the theme-specific options
			law_office_storage_set_array_before( 'options', 'show_related_posts_shop', apply_filters( 'law_office_filter_woocommerce_extensions_additional_info_args', array(
				'single_product_additional_info' => array(
					'title'   => esc_html__( 'Show additional info under short description', 'law-office' ),
					'desc'    => wp_kses_data( __( "Make additional product information more prominent by moving it up under the short description.", 'law-office' ) ),
					'std'     => 0,
					'type'    => 'switch',
				),
			) ) );
		}
	}
}

// Remove tab 'Additional information' from the tabs list
if ( ! function_exists( 'law_office_woocommerce_extensions_additional_info_default_product_tabs' ) ) {
	add_filter( 'woocommerce_product_tabs', 'law_office_woocommerce_extensions_additional_info_default_product_tabs', 100 );
	function law_office_woocommerce_extensions_additional_info_default_product_tabs( $tabs = array() ) {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'additional_info' )
			&& (int) law_office_get_theme_option( 'single_product_additional_info' ) == 1
			&& isset( $tabs['additional_information'] )
		) {
			unset( $tabs['additional_information'] );	//woocommerce_product_additional_information_tab
		}
		return $tabs;
	}
}

// Add 'Additional information' above the product's price
if ( ! function_exists( 'law_office_woocommerce_extensions_additional_info_template' ) ) {
	add_action( 'woocommerce_single_product_summary', 'law_office_woocommerce_extensions_additional_info_template', 25 );
	function law_office_woocommerce_extensions_additional_info_template() {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'additional_info' )
			&& (int) law_office_get_theme_option( 'single_product_additional_info' ) == 1
			&& function_exists( 'woocommerce_product_additional_information_tab' )
		) {
			?><div class="woocommerce-product-details__additional-information"><?php
				woocommerce_product_additional_information_tab();
			?></div><?php
		}
	}
}



/* Custom text after price on the single product page
------------------------------------------------------------------- */

// Theme init priorities:
// 3 - add/remove Theme Options elements
if ( ! function_exists( 'law_office_woocommerce_extensions_product_text_after_price_theme_setup3' ) ) {
	add_action( 'after_setup_theme', 'law_office_woocommerce_extensions_product_text_after_price_theme_setup3', 3 );
	function law_office_woocommerce_extensions_product_text_after_price_theme_setup3() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'text_after_price' )
		) {
			// Add 'single_product_text_after_price' to the theme-specific options
			law_office_storage_set_array_before( 'options', 'show_related_posts_shop', apply_filters( 'law_office_filter_woocommerce_extensions_text_after_price_args', array(
				'single_product_text_after_price' => array(
					'title'    => esc_html__( 'Text after price', 'law-office' ),
					'desc'     => wp_kses_data( __( 'Specify custom text to show it after the product price on the single product page.', 'law-office' ) ),
					'std'      => '',
					'type'     => 'text_editor',
				),
			) ) );
		}
	}
}

// Add custom text after "Add to cart" button
// @hooked woocommerce_template_single_price - 10
if ( ! function_exists( 'law_office_woocommerce_extensions_product_text_after_price' ) ) {
	add_action( 'woocommerce_single_product_summary', 'law_office_woocommerce_extensions_product_text_after_price', 10 );
	function law_office_woocommerce_extensions_product_text_after_price() {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'text_after_price' ) ) {
			law_office_show_layout( law_office_get_theme_option( 'single_product_text_after_price' ), '<div class="single_product_custom_text_after_price">', '</div>' );
		}
	}
}



/* Custom text after "Add to cart" on the single product page
------------------------------------------------------------------- */

// Theme init priorities:
// 3 - add/remove Theme Options elements
if ( ! function_exists( 'law_office_woocommerce_extensions_product_text_after_add_to_cart_theme_setup3' ) ) {
	add_action( 'after_setup_theme', 'law_office_woocommerce_extensions_product_text_after_add_to_cart_theme_setup3', 3 );
	function law_office_woocommerce_extensions_product_text_after_add_to_cart_theme_setup3() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'text_after_add_to_cart' )
		) {
			// Add 'single_product_text_after_add_to_cart' to the theme-specific options
			law_office_storage_set_array_before( 'options', 'show_related_posts_shop', apply_filters( 'law_office_filter_woocommerce_extensions_text_after_add_to_cart_args', array(
				'single_product_text_after_add_to_cart' => array(
					'title'    => esc_html__( 'Text after "Add to Cart"', 'law-office' ),
					'desc'     => wp_kses_data( __( 'Specify custom text to show it after "Add to Cart" button on the single product page.', 'law-office' ) ),
					'std'      => '',
					'type'     => 'text_editor',
				),
			) ) );
		}
	}
}

// Add custom text after "Add to cart" button
// @hooked woocommerce_template_single_add_to_cart - 30
if ( ! function_exists( 'law_office_woocommerce_extensions_product_text_after_add_to_cart' ) ) {
	add_action( 'woocommerce_single_product_summary', 'law_office_woocommerce_extensions_product_text_after_add_to_cart', 30 );
	function law_office_woocommerce_extensions_product_text_after_add_to_cart() {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'text_after_add_to_cart' ) ) {
			law_office_show_layout( law_office_get_theme_option( 'single_product_text_after_add_to_cart' ), '<div class="single_product_custom_text_after_add_to_cart">', '</div>' );
		}
	}
}



/* Add parameter 'Single Product "Add to cart" Sticky'
------------------------------------------------------------------- */

// Theme init priorities:
// 3 - add/remove Theme Options elements
if ( ! function_exists( 'law_office_woocommerce_extensions_product_add_to_cart_sticky_theme_setup3' ) ) {
	add_action( 'after_setup_theme', 'law_office_woocommerce_extensions_product_add_to_cart_sticky_theme_setup3', 3 );
	function law_office_woocommerce_extensions_product_add_to_cart_sticky_theme_setup3() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'add_to_cart_sticky' )
		) {
			// Add 'single_product_add_to_cart_sticky' to the theme-specific options
			law_office_storage_set_array_before( 'options', 'show_related_posts_shop', apply_filters( 'law_office_filter_woocommerce_extensions_add_to_cart_sticky_args', array(
				'single_product_add_to_cart_sticky' => array(
					'title'    => esc_html__( 'Sticky "Add to Cart" Bottom Bar', 'law-office' ),
					'desc'     => wp_kses_data( __( 'Add sticky "Add to Cart" bottom bar to the single product page.', 'law-office' ) ),
					'override' => array(
						'mode'    => 'product',
						'section' => esc_html__( 'Content', 'law-office' ),
					),
					'std'      => 0,
					'type'     => 'switch',
				),
			) ) );
		}
	}
}

// Add sticky bar to the footer
if ( ! function_exists( 'law_office_woocommerce_extensions_product_add_to_cart_sticky_bottom_bar' ) ) {
	add_action( 'wp_footer', 'law_office_woocommerce_extensions_product_add_to_cart_sticky_bottom_bar' );
	function law_office_woocommerce_extensions_product_add_to_cart_sticky_bottom_bar() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'add_to_cart_sticky' )
			&& (int) law_office_get_theme_option( 'single_product_add_to_cart_sticky' ) == 1
			&& is_product()
		) {
			global $product;
			// Disable slider in bottom bar
			add_filter( 'woocommerce_single_product_flexslider_enabled', '__return_false' );
			?>
			<div class="single_product_bottom_bar_sticky">
				<div class="single_product_bottom_bar_title">
					<?php
					$post_thumbnail_id = $product->get_image_id();
					if ( $post_thumbnail_id > 0 ) {
						$html = apply_filters( 'woocommerce_single_product_image_thumbnail_html', wc_get_gallery_image_html( $post_thumbnail_id ), $post_thumbnail_id );
						?><div class="single_product_bottom_bar_product_image"><?php
							echo wp_kses( $html, 'law_office_kses_content' );
						?></div><?php
					}
					?>
					<div class="single_product_bottom_bar_product_title"><?php
						the_title( '<h6 class="product_title entry-title">', '</h6>' );
					?></div>
				</div>
				<div class="single_product_bottom_bar_info">
					<div class="single_product_bottom_bar_product_price"><?php
						echo wp_kses( $product->get_price_html(), 'law_office_kses_content' );
					?></div>
					<div class="single_product_bottom_bar_product_button"><?php
						ob_start();
						woocommerce_template_single_add_to_cart();
						$output = ob_get_contents();
						ob_end_clean();
						if ( strpos( $output, '<form class="variations_form ' ) !== false ) {
							$output = '<button type="button" class="button alt single_product_bottom_bar_button_select_options">' . esc_html__( 'Select options', 'law-office' ) . '</button>';
						}
						trx_addons_show_layout( $output );
					?></div>
				</div>
			</div>
			<div class="single_product_bottom_bar_sticky_holder"></div>
			<?php
			// Enable slider again
			remove_action( 'woocommerce_single_product_flexslider_enabled', '__return_false' );	// Is equal to rf
		}
	}
}

// Add var to js
if ( ! function_exists( 'law_office_woocommerce_extensions_product_add_to_cart_sticky_localize_script' ) ) {
	add_action( 'law_office_filter_localize_script', 'law_office_woocommerce_extensions_product_add_to_cart_sticky_localize_script' );
	function law_office_woocommerce_extensions_product_add_to_cart_sticky_localize_script( $vars = array() ) {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'add_to_cart_sticky' ) ) {
			$vars['add_to_cart_sticky'] = law_office_exists_woocommerce()
											&& is_product()
											&& (int)law_office_get_theme_option( 'single_product_add_to_cart_sticky' ) == 1;
		}
		return $vars;
	}
}



/* Add parameter 'Show Product meta'
------------------------------------------------------------------- */

// Theme init priorities:
// 3 - add/remove Theme Options elements
if ( ! function_exists( 'law_office_woocommerce_extensions_product_meta_theme_setup3' ) ) {
	add_action( 'after_setup_theme', 'law_office_woocommerce_extensions_product_meta_theme_setup3', 3 );
	function law_office_woocommerce_extensions_product_meta_theme_setup3() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'product_meta' )
		) {
			// Add 'single_product_meta' to the theme-specific options
			law_office_storage_set_array_before( 'options', 'show_related_posts_shop', apply_filters( 'law_office_filter_woocommerce_extensions_product_meta_args', array(
				'single_product_meta' => array(
					'title'   => esc_html__( 'Show product meta', 'law-office' ),
					'desc'    => wp_kses_data( __( "Show Categories, Tags, SKU, Product ID on the single product page.", 'law-office' ) ),
					'std'     => 1,
					'type'    => 'switch',
				),
			) ) );
		}
	}
}


// Disable product meta
if ( ! function_exists( 'law_office_woocommerce_extensions_product_meta_disable' ) ) {
	add_action( 'woocommerce_single_product_summary', 'law_office_woocommerce_extensions_product_meta_disable', 1 );
	function law_office_woocommerce_extensions_product_meta_disable() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'product_meta' )
			&& is_product()
		) {
			if ( (int) law_office_get_theme_option( 'single_product_meta' ) == 0 ) {
				remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
			}
		}
	}
}



/* Add tabs to the single product details
------------------------------------------------------------------- */

// Theme init priorities:
// 3 - add/remove Theme Options elements
if ( ! function_exists( 'law_office_woocommerce_extensions_custom_tabs_theme_setup3' ) ) {
	add_action( 'after_setup_theme', 'law_office_woocommerce_extensions_custom_tabs_theme_setup3', 3 );
	function law_office_woocommerce_extensions_custom_tabs_theme_setup3() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'custom_tabs' )
		) {
			// Add 'single_product_custom_tabs' to the theme-specific options
			law_office_storage_set_array_before( 'options', 'show_related_posts_shop', apply_filters( 'law_office_filter_woocommerce_extensions_custom_tabs_args', array(
				'single_product_custom_tabs' => array(
					"title" => esc_html__( "Tabs manager", 'law-office' ),
					"desc" => wp_kses_data( __( "Manage tabs in the Details section of the single product: hide any tab, reorder tabs, add an icon to the tab title, change a title, add new tabs with custom content", 'law-office' ) ),
					'override'   => array(
						'mode'    => 'product',
						'section' => esc_html__( 'Content', 'law-office' ),
					),
					"clone" => true,
					"std" => array(
						array( 'tab_title' => esc_html__( "Description", 'law-office' ), 'tab_slug' => 'description', 'tab_icon' => '', 'tab_show' => 1, 'tab_content' => '' ),
						array( 'tab_title' => esc_html__( "Reviews (%d)", 'law-office' ), 'tab_slug' => 'reviews', 'tab_icon' => '', 'tab_show' => 1, 'tab_content' => '' ),
						array( 'tab_title' => esc_html__( "Additional information", 'law-office' ), 'tab_slug' => 'additional_information', 'tab_icon' => '', 'tab_show' => 1, 'tab_content' => '' ),
					),
					"type" => "group",
					"fields" => array(
						"tab_show" => array(
							"title" => esc_html__( "Tab visible", 'law-office' ),
							"class" => "law_office_column-1_4",
							"std" => 1,
							"type" => "switch"
						),
						"tab_title" => array(
							"title" => esc_html__( "Tab title", 'law-office' ),
							"class" => "law_office_column-1_4",
							"std" => "",
							"type" => "text"
						),
						"tab_slug" => array(
							"title" => esc_html__( "Tab slug", 'law-office' ),
							"class" => "law_office_column-1_4",
							"std" => "",
							"type" => "text"
						),
						"tab_icon" => array(
							"title" => esc_html__( "Tab icon", 'law-office' ),
							"class" => "law_office_column-1_4",
							"std" => "",
							"type" => "icon"
						),
						"tab_content" => array(
							"title" => esc_html__( "Tab content", 'law-office' ),
							"desc" => esc_html__( "Only reordering, changing a title and visibility are allowed for standard WooCommerce tabs (Reviews, Description and Additional information). Don't fill a content for them - this is allowed only for custom tabs.", 'law-office')
									. '<br>'
									. esc_html__( "You can use a macros %%TITLE%% and %%PRICE%% to insert a title and a price of this product to the content.", 'law-office'),
							"class" => "law_office_column-1_1",
							"std" => "",
							"type" => "text_editor"
						),
					)
				),
			) ) );
		}
	}
}

// Add custom tabs (if defined for the current product)
if ( ! function_exists( 'law_office_woocommerce_extensions_custom_tabs_add_to_product_tabs' ) ) {
	add_filter( 'woocommerce_product_tabs', 'law_office_woocommerce_extensions_custom_tabs_add_to_product_tabs' );
	function law_office_woocommerce_extensions_custom_tabs_add_to_product_tabs( $tabs = array() ) {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'custom_tabs' ) ) {
			global $product;
			$custom_tabs = law_office_get_theme_option( 'single_product_custom_tabs' );
			if ( is_array( $custom_tabs ) ) {
				$priority = 0;
				$new_tabs = array();
				foreach ( $custom_tabs as $tab ) {
					if ( ! empty( $tab['tab_title'] ) && (int) $tab['tab_show'] > 0 ) {
						$priority += 10;
						$slug = sanitize_title( ! empty( $tab['tab_slug'] ) ? $tab['tab_slug'] : $tab['tab_title'] );
						$new_tabs[ $slug ] = isset( $tabs[ $slug ] )
												? $tabs[ $slug ]
												: array( 'callback' => 'law_office_woocommerce_extensions_custom_tabs_template' );
						$new_tabs[ $slug ][ 'title' ] = ( ! law_office_is_off( $tab['tab_icon'] )
												 			? '<span class="woocommerce-tab-icon ' . esc_attr( $tab['tab_icon'] ) . '"></span>'
												 			: ''
												 			)
														. ( 'reviews' == $slug && is_object( $product )
															? str_replace( '%d', $product->get_review_count(), $tab['tab_title'] )
															: $tab['tab_title']
															);
						$new_tabs[ $slug ][ 'priority' ] = $priority;
					}
				}
				$tabs = $new_tabs;
			}
		}
		return $tabs;
	}
}

// Display custom tabs
if ( ! function_exists( 'law_office_woocommerce_extensions_custom_tabs_template' ) ) {
	function law_office_woocommerce_extensions_custom_tabs_template( $tab_slug, $tab_data ) {
		global $product;
		$custom_tabs = law_office_get_theme_option( 'single_product_custom_tabs' );
		if ( is_array( $custom_tabs ) && is_object( $product ) ) {
			foreach ( $custom_tabs as $tab ) {
				$slug = sanitize_title( $tab['tab_title'] );
				if ( $slug == $tab_slug ) {
					$title = get_the_title();
					$price = $product->get_price_html();
					law_office_show_layout(
						str_ireplace(
							array( '%%TITLE%%', '%%PRICE%%' ),
							array( $title,      $price ),
							$tab['tab_content']
						)
					);
				}
			}
		}
	}
}



/* Add section 'Rating details' to the single product
------------------------------------------------------------------- */

// Replace core Woocommerce tabs output
if ( ! function_exists( 'law_office_woocommerce_extensions_replace_woocommerce_core_tabs' ) ) {
	add_action( 'woocommerce_after_single_product_summary', 'law_office_woocommerce_extensions_replace_woocommerce_core_tabs', 1 );
	function law_office_woocommerce_extensions_replace_woocommerce_core_tabs() {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'rating_details' ) ) {
			global $product;
			if ( is_object( $product ) && function_exists( 'woocommerce_output_product_data_tabs' ) ) {
				$rating_count = $product->get_rating_count();
				if ( $rating_count > 0 ) {
					remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
					add_action( 'woocommerce_after_single_product_summary', 'law_office_woocommerce_extensions_add_rating_details', 10 );
				}
			}
		}
	}
}

// Add 'Rating Details' to the tab 'Reviews'
if ( ! function_exists( 'law_office_woocommerce_extensions_add_rating_details' ) ) {
	// Handler of the add_action( 'woocommerce_after_single_product_summary', 'law_office_woocommerce_extensions_add_rating_details', 10 );
	function law_office_woocommerce_extensions_add_rating_details() {
		global $product;
		// Capture a tabs output
		ob_start();
		woocommerce_output_product_data_tabs();
		$output = ob_get_contents();
		ob_end_clean();
		// Inject the section 'Rating Details'
		$rating_count  = $product->get_rating_count();
		$review_count  = $product->get_review_count();
		$average       = $product->get_average_rating();
		$html          = '<h5 class="rating_details_title">' . esc_html__( 'Average rating', 'law-office' ) . '</h5>'
						. '<div class="rating_details_avg">' . esc_html( $average ) . '</div>'
						. '<div class="rating_details_avg_stars">' . wc_get_rating_html( $average, $rating_count ) . '</div>'
						. '<div class="rating_details_avg_total">' . esc_html( sprintf( _n( '%d review', '%d reviews', $review_count, 'law-office' ), $review_count ) ) . '</div>';
		$rating_counts = $product->get_rating_counts();
		if ( is_array( $rating_counts ) ) {
			$rating_counts = law_office_array_merge( array( 5 => 0, 4 => 0, 3 => 0, 2 => 0, 1 => 0 ), $rating_counts );
			$html .= '<div class="rating_details_table">';
			foreach( $rating_counts as $mark => $total ) {
				$html .= '<div class="rating_details_table_row">'
							. '<div class="rating_details_table_cell rating_details_table_cell_mark">' . esc_html( sprintf( _n( '%d star', '%d stars', $mark, 'law-office' ), $mark ) ) . '</div>'
							. '<div class="rating_details_table_cell rating_details_table_cell_bar">'
									. '<div class="rating_details_table_cell_bar_fill_' . esc_attr( $total ) . '"></div>'
								. '</div>'
							. '<div class="rating_details_table_cell rating_details_table_cell_total">' . esc_html( apply_filters( 'law_office_filter_woocommerce_extensions_rating_details_as_percent', true ) ? round( $total / $rating_count * 100 ) . '%' : $total ) . '</div>'
							. '</div>';
			}
			$html .= '</div>';
		}
		$output = str_replace(
						'<div id="comments',
						'<div class="rating_details">' . $html . '</div>'
						. '<div id="comments',
						$output
						);
		// Display tabs
		law_office_show_layout( $output );
	}
}



/* Add parameter "Product's Video"
------------------------------------------------------------------- */
if ( ! function_exists( 'law_office_woocommerce_extensions_product_video_setup' ) ) {
	add_action( 'after_setup_theme', 'law_office_woocommerce_extensions_product_video_setup' );
	function law_office_woocommerce_extensions_product_video_setup() {
		if ( law_office_exists_trx_addons()
			&& law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'product_video' )
		) {
			if ( is_admin() ) {
				add_filter( 'law_office_filter_override_options', 'law_office_woocommerce_extensions_product_video_add_options' );
				add_filter( 'law_office_filter_update_post_options', 'law_office_woocommerce_extensions_product_video_update_options', 10, 3 );
			}
		}
	}
}

// After 'wp' options from the post meta are available
if ( ! function_exists( 'law_office_woocommerce_extensions_product_video_init' ) ) {
	add_action( 'wp', 'law_office_woocommerce_extensions_product_video_init', 20 );
	function law_office_woocommerce_extensions_product_video_init() {
		if ( law_office_exists_trx_addons()
			&& law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'product_video' )
		) {
			if ( ! is_admin() ) {
				// Add video to the gallery
				if ( law_office_get_theme_option( 'woocommerce_extensions_product_video_position' ) == 'first_thumb' ) {
					// Place video first (before main video)
					add_filter( 'woocommerce_single_product_image_thumbnail_html', 'law_office_woocommerce_extensions_product_video_add_thumbnails', 10, 2 );
				} else {
					// Place video last (after all thumbnails)
					add_action( 'woocommerce_product_thumbnails', 'law_office_woocommerce_extensions_product_video_add_thumbnails', 100 );
				}
			}
		}
	}
}

// Add options to the product
if ( ! function_exists( 'law_office_woocommerce_extensions_product_video_add_options' ) ) {
	//Handler of the add_filter('law_office_filter_override_options', 'law_office_woocommerce_extensions_product_video_add_options');
	function law_office_woocommerce_extensions_product_video_add_options( $list ) {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'product_video' ) ) {
			global $post_type;
			if ( 'product' == $post_type && law_office_options_allow_override( $post_type ) ) {
				$list[] = array(
					'law_office_woocommerce_extensions_product_video_options',
					esc_html__( 'Product video', 'law-office' ),
					'law_office_woocommerce_extensions_product_video_options_show',
					$post_type,
					'side',
					'default',
				);
			}
		}
		return $list;
	}
}

// Return options to the product video
if ( ! function_exists( 'law_office_woocommerce_extensions_product_video_get_options' ) ) {
	function law_office_woocommerce_extensions_product_video_get_options() {
		return apply_filters( 'law_office_filter_woocommerce_extensions_product_video_options', array(
					'woocommerce_extensions_product_video_url'   => array(
						"title" => esc_html__( "Video URL", 'law-office' ),
						"desc" => wp_kses_data( __('Specify URL to show a videoplayer from Youtube, Vimeo or other compatible video hosting', 'law-office') ),
						"std" => "",
						"type" => "text"
					),
					'woocommerce_extensions_product_video_image' => array(
						"title" => esc_html__( "Cover image", 'law-office' ),
						"desc" => wp_kses_data( __( "Select an image to be used as a video cover", 'law-office' ) ),
						"std" => "",
						"type" => "image"
					),
					'woocommerce_extensions_product_video_position' => array(
						"title" => esc_html__( "Video position", 'law-office' ),
						"desc" => wp_kses_data( __( "Select a position of the video in the galery", 'law-office' ) ),
						"std" => "first_thumb",
						"options" => apply_filters( 'law_office_filter_woocommerce_extensions_product_video_positions', array(
							'first_thumb' => esc_html__( 'First thumb', 'law-office' ),
							'last_thumb' => esc_html__( 'Last thumb', 'law-office' ),
						) ),
						"type" => "select"
					),
					'woocommerce_extensions_product_video_in_popup' => array(
						"title" => esc_html__( "Open video in popup", 'law-office' ),
						"desc" => wp_kses_data( __( "Open video in a popup or embed to the gallery", 'law-office' ) ),
						"std" => 1,
						"type" => "switch"
					),
					'woocommerce_extensions_product_video_button_position' => array(
						"title" => esc_html__( "Button position", 'law-office' ),
						"desc" => wp_kses_data( __( "Select a position of the button to open popup", 'law-office' ) ),
						"std" => "lb",
						"dependency" => array(
							'compare' => 'or',
							'woocommerce_extensions_product_video_in_popup' => array( 1 ),
							'woocommerce_extensions_product_video_image' => array( 'not_empty' )
						),
						"options" => apply_filters( 'law_office_filter_woocommerce_extensions_product_video_button_positions', array(
							'lt' => esc_html__( 'Top Left', 'law-office' ),
							'rt' => esc_html__( 'Top Right', 'law-office' ),
							'cc' => esc_html__( 'Middle Center', 'law-office' ),
							'lb' => esc_html__( 'Bottom Left', 'law-office' ),
							'rb' => esc_html__( 'Bottom Right', 'law-office' )
						) ),
						"type" => "select"
					),
					'woocommerce_extensions_product_video_ratio' => array(
						"title" => esc_html__( "Video ratio", 'law-office' ),
						"desc" => wp_kses_data( __( "Select a ratio of the video", 'law-office' ) ),
						"std" => "16:9",
						"dependency" => array(
							'woocommerce_extensions_product_video_in_popup' => array( 0 ),
							'woocommerce_extensions_product_video_image' => array( 'is_empty' )
						),
						"options" => apply_filters( 'law_office_filter_woocommerce_extensions_product_video_ratio', array(
							'2:1'  => esc_html__( '2:1', 'law-office' ),
							'17:9' => esc_html__( '17:9', 'law-office' ),
							'16:9' => esc_html__( '16:9', 'law-office' ),
							'4:3'  => esc_html__( '4:3', 'law-office' ),
							'1:1'  => esc_html__( '1:1', 'law-office' ),
							'3:4'  => esc_html__( '3:4', 'law-office' ),
							'9:16' => esc_html__( '9:16', 'law-office' ),
							'9:17' => esc_html__( '9:17', 'law-office' ),
							'1:2'  => esc_html__( '1:2', 'law-office' ),
						)),
						"type" => "select"
					),
		) );
	}
}


// Add extension-specific dependencies to the general theme dependencies
if ( ! function_exists( 'law_office_woocommerce_extensions_product_video_dependencies' ) ) {
	add_filter( 'law_office_filter_get_theme_dependencies', 'law_office_woocommerce_extensions_product_video_dependencies' );
	function law_office_woocommerce_extensions_product_video_dependencies( $depends ) {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'product_video' ) ) {
			$options = law_office_woocommerce_extensions_product_video_get_options();
			foreach ( $options as $k => $v ) {
				if ( isset( $v['dependency'] ) ) {
					$depends[ $k ] = $v['dependency'];
				}
			}
		}
		return $depends;
	}
}

// Callback function to show product options
if ( ! function_exists( 'law_office_woocommerce_extensions_product_video_options_show' ) ) {
	function law_office_woocommerce_extensions_product_video_options_show( $post = false, $args = false ) {
		if ( empty( $post ) || ! is_object( $post ) || empty( $post->ID ) ) {
			global $post, $post_type;
			$mb_post_id   = $post->ID;
			$mb_post_type = $post_type;
		} else {
			$mb_post_id   = $post->ID;
			$mb_post_type = $post->post_type;
		}
		if ( 'product' == $mb_post_type && law_office_options_allow_override( $mb_post_type ) ) {
			// Load saved options
			$meta = get_post_meta( $mb_post_id, 'law_office_options', true );
			$options = law_office_woocommerce_extensions_product_video_get_options();
			?>
			<div class="law_office_options law_office_product_options law_office_product_video_options">
				<div class="law_office_options_section">
					<?php
					foreach ( $options as $k => $v ) {
						$v['val'] = isset( $meta[ $k ] ) ? $meta[ $k ] : $v['std'];
						law_office_show_layout( law_office_options_show_field( $k, $v ) );
					}
					?>
				</div>
			</div>
			<?php
		}
	}
}


// Save product options
if ( ! function_exists( 'law_office_woocommerce_extensions_product_video_update_options' ) ) {
	//Handler of the add_filter( 'law_office_filter_update_post_options', 'law_office_woocommerce_extensions_product_video_update_options', 10, 3 );
	function law_office_woocommerce_extensions_product_video_update_options( $meta, $post_id, $post_type = '' ) {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'product_video' )
			&& 'product' == $post_type
		) {
			$options = law_office_woocommerce_extensions_product_video_get_options();
			foreach ( $options as $k => $v ) {
				// Get option value from POST
				$meta[ $k ] = isset( $_POST[ "law_office_options_field_{$k}" ] )
								? law_office_get_value_gp( "law_office_options_field_{$k}" )
								: ( 'checkbox' == $v['type'] ? 0 : '' );
			}
		}
		return $meta;
	}
}



// Add thumb with video to the gallery
if ( ! function_exists( 'law_office_woocommerce_extensions_product_video_add_thumbnails' ) ) {
	//Handler of the add_action( 'woocommerce_product_thumbnails', 'law_office_woocommerce_extensions_product_video_add_thumbnails', 1 | 100 );
	//Handler of the add_filter( 'woocommerce_single_product_image_thumbnail_html', 'law_office_woocommerce_extensions_product_video_add_thumbnails', 10, 2 );
	function law_office_woocommerce_extensions_product_video_add_thumbnails( $image_html = '', $post_thumbnail_id = 0 ) {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'product_video' ) ) {
			// Remove filter to prevent resursion
			if ( has_filter( 'woocommerce_single_product_image_thumbnail_html', 'law_office_woocommerce_extensions_product_video_add_thumbnails' ) ) {
				remove_action( 'woocommerce_single_product_image_thumbnail_html', 'law_office_woocommerce_extensions_product_video_add_thumbnails', 10 );	// Is equal to rf
			}
			// Add thumb with video (if specified)
			$video = law_office_get_theme_option( 'woocommerce_extensions_product_video_url' );
			if ( ! empty( $video ) ) {
				$popup      = law_office_get_theme_option( 'woocommerce_extensions_product_video_in_popup' );
				$cover      = law_office_get_theme_option( 'woocommerce_extensions_product_video_image' );
				$cover_id   = ! empty( $cover ) ? law_office_attachment_url_to_postid( $cover ) : 0;
				$button_pos = law_office_get_theme_option( 'woocommerce_extensions_product_video_button_position' );
				// Prepare layout with thumb
				if ( $cover_id ) {
					$html = wc_get_gallery_image_html( $cover_id );
				} else {
					$cover     = wc_placeholder_img_src( 'woocommerce_single' );
					$cover_id  = $cover;
					$cover_alt = esc_html__( 'Product video', 'law-office' );
					$html  = sprintf( '<div class="woocommerce-product-gallery__image woocommerce-product-gallery__image--placeholder" data-thumb="%1$s" data-thumb-alt="%2$s">',
										esc_url( $cover ),
										$cover_alt
									);
					$style = law_office_get_theme_option( 'single_product_gallery_style', 'default' );
					if ( $popup || ! in_array( $style, array( 'cascade', 'grid' ) ) ) {
						$html .= '<a href="' . esc_url( $cover ) . '">'
									. sprintf( '<img src="%1$s" alt="%2$s" class="wp-post-image" />',
												esc_url( $cover ),
												$cover_alt
											)
									. '</a>';
					} else {
						$cover    = '';
						$cover_id = '';
					}
					$html .= '</div>';
				}
				// Add class to the image wrap
				$html = str_replace(
							'"woocommerce-product-gallery__image',
							'"woocommerce-product-gallery__image'
								. ' woocommerce-product-gallery__image--with_video'
								. ( $cover
									? ' woocommerce-product-gallery__image--video_button_' . esc_attr( $button_pos )
									: ' woocommerce-product-gallery__image--video_present'
										. ' woocommerce-product-gallery__image--video_ratio_' . str_replace( ':', '_', law_office_get_theme_option( 'woocommerce_extensions_product_video_ratio' ) )
									),
							$html
						);
				// If video in the popup - add a 'play' button
				if ( $popup ) {
					$popup_layout = explode(
									'<!-- .sc_layouts_popup -->',
									law_office_get_video_layout( array(
																	'link'  => $video,
																	'embed' => '',
																	'cover' => $cover_id,
																	'cover_size' => 'full',
																	'show_cover' => false,
																	'popup' => true
																	)
																)
									);
					if ( ! empty( $popup_layout[0] ) && ! empty( $popup_layout[1] ) ) {
						if ( preg_match( '/<a .*<\/a>/', $popup_layout[0], $matches ) && ! empty( $matches[0] ) ) {
							// Remove link to the lightbox
							//$html = preg_replace( '/(.*)(<a[^>]*>)(.*)(<\/a>)(.*)/', '$1$3$5', $html );
							//$html = str_replace( 'data-large_image', 'data-dummy_image', $html );
							// Add popup and button
							$html = str_replace(
										'</div>',
										sprintf( '<div class="post_video_hover post_video_hover_popup">%1$s</div>%2$s',
													 $matches[0],
													 $popup_layout[1]
												)
										. '</div>',
										$html
									);
						}
					}
				} else {
					$video_layout = law_office_get_video_layout( array(
																	'link'  => $video,
																	'embed' => '',
																	'cover' => $cover_id,
																	'cover_size' => 'full',
																	'show_cover' => false,
																	'popup' => false
																	)
																);
					$html = str_replace(
								'</div>',
								sprintf( '<div class="woocommerce-product-gallery__image--video_wrap">%s</div>', $video_layout )
								. '</div>',
								$html
							);
				}
				$html = apply_filters( 'woocommerce_single_product_image_thumbnail_html', $html, $cover );
				if ( ! empty( $image_html ) ) {
					$image_html = trim( $html ) . trim( $image_html );
				} else {
					law_office_show_layout( $html );
				}
			}
		}
		return $image_html;
	}
}











/* Add parameter 'Add attributes to the products list'
------------------------------------------------------------------- */

// Theme init priorities:
// 3 - add/remove Theme Options elements
if ( ! function_exists( 'law_office_woocommerce_extensions_add_attributes_theme_setup3' ) ) {
	add_action( 'after_setup_theme', 'law_office_woocommerce_extensions_add_attributes_theme_setup3', 3 );
	function law_office_woocommerce_extensions_add_attributes_theme_setup3() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'add_attributes_to_product_list' )
		) {
			// Add parameter to the theme-specific options
			law_office_storage_set_array_after( 'options', 'shop_pagination', apply_filters( 'law_office_filter_woocommerce_extensions_add_attributes_to_products_list_args', array(
				'add_attributes_to_products_list' => array(
					'title'   => esc_html__( 'Add attributes to the products list', 'law-office' ),
					'desc'    => wp_kses_data( __( "Display selected attributes in products on the shop page.", 'law-office' ) ),
					'std'     => '',
					'dir'     => 'vertical',
					'sortable'=> true,
					'options' => array(),
					'type'    => 'checklist',
				),
				'action_on_attribute_click' => array(
					'title'   => esc_html__( 'Action on attribute click', 'law-office' ),
					'desc'    => wp_kses_data( __( "Select an action on attribute click: open a single product page or apply a filter.", 'law-office' ) ),
					'std'     => 'none',
					'options' => array(
						'none'   => esc_html__( 'No action', 'law-office' ),
						'link'   => esc_html__( 'Open product', 'law-office' ),
						'filter' => esc_html__( 'Apply filter', 'law-office' ),
					),
					'type'    => 'radio',
				),
				'swap_on_attribute_hover' => array(
					'title' => esc_html__( 'Swap images on attribute hover', 'law-office' ),
					'desc'  => wp_kses_data( __( "Swap a product image (only for variable products) on attribute hover.", 'law-office' ) ),
					'std'   => 0,
					'type'  => 'switch',
				),
			) ) );
		}
	}
}


// Return lists with choises when its need in the admin mode
if ( ! function_exists( 'law_office_woocommerce_extensions_add_attributes_get_list_choises' ) ) {
	add_filter( 'law_office_filter_options_get_list_choises', 'law_office_woocommerce_extensions_add_attributes_get_list_choises', 10, 2 );
	function law_office_woocommerce_extensions_add_attributes_get_list_choises( $list, $id ) {
		if ( is_array( $list ) && count( $list ) == 0 ) {
			if ( strpos( $id, 'add_attributes_to_products_list' ) === 0 ) {
				$list = law_office_woocommerce_extensions_get_list_wc_attributes();
			}
		}
		return $list;
	}
}


// Return list of the WooCommerce attributes
if ( !function_exists( 'law_office_woocommerce_extensions_get_list_wc_attributes' ) ) {
	function law_office_woocommerce_extensions_get_list_wc_attributes() {
		static $list = false;
		if ( $list === false ) {
			$list = array();
			$attribute_taxonomies = wc_get_attribute_taxonomies();
			if ( ! empty( $attribute_taxonomies ) ) {
				foreach ( $attribute_taxonomies as $attribute ) {
					$list[ wc_attribute_taxonomy_name( $attribute->attribute_name ) ] = $attribute->attribute_label;
				}
			}
		}
		return apply_filters( 'law_office_filter_woocommerce_extensions_get_list_wc_attributes', $list );
	}
}


// Display selected attribute
if ( ! function_exists( 'law_office_woocommerce_extensions_show_attributes' ) ) {
	add_action( 'woocommerce_after_shop_loop_item', 'law_office_woocommerce_extensions_show_attributes', 18 );         // 20 is used to close item wrapper
	add_action( 'woocommerce_after_shop_loop_item_title', 'law_office_woocommerce_extensions_show_attributes', 8 );    //  5 is used to display rating
																													//  7 is used to display excerpt (in the list mode)
																													// 10 is used to display price
	function law_office_woocommerce_extensions_show_attributes() {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'add_attributes_to_product_list' ) ) {
			$attributes = law_office_get_theme_option( 'add_attributes_to_products_list' );
			if ( ! empty( $attributes ) && ! law_office_is_off( $attributes ) && apply_filters( 'law_office_filter_woocommerce_extensions_show_attributes', is_shop(), $attributes ) ) {
				$mode = law_office_storage_get( 'shop_mode' );
				if ( ( 'woocommerce_after_shop_loop_item_title' == current_action() && 'list' == $mode )
					||
					( 'woocommerce_after_shop_loop_item' == current_action() && 'thumbs' == $mode )
				) {
					do_action( 'law_office_action_product_attributes', apply_filters( 'law_office_filter_woocommerce_extensions_show_attributes_args', array(
								'attributes' => $attributes,
								'action'     => law_office_get_theme_option( 'action_on_attribute_click' ),
								'swap'       => law_office_get_theme_option( 'swap_on_attribute_hover' )
								) ) );
				}
			}
		}
	}
}



/* Add parameter 'Product style' to the shop page settings
------------------------------------------------------------------- */

// Theme init priorities:
// 3 - add/remove Theme Options elements
if ( ! function_exists( 'law_office_woocommerce_extensions_add_product_style_theme_setup3' ) ) {
	add_action( 'after_setup_theme', 'law_office_woocommerce_extensions_add_product_style_theme_setup3', 3 );
	function law_office_woocommerce_extensions_add_product_style_theme_setup3() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'product_style' )
		) {
			// Add parameter to the theme-specific options
			law_office_storage_set_array_after( 'options', 'shop_mode', apply_filters( 'law_office_filter_woocommerce_extensions_add_product_style_args', array(
				'product_style' => array(
					'title'      => esc_html__( 'Product style', 'law-office' ),
					'desc'       => wp_kses_data( __( 'Style of product items on the shop page.', 'law-office' ) ),
					'std'     => 'default',
					'options' => array(),
					'type'    => 'select',
				),
			) ) );
		}
	}
}


// Return lists with choises when its need in the admin mode
if ( ! function_exists( 'law_office_woocommerce_extensions_add_product_style_get_list_choises' ) ) {
	add_filter( 'law_office_filter_options_get_list_choises', 'law_office_woocommerce_extensions_add_product_style_get_list_choises', 10, 2 );
	function law_office_woocommerce_extensions_add_product_style_get_list_choises( $list, $id ) {
		if ( is_array( $list ) && count( $list ) == 0 ) {
			if ( strpos( $id, 'product_style' ) === 0 && function_exists( 'trx_addons_woocommerce_extended_products_get_list_styles' ) ) {
				$list = trx_addons_woocommerce_extended_products_get_list_styles();
			}
		}
		return $list;
	}
}


// Substitute default template in the products loop with selected in Theme Options
if ( ! function_exists( 'law_office_woocommerce_extensions_add_product_style_wc_get_template_part' ) ) {
	add_filter( 'wc_get_template_part', 'law_office_woocommerce_extensions_add_product_style_wc_get_template_part', 200, 3 );
	function law_office_woocommerce_extensions_add_product_style_wc_get_template_part( $template, $slug, $name ) {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'product_style' ) ) {
			if ( $slug == 'content' && $name == 'product'
				&& function_exists( 'trx_addons_woocommerce_extended_products_get_layouts' )
				&& ( ! function_exists( 'trx_addons_sc_stack_check' )
					 ||
					 ! trx_addons_sc_stack_check( 'trx_sc_extended_products' )
					)
				&& ! is_product()	// To prevent replace template of related products in the single product page
			) {
				$style = law_office_get_theme_option( 'product_style' );
				if ( 'default' != $style ) {
					$layouts = trx_addons_woocommerce_extended_products_get_layouts();
					if ( isset( $layouts[ $style ] ) && ! empty( $layouts[ $style ]['template'] ) ) {
						$template = $layouts[ $style ]['template'];
					}
				}
			}
		}
		return $template;
	}
}


// Add class with a "product style" to the wrap ul.products
// ( if we are not inside a shortcode 'trx_sc_extended_products' )
if ( ! function_exists( 'law_office_woocommerce_extensions_add_product_style_to_products_wrap' ) ) {
	add_filter( 'woocommerce_product_loop_start', 'law_office_woocommerce_extensions_add_product_style_to_products_wrap', 200, 1 );
	function law_office_woocommerce_extensions_add_product_style_to_products_wrap( $template ) {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'product_style' ) ) {
			if ( function_exists( 'trx_addons_woocommerce_extended_products_get_layouts' )
				&& ( ! function_exists( 'trx_addons_sc_stack_check' )
					 ||
					 ! trx_addons_sc_stack_check( 'trx_sc_extended_products' )
					)
				&& ! is_product()	// To prevent add class for the wrap of related products in the single product page
			) {
				$style = law_office_get_theme_option( 'product_style' );
				$new_classes = array(
					sprintf( 'products_style_%s', $style )
				);
				$layouts = trx_addons_woocommerce_extended_products_get_layouts();
				if ( isset( $layouts[ $style ] ) && ! empty( $layouts[ $style ]['products_classes'] ) ) {
					$new_classes = array_merge(
										$new_classes, 
										is_array( $layouts[ $style ]['products_classes'] )
											? $layouts[ $style ]['products_classes']
											: explode( ' ', $layouts[ $style ]['products_classes'] )
										);
				}
				$template = preg_replace( 
										'/(<ul[^>]*class="products )/',
										'$1' . esc_attr( join( ' ', $new_classes ) ) . ' ',
										$template
										);
			}
		}
		return $template;
	}
}


// Add class with a "product style" to each product item
if ( ! function_exists( 'law_office_woocommerce_extensions_add_product_style_to_product_items' ) ) {
	add_filter( 'woocommerce_post_class', 'law_office_woocommerce_extensions_add_product_style_to_product_items', 200, 2 );
	function law_office_woocommerce_extensions_add_product_style_to_product_items( $classes, $product ) {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'product_style' ) ) {
			if ( function_exists( 'trx_addons_woocommerce_extended_products_get_layouts' )
				&& ( ! function_exists( 'trx_addons_sc_stack_check' )
					 ||
					 ! trx_addons_sc_stack_check( 'trx_sc_extended_products' )
					)
				&& ! is_product()	// To prevent add class for the wrap of related products in the single product page
			) {
				if ( is_array( $classes ) ) {
					$style = law_office_get_theme_option( 'product_style' );
					$new_classes = array(
										sprintf( 'product_style_%s', esc_attr( $style ) )
									);
					$layouts = trx_addons_woocommerce_extended_products_get_layouts();
					if ( isset( $layouts[ $style ] ) && ! empty( $layouts[ $style ]['product_classes'] ) ) {
						$new_classes = array_merge(
											$new_classes, 
											is_array( $layouts[ $style ]['product_classes'] )
												? $layouts[ $style ]['product_classes']
												: explode( ' ', $layouts[ $style ]['product_classes'] )
											);
					}
					foreach( $new_classes as $c ) {
						$c = trim( $c );
						if ( ! empty( $c ) && ! in_array( $c, $classes ) ) {
							$classes[] = $c;
						}
					}
				}
			}
		}
		return $classes;
	}
}



/* Add parameter 'Brand attribute'
------------------------------------------------------------------- */

// Theme init priorities:
// 3 - add/remove Theme Options elements
if ( ! function_exists( 'law_office_woocommerce_extensions_add_brand_theme_setup3' ) ) {
	add_action( 'after_setup_theme', 'law_office_woocommerce_extensions_add_brand_theme_setup3', 3 );
	function law_office_woocommerce_extensions_add_brand_theme_setup3() {
		if ( law_office_exists_woocommerce()
			&& apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'add_brand' )
		) {
			// Add parameter to the theme-specific options
			law_office_storage_set_array_before( 'options', 'fonts', apply_filters( 'law_office_filter_woocommerce_extensions_add_brand_args', array(
				'shop_brand_section'         => array(
					'title'      => esc_html__( 'Brands', 'law-office' ),
					'desc'       => wp_kses_data( __( 'Settings of the "Brand" attribute', 'law-office' ) ),
					'icon'       => 'icon-star',
					'type'       => 'section',
				),
				'shop_brand_info'            => array(
					'title'      => esc_html__( 'Brand settings', 'law-office' ),
					'desc'       => wp_kses_data( __( 'Settings of the "Brand" attribute', 'law-office' ) ),
					'type'       => 'info',
				),
				'brand_attribute'            => array(
					'title'   => esc_html__( "'Brand' attribute", 'law-office' ),
					'desc'    => wp_kses_data( __( "Use selected attribute as 'Brand' (display it after title).", 'law-office' ) ),
					'std'     => 'none',
					'options' => array(),
					'type'    => 'select',
				),
				'brand_attribute_on_shop'    => array(
					'title'  => esc_html__( "Show 'Brand' on the shop page", 'law-office' ),
					'desc'   => wp_kses_data( __( "Show attribute 'Brand' in the each product on the shop page", 'law-office' ) ),
					'std'    => 0,
					'type'   => 'switch',
				),
				'brand_attribute_on_product' => array(
					'title'  => esc_html__( "Show 'Brand' on the single product page", 'law-office' ),
					'desc'   => wp_kses_data( __( "Show 'Brand' on the single product page", 'law-office' ) ),
					'std'    => 1,
					'type'   => 'switch',
				),
				'brand_attribute_label'      => array(
					'title'  => esc_html__( "Label before 'Brand'", 'law-office' ),
					'desc'   => wp_kses_data( __( "Some text to display before the 'Brand' on the single product page", 'law-office' ) ),
					'std'    => '',
					'type'   => 'text',
				),
			) ) );
		}
	}
}


// Return lists with choises when its need in the admin mode
if ( ! function_exists( 'law_office_woocommerce_extensions_add_brand_get_list_choises' ) ) {
	add_filter( 'law_office_filter_options_get_list_choises', 'law_office_woocommerce_extensions_add_brand_get_list_choises', 10, 2 );
	function law_office_woocommerce_extensions_add_brand_get_list_choises( $list, $id ) {
		if ( is_array( $list ) && count( $list ) == 0 ) {
			if ( strpos( $id, 'brand_attribute' ) === 0 ) {
				$list = array_merge( array(
							'none' => law_office_get_not_selected_text( esc_html__( 'Select attribute', 'law-office' ) )
							), law_office_woocommerce_extensions_get_list_wc_attributes() );
			}
		}
		return $list;
	}
}


// Remove brand attribute from attributes list
if ( ! function_exists( 'law_office_woocommerce_extensions_remove_brand_from_attributes_list' ) ) {
	add_filter( 'woocommerce_display_product_attributes', 'law_office_woocommerce_extensions_remove_brand_from_attributes_list', 10, 2 );
	function law_office_woocommerce_extensions_remove_brand_from_attributes_list( $attributes, $product ) {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'add_brand' ) ) {
			$brand = law_office_get_theme_option( 'brand_attribute' );
			if ( ! empty( $brand )
				&& ! law_office_is_off( $brand )
				&& (int) law_office_get_theme_option( 'brand_attribute_on_' . ( is_product() ? 'product' : 'shop' ) ) > 0
				&& taxonomy_exists( $brand )
				&& apply_filters( 'law_office_filter_woocommerce_extensions_show_brand', true )
			) {
				$key = "attribute_{$brand}";
				if ( isset( $attributes[ $key ] ) ) {
					unset( $attributes[ $key ] );
				}
			}
		}
		return $attributes;
	}
}


// Display selected attribute as brand (after title) on single product page and on the shop page
if ( ! function_exists( 'law_office_woocommerce_extensions_show_brand' ) ) {
	add_action( 'woocommerce_single_product_summary', 'law_office_woocommerce_extensions_show_brand', 6 );     // woocommerce_single_product_summary:5 is used to display a product title
	add_action( 'woocommerce_after_shop_loop_item_title', 'law_office_woocommerce_extensions_show_brand', 6 ); // woocommerce_after_shop_loop_item_title:5 is used to display a product rating
																											// woocommerce_shop_loop_item_title:10 is used to display a product title
	function law_office_woocommerce_extensions_show_brand() {
		if ( apply_filters( 'law_office_filter_woocommerce_extensions_allow_components', true, 'add_brand' ) ) {
			global $product;
			$brand = law_office_get_theme_option( 'brand_attribute' );
			$show  = (int) law_office_get_theme_option( 'brand_attribute_on_' . ( current_action() == 'woocommerce_single_product_summary' ? 'product' : 'shop' ) );
			if ( ! empty( $brand ) && ! law_office_is_off( $brand ) && taxonomy_exists( $brand ) && is_object( $product ) && apply_filters( 'law_office_filter_woocommerce_extensions_show_brand', $show ) ) {
				$type = law_office_woocommerce_extensions_attrib_get_type( $brand );
				$terms = wc_get_product_terms( $product->get_id(), $brand, array( 'fields' => 'all' ) );
				if ( is_array( $terms ) && count( $terms ) > 0 ) {
					?><div class="woocommerce_extensions_brand"><?php
					foreach ( $terms as $term ) {
						$term_val = $term->name;
						$term_image = '';
						$term_color = '';
						if ( $type == 'image' ) {
							$term_image = get_term_meta( $term->term_id, 'value', true );
							$term_image = ! empty( $term_image ) && law_office_is_url( $term_image )
											? law_office_add_thumb_size( $term_image, law_office_get_thumb_size( 'masonry' ) )
											: '';
						} else if ( $type == 'color' ) {
							$term_color = get_term_meta( $term->term_id, 'value', true );
							if ( empty( $term_color ) ) {
								$term_color = $term->slug;
							}
						}
						?><span class="woocommerce_extensions_brand_item"><?php
							if ( $type == 'image' && ! empty( $term_image ) ) {
								?><img src="<?php echo esc_url( $term_image ); ?>" class="woocommerce_extensions_brand_item_image" alt="<?php echo esc_attr( $term_val ); ?>"><?php
							}
							// Label
							$label = law_office_get_theme_option( 'brand_attribute_label' );
							if ( ! empty( $label ) && apply_filters( 'law_office_filter_woocommerce_extensions_show_brand_label', current_action() == 'woocommerce_single_product_summary' ) ) {
								?><span class="woocommerce_extensions_brand_item_label"><?php echo esc_html( $label ); ?></span><?php
							}
							// Brand
							?><span class="woocommerce_extensions_brand_item_caption<?php
								if ( ! empty( $term_color ) ) {
									echo ' ' . esc_attr( law_office_add_inline_css_class( 'color: ' . esc_attr( $term_color ) ) );
								}
							?>"><?php echo esc_html( $term_val ); ?></span><?php
						?></span><?php
					}
					?></div><?php
				}
			}
		}
	}
}



/* Add colors and fonts to the custom CSS
--------------------------------------------------------------- */
if ( law_office_exists_woocommerce() ) {
	require_once law_office_get_file_dir( 'plugins/woocommerce/woocommerce-extensions-style.php' );
}
