add_action( 'save_post', $this, 'maybe_schedule', 10, 2 ); $loader->add_action( self::HOOK, $this, 'run', 10, 1 ); } /** * Schedules a background job to (re-)cache one post's content_score, * skipping autosaves/revisions/non-published posts and de-duplicating * against an already-queued job for the same post (rapid consecutive * saves — e.g. autosave then a real save moments later — shouldn't * pile up redundant jobs). * * @param int $post_id Post ID being saved. * @param WP_Post $post Post object. */ public function maybe_schedule( $post_id, $post ): void { if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) { return; } if ( 'publish' !== $post->post_status ) { return; } if ( ! function_exists( 'as_schedule_single_action' ) ) { return; // Action Scheduler not available; caller falls back to no-op rather than blocking a real request. } if ( as_has_scheduled_action( self::HOOK, array( $post_id ), self::GROUP ) ) { return; } as_schedule_single_action( time(), self::HOOK, array( $post_id ), self::GROUP ); } /** * Runs the actual (re-)analysis for one post, using whatever focus * keyword is currently set. analyze() persists content_score to * wp_abds_seo_page_scores itself as an existing side effect — this * method exists only to trigger that with the right keyword, not to * duplicate its persistence logic. * * @param int $post_id Post ID. */ public function run( int $post_id ): void { $keyword = (string) get_post_meta( $post_id, '_abds_seo_focus_keyword', true ); ( new ABDS_SEO_Content_Analyzer() )->analyze( $post_id, $keyword ); } }