add_action( 'rest_api_init', $this, 'register_routes' ); } /** * Registers every REST route this controller owns. */ public function register_routes(): void { register_rest_route( self::NAMESPACE_V1, '/audit/runs', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'start_run' ), 'permission_callback' => array( $this, 'can_audit' ), ) ); register_rest_route( self::NAMESPACE_V1, '/audit/runs/(?P\d+)', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_run_status' ), 'permission_callback' => array( $this, 'can_audit' ), ) ); register_rest_route( self::NAMESPACE_V1, '/audit/runs/(?P\d+)/issues', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_run_issues' ), 'permission_callback' => array( $this, 'can_audit' ), ) ); } /** * POST /audit/runs — starts a new full-site audit run and immediately * returns its status row (D4: the caller starts polling GET /audit/runs/{id} * against the run_id in this response right away, not a separate lookup). * * @return WP_REST_Response|WP_Error */ public function start_run() { $scheduler = new ABDS_SEO_Audit_Scheduler(); $run_id = $scheduler->start_audit_run( 'manual', get_current_user_id() ); if ( 0 === $run_id ) { return new WP_Error( 'abds_seo_audit_unavailable', __( 'The background task runner is unavailable, so an audit can\'t be started right now.', 'abds-seo' ), array( 'status' => 503 ) ); } return new WP_REST_Response( $scheduler->get_run_status( $run_id ) ); } /** * GET /audit/runs/{id} — the run's own progress/result row. * * @param WP_REST_Request $request REST request. * @return WP_REST_Response|WP_Error */ public function get_run_status( WP_REST_Request $request ) { $status = ( new ABDS_SEO_Audit_Scheduler() )->get_run_status( (int) $request->get_param( 'id' ) ); if ( null === $status ) { return new WP_Error( 'abds_seo_audit_run_not_found', __( 'Audit run not found.', 'abds-seo' ), array( 'status' => 404 ) ); } return new WP_REST_Response( $status ); } /** * GET /audit/runs/{id}/issues — the run's issues grouped by issue_key, * the shape the IssueList screen renders (mockup Screen 3). * * @param WP_REST_Request $request REST request. * @return WP_REST_Response */ public function get_run_issues( WP_REST_Request $request ): WP_REST_Response { $issues = ( new ABDS_SEO_Audit_Scheduler() )->get_run_issues( (int) $request->get_param( 'id' ) ); return new WP_REST_Response( $issues ); } }