Tribe__Events__Pro__Geo_Loc::save_venue_geodata( int $venue_id, array $data, bool|null $force = false )

Hooks into the venue save and if we don’t have Geo Data for that address, it calls the Google Maps API and grabs the Lat and Lng for that venue.


Parameters

$venue_id

(int) (Required) The The Venue post ID.

$data

(array) (Required) An array of location data for the Venue as provided by WordPress.

$force

(bool|null) (Optional) Whether to force the re-fetch of the geolocation data for the Venue or not.

Default value: false


Top ↑

Return

(bool) Whether the Venue geolocation information was updated or not.


Top ↑

Source

File: src/Tribe/Geo_Loc.php

	public function save_venue_geodata( $venueId, $data ) {

		$_address  = ( ! empty( $data['Address'] ) ) ? $data['Address'] : '';
		$_city     = ( ! empty( $data['City'] ) ) ? $data['City'] : '';
		$_province = ( ! empty( $data['Province'] ) ) ? $data['Province'] : '';
		$_state    = ( ! empty( $data['State'] ) ) ? $data['State'] : '';
		$_zip      = ( ! empty( $data['Zip'] ) ) ? $data['Zip'] : '';
		$_country  = ( ! empty( $data['Country'] ) ) ? $data['Country'] : '';

		$overwrite = ( ! empty( $data['OverwriteCoords'] ) ) ? 1 : 0;
		$_lat = ( ! empty( $data['Lat'] ) && is_numeric( $data['Lat'] ) ) ? (float) $data['Lat'] : false;
		$_lng = ( ! empty( $data['Lng'] ) && is_numeric( $data['Lng'] ) ) ? (float) $data['Lng'] : false;
		$reset = false;

		// Check the Overwrite data, otherwise just reset it
		if ( $overwrite && false !== $_lat && false !== $_lng ) {
			update_post_meta( $venueId, self::OVERWRITE, 1 );
			update_post_meta( $venueId, self::LAT, (string) $_lat );
			update_post_meta( $venueId, self::LNG, (string) $_lng );

			$this->clear_min_max_coords_cache();
			return true;
		} else {
			if ( 1 === (int) get_post_meta( $venueId, self::OVERWRITE, true ) ) {
				$reset = true;
			}
			update_post_meta( $venueId, self::OVERWRITE, 0 );
		}

		// Remove remaining spaces from any of the pieces of the address.
		$pieces = array_map( 'trim', compact( '_address', '_province', '_city', '_state', '_zip', '_country' ) );
		$address = implode( ' ', array_filter( $pieces ) );
		// Remove any parenthesis from the address and his content as well
		$address = preg_replace( '/\(.*\)/', '', $address );

		if ( empty( $address ) ) {
			return false;
		}

		/**
		 * Allows customizing whether the Google Maps Geocode API will be used for geocoding addresses.
		 *
		 * @since 4.4.34
		 *
		 * @param boolean $geocode_addresses Whether the Geocode Address API is enabled for geocoding addresses.
		 * @param int     $venueId           Venue post ID.
		 * @param string  $address           Address string that will be used for geocoding.
		 */
		$geocode_addresses = apply_filters( 'tribe_events_pro_geocode_addresses', true, $venueId, $address );

		if ( false === $geocode_addresses ) {
			return false;
		}

		// If the address didn't change, it doesn't make sense to query Google again for the geo data.
		if ( true !== $reset && $address === get_post_meta( $venueId, self::ADDRESS, true ) ) {
			return false;
		}

		//The basic Google Maps API key we provide doesn't support geo-coding queries.
		if ( tribe_is_using_basic_gmaps_api() ) {
			return update_post_meta( $venueId, self::ADDRESS, $address );
		}

		$resolved = $this->geocode_address( $address, $venueId, $pieces );

		if ( false !== $resolved ) {
			if ( isset( $resolved['lat'] ) ) {
				update_post_meta( $venueId, self::LAT, $resolved['lat'] );
			}
			if ( isset( $resolved['lng'] ) ) {
				update_post_meta( $venueId, self::LNG, $resolved['lng'] );
			}
		}

		// Saving the aggregated address so we don't need to ping google on every save
		update_post_meta( $venueId, self::ADDRESS, $address );

		$this->clear_min_max_coords_cache();

		return true;
	}