Make WordPress Core


Ignore:
Timestamp:
04/30/2026 12:16:22 PM (3 months ago)
Author:
jorgefilipecosta
Message:

Connectors: Add is_active callback support to plugin registration.

Adds an optional is_active callable to the plugin definition accepted by
WP_Connector_Registry::register(). The callback receives no arguments, must
return a boolean, and is used by the Connectors screen to decide whether a
connector's backing plugin is currently active. When omitted, it defaults to
__return_true; when provided but not callable, registration fails with a
_doing_it_wrong() notice.

Developed in: https://github.com/WordPress/wordpress-develop/pull/11565

Props iamadisingh, jorgefilipecosta, mukesh27, gziolo.
Fixes #65020.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/connectors.php

    r62210 r62288  
    368368                        }
    369369                }
     370
     371                if ( ! isset( $args['plugin']['is_active'] ) ) {
     372                        $args['plugin']['is_active'] = static function () use ( $ai_registry, $id ): bool {
     373                                try {
     374                                        return $ai_registry->hasProvider( $id );
     375                                } catch ( Exception $e ) {
     376                                        return false;
     377                                }
     378                        };
     379                }
     380
    370381                $registry->register( $id, $args );
    371382        }
     
    639650        $registry = AiClient::defaultRegistry();
    640651
    641         if ( ! function_exists( 'is_plugin_active' ) ) {
    642                 require_once ABSPATH . 'wp-admin/includes/plugin.php';
    643         }
    644 
    645652        $connectors = array();
    646653        foreach ( wp_get_connectors() as $connector_id => $connector_data ) {
     
    675682                if ( ! empty( $connector_data['plugin']['file'] ) ) {
    676683                        $file         = $connector_data['plugin']['file'];
    677                         $is_installed = file_exists( wp_normalize_path( WP_PLUGIN_DIR . '/' . $file ) );
    678                         $is_activated = $is_installed && is_plugin_active( $file );
     684                        $is_activated = (bool) call_user_func( $connector_data['plugin']['is_active'] );
     685                        $is_installed = $is_activated || file_exists( wp_normalize_path( WP_PLUGIN_DIR . '/' . $file ) );
    679686
    680687                        $connector_out['plugin'] = array(
Note: See TracChangeset for help on using the changeset viewer.