Inventory_Presser_Schema_Org_Generator::schema_org_json_ld( $post_ID )

schema_org_json_ld


Description Description

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


Top ↑

Return Return

(string) Schema.org JSON script element


Top ↑

Source Source

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

	function schema_org_json_ld( $post_ID )
	{
		$obj = [
			'@context' => 'http://schema.org/',
			'@type'    => 'Vehicle'
		];

		$obj['name'] = get_the_title( $post_ID );
		
		$make = invp_get_the_make( $post_ID );
		if( '' != $make )
		{
			$obj['brand'] = [
				'@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;
		}

		//if the image does not end with 'no-photo.png'
		if( 'no-photo.png' != substr( invp_get_the_photo_url( $post_ID ), 12 ) )
		{
			$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'] = [
				'@type'    => 'QuantitativeValue',
				'value'    => $odometer_digits,
				'unitCode' => 'SMI'
			];
		}

		if( '' != invp_get_the_engine( $post_ID ) || '' != invp_get_the_fuel( $post_ID ) )
		{
			$obj['vehicleEngine'] = [];
			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">' . json_encode( $obj ) . '</script>';
	}