Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.73% |
157 / 164 |
|
82.35% |
14 / 17 |
CRAP | |
0.00% |
0 / 1 |
| Indexnow_Events | |
95.73% |
157 / 164 |
|
82.35% |
14 / 17 |
103 | |
0.00% |
0 / 1 |
| init | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
| preference_changed | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
5 | |||
| capture_previous_url | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
8 | |||
| eligible | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
8 | |||
| saved | |
89.66% |
26 / 29 |
|
0.00% |
0 / 1 |
21.49 | |||
| removal_allowed | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
5 | |||
| trashing | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
| deleted | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
10.03 | |||
| forget_deleted | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| remember_before_meta_change | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
8 | |||
| meta_changed | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
13 | |||
| flush_metadata_changes | |
85.00% |
17 / 20 |
|
0.00% |
0 / 1 |
11.41 | |||
| post_key | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| supported | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| relevant_metadata | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
1 | |||
| notify_restriction | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| notify | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Notification candidates derived from actual public content transitions. |
| 4 | * |
| 5 | * @package automattic/jetpack-seo |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\SEO; |
| 9 | |
| 10 | /** |
| 11 | * Privacy changes suppress discovery; permanent deletion retains the former URL. |
| 12 | */ |
| 13 | class Indexnow_Events { |
| 14 | const PUBLIC_URL_META = '_jetpack_seo_indexnow_public_url'; |
| 15 | const MAX_PENDING_METADATA = 1000; |
| 16 | |
| 17 | /** |
| 18 | * URLs captured before a permalink-changing update. |
| 19 | * |
| 20 | * @var array<string,string> |
| 21 | */ |
| 22 | private static $previous_urls = array(); |
| 23 | |
| 24 | /** |
| 25 | * Posts whose metadata is being removed by core. |
| 26 | * |
| 27 | * @var array<string,bool> |
| 28 | */ |
| 29 | private static $deleting = array(); |
| 30 | |
| 31 | /** |
| 32 | * Metadata changes awaiting the completed save or standalone reconciliation. |
| 33 | * |
| 34 | * @var array<string,array{blog:int,post:int,url?:string,changed?:bool}> |
| 35 | */ |
| 36 | private static $pending_metadata = array(); |
| 37 | |
| 38 | /** Register write hooks independently of dashboard rendering. */ |
| 39 | public static function init() { |
| 40 | add_action( 'pre_post_update', array( __CLASS__, 'capture_previous_url' ), 10, 2 ); |
| 41 | add_action( 'wp_after_insert_post', array( __CLASS__, 'saved' ), 10, 4 ); |
| 42 | add_action( 'wp_trash_post', array( __CLASS__, 'trashing' ) ); |
| 43 | add_action( 'before_delete_post', array( __CLASS__, 'deleted' ), 10, 2 ); |
| 44 | add_action( 'deleted_post', array( __CLASS__, 'forget_deleted' ) ); |
| 45 | add_filter( 'add_post_metadata', array( __CLASS__, 'remember_before_meta_change' ), 10, 3 ); |
| 46 | add_filter( 'update_post_metadata', array( __CLASS__, 'remember_before_meta_change' ), 10, 3 ); |
| 47 | add_filter( 'delete_post_metadata', array( __CLASS__, 'remember_before_meta_change' ), 10, 3 ); |
| 48 | add_action( 'updated_post_meta', array( __CLASS__, 'meta_changed' ), 10, 3 ); |
| 49 | add_action( 'added_post_meta', array( __CLASS__, 'meta_changed' ), 10, 3 ); |
| 50 | add_action( 'deleted_post_meta', array( __CLASS__, 'meta_changed' ), 10, 3 ); |
| 51 | add_action( 'shutdown', array( __CLASS__, 'flush_metadata_changes' ) ); |
| 52 | add_action( 'update_option_' . Indexnow_Client::ENABLED_OPTION, array( __CLASS__, 'preference_changed' ), 10, 2 ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Discard request-local changes when consent is withdrawn. |
| 57 | * |
| 58 | * @param mixed $old Previous preference. |
| 59 | * @param mixed $enabled Saved preference. |
| 60 | */ |
| 61 | public static function preference_changed( $old, $enabled ) { |
| 62 | if ( $old && ! $enabled ) { |
| 63 | $prefix = get_current_blog_id() . ':'; |
| 64 | foreach ( array_keys( self::$pending_metadata + self::$previous_urls ) as $key ) { |
| 65 | if ( 0 === strpos( $key, $prefix ) ) { |
| 66 | unset( self::$pending_metadata[ $key ], self::$previous_urls[ $key ] ); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Capture the public permalink before the database row changes. |
| 74 | * |
| 75 | * @param int $id Post ID. |
| 76 | * @param array $data Updated post fields. |
| 77 | */ |
| 78 | public static function capture_previous_url( $id, $data ) { |
| 79 | $post = get_post( $id ); |
| 80 | if ( ! self::supported( $post ) || ! Indexnow_Client::enabled() ) { |
| 81 | return; |
| 82 | } |
| 83 | $key = self::post_key( $id ); |
| 84 | if ( ! isset( self::$previous_urls[ $key ] ) ) { |
| 85 | self::$previous_urls[ $key ] = ''; |
| 86 | if ( self::eligible( $post ) ) { |
| 87 | $known = get_post_meta( $id, self::PUBLIC_URL_META, true ); |
| 88 | // Core may append __trashed before this hook; trashing() preserves the former URL. |
| 89 | if ( $known || 'trash' !== ( $data['post_status'] ?? '' ) ) { |
| 90 | self::$previous_urls[ $key ] = $known ? $known : get_permalink( $post ); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Reuse SEO's content and access restrictions before exposing a URL. |
| 98 | * |
| 99 | * @param \WP_Post|null $post Current content. |
| 100 | * @param string $url Expected canonical URL, when required. |
| 101 | * @return bool |
| 102 | */ |
| 103 | public static function eligible( $post, $url = '' ) { |
| 104 | return $post instanceof \WP_Post && Indexnow_Client::enabled() |
| 105 | && in_array( $post->post_type, Post_Types::get_supported_content_types(), true ) |
| 106 | && is_post_publicly_viewable( $post ) && ! Content_Gate::is_gated( $post ) |
| 107 | && ! get_post_meta( $post->ID, Content_Coverage::META_NOINDEX, true ) |
| 108 | && ( '' === $url || get_permalink( $post ) === $url ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Capture eligible saves and the previous URL on a permalink change. |
| 113 | * |
| 114 | * @param int $id Post ID. |
| 115 | * @param \WP_Post $post Saved post. |
| 116 | * @param bool $updated Whether this was an update. |
| 117 | * @param \WP_Post|null $before Previous post. |
| 118 | */ |
| 119 | public static function saved( $id, $post, $updated, $before ) { |
| 120 | $metadata_changed = ! empty( self::$pending_metadata[ self::post_key( $id ) ]['changed'] ); |
| 121 | unset( self::$pending_metadata[ self::post_key( $id ) ] ); |
| 122 | if ( ! self::supported( $post ) || ! Indexnow_Client::enabled() ) { |
| 123 | unset( self::$previous_urls[ self::post_key( $id ) ] ); |
| 124 | return; |
| 125 | } |
| 126 | $old = self::$previous_urls[ self::post_key( $id ) ] ?? ''; |
| 127 | unset( self::$previous_urls[ self::post_key( $id ) ] ); |
| 128 | if ( ! $old && $before instanceof \WP_Post && self::eligible( $before ) ) { |
| 129 | $old = get_permalink( $before ); |
| 130 | } |
| 131 | if ( wp_is_post_revision( $id ) || wp_is_post_autosave( $id ) ) { |
| 132 | return; |
| 133 | } |
| 134 | if ( ! self::eligible( $post ) ) { |
| 135 | if ( ! self::removal_allowed( $post ) ) { |
| 136 | self::notify_restriction( $id ); |
| 137 | } |
| 138 | if ( $old && self::removal_allowed( $post ) ) { |
| 139 | self::notify( $old, $id, true ); |
| 140 | } |
| 141 | delete_post_meta( $id, self::PUBLIC_URL_META ); |
| 142 | return; |
| 143 | } |
| 144 | $current = get_permalink( $post ); |
| 145 | if ( $old && $old !== $current ) { |
| 146 | self::notify( $old, $id, true ); |
| 147 | } |
| 148 | if ( ! $metadata_changed && $updated && $before instanceof \WP_Post && $old === $current ) { |
| 149 | $changed = false; |
| 150 | foreach ( array( 'post_title', 'post_content', 'post_excerpt', 'post_status', 'post_name', 'post_parent', 'post_author', 'post_date' ) as $field ) { |
| 151 | $changed = $changed || $before->$field !== $post->$field; |
| 152 | } |
| 153 | if ( ! $changed ) { |
| 154 | return; |
| 155 | } |
| 156 | } |
| 157 | update_post_meta( $id, self::PUBLIC_URL_META, $current ); |
| 158 | self::notify( $current, $id ); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Recheck a previously public URL without revealing private or protected content. |
| 163 | * |
| 164 | * @param \WP_Post|null $post Current content, or null after permanent deletion. |
| 165 | * @return bool |
| 166 | */ |
| 167 | public static function removal_allowed( $post ) { |
| 168 | return ! $post || ( $post instanceof \WP_Post |
| 169 | && in_array( $post->post_status, array( 'publish', 'trash', 'draft', 'pending', 'future' ), true ) |
| 170 | && ! Content_Gate::is_gated( $post ) |
| 171 | && in_array( $post->post_type, Post_Types::get_supported_content_types(), true ) ); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Capture the public URL before WordPress changes its status to trash. |
| 176 | * |
| 177 | * @param int $id Post ID. |
| 178 | */ |
| 179 | public static function trashing( $id ) { |
| 180 | $post = get_post( $id ); |
| 181 | if ( ! self::supported( $post ) || ! Indexnow_Client::enabled() ) { |
| 182 | return; |
| 183 | } |
| 184 | if ( self::eligible( $post ) ) { |
| 185 | $known = get_post_meta( $id, self::PUBLIC_URL_META, true ); |
| 186 | self::$previous_urls[ self::post_key( $id ) ] = $known ? $known : get_permalink( $post ); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Retain a public URL before permanent removal destroys the post record. |
| 192 | * |
| 193 | * @param int $id Post ID. |
| 194 | * @param \WP_Post $post Previous post. |
| 195 | */ |
| 196 | public static function deleted( $id, $post ) { |
| 197 | if ( ! self::supported( $post ) || ! Indexnow_Client::enabled() ) { |
| 198 | return; |
| 199 | } |
| 200 | self::$deleting[ self::post_key( $id ) ] = true; |
| 201 | if ( ! self::removal_allowed( $post ) ) { |
| 202 | unset( self::$pending_metadata[ self::post_key( $id ) ] ); |
| 203 | self::notify_restriction( $id ); |
| 204 | return; |
| 205 | } |
| 206 | $url = get_post_meta( $id, self::PUBLIC_URL_META, true ); |
| 207 | $url = $url ? $url : ( self::$pending_metadata[ self::post_key( $id ) ]['url'] ?? '' ); |
| 208 | unset( self::$pending_metadata[ self::post_key( $id ) ] ); |
| 209 | if ( $url || self::eligible( $post ) ) { |
| 210 | if ( ! $url && self::eligible( $post ) ) { |
| 211 | $url = get_permalink( $post ); |
| 212 | } |
| 213 | if ( $url ) { |
| 214 | self::notify( $url, $id, true ); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Release request-local deletion state once core has finished. |
| 221 | * |
| 222 | * @param int $id Deleted post ID. |
| 223 | */ |
| 224 | public static function forget_deleted( $id ) { |
| 225 | unset( self::$deleting[ self::post_key( $id ) ], self::$previous_urls[ self::post_key( $id ) ] ); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Snapshot visibility before the first relevant metadata write. |
| 230 | * |
| 231 | * @param mixed $check Metadata short-circuit result. |
| 232 | * @param int $post_id Source post ID. |
| 233 | * @param string $key Meta key. |
| 234 | * @return mixed |
| 235 | */ |
| 236 | public static function remember_before_meta_change( $check, $post_id, $key ) { |
| 237 | if ( ! self::relevant_metadata( $key ) || isset( self::$deleting[ self::post_key( $post_id ) ] ) ) { |
| 238 | return $check; |
| 239 | } |
| 240 | $post = get_post( $post_id ); |
| 241 | if ( ! self::supported( $post ) || ! Indexnow_Client::enabled() ) { |
| 242 | return $check; |
| 243 | } |
| 244 | $identity = self::post_key( $post_id ); |
| 245 | if ( ! isset( self::$pending_metadata[ $identity ] ) && count( self::$pending_metadata ) < self::MAX_PENDING_METADATA ) { |
| 246 | self::$pending_metadata[ $identity ] = array( |
| 247 | 'blog' => get_current_blog_id(), |
| 248 | 'post' => $post_id, |
| 249 | 'url' => self::eligible( $post ) ? get_permalink( $post ) : '', |
| 250 | ); |
| 251 | } |
| 252 | return $check; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Reconsider public visibility after relevant metadata changes. |
| 257 | * |
| 258 | * @param mixed $meta_id Meta row ID. |
| 259 | * @param int $post_id Post ID. |
| 260 | * @param string $key Meta key. |
| 261 | */ |
| 262 | public static function meta_changed( $meta_id, $post_id, $key ) { |
| 263 | $identity = self::post_key( $post_id ); |
| 264 | if ( isset( self::$deleting[ $identity ] ) || ! self::relevant_metadata( $key ) ) { |
| 265 | return; |
| 266 | } |
| 267 | $post = get_post( $post_id ); |
| 268 | if ( ! self::supported( $post ) || ! Indexnow_Client::enabled() ) { |
| 269 | return; |
| 270 | } |
| 271 | if ( isset( self::$previous_urls[ $identity ] ) || ! get_post_meta( $post_id, self::PUBLIC_URL_META, true ) ) { |
| 272 | if ( isset( self::$pending_metadata[ $identity ] ) || count( self::$pending_metadata ) < self::MAX_PENDING_METADATA ) { |
| 273 | self::$pending_metadata[ $identity ]['blog'] = get_current_blog_id(); |
| 274 | self::$pending_metadata[ $identity ]['post'] = $post_id; |
| 275 | self::$pending_metadata[ $identity ]['changed'] = true; |
| 276 | } |
| 277 | if ( ! self::removal_allowed( $post ) ) { |
| 278 | self::notify_restriction( $post_id ); |
| 279 | delete_post_meta( $post_id, self::PUBLIC_URL_META ); |
| 280 | } |
| 281 | return; |
| 282 | } |
| 283 | unset( self::$pending_metadata[ $identity ] ); |
| 284 | if ( ! self::removal_allowed( $post ) ) { |
| 285 | self::notify_restriction( $post_id ); |
| 286 | delete_post_meta( $post_id, self::PUBLIC_URL_META ); |
| 287 | } elseif ( self::eligible( $post ) ) { |
| 288 | update_post_meta( $post_id, self::PUBLIC_URL_META, get_permalink( $post ) ); |
| 289 | self::notify( get_permalink( $post ), $post_id ); |
| 290 | } elseif ( Content_Coverage::META_NOINDEX === $key ) { |
| 291 | self::notify( get_post_meta( $post_id, self::PUBLIC_URL_META, true ), $post_id, true ); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /** Reconcile standalone metadata writes after any enclosing post save has completed. */ |
| 296 | public static function flush_metadata_changes() { |
| 297 | $pending = self::$pending_metadata; |
| 298 | self::$pending_metadata = array(); |
| 299 | foreach ( $pending as $change ) { |
| 300 | if ( empty( $change['changed'] ) ) { |
| 301 | continue; |
| 302 | } |
| 303 | $switched = (int) $change['blog'] !== get_current_blog_id(); |
| 304 | if ( $switched ) { |
| 305 | switch_to_blog( $change['blog'] ); |
| 306 | } |
| 307 | try { |
| 308 | $id = $change['post']; |
| 309 | $post = get_post( $id ); |
| 310 | if ( ! Indexnow_Client::enabled() || ! self::supported( $post ) ) { |
| 311 | continue; |
| 312 | } |
| 313 | if ( self::eligible( $post ) ) { |
| 314 | $url = get_permalink( $post ); |
| 315 | update_post_meta( $id, self::PUBLIC_URL_META, $url ); |
| 316 | self::notify( $url, $id ); |
| 317 | } elseif ( ! empty( $change['url'] ) && self::removal_allowed( $post ) && get_post_meta( $id, Content_Coverage::META_NOINDEX, true ) ) { |
| 318 | self::notify( $change['url'], $id, true ); |
| 319 | } |
| 320 | } finally { |
| 321 | if ( $switched ) { |
| 322 | restore_current_blog(); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Scope request-local post state to its originating site. |
| 330 | * |
| 331 | * @param int $id Post ID. |
| 332 | * @return string |
| 333 | */ |
| 334 | private static function post_key( $id ) { |
| 335 | return get_current_blog_id() . ':' . $id; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Exclude content types and placeholders that cannot enter the queue. |
| 340 | * |
| 341 | * @param \WP_Post|null $post Current post. |
| 342 | * @return bool |
| 343 | */ |
| 344 | private static function supported( $post ) { |
| 345 | return $post instanceof \WP_Post && 'auto-draft' !== $post->post_status |
| 346 | && in_array( $post->post_type, Post_Types::get_supported_content_types(), true ); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Whether metadata can change notification eligibility or search output. |
| 351 | * |
| 352 | * @param string $key Metadata key. |
| 353 | * @return bool |
| 354 | */ |
| 355 | private static function relevant_metadata( $key ) { |
| 356 | return in_array( |
| 357 | $key, |
| 358 | array( |
| 359 | Content_Coverage::META_NOINDEX, |
| 360 | Content_Coverage::META_TITLE, |
| 361 | Content_Coverage::META_DESCRIPTION, |
| 362 | Content_Coverage::META_SCHEMA_TYPE, |
| 363 | '_thumbnail_id', |
| 364 | Content_Gate::META_NEWSLETTER_ACCESS, |
| 365 | Content_Gate::META_CONTAINS_PAID_CONTENT, |
| 366 | Content_Gate::META_CONTAINS_PAYWALLED_CONTENT, |
| 367 | ), |
| 368 | true |
| 369 | ); |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Stop older notifications before deletion can erase a restriction. |
| 374 | * |
| 375 | * @param int $post_id Restricted content ID. |
| 376 | */ |
| 377 | private static function notify_restriction( $post_id ) { |
| 378 | /** |
| 379 | * Fires when queued URLs must be forgotten for restricted content. |
| 380 | * |
| 381 | * @since $$next-version$$ |
| 382 | * @param int $post_id Restricted content ID. |
| 383 | */ |
| 384 | do_action( 'jetpack_seo_indexnow_post_restricted', $post_id ); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Publish an eligible change without requiring a delivery implementation. |
| 389 | * |
| 390 | * @param string $url Known public URL. |
| 391 | * @param int $post_id Source post ID. |
| 392 | * @param bool $removed Whether this refers to the previous public representation. |
| 393 | */ |
| 394 | private static function notify( $url, $post_id, $removed = false ) { |
| 395 | /** |
| 396 | * Fires when a public URL has a change eligible for IndexNow. |
| 397 | * |
| 398 | * @since $$next-version$$ |
| 399 | * @param string $url Known public URL. |
| 400 | * @param int $post_id Source post ID. |
| 401 | * @param bool $removed Whether the previous public representation was removed. |
| 402 | */ |
| 403 | do_action( 'jetpack_seo_indexnow_url_changed', $url, $post_id, $removed ); |
| 404 | } |
| 405 | } |