From 442cb4e81b4c83c76f188ab54e1d492186180719 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Tue, 24 Sep 2024 19:37:57 +0800 Subject: [PATCH] [Beta tester] Add tool for changing woocommerce.com base URL (#51537) * Refactor WooCommerce Helper class This commit refactors the WooCommerce Helper class in the `class-wc-helper.php` file. The changes include: - Updating the comparison operator from `==` to `===` in the `activate_plugin` condition. - Renaming the `get_install_base_url` method to `get_woocommerce_com_base_url`. - Adding a new `get_install_base_url` method that returns the base URL for the plugin auto installer. These changes improve the readability and maintainability of the code. * Refactor data source poller classes for marketing recommendations, payment gateway suggestions, shipping partner suggestions, inbox notifications, remote free extensions, and WooPayments promotion - Refactor the data source poller classes for marketing recommendations, payment gateway suggestions, shipping partner suggestions, inbox notifications, remote free extensions, and WooPayments promotion to use a dynamic base URL for the data sources. - Update the deprecated DATA_SOURCES constant to an empty array in each class. - Add a new method get_data_sources() in each class to return the updated data sources with the dynamic base URL. - Modify the constructor of each class to use the get_data_sources() method instead of the deprecated DATA_SOURCES constant. * Add changelog * Add set-woocom-base-url tool * Add changelog * Rename woo_com and woocom -> wccom * Change to falsy check !url * Change woocom/WOOCOM to wccom/WCCOM * Change default value to https://woocommerce.com/ --------- Co-authored-by: Ilyas Foo --- plugins/woocommerce-beta-tester/api/api.php | 1 + .../api/tools/set-wccom-base-url.php | 102 ++++++++++++++++++ .../dev-beta-tester-wccom-base-url-tool | 4 + .../src/tools/commands/index.js | 9 ++ .../src/tools/commands/set-wccom-base-url.js | 36 +++++++ .../src/tools/data/actions.js | 10 ++ .../src/tools/data/reducer.js | 1 + .../src/tools/data/resolvers.js | 17 +++ .../src/tools/data/selectors.js | 4 + 9 files changed, 184 insertions(+) create mode 100644 plugins/woocommerce-beta-tester/api/tools/set-wccom-base-url.php create mode 100644 plugins/woocommerce-beta-tester/changelog/dev-beta-tester-wccom-base-url-tool create mode 100644 plugins/woocommerce-beta-tester/src/tools/commands/set-wccom-base-url.js diff --git a/plugins/woocommerce-beta-tester/api/api.php b/plugins/woocommerce-beta-tester/api/api.php index 65b1d151aa8..01091b7e92a 100644 --- a/plugins/woocommerce-beta-tester/api/api.php +++ b/plugins/woocommerce-beta-tester/api/api.php @@ -66,3 +66,4 @@ require 'remote-spec-validator/class-wca-test-helper-remote-spec-validator.php'; require 'remote-inbox-notifications/class-wca-test-helper-remote-inbox-notifications.php'; require 'remote-logging/remote-logging.php'; require 'tools/wccom-request-errors.php'; +require 'tools/set-wccom-base-url.php'; diff --git a/plugins/woocommerce-beta-tester/api/tools/set-wccom-base-url.php b/plugins/woocommerce-beta-tester/api/tools/set-wccom-base-url.php new file mode 100644 index 00000000000..06bfb4ef761 --- /dev/null +++ b/plugins/woocommerce-beta-tester/api/tools/set-wccom-base-url.php @@ -0,0 +1,102 @@ + 'POST', + 'args' => array( + 'url' => array( + 'description' => 'WooCommerce.com base URL', + 'type' => 'string', + 'required' => true, + 'sanitize_callback' => 'esc_url_raw', + ), + ), + ) +); + +register_woocommerce_admin_test_helper_rest_route( + '/tools/get-wccom-base-url/v1', + 'tools_get_wccom_base_url', + array( + 'methods' => 'GET', + ) +); + +/** + * Set WooCommerce.com base URL. + * + * @param WP_REST_Request $request Full details about the request. + * @return WP_REST_Response + */ +function tools_set_wccom_base_url( $request ) { + $url = $request->get_param( 'url' ); + + if ( empty( $url ) ) { + delete_option( 'wc_beta_tester_wccom_base_url' ); + return new WP_REST_Response( array( 'message' => 'WooCommerce.com base URL is reset' ), 200 ); + } + + if ( ! filter_var( $url, FILTER_VALIDATE_URL ) ) { + return new WP_REST_Response( array( 'message' => 'Invalid URL' ), 400 ); + } + + update_option( 'wc_beta_tester_wccom_base_url', $url ); + return new WP_REST_Response( $url, 200 ); +} + +/** + * Get WooCommerce.com base URL. + * + * @return WP_REST_Response + */ +function tools_get_wccom_base_url() { + if ( class_exists( 'WC_Helper' ) && method_exists( 'WC_Helper', 'get_woocommerce_com_base_url' ) ) { + $url = WC_Helper::get_woocommerce_com_base_url(); + } else { + $url = get_option( 'wc_beta_tester_wccom_base_url', '' ); + } + return new WP_REST_Response( $url, 200 ); +} + +/** + * Filter WooCommerce.com base URL. + * + * @param string $default_url Default WooCommerce.com base URL. + * @return string + */ +function filter_wccom_base_url( $default_url ) { + $custom_url = get_option( 'wc_beta_tester_wccom_base_url', '' ); + return ! empty( $custom_url ) ? $custom_url : $default_url; +} + +add_filter( 'woo_com_base_url', 'filter_wccom_base_url' ); + +/** + * Filter HTTP request arguments to disable SSL verification for custom WooCommerce.com base URL. + * + * @param array $args HTTP request arguments. + * @param string $url HTTP request URL. + * @return array Modified HTTP request arguments. + */ +function filter_http_request_args_for_custom_wccom_url( $args, $url ) { + $custom_url = get_option( 'wc_beta_tester_wccom_base_url', false ); + if ( $custom_url && strpos( $url, $custom_url ) !== false ) { + // Disable SSL verification for requests to the custom URL since local dev might not have a valid SSL certificate. + $args['sslverify'] = false; + } + return $args; +} + +add_filter( 'http_request_args', 'filter_http_request_args_for_custom_wccom_url', 10, 2 ); diff --git a/plugins/woocommerce-beta-tester/changelog/dev-beta-tester-wccom-base-url-tool b/plugins/woocommerce-beta-tester/changelog/dev-beta-tester-wccom-base-url-tool new file mode 100644 index 00000000000..64f2d280701 --- /dev/null +++ b/plugins/woocommerce-beta-tester/changelog/dev-beta-tester-wccom-base-url-tool @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Add set woocom base url tool diff --git a/plugins/woocommerce-beta-tester/src/tools/commands/index.js b/plugins/woocommerce-beta-tester/src/tools/commands/index.js index 66ba187eefa..55d8b18df6d 100644 --- a/plugins/woocommerce-beta-tester/src/tools/commands/index.js +++ b/plugins/woocommerce-beta-tester/src/tools/commands/index.js @@ -24,6 +24,10 @@ import { UPDATE_WCCOM_REQUEST_ERRORS_MODE, SetWccomRequestErrros, } from './set-wccom-request-errors'; +import { + SetWccomBaseUrl, + UPDATE_WCCOM_BASE_URL_ACTION_NAME, +} from './set-wccom-base-url'; export default [ { @@ -106,4 +110,9 @@ export default [ description: , action: FAKE_WOO_PAYMENTS_ACTION_NAME, }, + { + command: 'Set WooCommerce.com Base URL', + description: , + action: UPDATE_WCCOM_BASE_URL_ACTION_NAME, + }, ]; diff --git a/plugins/woocommerce-beta-tester/src/tools/commands/set-wccom-base-url.js b/plugins/woocommerce-beta-tester/src/tools/commands/set-wccom-base-url.js new file mode 100644 index 00000000000..de24d86596f --- /dev/null +++ b/plugins/woocommerce-beta-tester/src/tools/commands/set-wccom-base-url.js @@ -0,0 +1,36 @@ +/** + * External dependencies + */ +import { TextControl } from '@wordpress/components'; +import { useDispatch, useSelect } from '@wordpress/data'; + +/** + * Internal dependencies + */ +import { STORE_KEY } from '../data/constants'; + +export const UPDATE_WCCOM_BASE_URL_ACTION_NAME = 'updateWccomBaseUrl'; + +export const SetWccomBaseUrl = () => { + const url = useSelect( + ( select ) => select( STORE_KEY ).getWccomBaseUrl(), + [] + ); + const { updateCommandParams } = useDispatch( STORE_KEY ); + + function onUpdate( newUrl ) { + updateCommandParams( UPDATE_WCCOM_BASE_URL_ACTION_NAME, { + url: newUrl, + } ); + } + + return ( +
+ { url === undefined ? ( +

Loading...

+ ) : ( + + ) } +
+ ); +}; diff --git a/plugins/woocommerce-beta-tester/src/tools/data/actions.js b/plugins/woocommerce-beta-tester/src/tools/data/actions.js index e2cc1e67cb4..8f138a3874c 100644 --- a/plugins/woocommerce-beta-tester/src/tools/data/actions.js +++ b/plugins/woocommerce-beta-tester/src/tools/data/actions.js @@ -307,3 +307,13 @@ export function* fakeWooPayments( params ) { ); } ); } + +export function* updateWccomBaseUrl( { url } ) { + yield runCommand( 'Set WooCommerce.com Base URL', function* () { + yield apiFetch( { + path: '/wc-admin-test-helper/tools/set-wccom-base-url/v1', + method: 'POST', + data: { url }, + } ); + } ); +} diff --git a/plugins/woocommerce-beta-tester/src/tools/data/reducer.js b/plugins/woocommerce-beta-tester/src/tools/data/reducer.js index 481561117d8..615007895ea 100644 --- a/plugins/woocommerce-beta-tester/src/tools/data/reducer.js +++ b/plugins/woocommerce-beta-tester/src/tools/data/reducer.js @@ -15,6 +15,7 @@ const DEFAULT_STATE = { runSelectedUpdateCallbacks: {}, updateWccomRequestErrorsMode: {}, fakeWooPayments: {}, + updateWccomBaseUrl: { url: '' }, }, status: '', dbUpdateVersions: [], diff --git a/plugins/woocommerce-beta-tester/src/tools/data/resolvers.js b/plugins/woocommerce-beta-tester/src/tools/data/resolvers.js index 0e839b27760..89c8daefb7b 100644 --- a/plugins/woocommerce-beta-tester/src/tools/data/resolvers.js +++ b/plugins/woocommerce-beta-tester/src/tools/data/resolvers.js @@ -19,6 +19,7 @@ import { UPDATE_COMING_SOON_MODE_ACTION_NAME } from '../commands/set-coming-soon import { TRIGGER_UPDATE_CALLBACKS_ACTION_NAME } from '../commands/trigger-update-callbacks'; import { UPDATE_WCCOM_REQUEST_ERRORS_MODE } from '../commands/set-wccom-request-errors'; import { FAKE_WOO_PAYMENTS_ACTION_NAME } from '../commands/fake-woo-payments'; +import { UPDATE_WCCOM_BASE_URL_ACTION_NAME } from '../commands/set-wccom-base-url'; export function* getCronJobs() { const path = `${ API_NAMESPACE }/tools/get-cron-list/v1`; @@ -150,3 +151,19 @@ export function* getIsFakeWooPaymentsEnabled() { throw new Error( error ); } } + +export function* getWccomBaseUrl() { + const path = `${ API_NAMESPACE }/tools/get-wccom-base-url/v1`; + + try { + const url = yield apiFetch( { + path, + method: 'GET', + } ); + yield updateCommandParams( UPDATE_WCCOM_BASE_URL_ACTION_NAME, { + url: url || 'https://woocommerce.com/', + } ); + } catch ( error ) { + throw new Error( error ); + } +} diff --git a/plugins/woocommerce-beta-tester/src/tools/data/selectors.js b/plugins/woocommerce-beta-tester/src/tools/data/selectors.js index bca03b9bbf9..0170030ceab 100644 --- a/plugins/woocommerce-beta-tester/src/tools/data/selectors.js +++ b/plugins/woocommerce-beta-tester/src/tools/data/selectors.js @@ -45,3 +45,7 @@ export function getWccomRequestErrorsMode( state ) { export function getIsFakeWooPaymentsEnabled( state ) { return state.params.fakeWooPayments.enabled; } + +export function getWccomBaseUrl( state ) { + return state.params.updateWccomBaseUrl.url; +}