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' );

		/**
		 * $args array keys
		 * ----------------
		 * + columns
		 * + featured_only
		 * + limit
		 * + newest_first
		 * + show_button
		 * + show_captions
		 * + show_prices
		 */
		$default_args = array(
			'columns'       => 5,
			'featured_only' => false,
			'limit'         => 15,
			'newest_first'  => false,
			'show_button'   => false,
			'show_captions' => false,
			'show_prices'   => false,
		);
		$args         = wp_parse_args( $args, $default_args );

		// Make sure the limit is not zero or empty string.
		if ( empty( $args['limit'] ) ) {
			$args['limit'] = -1;
		}

		$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',
			'order'          => 'ASC',
		);

		if ( $args['newest_first'] ) {
			$post_args['meta_key'] = apply_filters( 'invp_prefix_meta_key', 'last_modified' );
			$post_args['orderby']  = 'STR_TO_DATE( meta1.meta_value, \'%a, %d %b %Y %T\' )';
			$post_args['order']    = 'DESC';
		}

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

		// 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;
		}

		$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 .= sprintf(
					'<p class="grid-caption">%s&nbsp;%s</p>',
					get_the_title( $inventory_id ),
					$args['show_prices'] ? '<span class="grid-price">' . invp_get_the_price( ' ', $inventory_id ) . '</span>' : ''
				);
			}

			$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">%s</button></div>',
				get_post_type_archive_link( INVP::POST_TYPE ),
				__( 'Full Inventory', 'inventory-presser' )
			);
		}

		return $grid_html;
	}