Skip to content

refactor: align Chat REST handlers with core Abilities API contract#1000

Merged
chubes4 merged 1 commit intomainfrom
refactor/chat-rest-ability-contract
Mar 30, 2026
Merged

refactor: align Chat REST handlers with core Abilities API contract#1000
chubes4 merged 1 commit intomainfrom
refactor/chat-rest-ability-contract

Conversation

@chubes4
Copy link
Copy Markdown
Member

@chubes4 chubes4 commented Mar 30, 2026

Summary

Remove all legacy fallback paths from Chat REST session handlers. REST is a thin wrapper around abilities — nothing else.

  • Remove function_exists('wp_get_ability') guards (WP 6.9 minimum)
  • Remove 3 direct-DB fallback code paths (list, get, delete)
  • Introduce execute_ability() helper that follows core's WP_Ability::execute() contract
  • Handle both WP_Error returns (from core) and legacy { success: false } arrays (from our callbacks)
  • Every session handler is now a one-liner

Net: -61 lines (79 added, 140 removed)

Before

public static function delete_session( WP_REST_Request $request ) {
    $ability = function_exists( 'wp_get_ability' ) ? wp_get_ability( '...' ) : null;
    if ( $ability ) {
        $result = $ability->execute( ... );
        if ( empty( $result['success'] ) ) { return new WP_Error( ... ); }
        return rest_ensure_response( ... );
    }
    // Fallback: 20 lines of duplicated DB logic...
}

After

public static function delete_session( WP_REST_Request $request ) {
    return self::execute_ability(
        'datamachine/delete-chat-session',
        array(
            'session_id' => sanitize_text_field( $request->get_param( 'session_id' ) ),
            'user_id'    => get_current_user_id(),
        )
    );
}

Part of the broader ability contract alignment tracked in #999.

Remove legacy fallback paths from all Chat REST session handlers
(list, get, delete). REST handlers are thin wrappers around abilities.

Introduce execute_ability() helper that follows core's WP_Ability::execute()
contract:
- wp_get_ability() directly (WP 6.9+ guaranteed)
- Core handles input validation, permissions, callback execution
- WP_Error pass-through for core failures
- Temporary shim for legacy { success: false } array convention (#999)

Every session handler is now a one-liner: extract params → execute_ability().

Part of the broader ability contract alignment tracked in #999.
@chubes4 chubes4 merged commit a4051fb into main Mar 30, 2026
1 check failed
@chubes4 chubes4 deleted the refactor/chat-rest-ability-contract branch March 30, 2026 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant