\WP_REST_Server::CREATABLE, 'callback' => array( $this, 'clear_cache' ), 'permission_callback' => array( $this, 'can_manage' ), 'args' => array(), ) ); register_rest_route( self::REST_NAMESPACE, '/health', array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'health' ), 'permission_callback' => array( $this, 'can_manage' ), 'args' => array(), ) ); register_rest_route( self::REST_NAMESPACE, '/site-health/(?Pconnection|tag)', array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'site_health' ), 'permission_callback' => array( $this, 'can_view_site_health' ), 'args' => array( 'test' => array( 'type' => 'string', 'enum' => array( 'connection', 'tag' ), ), ), ) ); } /** * Permission callback for /site-health/{test}: WordPress's own Site * Health capability, matching what core requires for its async tests. */ public function can_view_site_health(): bool { return current_user_can( 'view_site_health_checks' ); } /** * POST /cache/clear → {ok:true}, or 429 within a minute of the last clear. * * @return \WP_REST_Response|\WP_Error */ public function clear_cache() { if ( false !== get_transient( self::CACHE_CLEAR_LOCK ) ) { return new \WP_Error( 'tagsmith_rate_limited', __( 'The report cache was cleared less than a minute ago. Please wait a moment and try again.', 'tagsmith' ), array( 'status' => 429 ) ); } set_transient( self::CACHE_CLEAR_LOCK, 1, MINUTE_IN_SECONDS ); $this->plugin->reports()->clear_cache(); return new \WP_REST_Response( array( 'ok' => true ) ); } /** * GET /health → {data: checks, pixel_scan}. `pixel_scan` is the ad pixel * scan this run made for the Ads & pixels tab, or null when it made none * (no ad pixel is on, or the homepage could not be read): an older scan * is never passed off as a new one. */ public function health(): \WP_REST_Response { $health = $this->plugin->health(); $checks = $health->run(); return new \WP_REST_Response( array( 'data' => $checks, 'pixel_scan' => $health->last_pixel_scan(), ) ); } /** * GET /site-health/{test} → the Site Health result array for that check * (label, status, badge, description, actions, test), for WordPress's * async Site Health tests to fetch. * * @param \WP_REST_Request $request The request, with the 'test' route param. */ public function site_health( \WP_REST_Request $request ): \WP_REST_Response { $test = (string) $request->get_param( 'test' ); $health = $this->plugin->health(); $result = 'connection' === $test ? $health->site_health_connection() : $health->site_health_tag(); return new \WP_REST_Response( $result ); } }