Inventory_Presser_Grid::content( array $args )

Creates HTML that renders the widget front-end.


Parameters Parameters

$args

(array) (Required) The widget’s settings.


Top ↑

Return Return

(string) The HTML that creates the widget front-end


Top ↑

Source Source

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

	public function content( $args ) {

		// Need the stylesheet for this content.
		wp_enqueue_style( 'invp-grid' );

		$default_args = array(
			'columns'                 => 5,     // In how many columns should the tiles be arranged?
			'featured_only'           => false, // Include only featured vehicles?
			'limit'                   => 15,    // How many vehicles in the grid maximum?
			'make'                    => '',    // Only show vehicles of this make.
			'model'                   => '',    // Only show vehicles of this model.
			'newest_first'            => false, // Sort the most recently modified vehicles first?
			'priced_first'            => false, // Sort the vehicles with prices first?
			'show_button'             => false, // Display a "Full Inventory" button below the grid?
			'show_captions'           => false, // Show text captions near each photo?
			'show_odometers'          => false, // Show odometers in the captions?
			'show_prices'             => false, // Show prices in the captions?
			'suppress_call_for_price' => false, // When the price setting is {$Price}, this prevents "Call for price" in the grid.
		);
		$args         = wp_parse_args( $args, $default_args );

		// Make sure the limit is not zero or empty string.
		if ( empty( $args['limit'] ) ) {
			$args['limit'] = apply_filters( 'invp_query_limit', 1000, __METHOD__ );
		}

		$post_args = array(
			'posts_per_page' => $args['limit'],
			'post_type'      => INVP::POST_TYPE,
			'meta_query'     => array(
				array(
					'key'     => '_thumbnail_id',
					'compare' => 'EXISTS',
				),
			),
			'fields'         => 'ids',
			'orderby'        => 'rand', // Defaults to random order.
			'order'          => 'ASC',
			'tax_query'      => array(),
		);

		if ( $args['newest_first'] ) {
			// Change the order to last_modified date.
			$post_args['meta_key'] = apply_filters( 'invp_prefix_meta_key', 'last_modified' );
			global $wpdb;
			$post_args['orderby'] = "STR_TO_DATE( {$wpdb->postmeta}.meta_value, '%a, %d %b %Y %T' )";
			$post_args['order']   = 'DESC';
		}

		// Do we want featured vehicles only?
		if ( $args['featured_only'] ) {
			$post_args['meta_query'][] = array(
				'key'   => apply_filters( 'invp_prefix_meta_key', 'featured' ),
				'value' => '1',
			);
		}

		// Are we only showing 1 make?
		if ( ! empty( $args['make'] ) ) {
			$post_args['tax_query'][] = array(
				'taxonomy' => 'make',
				'field'    => 'slug',
				'terms'    => $args['make'],
			);
		}

		// Are we only showing 1 model?
		if ( ! empty( $args['model'] ) ) {
			$post_args['meta_query'][] = array(
				'key'     => apply_filters( 'invp_prefix_meta_key', 'model' ),
				'value'   => $args['model'],
				'compare' => 'LIKE',
			);
		}

		// Should we exclude sold vehicles?
		$plugin_settings = INVP::settings();
		if ( isset( $plugin_settings['include_sold_vehicles'] ) && ! $plugin_settings['include_sold_vehicles'] ) {
			$post_args['tax_query'][] = Inventory_Presser_Taxonomies::tax_query_exclude_sold();
		}

		$inventory_ids = get_posts( $post_args );
		if ( empty( $inventory_ids ) ) {
			return;
		}

		// Do we want priced vehicles first?
		if ( $args['priced_first'] ) {
			// Yes. Scan the results for vehicles without prices.
			$ids_without_prices = array();
			foreach ( $inventory_ids as $inventory_id ) {
				if ( empty( get_post_meta( $inventory_id, apply_filters( 'invp_prefix_meta_key', 'price' ) ) ) ) {
					$ids_without_prices[] = $inventory_id;
				}
			}
			if ( ! empty( $ids_without_prices ) ) {
				// Remove IDs and then add them to the end of the array.
				$inventory_ids = array_merge( array_diff( $inventory_ids, $ids_without_prices ), $ids_without_prices );
			}
		}

		$grid_html = sprintf(
			'<div class="invp-grid"><ul class="grid-slides columns-%s">',
			$args['columns']
		);

		foreach ( $inventory_ids as $inventory_id ) {

			$grid_html .= sprintf(
				'<li><a class="grid-link" href="%s"><div class="grid-image" style="background-image: url(%s);"></div>',
				get_the_permalink( $inventory_id ),
				invp_get_the_photo_url( 'large', $inventory_id )
			);

			if ( $args['show_captions'] ) {
				$grid_html .= '<p class="grid-caption">' . get_the_title( $inventory_id );

				if ( $args['show_odometers'] ) {
					$grid_html .= sprintf(
						'<span class="grid-odometer">%s</span>',
						invp_get_the_odometer( ' ' . apply_filters( 'invp_odometer_word', 'mi' ), $inventory_id )
					);
				}

				if ( $args['show_prices'] ) {
					$grid_html .= sprintf(
						'<span class="grid-price">%s</span>',
						invp_get_the_price( $args['suppress_call_for_price'] ? ' ' : null, $inventory_id )
					);
				}

				$grid_html .= '</p>';
			}

			$grid_html .= '</a></li>';
		}
		$grid_html .= '</ul></div>';

		if ( $args['show_button'] ) {
			$grid_html .= sprintf(
				'<div class="invp-grid-button"><button onclick="location.href=\'%s\';" class="button wp-element-button">%s</button></div>',
				esc_url( get_post_type_archive_link( INVP::POST_TYPE ) ),
				esc_html__( 'Full Inventory', 'inventory-presser' )
			);
		}

		return $grid_html;
	}