class-sitemaps-admin.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\XML Sitemaps
  6. */
  7. /**
  8. * Class that handles the Admin side of XML sitemaps
  9. */
  10. class WPSEO_Sitemaps_Admin {
  11. /**
  12. * @var array Post_types that are being imported.
  13. */
  14. private $importing_post_types = array();
  15. /**
  16. * Class constructor
  17. */
  18. public function __construct() {
  19. add_action( 'transition_post_status', array( $this, 'status_transition' ), 10, 3 );
  20. add_action( 'admin_footer', array( $this, 'status_transition_bulk_finished' ) );
  21. WPSEO_Sitemaps_Cache::register_clear_on_option_update( 'wpseo_titles', '' );
  22. WPSEO_Sitemaps_Cache::register_clear_on_option_update( 'wpseo', '' );
  23. }
  24. /**
  25. * Hooked into transition_post_status. Will initiate search engine pings
  26. * if the post is being published, is a post type that a sitemap is built for
  27. * and is a post that is included in sitemaps.
  28. *
  29. * @param string $new_status New post status.
  30. * @param string $old_status Old post status.
  31. * @param \WP_Post $post Post object.
  32. */
  33. public function status_transition( $new_status, $old_status, $post ) {
  34. if ( $new_status !== 'publish' ) {
  35. return;
  36. }
  37. if ( defined( 'WP_IMPORTING' ) ) {
  38. $this->status_transition_bulk( $new_status, $old_status, $post );
  39. return;
  40. }
  41. $post_type = get_post_type( $post );
  42. wp_cache_delete( 'lastpostmodified:gmt:' . $post_type, 'timeinfo' ); // #17455.
  43. // None of our interest..
  44. if ( 'nav_menu_item' === $post_type ) {
  45. return;
  46. }
  47. // If the post type is excluded in options, we can stop.
  48. if ( WPSEO_Options::get( 'noindex-' . $post_type, false ) ) {
  49. return;
  50. }
  51. if ( WP_CACHE ) {
  52. wp_schedule_single_event( ( time() + 300 ), 'wpseo_hit_sitemap_index' );
  53. }
  54. /**
  55. * Filter: 'wpseo_allow_xml_sitemap_ping' - Check if pinging is not allowed (allowed by default)
  56. *
  57. * @api boolean $allow_ping The boolean that is set to true by default.
  58. */
  59. if ( apply_filters( 'wpseo_allow_xml_sitemap_ping', true ) === false ) {
  60. return;
  61. }
  62. if ( defined( 'YOAST_SEO_PING_IMMEDIATELY' ) && YOAST_SEO_PING_IMMEDIATELY ) {
  63. WPSEO_Sitemaps::ping_search_engines();
  64. }
  65. elseif ( ! wp_next_scheduled( 'wpseo_ping_search_engines' ) ) {
  66. wp_schedule_single_event( ( time() + 300 ), 'wpseo_ping_search_engines' );
  67. }
  68. }
  69. /**
  70. * While bulk importing, just save unique post_types
  71. *
  72. * When importing is done, if we have a post_type that is saved in the sitemap
  73. * try to ping the search engines
  74. *
  75. * @param string $new_status New post status.
  76. * @param string $old_status Old post status.
  77. * @param \WP_Post $post Post object.
  78. */
  79. private function status_transition_bulk( $new_status, $old_status, $post ) {
  80. $this->importing_post_types[] = get_post_type( $post );
  81. $this->importing_post_types = array_unique( $this->importing_post_types );
  82. }
  83. /**
  84. * After import finished, walk through imported post_types and update info.
  85. */
  86. public function status_transition_bulk_finished() {
  87. if ( ! defined( 'WP_IMPORTING' ) ) {
  88. return;
  89. }
  90. if ( empty( $this->importing_post_types ) ) {
  91. return;
  92. }
  93. $ping_search_engines = false;
  94. foreach ( $this->importing_post_types as $post_type ) {
  95. wp_cache_delete( 'lastpostmodified:gmt:' . $post_type, 'timeinfo' ); // #17455.
  96. // Just have the cache deleted for nav_menu_item.
  97. if ( 'nav_menu_item' === $post_type ) {
  98. continue;
  99. }
  100. if ( WPSEO_Options::get( 'noindex-' . $post_type, false ) === false ) {
  101. $ping_search_engines = true;
  102. }
  103. }
  104. // Nothing to do.
  105. if ( false === $ping_search_engines ) {
  106. return;
  107. }
  108. if ( WP_CACHE ) {
  109. do_action( 'wpseo_hit_sitemap_index' );
  110. }
  111. WPSEO_Sitemaps::ping_search_engines();
  112. }
  113. // @codeCoverageIgnoreStart
  114. /**
  115. * Find sitemaps residing on disk as they will block our rewrite.
  116. *
  117. * @deprecated 7.0
  118. */
  119. public function delete_sitemaps() {
  120. _deprecated_function( 'WPSEO_Sitemaps_Admin::delete_sitemaps', '7.0' );
  121. }
  122. /**
  123. * Find sitemaps residing on disk as they will block our rewrite.
  124. *
  125. * @deprecated 7.0
  126. */
  127. public function detect_blocking_filesystem_sitemaps() {
  128. _deprecated_function( 'WPSEO_Sitemaps_Admin::delete_sitemaps', '7.0' );
  129. }
  130. // @codeCoverageIgnoreEnd
  131. } /* End of class */