<?php
//このファイルへの直接アクセスを禁止
if ( !defined( 'ABSPATH' ) ) exit;

function haup_enqueue_swiper_post_slider_scripts() {
	//CDNからswiperのスクリプトとスタイルを読み込む
	wp_enqueue_style(
		'swiper-css',
		'https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.css',
		array(),
		haup_plugin_version()
	);
	wp_enqueue_script('swiper-js',
		'https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.js',
		array(),
		haup_plugin_version(),
		array( 
			'strategy'  => 'defer',
			'in_footer' => true, // Note: This is the default value.
		)
	);

	//スタイルシートを読み込む
	wp_enqueue_style(
	'haup-postslide-style',
	plugin_dir_url( __FILE__ ) .'style.css',
	array(),
	haup_plugin_version()
		);

	//スクリプトを読み込む
	wp_enqueue_script(
		'haup-postslide-js',
		plugin_dir_url( __FILE__ ) .'script.js',
		array('jquery'),
		haup_plugin_version(),
		array( 
			'strategy'  => 'defer',
			'in_footer' => true, // Note: This is the default value.
		)
	);
}
add_action('wp_enqueue_scripts', 'haup_enqueue_swiper_post_slider_scripts');

function haup_custom_swiper_post_slider_shortcode($atts) {
	$atts = shortcode_atts(array(
		'post_type'      => 'post',
		'category'       => '',
		'speed'          => '3000',
		'delay'          => '500',
		'per_page' => -1,
		'slides_768'    => '2', // 追加：768px以上の表示数
		'slides_1024'    => '3', // 追加：1024px以上の表示数
		'slides_1200'    => '6', // 追加：1200px以上の表示数
		'slides_1900'    => '8', // 追加：1900px以上の表示数
		'class' => '',
		'orderby' => 'rand',
		'order' => '',
	), $atts);

	// コンマ区切りの複数の投稿タイプから抽出できるようにする
	$post_types = array_map('esc_attr', array_map('trim', explode(',', $atts['post_type'])));

	/* 並び順と並び替えのパラメーター */
	/* https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters */
	$args = array(
		'post_type'      => $post_types,
		'posts_per_page' => intval($atts['per_page']),
		'orderby'        => esc_html($atts['orderby']),
		'order' => esc_html($atts['order']),
	);

	if (!empty($atts['category'])) {
		$args['category_name'] = esc_attr($atts['category']);
	}

	if (!empty($atts['class'])) {
		$slider_id = esc_attr($atts['class']);
	}else{
		$slider_id = '';
	}

	$query = new WP_Query($args);
	if (!$query->have_posts()) return '';

ob_start();
?>
<div class="ha-slider-container   <?php echo esc_html($slider_id); ?>">
	<div class="swiper ha-post-swiper" data-speed="<?php echo esc_attr($atts['speed']); ?>" data-delay="<?php echo esc_attr($atts['delay']); ?>" data-slides-1200="<?php echo esc_attr($atts['slides_1200']); ?>" data-slides-1900="<?php echo esc_attr($atts['slides_1900']); ?>">
		<div class="swiper-wrapper">
			<?php while ($query->have_posts()) : $query->the_post(); ?>
				<div class="swiper-slide">
					<div class="ha-slider-img">
						<a href="<?php echo esc_url(get_permalink()); ?>"><?php the_post_thumbnail('medium'); ?></a>
					</div>
					<div class="ha-slider-content">
						<p class="ha-slider-date"><a href="<?php echo esc_url(get_permalink()); ?>"><?php echo esc_html(get_the_date()); ?></a></p> 
						<h3 class="ha-slider-title"><a href="<?php echo esc_url(get_permalink()); ?>"><?php echo esc_html(get_the_title()); ?></a></h3>
						<div class="ha-slider-excerpt"><a href="<?php echo esc_url(get_permalink()); ?>"><?php echo esc_html(wp_trim_words(get_the_excerpt(), 25, '...')); ?></a></div>
					</div>
				</div>
			<?php endwhile; ?>
		</div>
		<div class="swiper-button-prev ha-swiper-button-prev"></div>
		<div class="swiper-button-next ha-swiper-button-next"></div>
	</div>
</div>
<?php

	wp_reset_postdata();

	return ob_get_clean();
}
add_shortcode('ha-post-slider', 'haup_custom_swiper_post_slider_shortcode');