Make WordPress Core

Changeset 62192


Ignore:
Timestamp:
04/01/2026 02:56:47 PM (10 hours ago)
Author:
jorgefilipecosta
Message:

Connectors: Replace plugin.slug with plugin.file in connector registration.

Use the plugin's main file path relative to the plugins directory
(e.g. akismet/akismet.php or hello.php) instead of the WordPress.org slug
to identify a connector's associated plugin.
This lets _wp_connectors_get_connector_script_module_data() check plugin
status with file_exists() and is_plugin_active() directly, removing the
get_plugins() slug-to-file mapping that was previously needed.

Props jorgefilipecosta, mukesh27, gziolo.
Fixes #65002.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-connector-registry.php

    r62180 r62192  
    4141 *     },
    4242 *     plugin?: array{
    43  *         slug: non-empty-string
     43 *         file: non-empty-string
    4444 *     }
    4545 * }
     
    110110     *         Optional. Plugin data for install/activate UI.
    111111     *
    112      *         @type string $slug The WordPress.org plugin slug.
     112     *         @type string $file The plugin's main file path relative to the plugins
     113     *                            directory (e.g. 'akismet/akismet.php' or 'hello.php').
    113114     *     }
    114115     * }
     
    243244        }
    244245
    245         if ( ! empty( $args['plugin'] ) && is_array( $args['plugin'] ) ) {
    246             $connector['plugin'] = $args['plugin'];
     246        if ( ! empty( $args['plugin'] ) && is_array( $args['plugin'] ) && ! empty( $args['plugin']['file'] ) ) {
     247            $connector['plugin'] = array( 'file' => $args['plugin']['file'] );
    247248        }
    248249
  • trunk/src/wp-includes/connectors.php

    r62180 r62192  
    5959 *         Optional. Plugin data for install/activate UI.
    6060 *
    61  *         @type string $slug The WordPress.org plugin slug.
     61 *         @type string $file The plugin's main file path relative to the plugins
     62 *                            directory (e.g. 'akismet/akismet.php' or 'hello.php').
    6263 *     }
    6364 * }
     
    7576 *     },
    7677 *     plugin?: array{
    77  *         slug: non-empty-string
     78 *         file: non-empty-string
    7879 *     }
    7980 * }
     
    119120 *             Optional. Plugin data for install/activate UI.
    120121 *
    121  *             @type string $slug The WordPress.org plugin slug.
     122 *             @type string $file The plugin's main file path relative to the plugins
     123 *                                directory (e.g. 'akismet/akismet.php' or 'hello.php').
    122124 *         }
    123125 *     }
     
    136138 *     },
    137139 *     plugin?: array{
    138  *         slug: non-empty-string
     140 *         file: non-empty-string
    139141 *     }
    140142 * }>
     
    257259            'type'           => 'ai_provider',
    258260            'plugin'         => array(
    259                 'slug' => 'ai-provider-for-anthropic',
     261                'file' => 'ai-provider-for-anthropic/plugin.php',
    260262            ),
    261263            'authentication' => array(
     
    269271            'type'           => 'ai_provider',
    270272            'plugin'         => array(
    271                 'slug' => 'ai-provider-for-google',
     273                'file' => 'ai-provider-for-google/plugin.php',
    272274            ),
    273275            'authentication' => array(
     
    281283            'type'           => 'ai_provider',
    282284            'plugin'         => array(
    283                 'slug' => 'ai-provider-for-openai',
     285                'file' => 'ai-provider-for-openai/plugin.php',
    284286            ),
    285287            'authentication' => array(
     
    637639    $registry = AiClient::defaultRegistry();
    638640
    639     // Build a slug-to-file map for plugin installation status.
    640     if ( ! function_exists( 'get_plugins' ) ) {
     641    if ( ! function_exists( 'is_plugin_active' ) ) {
    641642        require_once ABSPATH . 'wp-admin/includes/plugin.php';
    642     }
    643     $plugin_files_by_slug = array();
    644     foreach ( array_keys( get_plugins() ) as $plugin_file ) {
    645         $slug                          = str_contains( $plugin_file, '/' ) ? dirname( $plugin_file ) : str_replace( '.php', '', $plugin_file );
    646         $plugin_files_by_slug[ $slug ] = $plugin_file;
    647643    }
    648644
     
    677673        );
    678674
    679         if ( ! empty( $connector_data['plugin']['slug'] ) ) {
    680             $plugin_slug = $connector_data['plugin']['slug'];
    681             $plugin_file = $plugin_files_by_slug[ $plugin_slug ] ?? null;
    682 
    683             $is_installed = null !== $plugin_file;
    684             $is_activated = $is_installed && is_plugin_active( $plugin_file );
     675        if ( ! empty( $connector_data['plugin']['file'] ) ) {
     676            $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 );
    685679
    686680            $connector_out['plugin'] = array(
    687                 'slug'        => $plugin_slug,
    688                 'pluginFile'  => $is_installed
    689                     ? ( str_ends_with( $plugin_file, '.php' ) ? substr( $plugin_file, 0, -4 ) : $plugin_file )
    690                     : null,
     681                'file'        => $file,
     682                'isInstalled' => $is_installed,
    691683                'isActivated' => $is_activated,
    692684            );
  • trunk/tests/phpunit/tests/connectors/wpConnectorRegistry.php

    r62180 r62192  
    295295    public function test_register_includes_plugin_data() {
    296296        $args           = self::$default_args;
    297         $args['plugin'] = array( 'slug' => 'my-plugin' );
     297        $args['plugin'] = array( 'file' => 'my-plugin/my-plugin.php' );
    298298
    299299        $result = $this->registry->register( 'with-plugin', $args );
    300300
    301301        $this->assertArrayHasKey( 'plugin', $result );
    302         $this->assertSame( array( 'slug' => 'my-plugin' ), $result['plugin'] );
     302        $this->assertSame( array( 'file' => 'my-plugin/my-plugin.php' ), $result['plugin'] );
    303303    }
    304304
Note: See TracChangeset for help on using the changeset viewer.