Add set-woocom-base-url tool

This commit is contained in:
Chi-Hsuan Huang 2024-09-19 16:26:58 +08:00
parent 4d0beb757c
commit c1b7627228
8 changed files with 180 additions and 0 deletions

View File

@ -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-woocom-base-url.php';

View File

@ -0,0 +1,102 @@
<?php
/**
* WooCommerce Beta Tester WooCommerce.com Base URL
*
* @package WC_Beta_Tester
*/
defined( 'ABSPATH' ) || exit;
/**
* Register REST API routes for setting and getting WooCommerce.com base URL.
*/
register_woocommerce_admin_test_helper_rest_route(
'/tools/set-woocom-base-url/v1',
'tools_set_woocom_base_url',
array(
'methods' => '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-woocom-base-url/v1',
'tools_get_woocom_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_woocom_base_url( $request ) {
$url = $request->get_param( 'url' );
if ( empty( $url ) ) {
delete_option( 'wc_beta_tester_woocom_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_woocom_base_url', $url );
return new WP_REST_Response( $url, 200 );
}
/**
* Get WooCommerce.com base URL.
*
* @return WP_REST_Response
*/
function tools_get_woocom_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_woocom_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_woo_com_base_url( $default_url ) {
$custom_url = get_option( 'wc_beta_tester_woocom_base_url', '' );
return ! empty( $custom_url ) ? $custom_url : $default_url;
}
add_filter( 'woo_com_base_url', 'filter_woo_com_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_woocom_url( $args, $url ) {
$custom_url = get_option( 'wc_beta_tester_woocom_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_woocom_url', 10, 2 );

View File

@ -24,6 +24,10 @@ import {
UPDATE_WCCOM_REQUEST_ERRORS_MODE,
SetWccomRequestErrros,
} from './set-wccom-request-errors';
import {
SetWoocomBaseUrl,
UPDATE_WOOCOM_BASE_URL_ACTION_NAME,
} from './set-woocom-base-url';
export default [
{
@ -106,4 +110,9 @@ export default [
description: <FakeWooPayments />,
action: FAKE_WOO_PAYMENTS_ACTION_NAME,
},
{
command: 'Set WooCommerce.com Base URL',
description: <SetWoocomBaseUrl />,
action: UPDATE_WOOCOM_BASE_URL_ACTION_NAME,
},
];

View File

@ -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_WOOCOM_BASE_URL_ACTION_NAME = 'updateWoocomBaseUrl';
export const SetWoocomBaseUrl = () => {
const url = useSelect(
( select ) => select( STORE_KEY ).getWoocomBaseUrl(),
[]
);
const { updateCommandParams } = useDispatch( STORE_KEY );
function onUpdate( newUrl ) {
updateCommandParams( UPDATE_WOOCOM_BASE_URL_ACTION_NAME, {
url: newUrl,
} );
}
return (
<div className="woocom-base-url-control">
{ url === undefined ? (
<p>Loading...</p>
) : (
<TextControl value={ url } onChange={ onUpdate } />
) }
</div>
);
};

View File

@ -307,3 +307,13 @@ export function* fakeWooPayments( params ) {
);
} );
}
export function* updateWoocomBaseUrl( { url } ) {
yield runCommand( 'Set WooCommerce.com Base URL', function* () {
yield apiFetch( {
path: '/wc-admin-test-helper/tools/set-woocom-base-url/v1',
method: 'POST',
data: { url },
} );
} );
}

View File

@ -15,6 +15,7 @@ const DEFAULT_STATE = {
runSelectedUpdateCallbacks: {},
updateWccomRequestErrorsMode: {},
fakeWooPayments: {},
updateWoocomBaseUrl: { url: '' },
},
status: '',
dbUpdateVersions: [],

View File

@ -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_WOOCOM_BASE_URL_ACTION_NAME } from '../commands/set-woocom-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* getWoocomBaseUrl() {
const path = `${ API_NAMESPACE }/tools/get-woocom-base-url/v1`;
try {
const url = yield apiFetch( {
path,
method: 'GET',
} );
yield updateCommandParams( UPDATE_WOOCOM_BASE_URL_ACTION_NAME, {
url: url || '',
} );
} catch ( error ) {
throw new Error( error );
}
}

View File

@ -45,3 +45,7 @@ export function getWccomRequestErrorsMode( state ) {
export function getIsFakeWooPaymentsEnabled( state ) {
return state.params.fakeWooPayments.enabled;
}
export function getWoocomBaseUrl( state ) {
return state.params.updateWoocomBaseUrl.url;
}