*/ private array $permalinks = []; /** * Constructor. * * @param IndexNowSettings $settings Settings instance. */ public function __construct( IndexNowSettings $settings ) { $this->settings = $settings; } /** * Remember the permalink a post has right now. * * Called before an update lands, so a later trash can submit the URL * that actually existed rather than the __trashed variant. * * @param int $postId Post id. * @param string $permalink Permalink at capture time. * @return void */ public function snapshotPermalink( int $postId, string $permalink ): void { if ( $postId <= 0 || '' === $permalink ) { return; } $this->permalinks[ $postId ] = $permalink; } /** * URLs to submit for a post transition. * * A trash transition returns the snapshot, the URL captured before * the status changed. A publish signal submits the current permalink * and, when a previously published post changed its permalink since * the snapshot was taken, the previous URL first so the old address * is signalled for removal. The snapshot is refreshed afterwards. * * @param int $postId Post id. * @param string $newStatus New post status. * @param string $oldStatus Previous post status. * @return string[] The result. */ public function postUrls( int $postId, string $newStatus, string $oldStatus ): array { if ( 'trash' === $newStatus ) { $snapshot = $this->permalinks[ $postId ] ?? ''; return '' === $snapshot ? [] : [ $snapshot ]; } $permalink = $this->currentPermalink( $postId ); if ( '' === $permalink ) { return []; } $previous = $this->permalinks[ $postId ] ?? ''; $urls = [ $permalink ]; // The previous URL only counts when the post was published before: // the pre-publish permalink of a draft was never live, so signalling // it would send a draft URL the spec excludes. if ( 'publish' === $oldStatus && '' !== $previous && $previous !== $permalink ) { array_unshift( $urls, $previous ); } $this->snapshotPermalink( $postId, $permalink ); return $urls; } /** * Public URL of a term, empty when it cannot be resolved. * * @param int $termId Term id. * @param string $taxonomy Taxonomy slug. * @return string The result. */ public function termUrl( int $termId, string $taxonomy ): string { if ( $termId <= 0 || '' === $taxonomy ) { return ''; } if ( function_exists( 'taxonomy_exists' ) && ! taxonomy_exists( $taxonomy ) ) { return ''; } if ( function_exists( 'is_taxonomy_viewable' ) && ! is_taxonomy_viewable( $taxonomy ) ) { return ''; } if ( ! function_exists( 'get_term_link' ) ) { return ''; } $link = get_term_link( $termId, $taxonomy ); return is_string( $link ) ? $link : ''; } /** * Whether a URL was submitted inside the debounce window. * * @param int $postId Post id. * @param string $url URL to check. * @return bool The result. */ public function wasRecentlySubmitted( int $postId, string $url ): bool { if ( $postId <= 0 || '' === $url || ! function_exists( 'get_post_meta' ) ) { return false; } $stored = get_post_meta( $postId, self::META_KEY, true ); if ( ! is_array( $stored ) ) { return false; } if ( (string) ( $stored['last_url'] ?? '' ) !== $url ) { return false; } return ( time() - (int) ( $stored['last_submitted'] ?? 0 ) ) < $this->debounceSeconds(); } /** * Record that a URL was just submitted for a post. * * @param int $postId Post id. * @param string $url Submitted URL. * @param string $contentHash Content hash for the unchanged-republish dedupe. * @return void */ public function markSubmitted( int $postId, string $url, string $contentHash = '' ): void { if ( $postId <= 0 || '' === $url || ! function_exists( 'update_post_meta' ) ) { return; } update_post_meta( $postId, self::META_KEY, [ 'last_url' => $url, 'last_submitted' => time(), 'content_hash' => $contentHash, ] ); } /** * Whether a republish carries byte-identical content to the last submission. * * A publish-to-publish re-save with the same URL, same status context and * unchanged content submits nothing: there is nothing new for the engine * to discover. Any content change still submits, so genuine updates are * never swallowed. * * @param int $postId Post id. * @param string $url Current URL. * @param string $contentHash Hash of the current content. * @return bool True when the republish is a no-op for submission. */ public function isUnchangedRepublish( int $postId, string $url, string $contentHash ): bool { if ( $postId <= 0 || '' === $url || '' === $contentHash || ! function_exists( 'get_post_meta' ) ) { return false; } $stored = get_post_meta( $postId, self::META_KEY, true ); if ( ! is_array( $stored ) ) { return false; } return (string) ( $stored['last_url'] ?? '' ) === $url && (string) ( $stored['content_hash'] ?? '' ) === $contentHash; } /** * Debounce window in seconds, owned by the settings class. * * Read through the injected instance so the window stays the * settings class's contract rather than a value duplicated here. * * @return int The result. */ private function debounceSeconds(): int { return (int) $this->settings::DEBOUNCE_SECONDS; } /** * Current permalink of a post, empty when it cannot be resolved. * * @param int $postId Post id. * @return string The result. */ private function currentPermalink( int $postId ): string { if ( ! function_exists( 'get_permalink' ) ) { return ''; } $permalink = get_permalink( $postId ); return is_string( $permalink ) ? $permalink : ''; } }