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
// Powered by Google branding.
[31] Fix | Delete
$this->branding_html = 'Powered by&nbsp;<img style="height: 15px; width: 45px; margin-bottom: -2px;" src="' . plugins_url( '/assets/images/address-autocomplete/google.svg', WC_PLUGIN_FILE ) . '" alt="Google logo" />';
[32] Fix | Delete
}
[33] Fix | Delete
[34] Fix | Delete
/**
[35] 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.
[36] Fix | Delete
*
[37] 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.
[38] Fix | Delete
*
[39] Fix | Delete
* @return string The JWT for the address service.
[40] Fix | Delete
*/
[41] Fix | Delete
abstract public function get_address_service_jwt();
[42] Fix | Delete
[43] Fix | Delete
/**
[44] 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.
[45] Fix | Delete
*
[46] Fix | Delete
* @return bool The telemetry status for the address service.
[47] Fix | Delete
*/
[48] Fix | Delete
public function can_telemetry() {
[49] Fix | Delete
return false;
[50] Fix | Delete
}
[51] Fix | Delete
[52] Fix | Delete
/**
[53] Fix | Delete
* Loads up a JWT from cache or from the implementor side.
[54] Fix | Delete
*
[55] Fix | Delete
* @return void
[56] Fix | Delete
*
[57] Fix | Delete
* phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.Missing -- As we wrap the throw in a try/catch.
[58] Fix | Delete
*/
[59] Fix | Delete
public function load_jwt() {
[60] Fix | Delete
[61] Fix | Delete
// If the address autocomplete is disabled, we don't load the JWT.
[62] Fix | Delete
if ( wc_string_to_bool( get_option( 'woocommerce_address_autocomplete_enabled', 'no' ) ) !== true ) {
[63] Fix | Delete
return;
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
// If we already have a loaded, valid token, we return early.
[67] Fix | Delete
if ( $this->jwt && is_string( $this->jwt ) && JsonWebToken::shallow_validate( $this->jwt ) ) {
[68] Fix | Delete
return;
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
$cached_jwt = $this->get_cached_option( 'address_autocomplete_jwt' );
[72] Fix | Delete
// If we have a cached, valid token, we load it to class and return early.
[73] Fix | Delete
if ( $cached_jwt && is_string( $cached_jwt ) && JsonWebToken::shallow_validate( $cached_jwt ) ) {
[74] Fix | Delete
$this->jwt = $cached_jwt;
[75] Fix | Delete
return;
[76] Fix | Delete
}
[77] Fix | Delete
[78] Fix | Delete
$retry_data = $this->get_cached_option( 'jwt_retry_data' );
[79] Fix | Delete
[80] Fix | Delete
if ( $retry_data && isset( $retry_data['try_after'] ) && $retry_data['try_after'] > time() ) {
[81] Fix | Delete
return;
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
try {
[85] Fix | Delete
$fresh_jwt = $this->get_address_service_jwt();
[86] Fix | Delete
if ( $fresh_jwt && is_string( $fresh_jwt ) && JsonWebToken::shallow_validate( $fresh_jwt ) ) {
[87] Fix | Delete
$this->set_jwt( $fresh_jwt );
[88] Fix | Delete
// Clear retry data on success.
[89] Fix | Delete
$this->delete_cached_option( 'jwt_retry_data' );
[90] Fix | Delete
return;
[91] Fix | Delete
} else {
[92] Fix | Delete
throw new \Exception( 'Invalid JWT received from address service.' );
[93] Fix | Delete
}
[94] Fix | Delete
} catch ( \Exception $e ) {
[95] Fix | Delete
$retry_data['attempts'] = isset( $retry_data['attempts'] ) ? $retry_data['attempts'] + 1 : 1;
[96] Fix | Delete
wc_get_logger()->error(
[97] Fix | Delete
sprintf(
[98] Fix | Delete
'Failed loading JWT for %1$s address autocomplete service (attempt %2$d) with error %3$s.',
[99] Fix | Delete
$this->name,
[100] Fix | Delete
$retry_data['attempts'],
[101] Fix | Delete
$e->getMessage()
[102] Fix | Delete
),
[103] Fix | Delete
'address-autocomplete'
[104] Fix | Delete
);
[105] Fix | Delete
$backoff_hours = pow( 2, $retry_data['attempts'] - 1 ); // 1, 2, 4, 8 hours.
[106] Fix | Delete
$retry_data['try_after'] = time() + ( $backoff_hours * HOUR_IN_SECONDS );
[107] Fix | Delete
$this->update_cached_option( 'jwt_retry_data', $retry_data, DAY_IN_SECONDS );
[108] Fix | Delete
}
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
/**
[112] Fix | Delete
* Gets the JWT for the address service.
[113] Fix | Delete
*
[114] Fix | Delete
* @return string The JWT for the address service.
[115] Fix | Delete
*/
[116] Fix | Delete
public function get_jwt() {
[117] Fix | Delete
if ( null === $this->jwt ) {
[118] Fix | Delete
$this->load_jwt();
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
return $this->jwt;
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
/**
[125] Fix | Delete
* Sets the JWT for the address service.
[126] Fix | Delete
*
[127] Fix | Delete
* @param string $jwt The JWT for the address service.
[128] Fix | Delete
*/
[129] Fix | Delete
public function set_jwt( $jwt ) {
[130] Fix | Delete
$this->jwt = $jwt;
[131] Fix | Delete
if ( null !== $jwt ) {
[132] Fix | Delete
$cache_duration = $this->get_jwt_cache_duration( $jwt );
[133] Fix | Delete
// If the token is expired, we don't cache it and we fetch a new one.
[134] Fix | Delete
if ( 0 === $cache_duration ) {
[135] Fix | Delete
$this->jwt = null;
[136] Fix | Delete
$this->load_jwt();
[137] Fix | Delete
return;
[138] Fix | Delete
}
[139] Fix | Delete
$this->update_cached_option( 'address_autocomplete_jwt', $jwt, $cache_duration );
[140] Fix | Delete
} else {
[141] Fix | Delete
$this->delete_cached_option( 'address_autocomplete_jwt' );
[142] Fix | Delete
}
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
/**
[146] Fix | Delete
* Gets the cache duration for the JWT.
[147] Fix | Delete
*
[148] Fix | Delete
* @param string $jwt The JWT for the address service.
[149] Fix | Delete
* @return int The cache duration for the JWT.
[150] Fix | Delete
*/
[151] Fix | Delete
public function get_jwt_cache_duration( $jwt ) {
[152] Fix | Delete
$parts = JsonWebToken::get_parts( $jwt );
[153] Fix | Delete
if ( property_exists( $parts->payload, 'exp' ) ) {
[154] Fix | Delete
return max( $parts->payload->exp - time(), 0 );
[155] Fix | Delete
}
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
/**
[159] Fix | Delete
* Deletes the cached token if we disable the autocomplete service or fetches a new one if it's enabled.
[160] Fix | Delete
*
[161] Fix | Delete
* @param string $setting If the service is enabled or disabled.
[162] Fix | Delete
* @return string the setting value.
[163] Fix | Delete
*/
[164] Fix | Delete
public function refresh_cache( $setting ) {
[165] Fix | Delete
if ( wc_string_to_bool( $setting ) ) {
[166] Fix | Delete
$this->load_jwt();
[167] Fix | Delete
} else {
[168] Fix | Delete
$this->set_jwt( null );
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
return $setting;
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
/**
[175] Fix | Delete
* Gets the cached option.
[176] Fix | Delete
*
[177] Fix | Delete
* @param string $key The key of the option.
[178] Fix | Delete
* @return mixed|null The cached option.
[179] Fix | Delete
*/
[180] Fix | Delete
private function get_cached_option( $key ) {
[181] Fix | Delete
$data = get_option( $this->id . '_' . $key );
[182] Fix | Delete
if ( is_array( $data ) && isset( $data['data'] ) ) {
[183] Fix | Delete
if ( ! self::is_expired( $data ) ) {
[184] Fix | Delete
return $data['data'];
[185] Fix | Delete
}
[186] Fix | Delete
$this->delete_cached_option( $key );
[187] Fix | Delete
}
[188] Fix | Delete
return null;
[189] Fix | Delete
}
[190] Fix | Delete
[191] Fix | Delete
/**
[192] Fix | Delete
* Updates the cached option.
[193] Fix | Delete
*
[194] Fix | Delete
* @param string $key The key of the option.
[195] Fix | Delete
* @param mixed $value The value of the option.
[196] Fix | Delete
* @param int $ttl The TTL of the option.
[197] Fix | Delete
*/
[198] Fix | Delete
private function update_cached_option( $key, $value, $ttl = DAY_IN_SECONDS ) {
[199] Fix | Delete
$result = update_option(
[200] Fix | Delete
$this->id . '_' . $key,
[201] Fix | Delete
array(
[202] Fix | Delete
'data' => $value,
[203] Fix | Delete
'updated' => time(),
[204] Fix | Delete
'ttl' => $ttl,
[205] Fix | Delete
),
[206] Fix | Delete
false
[207] Fix | Delete
);
[208] Fix | Delete
if ( false === $result ) {
[209] Fix | Delete
wp_cache_delete( $this->id . '_' . $key, 'options' );
[210] Fix | Delete
}
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
/**
[214] Fix | Delete
* Deletes the cached option.
[215] Fix | Delete
*
[216] Fix | Delete
* @param string $key The key of the option.
[217] Fix | Delete
*/
[218] Fix | Delete
private function delete_cached_option( $key ) {
[219] Fix | Delete
if ( delete_option( $this->id . '_' . $key ) ) {
[220] Fix | Delete
wp_cache_delete( $this->id . '_' . $key, 'options' );
[221] Fix | Delete
}
[222] Fix | Delete
}
[223] Fix | Delete
[224] Fix | Delete
/**
[225] Fix | Delete
* Checks if the cache value is expired.
[226] Fix | Delete
*
[227] Fix | Delete
* @param array $cache_contents The cache contents.
[228] Fix | Delete
*
[229] Fix | Delete
* @return boolean True if the contents are expired. False otherwise.
[230] Fix | Delete
*/
[231] Fix | Delete
private static function is_expired( $cache_contents ) {
[232] Fix | Delete
if ( ! is_array( $cache_contents ) || ! isset( $cache_contents['updated'] ) || ! isset( $cache_contents['ttl'] ) ) {
[233] Fix | Delete
// Treat bad/invalid cache contents as expired.
[234] Fix | Delete
return true;
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
// Double-check that we have integers for `updated` and `ttl`.
[238] Fix | Delete
if ( ! is_int( $cache_contents['updated'] ) || ! is_int( $cache_contents['ttl'] ) ) {
[239] Fix | Delete
return true;
[240] Fix | Delete
}
[241] Fix | Delete
[242] Fix | Delete
$expires = $cache_contents['updated'] + $cache_contents['ttl'];
[243] Fix | Delete
$now = time();
[244] Fix | Delete
return $expires < $now;
[245] Fix | Delete
}
[246] Fix | Delete
[247] Fix | Delete
/**
[248] Fix | Delete
* Return asset URL, copied from WC_Frontend_Scripts::get_asset_url.
[249] Fix | Delete
*
[250] Fix | Delete
* @param string $path Assets path.
[251] Fix | Delete
* @return string
[252] Fix | Delete
*/
[253] Fix | Delete
public static function get_asset_url( $path ) {
[254] Fix | Delete
/**
[255] Fix | Delete
* Filters the asset URL.
[256] Fix | Delete
*
[257] Fix | Delete
* @since 3.2.0
[258] Fix | Delete
*
[259] Fix | Delete
* @param string $url The asset URL.
[260] Fix | Delete
* @param string $path The asset path.
[261] Fix | Delete
* @return string The filtered asset URL.
[262] Fix | Delete
*/
[263] Fix | Delete
return apply_filters( 'woocommerce_get_asset_url', plugins_url( $path, Constants::get_constant( 'WC_PLUGIN_FILE' ) ), $path );
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
[267] Fix | Delete
/**
[268] 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.
[269] Fix | Delete
*/
[270] Fix | Delete
public function load_scripts() {
[271] Fix | Delete
// If the address autocomplete setting is disabled, don't load the scripts.
[272] Fix | Delete
if ( wc_string_to_bool( get_option( 'woocommerce_address_autocomplete_enabled', 'no' ) ) !== true ) {
[273] Fix | Delete
return;
[274] Fix | Delete
}
[275] Fix | Delete
[276] Fix | Delete
if ( ! is_checkout() ) {
[277] Fix | Delete
return;
[278] Fix | Delete
}
[279] Fix | Delete
[280] Fix | Delete
if ( ! $this->get_jwt() ) {
[281] Fix | Delete
return;
[282] Fix | Delete
}
[283] Fix | Delete
[284] Fix | Delete
$suffix = Constants::is_true( 'SCRIPT_DEBUG' ) ? '' : '.min';
[285] Fix | Delete
$version = Constants::get_constant( 'WC_VERSION' );
[286] Fix | Delete
[287] Fix | Delete
if ( ! wp_script_is( 'a8c-address-autocomplete-service', 'registered' ) ) {
[288] 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' ) );
[289] Fix | Delete
}
[290] Fix | Delete
[291] Fix | Delete
if ( ! wp_script_is( 'a8c-address-autocomplete-service', 'enqueued' ) ) {
[292] Fix | Delete
wp_enqueue_script( 'a8c-address-autocomplete-service' );
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
wp_add_inline_script(
[296] Fix | Delete
'a8c-address-autocomplete-service',
[297] Fix | Delete
sprintf(
[298] Fix | Delete
'var a8cAddressAutocompleteServiceKeys = a8cAddressAutocompleteServiceKeys || {}; a8cAddressAutocompleteServiceKeys[ %1$s ] = { key: %2$s, canTelemetry: %3$s };',
[299] Fix | Delete
wp_json_encode( $this->id, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
[300] Fix | Delete
wp_json_encode( $this->get_jwt(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
[301] Fix | Delete
wp_json_encode( false !== $this->can_telemetry() && (bool) $this->can_telemetry(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
[302] Fix | Delete
),
[303] Fix | Delete
'before'
[304] Fix | Delete
);
[305] Fix | Delete
}
[306] Fix | Delete
}
[307] Fix | Delete
[308] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function