bozdoz / wp-plugin-leaflet-map

Add leaflet maps to Wordpress with shortcodes
https://wordpress.org/plugins/leaflet-map/
GNU General Public License v2.0
140 stars 71 forks source link

Adds Filters to Geocoder; Sets options to autoload=false #217

Open bozdoz opened 1 year ago

bozdoz commented 1 year ago

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 commented 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

Screenshot 2023-04-25 at 9 14 42 PM
sonarcloud[bot] commented 1 year ago

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

bozdoz commented 1 year ago

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
sonarcloud[bot] commented 9 months ago

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