<?php

/**
 * Shortcodes
 *
 * @package Dotspice
 * @version 1.3.0
 */


/**
 * [logo] Shortcode
 */
function dotspice_logo_shortcode($atts)
{
	$atts = shortcode_atts(array(
		'option' => 'header_logo',
	), $atts);

	ob_start();
	dotspice_logo($atts['option']);
	return ob_get_clean();
}
add_shortcode('logo', 'dotspice_logo_shortcode');


/**
 * [jumbotron-title] Shortcode
 */
function dotspice_jumbotron_title_shortcode()
{
	$options_ID = false;

	if (class_exists('woocommerce') && is_shop()) {
		$options_ID = wc_get_page_id('shop');
	}

	$title      = get_field('jumbotron_title', $options_ID);
	$title_text = !empty($title['text']) ? $title['text'] : dotspice_get_page_title();
	$title_tag  = !empty($title['tag']) ? $title['tag'] : 'h2';

	return sprintf('<%s class="jumbotron__h" %s>%s</%s>', esc_attr($title_tag), dotspice_schema('title', false), $title_text, esc_attr($title_tag));
}
add_shortcode('jumbotron-title', 'dotspice_jumbotron_title_shortcode');


/**
 * [jumbotron-desc] Shortcode
 */
function dotspice_jumbotron_desc_shortcode()
{
	$options_ID = false;

	if (class_exists('woocommerce') && is_shop()) {
		$options_ID = wc_get_page_id('shop');
	}

	if (get_field('jumbotron_desc', $options_ID)) {
		return sprintf('<div class="jumbotron__p">%s</div>', get_field('jumbotron_desc', $options_ID));
	}
}
add_shortcode('jumbotron-desc', 'dotspice_jumbotron_desc_shortcode');


/**
 * [footer-copy] Shortcode
 */
function footer_copy_shortcode($atts)
{
	$atts = shortcode_atts(array(
		'title' => parse_url(get_bloginfo('url'), PHP_URL_HOST),
	), $atts);

	return sprintf(
		'&copy; %s %s | %s <a href="https://dotspice.com/" target="_blank">Dotspice.com</a>',
		date('Y'),
		$atts['title'],
		esc_html__('Designed by:', 'dotspice')
	);
}
add_shortcode('footer-copy', 'footer_copy_shortcode');


/**
 * [counter] Shortcode
 */
function counter_shortcode($atts)
{
	$atts = shortcode_atts(array(
		'numb'  => '',
		'delay' => '10',
		'time'  => '2000'
	), $atts);


	if (!empty($atts['numb'])) {

		// Enqueue scripts
		wp_enqueue_script('waypoints', 'https://cdnjs.cloudflare.com/ajax/libs/waypoints/4.0.1/jquery.waypoints.min.js', array('jquery'), '', true);
		wp_enqueue_script('countup', get_template_directory_uri() . '/js/jquery.countup.min.js', array('jquery'), '', true);

		return sprintf(
			'<span class="counter" data-counter-delay="%s" data-counter-time="%s">%s</span>',
			$atts['delay'],
			$atts['time'],
			$atts['numb']
		);
	}
}
add_shortcode('counter', 'counter_shortcode');


/**
 * [svg] Shortcode
 */
function svg_shortcode($atts)
{
	$atts = shortcode_atts(array(
		'icon'  => '',
		'size'  => false,
		'fill'  => '',
		'style' => false,
	), $atts);

	if (!empty($atts['icon'])) {
		return dotspice_get_svg_icon($atts['icon'], 'html', $atts);
	}
}
add_shortcode('svg', 'svg_shortcode');


/**
 * [blog-posts] Shortcode
 */
function blog_posts_shortcode($atts)
{
	$atts = shortcode_atts(array(
		'post_type'           => 'post',
		'orderby'             => 'date',
		'order'               => 'DESC',
		'post_status'         => 'publish',
		'posts_per_page'      => 3,
		'cat'                 => '',
		'offset'              => 0,
		'post__in'            => false,
		'post__not_in'        => false,
		'ignore_sticky_posts' => true,
	), $atts);

	$atts['post__in']     = !empty($atts['post__in']) ? wp_parse_id_list($atts['post__in']) : false;
	$atts['post__not_in'] = !empty($atts['post__not_in']) ? wp_parse_id_list($atts['post__not_in']) : false;

	$query = new WP_Query($atts);

	ob_start();
	if ($query->have_posts()) {
?>
		<div class="row blog-post__list nc-3">
			<?php
			while ($query->have_posts()) : $query->the_post();
				get_template_part('templates/content');
			endwhile;
			?>
		</div>
	<?php
	}

	wp_reset_postdata();
	return ob_get_clean();
}
add_shortcode('blog-posts', 'blog_posts_shortcode');


/**
 * [accordion] Shortcode
 */
function accordion_shortcode($atts, $content)
{
	$atts = shortcode_atts(array(
		'class' => '',
	), $atts);

	$patterns = array(
		'#^\s*</p>#',
		'#<p>\s*$#',
		'#<br\s*/?>#'
	);
	$acc_ID    = 'acc-' . dotspice_unique_ID('accordion');
	$acc_class = !empty($atts['class']) ? $atts['class'] . ' accordion' : 'accordion';

	ob_start();
	?>
	<div id="<?php echo esc_attr($acc_ID); ?>" class="<?php echo esc_attr($acc_class); ?>">
		<?php echo do_shortcode(preg_replace($patterns, '', $content)); ?>
	</div>
<?php
	return ob_get_clean();
}
add_shortcode('accordion', 'accordion_shortcode');


/**
 * [accordion-item] Shortcode
 */
function accordion_item_shortcode($atts, $content)
{
	$atts = shortcode_atts(array(
		'title' => '',
		'tag'   => 'h3',
		'open'  => false,
	), $atts);

	$acc_ID     = 'acc-' . dotspice_unique_ID('accordion', false);
	$card_ID    = sanitize_title($acc_ID . '-card-' . $atts['title']);
	$card_aria  = boolval($atts['open']) ? 'true' : 'false';
	$card_class = boolval($atts['open']) ? ' show' : '';

	ob_start();
?>
	<div class="accordion-item">
		<?php
		echo sprintf(
			'<%1$s class="accordion-header"><button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#%2$s" aria-expanded="%3$s" aria-controls="%2$s">%4$s<span class="icon"></span></button></%1$s>',
			esc_attr($atts['tag']),
			esc_attr($card_ID),
			esc_attr($card_aria),
			esc_html($atts['title'])
		);
		echo sprintf(
			'<div id="%1$s" class="accordion-collapse collapse%2$s" data-bs-parent="#%3$s"><div class="accordion-body">%4$s</div></div>',
			esc_attr($card_ID),
			esc_attr($card_class),
			esc_attr($acc_ID),
			do_shortcode(wpautop($content))
		);
		?>
	</div>
<?php
	return ob_get_clean();
}
add_shortcode('accordion-item', 'accordion_item_shortcode');
