Conditions | 29 |
Paths | 6337 |
Total Lines | 207 |
Code Lines | 100 |
Lines | 3 |
Ratio | 1.45 % |
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 |
||
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 | |||
328 | /** |
||
329 | * Callback for adding sitemap-interval to the list of schedules. |
||
330 | * |
||
331 | * @access public |
||
332 | * @since 4.7.0 |
||
333 | * |
||
334 | * @param array $schedules The array of WP_Cron schedules. |
||
335 | * |
||
336 | * @return array The updated array of WP_Cron schedules. |
||
337 | */ |
||
338 | public function callback_add_sitemap_schedule( $schedules ) { |
||
339 | $schedules['sitemap-interval'] = array( |
||
340 | 'interval' => JP_SITEMAP_INTERVAL, |
||
341 | 'display' => __( 'Sitemap Interval', 'jetpack' ), |
||
342 | ); |
||
343 | return $schedules; |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Add actions to schedule sitemap generation. |
||
348 | * Should only be called once, in the constructor. |
||
349 | * |
||
350 | * @access private |
||
351 | * @since 4.7.0 |
||
352 | */ |
||
353 | private function schedule_sitemap_generation() { |
||
354 | // Add cron schedule. |
||
355 | add_filter( 'cron_schedules', array( $this, 'callback_add_sitemap_schedule' ) ); |
||
356 | |||
357 | $sitemap_builder = new Jetpack_Sitemap_Builder(); |
||
358 | |||
359 | add_action( |
||
360 | 'jp_sitemap_cron_hook', |
||
361 | array( $sitemap_builder, 'update_sitemap' ) |
||
362 | ); |
||
363 | |||
364 | if ( ! wp_next_scheduled( 'jp_sitemap_cron_hook' ) ) { |
||
365 | wp_schedule_event( |
||
366 | time(), |
||
367 | 'sitemap-interval', |
||
368 | 'jp_sitemap_cron_hook' |
||
369 | ); |
||
370 | } |
||
371 | |||
372 | return; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * Callback to add sitemap to robots.txt. |
||
377 | * |
||
378 | * @access public |
||
379 | * @since 4.7.0 |
||
380 | */ |
||
381 | public function callback_action_do_robotstxt() { |
||
382 | |||
383 | /** |
||
384 | * Filter whether to make the default sitemap discoverable to robots or not. Default true. |
||
385 | * |
||
386 | * @module sitemaps |
||
387 | * @since 3.9.0 |
||
388 | * |
||
389 | * @param bool $discover_sitemap Make default sitemap discoverable to robots. |
||
390 | */ |
||
391 | $discover_sitemap = apply_filters( 'jetpack_sitemap_generate', true ); |
||
392 | |||
393 | View Code Duplication | if ( true === $discover_sitemap ) { |
|
394 | $sitemap_url = $this->finder->construct_sitemap_url( 'sitemap.xml' ); |
||
395 | echo 'Sitemap: ' . esc_url( $sitemap_url ) . "\n"; |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Filter whether to make the news sitemap discoverable to robots or not. Default true. |
||
400 | * |
||
401 | * @module sitemaps |
||
402 | * @since 3.9.0 |
||
403 | * |
||
404 | * @param bool $discover_news_sitemap Make default news sitemap discoverable to robots. |
||
405 | */ |
||
406 | $discover_news_sitemap = apply_filters( 'jetpack_news_sitemap_generate', true ); |
||
407 | |||
408 | View Code Duplication | if ( true === $discover_news_sitemap ) { |
|
409 | $news_sitemap_url = $this->finder->construct_sitemap_url( 'news-sitemap.xml' ); |
||
410 | echo 'Sitemap: ' . esc_url( $news_sitemap_url ) . "\n"; |
||
411 | } |
||
412 | |||
413 | return; |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * Callback to delete the news sitemap cache. |
||
418 | * |
||
419 | * @access public |
||
420 | * @since 4.7.0 |
||
421 | */ |
||
422 | public function callback_action_flush_news_sitemap_cache() { |
||
423 | delete_transient( 'jetpack_news_sitemap_xml' ); |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * Callback to set the sitemap location. |
||
428 | * |
||
429 | * @access public |
||
430 | * @since 4.7.0 |
||
431 | */ |
||
432 | public function callback_action_filter_sitemap_location() { |
||
433 | update_option( |
||
434 | 'jetpack_sitemap_location', |
||
435 | /** |
||
436 | * Additional path for sitemap URIs. Default value is empty. |
||
437 | * |
||
438 | * This string is any additional path fragment you want included between |
||
439 | * the home URL and the sitemap filenames. Exactly how this fragment is |
||
440 | * interpreted depends on your permalink settings. For example: |
||
441 | * |
||
442 | * Pretty permalinks: |
||
443 | * home_url() . jetpack_sitemap_location . '/sitemap.xml' |
||
444 | * |
||
445 | * Plain ("ugly") permalinks: |
||
446 | * home_url() . jetpack_sitemap_location . '/?jetpack-sitemap=sitemap.xml' |
||
447 | * |
||
448 | * PATHINFO permalinks: |
||
449 | * home_url() . '/index.php' . jetpack_sitemap_location . '/sitemap.xml' |
||
450 | * |
||
451 | * where 'sitemap.xml' is the name of a specific sitemap file. |
||
452 | * The value of this filter must be a valid path fragment per RFC 3986; |
||
453 | * in particular it must either be empty or begin with a '/'. |
||
454 | * Also take care that any restrictions on sitemap location imposed by |
||
455 | * the sitemap protocol are satisfied. |
||
456 | * |
||
457 | * The result of this filter is stored in an option, 'jetpack_sitemap_location'; |
||
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.