Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Internal/AddressP...
File: AbstractAutomatticAddressProvider.php
<?php
[0] Fix | Delete
declare( strict_types=1 );
[1] Fix | Delete
[2] Fix | Delete
namespace Automattic\WooCommerce\Internal\AddressProvider;
[3] Fix | Delete
[4] Fix | Delete
use Automattic\WooCommerce\StoreApi\Utilities\JsonWebToken;
[5] Fix | Delete
use Automattic\Jetpack\Constants;
[6] Fix | Delete
use WC_Address_Provider;
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Abstract Automattic address provider is an abstract implementation of the WC_Address_Provider that is meant to be used by Automattic services to get support for address autocomplete and maps with minimal code maintenance.
[10] Fix | Delete
*
[11] Fix | Delete
* @since 10.1.0
[12] Fix | Delete
* @package WooCommerce
[13] Fix | Delete
*/
[14] Fix | Delete
abstract class AbstractAutomatticAddressProvider extends WC_Address_Provider {
[15] Fix | Delete
[16] Fix | Delete
/**
[17] Fix | Delete
* The JWT for the address service.
[18] Fix | Delete
*
[19] Fix | Delete
* @var string
[20] Fix | Delete
*/
[21] Fix | Delete
private $jwt = null;
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Loads up the JWT for the address service and saves it to transient.
[25] Fix | Delete
*/
[26] Fix | Delete
public function __construct() {
[27] Fix | Delete
add_filter( 'pre_update_option_woocommerce_address_autocomplete_enabled', array( $this, 'refresh_cache' ) );
[28] Fix | Delete
add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) );
[29] Fix | Delete
}
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Get the JWT for the address service, a service should implement an A8C hosted API or some mechanism to get a JWT, this will be passed to frontend code to be used in the address autocomplete and maps.
[33] Fix | Delete
*
[34] Fix | Delete
* This method shouldn't implement any caching, it should only fetch the token or throw an exception, if you must handle caching, consider also overriding get_jwt.
[35] Fix | Delete
*
[36] Fix | Delete
* @return string The JWT for the address service.
[37] Fix | Delete
*/
[38] Fix | Delete
abstract public function get_address_service_jwt();
[39] Fix | Delete
[40] Fix | Delete
/**
[41] Fix | Delete
* Get the telemetry status for the address service, this is meant to be overridden by the implementor to return true if the service has permission to send telemetry data.
[42] Fix | Delete
*
[43] Fix | Delete
* @return bool The telemetry status for the address service.
[44] Fix | Delete
*/
[45] Fix | Delete
public function can_telemetry() {
[46] Fix | Delete
return false;
[47] Fix | Delete
}
[48] Fix | Delete
[49] Fix | Delete
/**
[50] Fix | Delete
* Loads up a JWT from cache or from the implementor side.
[51] Fix | Delete
*
[52] Fix | Delete
* @return void
[53] Fix | Delete
*/
[54] Fix | Delete
public function load_jwt() {
[55] Fix | Delete
[56] Fix | Delete
// If we already have a loaded, valid token, we return early.
[57] Fix | Delete
if ( $this->jwt && is_string( $this->jwt ) && JsonWebToken::shallow_validate( $this->jwt ) ) {
[58] Fix | Delete
return;
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
$cached_jwt = $this->get_cached_option( 'address_autocomplete_jwt' );
[62] Fix | Delete
// If we have a cached, valid token, we load it to class and return early.
[63] Fix | Delete
if ( $cached_jwt && is_string( $cached_jwt ) && JsonWebToken::shallow_validate( $cached_jwt ) ) {
[64] Fix | Delete
$this->jwt = $cached_jwt;
[65] Fix | Delete
return;
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
$retry_data = $this->get_cached_option( 'jwt_retry_data' );
[69] Fix | Delete
[70] Fix | Delete
if ( $retry_data && isset( $retry_data['try_after'] ) && $retry_data['try_after'] > time() ) {
[71] Fix | Delete
return;
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
try {
[75] Fix | Delete
$fresh_jwt = $this->get_address_service_jwt();
[76] Fix | Delete
if ( $fresh_jwt && is_string( $fresh_jwt ) && JsonWebToken::shallow_validate( $fresh_jwt ) ) {
[77] Fix | Delete
$this->set_jwt( $fresh_jwt );
[78] Fix | Delete
// Clear retry data on success.
[79] Fix | Delete
$this->delete_cached_option( 'jwt_retry_data' );
[80] Fix | Delete
return;
[81] Fix | Delete
}
[82] Fix | Delete
} catch ( \Exception $e ) {
[83] Fix | Delete
$retry_data['attempts'] = isset( $retry_data['attempts'] ) ? $retry_data['attempts'] + 1 : 1;
[84] Fix | Delete
wc_get_logger()->error(
[85] Fix | Delete
sprintf(
[86] Fix | Delete
'Failed loading JWT for %1$s address autocomplete service (attempt %2$d) with error %3$s.',
[87] Fix | Delete
$this->name,
[88] Fix | Delete
$retry_data['attempts'],
[89] Fix | Delete
$e->getMessage()
[90] Fix | Delete
),
[91] Fix | Delete
'address-autocomplete'
[92] Fix | Delete
);
[93] Fix | Delete
$backoff_hours = pow( 2, $retry_data['attempts'] - 1 ); // 1, 2, 4, 8 hours.
[94] Fix | Delete
$retry_data['try_after'] = time() + ( $backoff_hours * HOUR_IN_SECONDS );
[95] Fix | Delete
$this->update_cached_option( 'jwt_retry_data', $retry_data, DAY_IN_SECONDS );
[96] Fix | Delete
}
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
/**
[100] Fix | Delete
* Gets the JWT for the address service.
[101] Fix | Delete
*
[102] Fix | Delete
* @return string The JWT for the address service.
[103] Fix | Delete
*/
[104] Fix | Delete
public function get_jwt() {
[105] Fix | Delete
if ( null === $this->jwt ) {
[106] Fix | Delete
$this->load_jwt();
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
return $this->jwt;
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
/**
[113] Fix | Delete
* Sets the JWT for the address service.
[114] Fix | Delete
*
[115] Fix | Delete
* @param string $jwt The JWT for the address service.
[116] Fix | Delete
*/
[117] Fix | Delete
public function set_jwt( $jwt ) {
[118] Fix | Delete
$this->jwt = $jwt;
[119] Fix | Delete
if ( null !== $jwt ) {
[120] Fix | Delete
$cache_duration = $this->get_jwt_cache_duration( $jwt );
[121] Fix | Delete
// If the token is expired, we don't cache it and we fetch a new one.
[122] Fix | Delete
if ( 0 === $cache_duration ) {
[123] Fix | Delete
$this->jwt = null;
[124] Fix | Delete
$this->load_jwt();
[125] Fix | Delete
return;
[126] Fix | Delete
}
[127] Fix | Delete
$this->update_cached_option( 'address_autocomplete_jwt', $jwt, $cache_duration );
[128] Fix | Delete
} else {
[129] Fix | Delete
$this->delete_cached_option( 'address_autocomplete_jwt' );
[130] Fix | Delete
}
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
/**
[134] Fix | Delete
* Gets the cache duration for the JWT.
[135] Fix | Delete
*
[136] Fix | Delete
* @param string $jwt The JWT for the address service.
[137] Fix | Delete
* @return int The cache duration for the JWT.
[138] Fix | Delete
*/
[139] Fix | Delete
public function get_jwt_cache_duration( $jwt ) {
[140] Fix | Delete
$parts = JsonWebToken::get_parts( $jwt );
[141] Fix | Delete
if ( property_exists( $parts->payload, 'exp' ) ) {
[142] Fix | Delete
return max( $parts->payload->exp - time(), 0 );
[143] Fix | Delete
}
[144] Fix | Delete
}
[145] Fix | Delete
[146] Fix | Delete
/**
[147] Fix | Delete
* Deletes the cached token if we disable the autocomplete service or fetches a new one if it's enabled.
[148] Fix | Delete
*
[149] Fix | Delete
* @param string $setting If the service is enabled or disabled.
[150] Fix | Delete
* @return string the setting value.
[151] Fix | Delete
*/
[152] Fix | Delete
public function refresh_cache( $setting ) {
[153] Fix | Delete
if ( wc_string_to_bool( $setting ) ) {
[154] Fix | Delete
$this->load_jwt();
[155] Fix | Delete
} else {
[156] Fix | Delete
$this->set_jwt( null );
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
return $setting;
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
/**
[163] Fix | Delete
* Gets the cached option.
[164] Fix | Delete
*
[165] Fix | Delete
* @param string $key The key of the option.
[166] Fix | Delete
* @return mixed|null The cached option.
[167] Fix | Delete
*/
[168] Fix | Delete
private function get_cached_option( $key ) {
[169] Fix | Delete
$data = get_option( $this->id . '_' . $key );
[170] Fix | Delete
if ( is_array( $data ) && isset( $data['data'] ) ) {
[171] Fix | Delete
if ( ! self::is_expired( $data ) ) {
[172] Fix | Delete
return $data['data'];
[173] Fix | Delete
}
[174] Fix | Delete
$this->delete_cached_option( $key );
[175] Fix | Delete
}
[176] Fix | Delete
return null;
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
/**
[180] Fix | Delete
* Updates the cached option.
[181] Fix | Delete
*
[182] Fix | Delete
* @param string $key The key of the option.
[183] Fix | Delete
* @param mixed $value The value of the option.
[184] Fix | Delete
* @param int $ttl The TTL of the option.
[185] Fix | Delete
*/
[186] Fix | Delete
private function update_cached_option( $key, $value, $ttl = DAY_IN_SECONDS ) {
[187] Fix | Delete
$result = update_option(
[188] Fix | Delete
$this->id . '_' . $key,
[189] Fix | Delete
array(
[190] Fix | Delete
'data' => $value,
[191] Fix | Delete
'updated' => time(),
[192] Fix | Delete
'ttl' => $ttl,
[193] Fix | Delete
),
[194] Fix | Delete
false
[195] Fix | Delete
);
[196] Fix | Delete
if ( false === $result ) {
[197] Fix | Delete
wp_cache_delete( $this->id . '_' . $key, 'options' );
[198] Fix | Delete
}
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
/**
[202] Fix | Delete
* Deletes the cached option.
[203] Fix | Delete
*
[204] Fix | Delete
* @param string $key The key of the option.
[205] Fix | Delete
*/
[206] Fix | Delete
private function delete_cached_option( $key ) {
[207] Fix | Delete
if ( delete_option( $this->id . '_' . $key ) ) {
[208] Fix | Delete
wp_cache_delete( $this->id . '_' . $key, 'options' );
[209] Fix | Delete
}
[210] Fix | Delete
}
[211] Fix | Delete
[212] Fix | Delete
/**
[213] Fix | Delete
* Checks if the cache value is expired.
[214] Fix | Delete
*
[215] Fix | Delete
* @param array $cache_contents The cache contents.
[216] Fix | Delete
*
[217] Fix | Delete
* @return boolean True if the contents are expired. False otherwise.
[218] Fix | Delete
*/
[219] Fix | Delete
private static function is_expired( $cache_contents ) {
[220] Fix | Delete
if ( ! is_array( $cache_contents ) || ! isset( $cache_contents['updated'] ) || ! isset( $cache_contents['ttl'] ) ) {
[221] Fix | Delete
// Treat bad/invalid cache contents as expired.
[222] Fix | Delete
return true;
[223] Fix | Delete
}
[224] Fix | Delete
[225] Fix | Delete
// Double-check that we have integers for `updated` and `ttl`.
[226] Fix | Delete
if ( ! is_int( $cache_contents['updated'] ) || ! is_int( $cache_contents['ttl'] ) ) {
[227] Fix | Delete
return true;
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
$expires = $cache_contents['updated'] + $cache_contents['ttl'];
[231] Fix | Delete
$now = time();
[232] Fix | Delete
return $expires < $now;
[233] Fix | Delete
}
[234] Fix | Delete
[235] Fix | Delete
/**
[236] Fix | Delete
* Return asset URL, copied from WC_Frontend_Scripts::get_asset_url.
[237] Fix | Delete
*
[238] Fix | Delete
* @param string $path Assets path.
[239] Fix | Delete
* @return string
[240] Fix | Delete
*/
[241] Fix | Delete
public static function get_asset_url( $path ) {
[242] Fix | Delete
/**
[243] Fix | Delete
* Filters the asset URL.
[244] Fix | Delete
*
[245] Fix | Delete
* @since 3.2.0
[246] Fix | Delete
*
[247] Fix | Delete
* @param string $url The asset URL.
[248] Fix | Delete
* @param string $path The asset path.
[249] Fix | Delete
* @return string The filtered asset URL.
[250] Fix | Delete
*/
[251] Fix | Delete
return apply_filters( 'woocommerce_get_asset_url', plugins_url( $path, Constants::get_constant( 'WC_PLUGIN_FILE' ) ), $path );
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
[255] Fix | Delete
/**
[256] Fix | Delete
* Enqueues the checkout script, checks if it's already registered or not so we don't duplicate, and prints out the JWT to the page to be consumed.
[257] Fix | Delete
*/
[258] Fix | Delete
public function load_scripts() {
[259] Fix | Delete
if ( ! $this->get_jwt() ) {
[260] Fix | Delete
return;
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
$suffix = Constants::is_true( 'SCRIPT_DEBUG' ) ? '' : '.min';
[264] Fix | Delete
$version = Constants::get_constant( 'WC_VERSION' );
[265] Fix | Delete
[266] Fix | Delete
if ( ! wp_script_is( 'a8c-address-autocomplete-service', 'registered' ) ) {
[267] Fix | Delete
wp_register_script( 'a8c-address-autocomplete-service', self::get_asset_url( 'assets/js/frontend/a8c-address-autocomplete-service' . $suffix . '.js' ), array( 'wc-address-autocomplete' ), $version, array( 'strategy' => 'defer' ) );
[268] Fix | Delete
}
[269] Fix | Delete
[270] Fix | Delete
if ( ! wp_script_is( 'a8c-address-autocomplete-service', 'enqueued' ) ) {
[271] Fix | Delete
wp_enqueue_script( 'a8c-address-autocomplete-service' );
[272] Fix | Delete
}
[273] Fix | Delete
[274] Fix | Delete
wp_add_inline_script(
[275] Fix | Delete
'a8c-address-autocomplete-service',
[276] Fix | Delete
sprintf(
[277] Fix | Delete
'var a8cAddressAutocompleteServiceKeys = a8cAddressAutocompleteServiceKeys || {}; a8cAddressAutocompleteServiceKeys[ %1$s ] = { key: %2$s, canTelemetry: %3$s };',
[278] Fix | Delete
wp_json_encode( $this->id ),
[279] Fix | Delete
wp_json_encode( $this->get_jwt() ),
[280] Fix | Delete
wp_json_encode( false !== $this->can_telemetry() && (bool) $this->can_telemetry() )
[281] Fix | Delete
),
[282] Fix | Delete
'before'
[283] Fix | Delete
);
[284] Fix | Delete
}
[285] Fix | Delete
}
[286] Fix | Delete
[287] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function