links = $links; } /** * @since 1.18.4 * * @return array */ public function get_hooks(): array { return array( // Fires for publishing, editing, trashing, and a scheduled post // going live (wp_publish_post() fires it too), after the post's // own data is saved. Postlinkly's link inserts save through // wp_update_post(), so they arrive here as well. array( 'wp_after_insert_post', array( $this, 'on_post_saved' ), 20, 2 ), // Permanent deletion; trashing is a status change handled above. array( 'delete_post', array( $this, 'on_post_deleted' ), 10, 1 ), ); } /** * Indexes a saved post, or removes it when it is no longer tracked. * * @since 1.18.4 * @hook wp_after_insert_post * * @param int $post_id Post that was saved. * @param WP_Post $post The saved post. * * @return void */ public function on_post_saved( int $post_id, $post ): void { if ( ! $post instanceof WP_Post ) { return; } // Auto-drafts are the empty placeholders the editor creates when it // opens. Revisions and untracked types were never in the report. if ( 'auto-draft' === $post->post_status || ! $this->links->tracks_type( $post->post_type ) ) { return; } $done = $this->links->is_tracked( $post ) ? $this->links->index_post( $post ) : $this->links->remove_post( $post_id ); $this->after_change( $done, $post_id ); } /** * Removes a permanently deleted post's links from the report. * * @since 1.18.4 * @hook delete_post * * @param int $post_id Post being deleted. * * @return void */ public function on_post_deleted( int $post_id ): void { // Deleting a post deletes each of its revisions too, one call each; // the post still exists at this point, so its type can be checked. $post = get_post( $post_id ); if ( ! $post instanceof WP_Post || ! $this->links->tracks_type( $post->post_type ) ) { return; } $this->after_change( $this->links->remove_post( $post_id ), $post_id ); } /** * Refreshes the Dashboard, or records a write the database refused. * * A refused write leaves the post unmarked, so the next full scan retries * it; the log says why its counts are out of date until then. * * @since 1.18.4 * * @param bool $done Whether the report was updated. * @param int $post_id Post concerned. * * @return void */ private function after_change( bool $done, int $post_id ): void { if ( ! $done ) { Logger::error( 'Could not update the link report for post %d; the next full scan will retry it.', array( $post_id ) ); return; } DashboardStatsProvider::flush_cache(); } }