Make WordPress Core


Ignore:
Timestamp:
04/30/2026 12:16:22 PM (4 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/class-wp-connector-registry.php

    r62210 r62288  
    4141 *     },
    4242 *     plugin?: array{
    43  *         file: non-empty-string
     43 *         file: non-empty-string,
     44 *         is_active?: callable(): bool
    4445 *     }
    4546 * }
     
    110111         *         Optional. Plugin data for install/activate UI.
    111112         *
    112          *         @type string $file The plugin's main file path relative to the plugins
    113          *                            directory (e.g. 'my-plugin/my-plugin.php' or 'hello.php').
     113         *         @type string   $file      Optional. The plugin's main file path relative to the
     114         *                                   plugins directory (e.g. 'my-plugin/my-plugin.php' or
     115         *                                   'hello.php').
     116         *         @type callable $is_active Optional callback to determine whether the plugin
     117         *                                   is active. Receives no arguments and must return bool.
     118         *                                   Defaults to `__return_true`.
    114119         *     }
    115120         * }
     
    244249                }
    245250
    246                 if ( ! empty( $args['plugin'] ) && is_array( $args['plugin'] ) && ! empty( $args['plugin']['file'] ) ) {
    247                         $connector['plugin'] = array( 'file' => $args['plugin']['file'] );
     251                $connector['plugin'] = array();
     252
     253                if ( ! empty( $args['plugin'] ) && is_array( $args['plugin'] ) ) {
     254                        if ( ! empty( $args['plugin']['file'] ) ) {
     255                                $connector['plugin']['file'] = $args['plugin']['file'];
     256                        }
     257
     258                        if ( isset( $args['plugin']['is_active'] ) ) {
     259                                if ( ! is_callable( $args['plugin']['is_active'] ) ) {
     260                                        _doing_it_wrong(
     261                                                __METHOD__,
     262                                                /* translators: %s: Connector ID. */
     263                                                sprintf( __( 'Connector "%s" plugin is_active must be callable.' ), esc_html( $id ) ),
     264                                                '7.0.0'
     265                                        );
     266                                        return null;
     267                                }
     268
     269                                $connector['plugin']['is_active'] = $args['plugin']['is_active'];
     270                        }
     271                }
     272
     273                if ( ! isset( $connector['plugin']['is_active'] ) ) {
     274                        $connector['plugin']['is_active'] = '__return_true';
    248275                }
    249276
Note: See TracChangeset for help on using the changeset viewer.