invp_get_the_location_sentence( int|null $post_ID = null )

Creates a short sentence identifying the dealership address where this vehicle is located. If there is only one term in the locations taxonomy containing vehicles, this method returns an empty string.


Parameters Parameters

$post_ID

(int|null) (Optional) The post ID of a vehicle. Must be passed when using this method outside the loop.

Default value: null


Top ↑

Return Return

(string) An HTML <div> element containing a sentence that identifies the lot where this vehicle is located.


Top ↑

Source Source

File: includes/template-tags.php

function invp_get_the_location_sentence( $post_ID = null ) {
	if ( empty( $post_ID ) ) {
		$post_ID = get_the_ID();
	}

	/**
	 * How many locations *with vehicles* does this dealer have? If only one,
	 * return empty string because there's no reason to point out where this
	 * vehicle is, the dealership address is all over the website.
	 */
	$location_terms = get_terms( 'location', array( 'hide_empty' => true ) );
	$location_count = ! is_wp_error( $location_terms ) ? count( $location_terms ) : 0;

	if ( 1 >= $location_count ) {
		return '';
	}

	/**
	 * We want the term description from the location taxonomy term because the
	 * meta key/term name only contains street address line one. The term
	 * description has the full address.
	 */
	$location_terms = wp_get_post_terms( $post_ID, 'location' );
	if ( empty( $location_terms ) ) {
		return '';
	}

	// Could have two locations on the same vehicle, so just take the first.
	$location = str_replace( chr( 13 ) . chr( 10 ), ', ', $location_terms[0]->description );
	if ( empty( $location ) ) {
		return '';
	}

	$sentence = sprintf(
		'%s %s %s <strong><address>%s</address></strong>',
		__( 'See this', 'inventory-presser' ),
		invp_get_the_make( $post_ID ),
		__( 'at', 'inventory-presser' ),
		apply_filters( 'invp_vehicle_location_sentence_address', $location )
	);

	// Does this location have a phone number?
	$phones = INVP::get_phones( $location_terms[0]->term_id );
	if ( 0 < count( $phones ) ) {
		// Yes, at least one.
		foreach ( $phones as $phone ) {
			// Try to avoid fax numbers.
			if ( preg_match( '/\bfax\b/i', $phone['description'] ) ) {
				continue;
			}

			$number    = apply_filters( 'invp_vehicle_location_sentence_phone', $phone['number'] );
			$sentence .= sprintf(
				'<span class="location-phone">%s <a href="tel:+%s">%s</a></span>',
				__( 'Call', 'inventory-presser' ),
				INVP::prepare_phone_number_for_link( $number ),
				$number
			);
			break; // only add one phone number to the sentence.
		}
	}

	$sentence = apply_filters( 'invp_vehicle_location_sentence', $sentence, $post_ID );

	if ( function_exists( 'apply_shortcodes' ) ) {
		$sentence = apply_shortcodes( $sentence );
	}

	return $sentence;
}