';
/**
* Fires at the end of Contact Info widget.
*
* @module widgets
*
* @since 3.9.2
*/
do_action( 'jetpack_contact_info_widget_end' );
echo $args['after_widget'];
/** This action is documented in modules/widgets/gravatar-profile.php */
do_action( 'jetpack_stats_extra', 'widget_view', 'contact_info' );
}
/**
* Deals with the settings when they are saved by the admin. Here is
* where any validation should be dealt with.
*
* @param array $new_instance New configuration values
* @param array $old_instance Old configuration values
*
* @return array
*/
function update( $new_instance, $old_instance ) {
$update_lat_lon = false;
if (
! isset( $old_instance['address'] ) ||
$this->urlencode_address( $old_instance['address'] ) != $this->urlencode_address( $new_instance['address'] )
) {
$update_lat_lon = true;
}
$instance = array();
$instance['title'] = wp_kses( $new_instance['title'], array() );
$instance['address'] = wp_kses( $new_instance['address'], array() );
$instance['phone'] = wp_kses( $new_instance['phone'], array() );
$instance['email'] = wp_kses( $new_instance['email'], array() );
$instance['hours'] = wp_kses( $new_instance['hours'], array() );
$instance['apikey'] = wp_kses( isset( $new_instance['apikey'] ) ? $new_instance['apikey'] : $old_instance['apikey'], array() );
$instance['lat'] = isset( $old_instance['lat'] ) ? floatval( $old_instance['lat'] ) : 0;
$instance['lon'] = isset( $old_instance['lon'] ) ? floatval( $old_instance['lon'] ) : 0;
if ( ! $instance['lat'] || ! $instance['lon'] ) {
$update_lat_lon = true;
}
if ( $instance['address'] && $update_lat_lon ) {
// Get the lat/lon of the user specified address.
$address = $this->urlencode_address( $instance['address'] );
$path = 'https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=' . $address;
/** This action is documented in modules/widgets/contact-info.php */
$key = apply_filters( 'jetpack_google_maps_api_key', $instance['apikey'] );
if ( ! empty( $key ) ) {
$path = add_query_arg( 'key', $key, $path );
}
$json = wp_remote_retrieve_body( wp_remote_get( esc_url( $path, null, null ) ) );
if ( ! $json ) {
// The read failed :(
esc_html_e( 'There was a problem getting the data to display this address on a map. Please refresh your browser and try again.', 'jetpack' );
die();
}
$json_obj = json_decode( $json );
if ( 'ZERO_RESULTS' == $json_obj->status ) {
// The address supplied does not have a matching lat / lon.
// No map is available.
$instance['lat'] = '0';
$instance['lon'] = '0';
} else {
$loc = $json_obj->results[0]->geometry->location;
$lat = floatval( $loc->lat );
$lon = floatval( $loc->lng );
$instance['lat'] = "$lat";
$instance['lon'] = "$lon";
}
}
if ( ! isset( $new_instance['showmap'] ) ) {
$instance['showmap'] = 0;
} else {
$instance['showmap'] = intval( $new_instance['showmap'] );
}
return $instance;
}
/**
* Displays the form for this widget on the Widgets page of the WP Admin area.
*
* @param array $instance Instance configuration.
*
* @return void
*/
function form( $instance ) {
$instance = wp_parse_args( $instance, $this->defaults() );
wp_enqueue_script(
'contact-info-admin',
Jetpack::get_file_url_for_environment(
'_inc/build/widgets/contact-info/contact-info-admin.min.js',
'modules/widgets/contact-info/contact-info-admin.js'
),
array( 'jquery' ),
20160727
);
?>
has_good_map( $instance ) ) {
?>
/>
urlencode_address( $address );
}
/**
* Builds map display HTML code from the supplied latitude and longitude.
*
* @param string $address Address.
* @param string $api_key API Key.
*
* @return string HTML of the map.
*/
function build_map( $address, $api_key = null ) {
$this->enqueue_scripts();
$src = add_query_arg( 'q', rawurlencode( $address ), 'https://www.google.com/maps/embed/v1/place' );
if ( ! empty( $api_key ) ) {
$src = add_query_arg( 'key', $api_key, $src );
}
$height = 216;
$iframe_attributes = sprintf(
' height="%d" frameborder="0" src="%s" class="contact-map"',
esc_attr( $height ),
esc_url( $src )
);
$iframe_html = sprintf( '', $iframe_attributes );
if (
! class_exists( 'Jetpack_AMP_Support' )
|| ! Jetpack_AMP_Support::is_amp_request()
) {
return $iframe_html;
}
$amp_iframe_html = sprintf( '', $iframe_attributes );
// Add placeholder to avoid AMP error: elements must be positioned outside the first 75% of the viewport or 600px from the top (whichever is smaller).
$amp_iframe_html .= sprintf( '%s', esc_html__( 'Loading map…', 'jetpack' ) );
// Add original iframe as fallback in case JavaScript is disabled.
$amp_iframe_html .= sprintf( '', $iframe_html );
$amp_iframe_html .= '';
return $amp_iframe_html;
}
/**
* Encode an URL
*
* @param string $address The URL to encode
*
* @return string The encoded URL
*/
function urlencode_address( $address ) {
$address = strtolower( $address );
$address = preg_replace( '/\s+/', ' ', trim( $address ) ); // Get rid of any unwanted whitespace
$address = str_ireplace( ' ', '+', $address ); // Use + not %20
return urlencode( $address );
}
/**
* Check if the instance has a valid Map location.
*
* @param array $instance Widget instance configuration.
*
* @return bool Whether or not there is a valid map.
*/
function has_good_map( $instance ) {
// The lat and lon of an address that could not be plotted will have values of 0 and 0.
return ! ( '0' == $instance['lat'] && '0' == $instance['lon'] );
}
}
}