Skip to content

Commit

Permalink
Install and activate a list of plugins during migrations (#2745)
Browse files Browse the repository at this point in the history
* Add @wordpress/api-fetch package

* Created a place holder button to test plugin activation

* Update shrinkwrap since we installed api-fetch

* Use hello-dolly to test install and activate

* Use native fetch instead of apiFetch

* Remove apiFetch package

* Fix < /p> to </p>

* Bind activation and installation to the update button

* Deactivate WCS&T after installing Woo Shipping and Woo Tax

* Disable is_eligible_for_migration

Don't enable migration yet.

* Redirect to plugins after installation

* Fix deactivateWCSTPluginAPICall to return promise

* Use relative path for the actions in feature-announcement

* Add adminPluginPath in the js global config.

* Execute each task in steps and throw exception if any fails
  • Loading branch information
harriswong authored and Ferdev committed Jul 30, 2024
1 parent b599c65 commit f76a315
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
2 changes: 1 addition & 1 deletion classes/class-wc-connect-service-settings-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ public function get_package_lookup() {

return $lookup;
}

public function is_eligible_for_migration() {
return false;
}
Expand Down
62 changes: 59 additions & 3 deletions client/components/migration/feature-announcement.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useState } from '@wordpress/element';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { localize } from 'i18n-calypso';
import { getNonce, getBaseURL } from 'api/request'; // client/api/request.js

/**
* Internal dependencies
Expand All @@ -30,9 +31,64 @@ const FeatureAnnouncement = ({ translate, isEligable }) => {
};

const update = () => {
// Todo: implement update
setIsUpdating(true);
setTimeout(() => setIsUpdating(false), 2000);
const plugins = 'woocommerce-shipping,woocommerce-tax'; //this needs to be a CSV string.
const headers = {
"Content-Type": "application/json",
'X-WP-Nonce': getNonce()
};

const installPluginAPICall = () =>
fetch( getBaseURL() + 'wc-admin/plugins/install', {
method: 'POST',
headers,
body: JSON.stringify({
plugins
})
} );

const activatePluginAPICall = () =>
fetch( getBaseURL() + 'wc-admin/plugins/activate', {
method: 'POST',
headers,
body: JSON.stringify({
plugins
})
} );

const deactivateWCSTPluginAPICall = () =>
fetch( getBaseURL() + 'wp/v2/plugins/woocommerce-services/woocommerce-services', {
method: 'POST',
headers,
body: JSON.stringify({
status: 'inactive'
})
} );


const installAndActivatePlugins = async() => {
const tasks = [installPluginAPICall, activatePluginAPICall, deactivateWCSTPluginAPICall];

try {
setIsUpdating(true);

for (const task of tasks) {
const apiResponse = await task();
const apiJSONResponse = await apiResponse.json();
if (task.status >= 400) {
throw new Error(apiJSONResponse.message || translate("Failed to setup WooCommerce Shipping. Please try again."));
}
}

window.location = global.wcsPluginData.adminPluginPath;
} catch (e) {
//TODO: error handling.
// console.log('Failed to install or activate.', e);
} finally {
setIsUpdating(false);
}
};

installAndActivatePlugins();
};

return <>{isOpen && (<Modal
Expand Down
1 change: 1 addition & 0 deletions woocommerce-services.php
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,7 @@ public function admin_enqueue_scripts() {
'wcsPluginData',
array(
'assetPath' => self::get_wc_connect_base_url(),
'adminPluginPath' => admin_url('plugins.php'),
)
);
}
Expand Down

0 comments on commit f76a315

Please sign in to comment.