Inventory_Presser_Shortcode_Archive::content( array $atts )

content


Description Description

Creates the HTML content of the shortcode


Top ↑

Parameters Parameters

$atts

(array) (Required)


Top ↑

Return Return

(string) HTML that renders an archive-vehicle template


Top ↑

Source Source

File: includes/shortcode/class-shortcode-archive.php

	function content( $atts ) {
		wp_enqueue_style( 'invp-attribute-table' );
		wp_enqueue_style( 'invp_archive_vehicle' );
		$plugin_settings = INVP::settings();

		$atts = shortcode_atts(
			array(
				'paged'          => ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1,
				'posts_per_page' => get_option( 'posts_per_page' ),
				'post_status'    => 'publish',
				'show_titles'    => true,
			),
			$atts
		);

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

		// Don't let input change the post type
		$atts['post_type'] = INVP::POST_TYPE;

		// Add all taxonomy query vars to $atts so filters work
		$taxonomies = get_object_taxonomies( $atts['post_type'], 'objects' );
		foreach ( $taxonomies as $taxonomy ) {
			$atts[ $taxonomy->query_var ] = get_query_var( $taxonomy->query_var );
		}

		/**
		 * Handle querystring filters min_price, max_price, and max_odometer.
		 * This array $querystring_filters has no significance other than
		 * allowing the foreach loop below to handle 3 parameters similarly.
		 */
		$querystring_filters = array(
			array(
				'param'    => 'min_price', // querystring parameter name
				'field'    => 'price', // meta field suffix
				'operator' => '>=', // comparison operator
			),
			array(
				'param'    => 'max_price',
				'field'    => 'price',
				'operator' => '<=',
			),
			array(
				'param'    => 'max_odometer',
				'field'    => 'odometer',
				'operator' => '<=',
			),
		);

		foreach ( $querystring_filters as $arr ) {
			// Do we even have the querystring parameter?
			if ( empty( $_GET[ $arr['param'] ] ) ) {
				continue;
			}

			$atts['meta_query'] = Inventory_Presser_Plugin::maybe_add_meta_query(
				$atts['meta_query'],
				apply_filters( 'invp_prefix_meta_key', $arr['field'] ),
				(int) $_GET[ $arr['param'] ],
				$arr['operator'],
				'numeric'
			);
			if ( ! empty( $atts['meta_key'] ) ) {
				unset( $atts['meta_key'] );
			}
		}

		// Allow our order by mods to affect this query_posts() call
		add_filter( 'invp_apply_orderby_to_main_query_only', '__return_false' );
		query_posts( $this->clean_attributes_for_query( $atts ) );
		remove_filter( 'invp_apply_orderby_to_main_query_only', '__return_false' );

		$output = '';
		if ( have_posts() ) {
			while ( have_posts() ) {
				the_post();
				$shortcode = sprintf( '[invp_archive_vehicle show_titles="%s"]', strval( $atts['show_titles'] ) );
				$output   .= apply_shortcodes( $shortcode );
			}
		} else {
			$count_posts = wp_count_posts( INVP::POST_TYPE );
			if ( isset( $count_posts->publish ) && 0 < $count_posts->publish ) {
				$output .= apply_filters(
					'invp_archive_shortcode_no_results',
					sprintf(
						'<p>%s</p><h2>%s</h2><p>%s</p>',
						__( 'No vehicles found.', 'inventory-presser' ),
						__( 'Search Inventory', 'inventory-presser' ),
						apply_filters( 'invp_archive_shortcode_no_results_search', get_search_form() )
					)
				);
			}
		}

		// Paged navigation
		$output .= $this->paging_html();

		wp_reset_query();
		return $output;
	}