<?php
defined( 'ABSPATH' ) || exit;

/*-------------------------------------------------------------------------------*/
/* [ Table of contents ]
/*-------------------------------------------------------------------------------*/

	# Core
	# Archives
	# Entries
	# Single
	# Cards

/*-------------------------------------------------------------------------------*/
/* [ Core ]
/*-------------------------------------------------------------------------------*/

/**
 * Check if a given testimonial has a rating.
 *
 * @since 5.0
 */
function wpex_has_testimonial_rating( $post = null ) {
	$rating = wpex_get_testimonial_rating( $post );
	$check  = (bool) $rating;

	/**
	 * Filters whether the given testimonial has a rating.
	 *
	 * @param bool $check
	 * @param obj $post
	 * @param string $rating
	 */
	$check = (bool) apply_filters( 'wpex_has_testimonial_rating', $check, $post, $rating );

	return $check;
}

/**
 * Get testimonial rating.
 *
 * @since 5.0
 */
function wpex_get_testimonial_rating( $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return '';

	}

	$rating = get_post_meta( $post->ID, 'wpex_post_rating', true );

	if ( $rating ) {
		$rating = wpex_get_star_rating( $rating );
	}

	/**
	 * Filters the testimonial star rating html.
	 *
	 * @param string $star_rating
	 * @param obj $post
	 */
	$rating = apply_filters( 'wpex_testimonial_rating', $rating, $post );

	return $rating;
}

/**
 * Display testimonial rating.
 *
 * @since 5.0
 */
function wpex_testimonial_rating( $post = null ) {
	echo wpex_get_testimonial_rating( $post );
}

/**
 * Check if a given testimonial has an author.
 *
 * @since 5.0
 */
function wpex_has_testimonial_author( $post = null ) {
	$author = wpex_get_testimonial_author( $post );
	$check  = (bool) $author;

	/**
	 * Checks whether the testimonial has an author.
	 *
	 * @param bool $check
	 * @param obj $post
	 * @param string $author
	 */
	$check = (bool) apply_filters( 'wpex_has_testimonial_author', $check, $post, $author );

	return $check;
}

/**
 * Get testimonial author.
 *
 * @since 5.0
 */
function wpex_get_testimonial_author( $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return '';
	}

	$author = get_post_meta( $post->ID, 'wpex_testimonial_author', true ) ;

	/**
	 * Filters the testimonial author name.
	 *
	 * @param string $author
	 * @param obj $post
	 */
	$author = (string) apply_filters( 'wpex_testimonial_author', $author, $post );

	return $author;
}

/**
 * Check if a given testimonial has a company set.
 *
 * @since 5.0
 */
function wpex_has_testimonial_company( $post = null ) {
	$company = wpex_get_testimonial_company( $post );
	$check   = (bool) $company;

	/**
	 * Filters whether the testimonial has a company.
	 *
	 * @param bool $check
	 * @param obj $post
	 * @param string $company
	 */
	$check = (bool) apply_filters( 'wpex_has_testimonial_company', $check, $post, $company );

	return $check;
}

/**
 * Get testimonial company.
 *
 * @since 5.0
 */
function wpex_get_testimonial_company( $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return '';
	}

	$company = get_post_meta( $post->ID, 'wpex_testimonial_company', true );

	/**
	 * Filters the testimonial company name.
	 *
	 * @param string $company
	 * @param obj $post
	 */
	$company = apply_filters( 'wpex_testimonial_company', $company );

	return $company;
}

/**
 * Check if a given testimonial has a company url set.
 *
 * @since 5.0
 */
function wpex_has_testimonial_company_link( $post = null ) {
	$company = wpex_get_testimonial_company_url( $post );
	$check   = (bool) $company;

	/**
	 * Filters whether the testimonial has a company link.
	 *
	 * @param bool $check
	 */
	$check = (bool) apply_filters( 'wpex_has_testimonial_company_link', $check, $post, $company );

	return $check;
}

/**
 * Get testimonial company url.
 *
 * @since 5.0
 */
function wpex_get_testimonial_company_url( $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return '';
	}

	$url = get_post_meta( $post->ID, 'wpex_testimonial_url', true );

	/**
	 * Filters the testimonial company url.
	 *
	 * @param string $url
	 * @param obj $post
	 */
	$url = apply_filters( 'wpex_testimonial_company_url', $url, $post );

	return esc_url( $url );
}

