| 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 |
* @return void |
| 41 |
*/ |
| 42 |
public function status_transition( $new_status, $old_status, $post ) { |
| 43 |
if ( $new_status !== 'publish' ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
if ( defined( 'WP_IMPORTING' ) ) { |
| 48 |
$this->status_transition_bulk( $new_status, $old_status, $post ); |
| 49 |
|
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
$post_type = get_post_type( $post ); |
| 54 |
|
| 55 |
wp_cache_delete( 'lastpostmodified:gmt:' . $post_type, 'timeinfo' ); // #17455. |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Notify Google of the updated sitemap. |
| 60 |
* |
| 61 |
* @deprecated 22.0 |
| 62 |
* @codeCoverageIgnore |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function ping_search_engines() { |
| 67 |
_deprecated_function( __METHOD__, 'Yoast SEO 22.0' ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* While bulk importing, just save unique post_types. |
| 72 |
* |
| 73 |
* When importing is done, if we have a post_type that is saved in the sitemap |
| 74 |
* try to ping the search engines. |
| 75 |
* |
| 76 |
* @param string $new_status New post status. |
| 77 |
* @param string $old_status Old post status. |
| 78 |
* @param WP_Post $post Post object. |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
private function status_transition_bulk( $new_status, $old_status, $post ) { |
| 83 |
$this->importing_post_types[] = get_post_type( $post ); |
| 84 |
$this->importing_post_types = array_unique( $this->importing_post_types ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* After import finished, walk through imported post_types and update info. |
| 89 |
* |
| 90 |
* @return void |
| 91 |
*/ |
| 92 |
public function status_transition_bulk_finished() { |
| 93 |
if ( ! defined( 'WP_IMPORTING' ) ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
if ( empty( $this->importing_post_types ) ) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
$ping_search_engines = false; |
| 102 |
|
| 103 |
foreach ( $this->importing_post_types as $post_type ) { |
| 104 |
wp_cache_delete( 'lastpostmodified:gmt:' . $post_type, 'timeinfo' ); // #17455. |
| 105 |
|
| 106 |
// Just have the cache deleted for nav_menu_item. |
| 107 |
if ( $post_type === 'nav_menu_item' ) { |
| 108 |
continue; |
| 109 |
} |
| 110 |
|
| 111 |
if ( WPSEO_Options::get( 'noindex-' . $post_type, false ) === false ) { |
| 112 |
$ping_search_engines = true; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
// Nothing to do. |
| 117 |
if ( $ping_search_engines === false ) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
if ( WP_CACHE ) { |
| 122 |
do_action( 'wpseo_hit_sitemap_index' ); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|