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