health = $health; } /** * Hook the routes. */ public function register(): void { add_action( 'rest_api_init', array( $this, 'register_routes' ) ); } /** * Register the routes. */ public function register_routes(): void { register_rest_route( self::NAMESPACE, self::ROUTE, array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get' ), 'permission_callback' => array( $this, 'can_manage' ), ) ); register_rest_route( self::NAMESPACE, self::ROUTE . '/recheck', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'recheck' ), 'permission_callback' => array( $this, 'can_manage' ), ) ); } /** * Whether the current user may manage the plugin. */ public function can_manage(): bool { return current_user_can( Capabilities::MANAGE ); } /** * The checks. */ public function get(): WP_REST_Response { return new WP_REST_Response( $this->payload( false ) ); } /** * Read the home page again, then the checks. */ public function recheck(): WP_REST_Response { return new WP_REST_Response( $this->payload( true ) ); } /** * What the tab shows. * * @param bool $refresh Whether to read the home page again whatever is kept. * @return array{checks: list>, checked: int, counts: array{pass: int, warn: int, fail: int}} */ private function payload( bool $refresh ): array { $checks = $this->health->results( true, $refresh ); $counts = array( Health::PASS => 0, Health::WARN => 0, Health::FAIL => 0, ); foreach ( $checks as $check ) { ++$counts[ $check['status'] ]; } return array( 'checks' => $checks, 'checked' => $this->health->checked(), 'counts' => $counts, ); } }