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 |
||
110 | function render_single_block_page() { |
||
111 | // phpcs:ignore WordPress.Security.NonceVerification |
||
112 | $map_block_counter = isset( $_GET, $_GET['map-block-counter'] ) ? absint( $_GET['map-block-counter'] ) : null; |
||
113 | // phpcs:ignore WordPress.Security.NonceVerification |
||
114 | $map_block_post_id = isset( $_GET, $_GET['map-block-post-id'] ) ? absint( $_GET['map-block-post-id'] ) : null; |
||
115 | |||
116 | if ( ! $map_block_counter || ! $map_block_post_id ) { |
||
117 | return; |
||
118 | } |
||
119 | |||
120 | /* Create an array of all root-level DIVs that are Map Blocks */ |
||
121 | $post = get_post( $map_block_post_id ); |
||
122 | |||
123 | if ( ! class_exists( 'DOMDocument' ) ) { |
||
124 | return; |
||
125 | } |
||
126 | |||
127 | $post_html = new \DOMDocument(); |
||
128 | /** This filter is already documented in core/wp-includes/post-template.php */ |
||
129 | $content = apply_filters( 'the_content', $post->post_content ); |
||
130 | |||
131 | /* Suppress warnings */ |
||
132 | libxml_use_internal_errors( true ); |
||
133 | @$post_html->loadHTML( $content ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
||
|
|||
134 | libxml_use_internal_errors( false ); |
||
135 | |||
136 | $xpath = new \DOMXPath( $post_html ); |
||
137 | $container = $xpath->query( '//div[ contains( @class, "wp-block-jetpack-map" ) ]' )->item( $map_block_counter - 1 ); |
||
138 | |||
139 | /* Check that we have a block matching the counter position */ |
||
140 | if ( ! $container ) { |
||
141 | return; |
||
142 | } |
||
143 | |||
144 | /* Compile scripts and styles */ |
||
145 | ob_start(); |
||
146 | |||
147 | add_filter( 'jetpack_is_amp_request', '__return_false' ); |
||
148 | |||
149 | Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); |
||
150 | wp_scripts()->do_items(); |
||
151 | wp_styles()->do_items(); |
||
152 | |||
153 | add_filter( 'jetpack_is_amp_request', '__return_true' ); |
||
154 | |||
155 | $head_content = ob_get_clean(); |
||
156 | |||
157 | /* Put together a new complete document containing only the requested block markup and the scripts/styles needed to render it */ |
||
158 | $block_markup = $post_html->saveHTML( $container ); |
||
159 | $access_token = Jetpack_Mapbox_Helper::get_access_token(); |
||
160 | $page_html = sprintf( |
||
161 | '<!DOCTYPE html><head><style>html, body { margin: 0; padding: 0; }</style>%s</head><body>%s</body>', |
||
162 | $head_content, |
||
163 | preg_replace( '/(?<=<div\s)/', 'data-api-key="' . esc_attr( $access_token['key'] ) . '" ', $block_markup, 1 ) |
||
164 | ); |
||
165 | echo $page_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
||
166 | exit; |
||
167 | } |
||
168 | add_action( 'wp', __NAMESPACE__ . '\render_single_block_page' ); |
||
169 |
If you suppress an error, we recommend checking for the error condition explicitly: