Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/extensio.../blocks/map
File: map.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Map block.
[2] Fix | Delete
*
[3] Fix | Delete
* @since 6.8.0
[4] Fix | Delete
*
[5] Fix | Delete
* @package automattic/jetpack
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
namespace Automattic\Jetpack\Extensions\Map;
[9] Fix | Delete
[10] Fix | Delete
use Automattic\Jetpack\Blocks;
[11] Fix | Delete
use Automattic\Jetpack\Status\Host;
[12] Fix | Delete
use Automattic\Jetpack\Tracking;
[13] Fix | Delete
use Jetpack;
[14] Fix | Delete
use Jetpack_Gutenberg;
[15] Fix | Delete
use Jetpack_Mapbox_Helper;
[16] Fix | Delete
[17] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[18] Fix | Delete
exit( 0 );
[19] Fix | Delete
}
[20] Fix | Delete
[21] Fix | Delete
if ( ! class_exists( 'Jetpack_Mapbox_Helper' ) ) {
[22] Fix | Delete
require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-mapbox-helper.php';
[23] Fix | Delete
}
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* Registers the block for use in Gutenberg
[27] Fix | Delete
* This is done via an action so that we can disable
[28] Fix | Delete
* registration if we need to.
[29] Fix | Delete
*/
[30] Fix | Delete
function register_block() {
[31] Fix | Delete
Blocks::jetpack_register_block(
[32] Fix | Delete
__DIR__,
[33] Fix | Delete
array(
[34] Fix | Delete
'render_callback' => __NAMESPACE__ . '\load_assets',
[35] Fix | Delete
)
[36] Fix | Delete
);
[37] Fix | Delete
}
[38] Fix | Delete
add_action( 'init', __NAMESPACE__ . '\register_block' );
[39] Fix | Delete
[40] Fix | Delete
/**
[41] Fix | Delete
* Record a Tracks event every time the Map block is loaded on WordPress.com and Atomic.
[42] Fix | Delete
*
[43] Fix | Delete
* @param string $access_token_source The Mapbox API access token source.
[44] Fix | Delete
*/
[45] Fix | Delete
function wpcom_load_event( $access_token_source ) {
[46] Fix | Delete
if ( 'wpcom' !== $access_token_source ) {
[47] Fix | Delete
return;
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
$event_name = 'map_block_mapbox_wpcom_key_load';
[51] Fix | Delete
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
[52] Fix | Delete
require_lib( 'tracks/client' );
[53] Fix | Delete
tracks_record_event( wp_get_current_user(), $event_name );
[54] Fix | Delete
} elseif ( ( new Host() )->is_woa_site() && Jetpack::is_connection_ready() ) {
[55] Fix | Delete
$tracking = new Tracking();
[56] Fix | Delete
$tracking->record_user_event( $event_name );
[57] Fix | Delete
}
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
/**
[61] Fix | Delete
* Function to determine which map provider to choose
[62] Fix | Delete
*
[63] Fix | Delete
* @param string $html The block's HTML - needed for the class name.
[64] Fix | Delete
*
[65] Fix | Delete
* @return string The name of the map provider.
[66] Fix | Delete
*/
[67] Fix | Delete
function get_map_provider( $html ) {
[68] Fix | Delete
$mapbox_styles = array( 'is-style-terrain' );
[69] Fix | Delete
// return mapbox if html contains one of the mapbox styles
[70] Fix | Delete
foreach ( $mapbox_styles as $style ) {
[71] Fix | Delete
if ( str_contains( $html, $style ) ) {
[72] Fix | Delete
return 'mapbox';
[73] Fix | Delete
}
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
// you can override the map provider with a cookie
[77] Fix | Delete
if ( isset( $_COOKIE['map_provider'] ) ) {
[78] Fix | Delete
return sanitize_text_field( wp_unslash( $_COOKIE['map_provider'] ) );
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
// if we don't apply the filters & default to mapbox
[82] Fix | Delete
return apply_filters( 'wpcom_map_block_map_provider', 'mapbox' );
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
/**
[86] Fix | Delete
* Map block registration/dependency declaration.
[87] Fix | Delete
*
[88] Fix | Delete
* @param array $attr Array containing the map block attributes.
[89] Fix | Delete
* @param string $content String containing the map block content.
[90] Fix | Delete
*
[91] Fix | Delete
* @return string
[92] Fix | Delete
*/
[93] Fix | Delete
function load_assets( $attr, $content ) {
[94] Fix | Delete
$access_token = Jetpack_Mapbox_Helper::get_access_token();
[95] Fix | Delete
wpcom_load_event( $access_token['source'] );
[96] Fix | Delete
[97] Fix | Delete
if ( Blocks::is_amp_request() ) {
[98] Fix | Delete
static $map_block_counter = array();
[99] Fix | Delete
[100] Fix | Delete
$id = get_the_ID();
[101] Fix | Delete
if ( ! isset( $map_block_counter[ $id ] ) ) {
[102] Fix | Delete
$map_block_counter[ $id ] = 0;
[103] Fix | Delete
}
[104] Fix | Delete
++$map_block_counter[ $id ];
[105] Fix | Delete
[106] Fix | Delete
$iframe_url = add_query_arg(
[107] Fix | Delete
array(
[108] Fix | Delete
'map-block-counter' => absint( $map_block_counter[ $id ] ),
[109] Fix | Delete
'map-block-post-id' => $id,
[110] Fix | Delete
),
[111] Fix | Delete
get_permalink()
[112] Fix | Delete
);
[113] Fix | Delete
[114] Fix | Delete
$placeholder = preg_replace( '/(?<=<div\s)/', 'placeholder ', $content );
[115] Fix | Delete
[116] Fix | Delete
return sprintf(
[117] Fix | Delete
'<amp-iframe src="%s" width="%d" height="%d" layout="responsive" allowfullscreen sandbox="allow-scripts">%s</amp-iframe>',
[118] Fix | Delete
esc_url( $iframe_url ),
[119] Fix | Delete
4,
[120] Fix | Delete
3,
[121] Fix | Delete
$placeholder
[122] Fix | Delete
);
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
[126] Fix | Delete
[127] Fix | Delete
$map_provider = get_map_provider( $content );
[128] Fix | Delete
if ( $map_provider === 'mapkit' ) {
[129] Fix | Delete
return preg_replace( '/<div /', '<div data-map-provider="mapkit" data-api-key="' . esc_attr( $access_token['key'] ) . '" data-blog-id="' . \Jetpack_Options::get_option( 'id' ) . '" ', $content, 1 );
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
return preg_replace( '/<div /', '<div data-map-provider="mapbox" data-api-key="' . esc_attr( $access_token['key'] ) . '" ', $content, 1 );
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
/**
[136] Fix | Delete
* Render a page containing only a single Map block.
[137] Fix | Delete
*/
[138] Fix | Delete
function render_single_block_page() {
[139] Fix | Delete
// phpcs:ignore WordPress.Security.NonceVerification
[140] Fix | Delete
$map_block_counter = isset( $_GET['map-block-counter'] ) ? absint( $_GET['map-block-counter'] ) : null;
[141] Fix | Delete
// phpcs:ignore WordPress.Security.NonceVerification
[142] Fix | Delete
$map_block_post_id = isset( $_GET['map-block-post-id'] ) ? absint( $_GET['map-block-post-id'] ) : null;
[143] Fix | Delete
[144] Fix | Delete
if ( ! $map_block_counter || ! $map_block_post_id ) {
[145] Fix | Delete
return;
[146] Fix | Delete
}
[147] Fix | Delete
[148] Fix | Delete
/* Create an array of all root-level DIVs that are Map Blocks */
[149] Fix | Delete
$post = get_post( $map_block_post_id );
[150] Fix | Delete
[151] Fix | Delete
if ( ! class_exists( 'DOMDocument' ) ) {
[152] Fix | Delete
return;
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
$post_html = new \DOMDocument();
[156] Fix | Delete
/** This filter is already documented in core/wp-includes/post-template.php */
[157] Fix | Delete
$content = apply_filters( 'the_content', $post->post_content );
[158] Fix | Delete
[159] Fix | Delete
// Return early if empty to prevent DOMDocument::loadHTML fatal.
[160] Fix | Delete
if ( empty( $content ) ) {
[161] Fix | Delete
return;
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
/* Suppress warnings */
[165] Fix | Delete
libxml_use_internal_errors( true );
[166] Fix | Delete
@$post_html->loadHTML( $content ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
[167] Fix | Delete
libxml_use_internal_errors( false );
[168] Fix | Delete
[169] Fix | Delete
$xpath = new \DOMXPath( $post_html );
[170] Fix | Delete
$container = $xpath->query( '//div[ contains( @class, "wp-block-jetpack-map" ) ]' )->item( $map_block_counter - 1 );
[171] Fix | Delete
[172] Fix | Delete
/* Check that we have a block matching the counter position */
[173] Fix | Delete
if ( ! $container ) {
[174] Fix | Delete
return;
[175] Fix | Delete
}
[176] Fix | Delete
[177] Fix | Delete
/* Compile scripts and styles */
[178] Fix | Delete
ob_start();
[179] Fix | Delete
[180] Fix | Delete
add_filter( 'jetpack_is_amp_request', '__return_false' );
[181] Fix | Delete
[182] Fix | Delete
Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
[183] Fix | Delete
wp_scripts()->do_items();
[184] Fix | Delete
wp_styles()->do_items();
[185] Fix | Delete
[186] Fix | Delete
add_filter( 'jetpack_is_amp_request', '__return_true' );
[187] Fix | Delete
[188] Fix | Delete
$head_content = ob_get_clean();
[189] Fix | Delete
[190] Fix | Delete
/* Put together a new complete document containing only the requested block markup and the scripts/styles needed to render it */
[191] Fix | Delete
$block_markup = $post_html->saveHTML( $container );
[192] Fix | Delete
$access_token = Jetpack_Mapbox_Helper::get_access_token();
[193] Fix | Delete
$page_html = sprintf(
[194] Fix | Delete
'<!DOCTYPE html><head><style>html, body { margin: 0; padding: 0; }</style>%s</head><body>%s</body>',
[195] Fix | Delete
$head_content,
[196] Fix | Delete
preg_replace( '/(?<=<div\s)/', 'data-api-key="' . esc_attr( $access_token['key'] ) . '" ', $block_markup, 1 )
[197] Fix | Delete
);
[198] Fix | Delete
echo $page_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
[199] Fix | Delete
exit( 0 );
[200] Fix | Delete
}
[201] Fix | Delete
add_action( 'wp', __NAMESPACE__ . '\render_single_block_page' );
[202] Fix | Delete
[203] Fix | Delete
/**
[204] Fix | Delete
* Helper function to generate the markup of the block in PHP.
[205] Fix | Delete
*
[206] Fix | Delete
* @param Array $points - Array containing geo location points.
[207] Fix | Delete
*
[208] Fix | Delete
* @return string Markup for the jetpack/map block.
[209] Fix | Delete
*/
[210] Fix | Delete
function map_block_from_geo_points( $points ) {
[211] Fix | Delete
$map_block_data = array(
[212] Fix | Delete
'points' => $points,
[213] Fix | Delete
'zoom' => 8,
[214] Fix | Delete
'mapCenter' => array(
[215] Fix | Delete
'lng' => $points[0]['coordinates']['longitude'],
[216] Fix | Delete
'lat' => $points[0]['coordinates']['latitude'],
[217] Fix | Delete
),
[218] Fix | Delete
);
[219] Fix | Delete
[220] Fix | Delete
$list_items = array_map(
[221] Fix | Delete
function ( $point ) {
[222] Fix | Delete
$link = add_query_arg(
[223] Fix | Delete
array(
[224] Fix | Delete
'api' => 1,
[225] Fix | Delete
'query' => $point['coordinates']['latitude'] . ',' . $point['coordinates']['longitude'],
[226] Fix | Delete
),
[227] Fix | Delete
'https://www.google.com/maps/search/'
[228] Fix | Delete
);
[229] Fix | Delete
return sprintf( '<li><a href="%s">%s</a></li>', esc_url( $link ), $point['title'] );
[230] Fix | Delete
},
[231] Fix | Delete
$points
[232] Fix | Delete
);
[233] Fix | Delete
[234] Fix | Delete
$map_block = '<!-- wp:jetpack/map ' . wp_json_encode( $map_block_data, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ' -->' . PHP_EOL;
[235] Fix | Delete
$map_block .= sprintf(
[236] Fix | Delete
'<div class="wp-block-jetpack-map" data-map-style="default" data-map-details="true" data-points="%1$s" data-zoom="%2$d" data-map-center="%3$s" data-marker-color="red" data-show-fullscreen-button="true">',
[237] Fix | Delete
esc_attr( wp_json_encode( $map_block_data['points'], JSON_HEX_AMP | JSON_UNESCAPED_SLASHES ) ),
[238] Fix | Delete
$map_block_data['zoom'],
[239] Fix | Delete
esc_attr( wp_json_encode( $map_block_data['mapCenter'], JSON_HEX_AMP | JSON_UNESCAPED_SLASHES ) )
[240] Fix | Delete
);
[241] Fix | Delete
$map_block .= '<ul>' . implode( "\n", $list_items ) . '</ul>';
[242] Fix | Delete
$map_block .= '</div>' . PHP_EOL;
[243] Fix | Delete
$map_block .= '<!-- /wp:jetpack/map -->';
[244] Fix | Delete
[245] Fix | Delete
return $map_block;
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function