Make WordPress Core


Ignore:
Timestamp:
03/19/2026 01:55:45 PM (7 months ago)
Author:
SergeyBiryukov
Message:

AI: Introduce wp_supports_ai() function to control LLM-related features.

This includes a WP_AI_SUPPORT constant and a wp_supports_ai filter.

When false,

  • _wp_connectors_get_provider_settings() will return an empty array (short-circuiting the other functionality).
  • WP_AI_Client_Prompt_Builder() will short-circuit the construction with a WP_Error(). wp_ai_client_prompt() will still return an instance, to allow for fluidity to remain intact.

Priority: WP_AI_SUPPORT > add_filter( 'wp_supports_ai', ...) > (default: true)

Follow-up to [61943], [61749], [61943].

Props justlevine, westonruter, gziolo, flixos90, romainmrhenry, ahortin, chrismcelroyseo, SergeyBiryukov.
See #64706.

File:
1 edited

Legend:

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

    r62037 r62067  
    4646 *
    4747 * @since 7.0.0
     48 *
     49 * @phpstan-import-type Prompt from PromptBuilder
    4850 *
    4951 * @method self with_text(string $text) Adds text to the current message.
    … …  
    171173         * @since 7.0.0
    172174         *
    173          * @param ProviderRegistry                                                                 $registry The provider registry for finding suitable models.
    174          * @param string|MessagePart|Message|array|list<string|MessagePart|array>|list<Message>|null $prompt   Optional. Initial prompt content.
    175          *                                                                                                    A string for simple text prompts,
    176          *                                                                                                    a MessagePart or Message object for
    177          *                                                                                                    structured content, an array for a
    178          *                                                                                                    message array shape, or a list of
    179          *                                                                                                    parts or messages for multi-turn
    180          *                                                                                                    conversations. Default null.
     175         * @param ProviderRegistry $registry The provider registry for finding suitable models.
     176         * @param Prompt          $prompt   Optional. Initial prompt content.
     177         *                                   A string for simple text prompts,
     178         *                                   a MessagePart or Message object for
     179         *                                   structured content, an array for a
     180         *                                   message array shape, or a list of
     181         *                                   parts or messages for multi-turn
     182         *                                   conversations. Default null.
    181183         */
    182184        public function __construct( ProviderRegistry $registry, $prompt = null ) {
    … …  
    290292                // Check if the prompt should be prevented for is_supported* and generate_*/convert_text_to_speech* methods.
    291293                if ( self::is_support_check_method( $name ) || self::is_generating_method( $name ) ) {
    292                         /**
    293                          * Filters whether to prevent the prompt from being executed.
    294                          *
    295                          * @since 7.0.0
    296                          *
    297                          * @param bool                        $prevent Whether to prevent the prompt. Default false.
    298                          * @param WP_AI_Client_Prompt_Builder $builder A clone of the prompt builder instance (read-only).
    299                          */
    300                         $prevent = (bool) apply_filters( 'wp_ai_client_prevent_prompt', false, clone $this );
     294                        // If AI is not supported, then there's no need to apply the filter as the prompt will be prevented anyway.
     295                        $is_ai_disabled = ! wp_supports_ai();
     296                        $prevent        = $is_ai_disabled;
     297                        if ( ! $prevent ) {
     298                                /**
     299                                 * Filters whether to prevent the prompt from being executed.
     300                                 *
     301                                 * @since 7.0.0
     302                                 *
     303                                 * @param bool                        $prevent Whether to prevent the prompt. Default false.
     304                                 * @param WP_AI_Client_Prompt_Builder $builder A clone of the prompt builder instance (read-only).
     305                                 */
     306                                $prevent = (bool) apply_filters( 'wp_ai_client_prevent_prompt', false, clone $this );
     307                        }
    301308
    302309                        if ( $prevent ) {
    … …  
    306313                                }
    307314
     315                                $error_message = $is_ai_disabled
     316                                        ? __( 'AI features are not supported in this environment.' )
     317                                        : __( 'Prompt execution was prevented by a filter.' );
     318
    308319                                // For generate_* and convert_text_to_speech* methods, create a WP_Error.
    309320                                $this->error = new WP_Error(
    310321                                        'prompt_prevented',
    311                                         __( 'Prompt execution was prevented by a filter.' ),
     322                                        $error_message,
    312323                                        array(
    313324                                                'status' => 503,
    … …  
    424435                $camel_case_name = $this->snake_to_camel_case( $name );
    425436
    426                 if ( ! is_callable( array( $this->builder, $camel_case_name ) ) ) {
     437                $method = array( $this->builder, $camel_case_name );
     438                if ( ! is_callable( $method ) ) {
    427439                        throw new BadMethodCallException(
    428440                                sprintf(
    … …  
    435447                }
    436448
    437                 return array( $this->builder, $camel_case_name );
     449                return $method;
    438450        }
    439451
Note: See TracChangeset for help on using the changeset viewer.