Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove jetpack dependency #2439

Merged
merged 94 commits into from
Oct 27, 2023

Conversation

frosso
Copy link
Contributor

@frosso frosso commented Jun 30, 2021

Description

Replace the Jetpack plugin dependency with usage of the Jetpack Connection package.

Related issue(s)

Steps to reproduce & screenshots/GIFs

  • (optional) connect to your sandbox & apply D63626-code to see a modified jetpack DNA connection UI (rather than the default one) - this also ensures that the JP connection doesn't land on a 404 page upon successfull connection

Before:
Screen Shot 2021-07-01 at 4 10 19 PM

After:
Screen Shot 2021-07-01 at 4 41 09 PM

  • Ensure you run composer install
  • You can demo this locally or on JN by running npm run build and uploading the package to your JN installation
  • Tested scenarios:
    • Site with older version of Jetpack, then this is installed
    • Site with older WCS & Jetpack connected, then upgrade to this => no need to connect
    • Site with older WCS & Jetpack connected, then upgrade to this, then uninstall Jetpack => merchant is asked to connect site & plugin to Jetpack PLEASE NOTE: you'll need D63626-code
    • Site with older WCS & Jetpack connected, then upgrade to this, then disconnect & uninstall Jetpack => merchant is asked to connect site to Jetpack PLEASE NOTE: you'll need D63626-code
    • Brand new site with WCPay installed and connected, then merchant installs this => no need to connect to Jetpack
    • Brand new site with this installed and connected, then WCPay is installed => merchant skips Jetpack connection, goes to Stripe onboarding
    • Brand new site without Jetpack, then install this => merchant is asked to connect site to Jetpack
  • Untested scenarios:
    • Brand new site, merchant installs WC and on onboarding also opt-in to install WCS&T
    • Atomic sites (shouldn't be an issue with the connection, the atomic site checks are just used to send data to the connect server)

Checklist

  • unit tests
  • changelog.txt entry added

@@ -466,7 +466,7 @@ protected function request_body( $initial_body = array() ) {
'dimension_unit' => strtolower( get_option( 'woocommerce_dimension_unit' ) ),
'weight_unit' => strtolower( get_option( 'woocommerce_weight_unit' ) ),
'wcs_version' => WC_Connect_Loader::get_wcs_version(),
'jetpack_version' => JETPACK__VERSION,
'jetpack_version' => defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : 'embed',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like we're doing anything on the server with this string. However, the server is still requiring it in the payload.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was only ever used for troubleshooting. If it's easy to get the version of the jetpack-connection package I would include it, otherwise embed is fine.

Copy link
Contributor

@waclawjacek waclawjacek Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, just checked and we are not using it anywhere aside from Tracks calls. We require it to be a string if it is present.

I've changed this from embed to embed-{jetpack_connection_package_version}.

@@ -529,7 +529,7 @@ protected function authorization_header() {
}

list( $token_key, $token_secret ) = explode( '.', $token->secret );
$token_key = sprintf( '%s:%d:%d', $token_key, JETPACK__API_VERSION, $token->external_user_id );
$token_key = sprintf( '%s:%d:%d', $token_key, defined( 'JETPACK__API_VERSION' ) ? JETPACK__API_VERSION : 'embed', $token->external_user_id );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The server seems ok with this 🤷

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The format is %s:%d:%d so the second and third placeholders should be numbers (i.e. can't be "embed"). Testing on my local, it looks like the code below returns 1 anyway so I suggest we hardcode it to that, and in the future use Jetpack's header building function instead of our own, if possible.

Constants::get_constant( 'JETPACK__API_VERSION' )

@@ -28,7 +28,7 @@ function woocommerce_debug_tools( $tools ) {
function test_connection() {
$test_request = $this->api_client->auth_test();
if ( $test_request && ! is_wp_error( $test_request ) && $test_request->authorized ) {
echo '<div class="updated inline"><p>' . __( 'Your site is succesfully communicating to the WooCommerce Shipping & Tax API.', 'woocommerce-services' ) . '</p></div>';
echo '<div class="updated inline"><p>' . __( 'Your site is successfully communicating to the WooCommerce Shipping & Tax API.', 'woocommerce-services' ) . '</p></div>';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -102,14 +85,19 @@ protected function get_health_items() {
'state' => 'warning',
'message' => __( 'This is a Jetpack staging site', 'woocommerce-services' ),
);
} else {
} elseif ( defined( 'JETPACK__VERSION' ) ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this really matter? IDK

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to say no.

@@ -708,7 +722,6 @@ public function load_dependencies() {
$shipping_label = new WC_Connect_Shipping_Label( $api_client, $settings_store, $schemas_store, $payment_methods_store );
$nux = new WC_Connect_Nux( $tracks, $shipping_label );
$taxjar = new WC_Connect_TaxJar_Integration( $api_client, $taxes_logger, $this->wc_connect_base_url );
$options = new WC_Connect_Options();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no constructor in this class

define( 'WOOCOMMERCE_CONNECT_MAX_JSON_DECODE_DEPTH', 32 );

if ( ! defined( 'WOOCOMMERCE_CONNECT_SERVER_API_VERSION ' ) ) {
define( 'WOOCOMMERCE_CONNECT_SERVER_API_VERSION', '5' );
}

// Check for CI environment variable to trigger test mode.
if ( false !== getenv( 'WOOCOMMERCE_SERVICES_CI_TEST_MODE' ) ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When WOOCOMMERCE_SERVICES_CI_TEST_MODE is defined and !false, the other constants are already defined as true - this simplifies things a bit

require_once __DIR__ . '/classes/class-wc-connect-extension-compatibility.php';
require_once __DIR__ . '/classes/class-wc-connect-functions.php';
require_once __DIR__ . '/classes/class-wc-connect-jetpack.php';
require_once __DIR__ . '/classes/class-wc-connect-options.php';

if ( ! class_exists( 'WC_Connect_Loader' ) ) {
define( 'WOOCOMMERCE_CONNECT_MINIMUM_WOOCOMMERCE_VERSION', '2.6' );
define( 'WOOCOMMERCE_CONNECT_MINIMUM_JETPACK_VERSION', '7.5' );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed anymore

@@ -15,6 +15,7 @@ const dirsToCopy = [
'dist',
'i18n',
'images',
'vendor',
Copy link
Contributor Author

@frosso frosso Jun 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used to release - maybe we could just include the non-dev packages in the release?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely. The dev packages add a lot of bloat. Here's how we do it in WCPay, we make sure that the task that's run when we're preparing the release executes composer install --no-dev.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 573a363

composer.json Outdated
@@ -13,5 +13,10 @@
"phpcbf": [
"phpcbf -p"
]
},
"require": {
"automattic/jetpack-connection": "1.20.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not the latest versions.
The latest version of the JP packages has minimum compatibility with WP 5.5 - but we're still testing for 5.4

Automattic/jetpack#17820

public function __construct() {
$this->wc_connect_base_url = self::get_wc_connect_base_url();
add_action( 'plugins_loaded', array( $this, 'jetpack_on_plugins_loaded' ), 1 );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's important that this code is executed with priority 1 - I banged my head for a while trying to figure out why it wasn't working before 😄

Copy link
Contributor

@waclawjacek waclawjacek May 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -66,10 +66,6 @@ public function saved_service_settings( $service_id ) {
}

public function record_user_event( $event_type, $data = array() ) {
if ( ! function_exists( 'jetpack_tracks_record_event' ) && ! class_exists( 'Automattic\\Jetpack\\Tracking' ) ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used anymore - Automattic\Jetpack\Tracking will always be defined

@@ -13,9 +13,16 @@ jobs:
- uses: actions/checkout@v2
with:
submodules: recursive
- uses: shivammathur/setup-php@v2
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plugin now has composer dependencies for releasing - we need them when executing the tests

$is_connected = WC_Connect_Jetpack::is_active() || WC_Connect_Jetpack::is_development_mode();
if ( ! is_plugin_active( 'jetpack/jetpack.php' ) ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for all this branching logic anymore - checking whether jetpack is installed or not won't matter, we are now using the composer packages directly.

*/
public function ajax_activate_jetpack() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to activate jetpack anymore

@frosso frosso marked this pull request as ready for review July 2, 2021 14:45
changelog.txt Outdated Show resolved Hide resolved
@@ -17,20 +17,6 @@ class WC_Connect_API_Client_Live extends WC_Connect_API_Client {
protected function request( $method, $path, $body = array() ) {

// TODO - incorporate caching for repeated identical requests
if ( ! class_exists( '\Automattic\Jetpack\Connection\Manager' ) && ! class_exists( '\Automattic\Jetpack\Connection\Tokens' ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These classes are always present because we bundle Jetpack Connection with WCS&T now.

*
* @return bool
*/
public function can_accept_tos() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the logic now boils down to "is offline mode or is current user connected", I am inlining this logic in set_up_nux_notices() instead.

Copy link
Contributor

@harriswong harriswong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some questions but LGTM otherwise.

I tested buying and printing a USPS label, works.

I tested 2 nux flows by hacking around this condition:


			$banner_to_display = self::get_banner_type_to_display(
				array(
					'jetpack_connection_status'       => $jetpack_install_status,
					'tos_accepted'                    => WC_Connect_Options::get_option( 'tos_accepted' ),
					'can_accept_tos'                  => WC_Connect_Jetpack::is_current_user_connection_owner() || WC_Connect_Jetpack::is_offline_mode(),
					'should_display_after_cxn_banner' => WC_Connect_Options::get_option( self::SHOULD_SHOW_AFTER_CXN_BANNER ),
				)
			);

The ToS one looks good and I traced the code a bit, looks good to me.
image

The second nux flow gave me an error:
image
image

client/banner.js Show resolved Hide resolved
classes/class-wc-connect-tracks.php Show resolved Hide resolved
@waclawjacek
Copy link
Contributor

@harriswong Thank you so much for the review! It's hard to say why exactly you got the error. Is there any chance of getting a list of steps to reproduce to achieve that result? It might be something not caused by this PR.

@harriswong
Copy link
Contributor

harriswong commented Oct 24, 2023

I think it could be me setting the following:

array(
					'jetpack_connection_status'       => 'not-connected',
					'tos_accepted'                    => false, //WC_Connect_Options::get_option( 'tos_accepted' ),
					'can_accept_tos'                  => false, //WC_Connect_Jetpack::is_current_user_connection_owner() || WC_Connect_Jetpack::is_offline_mode(),
					'should_display_after_cxn_banner' => WC_Connect_Options::get_option( self::SHOULD_SHOW_AFTER_CXN_BANNER ),
				)

We can upload this plugin to a brand new site, and if that is fine, then we should be good! Have you tested that already? If so, I think we are good.

@harriswong
Copy link
Contributor

harriswong commented Oct 24, 2023

👋 @waclawjacek For sift, I added these 2 lines:
https://github.com/Automattic/woocommerce-services/blob/trunk/woocommerce-services.php#L1605
https://github.com/Automattic/woocommerce-services/blob/trunk/woocommerce-services.php#L1610

I think we will have to rename these. My recent changelog also conflicted with your PR. Let me know if you want me to fix these! Thanks.

@waclawjacek
Copy link
Contributor

waclawjacek commented Oct 25, 2023

👋 @waclawjacek For sift, I added these 2 lines: trunk/woocommerce-services.php#L1605 trunk/woocommerce-services.php#L1610

I think we will have to rename these. My recent changelog also conflicted with your PR. Let me know if you want me to fix these! Thanks.

Thank you! Great catch. I've just fixed these.

I think it could be me setting the following:

array(
					'jetpack_connection_status'       => 'not-connected',
					'tos_accepted'                    => false, //WC_Connect_Options::get_option( 'tos_accepted' ),
					'can_accept_tos'                  => false, //WC_Connect_Jetpack::is_current_user_connection_owner() || WC_Connect_Jetpack::is_offline_mode(),
					'should_display_after_cxn_banner' => WC_Connect_Options::get_option( self::SHOULD_SHOW_AFTER_CXN_BANNER ),
				)

I don't think that's it. Here's the line of code reporting that error that I found in OpenGrok: fbhepr%2Skers%2Sjcpbz%2Sjc%2Qpbagrag%2Szh%2Qcyhtvaf%2Swrgcnpx%2Spynff.wrgcnpx%2Qfreire%2Qirefvba.cuc%3Se%3Qp86809os%26zb%3Q43279%26sv%3Q1292%231292-og I've just tried hardcoding the same arguments that you tried locally and I was able to connect my site (with the exception of having to remove this later to get the NUX banner to disappear :)). Could you please retry this on a fresh site?

We can upload this plugin to a brand new site, and if that is fine, then we should be good! Have you tested that already? If so, I think we are good.

Yep! In so many test cases. 😄 Also just tested now. I'm attaching a build of the latest commit in this PR if you'd like to test on JN:

woocommerce-services-702256c7de757357fd36b93c040406a383502e69.zip

@harriswong
Copy link
Contributor

harriswong commented Oct 25, 2023

Tested on JN and there is no error, woot!

I have one question. After clicking the image
image

It bounces me to Jetpack page, is this expected? I was thinking the authentication should just connects the site but not bring to this page.
image

@waclawjacek
Copy link
Contributor

Thanks for checking! Did you apply the sandbox patch mentioned in the original post? Without it, you go through the old auth flows instead.

@waclawjacek
Copy link
Contributor

We've just merged the WPCOM changes so you can now test without sandboxing. Could you please give it a try?

@harriswong
Copy link
Contributor

harriswong commented Oct 26, 2023

Hi @waclawjacek, I gave this another test
🧪 With staging
✅ print USPS label + refund using staging, reprinting
✅ international shipping DHL + refund. Reprinting + reprinting custom form
✅ First onboarding nux flow
✅ Saving settings, credit card updates
✅ Refreshing connect server statuses

🧪 Without staging, using a new build with the latest commit(1d90a66)
✅ Installing WCS&T
✅ Activating and the NUX onboarding
✅ Tested creating an order, all the way to printing a label but without actually buying it with a real credit card.

🧪 I didn't test a fresh local dev setup env.

🧪 Tested locally
✅ Pulled this branch, npm run start still works with Jetpack installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Spike: Replace Jetpack requirement with Jetpack DNA
5 participants