90 || $lng < -180 || $lng > 180 ) { return null; } return array( 'lat' => $lat, 'lng' => $lng, ); } /** * OpenStreetMap embed address for a venue ('' without coordinates). * * @param array|null $venue Venue data. * @return string */ public static function embed_url( $venue ) { $c = self::coordinates( $venue ); if ( ! $c ) { return ''; } $d = 0.006; $bbox = array( max( -180, $c['lng'] - $d * 1.6 ), max( -90, $c['lat'] - $d ), min( 180, $c['lng'] + $d * 1.6 ), min( 90, $c['lat'] + $d ), ); return self::EMBED_BASE . '?' . http_build_query( array( 'bbox' => implode( ',', array_map( array( __CLASS__, 'num' ), $bbox ) ), 'layer' => 'mapnik', 'marker' => self::num( $c['lat'] ) . ',' . self::num( $c['lng'] ), ), '', '&', PHP_QUERY_RFC3986 ); } /** * Number with six decimals, trailing zeros removed. * * @param float $n Number. * @return string */ private static function num( $n ) { $s = rtrim( rtrim( number_format( (float) $n, 6, '.', '' ), '0' ), '.' ); return '' === $s || '-0' === $s ? '0' : $s; } /** * The "Show map" button. Nothing is loaded until it is clicked. * * @param array|null $venue Venue data. * @return string HTML (already escaped), or '' when maps are off or the venue has no coordinates. */ public static function button( $venue ) { if ( ! self::enabled() ) { return ''; } $url = self::embed_url( $venue ); if ( ! $url ) { return ''; } $id = 'evw-map-' . wp_unique_id(); $title = sprintf( /* translators: %s: venue name */ __( 'Map of %s', 'eventwright' ), isset( $venue['name'] ) ? $venue['name'] : '' ); $html = '
'; $html .= ' '; $html .= '' . esc_html__( 'Loads a map from OpenStreetMap when you click.', 'eventwright' ) . ''; $html .= ''; $html .= '
'; Frontend::enqueue_assets(); return $html; } /** * Load the venue editor script. * * @param string $hook Admin page. */ public static function admin_assets( $hook ) { if ( ! in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) { return; } $screen = get_current_screen(); if ( ! $screen || Post_Types::VENUE !== $screen->post_type ) { return; } wp_enqueue_script( 'eventwright-venue', EVWR_URL . 'assets/js/venue-admin.js', array( 'wp-i18n' ), EVWR_VERSION, true ); wp_set_script_translations( 'eventwright-venue', 'eventwright', EVWR_DIR . 'languages' ); wp_localize_script( 'eventwright-venue', 'eventwrightVenue', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'action' => self::GEOCODE_ACTION, 'nonce' => wp_create_nonce( self::GEOCODE_NONCE ), ) ); } /** * User-Agent for Nominatim. Their usage policy requires one that identifies the application. * * @return string */ public static function user_agent() { return 'Eventwright/' . EVWR_VERSION . ' (WordPress plugin; ' . home_url( '/' ) . ')'; } /** * Ask Nominatim for the coordinates of an address. * * @param string $query Address. * @return array|\WP_Error { lat, lng, label } */ public static function geocode( $query ) { $query = trim( preg_replace( '/\s+/', ' ', (string) $query ) ); if ( strlen( $query ) < 3 || strlen( $query ) > 300 ) { return new \WP_Error( 'evwr_geo_query', __( 'Enter an address first.', 'eventwright' ) ); } $url = self::NOMINATIM_URL . '?' . http_build_query( array( 'format' => 'json', 'limit' => 1, 'q' => $query, ), '', '&', PHP_QUERY_RFC3986 ); $response = wp_safe_remote_get( $url, array( 'timeout' => self::TIMEOUT, 'user-agent' => self::user_agent(), 'headers' => array( 'Accept' => 'application/json' ), ) ); if ( is_wp_error( $response ) ) { return new \WP_Error( 'evwr_geo_http', __( 'The address service could not be reached. Enter the coordinates by hand or try again later.', 'eventwright' ) ); } if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { return new \WP_Error( 'evwr_geo_http', __( 'The address service did not answer. Try again later.', 'eventwright' ) ); } $data = json_decode( wp_remote_retrieve_body( $response ), true ); if ( ! is_array( $data ) || empty( $data[0] ) || ! isset( $data[0]['lat'], $data[0]['lon'] ) || ! is_numeric( $data[0]['lat'] ) || ! is_numeric( $data[0]['lon'] ) ) { return new \WP_Error( 'evwr_geo_none', __( 'No place found for that address.', 'eventwright' ) ); } $lat = (float) $data[0]['lat']; $lng = (float) $data[0]['lon']; if ( $lat < -90 || $lat > 90 || $lng < -180 || $lng > 180 ) { return new \WP_Error( 'evwr_geo_none', __( 'No place found for that address.', 'eventwright' ) ); } return array( 'lat' => round( $lat, 6 ), 'lng' => round( $lng, 6 ), 'label' => isset( $data[0]['display_name'] ) ? sanitize_text_field( (string) $data[0]['display_name'] ) : '', ); } /** * Admin AJAX: find coordinates. Nonce and capability checked. */ public static function ajax_geocode() { if ( ! check_ajax_referer( self::GEOCODE_NONCE, 'nonce', false ) ) { wp_send_json_error( array( 'message' => __( 'Your session has expired. Reload the page and try again.', 'eventwright' ) ), 403 ); } if ( ! current_user_can( 'edit_posts' ) ) { wp_send_json_error( array( 'message' => __( 'Sorry, you are not allowed to do this.', 'eventwright' ) ), 403 ); } // One lookup every two seconds per person (Nominatim allows at most one request per second). $lock = 'evwr_geo_' . get_current_user_id(); if ( get_transient( $lock ) ) { wp_send_json_error( array( 'message' => __( 'Please wait a moment and try again.', 'eventwright' ) ), 429 ); } set_transient( $lock, 1, 2 ); $query = isset( $_POST['q'] ) ? sanitize_text_field( wp_unslash( $_POST['q'] ) ) : ''; $result = self::geocode( $query ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ), 'evwr_geo_query' === $result->get_error_code() ? 400 : 502 ); } wp_send_json_success( $result ); } }