Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. <?php
  2. namespace WP_Rocket\Preload;
  3.  
  4. /**
  5. * Sitemap preload
  6. *
  7. * @since 3.2
  8. * @author Remy Perona
  9. */
  10. class Sitemap extends Abstract_Preload {
  11. /**
  12. * Launches the sitemap preload
  13. *
  14. * @since 3.2
  15. * @author Remy Perona
  16. *
  17. * @param array $sitemaps Sitemaps to use for preloading.
  18. * @return void
  19. */
  20. public function run_preload( $sitemaps ) {
  21.  
  22. error_log( "\n[Timestamp - " . gmdate( "F jS, Y G:i:s", strtotime( '+ 5 hours 30 minutes' ) ) . " IST] Preload Start" . print_r( $settings, true ), 3, ABSPATH . "/my_error_log.log" );
  23.  
  24. if ( ! $sitemaps ) {
  25. return;
  26. }
  27.  
  28. $urls_group = [];
  29.  
  30. foreach ( $sitemaps as $sitemap_type => $sitemap_url ) {
  31. /**
  32. * Fires before WP Rocket sitemap preload is called for a sitemap URL
  33. *
  34. * @since 2.8
  35. *
  36. * @param string $sitemap_type the sitemap identifier
  37. * @param string $sitemap_url sitemap URL to be crawler
  38. */
  39. do_action( 'before_run_rocket_sitemap_preload', $sitemap_type, $sitemap_url );
  40.  
  41. $urls_group[] = $this->process_sitemap( $sitemap_url );
  42.  
  43. /**
  44. * Fires after WP Rocket sitemap preload was called for a sitemap URL
  45. *
  46. * @since 2.8
  47. *
  48. * @param string $sitemap_type the sitemap identifier
  49. * @param string $sitemap_url sitemap URL crawled
  50. */
  51. do_action( 'after_run_rocket_sitemap_preload', $sitemap_type, $sitemap_url );
  52. }
  53.  
  54. $urls_group = array_filter( $urls_group );
  55.  
  56. if ( ! $urls_group ) {
  57. return;
  58. }
  59.  
  60. foreach ( $urls_group as $urls ) {
  61. $urls = array_flip( array_flip( $urls ) );
  62. foreach ( $urls as $url ) {
  63. $this->preload_process->push_to_queue( $url );
  64. }
  65. }
  66.  
  67. set_transient( 'rocket_preload_running', 0 );
  68. $this->preload_process->save()->dispatch();
  69. }
  70.  
  71. /**
  72. * Processes the sitemaps recursively
  73. *
  74. * @since 3.2
  75. * @author Remy Perona
  76. *
  77. * @param string $sitemap_url URL of the sitemap.
  78. * @param array $urls An array of URLs.
  79. * @return array Empty array or array containing URLs
  80. */
  81. public function process_sitemap( $sitemap_url, $urls = [] ) {
  82. $tmp_urls = [];
  83.  
  84. /**
  85. * Filters the arguments for the sitemap preload request
  86. *
  87. * @since 2.10.8
  88. * @author Remy Perona
  89. *
  90. * @param array $args Arguments for the request.
  91. */
  92. $args = apply_filters( 'rocket_preload_sitemap_request_args', array(
  93. 'user-agent' => 'WP Rocket/Sitemaps',
  94. 'sslverify' => apply_filters( 'https_local_ssl_verify', true ),
  95. ) );
  96.  
  97. $sitemap = wp_remote_get( esc_url( $sitemap_url ), $args );
  98.  
  99. if ( is_wp_error( $sitemap ) ) {
  100. error_log( "\n[Timestamp - " . gmdate( "F jS, Y G:i:s", strtotime( '+ 5 hours 30 minutes' ) ) . " IST] " . print_r( $sitemap, true ), 3, ABSPATH . "/my_error_log.log" );
  101. return [];
  102. }
  103.  
  104. $xml_data = wp_remote_retrieve_body( $sitemap );
  105.  
  106. if ( empty( $xml_data ) ) {
  107. error_log( "\n[Timestamp - " . gmdate( "F jS, Y G:i:s", strtotime( '+ 5 hours 30 minutes' ) ) . " IST] xml_data is empty ", 3, ABSPATH . "/my_error_log.log" );
  108. return [];
  109. }
  110.  
  111. libxml_use_internal_errors( true );
  112.  
  113. $xml = simplexml_load_string( $xml_data );
  114.  
  115. if ( false === $xml ) {
  116. error_log( "\n[Timestamp - " . gmdate( "F jS, Y G:i:s", strtotime( '+ 5 hours 30 minutes' ) ) . " IST] xml is false ", 3, ABSPATH . "/my_error_log.log" );
  117. libxml_clear_errors();
  118. return [];
  119. }
  120.  
  121. $url_count = count( $xml->url );
  122. $sitemap_children = count( $xml->sitemap );
  123.  
  124. if ( $url_count > 0 ) {
  125. for ( $i = 0; $i < $url_count; $i++ ) {
  126. $tmp_urls[] = (string) $xml->url[ $i ]->loc;
  127. }
  128. } elseif ( $sitemap_children > 0 ) {
  129. for ( $i = 0; $i < $sitemap_children; $i++ ) {
  130. $sub_sitemap_url = (string) $xml->sitemap[ $i ]->loc;
  131. $urls = $this->process_sitemap( $sub_sitemap_url, $urls );
  132. }
  133. }
  134.  
  135. $urls = array_merge( $urls, $tmp_urls );
  136. error_log( "\n[Timestamp - " . gmdate( "F jS, Y G:i:s", strtotime( '+ 5 hours 30 minutes' ) ) . " IST] urls = " . print_r( $urls, true ), 3, ABSPATH . "/my_error_log.log" );
  137. return $urls;
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement