Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/modules/widgets
File: contact-info.php
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
[0] Fix | Delete
[1] Fix | Delete
// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
[2] Fix | Delete
[3] Fix | Delete
use Automattic\Jetpack\Assets;
[4] Fix | Delete
use Automattic\Jetpack\Redirect;
[5] Fix | Delete
[6] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[7] Fix | Delete
exit( 0 );
[8] Fix | Delete
}
[9] Fix | Delete
[10] Fix | Delete
if ( ! class_exists( 'Jetpack_Contact_Info_Widget' ) ) {
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* Register Contact_Info_Widget widget
[14] Fix | Delete
*/
[15] Fix | Delete
function jetpack_contact_info_widget_init() {
[16] Fix | Delete
register_widget( 'Jetpack_Contact_Info_Widget' );
[17] Fix | Delete
}
[18] Fix | Delete
[19] Fix | Delete
add_action( 'widgets_init', 'jetpack_contact_info_widget_init' );
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* Makes a custom Widget for displaying Restaurant Location/Map, Hours, and Contact Info available.
[23] Fix | Delete
*
[24] Fix | Delete
* @package WordPress
[25] Fix | Delete
*/
[26] Fix | Delete
class Jetpack_Contact_Info_Widget extends WP_Widget {
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* Constructor
[30] Fix | Delete
*/
[31] Fix | Delete
public function __construct() {
[32] Fix | Delete
global $pagenow;
[33] Fix | Delete
[34] Fix | Delete
$widget_ops = array(
[35] Fix | Delete
'classname' => 'widget_contact_info',
[36] Fix | Delete
'description' => __( 'Display a map with your location, hours, and contact information.', 'jetpack' ),
[37] Fix | Delete
'customize_selective_refresh' => true,
[38] Fix | Delete
'show_instance_in_rest' => true,
[39] Fix | Delete
);
[40] Fix | Delete
parent::__construct(
[41] Fix | Delete
'widget_contact_info',
[42] Fix | Delete
/** This filter is documented in modules/widgets/facebook-likebox.php */
[43] Fix | Delete
apply_filters( 'jetpack_widget_name', __( 'Contact Info & Map', 'jetpack' ) ),
[44] Fix | Delete
$widget_ops
[45] Fix | Delete
);
[46] Fix | Delete
$this->alt_option_name = 'widget_contact_info';
[47] Fix | Delete
[48] Fix | Delete
if ( is_customize_preview() ) {
[49] Fix | Delete
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
[50] Fix | Delete
} elseif ( 'widgets.php' === $pagenow ) {
[51] Fix | Delete
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
add_action( 'wp_ajax_customize-contact-info-api-key', array( $this, 'ajax_check_api_key' ) );
[55] Fix | Delete
add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) );
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
/**
[59] Fix | Delete
* Remove the "Contact info and Map" widget from the Legacy Widget block
[60] Fix | Delete
*
[61] Fix | Delete
* @param array $widget_types List of widgets that are currently removed from the Legacy Widget block.
[62] Fix | Delete
* @return array $widget_types New list of widgets that will be removed.
[63] Fix | Delete
*/
[64] Fix | Delete
public function hide_widget_in_block_editor( $widget_types ) {
[65] Fix | Delete
$widget_types[] = 'widget_contact_info';
[66] Fix | Delete
return $widget_types;
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
/**
[70] Fix | Delete
* Enqueue scripts and styles.
[71] Fix | Delete
*/
[72] Fix | Delete
public function enqueue_scripts() {
[73] Fix | Delete
wp_enqueue_style(
[74] Fix | Delete
'contact-info-map-css',
[75] Fix | Delete
plugins_url( 'contact-info/contact-info-map.css', __FILE__ ),
[76] Fix | Delete
array(),
[77] Fix | Delete
JETPACK__VERSION
[78] Fix | Delete
);
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
/**
[82] Fix | Delete
* Return an associative array of default values
[83] Fix | Delete
*
[84] Fix | Delete
* These values are used in new widgets.
[85] Fix | Delete
*
[86] Fix | Delete
* @return array Array of default values for the Widget's options
[87] Fix | Delete
*/
[88] Fix | Delete
public function defaults() {
[89] Fix | Delete
return array(
[90] Fix | Delete
'title' => __( 'Hours & Info', 'jetpack' ),
[91] Fix | Delete
'address' => __( "3999 Mission Boulevard,\nSan Diego CA 92109", 'jetpack' ),
[92] Fix | Delete
'phone' => _x( '1-202-555-1212', 'Example of a phone number', 'jetpack' ),
[93] Fix | Delete
'hours' => __( "Lunch: 11am - 2pm \nDinner: M-Th 5pm - 11pm, Fri-Sat:5pm - 1am", 'jetpack' ),
[94] Fix | Delete
'email' => null,
[95] Fix | Delete
'showmap' => 0,
[96] Fix | Delete
'apikey' => null,
[97] Fix | Delete
'goodmap' => null,
[98] Fix | Delete
);
[99] Fix | Delete
}
[100] Fix | Delete
[101] Fix | Delete
/**
[102] Fix | Delete
* Outputs the HTML for this widget.
[103] Fix | Delete
*
[104] Fix | Delete
* @param array $args An array of standard parameters for widgets in this theme.
[105] Fix | Delete
* @param array $instance An array of settings for this widget instance.
[106] Fix | Delete
*
[107] Fix | Delete
* @return void Echoes it's output
[108] Fix | Delete
**/
[109] Fix | Delete
public function widget( $args, $instance ) {
[110] Fix | Delete
$instance = wp_parse_args( $instance, $this->defaults() );
[111] Fix | Delete
[112] Fix | Delete
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
[113] Fix | Delete
[114] Fix | Delete
if ( '' !== $instance['title'] ) {
[115] Fix | Delete
echo $args['before_title'] . $instance['title'] . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
/**
[119] Fix | Delete
* Fires at the beginning of the Contact Info widget, after the title.
[120] Fix | Delete
*
[121] Fix | Delete
* @module widgets
[122] Fix | Delete
*
[123] Fix | Delete
* @since 3.9.2
[124] Fix | Delete
*/
[125] Fix | Delete
do_action( 'jetpack_contact_info_widget_start' );
[126] Fix | Delete
[127] Fix | Delete
echo '<div itemscope itemtype="http://schema.org/LocalBusiness">';
[128] Fix | Delete
[129] Fix | Delete
if ( '' !== $instance['address'] ) {
[130] Fix | Delete
[131] Fix | Delete
$showmap = $instance['showmap'];
[132] Fix | Delete
$goodmap = isset( $instance['goodmap'] ) ? $instance['goodmap'] : $this->has_good_map( $instance );
[133] Fix | Delete
[134] Fix | Delete
if ( $showmap && true === $goodmap ) {
[135] Fix | Delete
/**
[136] Fix | Delete
* Set a Google Maps API Key.
[137] Fix | Delete
*
[138] Fix | Delete
* @since 4.1.0
[139] Fix | Delete
*
[140] Fix | Delete
* @param string $api_key Google Maps API Key
[141] Fix | Delete
*/
[142] Fix | Delete
$api_key = apply_filters( 'jetpack_google_maps_api_key', $instance['apikey'] );
[143] Fix | Delete
echo $this->build_map( $instance['address'], $api_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
[144] Fix | Delete
} elseif ( $showmap && is_customize_preview() && true !== $goodmap ) {
[145] Fix | Delete
printf(
[146] Fix | Delete
'<span class="contact-map-api-error" style="display: block;">%s</span>',
[147] Fix | Delete
esc_html( $instance['goodmap'] )
[148] Fix | Delete
);
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
$map_link = $this->build_map_link( $instance['address'] );
[152] Fix | Delete
[153] Fix | Delete
printf(
[154] Fix | Delete
'<div class="confit-address" itemscope itemtype="http://schema.org/PostalAddress" itemprop="address"><a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a></div>',
[155] Fix | Delete
esc_url( $map_link ),
[156] Fix | Delete
str_replace( "\n", '<br/>', esc_html( $instance['address'] ) ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
[157] Fix | Delete
);
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
if ( '' !== $instance['phone'] ) {
[161] Fix | Delete
if ( wp_is_mobile() ) {
[162] Fix | Delete
echo '<div class="confit-phone"><span itemprop="telephone"><a href="' . esc_url( 'tel:' . $instance['phone'] ) . '">' . esc_html( $instance['phone'] ) . '</a></span></div>';
[163] Fix | Delete
} else {
[164] Fix | Delete
echo '<div class="confit-phone"><span itemprop="telephone">' . esc_html( $instance['phone'] ) . '</span></div>';
[165] Fix | Delete
}
[166] Fix | Delete
}
[167] Fix | Delete
[168] Fix | Delete
if (
[169] Fix | Delete
$instance['email']
[170] Fix | Delete
&& is_email( trim( $instance['email'] ) )
[171] Fix | Delete
) {
[172] Fix | Delete
printf(
[173] Fix | Delete
'<div class="confit-email"><a href="mailto:%1$s">%1$s</a></div>',
[174] Fix | Delete
esc_html( $instance['email'] )
[175] Fix | Delete
);
[176] Fix | Delete
}
[177] Fix | Delete
[178] Fix | Delete
if ( '' !== $instance['hours'] ) {
[179] Fix | Delete
printf(
[180] Fix | Delete
'<div class="confit-hours" itemprop="openingHours">%s</div>',
[181] Fix | Delete
str_replace( "\n", '<br/>', esc_html( $instance['hours'] ) ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
[182] Fix | Delete
);
[183] Fix | Delete
}
[184] Fix | Delete
[185] Fix | Delete
echo '</div>';
[186] Fix | Delete
[187] Fix | Delete
/**
[188] Fix | Delete
* Fires at the end of Contact Info widget.
[189] Fix | Delete
*
[190] Fix | Delete
* @module widgets
[191] Fix | Delete
*
[192] Fix | Delete
* @since 3.9.2
[193] Fix | Delete
*/
[194] Fix | Delete
do_action( 'jetpack_contact_info_widget_end' );
[195] Fix | Delete
[196] Fix | Delete
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
[197] Fix | Delete
[198] Fix | Delete
/** This action is documented in modules/widgets/gravatar-profile.php */
[199] Fix | Delete
do_action( 'jetpack_stats_extra', 'widget_view', 'contact_info' );
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
/**
[203] Fix | Delete
* Deals with the settings when they are saved by the admin. Here is
[204] Fix | Delete
* where any validation should be dealt with.
[205] Fix | Delete
*
[206] Fix | Delete
* @param array $new_instance New configuration values.
[207] Fix | Delete
* @param array $old_instance Old configuration values.
[208] Fix | Delete
*
[209] Fix | Delete
* @return array
[210] Fix | Delete
*/
[211] Fix | Delete
public function update( $new_instance, $old_instance ) {
[212] Fix | Delete
[213] Fix | Delete
$instance = array();
[214] Fix | Delete
$instance['title'] = wp_kses( $new_instance['title'], array() );
[215] Fix | Delete
$instance['address'] = wp_kses( $new_instance['address'], array() );
[216] Fix | Delete
$instance['phone'] = wp_kses( $new_instance['phone'], array() );
[217] Fix | Delete
$instance['email'] = wp_kses( $new_instance['email'], array() );
[218] Fix | Delete
$instance['hours'] = wp_kses( $new_instance['hours'], array() );
[219] Fix | Delete
$instance['apikey'] = wp_kses( isset( $new_instance['apikey'] ) ? $new_instance['apikey'] : $old_instance['apikey'], array() );
[220] Fix | Delete
[221] Fix | Delete
if ( ! isset( $new_instance['showmap'] ) ) {
[222] Fix | Delete
$instance['showmap'] = 0;
[223] Fix | Delete
} else {
[224] Fix | Delete
$instance['showmap'] = (int) $new_instance['showmap'];
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
$instance['goodmap'] = $this->update_goodmap( $old_instance, $instance );
[228] Fix | Delete
[229] Fix | Delete
return $instance;
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
/**
[233] Fix | Delete
* Displays the form for this widget on the Widgets page of the WP Admin area.
[234] Fix | Delete
*
[235] Fix | Delete
* @param array $instance Instance configuration.
[236] Fix | Delete
*
[237] Fix | Delete
* @return string|void
[238] Fix | Delete
*/
[239] Fix | Delete
public function form( $instance ) {
[240] Fix | Delete
$instance = wp_parse_args( $instance, $this->defaults() );
[241] Fix | Delete
/** This filter is documented in modules/widgets/contact-info.php */
[242] Fix | Delete
$apikey = apply_filters( 'jetpack_google_maps_api_key', $instance['apikey'] );
[243] Fix | Delete
[244] Fix | Delete
wp_enqueue_script(
[245] Fix | Delete
'contact-info-admin',
[246] Fix | Delete
Assets::get_file_url_for_environment(
[247] Fix | Delete
'_inc/build/widgets/contact-info/contact-info-admin.min.js',
[248] Fix | Delete
'modules/widgets/contact-info/contact-info-admin.js'
[249] Fix | Delete
),
[250] Fix | Delete
array( 'jquery' ),
[251] Fix | Delete
20160727,
[252] Fix | Delete
false
[253] Fix | Delete
);
[254] Fix | Delete
[255] Fix | Delete
if ( is_customize_preview() ) {
[256] Fix | Delete
$customize_contact_info_api_key_nonce = wp_create_nonce( 'customize_contact_info_api_key' );
[257] Fix | Delete
wp_localize_script(
[258] Fix | Delete
'contact-info-admin',
[259] Fix | Delete
'contact_info_api_key_ajax_obj',
[260] Fix | Delete
array( 'nonce' => $customize_contact_info_api_key_nonce )
[261] Fix | Delete
);
[262] Fix | Delete
}
[263] Fix | Delete
[264] Fix | Delete
?>
[265] Fix | Delete
<p>
[266] Fix | Delete
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
[267] Fix | Delete
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
[268] Fix | Delete
</p>
[269] Fix | Delete
[270] Fix | Delete
<p>
[271] Fix | Delete
<label for="<?php echo esc_attr( $this->get_field_id( 'address' ) ); ?>"><?php esc_html_e( 'Address:', 'jetpack' ); ?></label>
[272] Fix | Delete
<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'address' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'address' ) ); ?>"><?php echo esc_textarea( $instance['address'] ); ?></textarea>
[273] Fix | Delete
</p>
[274] Fix | Delete
[275] Fix | Delete
<p>
[276] Fix | Delete
<input class="jp-contact-info-showmap" id="<?php echo esc_attr( $this->get_field_id( 'showmap' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'showmap' ) ); ?>" value="1" type="checkbox" <?php checked( $instance['showmap'], 1 ); ?> />
[277] Fix | Delete
<label for="<?php echo esc_attr( $this->get_field_id( 'showmap' ) ); ?>"><?php esc_html_e( 'Show map', 'jetpack' ); ?></label>
[278] Fix | Delete
</p>
[279] Fix | Delete
[280] Fix | Delete
<?php if ( ! has_filter( 'jetpack_google_maps_api_key' ) || false === apply_filters( 'jetpack_google_maps_api_key', false ) ) { ?>
[281] Fix | Delete
[282] Fix | Delete
<p class="jp-contact-info-admin-map" style="<?php echo $instance['showmap'] ? '' : 'display: none;'; ?>">
[283] Fix | Delete
<label for="<?php echo esc_attr( $this->get_field_id( 'apikey' ) ); ?>">
[284] Fix | Delete
<?php esc_html_e( 'Google Maps API Key', 'jetpack' ); ?>
[285] Fix | Delete
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'apikey' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'apikey' ) ); ?>" type="text" value="<?php echo esc_attr( $apikey ); ?>" />
[286] Fix | Delete
<br />
[287] Fix | Delete
<small>
[288] Fix | Delete
<?php
[289] Fix | Delete
printf(
[290] Fix | Delete
wp_kses(
[291] Fix | Delete
/* Translators: placeholder is a URL to support documentation. */
[292] Fix | Delete
__( 'Google now requires an API key to use their maps on your site. <a href="%s">See our documentation</a> for instructions on acquiring a key.', 'jetpack' ),
[293] Fix | Delete
array(
[294] Fix | Delete
'a' => array(
[295] Fix | Delete
'href' => true,
[296] Fix | Delete
),
[297] Fix | Delete
)
[298] Fix | Delete
),
[299] Fix | Delete
( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? 'https://wordpress.com/support/widgets/contact-info/' : esc_url( Redirect::get_url( 'jetpack-support-extra-sidebar-widgets-contact-info-widget' ) )
[300] Fix | Delete
);
[301] Fix | Delete
?>
[302] Fix | Delete
</small>
[303] Fix | Delete
</label>
[304] Fix | Delete
</p>
[305] Fix | Delete
[306] Fix | Delete
<?php } else { ?>
[307] Fix | Delete
[308] Fix | Delete
<input type="hidden" id="<?php echo esc_attr( $this->get_field_id( 'apikey' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'apikey' ) ); ?>" value="<?php echo esc_attr( $apikey ); ?>" />
[309] Fix | Delete
[310] Fix | Delete
<?php } // end if jetpack_google_maps_api_key check. ?>
[311] Fix | Delete
[312] Fix | Delete
<p class="jp-contact-info-admin-map jp-contact-info-embed-map" style="<?php echo $instance['showmap'] ? '' : 'display: none;'; ?>">
[313] Fix | Delete
<?php
[314] Fix | Delete
if ( ! is_customize_preview() && true === $instance['goodmap'] ) {
[315] Fix | Delete
echo $this->build_map( $instance['address'], $apikey ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
[316] Fix | Delete
} elseif ( true !== $instance['goodmap'] && ! empty( $instance['goodmap'] ) ) {
[317] Fix | Delete
printf(
[318] Fix | Delete
'<span class="button-link-delete">%s</span>',
[319] Fix | Delete
esc_html( $instance['goodmap'] )
[320] Fix | Delete
);
[321] Fix | Delete
}
[322] Fix | Delete
?>
[323] Fix | Delete
</p>
[324] Fix | Delete
[325] Fix | Delete
<p>
[326] Fix | Delete
<label for="<?php echo esc_attr( $this->get_field_id( 'phone' ) ); ?>"><?php esc_html_e( 'Phone:', 'jetpack' ); ?></label>
[327] Fix | Delete
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'phone' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'phone' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['phone'] ); ?>" />
[328] Fix | Delete
</p>
[329] Fix | Delete
[330] Fix | Delete
<p>
[331] Fix | Delete
<label for="<?php echo esc_attr( $this->get_field_id( 'email' ) ); ?>"><?php esc_html_e( 'Email Address:', 'jetpack' ); ?></label>
[332] Fix | Delete
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'email' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'email' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['email'] ); ?>" />
[333] Fix | Delete
</p>
[334] Fix | Delete
[335] Fix | Delete
<p>
[336] Fix | Delete
<label for="<?php echo esc_attr( $this->get_field_id( 'hours' ) ); ?>"><?php esc_html_e( 'Hours:', 'jetpack' ); ?></label>
[337] Fix | Delete
<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'hours' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'hours' ) ); ?>"><?php echo esc_textarea( $instance['hours'] ); ?></textarea>
[338] Fix | Delete
</p>
[339] Fix | Delete
[340] Fix | Delete
<?php
[341] Fix | Delete
}
[342] Fix | Delete
[343] Fix | Delete
/**
[344] Fix | Delete
* Generate a Google Maps link for the supplied address.
[345] Fix | Delete
*
[346] Fix | Delete
* @param string $address Address to link to.
[347] Fix | Delete
*
[348] Fix | Delete
* @return string
[349] Fix | Delete
*/
[350] Fix | Delete
private function build_map_link( $address ) {
[351] Fix | Delete
// Google map urls have lots of available params but zoom (z) and query (q) are enough.
[352] Fix | Delete
return 'https://maps.google.com/maps?z=16&q=' . $this->urlencode_address( $address );
[353] Fix | Delete
}
[354] Fix | Delete
[355] Fix | Delete
/**
[356] Fix | Delete
* Builds map display HTML code from the supplied address.
[357] Fix | Delete
*
[358] Fix | Delete
* @param string $address Address.
[359] Fix | Delete
* @param string $api_key API Key.
[360] Fix | Delete
*
[361] Fix | Delete
* @return string HTML of the map.
[362] Fix | Delete
*/
[363] Fix | Delete
private function build_map( $address, $api_key = null ) {
[364] Fix | Delete
$this->enqueue_scripts();
[365] Fix | Delete
$src = add_query_arg( 'q', rawurlencode( $address ), 'https://www.google.com/maps/embed/v1/place' );
[366] Fix | Delete
if ( ! empty( $api_key ) ) {
[367] Fix | Delete
$src = add_query_arg( 'key', $api_key, $src );
[368] Fix | Delete
}
[369] Fix | Delete
[370] Fix | Delete
$height = 216;
[371] Fix | Delete
[372] Fix | Delete
$iframe_attributes = sprintf(
[373] Fix | Delete
' height="%d" frameborder="0" src="%s" title="%s" class="contact-map"',
[374] Fix | Delete
esc_attr( $height ),
[375] Fix | Delete
esc_url( $src ),
[376] Fix | Delete
__( 'Google Map Embed', 'jetpack' )
[377] Fix | Delete
);
[378] Fix | Delete
[379] Fix | Delete
$iframe_html = sprintf( '<iframe width="600" %s></iframe>', $iframe_attributes );
[380] Fix | Delete
[381] Fix | Delete
if (
[382] Fix | Delete
! class_exists( 'Jetpack_AMP_Support' )
[383] Fix | Delete
|| ! Jetpack_AMP_Support::is_amp_request()
[384] Fix | Delete
) {
[385] Fix | Delete
return $iframe_html;
[386] Fix | Delete
}
[387] Fix | Delete
[388] Fix | Delete
$amp_iframe_html = sprintf( '<amp-iframe layout="fixed-height" width="auto" sandbox="allow-scripts allow-same-origin" %s>', $iframe_attributes );
[389] Fix | Delete
[390] Fix | Delete
// Add placeholder to avoid AMP error: <amp-iframe> elements must be positioned outside the first 75% of the viewport or 600px from the top (whichever is smaller).
[391] Fix | Delete
$amp_iframe_html .= sprintf( '<span placeholder>%s</span>', esc_html__( 'Loading map&hellip;', 'jetpack' ) );
[392] Fix | Delete
[393] Fix | Delete
// Add original iframe as fallback in case JavaScript is disabled.
[394] Fix | Delete
$amp_iframe_html .= sprintf( '<noscript>%s</noscript>', $iframe_html );
[395] Fix | Delete
[396] Fix | Delete
$amp_iframe_html .= '</amp-iframe>';
[397] Fix | Delete
return $amp_iframe_html;
[398] Fix | Delete
}
[399] Fix | Delete
[400] Fix | Delete
/**
[401] Fix | Delete
* Encode an URL
[402] Fix | Delete
*
[403] Fix | Delete
* @param string $address The URL to encode.
[404] Fix | Delete
*
[405] Fix | Delete
* @return string The encoded URL
[406] Fix | Delete
*/
[407] Fix | Delete
private function urlencode_address( $address ) {
[408] Fix | Delete
[409] Fix | Delete
$address = strtolower( $address );
[410] Fix | Delete
// Get rid of any unwanted whitespace.
[411] Fix | Delete
$address = preg_replace( '/\s+/', ' ', trim( $address ) );
[412] Fix | Delete
// Use + not %20.
[413] Fix | Delete
$address = str_ireplace( ' ', '+', $address );
[414] Fix | Delete
return rawurlencode( $address );
[415] Fix | Delete
}
[416] Fix | Delete
[417] Fix | Delete
/**
[418] Fix | Delete
* Returns the instance's updated 'goodmap' value.
[419] Fix | Delete
*
[420] Fix | Delete
* @param array $old_instance Old configuration values.
[421] Fix | Delete
* @param array $instance Current configuration values.
[422] Fix | Delete
*
[423] Fix | Delete
* @return bool|string The instance's updated 'goodmap' value. The value is true if
[424] Fix | Delete
* $instance can display a good map. If not, returns an error message.
[425] Fix | Delete
*/
[426] Fix | Delete
private function update_goodmap( $old_instance, $instance ) {
[427] Fix | Delete
/*
[428] Fix | Delete
* If we have no address or don't want to show a map,
[429] Fix | Delete
* no need to check if the map is valid.
[430] Fix | Delete
*/
[431] Fix | Delete
if ( empty( $instance['address'] ) || 0 === $instance['showmap'] ) {
[432] Fix | Delete
return false;
[433] Fix | Delete
}
[434] Fix | Delete
[435] Fix | Delete
/*
[436] Fix | Delete
* If there have been any changes that may impact the map in the widget
[437] Fix | Delete
* (adding an address, address changes, new API key, API key change)
[438] Fix | Delete
* then we want to check whether our map can be displayed again.
[439] Fix | Delete
*/
[440] Fix | Delete
if (
[441] Fix | Delete
! isset( $instance['goodmap'] )
[442] Fix | Delete
|| ! isset( $old_instance['address'] )
[443] Fix | Delete
|| $this->urlencode_address( $old_instance['address'] ) !== $this->urlencode_address( $instance['address'] )
[444] Fix | Delete
|| ! isset( $old_instance['apikey'] )
[445] Fix | Delete
|| $old_instance['apikey'] !== $instance['apikey']
[446] Fix | Delete
) {
[447] Fix | Delete
return $this->has_good_map( $instance );
[448] Fix | Delete
} else {
[449] Fix | Delete
return $instance['goodmap'];
[450] Fix | Delete
}
[451] Fix | Delete
}
[452] Fix | Delete
[453] Fix | Delete
/**
[454] Fix | Delete
* Check if the instance has a valid Map location.
[455] Fix | Delete
*
[456] Fix | Delete
* @param array $instance Widget instance configuration.
[457] Fix | Delete
*
[458] Fix | Delete
* @return bool|string Whether or not there is a valid map. If not, return an error message.
[459] Fix | Delete
*/
[460] Fix | Delete
private function has_good_map( $instance ) {
[461] Fix | Delete
/** This filter is documented in modules/widgets/contact-info.php */
[462] Fix | Delete
$api_key = apply_filters( 'jetpack_google_maps_api_key', $instance['apikey'] );
[463] Fix | Delete
if ( ! empty( $api_key ) ) {
[464] Fix | Delete
$path = add_query_arg(
[465] Fix | Delete
array(
[466] Fix | Delete
'q' => rawurlencode( $instance['address'] ),
[467] Fix | Delete
'key' => $api_key,
[468] Fix | Delete
),
[469] Fix | Delete
'https://www.google.com/maps/embed/v1/place'
[470] Fix | Delete
);
[471] Fix | Delete
$wp_remote_get_args = array(
[472] Fix | Delete
'headers' => array( 'Referer' => home_url() ),
[473] Fix | Delete
);
[474] Fix | Delete
$response = wp_remote_get( esc_url_raw( $path ), $wp_remote_get_args );
[475] Fix | Delete
[476] Fix | Delete
if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
[477] Fix | Delete
return true;
[478] Fix | Delete
} else {
[479] Fix | Delete
return wp_remote_retrieve_body( $response );
[480] Fix | Delete
}
[481] Fix | Delete
}
[482] Fix | Delete
[483] Fix | Delete
return __( 'Please enter a valid Google API Key.', 'jetpack' );
[484] Fix | Delete
}
[485] Fix | Delete
[486] Fix | Delete
/**
[487] Fix | Delete
* Check the Google Maps API key after an Ajax call from the widget's admin form in
[488] Fix | Delete
* the Customizer preview.
[489] Fix | Delete
*/
[490] Fix | Delete
public function ajax_check_api_key() {
[491] Fix | Delete
if ( isset( $_POST['apikey'] ) ) {
[492] Fix | Delete
if ( check_ajax_referer( 'customize_contact_info_api_key' ) && current_user_can( 'customize' ) ) {
[493] Fix | Delete
$apikey = wp_kses( wp_unslash( $_POST['apikey'] ), array() );
[494] Fix | Delete
$default_instance = $this->defaults();
[495] Fix | Delete
$default_instance['apikey'] = $apikey;
[496] Fix | Delete
// @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- It takes null, but its phpdoc only says int.
[497] Fix | Delete
wp_send_json( array( 'result' => esc_html( $this->has_good_map( $default_instance ) ) ), null, JSON_UNESCAPED_SLASHES );
[498] Fix | Delete
}
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function