Inventory_Presser_Shortcode_Archive::content( array $atts )

Creates the HTML content of the shortcode


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

	public function content( $atts ) {
		if ( ! wp_style_is( 'invp-attribute-table', 'registered' ) ) {
			Inventory_Presser_Plugin::include_scripts_and_styles();
		}
		wp_enqueue_style( 'invp-attribute-table' );
		wp_enqueue_style( 'invp_archive_vehicle' );

		$atts = shortcode_atts(
			array(
				'location'       => '',
				'order'          => get_query_var( 'order' ),
				'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
		);

		// Allow orderby and order $_GET parameters to change the sort order.
		$orderby = get_query_var( 'orderby' );
		if ( '' !== $orderby && 'meta_value' !== $orderby ) {
			$atts['meta_key'] = apply_filters( 'invp_prefix_meta_key', $orderby );
			$atts['orderby']  = 'meta_value';
		}

		// Parse boolean values to make life easy on users.
		$atts['show_titles'] = filter_var( $atts['show_titles'], FILTER_VALIDATE_BOOLEAN );
		$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 ) {
			$query_value = get_query_var( $taxonomy->query_var );
			if ( '' !== $query_value ) {
				$atts[ $taxonomy->query_var ] = $query_value;
			}
		}

		// Query vehicles.
		add_filter( 'invp_range_filters_main_query', '__return_false' );
		$vehicles_query = new WP_Query( $this->clean_attributes_for_query( $atts ) );
		remove_filter( 'invp_range_filters_main_query', '__return_false' );

		// Create the HTML output.
		$output = '';
		if ( $vehicles_query->have_posts() ) {
			while ( $vehicles_query->have_posts() ) {
				$vehicles_query->the_post();
				$shortcode = sprintf( '
		
		', strval( $atts['show_titles'] ) );
				$output   .= apply_shortcodes( $shortcode );
			}

			/**
			 * Paged navigation. Overwrite the global query with this shortcode's
			 * query so the core pagination functions work as expected. The
			 * query is rest on the following line outside this condition block.
			 */
			global $wp_query;
			$wp_query = $vehicles_query;
			$output  .= INVP::get_paging_html();
		} else {
			/**
			 * Do not encourage the user to search if there are zero vehicles.
			 * This query could be a filtered result that's empty, so check
			 * explicitly.
			 */
			$vehicle_count = INVP::vehicle_count();
			if ( 0 < $vehicle_count ) {
				$output .= apply_filters(
					'invp_archive_shortcode_no_results',
					sprintf(
						'<p>%s</p><h2>%s</h2><p>%s</p>',
						esc_html__( 'No vehicles found.', 'inventory-presser' ),
						esc_html__( 'Search Inventory', 'inventory-presser' ),
						apply_filters( 'invp_archive_shortcode_no_results_search', get_search_form() )
					)
				);
			}
		}

		// Restore original query & post data.
		wp_reset_query();

		return $output;
	}