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/tests/phpunit/tests/connectors/wpConnectorRegistry.php

    r62192 r62288  
    300300
    301301                $this->assertArrayHasKey( 'plugin', $result );
    302                 $this->assertSame( array( 'file' => 'my-plugin/my-plugin.php' ), $result['plugin'] );
    303         }
    304 
    305         /**
    306          * @ticket 64791
    307          */
    308         public function test_register_omits_plugin_when_not_provided() {
     302                $this->assertSame( 'my-plugin/my-plugin.php', $result['plugin']['file'] );
     303        }
     304
     305        /**
     306         * @ticket 65020
     307         */
     308        public function test_register_stores_plugin_is_active_callback() {
     309                $args           = self::$default_args;
     310                $args['plugin'] = array(
     311                        'file'      => 'my-plugin/my-plugin.php',
     312                        'is_active' => '__return_true',
     313                );
     314
     315                $result = $this->registry->register( 'with-callback', $args );
     316
     317                $this->assertIsArray( $result );
     318                $this->assertArrayHasKey( 'is_active', $result['plugin'] );
     319                $this->assertIsCallable( $result['plugin']['is_active'] );
     320        }
     321
     322        /**
     323         * @ticket 65020
     324         */
     325        public function test_register_rejects_non_callable_plugin_is_active() {
     326                $this->setExpectedIncorrectUsage( 'WP_Connector_Registry::register' );
     327
     328                $args           = self::$default_args;
     329                $args['plugin'] = array(
     330                        'file'      => 'my-plugin/my-plugin.php',
     331                        'is_active' => 'not_a_real_function_name',
     332                );
     333
     334                $result = $this->registry->register( 'bad-callback', $args );
     335
     336                $this->assertNull( $result );
     337        }
     338
     339        /**
     340         * @ticket 65020
     341         */
     342        public function test_register_defaults_plugin_is_active_to_return_true() {
     343                $args           = self::$default_args;
     344                $args['plugin'] = array( 'file' => 'my-plugin/my-plugin.php' );
     345
     346                $result = $this->registry->register( 'default-callback', $args );
     347
     348                $this->assertIsArray( $result );
     349                $this->assertArrayHasKey( 'is_active', $result['plugin'] );
     350                $this->assertSame( '__return_true', $result['plugin']['is_active'] );
     351        }
     352
     353        /**
     354         * @ticket 64791
     355         */
     356        public function test_register_defaults_plugin_when_not_provided() {
    309357                $result = $this->registry->register( 'no-plugin', self::$default_args );
    310358
    311                 $this->assertArrayNotHasKey( 'plugin', $result );
     359                $this->assertArrayHasKey( 'plugin', $result );
     360                $this->assertArrayNotHasKey( 'file', $result['plugin'] );
     361                $this->assertSame( '__return_true', $result['plugin']['is_active'] );
    312362        }
    313363
Note: See TracChangeset for help on using the changeset viewer.