Completed
Push — add/amazon ( 56165f )
by
unknown
07:37
created

map.php ➔ jetpack_get_mapbox_api_key()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Map block.
4
 *
5
 * @since 6.8.0
6
 *
7
 * @package Jetpack
8
 */
9
10
jetpack_register_block(
11
	'jetpack/map',
12
	array(
13
		'render_callback' => 'jetpack_map_block_load_assets',
14
	)
15
);
16
17
/**
18
 * Return the site's own Mapbox API key if set, or the WordPress.com's one otherwise.
19
 *
20
 * @return string
21
 */
22
function jetpack_get_mapbox_api_key() {
23
	if ( ! class_exists( 'WPCOM_REST_API_V2_Endpoint_Service_API_Keys' ) || ! Jetpack::is_active() ) {
24
		return Jetpack_Options::get_option( 'mapbox_api_key' );
25
	}
26
	$response = WPCOM_REST_API_V2_Endpoint_Service_API_Keys::get_service_api_key( array( 'service' => 'mapbox' ) );
27
	return $response['service_api_key'];
28
}
29
30
/**
31
 * Map block registration/dependency declaration.
32
 *
33
 * @param array  $attr    Array containing the map block attributes.
34
 * @param string $content String containing the map block content.
35
 *
36
 * @return string
37
 */
38
function jetpack_map_block_load_assets( $attr, $content ) {
39
	$api_key = jetpack_get_mapbox_api_key();
40
41
	if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
42
		static $map_block_counter = array();
43
44
		$id = get_the_ID();
45
		if ( ! isset( $map_block_counter[ $id ] ) ) {
46
			$map_block_counter[ $id ] = 0;
47
		}
48
		$map_block_counter[ $id ]++;
49
50
		$iframe_url = add_query_arg(
51
			array(
52
				'map-block-counter' => absint( $map_block_counter[ $id ] ),
53
				'map-block-post-id' => $id,
54
			),
55
			get_permalink()
56
		);
57
58
		$placeholder = preg_replace( '/(?<=<div\s)/', 'placeholder ', $content );
59
60
		return sprintf(
61
			'<amp-iframe src="%s" width="%d" height="%d" layout="responsive" allowfullscreen sandbox="allow-scripts">%s</amp-iframe>',
62
			esc_url( $iframe_url ),
63
			4,
64
			3,
65
			$placeholder
66
		);
67
	}
68
69
	Jetpack_Gutenberg::load_assets_as_required( 'map' );
70
71
	return preg_replace( '/<div /', '<div data-api-key="' . esc_attr( $api_key ) . '" ', $content, 1 );
72
}
73
74
/**
75
 * Render a page containing only a single Map block.
76
 */
77
function jetpack_map_block_render_single_block_page() {
78
	// phpcs:ignore WordPress.Security.NonceVerification
79
	$map_block_counter = isset( $_GET, $_GET['map-block-counter'] ) ? absint( $_GET['map-block-counter'] ) : null;
80
	// phpcs:ignore WordPress.Security.NonceVerification
81
	$map_block_post_id = isset( $_GET, $_GET['map-block-post-id'] ) ? absint( $_GET['map-block-post-id'] ) : null;
82
83
	if ( ! $map_block_counter || ! $map_block_post_id ) {
84
		return;
85
	}
86
87
	/* Create an array of all root-level DIVs that are Map Blocks */
88
	$post = get_post( $map_block_post_id );
89
90
	if ( ! class_exists( 'DOMDocument' ) ) {
91
		return;
92
	}
93
94
	$post_html = new DOMDocument();
95
	/** This filter is already documented in core/wp-includes/post-template.php */
96
	$content = apply_filters( 'the_content', $post->post_content );
97
98
	/* Suppress warnings */
99
	libxml_use_internal_errors( true );
100
	@$post_html->loadHTML( $content ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
101
	libxml_use_internal_errors( false );
102
103
	$xpath     = new DOMXPath( $post_html );
104
	$container = $xpath->query( '//div[ contains( @class, "wp-block-jetpack-map" ) ]' )->item( $map_block_counter - 1 );
105
106
	/* Check that we have a block matching the counter position */
107
	if ( ! $container ) {
108
		return;
109
	}
110
111
	/* Compile scripts and styles */
112
	ob_start();
113
114
	add_filter( 'jetpack_is_amp_request', '__return_false' );
115
116
	Jetpack_Gutenberg::load_assets_as_required( 'map' );
117
	wp_scripts()->do_items();
118
	wp_styles()->do_items();
119
120
	add_filter( 'jetpack_is_amp_request', '__return_true' );
121
122
	$head_content = ob_get_clean();
123
124
	/* Put together a new complete document containing only the requested block markup and the scripts/styles needed to render it */
125
	$block_markup = $post_html->saveHTML( $container );
126
	$api_key      = jetpack_get_mapbox_api_key();
127
	$page_html    = sprintf(
128
		'<!DOCTYPE html><head><style>html, body { margin: 0; padding: 0; }</style>%s</head><body>%s</body>',
129
		$head_content,
130
		preg_replace( '/(?<=<div\s)/', 'data-api-key="' . esc_attr( $api_key ) . '" ', $block_markup, 1 )
131
	);
132
	echo $page_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
133
	exit;
134
}
135
136
add_action( 'wp', 'jetpack_map_block_render_single_block_page' );
137