Open bozdoz opened 1 year ago
And the full example, using the filters to do md5 hashing, and transients from #218:
<?php
// theme/functions.php
//
// geocoders save to transients
//
const GEOCACHE_PREFIX = 'lm_';
// get transient
function leaflet_transient_get($key, $plain_address){
$transient_key = GEOCACHE_PREFIX . md5($key);
return get_transient( $transient_key );
}
add_filter( 'leaflet_geocoder_get_cache', 'leaflet_transient_get', 10, 2 );
// set transient
function leaflet_transient_set($key, $value){
$transient_key = GEOCACHE_PREFIX . md5($key);
set_transient( $transient_key, $value, MONTH_IN_SECONDS );
// return false to avoid saving to options table
return false;
}
add_filter( 'leaflet_geocoder_set_cache', 'leaflet_transient_set', 10, 2 );
// don't update cache key array
function update_caches($address){
return false;
}
add_filter( 'leaflet_geocoder_update_caches', 'update_caches' );
// custom cleanup function
function leaflet_remove_caches() {
global $wpdb;
// _transient_ is prefixed to all transient keys in the options table
$prefix = '_transient_' . GEOCACHE_PREFIX;
$prefix = $wpdb->esc_like( $prefix );
$sql = "SELECT `option_name` FROM $wpdb->options WHERE `option_name` LIKE '%s'";
$keys = $wpdb->get_results($wpdb->prepare($sql, $prefix . '%'), ARRAY_A);
if (is_wp_error($keys) || !is_array($keys)) {
return;
}
$transient_keys = array_map(function ($key) {
// Remove '_transient_' from the option name.
return ltrim($key['option_name'], '_transient_');
}, $keys);
foreach ($transient_keys as $transient_name) {
delete_transient($transient_name);
}
}
add_action( 'leaflet_geocoder_remove_caches', 'leaflet_remove_caches' );
Seems to work great, refreshing shortcode helper page and clearing geocoder cache from settings
Kudos, SonarCloud Quality Gate passed!
0 Bugs
0 Vulnerabilities
0 Security Hotspots
106 Code Smells
No Coverage information
0.0% Duplication
Screenshots of the migration (cycling between master branch and this branch):
v3.3.0
After migration to v3.4.0 (simply by refreshing the browser)
Kudos, SonarCloud Quality Gate passed!
0 Bugs
0 Vulnerabilities
0 Security Hotspots
69 Code Smells
No Coverage information
0.0% Duplication
Using these new filters, a consumer can handle the addresses themselves: