Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Internal/Utilitie...
File: LegacyRestApiStub.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Automattic\WooCommerce\Internal\Utilities;
[2] Fix | Delete
[3] Fix | Delete
use Automattic\WooCommerce\Internal\RegisterHooksInterface;
[4] Fix | Delete
use Automattic\WooCommerce\Utilities\RestApiUtil;
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* The Legacy REST API was removed in WooCommerce 9.0 and is now available as a dedicated extension.
[8] Fix | Delete
* A stub is kept in WooCommerce core that acts when the extension is not installed and has two purposes:
[9] Fix | Delete
*
[10] Fix | Delete
* 1. Return a "The WooCommerce API is disabled on this site" error for any request to the Legacy REST API endpoints.
[11] Fix | Delete
*
[12] Fix | Delete
* 2. Provide the not-endpoint related utility methods that were previously supplied by the WC_API class,
[13] Fix | Delete
* this is achieved by setting the value of WooCommerce::api (typically accessed via 'WC()->api') to an instance of this class.
[14] Fix | Delete
*
[15] Fix | Delete
* DO NOT add any additional public method to this class unless the method existed with the same signature in the old WC_API class.
[16] Fix | Delete
*
[17] Fix | Delete
* See: https://developer.woocommerce.com/2023/10/03/the-legacy-rest-api-will-move-to-a-dedicated-extension-in-woocommerce-9-0/
[18] Fix | Delete
*/
[19] Fix | Delete
class LegacyRestApiStub implements RegisterHooksInterface {
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* The instance of RestApiUtil to use.
[23] Fix | Delete
*
[24] Fix | Delete
* @var RestApiUtil
[25] Fix | Delete
*/
[26] Fix | Delete
private RestApiUtil $rest_api_util;
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* Set up the Legacy REST API endpoints stub.
[30] Fix | Delete
*/
[31] Fix | Delete
public function register() {
[32] Fix | Delete
add_action( 'init', array( __CLASS__, 'add_rewrite_rules_for_legacy_rest_api_stub' ), 0 );
[33] Fix | Delete
add_action( 'query_vars', array( __CLASS__, 'add_query_vars_for_legacy_rest_api_stub' ), 0 );
[34] Fix | Delete
add_action( 'parse_request', array( __CLASS__, 'parse_legacy_rest_api_request' ), 0 );
[35] Fix | Delete
}
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* Initialize the class dependencies.
[39] Fix | Delete
*
[40] Fix | Delete
* @internal
[41] Fix | Delete
* @param RestApiUtil $rest_api_util The instance of RestApiUtil to use.
[42] Fix | Delete
*/
[43] Fix | Delete
final public function init( RestApiUtil $rest_api_util ) {
[44] Fix | Delete
$this->rest_api_util = $rest_api_util;
[45] Fix | Delete
}
[46] Fix | Delete
[47] Fix | Delete
/**
[48] Fix | Delete
* Add the necessary rewrite rules for the Legacy REST API
[49] Fix | Delete
* (either the dedicated extension if it's installed, or the stub otherwise).
[50] Fix | Delete
*
[51] Fix | Delete
* @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
[52] Fix | Delete
*/
[53] Fix | Delete
public static function add_rewrite_rules_for_legacy_rest_api_stub() {
[54] Fix | Delete
add_rewrite_rule( '^wc-api/v([1-3]{1})/?$', 'index.php?wc-api-version=$matches[1]&wc-api-route=/', 'top' );
[55] Fix | Delete
add_rewrite_rule( '^wc-api/v([1-3]{1})(.*)?', 'index.php?wc-api-version=$matches[1]&wc-api-route=$matches[2]', 'top' );
[56] Fix | Delete
add_rewrite_endpoint( 'wc-api', EP_ALL );
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
/**
[60] Fix | Delete
* Add the necessary request query variables for the Legacy REST API
[61] Fix | Delete
* (either the dedicated extension if it's installed, or the stub otherwise).
[62] Fix | Delete
*
[63] Fix | Delete
* @param array $vars The query variables array to extend.
[64] Fix | Delete
* @return array The extended query variables array.
[65] Fix | Delete
*
[66] Fix | Delete
* @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
[67] Fix | Delete
*/
[68] Fix | Delete
public static function add_query_vars_for_legacy_rest_api_stub( $vars ) {
[69] Fix | Delete
$vars[] = 'wc-api-version';
[70] Fix | Delete
$vars[] = 'wc-api-route';
[71] Fix | Delete
$vars[] = 'wc-api';
[72] Fix | Delete
return $vars;
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
/**
[76] Fix | Delete
* Process an incoming request for the Legacy REST API.
[77] Fix | Delete
*
[78] Fix | Delete
* If the dedicated Legacy REST API extension is installed and active, this method does nothing.
[79] Fix | Delete
* Otherwise it returns a "The WooCommerce API is disabled on this site" error,
[80] Fix | Delete
* unless the request contains a "wc-api" variable and the appropriate
[81] Fix | Delete
* "woocommerce_api_*" hook is set.
[82] Fix | Delete
*
[83] Fix | Delete
* @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
[84] Fix | Delete
*/
[85] Fix | Delete
public static function parse_legacy_rest_api_request() {
[86] Fix | Delete
global $wp;
[87] Fix | Delete
[88] Fix | Delete
// The WC_Legacy_REST_API_Plugin class existence means that the Legacy REST API extension is installed and active.
[89] Fix | Delete
if ( class_exists( 'WC_Legacy_REST_API_Plugin' ) ) {
[90] Fix | Delete
return;
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
self::maybe_process_wc_api_query_var();
[94] Fix | Delete
[95] Fix | Delete
// phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
[96] Fix | Delete
[97] Fix | Delete
if ( ! empty( $_GET['wc-api-version'] ) ) {
[98] Fix | Delete
$wp->query_vars['wc-api-version'] = $_GET['wc-api-version'];
[99] Fix | Delete
}
[100] Fix | Delete
[101] Fix | Delete
if ( ! empty( $_GET['wc-api-route'] ) ) {
[102] Fix | Delete
$wp->query_vars['wc-api-route'] = $_GET['wc-api-route'];
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
if ( ! empty( $wp->query_vars['wc-api-version'] ) && ! empty( $wp->query_vars['wc-api-route'] ) ) {
[106] Fix | Delete
header(
[107] Fix | Delete
sprintf(
[108] Fix | Delete
'Content-Type: %s; charset=%s',
[109] Fix | Delete
isset( $_GET['_jsonp'] ) ? 'application/javascript' : 'application/json',
[110] Fix | Delete
get_option( 'blog_charset' )
[111] Fix | Delete
)
[112] Fix | Delete
);
[113] Fix | Delete
status_header( 404 );
[114] Fix | Delete
echo wp_json_encode(
[115] Fix | Delete
array(
[116] Fix | Delete
'errors' => array(
[117] Fix | Delete
'code' => 'woocommerce_api_disabled',
[118] Fix | Delete
'message' => 'The WooCommerce API is disabled on this site',
[119] Fix | Delete
),
[120] Fix | Delete
)
[121] Fix | Delete
);
[122] Fix | Delete
exit;
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
// phpcs:enable WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
/**
[129] Fix | Delete
* Process a "wc-api" variable if present in the query, by triggering the appropriate hooks.
[130] Fix | Delete
*/
[131] Fix | Delete
private static function maybe_process_wc_api_query_var() {
[132] Fix | Delete
global $wp;
[133] Fix | Delete
[134] Fix | Delete
// phpcs:disable WordPress.Security.NonceVerification.Recommended
[135] Fix | Delete
if ( ! empty( $_GET['wc-api'] ) ) {
[136] Fix | Delete
$wp->query_vars['wc-api'] = sanitize_key( wp_unslash( $_GET['wc-api'] ) );
[137] Fix | Delete
}
[138] Fix | Delete
// phpcs:enable WordPress.Security.NonceVerification.Recommended
[139] Fix | Delete
[140] Fix | Delete
// wc-api endpoint requests.
[141] Fix | Delete
if ( ! empty( $wp->query_vars['wc-api'] ) ) {
[142] Fix | Delete
[143] Fix | Delete
// Buffer, we won't want any output here.
[144] Fix | Delete
ob_start();
[145] Fix | Delete
[146] Fix | Delete
// No cache headers.
[147] Fix | Delete
wc_nocache_headers();
[148] Fix | Delete
[149] Fix | Delete
// Clean the API request.
[150] Fix | Delete
$api_request = strtolower( wc_clean( $wp->query_vars['wc-api'] ) );
[151] Fix | Delete
[152] Fix | Delete
// Make sure gateways are available for request.
[153] Fix | Delete
WC()->payment_gateways();
[154] Fix | Delete
[155] Fix | Delete
// phpcs:disable WooCommerce.Commenting.CommentHooks.HookCommentWrongStyle
[156] Fix | Delete
[157] Fix | Delete
// Trigger generic action before request hook.
[158] Fix | Delete
do_action( 'woocommerce_api_request', $api_request );
[159] Fix | Delete
[160] Fix | Delete
// Is there actually something hooked into this API request? If not trigger 400 - Bad request.
[161] Fix | Delete
status_header( has_action( 'woocommerce_api_' . $api_request ) ? 200 : 400 );
[162] Fix | Delete
[163] Fix | Delete
// Trigger an action which plugins can hook into to fulfill the request.
[164] Fix | Delete
do_action( 'woocommerce_api_' . $api_request );
[165] Fix | Delete
[166] Fix | Delete
// phpcs:enable WooCommerce.Commenting.CommentHooks.HookCommentWrongStyle
[167] Fix | Delete
[168] Fix | Delete
// Done, clear buffer and exit.
[169] Fix | Delete
ob_end_clean();
[170] Fix | Delete
die( '-1' );
[171] Fix | Delete
}
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
/**
[175] Fix | Delete
* Get data from a WooCommerce API endpoint.
[176] Fix | Delete
* This method used to be part of the WooCommerce Legacy REST API.
[177] Fix | Delete
*
[178] Fix | Delete
* @since 9.1.0
[179] Fix | Delete
*
[180] Fix | Delete
* @param string $endpoint Endpoint.
[181] Fix | Delete
* @param array $params Params to pass with request.
[182] Fix | Delete
* @return array|\WP_Error
[183] Fix | Delete
*/
[184] Fix | Delete
public function get_endpoint_data( $endpoint, $params = array() ) {
[185] Fix | Delete
wc_doing_it_wrong(
[186] Fix | Delete
'get_endpoint_data',
[187] Fix | Delete
"'WC()->api->get_endpoint_data' is deprecated, please use the following instead: wc_get_container()->get(Automattic\WooCommerce\Utilities\RestApiUtil::class)->get_endpoint_data",
[188] Fix | Delete
'9.1.0'
[189] Fix | Delete
);
[190] Fix | Delete
[191] Fix | Delete
return $this->rest_api_util->get_endpoint_data( $endpoint, $params );
[192] Fix | Delete
}
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function