<?php
/**
 * Testimonials custom post type (Ultimate Bundle).
 *
 * @package EasySite_Pro
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Register testimonials CPT.
 */
function easysite_register_testimonials_cpt() {
	register_post_type( 'easysite_testimonial', array(
		'labels' => array(
			'name'          => __( 'Testimonials', 'easysite-pro' ),
			'singular_name' => __( 'Testimonial', 'easysite-pro' ),
			'add_new_item'  => __( 'Add Testimonial', 'easysite-pro' ),
		),
		'public'       => false,
		'show_ui'      => true,
		'show_in_menu' => 'easysite-setup',
		'menu_icon'    => 'dashicons-format-quote',
		'supports'     => array( 'title', 'editor', 'thumbnail' ),
	) );
}
add_action( 'init', 'easysite_register_testimonials_cpt' );

/**
 * Meta box for testimonial details.
 */
function easysite_testimonial_meta_box() {
	add_meta_box( 'easysite_testimonial_details', __( 'Details', 'easysite-pro' ), 'easysite_testimonial_meta_render', 'easysite_testimonial', 'side' );
}
add_action( 'add_meta_boxes', 'easysite_testimonial_meta_box' );

/**
 * Render testimonial meta box.
 *
 * @param WP_Post $post Post.
 */
function easysite_testimonial_meta_render( $post ) {
	wp_nonce_field( 'easysite_testimonial_save', 'easysite_testimonial_nonce' );
	$role   = get_post_meta( $post->ID, '_easysite_role', true );
	$rating = get_post_meta( $post->ID, '_easysite_rating', true ) ?: 5;
	?>
	<p>
		<label for="easysite_role"><?php esc_html_e( 'Role / Company', 'easysite-pro' ); ?></label>
		<input type="text" class="widefat" id="easysite_role" name="easysite_role" value="<?php echo esc_attr( $role ); ?>" />
	</p>
	<p>
		<label for="easysite_rating"><?php esc_html_e( 'Rating (1–5)', 'easysite-pro' ); ?></label>
		<input type="number" min="1" max="5" id="easysite_rating" name="easysite_rating" value="<?php echo esc_attr( $rating ); ?>" />
	</p>
	<?php
}

/**
 * Save testimonial meta.
 *
 * @param int $post_id Post ID.
 */
function easysite_save_testimonial_meta( $post_id ) {
	if ( ! isset( $_POST['easysite_testimonial_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['easysite_testimonial_nonce'] ) ), 'easysite_testimonial_save' ) ) {
		return;
	}
	if ( isset( $_POST['easysite_role'] ) ) {
		update_post_meta( $post_id, '_easysite_role', sanitize_text_field( wp_unslash( $_POST['easysite_role'] ) ) );
	}
	if ( isset( $_POST['easysite_rating'] ) ) {
		update_post_meta( $post_id, '_easysite_rating', absint( $_POST['easysite_rating'] ) );
	}
}
add_action( 'save_post_easysite_testimonial', 'easysite_save_testimonial_meta' );

/**
 * Display testimonials grid/slider.
 *
 * @param array $args Query args.
 */
function easysite_display_testimonials( $args = array() ) {
	$query = new WP_Query( wp_parse_args( $args, array(
		'post_type'      => 'easysite_testimonial',
		'posts_per_page' => 6,
		'post_status'    => 'publish',
	) ) );

	if ( ! $query->have_posts() ) {
		easysite_display_basic_testimonials();
		return;
	}

	$layout = easysite_get_mod( 'easysite_testimonials_layout', 'grid' );
	?>
	<section class="easysite-section easysite-testimonials easysite-testimonials-<?php echo esc_attr( $layout ); ?>">
		<div class="easysite-container">
			<h2 class="easysite-section-title"><?php echo esc_html( easysite_get_mod( 'easysite_testimonials_title', __( 'What Our Customers Say', 'easysite-pro' ) ) ); ?></h2>
			<div class="easysite-testimonials-grid">
				<?php
				while ( $query->have_posts() ) :
					$query->the_post();
					$rating = (int) get_post_meta( get_the_ID(), '_easysite_rating', true );
					$role   = get_post_meta( get_the_ID(), '_easysite_role', true );
					?>
					<blockquote class="easysite-testimonial-card">
						<?php if ( has_post_thumbnail() ) : ?>
							<div class="easysite-testimonial-photo"><?php the_post_thumbnail( 'thumbnail' ); ?></div>
						<?php endif; ?>
						<div class="easysite-stars" aria-label="<?php echo esc_attr( sprintf( __( '%d out of 5 stars', 'easysite-pro' ), $rating ) ); ?>">
							<?php echo str_repeat( '★', $rating ) . str_repeat( '☆', 5 - $rating ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
						</div>
						<p class="easysite-quote"><?php echo wp_kses_post( wpautop( get_post_field( 'post_content', get_the_ID() ) ) ); ?></p>
						<cite><?php the_title(); ?><?php echo $role ? ' — ' . esc_html( $role ) : ''; ?></cite>
					</blockquote>
				<?php endwhile; ?>
			</div>
		</div>
	</section>
	<?php
	wp_reset_postdata();
}
