-
-
Notifications
You must be signed in to change notification settings - Fork 72
Adds Filters to Geocoder; Sets options to autoload=false #217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 7 commits
565a298
1d23749
4c70c70
7baf8a6
b6a7042
c95a0de
c38a3d7
7f34981
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| /** | ||
| * Geocoder | ||
| * | ||
| * calls the specific geocoder function (chosen in admin or default: google_geocode) | ||
| * calls the specific geocoder function (chosen in admin or default: osm) | ||
| * | ||
| */ | ||
|
|
||
|
|
@@ -11,7 +11,7 @@ class Leaflet_Geocoder { | |
| * Geocoder should return this on error/not found | ||
| * @var array $not_found | ||
| */ | ||
| private $not_found = array('lat' => 0, 'lng' => 0); | ||
| private static $not_found = array('lat' => 0, 'lng' => 0); | ||
| /** | ||
| * Latitude | ||
| * @var float $lat | ||
|
|
@@ -23,6 +23,9 @@ class Leaflet_Geocoder { | |
| */ | ||
| public $lng = 0; | ||
|
|
||
| /** key for all locations */ | ||
| public static $locations_key = 'leaflet_geocoded_locations'; | ||
|
|
||
| /** | ||
| * new Geocoder from address | ||
| * | ||
|
|
@@ -42,7 +45,7 @@ public function __construct ($address) { | |
| $cached_address = 'leaflet_' . $geocoder . '_' . $address; | ||
|
|
||
| /* retrieve cached geocoded location */ | ||
| $found_cache = get_option( $cached_address ); | ||
| $found_cache = $this->get_cache( $cached_address, $address ); | ||
|
|
||
| if ( $found_cache ) { | ||
| $location = $found_cache; | ||
|
|
@@ -53,16 +56,17 @@ public function __construct ($address) { | |
| try { | ||
| $location = (Object) $this->$geocoding_method( $address ); | ||
|
|
||
| /* add location */ | ||
| add_option($cached_address, $location); | ||
| /* update location data in db/cache */ | ||
| $this->set_cache( $cached_address, $location ); | ||
|
|
||
| /* add option key to locations for clean up purposes */ | ||
| $locations = get_option('leaflet_geocoded_locations', array()); | ||
| array_push($locations, $cached_address); | ||
| update_option('leaflet_geocoded_locations', $locations); | ||
| /* add cache to cached list for cleanup */ | ||
| $this->update_caches( $cached_address ); | ||
| } catch (Exception $e) { | ||
| // failed | ||
| $location = $this->not_found; | ||
| /** | ||
| * @since 3.4.0 | ||
| * use 'leaflet_geocoder_not_found' filter to return your own not_found response | ||
| */ | ||
| $location = apply_filters( 'leaflet_geocoder_not_found', self::$not_found ); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -72,17 +76,6 @@ public function __construct ($address) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Removes location caches | ||
| */ | ||
| public static function remove_caches () { | ||
| $addresses = get_option('leaflet_geocoded_locations', array()); | ||
| foreach ($addresses as $address) { | ||
| delete_option($address); | ||
| } | ||
| delete_option('leaflet_geocoded_locations'); | ||
| } | ||
|
|
||
| /** | ||
| * Used by geocoders to make requests via curl or file_get_contents | ||
| * | ||
|
|
@@ -95,13 +88,12 @@ private function get_url( $url ) { | |
| $referer = get_site_url(); | ||
|
|
||
| if (in_array('curl', get_loaded_extensions())) { | ||
| /* try curl */ | ||
| $ch = curl_init(); | ||
|
|
||
| curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE); | ||
| curl_setopt($ch, CURLOPT_HEADER, 0); | ||
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | ||
| curl_setopt($ch, CURLOPT_REFERER, $referer); | ||
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | ||
| curl_setopt($ch, CURLOPT_REFERER, $referer); | ||
| curl_setopt($ch, CURLOPT_URL, $url); | ||
| curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); | ||
|
|
||
|
|
@@ -110,14 +102,12 @@ private function get_url( $url ) { | |
|
|
||
| return $data; | ||
| } else if (ini_get('allow_url_fopen')) { | ||
| /* try file get contents */ | ||
|
|
||
| $opts = array( | ||
| 'http' => array( | ||
| 'header' => array("Referer: $referer\r\n") | ||
| ) | ||
| ); | ||
| $context = stream_context_create($opts); | ||
| $opts = array( | ||
| 'http' => array( | ||
| 'header' => array("Referer: $referer\r\n") | ||
| ) | ||
| ); | ||
| $context = stream_context_create($opts); | ||
|
|
||
| return file_get_contents($url, false, $context); | ||
| } | ||
|
|
@@ -132,7 +122,6 @@ private function get_url( $url ) { | |
| * @param string $address the urlencoded address to look up | ||
| * @return varies object from API or null (failed) | ||
| */ | ||
|
|
||
| private function google_geocode ( $address ) { | ||
| // Leaflet_Map_Plugin_Settings | ||
| $settings = Leaflet_Map_Plugin_Settings::init(); | ||
|
|
@@ -146,7 +135,6 @@ private function google_geocode ( $address ) { | |
|
|
||
| /* found location */ | ||
| if ($json->status == 'OK') { | ||
|
|
||
| $location = $json->results[0]->geometry->location; | ||
|
|
||
| return (Object) $location; | ||
|
|
@@ -161,7 +149,6 @@ private function google_geocode ( $address ) { | |
| * @param string $address the urlencoded address to look up | ||
| * @return varies object from API or null (failed) | ||
| */ | ||
|
|
||
| private function osm_geocode ( $address ) { | ||
| $geocode_url = 'https://nominatim.openstreetmap.org/?format=json&limit=1&q='; | ||
| $geocode_url .= $address; | ||
|
|
@@ -198,4 +185,89 @@ private function dawa_geocode ( $address ) { | |
| 'lng' => $json[0]->adgangsadresse->adgangspunkt->koordinater[0] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * gets a single location's coordinates from the cached locations | ||
| */ | ||
| public function get_cache($address_key, $plain_address) { | ||
| /** | ||
| * @since 3.4.0 | ||
| * using 'leaflet_geocoder_get_cache', | ||
| * you can return any value that is not identical to the address_key to avoid using get_transient | ||
| */ | ||
| $filtered = apply_filters( 'leaflet_geocoder_get_cache', $address_key, $plain_address ); | ||
|
|
||
| if ($filtered === $address_key) { | ||
| return get_transient( $address_key ); | ||
| } | ||
|
|
||
| return $filtered; | ||
| } | ||
|
|
||
| /** | ||
| * gets the array of saved locations and updates an individual location | ||
| */ | ||
| public function set_cache($key, $value) { | ||
| /** | ||
| * @since 3.4.0 | ||
| * using 'leaflet_geocoder_set_cache', | ||
| * you can return any falsy value to omit the set_transient | ||
| */ | ||
| if (apply_filters('leaflet_geocoder_set_cache', $key, $value)) { | ||
| // get user-defined expiry (maybe this should be an admin option?) | ||
| $expiry = apply_filters('leaflet_geocoder_expiry', null); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. power users can define their own transient expiries |
||
|
|
||
| if ($expiry === null) { | ||
| // stagger caches between 200-400 days to prevent all caches expiring on the same day | ||
| $stagger = random_int(200, 400); | ||
| $expiry = DAY_IN_SECONDS * $stagger; | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. by default, we set expiry randomly between 200 and 400 days |
||
| } | ||
|
|
||
| set_transient( $key, $value, $expiry ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Appends an address to a list of addresses in the db, for cleanup | ||
| */ | ||
| public function update_caches( $address ) { | ||
| /** | ||
| * @since 3.4.0 | ||
| * using 'leaflet_geocoder_update_caches', | ||
| * you can return any falsy value to omit the set_transient | ||
| */ | ||
| if (apply_filters('leaflet_geocoder_update_caches', $address)) { | ||
| $locations = get_transient( self::$locations_key ); | ||
|
|
||
| if (!$locations) { | ||
| $locations = array(); | ||
| } | ||
|
|
||
| array_push( $locations, $address ); | ||
|
|
||
| // set to 25 year expiry since we never really want it to expire | ||
| // but omitting expiry causes it to autoload | ||
| set_transient( self::$locations_key, $locations, YEAR_IN_SECONDS * 25 ); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the transient which holds all address keys is 25 years to avoid expiry and avoid autoload |
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Removes all location caches | ||
| */ | ||
| public static function remove_caches() { | ||
| /** @since 3.4.0 */ | ||
| do_action('leaflet_geocoder_remove_caches'); | ||
|
|
||
| $addresses = get_transient( self::$locations_key ); | ||
|
|
||
| if ( !$addresses ) { | ||
| return; | ||
| } | ||
|
|
||
| foreach ($addresses as $address) { | ||
| delete_transient( $address ); | ||
| } | ||
|
|
||
| delete_transient( self::$locations_key ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,13 +104,34 @@ public static function init() { | |
| * Leaflet_Map Constructor | ||
| */ | ||
| private function __construct() { | ||
| $this->run_migrations(); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. run migrations before any other initialization of the plugin |
||
| $this->init_hooks(); | ||
| $this->add_shortcodes(); | ||
|
|
||
| // loaded | ||
| do_action('leaflet_map_loaded'); | ||
| } | ||
|
|
||
| /** | ||
| * Check for version discrepancy and run migrations if necessary | ||
| * @since 3.4.0 | ||
| */ | ||
| private function run_migrations() { | ||
| // assume 3.3.0 if it doesn't exist | ||
| $db_version = get_option(LEAFLET_MAP__DB_VERSION_KEY, '3.3.0'); | ||
|
|
||
| if ($db_version === LEAFLET_MAP__PLUGIN_VERSION) { | ||
| return; | ||
| } | ||
|
|
||
| include_once LEAFLET_MAP__PLUGIN_DIR . 'class.migrations.php'; | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. only include migrations if there's a version mismatch in the db |
||
|
|
||
| Leaflet_Map_Migrations::run_once($db_version); | ||
|
|
||
| // set db version to current plugin version | ||
| update_option(LEAFLET_MAP__DB_VERSION_KEY, LEAFLET_MAP__PLUGIN_VERSION); | ||
| } | ||
|
|
||
| /** | ||
| * Add actions and filters | ||
| */ | ||
|
|
@@ -187,8 +208,8 @@ public static function enqueue_and_register() | |
| $js_url = $settings->get('js_url'); | ||
| $css_url = $settings->get('css_url'); | ||
|
|
||
| wp_register_style('leaflet_stylesheet', $css_url, Array(), null, false); | ||
| wp_register_script('leaflet_js', $js_url, Array(), null, true); | ||
| wp_register_style('leaflet_stylesheet', $css_url, Array(), LEAFLET_MAP__PLUGIN_VERSION, false); | ||
| wp_register_script('leaflet_js', $js_url, Array(), LEAFLET_MAP__PLUGIN_VERSION, true); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adds plugin version like all the other registered scripts; #222 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I don't think this is right. Adding the plugin version rather than the Leaflet JS version means that if the Leaflet JS version is changed then the cache potentially wouldn't know (if the URL was the same). This is why I would opt for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Furthermore, $in_footer is set to true here but line 218 is set to false. I think that both need to be true or both need to be false which is why the JavaScript is breaking when using "Merge + Minify + Refresh" plugin.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This makes me think "null" is better here.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hesitant to change this because we've had issues in the past with people running async and deferred scripts. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It makes no odds to me. I've developed around it by directly filtering the global $wp_styles and $wp_scripts. Here is what Wordpress says though (script version not plugin version) https://developer.wordpress.org/reference/functions/wp_register_script/#parameters
But "null" will definitely not help the cache at all. Again, it doesn't bother me if you change the scripts to both be in the header or footer because I've developed around it by directly filtering the global $wp_scripts. We chose to put them both in the footer FYI. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. UPDATE: We have changed our position on this and moved both scripts to be loaded in the header instead of the footer. This is because loading them in the footer breaks compatibility with Leaflet.GestureHandling https://elmarquis.github.io/Leaflet.GestureHandling/ |
||
|
|
||
| // new required MapQuest javascript file | ||
| $tiling_service = $settings->get('default_tiling_service'); | ||
|
|
@@ -204,7 +225,8 @@ public static function enqueue_and_register() | |
| // optional ajax geojson plugin | ||
| wp_register_script('tmcw_togeojson', $settings->get('togeojson_url'), Array('jquery'), LEAFLET_MAP__PLUGIN_VERSION, false); | ||
|
|
||
| if (defined('WP_DEBUG') && WP_DEBUG) { | ||
| // @since 3.4.0 | ||
| if (defined('LEAFLET_MAP__DEBUG') && LEAFLET_MAP__DEBUG) { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as per #222 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great! |
||
| $minified = ''; | ||
| } else { | ||
| $minified = '.min'; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
| /** | ||
| * Leaflet Map Migrations | ||
| * @since 3.4.0 | ||
| */ | ||
|
|
||
| // Exit if accessed directly | ||
| if (!defined('ABSPATH')) { | ||
| exit; | ||
| } | ||
|
|
||
| define('LEAFLET_MAP__MIGRATION_DIR', LEAFLET_MAP__PLUGIN_DIR . 'migrations/'); | ||
|
|
||
| class Leaflet_Map_Migrations { | ||
| /** | ||
| * only call once | ||
| **/ | ||
| private static $called = false; | ||
|
|
||
| /** | ||
| * We assume if this is called, our db version diverges from plugin version | ||
| * migrations here are run in ascending order, always compared via '<' against db version | ||
| */ | ||
| public static function run_once($db_version) { | ||
| if ( self::$called ) { | ||
| return; | ||
| } | ||
|
|
||
| self::$called = true; | ||
|
|
||
| if (version_compare($db_version, '3.4.0', '<')) { | ||
| include_once LEAFLET_MAP__MIGRATION_DIR . '001-v3.4.0-geocoder-transients.php'; | ||
|
|
||
| migration001(); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. included above; needs to be a unique migration function name; assuming this is ok; maybe I could use "leaflet_map__" prefix? |
||
|
|
||
| // update version, if for some reason something fails afterwards, and we want to prevent this migration | ||
| update_option(LEAFLET_MAP__DB_VERSION_KEY, '3.4.0'); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assuming there may be more migrations in the future, we update db asap so this particular migration never refires |
||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alright, I've gone back to transients