/**
 * Get testimonial company url target.
 *
 * @since 5.0
 */
function wpex_get_testimonial_company_link_target() {
	$target =  '_blank';

	/**
	 * Filters the testimonial company link target.
	 *
	 * @param string $target
	 */
	$target = (string) apply_filters( 'wpex_testimonial_company_link_target', $target );

	return $target;
}

/*-------------------------------------------------------------------------------*/
/* [ Archives ]
/*-------------------------------------------------------------------------------*/

/**
 * Returns testimonials archive grid style.
 *
 * @since 5.0
 */
function wpex_testimonials_archive_grid_style() {
	$style = get_theme_mod( 'testimonials_archive_grid_style' ) ?: 'fit-rows';

	/**
	 * Filters the testimonials archive grid style.
	 *
	 * @param string $style.
	 */
	$style = apply_filters( 'wpex_testimonials_archive_grid_style', $style );

	return $style;
}

/*-------------------------------------------------------------------------------*/
/* [ Entries ]
/*-------------------------------------------------------------------------------*/

/**
 * Testimonials entry class.
 *
 * @since 5.0
 */
function wpex_testimonials_entry_class() {
	$grid_style = wpex_testimonials_archive_grid_style();
	$columns    = wpex_testimonials_archive_columns();

	$class = array(
		'testimonial-entry',
	);

	// Grid classes.
	if ( 'singular' !== wpex_get_loop_instance() ) {

		// Add grid column class.
		if ( $col_class = wpex_row_column_width_class( $columns ) ) {
			$class[] = 'col';
			$class[] = $col_class;
		}

		// Add counter class.
		$loop_counter = wpex_get_loop_counter();

		if ( $loop_counter ) {
			$class[] = 'col-' . sanitize_html_class( $loop_counter );
		}

		if ( 'masonry' === $grid_style ) {
			$class[] = 'wpex-masonry-col';
		}

	}

	/**
	 * Filters the testimonial entry class.
	 *
	 * @param array $class
	 */
	$class = (array) apply_filters( 'wpex_testimonials_entry_class', $class );

	if ( $class ) {
		echo 'class="' . esc_attr( implode( ' ', $class ) ) . '"';
	}
}

/**
 * Testimonials entry content class.
 *
 * @since 5.0
 */
function wpex_testimonials_entry_content_class() {
	$class = array(
		'testimonial-entry-content',
		'wpex-relative', // for caret
		'wpex-boxed',
		'wpex-border-0',
		'wpex-clr',
	);

	/**
	 * Filters the testimonial entry content class.
	 *
	 * @param array $class
	 */
	$class = (array) apply_filters( 'wpex_testimonials_entry_content_class', $class );

	if ( $class ) {
		echo 'class="' . esc_attr( implode( ' ', $class ) ) . '"';
	}
}

/**
 * Testimonials entry title class.
 *
 * @since 5.0
 */
function wpex_testimonials_entry_title_class() {
	$class = array(
		'testimonial-entry-title',
		'entry-title',
		'wpex-mb-10',
	);

	/**
	 * Filters the testimonial entry title class.
	 *
	 * @param array $class
	 */
	$class = (array) apply_filters( 'wpex_testimonials_entry_title_class', $class );

	if ( $class ) {
		echo 'class="' . esc_attr( implode( ' ', $class ) ) . '"';
	}
}

/**
 * Testimonials entry bottom class.
 *
 * @since 5.0
 */
function wpex_testimonials_entry_bottom_class() {
	$class = array(
		'testimonial-entry-bottom',
		'wpex-flex',
		'wpex-flex-wrap',
		'wpex-mt-20',
	);

	/**
	 * Filters the testimonial entry bottom class.
	 *
	 * @param array $class
	 */
	$class = (array) apply_filters( 'wpex_testimonials_entry_bottom_class', $class );

	if ( $class ) {
		echo 'class="' . esc_attr( implode( ' ', $class ) ) . '"';
	}
}

/**
 * Testimonials entry media class.
 *
 * @since 5.0
 */
function wpex_testimonials_entry_media_class( $custom_dims = false ) {
	$class = array(
		'testimonial-entry-thumb',
		$custom_dims ? 'custom-dims' : 'default-dims',
		'wpex-flex-shrink-0',
		'wpex-mr-20',
	);

	/**
	 * Filters the testimonial entry media class.
	 *
	 * @param array $class
	 * @param bool $custom_dims
	 */
	$class = (array) apply_filters( 'wpex_testimonials_entry_media_class', $class, $custom_dims );

	if ( $class ) {
		echo 'class="' . esc_attr( implode( ' ', $class ) ) . '"';
	}
}

