Inventory_Presser_Schema_Org_Generator::schema_org_json_ld( int $post_ID )

Returns Schema.org markup for this Vehicle as a JSON-LD code block


Parameters Parameters

$post_ID

(int) (Required) The current post ID.


Top ↑

Return Return

(string) Schema.org JSON script element


Top ↑

Source Source

File: includes/class-schema-org-generator.php

	protected function schema_org_json_ld( $post_ID ) {
		$obj = array(
			'@context' => 'https://schema.org/',
			'@type'    => 'Vehicle',
		);

		$obj['name'] = get_the_title( $post_ID );

		$make = invp_get_the_make( $post_ID );
		if ( '' !== $make ) {
			$obj['brand'] = array(
				'@type' => 'Thing',
				'name'  => $make,
			);
		}

		$vin = invp_get_the_VIN( $post_ID );
		if ( '' !== $vin ) {
			$obj['vehicleIdentificationNumber'] = $vin;
		}

		$year = invp_get_the_year( $post_ID );
		if ( 0 !== $year ) {
			$obj['vehicleModelDate'] = $year;
		}

		// Do we have photos?
		if ( 0 < invp_get_the_photo_count( $post_ID ) ) {
			$obj['image'] = invp_get_the_photo_url( $post_ID );
		}

		$odometer = invp_get_the_odometer( '', $post_ID );
		if ( '' !== $odometer ) {
			// Extract just digits from the odometer value.
			$odometer_digits            = abs( (int) filter_var( $odometer, FILTER_SANITIZE_NUMBER_INT ) );
			$obj['mileageFromOdometer'] = array(
				'@type'    => 'QuantitativeValue',
				'value'    => $odometer_digits,
				'unitCode' => 'SMI',
			);
		}

		if ( '' !== invp_get_the_engine( $post_ID ) || '' !== invp_get_the_fuel( $post_ID ) ) {
			$obj['vehicleEngine'] = array();
			if ( '' !== invp_get_the_engine( $post_ID ) ) {
				$obj['vehicleEngine']['engineType'] = invp_get_the_engine( $post_ID );
			}
			if ( '' !== invp_get_the_fuel( $post_ID ) ) {
				$obj['vehicleEngine']['fuelType'] = invp_get_the_fuel( $post_ID );
			}
		}

		if ( '' !== invp_get_the_body_style( $post_ID ) ) {
			$obj['bodyType'] = invp_get_the_body_style( $post_ID );
		}

		if ( '' !== invp_get_the_color( $post_ID ) ) {
			$obj['color'] = invp_get_the_color( $post_ID );
		}

		if ( '' !== invp_get_the_interior_color( $post_ID ) ) {
			$obj['vehicleInteriorColor'] = invp_get_the_interior_color( $post_ID );
		}

		if ( invp_get_the_description( $post_ID ) ) {
			$obj['description'] = invp_get_the_description( $post_ID );
		}

		$schema_drive_type = $this->schema_org_drive_type( invp_get_the_drive_type( $post_ID ) );
		if ( null !== $schema_drive_type ) {
			$obj['driveWheelConfiguration'] = $schema_drive_type;
		}

		if ( '' !== invp_get_the_transmission( $post_ID ) ) {
			$obj['vehicleTransmission'] = invp_get_the_transmission( $post_ID );
		}

		return '<script type="application/ld+json">' . wp_json_encode( $obj ) . '</script>';
	}