Inventory_Presser_Shortcode_Slider::content( array $atts )

Creates the HTML content of the shortcode


Parameters Parameters

$atts

(array) (Required)


Top ↑

Return Return

(string) HTML that renders a vehicle photo flexslider


Top ↑

Source Source

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

	public function content( $atts ) {
		$atts = shortcode_atts(
			array(
				'captions'  => 'true',
				'make'      => '',
				'model'     => '',
				'type'      => '',
				'condition' => '',
				'orderby'   => 'rand',
				'order'     => 'ASC',
				'showcount' => 3, // How many vehicles are shown at one time?
			),
			$atts,
			'invp_inventory_slider'
		); // Use shortcode_atts_inventory_slider to filter the incoming attributes.

		// Parse boolean values to make life easy on users.
		$atts['captions'] = filter_var( $atts['captions'], FILTER_VALIDATE_BOOLEAN );

		// Get the vehicle IDs and loop over them.
		$inventory_ids = self::get_vehicle_IDs( $atts );
		if ( empty( $inventory_ids ) ) {
			return '';
		}

		if ( ! wp_script_is( 'invp-slider', 'registered' ) ) {
			Inventory_Presser_Plugin::include_scripts_and_styles();
		}
		// Need flexslider for this content.
		wp_enqueue_style( 'flexslider' );
		wp_enqueue_style( 'invp-flexslider' );
		wp_enqueue_style( 'invp-slider' );

		// Generate unique IDs for this slider instance.
		++self::$slider_counter;
		$slider_id        = 'invp-slider-' . self::$slider_counter;
		$slider_width_id  = 'slider-width-' . self::$slider_counter;
		$widget_slider_id = 'widget_slider-' . self::$slider_counter;

		// Enqueue the script first so inline scripts can be attached.
		wp_enqueue_script( 'invp-slider' );

		// Store slider configuration in a global object for JavaScript.
		// Initialize the object only on the first shortcode instance.
		if ( 1 === self::$slider_counter ) {
			wp_add_inline_script(
				'invp-slider',
				'if (typeof window.invpSliderConfigs === "undefined") { window.invpSliderConfigs = {}; }',
				'before'
			);
		}

		// Add this slider's configuration.
		wp_add_inline_script(
			'invp-slider',
			'window.invpSliderConfigs["' . esc_js( $slider_id ) . '"] = ' . wp_json_encode(
				array(
					'showcount' => $atts['showcount'],
					'sliderId'  => $slider_id,
				)
			) . ';',
			'before'
		);

		$flex_html = '<div class="widget__invp_slick" data-slider-id="' . esc_attr( $slider_id ) . '">'
		. '<div id="' . esc_attr( $slider_width_id ) . '" class="slider-width"></div>'
		. '<div id="' . esc_attr( $widget_slider_id ) . '" class="flexslider flex-native">'
		. '<ul class="slides">';

		foreach ( $inventory_ids as $inventory_id ) {
			$flex_html .= sprintf(
				'<li><a class="flex-link" href="%s">'
				. '%s',
				esc_url( get_the_permalink( $inventory_id ) ),
				get_the_post_thumbnail( $inventory_id, 'large' )
			);

			if ( $atts['captions'] ) {
				$flex_html .= sprintf(
					'<p class="flex-caption">%s</p>',
					esc_html( get_the_title( $inventory_id ) )
				);
			}

			$flex_html .= '</a></li>';
		}

		return $flex_html . '</ul></div></div>';
	}