2019-11-15 23:26:29 +01:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Map block.
|
|
|
|
|
*
|
|
|
|
|
* @since 6.8.0
|
|
|
|
|
*
|
|
|
|
|
* @package Jetpack
|
|
|
|
|
*/
|
|
|
|
|
|
2020-05-06 17:20:49 +02:00
|
|
|
namespace Automattic\Jetpack\Extensions\Map;
|
|
|
|
|
|
2020-10-20 18:05:12 +02:00
|
|
|
use Automattic\Jetpack\Blocks;
|
2020-05-06 17:20:49 +02:00
|
|
|
use Automattic\Jetpack\Tracking;
|
|
|
|
|
use Jetpack;
|
|
|
|
|
use Jetpack_Gutenberg;
|
|
|
|
|
use Jetpack_Mapbox_Helper;
|
|
|
|
|
use Jetpack_Options;
|
|
|
|
|
|
|
|
|
|
const FEATURE_NAME = 'map';
|
|
|
|
|
const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;
|
|
|
|
|
|
2020-03-03 18:49:45 +01:00
|
|
|
if ( ! class_exists( 'Jetpack_Mapbox_Helper' ) ) {
|
2020-05-06 17:20:49 +02:00
|
|
|
\jetpack_require_lib( 'class-jetpack-mapbox-helper' );
|
2020-03-03 18:49:45 +01:00
|
|
|
}
|
|
|
|
|
|
2020-05-06 17:20:49 +02:00
|
|
|
/**
|
|
|
|
|
* Registers the block for use in Gutenberg
|
|
|
|
|
* This is done via an action so that we can disable
|
|
|
|
|
* registration if we need to.
|
|
|
|
|
*/
|
|
|
|
|
function register_block() {
|
|
|
|
|
jetpack_register_block(
|
|
|
|
|
BLOCK_NAME,
|
|
|
|
|
array(
|
|
|
|
|
'render_callback' => __NAMESPACE__ . '\load_assets',
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
add_action( 'init', __NAMESPACE__ . '\register_block' );
|
2019-11-15 23:26:29 +01:00
|
|
|
|
2020-03-03 18:49:45 +01:00
|
|
|
/**
|
|
|
|
|
* Record a Tracks event every time the Map block is loaded on WordPress.com and Atomic.
|
|
|
|
|
*
|
|
|
|
|
* @param string $access_token_source The Mapbox API access token source.
|
|
|
|
|
*/
|
2020-05-06 17:20:49 +02:00
|
|
|
function wpcom_load_event( $access_token_source ) {
|
2020-03-03 18:49:45 +01:00
|
|
|
if ( 'wpcom' !== $access_token_source ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$event_name = 'map_block_mapbox_wpcom_key_load';
|
|
|
|
|
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
|
2020-05-06 17:20:49 +02:00
|
|
|
jetpack_require_lib( 'tracks/client' );
|
2020-03-03 18:49:45 +01:00
|
|
|
tracks_record_event( wp_get_current_user(), $event_name );
|
|
|
|
|
} elseif ( jetpack_is_atomic_site() && Jetpack::is_active() ) {
|
2020-05-06 17:20:49 +02:00
|
|
|
$tracking = new Tracking();
|
2020-03-03 18:49:45 +01:00
|
|
|
$tracking->record_user_event( $event_name );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 23:26:29 +01:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2020-05-06 17:20:49 +02:00
|
|
|
function load_assets( $attr, $content ) {
|
2020-03-03 18:49:45 +01:00
|
|
|
$access_token = Jetpack_Mapbox_Helper::get_access_token();
|
|
|
|
|
|
2020-05-06 17:20:49 +02:00
|
|
|
wpcom_load_event( $access_token['source'] );
|
2019-11-15 23:26:29 +01:00
|
|
|
|
2020-10-20 18:05:12 +02:00
|
|
|
if ( Blocks::is_amp_request() ) {
|
2020-01-26 12:53:20 +01:00
|
|
|
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( '/(?<=<div\s)/', 'placeholder ', $content );
|
|
|
|
|
|
|
|
|
|
return sprintf(
|
|
|
|
|
'<amp-iframe src="%s" width="%d" height="%d" layout="responsive" allowfullscreen sandbox="allow-scripts">%s</amp-iframe>',
|
|
|
|
|
esc_url( $iframe_url ),
|
|
|
|
|
4,
|
|
|
|
|
3,
|
|
|
|
|
$placeholder
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 17:20:49 +02:00
|
|
|
Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
|
2019-11-15 23:26:29 +01:00
|
|
|
|
2020-03-03 18:49:45 +01:00
|
|
|
return preg_replace( '/<div /', '<div data-api-key="' . esc_attr( $access_token['key'] ) . '" ', $content, 1 );
|
2019-11-15 23:26:29 +01:00
|
|
|
}
|
2020-01-26 12:53:20 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Render a page containing only a single Map block.
|
|
|
|
|
*/
|
2020-05-06 17:20:49 +02:00
|
|
|
function render_single_block_page() {
|
2020-01-26 12:53:20 +01:00
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification
|
|
|
|
|
$map_block_counter = isset( $_GET, $_GET['map-block-counter'] ) ? absint( $_GET['map-block-counter'] ) : null;
|
|
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification
|
|
|
|
|
$map_block_post_id = isset( $_GET, $_GET['map-block-post-id'] ) ? absint( $_GET['map-block-post-id'] ) : null;
|
|
|
|
|
|
|
|
|
|
if ( ! $map_block_counter || ! $map_block_post_id ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Create an array of all root-level DIVs that are Map Blocks */
|
|
|
|
|
$post = get_post( $map_block_post_id );
|
|
|
|
|
|
|
|
|
|
if ( ! class_exists( 'DOMDocument' ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 17:20:49 +02:00
|
|
|
$post_html = new \DOMDocument();
|
2020-01-26 12:53:20 +01:00
|
|
|
/** This filter is already documented in core/wp-includes/post-template.php */
|
|
|
|
|
$content = apply_filters( 'the_content', $post->post_content );
|
|
|
|
|
|
|
|
|
|
/* Suppress warnings */
|
|
|
|
|
libxml_use_internal_errors( true );
|
|
|
|
|
@$post_html->loadHTML( $content ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
|
|
|
|
|
libxml_use_internal_errors( false );
|
|
|
|
|
|
2020-05-06 17:20:49 +02:00
|
|
|
$xpath = new \DOMXPath( $post_html );
|
2020-01-26 12:53:20 +01:00
|
|
|
$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' );
|
|
|
|
|
|
2020-05-06 17:20:49 +02:00
|
|
|
Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
|
2020-01-26 12:53:20 +01:00
|
|
|
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 );
|
2020-03-03 18:49:45 +01:00
|
|
|
$access_token = Jetpack_Mapbox_Helper::get_access_token();
|
2020-01-26 12:53:20 +01:00
|
|
|
$page_html = sprintf(
|
|
|
|
|
'<!DOCTYPE html><head><style>html, body { margin: 0; padding: 0; }</style>%s</head><body>%s</body>',
|
|
|
|
|
$head_content,
|
2020-03-03 18:49:45 +01:00
|
|
|
preg_replace( '/(?<=<div\s)/', 'data-api-key="' . esc_attr( $access_token['key'] ) . '" ', $block_markup, 1 )
|
2020-01-26 12:53:20 +01:00
|
|
|
);
|
|
|
|
|
echo $page_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
|
|
|
exit;
|
|
|
|
|
}
|
2020-05-06 17:20:49 +02:00
|
|
|
add_action( 'wp', __NAMESPACE__ . '\render_single_block_page' );
|