<?php

/**
 * Registers 'testimonials' post type 
 *
 * This function registers the 'testimonials' posts
 * type which is used for the testimonials posts.
 *
 * @function testimonial_post_type()
 *
 * @uses register_post_type()
 *
 * @param N/A
 *
 * @return N/A
 *
 * @since 1.3.9 
 *
 */
function testimonial_post_type() {
	
	$labels = array(
		'name' 				 => _x( 'Testimonials', 'post type general name' ),
		'singular_name' 	 => _x( 'Testimonial', 'post type singular name' ),
		'menu_name' 		 => __( 'Testimonials' ),
		'add_new' 			 => _x( 'Add New', 'post item' ),
		'add_new_item' 		 => __( 'Add New Testimonial' ),
		'edit_item' 		 => __( 'Edit Testimonial' ),
		'new_item' 			 => __( 'New Testimonial' ),
		'view_item' 		 => __( 'View Testimonial' ),
		'search_items' 		 => __( 'Search Testimonials' ),
		'not_found' 		 => __( 'No Testimonials Found' ),
		'not_found_in_trash' => __( 'No Testimonials Found in Trash' ),
		'parent_item_colon'  => '',
	);
	register_post_type( 'testimonials', array(
		'labels' 	  => $labels,
		'public' 	  => true,
		'has_archive' => false,
		'menu_icon'   => 'dashicons-format-quote',
		'rewrite' 	  => true,
		'supports'    => array( 'title', 'editor' ),
		'show_in_rest' => true,
	) );
}
add_action( 'init', 'testimonial_post_type', 1 );



/**
 * returns the testimonials list
 *
 * This function returns the testimonials posts
 *
 * @function add_testimonials()
 *
 * @uses ow_testimonials_list()
 *
 * @param N/A
 *
 * @return (string) Testimonials list for the testimonial page
 *
 * @since 1.0.0 
 *
 */
function add_testimonials() {
	
	return ow_testimonials_list( array( 'id' => 'testimonials-list' ) );
}
add_shortcode( 'testimonials', 'add_testimonials' );



/**
 * returns the testimonials list
 *
 * This function returns the testimonials
 * posts in a list. This function can be
 * called for a testimonial slider on the
 * homepage, and is called from the
 * testimonials page by default.
 *
 * @function ow_testimonials_list()
 *
 * @uses WP_Query::the_post()
 *
 * @param (mixed) $var[ (string) id, (string) class, (integer) limit, (boolean) random ]
 *
 * @return (string) Testimonials list
 *
 * @since 1.0.14 
 *
 */
function ow_testimonials_list( $var = array() ) {
	
	$defaults = array( 'id' => '', 'class' => '', 'limit' => -1, 'random' => false );
	$var = wp_parse_args( $var, $defaults );	
	$random = ( $var['random'] == true ) ? 'rand' : 'post_date';
    $query = new WP_Query( array(
    	'post_type' 	 => 'testimonials',
        'post_status' 	 => 'publish',
		'posts_per_page' => $var['limit'],
		'orderby' 		 => $random,
    ) );
	$string = '';
	
	if ( $query->have_posts() ){
		
		while ( $query->have_posts() ){
			
			$query->the_post();
			$string .= '<li>';
			$string .= '<blockquote><p>' . get_the_excerpt() . '</p>';
			$string .= '<cite><strong>' . get_field('testimonial-name') . '</strong><br />';
			$string .= get_field('testimonial-job-title');
			
			if ( get_field('testimonial-job-title') && get_field('testimonial-company') ) {
				
				$string .= ' - ';
			}
			$string .= get_field('testimonial-company');
			$string .= '</cite>';
			$string .= '</blockquote>';
			$string .= '</li>';
		}
	}
	wp_reset_postdata();
	$id = ( $var['id'] != '' ) ? ' id="' . $var['id'] . '"' : '';
	$class = ( $var['class'] != '') ? ' class="' . $var['class'] . '"' : '';	
	return '<ul' . $id . $class . '>' . $string . '</ul>';
}