wp-plugin-leaflet-map icon indicating copy to clipboard operation
wp-plugin-leaflet-map copied to clipboard

Adds Filters to Geocoder; Sets options to autoload=false

Open bozdoz opened this issue 2 years ago • 5 comments

bozdoz avatar Apr 21 '23 03:04 bozdoz

Using these new filters, a consumer can handle the addresses themselves:

<?php

function getit($address_key, $plain_address){
  echo "<pre>". "in getit";
  var_dump($address_key);
  var_dump($plain_address);
  echo "</pre>";
}

add_filter( 'leaflet_geocoder_get_cache', 'getit', 10, 2 );

function setit($key, $value){
  echo "<pre>". "in setit";
  var_dump($key);
  var_dump($value);
  echo "</pre>";
}

add_filter( 'leaflet_geocoder_set_cache', 'setit', 10, 2 );

function update_caches($address){
  echo "<pre>". "in update_caches";
  var_dump($address);
  echo "</pre>";
}

add_filter( 'leaflet_geocoder_update_caches', 'update_caches' );
Screenshot 2023-04-25 at 8 53 15 PM

bozdoz avatar Apr 25 '23 23:04 bozdoz

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

Screenshot 2023-04-25 at 9 14 42 PM

bozdoz avatar Apr 26 '23 00:04 bozdoz

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 106 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

sonarqubecloud[bot] avatar May 18 '23 02:05 sonarqubecloud[bot]

Screenshots of the migration (cycling between master branch and this branch):

v3.3.0

Screenshot 2023-05-17 at 11 54 27 PM

After migration to v3.4.0 (simply by refreshing the browser)

Screenshot 2023-05-17 at 11 55 04 PM

bozdoz avatar May 18 '23 03:05 bozdoz

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 69 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

sonarqubecloud[bot] avatar Sep 20 '23 01:09 sonarqubecloud[bot]