/**
 * Testimonials entry title class.
 *
 * @since 5.0
 */
function wpex_testimonials_entry_meta_class() {
	$class = array(
		'testimonial-entry-meta',
		'wpex-flex-grow',
	);

	/**
	 * Filters the testimonial entry meta class.
	 *
	 * @param array $class
	 */
	$class = (array) apply_filters( 'wpex_testimonials_entry_meta_class', $class );

	if ( $class ) {
		echo 'class="' . esc_attr( implode( ' ', $class ) ) . '"';
	}
}

/**
 * Testimonials entry author class.
 *
 * @since 5.0
 */
function wpex_testimonials_entry_author_class() {
	$class = array(
		'testimonial-entry-author',
		'entry-title',
		'wpex-m-0',
	);

	/**
	 * Filters the testimonial entry author class.
	 *
	 * @param array $class
	 */
	$class = (array) apply_filters( 'wpex_testimonials_entry_author_class', $class );

	if ( $class ) {
		echo 'class="' . esc_attr( implode( ' ', $class ) ) . '"';
	}
}

/**
 * Testimonials entry rating class.
 *
 * @since 5.0
 */
function wpex_testimonials_entry_rating_class() {
	$class = array(
		'testimonial-entry-rating',
	);

	/**
	 * Filters the testimonial rating class.
	 *
	 * @param array $class
	 */
	$class = (array) apply_filters( 'wpex_testimonials_entry_rating_class', $class );

	if ( $class ) {
		echo 'class="' . esc_attr( implode( ' ', $class ) ) . '"';
	}
}

/**
 * Testimonial entry company class.
 *
 * @since 5.0
 */
function wpex_testimonials_entry_company_class() {
	$class = array(
		'testimonial-entry-company',
		'wpex-text-3',
		'wpex-hover-text-3', // for links.
	);

	/**
	 * Filters the testimonial company class.
	 *
	 * @param array $class
	 */
	$class = (array) apply_filters( 'wpex_testimonials_entry_company_class', $class );

	if ( $class ) {
		echo 'class="' . esc_attr( implode( ' ', $class ) ) . '"';
	}
}

/**
 * Testimonial entry link target.
 *
 * @since 5.0
 */
function wpex_testimonials_entry_company_link_target() {
	$target = wpex_get_testimonial_company_link_target();
	if ( 'blank' === $target || '_blank' === $target ) {
		echo ' target="_blank" rel="noopener noreferrer"';
	}
}

/**
 * Display testimonial thumbnail
 *
 * @since 5.0
 */
function wpex_testimonials_entry_thumbnail( $args = array() ) {
	echo wpex_get_testimonials_entry_thumbnail( $args );
}

/**
 * Returns correct thumbnail HTML for the testimonials entries.
 *
 * @since 2.0.0
 */
function wpex_get_testimonials_entry_thumbnail( $args = array() ) {
	$classes = array(
		'testimonials-entry-img',
		'wpex-align-middle',
		'wpex-round',
		'wpex-border',
		'wpex-border-solid',
		'wpex-border-main',
	);

	/**
	 * Filters the testimonial entry thumbnail class.
	 *
	 * @param array $class
	 */
	$classes = apply_filters( 'wpex_testimonials_entry_thumbnail_class', $classes );

	$defaults = array(
		'size'  => 'testimonials_entry',
		'class' => $classes,
	);

	$args = wp_parse_args( $args, $defaults );

	return wpex_get_post_thumbnail( $args );
}

/**
 * Returns testimonials archive columns.
 *
 * @since 2.0.0
 */
function wpex_testimonials_archive_columns() {
	return get_theme_mod( 'testimonials_entry_columns', '4' );
}

/**
 * Returns the testimonials loop top class.
 *
 * @since 5.0
 */
function wpex_testimonials_loop_top_class() {
	$classes = wpex_get_testimonials_wrap_classes();

	/**
	 * Filters the testimonial loop top class.
	 *
	 * @param array $class
	 */
	$classes = (array) apply_filters( 'wpex_testimonials_loop_top_class', wpex_get_testimonials_wrap_classes() );

	if ( $classes ) {
		echo 'class="' . esc_attr( implode( ' ', $classes ) ) . '"';
	}
}

