'jetpack_map_block_load_assets',
)
);
/**
* Map block registration/dependency declaration.
*
* @param array $attr Array containing the map block attributes.
* @param string $content String containing the map block content.
*
* @return string
*/
function jetpack_map_block_load_assets( $attr, $content ) {
$api_key = Jetpack_Options::get_option( 'mapbox_api_key' );
if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
static $map_block_counter = array();
$id = get_the_ID();
if ( ! isset( $map_block_counter[ $id ] ) ) {
$map_block_counter[ $id ] = 0;
}
$map_block_counter[ $id ]++;
$iframe_url = add_query_arg(
array(
'map-block-counter' => absint( $map_block_counter[ $id ] ),
'map-block-post-id' => $id,
),
get_permalink()
);
$placeholder = preg_replace( '/(?<=
%s',
esc_url( $iframe_url ),
4,
3,
$placeholder
);
}
Jetpack_Gutenberg::load_assets_as_required( 'map' );
return preg_replace( '/
post_content );
/* Suppress warnings */
libxml_use_internal_errors( true );
@$post_html->loadHTML( $content ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
libxml_use_internal_errors( false );
$xpath = new DOMXPath( $post_html );
$container = $xpath->query( '//div[ contains( @class, "wp-block-jetpack-map" ) ]' )->item( $map_block_counter - 1 );
/* Check that we have a block matching the counter position */
if ( ! $container ) {
return;
}
/* Compile scripts and styles */
ob_start();
add_filter( 'jetpack_is_amp_request', '__return_false' );
Jetpack_Gutenberg::load_assets_as_required( 'map' );
wp_scripts()->do_items();
wp_styles()->do_items();
add_filter( 'jetpack_is_amp_request', '__return_true' );
$head_content = ob_get_clean();
/* Put together a new complete document containing only the requested block markup and the scripts/styles needed to render it */
$block_markup = $post_html->saveHTML( $container );
$api_key = Jetpack_Options::get_option( 'mapbox_api_key' );
$page_html = sprintf(
'%s%s',
$head_content,
preg_replace( '/(?<=