share( Auditor::class, static fn() => new Auditor( $container->get( Options::class ) ) ); $container->share( Alerts::class, static fn() => new Alerts( $container->get( Options::class ), $container->get( Auditor::class ) ) ); } public function boot( Container $container ): void { $alerts = $container->get( Alerts::class ); add_action( Alerts::CRON_HOOK, array( $alerts, 'run' ) ); $alerts->ensure_schedule(); /* * Registered in boot(), not inside rest_api_init. * * The resumer ticks an abandoned run from cron; the lock keeps two * tickers apart. But a WP-Cron request never fires rest_api_init, so * while this line lived inside that closure the registry was empty * exactly when the resumer needed it — and an audit whose browser tab * was closed stopped where it stood. Observed on a live site: 66 of * 156 pages, frozen for three hours, and one manual tick moved it * straight on to 88. */ \AiRankSuite\Support\Job::register( 'audit', static fn() => $container->get( Auditor::class )->status(), static fn() => $container->get( Auditor::class )->tick() ); add_action( 'rest_api_init', function () use ( $container ): void { $auditor = $container->get( Auditor::class ); register_rest_route( self::NAMESPACE, '/audit/start', array( 'methods' => 'POST', 'callback' => static fn( WP_REST_Request $request ) => new WP_REST_Response( $auditor->start( array_map( 'sanitize_key', (array) ( $request->get_param( 'post_types' ) ?: array() ) ), (int) ( $request->get_param( 'limit' ) ?: 5000 ) ) ), // A full run touches every page — cap restarts hard. 'permission_callback' => static function () { if ( ! current_user_can( 'manage_options' ) ) { return self::forbidden(); } return self::spend( 'audit', 20 ); }, 'args' => array( 'post_types' => array( 'type' => 'array' ), 'limit' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 50000, 'default' => 5000, ), ), ) ); register_rest_route( self::NAMESPACE, '/audit/tick', array( 'methods' => 'POST', 'callback' => static fn() => new WP_REST_Response( \AiRankSuite\Support\Job::guard( 'audit', array( $auditor, 'tick' ) ) ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); register_rest_route( self::NAMESPACE, '/audit/status', array( 'methods' => 'GET', 'callback' => static fn() => new WP_REST_Response( $auditor->status() ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); register_rest_route( self::NAMESPACE, '/audit/cancel', array( 'methods' => 'POST', 'callback' => static fn() => new WP_REST_Response( $auditor->cancel() ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); register_rest_route( self::NAMESPACE, '/audit/report', array( 'methods' => 'GET', 'callback' => static fn( WP_REST_Request $request ) => new WP_REST_Response( $auditor->report( (string) ( $request->get_param( 'group' ) ?: '' ), (string) ( $request->get_param( 'severity' ) ?: '' ), max( 1, (int) ( $request->get_param( 'page' ) ?: 1 ) ), (int) ( $request->get_param( 'per_page' ) ?: 50 ), (string) ( $request->get_param( 'lang' ) ?: '' ), (string) ( $request->get_param( 'check' ) ?: '' ), (string) ( $request->get_param( 'type' ) ?: '' ) ) ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'check' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_key' ), 'type' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_key' ), 'group' => array( 'type' => 'string', 'enum' => array_merge( array( '' ), array_keys( Auditor::groups() ) ), ), 'severity' => array( 'type' => 'string', 'enum' => array_merge( array( '' ), Auditor::SEVERITIES ), ), 'page' => array( 'type' => 'integer', 'minimum' => 1, 'default' => 1, ), 'per_page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 200, 'default' => 50, ), 'lang' => array( 'type' => 'string' ), ), ) ); register_rest_route( self::NAMESPACE, '/audit/trend', array( 'methods' => 'GET', 'callback' => static fn( WP_REST_Request $request ) => new WP_REST_Response( array( 'runs' => $auditor->trend( (int) ( $request->get_param( 'limit' ) ?: 20 ) ) ) ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'limit' => array( 'type' => 'integer', 'minimum' => 2, 'maximum' => 60, 'default' => 20 ), ), ) ); // The findings as spreadsheet rows. The server returns data and // nothing else: the browser writes the file. Echoing a document from // a REST callback means output that no escaping function fits, and a // CSV written on the server still has to be read by a spreadsheet, // which happily runs a cell that starts with = + - or @. Both // problems disappear when the rows travel as JSON. register_rest_route( self::NAMESPACE, '/audit/export', array( 'methods' => 'GET', 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'callback' => static function () use ( $auditor ) { $rows = array(); foreach ( $auditor->export_rows() as $row ) { $rows[] = array_map( static function ( $value ): string { $text = sanitize_text_field( (string) $value ); // A leading =, +, - or @ makes a spreadsheet treat the // cell as a formula; a leading apostrophe makes it text. return '' !== $text && strpos( "=+-@\t\r", $text[0] ) !== false ? "'" . $text : $text; }, $row ); } return rest_ensure_response( array( 'filename' => 'site-audit-' . gmdate( 'Y-m-d' ) . '.csv', 'rows' => $rows, ) ); }, ) ); register_rest_route( self::NAMESPACE, '/audit/ignore', array( 'methods' => 'POST', 'callback' => static function ( WP_REST_Request $request ) use ( $auditor ) { $on = null === $request->get_param( 'on' ) ? true : (bool) $request->get_param( 'on' ); $check = (string) ( $request->get_param( 'check_id' ) ?: '' ); // Either one finding by fingerprint, or a whole rule by check id. if ( '' !== $check ) { return new WP_REST_Response( array( 'ignored' => $auditor->ignored(), 'ignored_rules' => $auditor->ignore_rule( $check, $on ), ) ); } return new WP_REST_Response( array( 'ignored' => $auditor->ignore( (string) $request->get_param( 'fingerprint' ), $on ), 'ignored_rules' => $auditor->ignored_rules(), ) ); }, 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'fingerprint' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_key', ), 'check_id' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_key', ), 'on' => array( 'type' => 'boolean' ), ), ) ); register_rest_route( self::NAMESPACE, '/audit/alerts', array( array( 'methods' => 'GET', 'callback' => static fn() => new WP_REST_Response( array( 'settings' => \AiRankSuite\Kernel::service( Options::class )->get( 'audit_alerts', Alerts::defaults() ), 'next' => (int) ( wp_next_scheduled( Alerts::CRON_HOOK ) ?: 0 ), ) ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ), array( 'methods' => 'PUT', 'callback' => function ( WP_REST_Request $request ) use ( $container ) { $defaults = Alerts::defaults(); $values = array(); foreach ( array_keys( $defaults ) as $key ) { if ( $request->has_param( $key ) ) { $values[ $key ] = $request->get_param( $key ); } } $options = $container->get( Options::class ); $options->set( 'audit_alerts', $values, $defaults ); // The schedule has to follow the setting in the // same request, or switching alerts on leaves a // cron event that never gets created. $alerts = $container->get( Alerts::class ); wp_clear_scheduled_hook( Alerts::CRON_HOOK ); $alerts->ensure_schedule(); return new WP_REST_Response( array( 'settings' => $options->get( 'audit_alerts', $defaults ), 'next' => (int) ( wp_next_scheduled( Alerts::CRON_HOOK ) ?: 0 ), ) ); }, 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'enabled' => array( 'type' => 'boolean' ), 'frequency' => array( 'type' => 'string', 'enum' => array( 'daily', 'weekly' ), ), 'severity' => array( 'type' => 'string', 'enum' => array( 'critical', 'warning' ), ), 'recipients' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), ), ), ) ); register_rest_route( self::NAMESPACE, '/audit/alerts/test', array( 'methods' => 'POST', 'callback' => static fn() => new WP_REST_Response( $container->get( Alerts::class )->notify() ), 'permission_callback' => static function () { if ( ! current_user_can( 'manage_options' ) ) { return self::forbidden(); } return self::spend( 'alerttest', 10 ); }, ) ); register_rest_route( self::NAMESPACE, '/audit/site-tests', array( 'methods' => 'POST', // Each run makes a handful of loopback HTTP requests to this // same site, so it is metered like an audit rather than like a // settings read. 'permission_callback' => static function () { if ( ! current_user_can( 'manage_options' ) ) { return self::forbidden(); } return self::spend( 'site_tests', 12 ); }, 'callback' => static function () { $report = Services\SiteTests::run(); update_option( 'airanksuite_site_tests', $report, false ); return new WP_REST_Response( $report ); }, ) ); register_rest_route( self::NAMESPACE, '/audit/site-tests', array( 'methods' => 'GET', 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'callback' => static function () { $stored = get_option( 'airanksuite_site_tests', array() ); return new WP_REST_Response( is_array( $stored ) && ! empty( $stored ) ? $stored : array( 'ran_at' => 0, 'tests' => array(), 'groups' => Services\SiteTests::groups() ) ); }, ) ); register_rest_route( self::NAMESPACE, '/audit/checks', array( 'methods' => 'GET', 'callback' => static function () { $out = array(); foreach ( Auditor::checks() as $id => $def ) { $out[] = array_merge( array( 'id' => $id ), $def ); } return new WP_REST_Response( array( 'checks' => $out, 'groups' => Auditor::groups(), ) ); }, 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); } ); } }