Make WordPress Core


Ignore:
Timestamp:
08/14/2026 11:17:51 AM (3 months ago)
Author:
gziolo
Message:

Clone the wrapped builder when cloning WP_AI_Client_Prompt_Builder

WP_AI_Client_Prompt_Builder had no __clone(), so cloning it copied the reference to the wrapped PromptBuilder instead of the builder itself. That builder mutates its own state, so the clone handed to the wp_ai_client_prevent_prompt filter shared its messages and configuration with the original, and a filter could change the prompt that was then sent to the provider, despite the clone being documented as read-only.

Add __clone() so the wrapped builder, and any stored error, are copied too. The bundled client already implements PromptBuilder::__clone() to deep clone messages, model config and request options, it just never ran because only the wrapper was being cloned.

Props bejignesh, gziolo.
Fixes #65782.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php

    r63298 r63299  
    227227
    228228        /**
     229         * Clones the wrapped prompt builder alongside this instance.
     230         *
     231         * The wrapped builder mutates its own state, so without this a clone would
     232         * share that state with the original and any change made to one would be
     233         * visible in the other.
     234         *
     235         * @since 7.2.0
     236         */
     237        public function __clone() {
     238                $this->builder = clone $this->builder;
     239
     240                if ( null !== $this->error ) {
     241                        $this->error = clone $this->error;
     242                }
     243        }
     244
     245        /**
    229246         * Registers WordPress abilities as function declarations for the AI model.
    230247         *
Note: See TracChangeset for help on using the changeset viewer.