Automattic / jetpack

Security, performance, marketing, and design tools — Jetpack is made by WordPress experts to make WP sites safer and faster, and help you grow your traffic.
https://jetpack.com/
Other
1.59k stars 798 forks source link

Allow specifying `plugin_slug` in `ConnectButton` component and `useConnection` hook #40011

Open ecgan opened 1 month ago

ecgan commented 1 month ago

I would like to use ConnectButton component and useConnection hook from https://www.npmjs.com/package/@automattic/jetpack-connection in my own custom WooCommerce plugin.

However, when I use it, during the registerSite process, I noticed that the plugin_slug / consumer_plug is set to 'connection_package', and there isn't a way for me to override it:

https://github.com/Automattic/jetpack/blob/13222c64b1c391f2a4f9d119773a70c47bb7b2f2/projects/js-packages/api/index.jsx#L92-L94

https://github.com/Automattic/jetpack/blob/13222c64b1c391f2a4f9d119773a70c47bb7b2f2/projects/packages/connection/webpack.config.js#L59

I looked into https://www.npmjs.com/package/@automattic/jetpack-config, and I've put the following webpack config in my plugin, but it doesn't work:

    externals: {
        jetpackConfig: JSON.stringify( {
            consumer_slug: 'my-plugin-slug',
        } ),
    }

Looking at the source code for jetpackConfigHas and jetpackConfigGet, it seems that it would only work for projects / packages within the Jetpack monorepo, and wouldn't work for plugins outside of the monorepo.

So, my suggestion in this issue is to expose plugin_slug in the ConnectButton component and useConnection hook, so that third party plugins can specify their own plugin_slug.

anomiex commented 4 weeks ago

Looking at the source code for jetpackConfigHas and jetpackConfigGet, it seems that it would only work for projects / packages within the Jetpack monorepo, and wouldn't work for plugins outside of the monorepo.

It's not a inside/outside monorepo issue, it's that since #38877 it can't be set anywhere. More specifically, the jetpackConfig stuff is scoped to the Webpack bundle, and since #38877 everything should be using the one bundle from the Connection PHP package rather than including the Connection-JS code inside their own bundles.

At the time everyone we asked (e.g. p1724180304428379/1723632909.832209-slack-C05PV073SG3) said that the consumer_slug isn't actually recorded or used for anything. If that remains true, maybe we should remove it entirely? Or if we want to keep it, then yeah, it would need to be passed in to the appropriate components/hooks.