class-sitemaps-admin.php 4.1 KB

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