WPSEO_Sitemaps_Admin

Class that handles the Admin side of XML sitemaps.

Defined (1)

The class is defined in the following location(s).

/inc/sitemaps/class-sitemaps-admin.php  
  1. class WPSEO_Sitemaps_Admin { 
  2.  
  3. /** 
  4. * @var array Post_types that are being imported. 
  5. */ 
  6. private $importing_post_types = array(); 
  7.  
  8. /** 
  9. * Class constructor 
  10. */ 
  11. public function __construct() { 
  12. add_action( 'transition_post_status', array( $this, 'status_transition' ), 10, 3 ); 
  13. add_action( 'admin_footer', array( $this, 'status_transition_bulk_finished' ) ); 
  14.  
  15. add_action( 'admin_init', array( $this, 'delete_sitemaps' ) ); 
  16.  
  17. WPSEO_Sitemaps_Cache::register_clear_on_option_update( 'wpseo_xml', '' ); 
  18.  
  19. /** 
  20. * Find sitemaps residing on disk as they will block our rewrite. 
  21. * @todo issue #561 https://github.com/Yoast/wordpress-seo/issues/561 
  22.   
  23. * @deprecated since 3.1 in favor of 'detect_blocking_filesystem_sitemaps' 
  24. */ 
  25. public function delete_sitemaps() { 
  26. /** 
  27. * When removing this, make sure the 'admin_init' action is replaced with the following function: 
  28. */ 
  29. $this->detect_blocking_filesystem_sitemaps(); 
  30.  
  31. /** 
  32. * Find sitemaps residing on disk as they will block our rewrite. 
  33. * @since 3.1 
  34. */ 
  35. public function detect_blocking_filesystem_sitemaps() { 
  36. $wpseo_xml_options = WPSEO_Options::get_option( 'wpseo_xml' ); 
  37. if ( $wpseo_xml_options['enablexmlsitemap'] !== true ) { 
  38. return; 
  39. unset( $wpseo_xml_options ); 
  40.  
  41. // Find all files and directories containing 'sitemap' and are post-fixed .xml. 
  42. $blocking_files = glob( ABSPATH . '*sitemap*.xml', ( GLOB_NOSORT | GLOB_MARK ) ); 
  43.  
  44. if ( false === $blocking_files ) { // Some systems might return error on no matches. 
  45. $blocking_files = array(); 
  46.  
  47. // Save if we have changes. 
  48. $wpseo_options = WPSEO_Options::get_option( 'wpseo' ); 
  49.  
  50. if ( $wpseo_options['blocking_files'] !== $blocking_files ) { 
  51. $wpseo_options['blocking_files'] = $blocking_files; 
  52.  
  53. update_option( 'wpseo', $wpseo_options ); 
  54.  
  55. /** 
  56. * Hooked into transition_post_status. Will initiate search engine pings 
  57. * if the post is being published, is a post type that a sitemap is built for 
  58. * and is a post that is included in sitemaps. 
  59. * @param string $new_status New post status. 
  60. * @param string $old_status Old post status. 
  61. * @param \WP_Post $post Post object. 
  62. */ 
  63. public function status_transition( $new_status, $old_status, $post ) { 
  64. if ( $new_status !== 'publish' ) { 
  65. return; 
  66.  
  67. if ( defined( 'WP_IMPORTING' ) ) { 
  68. $this->status_transition_bulk( $new_status, $old_status, $post ); 
  69.  
  70. return; 
  71.  
  72. $post_type = get_post_type( $post ); 
  73.  
  74. wp_cache_delete( 'lastpostmodified:gmt:' . $post_type, 'timeinfo' ); // #17455. 
  75.  
  76. // None of our interest.. 
  77. if ( 'nav_menu_item' === $post_type ) { 
  78. return; 
  79.  
  80. $options = WPSEO_Options::get_options( array( 'wpseo_xml', 'wpseo_titles' ) ); 
  81.  
  82. // If the post type is excluded in options, we can stop. 
  83. $option = sprintf( 'post_types-%s-not_in_sitemap', $post_type ); 
  84. if ( isset( $options[ $option ] ) && $options[ $option ] === true ) { 
  85. return; 
  86.  
  87. if ( WP_CACHE ) { 
  88. wp_schedule_single_event( ( time() + 300 ), 'wpseo_hit_sitemap_index' ); 
  89.  
  90. /** 
  91. * Filter: 'wpseo_allow_xml_sitemap_ping' - Check if pinging is not allowed (allowed by default) 
  92. * @api boolean $allow_ping The boolean that is set to true by default. 
  93. */ 
  94. if ( apply_filters( 'wpseo_allow_xml_sitemap_ping', true ) === false ) { 
  95. return; 
  96.  
  97. // Allow the pinging to happen slightly after the hit sitemap index so the sitemap is fully regenerated when the ping happens. 
  98. $excluded_posts = explode( ', ', $options['excluded-posts'] ); 
  99.  
  100. if ( ! in_array( $post->ID, $excluded_posts ) ) { 
  101.  
  102. if ( defined( 'YOAST_SEO_PING_IMMEDIATELY' ) && YOAST_SEO_PING_IMMEDIATELY ) { 
  103. WPSEO_Sitemaps::ping_search_engines(); 
  104. elseif ( ! wp_next_scheduled( 'wpseo_ping_search_engines' ) ) { 
  105. wp_schedule_single_event( ( time() + 300 ), 'wpseo_ping_search_engines' ); 
  106.  
  107. /** 
  108. * While bulk importing, just save unique post_types 
  109. * When importing is done, if we have a post_type that is saved in the sitemap 
  110. * try to ping the search engines 
  111. * @param string $new_status New post status. 
  112. * @param string $old_status Old post status. 
  113. * @param \WP_Post $post Post object. 
  114. */ 
  115. private function status_transition_bulk( $new_status, $old_status, $post ) { 
  116. $this->importing_post_types[] = get_post_type( $post ); 
  117. $this->importing_post_types = array_unique( $this->importing_post_types ); 
  118.  
  119. /** 
  120. * After import finished, walk through imported post_types and update info. 
  121. */ 
  122. public function status_transition_bulk_finished() { 
  123. if ( ! defined( 'WP_IMPORTING' ) ) { 
  124. return; 
  125.  
  126. if ( empty( $this->importing_post_types ) ) { 
  127. return; 
  128.  
  129. $options = WPSEO_Options::get_option( 'wpseo_xml' ); 
  130.  
  131. $ping_search_engines = false; 
  132.  
  133. foreach ( $this->importing_post_types as $post_type ) { 
  134. wp_cache_delete( 'lastpostmodified:gmt:' . $post_type, 'timeinfo' ); // #17455. 
  135.  
  136. // Just have the cache deleted for nav_menu_item. 
  137. if ( 'nav_menu_item' === $post_type ) { 
  138. continue; 
  139.  
  140. $option = sprintf( 'post_types-%s-not_in_sitemap', $post_type ); 
  141. if ( ! isset( $options[ $option ] ) || $options[ $option ] === false ) { 
  142. $ping_search_engines = true; 
  143.  
  144. // Nothing to do. 
  145. if ( false === $ping_search_engines ) { 
  146. return; 
  147.  
  148. if ( WP_CACHE ) { 
  149. do_action( 'wpseo_hit_sitemap_index' ); 
  150.  
  151. WPSEO_Sitemaps::ping_search_engines(); 
  152. } /** End of class */