Inventory_Presser_Slider::widget( array $args, array $instance )

Outputs the widget front-end HTML


Parameters Parameters

$args

(array) (Required)

$instance

(array) (Required)


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/widget/class-widget-inventory-slider.php

	 */
	public function widget( $args, $instance ) {
		$this->include_scripts( $instance );

		$title                 = empty( $instance['title'] ) ? '' : apply_filters( 'widget_title', $instance['title'] );
		$showcount             = empty( $instance['showcount'] ) ? 3 : $instance['showcount'];
		$showtext              = isset( $instance['showtext'] ) ? $instance['showtext'] : false;
		$featured_select_slugs = array_keys( $this->featured_select_options() );
		$featured_select       = isset( $instance['featured_select'] ) ? $instance['featured_select'] : $featured_select_slugs[0];
		$showtitle             = ( isset( $instance['cb_showtitle'] ) && 'true' === $instance['cb_showtitle'] );
		$showprice             = ( isset( $instance['cb_showprice'] ) && 'true' === $instance['cb_showprice'] );

		$inventory_ids = array();

		$get_posts_args = array(
			'fields'         => 'ids',
			'meta_query'     => array(
				array(
					'key'     => '_thumbnail_id',
					'compare' => 'EXISTS',
				),
			),
			'order'          => 'ASC',
			'orderby'        => 'rand',
			'post_type'      => INVP::POST_TYPE,
			'posts_per_page' => $showcount * 5, // get 5 sets of the number we'll show at one time.
		);

		switch ( $featured_select ) {

			case 'random':
				$get_posts_args['meta_key'] = '_thumbnail_id';
				$inventory_ids              = get_posts( apply_filters( 'invp_slider_widget_query_args', $get_posts_args ) );
				break;

			case 'newest_first':
				$get_posts_args['meta_key'] = apply_filters( 'invp_prefix_meta_key', 'last_modified' );
				$get_posts_args['orderby']  = ' STR_TO_DATE( meta1.meta_value, \'%a, %d %b %Y %T\' ) ';
				$get_posts_args['order']    = 'DESC';
				$inventory_ids              = get_posts( apply_filters( 'invp_slider_widget_query_args', $get_posts_args ) );
				break;

			// featured_only.
			// featured_priority.
			default:
				$get_posts_args['meta_query'][] = array(
					'key'   => apply_filters( 'invp_prefix_meta_key', 'featured' ),
					'value' => 1,
				);
				$inventory_ids                  = get_posts( apply_filters( 'invp_slider_widget_query_args', $get_posts_args ) );

				if ( count( $inventory_ids ) < ( $showcount * 5 ) && 'featured_priority' === $featured_select ) {
					// Get enough non-featured vehicles to fill out the number we need.
					$get_posts_args['posts_per_page'] = ( $showcount * 5 ) - ( count( $inventory_ids ) );
					if ( ! empty( $inventory_ids ) ) {
						$get_posts_args['exclude'] = $inventory_ids;
					}
					$get_posts_args['meta_query'] = array(
						array(
							'key'     => '_thumbnail_id',
							'compare' => 'EXISTS',
						),
					);

					$second_pass    = get_posts( apply_filters( 'invp_slider_widget_query_args', $get_posts_args ) );
					$inventory_ids += $second_pass;
				}
				// randomize the order, a strange choice we'll maintain
				shuffle( $inventory_ids );

				break;
		}

		if ( ! $inventory_ids ) {
			// No vehicles to show, don't output anything.
			return;
		}

		// before and after widget arguments are defined by themes.
		echo $args['before_widget'];
		if ( ! empty( $title ) ) {
			echo $args['before_title'] . $title . $args['after_title'];
		}
		echo '<div id="slider-width"></div><div id="widget_slider" class="flexslider"><ul class="slides">';

		foreach ( $inventory_ids as $inventory_id ) {
			printf(
				'<li style="position: relative"><a href="%s">%s',
				esc_url( get_the_permalink( $inventory_id ) ),
				get_the_post_thumbnail( $inventory_id, 'large' )
			);
			if ( $showtext != 'none' ) {
				printf( '<div class="flex-caption flex-caption-%s">', $showtext );
				if ( $showtitle ) {
					printf(
						'<h3>%s %s %s</h3>',
						esc_html( invp_get_the_year( $inventory_id ) ),
						esc_html( invp_get_the_make( $inventory_id ) ),
						esc_html( invp_get_the_model( $inventory_id ) )
					);
				}
				if ( $showprice ) {
					printf( '<h2>%s</h2>', esc_html( invp_get_the_price( '', $inventory_id ) ) );
				}