Conditions | 16 |
Paths | 16386 |
Total Lines | 172 |
Code Lines | 91 |
Lines | 54 |
Ratio | 31.4 % |
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 |
||
155 | public function callback_action_catch_sitemap_urls() { |
||
156 | // Regular expressions for sitemap URL routing. |
||
157 | $regex = array( |
||
158 | 'master' => '/^sitemap\.xml$/', |
||
159 | 'sitemap' => '/^sitemap-[1-9][0-9]*\.xml$/', |
||
160 | 'index' => '/^sitemap-index-[1-9][0-9]*\.xml$/', |
||
161 | 'sitemap-style' => '/^sitemap\.xsl$/', |
||
162 | 'index-style' => '/^sitemap-index\.xsl$/', |
||
163 | 'image' => '/^image-sitemap-[1-9][0-9]*\.xml$/', |
||
164 | 'image-index' => '/^image-sitemap-index-[1-9][0-9]*\.xml$/', |
||
165 | 'image-style' => '/^image-sitemap\.xsl$/', |
||
166 | 'video' => '/^video-sitemap-[1-9][0-9]*\.xml$/', |
||
167 | 'video-index' => '/^video-sitemap-index-[1-9][0-9]*\.xml$/', |
||
168 | 'video-style' => '/^video-sitemap\.xsl$/', |
||
169 | 'news' => '/^news-sitemap\.xml$/', |
||
170 | 'news-style' => '/^news-sitemap\.xsl$/', |
||
171 | ); |
||
172 | |||
173 | // The raw path(+query) of the requested URI. |
||
174 | if ( isset( $_SERVER['REQUEST_URI'] ) ) { // WPCS: Input var okay. |
||
175 | $raw_uri = sanitize_text_field( |
||
176 | wp_unslash( $_SERVER['REQUEST_URI'] ) // WPCS: Input var okay. |
||
177 | ); |
||
178 | } else { |
||
179 | $raw_uri = ''; |
||
180 | } |
||
181 | |||
182 | $request = $this->finder->recognize_sitemap_uri( $raw_uri ); |
||
183 | |||
184 | if ( isset( $request['sitemap_name'] ) ) { |
||
185 | |||
186 | /** |
||
187 | * Filter the content type used to serve the sitemap XML files. |
||
188 | * |
||
189 | * @module sitemaps |
||
190 | * |
||
191 | * @since 3.9.0 |
||
192 | * |
||
193 | * @param string $xml_content_type By default, it's 'text/xml'. |
||
194 | */ |
||
195 | $xml_content_type = apply_filters( 'jetpack_sitemap_content_type', 'text/xml' ); |
||
196 | |||
197 | // Catch master sitemap xml. |
||
198 | if ( preg_match( $regex['master'], $request['sitemap_name'] ) ) { |
||
199 | $this->serve_raw_and_die( |
||
200 | $xml_content_type, |
||
201 | $this->librarian->get_sitemap_text( |
||
202 | jp_sitemap_filename( JP_MASTER_SITEMAP_TYPE, 0 ), |
||
203 | JP_MASTER_SITEMAP_TYPE |
||
204 | ) |
||
205 | ); |
||
206 | } |
||
207 | |||
208 | // Catch sitemap xml. |
||
209 | View Code Duplication | if ( preg_match( $regex['sitemap'], $request['sitemap_name'] ) ) { |
|
210 | $this->serve_raw_and_die( |
||
211 | $xml_content_type, |
||
212 | $this->librarian->get_sitemap_text( |
||
213 | $request['sitemap_name'], |
||
214 | JP_PAGE_SITEMAP_TYPE |
||
215 | ) |
||
216 | ); |
||
217 | } |
||
218 | |||
219 | // Catch sitemap index xml. |
||
220 | View Code Duplication | if ( preg_match( $regex['index'], $request['sitemap_name'] ) ) { |
|
221 | $this->serve_raw_and_die( |
||
222 | $xml_content_type, |
||
223 | $this->librarian->get_sitemap_text( |
||
224 | $request['sitemap_name'], |
||
225 | JP_PAGE_SITEMAP_INDEX_TYPE |
||
226 | ) |
||
227 | ); |
||
228 | } |
||
229 | |||
230 | // Catch sitemap xsl. |
||
231 | if ( preg_match( $regex['sitemap-style'], $request['sitemap_name'] ) ) { |
||
232 | $this->serve_raw_and_die( |
||
233 | 'application/xml', |
||
234 | Jetpack_Sitemap_Stylist::sitemap_xsl() |
||
235 | ); |
||
236 | } |
||
237 | |||
238 | // Catch sitemap index xsl. |
||
239 | if ( preg_match( $regex['index-style'], $request['sitemap_name'] ) ) { |
||
240 | $this->serve_raw_and_die( |
||
241 | 'application/xml', |
||
242 | Jetpack_Sitemap_Stylist::sitemap_index_xsl() |
||
243 | ); |
||
244 | } |
||
245 | |||
246 | // Catch image sitemap xml. |
||
247 | View Code Duplication | if ( preg_match( $regex['image'], $request['sitemap_name'] ) ) { |
|
248 | $this->serve_raw_and_die( |
||
249 | $xml_content_type, |
||
250 | $this->librarian->get_sitemap_text( |
||
251 | $request['sitemap_name'], |
||
252 | JP_IMAGE_SITEMAP_TYPE |
||
253 | ) |
||
254 | ); |
||
255 | } |
||
256 | |||
257 | // Catch image sitemap index xml. |
||
258 | View Code Duplication | if ( preg_match( $regex['image-index'], $request['sitemap_name'] ) ) { |
|
259 | $this->serve_raw_and_die( |
||
260 | $xml_content_type, |
||
261 | $this->librarian->get_sitemap_text( |
||
262 | $request['sitemap_name'], |
||
263 | JP_IMAGE_SITEMAP_INDEX_TYPE |
||
264 | ) |
||
265 | ); |
||
266 | } |
||
267 | |||
268 | // Catch image sitemap xsl. |
||
269 | if ( preg_match( $regex['image-style'], $request['sitemap_name'] ) ) { |
||
270 | $this->serve_raw_and_die( |
||
271 | 'application/xml', |
||
272 | Jetpack_Sitemap_Stylist::image_sitemap_xsl() |
||
273 | ); |
||
274 | } |
||
275 | |||
276 | // Catch video sitemap xml. |
||
277 | View Code Duplication | if ( preg_match( $regex['video'], $request['sitemap_name'] ) ) { |
|
278 | $this->serve_raw_and_die( |
||
279 | $xml_content_type, |
||
280 | $this->librarian->get_sitemap_text( |
||
281 | $request['sitemap_name'], |
||
282 | JP_VIDEO_SITEMAP_TYPE |
||
283 | ) |
||
284 | ); |
||
285 | } |
||
286 | |||
287 | // Catch video sitemap index xml. |
||
288 | View Code Duplication | if ( preg_match( $regex['video-index'], $request['sitemap_name'] ) ) { |
|
289 | $this->serve_raw_and_die( |
||
290 | $xml_content_type, |
||
291 | $this->librarian->get_sitemap_text( |
||
292 | $request['sitemap_name'], |
||
293 | JP_VIDEO_SITEMAP_INDEX_TYPE |
||
294 | ) |
||
295 | ); |
||
296 | } |
||
297 | |||
298 | // Catch video sitemap xsl. |
||
299 | if ( preg_match( $regex['video-style'], $request['sitemap_name'] ) ) { |
||
300 | $this->serve_raw_and_die( |
||
301 | 'application/xml', |
||
302 | Jetpack_Sitemap_Stylist::video_sitemap_xsl() |
||
303 | ); |
||
304 | } |
||
305 | |||
306 | // Catch news sitemap xml. |
||
307 | if ( preg_match( $regex['news'], $request['sitemap_name'] ) ) { |
||
308 | $sitemap_builder = new Jetpack_Sitemap_Builder(); |
||
309 | $this->serve_raw_and_die( |
||
310 | $xml_content_type, |
||
311 | $sitemap_builder->news_sitemap_xml() |
||
312 | ); |
||
313 | } |
||
314 | |||
315 | // Catch news sitemap xsl. |
||
316 | if ( preg_match( $regex['news-style'], $request['sitemap_name'] ) ) { |
||
317 | $this->serve_raw_and_die( |
||
318 | 'application/xml', |
||
319 | Jetpack_Sitemap_Stylist::news_sitemap_xsl() |
||
320 | ); |
||
321 | } |
||
322 | } |
||
323 | |||
324 | // URL did not match any sitemap patterns. |
||
325 | return; |
||
326 | } |
||
327 | |||
478 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.