sessions = $sessions;
}
/**
* Hooks.
*/
public function register_hooks(): void {
add_action( 'admin_post_' . self::ACTION, array( $this, 'handle' ) );
}
/**
* Download URL for a session (the id is appended by the app).
*/
public static function url(): string {
return add_query_arg(
array(
'action' => self::ACTION,
'_wpnonce' => wp_create_nonce( self::ACTION ),
),
admin_url( 'admin-post.php' )
);
}
/**
* Send the report.
*/
public function handle(): void {
check_admin_referer( self::ACTION );
if ( ! current_user_can( Settings::capability() ) ) {
wp_die( esc_html__( 'You are not allowed to do that.', 'bloatwright' ), '', array( 'response' => 403 ) );
}
$id = isset( $_GET['bloatwright_session'] ) ? absint( wp_unslash( $_GET['bloatwright_session'] ) ) : 0;
$s = $id > 0 ? $this->sessions->find( $id ) : null;
if ( ! $s || empty( $s['summary'] ) ) {
wp_die( esc_html__( 'Session not found or not finished.', 'bloatwright' ), '', array( 'response' => 404 ) );
}
if ( ! headers_sent() ) {
nocache_headers();
header( 'Content-Type: text/html; charset=utf-8' );
header( 'Content-Disposition: attachment; filename="bloatwright-session-' . (int) $s['id'] . '.html"' );
}
echo HtmlExport::render( $s ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- fully escaped in HtmlExport.
if ( defined( 'BLOATWRIGHT_TESTS' ) && constant( 'BLOATWRIGHT_TESTS' ) ) {
return;
}
exit;
}
}