Completed
Push — rm/minileven-ui ( 326a89...9f59a8 )
by Jeremy
09:25 queued 02:29
created

map.php ➔ jetpack_map_block_render_single_block_page()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 16
nop 0
dl 0
loc 58
rs 7.983
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 * Map block registration/dependency declaration.
19
 *
20
 * @param array  $attr    Array containing the map block attributes.
21
 * @param string $content String containing the map block content.
22
 *
23
 * @return string
24
 */
25
function jetpack_map_block_load_assets( $attr, $content ) {
26
	$api_key = Jetpack_Options::get_option( 'mapbox_api_key' );
27
28
	if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
29
		static $map_block_counter = array();
30
31
		$id = get_the_ID();
32
		if ( ! isset( $map_block_counter[ $id ] ) ) {
33
			$map_block_counter[ $id ] = 0;
34
		}
35
		$map_block_counter[ $id ]++;
36
37
		$iframe_url = add_query_arg(
38
			array(
39
				'map-block-counter' => absint( $map_block_counter[ $id ] ),
40
				'map-block-post-id' => $id,
41
			),
42
			get_permalink()
43
		);
44
45
		$placeholder = preg_replace( '/(?<=<div\s)/', 'placeholder ', $content );
46
47
		return sprintf(
48
			'<amp-iframe src="%s" width="%d" height="%d" layout="responsive" allowfullscreen sandbox="allow-scripts">%s</amp-iframe>',
49
			esc_url( $iframe_url ),
50
			4,
51
			3,
52
			$placeholder
53
		);
54
	}
55
56
	Jetpack_Gutenberg::load_assets_as_required( 'map' );
57
58
	return preg_replace( '/<div /', '<div data-api-key="' . esc_attr( $api_key ) . '" ', $content, 1 );
59
}
60
61
/**
62
 * Render a page containing only a single Map block.
63
 */
64
function jetpack_map_block_render_single_block_page() {
65
	// phpcs:ignore WordPress.Security.NonceVerification
66
	$map_block_counter = isset( $_GET, $_GET['map-block-counter'] ) ? absint( $_GET['map-block-counter'] ) : null;
67
	// phpcs:ignore WordPress.Security.NonceVerification
68
	$map_block_post_id = isset( $_GET, $_GET['map-block-post-id'] ) ? absint( $_GET['map-block-post-id'] ) : null;
69
70
	if ( ! $map_block_counter || ! $map_block_post_id ) {
71
		return;
72
	}
73
74
	/* Create an array of all root-level DIVs that are Map Blocks */
75
	$post = get_post( $map_block_post_id );
76
77
	if ( ! class_exists( 'DOMDocument' ) ) {
78
		return;
79
	}
80
81
	$post_html = new DOMDocument();
82
	/** This filter is already documented in core/wp-includes/post-template.php */
83
	$content = apply_filters( 'the_content', $post->post_content );
84
85
	/* Suppress warnings */
86
	libxml_use_internal_errors( true );
87
	@$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...
88
	libxml_use_internal_errors( false );
89
90
	$xpath     = new DOMXPath( $post_html );
91
	$container = $xpath->query( '//div[ contains( @class, "wp-block-jetpack-map" ) ]' )->item( $map_block_counter - 1 );
92
93
	/* Check that we have a block matching the counter position */
94
	if ( ! $container ) {
95
		return;
96
	}
97
98
	/* Compile scripts and styles */
99
	ob_start();
100
101
	add_filter( 'jetpack_is_amp_request', '__return_false' );
102
103
	Jetpack_Gutenberg::load_assets_as_required( 'map' );
104
	wp_scripts()->do_items();
105
	wp_styles()->do_items();
106
107
	add_filter( 'jetpack_is_amp_request', '__return_true' );
108
109
	$head_content = ob_get_clean();
110
111
	/* Put together a new complete document containing only the requested block markup and the scripts/styles needed to render it */
112
	$block_markup = $post_html->saveHTML( $container );
113
	$api_key      = Jetpack_Options::get_option( 'mapbox_api_key' );
114
	$page_html    = sprintf(
115
		'<!DOCTYPE html><head><style>html, body { margin: 0; padding: 0; }</style>%s</head><body>%s</body>',
116
		$head_content,
117
		preg_replace( '/(?<=<div\s)/', 'data-api-key="' . esc_attr( $api_key ) . '" ', $block_markup, 1 )
118
	);
119
	echo $page_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
120
	exit;
121
}
122
123
add_action( 'wp', 'jetpack_map_block_render_single_block_page' );
124