Making WordPress.org

Changeset 1412


Ignore:
Timestamp:
03/17/2015 11:58:47 PM (11 years ago)
Author:
iandunn
Message:

WordCamp Post Type: Store coordinates of WordCamp venues.

This data will be used by the Central theme to display camps on a map.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-wordcamp/wordcamp-admin.php

    r1233 r1412  
    105105        // WordCamp post type only
    106106        if ( WCPT_POST_TYPE_ID == get_post_type() ) {
     107            // If the venue address was changed, update it's coordinates
     108            $new_address = $_POST[ wcpt_key_to_str( 'Physical Address', 'wcpt_' ) ];
     109            if ( $new_address != get_post_meta( $post_id, 'Physical Address', true ) ) {
     110                if ( $coordinates = $this->geocode_address( $new_address ) ) {
     111                    update_post_meta( $post_id, '_venue_coordinates', $coordinates );
     112                } else {
     113                    delete_post_meta( $post_id, '_venue_coordinates' );
     114                }
     115            }
     116
    107117            // Post meta keys
    108118            $wcpt_meta_keys = WordCamp_Admin::meta_keys();
     
    142152            }
    143153        }
     154    }
     155
     156    /**
     157     * Geocode the given address into a latitude and longitude pair.
     158     *
     159     * @param string $address
     160     *
     161     * @return mixed
     162     *      false if the geocode request failed
     163     *      array with latitude and longitude indexes if the request succeeded
     164     */
     165    function geocode_address( $address ) {
     166        if ( ! $address ) {
     167            return false;
     168        }
     169
     170        $coordinates      = false;
     171        $request_url      = add_query_arg( 'address', urlencode( $address ), 'https://maps.googleapis.com/maps/api/geocode/json' );
     172        $geocode_response = json_decode( wp_remote_retrieve_body( wp_remote_get( $request_url ) ) );
     173
     174        if ( ! empty( $geocode_response->results[0]->geometry->location->lat ) ) {
     175            $coordinates = array(
     176                'latitude'  => $geocode_response->results[0]->geometry->location->lat,
     177                'longitude' => $geocode_response->results[0]->geometry->location->lng
     178            );
     179        }
     180
     181        return $coordinates;
    144182    }
    145183
Note: See TracChangeset for help on using the changeset viewer.