Conditions | 7 |
Paths | 16 |
Total Lines | 58 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 |
||
|
|||
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 | |||
124 |
If you suppress an error, we recommend checking for the error condition explicitly: