<?php
/**
 * Shortcode file.
 *
 * @author Kushang Tailor
 * @package Product Category
 */

/**
 * Shortcode function.
 *
 * @param array $atts Attributes.
 * @return $atts
 * @since 1.0.0
 */
function product_categories_widget( $atts ) {

	global $woocommerce_loop;

	$atts = shortcode_atts(
		array(
			'number'         => null,
			'orderby'        => 'name',
			'order'          => 'ASC',
			'columns'        => '4',
			'hide_empty'     => 1,
			'parent'         => '',
			'ids'            => '',
			'description'    => true,
			'cat_image'      => true,
			'font-size'      => '',
			'font-weight'    => '',
			'font-family'    => '',
			'letter-spacing' => '',
			'color'          => '',
			'style'          => '',
			'pagination'     => '',
		),
		$atts,
		'PCW'
	);

	$ids = array_filter( array_map( 'absint', explode( ',', $atts['ids'] ) ) );

	$page     = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
	$per_page = ! empty( $atts['number'] ) ? absint( $atts['number'] ) : 50;
	$offset   = ( $page > 0 ) ? $per_page * ( $page - 1 ) : 1;

	$columns    = min( 6, max( 1, absint( $atts['columns'] ) ) );
	$parent     = '' !== $atts['parent'] ? absint( $atts['parent'] ) : '';
	$hide_empty = filter_var( $atts['hide_empty'], FILTER_VALIDATE_BOOLEAN ) ? 1 : false;
	$show_desc  = filter_var( $atts['description'], FILTER_VALIDATE_BOOLEAN );
	$show_image = filter_var( $atts['cat_image'], FILTER_VALIDATE_BOOLEAN );

	$args = array(
		'taxonomy'   => 'product_cat',
		'number'     => $per_page,
		'offset'     => $offset,
		'orderby'    => $atts['orderby'],
		'order'      => $atts['order'],
		'hide_empty' => $hide_empty,
		'include'    => $ids,
		'pad_counts' => true,
		'child_of'   => $parent,
	);

	$product_categories = get_terms( $args );
	if ( is_wp_error( $product_categories ) || empty( $product_categories ) ) {
		return '';
	}

	if ( '' !== $parent ) {
		$product_categories = wp_list_filter( $product_categories, array( 'parent' => $parent ) );
	}

	if ( $hide_empty ) {
		foreach ( $product_categories as $key => $category ) {
			if ( 0 === $category->count ) {
				unset( $product_categories[ $key ] );
			}
		}
	}

	if ( $atts['number'] ) {
		$product_categories = array_slice( $product_categories, 0, $atts['number'] );
	}

	$woocommerce_loop['columns'] = $columns;
	$placeholder_img             = plugins_url( 'admin/images/placeholder.png', PCW_PLUGIN_PATH . 'product-category.php' );

	ob_start();

	?>
	<div class="product-container <?php echo esc_attr( $atts['style'] ); ?> columns-<?php echo esc_attr( $columns ); ?>">
	<?php
	foreach ( $product_categories as $category ) {
		$cat_link = get_term_link( $category, 'product_cat' );
		if ( is_wp_error( $cat_link ) ) {
			$cat_link = '#';
		}
		?>
		<div class="product-cats">
			<?php
			if ( $show_image ) {
				?>
				<div class="pro-img">
					<a href="<?php echo esc_url( $cat_link ); ?>">
						<?php
						$thumbnail_id = get_term_meta( $category->term_id, 'thumbnail_id', true );
						$image        = wp_get_attachment_url( $thumbnail_id );
						$image_alt    = $thumbnail_id ? get_the_title( $thumbnail_id ) : $category->name;

						if ( $image ) {
							echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $image_alt ) . '" />';
						} else {
							echo '<img src="' . esc_url( $placeholder_img ) . '" alt="' . esc_attr( $image_alt ) . '" class="pcw-placeholder"/>';
						}
						?>
						</a>
				</div>
				<?php
			}
			?>
			<div class="pro-info" style="letter-spacing: <?php echo esc_attr( $atts['letter-spacing'] ); ?>;">

				<?php
				echo '<div class="pro_name">' . esc_html( $category->name ) . ' (' . esc_html( $category->count ) . ') </div>';
				if ( $show_desc ) {
					echo '<div class="pro_desc">' . esc_html( $category->description ) . '</div>';
				}
				?>
			</div>
		</div>
		<?php
	}
	?>
	</div>
	<style type="text/css">
		.pro_name{
			font-size: <?php echo esc_attr( $atts['font-size'] ); ?>;
			font-weight: <?php echo esc_attr( $atts['font-weight'] ); ?>;
			font-family: <?php echo esc_attr( $atts['font-family'] ); ?>;
			color: <?php echo esc_attr( $atts['color'] ); ?>;
		}
	</style>
	<?php
	if ( 'hide' !== $atts['pagination'] ) {
		?>
		<nav class="woocommerce-pagination pcw-product-cat">
			<?php
			$big   = 999999999;
			$total = wp_count_terms( 'product_cat' );
			if ( ! is_wp_error( $total ) && $per_page ) {
				echo wp_kses_post(
					paginate_links(
						array(
							'base'      => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
							'format'    => '',
							'prev_text' => __( '&#x2190;', 'product-category' ),
							'next_text' => __( '&#x2192;', 'product-category' ),
							'type'      => 'list',
							'total'     => ceil( $total / $per_page ),
							'current'   => max( 1, get_query_var( 'paged' ) ),
						)
					)
				);
			}
			?>
		</nav>
		<?php
	}

	if ( function_exists( 'wc_reset_loop' ) ) {
		wc_reset_loop();
	}

	return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
add_shortcode( 'PCW', 'product_categories_widget' );
add_shortcode( 'pcw', 'product_categories_widget' );
