← Back
Editing: class-geocoder.php
<?php defined( 'ABSPATH' ) || exit; class ADM_Alt_Geocoder { /** * Return manually configured GPS coordinates from Settings. */ public static function get_coords(): ?array { $cached = get_option( 'adm_alt_gps_coords' ); if ( $cached && isset( $cached['lat'], $cached['lng'] ) ) { return $cached; } return null; } public static function geocode( string $address ): ?array { $api_key = get_option( 'adm_alt_geo_key', '' ); if ( ! $api_key ) return null; $url = add_query_arg( [ 'address' => urlencode( $address ), 'key' => $api_key, ], 'https://maps.googleapis.com/maps/api/geocode/json' ); $resp = wp_remote_get( $url, [ 'timeout' => 10 ] ); if ( is_wp_error( $resp ) ) return null; $data = json_decode( wp_remote_retrieve_body( $resp ), true ); if ( empty( $data['results'][0]['geometry']['location'] ) ) return null; $loc = $data['results'][0]['geometry']['location']; return [ 'lat' => (float) $loc['lat'], 'lng' => (float) $loc['lng'], 'address' => $data['results'][0]['formatted_address'] ?? $address, ]; } /** * Try to discover the business address from Google My Business via Places API * using the site name + domain as the search query. */ private static function guess_address_from_site(): string { $places_key = get_option( 'adm_alt_places_key', '' ); if ( ! $places_key ) return ''; $site_name = get_bloginfo( 'name' ); $site_host = parse_url( home_url(), PHP_URL_HOST ); $query = $site_name . ' ' . $site_host; $url = add_query_arg( [ 'query' => urlencode( $query ), 'key' => $places_key, ], 'https://maps.googleapis.com/maps/api/place/textsearch/json' ); $resp = wp_remote_get( $url, [ 'timeout' => 10 ] ); if ( is_wp_error( $resp ) ) return ''; $data = json_decode( wp_remote_retrieve_body( $resp ), true ); $address = $data['results'][0]['formatted_address'] ?? ''; if ( $address ) { update_option( 'adm_alt_business_address', $address ); } return $address; } /** * Write GPS EXIF data into an image file. * Uses Imagick if available, falls back to pure-PHP JPEG EXIF writer. * Returns true on success. */ public static function write_gps( int $attachment_id, float $lat, float $lng ): bool { $path = get_attached_file( $attachment_id ); if ( ! $path || ! file_exists( $path ) ) return false; $mime = get_post_mime_type( $attachment_id ); // Imagick path (supports JPEG, PNG, WebP) if ( class_exists( 'Imagick' ) && in_array( $mime, [ 'image/jpeg', 'image/png', 'image/webp' ], true ) ) { return self::write_gps_imagick( $path, $lat, $lng ); } // Pure-PHP fallback — JPEG only if ( $mime === 'image/jpeg' ) { return self::write_gps_jpeg( $path, $lat, $lng ); } return false; } private static function write_gps_imagick( string $path, float $lat, float $lng ): bool { try { $img = new Imagick( $path ); $lat_ref = $lat >= 0 ? 'N' : 'S'; $lng_ref = $lng >= 0 ? 'E' : 'W'; $lat_abs = abs( $lat ); $lng_abs = abs( $lng ); $img->setImageProperty( 'exif:GPSLatitudeRef', $lat_ref ); $img->setImageProperty( 'exif:GPSLatitude', self::dd_to_dms_string( $lat_abs ) ); $img->setImageProperty( 'exif:GPSLongitudeRef', $lng_ref ); $img->setImageProperty( 'exif:GPSLongitude', self::dd_to_dms_string( $lng_abs ) ); $img->writeImage( $path ); $img->destroy(); return true; } catch ( Exception $e ) { return false; } } /** * Minimal pure-PHP JPEG EXIF GPS writer. * Appends/replaces the APP1 EXIF segment with GPS IFD data. */ private static function write_gps_jpeg( string $path, float $lat, float $lng ): bool { $data = file_get_contents( $path ); if ( ! $data || substr( $data, 0, 2 ) !== "\xFF\xD8" ) return false; $lat_ref = $lat >= 0 ? 'N' : 'S'; $lng_ref = $lng >= 0 ? 'E' : 'W'; $lat_abs = abs( $lat ); $lng_abs = abs( $lng ); $exif = self::build_gps_exif_segment( $lat_abs, $lat_ref, $lng_abs, $lng_ref ); if ( ! $exif ) return false; // Remove existing APP1 segment if present, then prepend new one $body = self::strip_app1( $data ); $new_data = "\xFF\xD8" . $exif . substr( $body, 2 ); return file_put_contents( $path, $new_data ) !== false; } private static function strip_app1( string $data ): string { // Walk JPEG markers and drop APP1 (FF E1) $out = "\xFF\xD8"; $offset = 2; $len = strlen( $data ); while ( $offset + 4 <= $len ) { if ( $data[ $offset ] !== "\xFF" ) break; $marker = ord( $data[ $offset + 1 ] ); $seg_len = unpack( 'n', substr( $data, $offset + 2, 2 ) )[1]; $seg_data = substr( $data, $offset, $seg_len + 2 ); if ( $marker !== 0xE1 ) { $out .= $seg_data; } $offset += $seg_len + 2; if ( $marker === 0xDA ) { // SOS — rest is image data $out .= substr( $data, $offset ); break; } } return $out; } private static function build_gps_exif_segment( float $lat, string $lat_ref, float $lng, string $lng_ref ): ?string { // Minimal Exif with GPS IFD only, little-endian (Intel) $header = "Exif\x00\x00"; $tiff = "II"; // Little-endian $tiff .= pack( 'v', 42 ); // TIFF magic $tiff .= pack( 'V', 8 ); // IFD0 offset // IFD0: one entry — GPS IFD pointer (tag 0x8825) $gps_ifd_offset = 8 + 2 + 12 + 4; // IFD0 start + count + entry + next ptr $tiff .= pack( 'v', 1 ); // 1 entry in IFD0 $tiff .= pack( 'v', 0x8825 ); // GPS IFD tag $tiff .= pack( 'v', 4 ); // type LONG $tiff .= pack( 'V', 1 ); // count 1 $tiff .= pack( 'V', $gps_ifd_offset ); // value = offset to GPS IFD $tiff .= pack( 'V', 0 ); // next IFD = none // GPS IFD (4 entries: LatRef, Lat, LngRef, Lng) $gps_data_offset = $gps_ifd_offset + 2 + ( 4 * 12 ) + 4; list( $lat_d, $lat_m, $lat_s ) = self::dd_to_dms( $lat ); list( $lng_d, $lng_m, $lng_s ) = self::dd_to_dms( $lng ); $gps_ifd = pack( 'v', 4 ); // 4 entries // LatRef (tag 1, ASCII, 2 bytes) $gps_ifd .= pack( 'v', 1 ) . pack( 'v', 2 ) . pack( 'V', 2 ) . str_pad( $lat_ref, 4, "\x00" ); // Lat (tag 2, RATIONAL, 3 values) $lat_val_offset = $gps_data_offset; $gps_ifd .= pack( 'v', 2 ) . pack( 'v', 5 ) . pack( 'V', 3 ) . pack( 'V', $lat_val_offset ); // LngRef (tag 3, ASCII, 2 bytes) $gps_ifd .= pack( 'v', 3 ) . pack( 'v', 2 ) . pack( 'V', 2 ) . str_pad( $lng_ref, 4, "\x00" ); // Lng (tag 4, RATIONAL, 3 values) $lng_val_offset = $lat_val_offset + 24; // 3 rationals × 8 bytes $gps_ifd .= pack( 'v', 4 ) . pack( 'v', 5 ) . pack( 'V', 3 ) . pack( 'V', $lng_val_offset ); $gps_ifd .= pack( 'V', 0 ); // next IFD = none // Rational values (numerator/denominator pairs) $rational_data = pack( 'VV', (int) $lat_d, 1 ); $rational_data .= pack( 'VV', (int) $lat_m, 1 ); $rational_data .= pack( 'VV', (int) round( $lat_s * 1000 ), 1000 ); $rational_data .= pack( 'VV', (int) $lng_d, 1 ); $rational_data .= pack( 'VV', (int) $lng_m, 1 ); $rational_data .= pack( 'VV', (int) round( $lng_s * 1000 ), 1000 ); $exif_body = $tiff . $gps_ifd . $rational_data; $app1_body = $header . $exif_body; $app1 = "\xFF\xE1" . pack( 'n', strlen( $app1_body ) + 2 ) . $app1_body; return $app1; } private static function dd_to_dms( float $dd ): array { $d = floor( $dd ); $m = floor( ( $dd - $d ) * 60 ); $s = ( $dd - $d - $m / 60 ) * 3600; return [ $d, $m, $s ]; } private static function dd_to_dms_string( float $dd ): string { [ $d, $m, $s ] = self::dd_to_dms( $dd ); return "{$d}/1,{$m}/1," . (int) round( $s * 1000 ) . '/1000'; } }
Save File
Cancel