/**
 * Returns correct classes for the testimonials archive wrap.
 *
 * @since 2.0.0
 */
function wpex_get_testimonials_wrap_classes() {
	$classes = array(
		'wpex-row',
	);

	// Get grid style
	$grid_style = wpex_testimonials_archive_grid_style();

	// This is a good spot to enqueue grid scripts
	if ( 'masonry' === $grid_style || 'no-margins' === $grid_style ) {
		$classes[] = 'wpex-masonry-grid';
		wpex_enqueue_isotope_scripts();
	}

	// Get grid style
	if ( 'masonry' === get_theme_mod( 'testimonials_archive_grid_style', 'fit-rows' ) ) {
		$classes[] = 'testimonials-masonry';
	}

	// Add gap
	if ( $gap = get_theme_mod( 'testimonials_archive_grid_gap' ) ) {
		$classes[] = 'gap-' . sanitize_html_class( $gap );
	}

	// Clear floats
	$classes[] = 'wpex-clr';

	// Sanitize
	$classes = array_map( 'esc_attr', $classes );

	/**
	 * Filters the testimonial wrap class.
	 *
	 * @param array $class
	 */
	$classes = apply_filters( 'wpex_testimonials_wrap_classes', $classes );

	// Turn array into string
	$classes = implode( ' ', $classes );

	return $classes;
}

/*-------------------------------------------------------------------------------*/
/* [ Single ]
/*-------------------------------------------------------------------------------*/

/**
 * Testimonials single layout.
 *
 * @since 5.0
 */
function wpex_get_testimonials_single_layout() {
	return apply_filters( 'wpex_testimonials_single_layout', get_theme_mod( 'testimonial_post_style', 'blockquote' ) );
}

/**
 * Testimonials single content class.
 *
 * @since 5.0
 */
function wpex_testimonials_single_content_class() {
	$class = array(
		'single-content',
		'entry',
	);

	if ( ! wpex_has_post_wpbakery_content( get_the_ID() ) ) {
		$class[] = 'wpex-mb-40';
	}

	$class[] = 'wpex-clr';

	/**
	 * Filters the testimonial single content class.
	 *
	 * @param array $class
	 */
	$class = (array) apply_filters( 'wpex_testimonials_single_content_class', $class );

	if ( $class ) {
		echo 'class="' . esc_attr( implode( ' ', $class ) ) . '"';
	}
}

/**
 * Testimonial single comments class.
 *
 * @since 5.0
 */
function wpex_testimonials_single_comments_class() {
	$class = array(
		'single-comments',
		'wpex-mb-40',
		'wpex-clr',
	);

	/**
	 * Filters the testimonial single comments class.
	 *
	 * @param array $class
	 */
	$class = (array) apply_filters( 'wpex_testimonials_single_comments_class', $class );

	if ( $class ) {
		echo 'class="' . esc_attr( implode( ' ', $class ) ) . '"';
	}
}

/*-------------------------------------------------------------------------------*/
/* [ Cards ]
/*-------------------------------------------------------------------------------*/

/**
 * Testimonial Card Entry.
 *
 * @since 5.0
 */
function wpex_testimonials_entry_card() {
	$card_style = get_theme_mod( 'testimonials_entry_card_style' );
	$instance = wpex_get_loop_instance();

	if ( 'related' === $instance ) {
		$card_style = wpex_get_mod( 'testimonials_related_entry_card_style', $card_style, true );
	} else {
		$term_meta_check = wpex_get_term_meta( '', 'wpex_entry_card_style', true );
		if ( ! empty( $term_meta_check ) ) {
			$card_style = $term_meta_check;
		}
	}

	/**
	 * Filters the testimonial entry card style.
	 *
	 * @param string $style
	 */
	$card_style = (string) apply_filters( 'wpex_testimonials_entry_card_style', $card_style );

	if ( ! $card_style ) {
		return false;
	}

	$args = array(
		'style'          => $card_style,
		'post_id'        => get_the_ID(),
		'thumbnail_size' => 'testimonials_entry',
		'excerpt_length' => get_theme_mod( 'testimonials_entry_excerpt_length', '-1' ),
	);

	/**
	 * Filters the testimonial entry card args.
	 *
	 * @param array $args
	 */
	$args = (array) apply_filters( 'wpex_testimonials_entry_card_args', $args );

	wpex_card( $args );

	return true;
}