PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/inc/sitemaps/class-sitemaps-admin.php

https://gitlab.com/ngochuynh1991/cuacuon
PHP | 185 lines | 91 code | 33 blank | 61 comment | 20 complexity | 4d50021e507a0d64f5a300afe681894a MD5 | raw file
  1. <?php
  2. /**
  3. * @package WPSEO\Admin\XML Sitemaps
  4. */
  5. /**
  6. * Class that handles the Admin side of XML sitemaps
  7. */
  8. class WPSEO_Sitemaps_Admin {
  9. /**
  10. * @var array Post_types that are being imported.
  11. */
  12. private $importing_post_types = array();
  13. /**
  14. * Class constructor
  15. */
  16. public function __construct() {
  17. add_action( 'transition_post_status', array( $this, 'status_transition' ), 10, 3 );
  18. add_action( 'admin_footer', array( $this, 'status_transition_bulk_finished' ) );
  19. add_action( 'admin_init', array( $this, 'delete_sitemaps' ) );
  20. WPSEO_Sitemaps_Cache::register_clear_on_option_update( 'wpseo_xml', '' );
  21. }
  22. /**
  23. * Find sitemaps residing on disk as they will block our rewrite.
  24. *
  25. * @todo issue #561 https://github.com/Yoast/wordpress-seo/issues/561
  26. * @deprecated since 3.1 in favor of 'detect_blocking_filesystem_sitemaps'
  27. */
  28. public function delete_sitemaps() {
  29. /**
  30. * When removing this, make sure the 'admin_init' action is replaced with the following function:
  31. */
  32. $this->detect_blocking_filesystem_sitemaps();
  33. }
  34. /**
  35. * Find sitemaps residing on disk as they will block our rewrite.
  36. *
  37. * @since 3.1
  38. */
  39. public function detect_blocking_filesystem_sitemaps() {
  40. $wpseo_xml_options = WPSEO_Options::get_option( 'wpseo_xml' );
  41. if ( $wpseo_xml_options['enablexmlsitemap'] !== true ) {
  42. return;
  43. }
  44. unset( $wpseo_xml_options );
  45. // Find all files and directories containing 'sitemap' and are post-fixed .xml.
  46. $blocking_files = glob( ABSPATH . '*sitemap*.xml', ( GLOB_NOSORT | GLOB_MARK ) );
  47. // Save if we have changes.
  48. $wpseo_options = WPSEO_Options::get_option( 'wpseo' );
  49. if ( $wpseo_options['blocking_files'] !== $blocking_files ) {
  50. $wpseo_options['blocking_files'] = $blocking_files;
  51. update_option( 'wpseo', $wpseo_options );
  52. }
  53. }
  54. /**
  55. * Hooked into transition_post_status. Will initiate search engine pings
  56. * if the post is being published, is a post type that a sitemap is built for
  57. * and is a post that is included in sitemaps.
  58. *
  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. return;
  70. }
  71. $post_type = get_post_type( $post );
  72. wp_cache_delete( 'lastpostmodified:gmt:' . $post_type, 'timeinfo' ); // #17455.
  73. // None of our interest..
  74. if ( 'nav_menu_item' === $post_type ) {
  75. return;
  76. }
  77. $options = WPSEO_Options::get_options( array( 'wpseo_xml', 'wpseo_titles' ) );
  78. // If the post type is excluded in options, we can stop.
  79. $option = sprintf( 'post_types-%s-not_in_sitemap', $post_type );
  80. if ( isset( $options[ $option ] ) && $options[ $option ] === true ) {
  81. return;
  82. }
  83. if ( WP_CACHE ) {
  84. wp_schedule_single_event( ( time() + 300 ), 'wpseo_hit_sitemap_index' );
  85. }
  86. /**
  87. * Filter: 'wpseo_allow_xml_sitemap_ping' - Check if pinging is not allowed (allowed by default)
  88. *
  89. * @api boolean $allow_ping The boolean that is set to true by default.
  90. */
  91. if ( apply_filters( 'wpseo_allow_xml_sitemap_ping', true ) === false ) {
  92. return;
  93. }
  94. // Allow the pinging to happen slightly after the hit sitemap index so the sitemap is fully regenerated when the ping happens.
  95. $excluded_posts = explode( ',', $options['excluded-posts'] );
  96. if ( ! in_array( $post->ID, $excluded_posts ) ) {
  97. if ( defined( 'YOAST_SEO_PING_IMMEDIATELY' ) && YOAST_SEO_PING_IMMEDIATELY ) {
  98. WPSEO_Sitemaps::ping_search_engines();
  99. }
  100. else {
  101. wp_schedule_single_event( ( time() + 300 ), 'wpseo_ping_search_engines' );
  102. }
  103. }
  104. }
  105. /**
  106. * While bulk importing, just save unique post_types
  107. *
  108. * When importing is done, if we have a post_type that is saved in the sitemap
  109. * try to ping the search engines
  110. *
  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. $ping_search_engines = false;
  131. foreach ( $this->importing_post_types as $post_type ) {
  132. wp_cache_delete( 'lastpostmodified:gmt:' . $post_type, 'timeinfo' ); // #17455.
  133. // Just have the cache deleted for nav_menu_item.
  134. if ( 'nav_menu_item' === $post_type ) {
  135. continue;
  136. }
  137. $option = sprintf( 'post_types-%s-not_in_sitemap', $post_type );
  138. if ( ! isset( $options[ $option ] ) || $options[ $option ] === false ) {
  139. $ping_search_engines = true;
  140. }
  141. }
  142. // Nothing to do.
  143. if ( false === $ping_search_engines ) {
  144. return;
  145. }
  146. if ( WP_CACHE ) {
  147. do_action( 'wpseo_hit_sitemap_index' );
  148. }
  149. WPSEO_Sitemaps::ping_search_engines();
  150. }
  151. } /* End of class */