Skip to content

Commit

Permalink
OOP classes
Browse files Browse the repository at this point in the history
  • Loading branch information
bozdoz committed Aug 7, 2017
1 parent 7292587 commit 6653761
Show file tree
Hide file tree
Showing 14 changed files with 1,651 additions and 1,309 deletions.
122 changes: 41 additions & 81 deletions admin/settings.php
Original file line number Diff line number Diff line change
@@ -1,126 +1,86 @@
<?php
$defaults = self::$defaults;
unset($defaults['leaflet_geocoded_locations']);
$title = $plugin_data['Name'];
$description = $plugin_data['Description'];

function option_label ($opt) {
$opt = explode('_', $opt);

foreach($opt as &$v) {
$v = ucfirst($v);
}
echo implode(' ', $opt);
}
?>
<div class="wrap">
<h1><?php echo $title; ?></h1>
<p><?php echo $description; ?></p>
<?php
if (isset($_POST['submit'])) {
/* copy and overwrite $post for checkboxes */
$form = $_POST;

foreach ($defaults as $name=>$atts) {
$type = isset($atts['type']) ? $atts['type'] : '';
foreach ($settings->options as $name => $option) {
if (!$option->type) continue;

/* checkboxes don't get sent if not checked */
if ($type === 'checkbox') {
$form[$name] = isset($_POST[$name]) ? 1 : 0;
if ($option->type === 'checkbox') {
$form[$name] = isset($_POST[ $name ]) ? 1 : 0;
}
update_option($name, stripslashes( $form[$name]) );

$settings->set($name, stripslashes( $form[$name]));
}
?>
<div class="updated">
<p>Options Updated!</p>
<div class="notice notice-success is-dismissible">
<p>Options Updated!</p>
</div>
<?php
} elseif (isset($_POST['reset'])) {
foreach ($defaults as $name=>$atts) {
if (isset($atts['default']) && !isset($atts['noreset'])) {
update_option($name, $atts['default']);
}
}
$settings->reset();
?>
<div class="updated">
<p>Options have been reset to default values!</p>
<div class="notice notice-success is-dismissible">
<p>Options have been reset to default values!</p>
</div>
<?php
}
?>

<div class="wrap">
<h2>Leaflet Map Plugin</h2>
<div class="wrap">
<form method="post">
<div class="container">
<h2>Settings</h2>
<hr>
</div>
<?php
function option_label ($opt = 'leaflet_map_tile_url') {
$opt = explode('_', $opt);
array_shift($opt);
foreach($opt as &$v) {
$v = ucfirst($v);
}
echo implode(' ', $opt);
}

foreach ($defaults as $name=>$atts) {
$type = isset($atts['type']) ? $atts['type'] : '';
foreach ($settings->options as $name => $option) {
if (!$option->type) continue;
?>
<div class="container">
<label>
<span class="label"><?php option_label($name); ?></span>
<span class="input-group">
<?php
if ($type === 'select') {
?>
<select id="<?php echo $name; ?>"
name="<?php echo $name; ?>"
class="full-width">
<?php
foreach ($atts['options'] as $o => $n) {
?>
<option value="<?php echo $o; ?>"<?php if (get_option($name) == $o) echo ' selected' ?>>
<?php echo $n; ?>
</option>
<?php
}
?>
</select>
<?php
} elseif ($type === 'text') {
?>
<input
class="full-width"
name="<?php echo $name; ?>"
type="text"
id="<?php echo $name; ?>"
value="<?php echo htmlspecialchars( get_option($name, $atts['default']) ); ?>"
/>
<?php
} elseif ($type === 'textarea') {
?>
<textarea
id="<?php echo $name; ?>"
class="full-width"
name="<?php echo $name; ?>"><?php echo htmlspecialchars( get_option($name, $atts['default']) ); ?></textarea>
<?php
} elseif ($type === 'checkbox') {
?>
<input
class="checkbox"
name="<?php echo $name; ?>"
type="checkbox"
id="<?php echo $name; ?>"
<?php if (get_option($name, $atts['default'])) echo ' checked="checked"' ?>
/>
<?php
}
$option->widget($name, $settings->get($name));
?>
</span>
</label>

<?php
if (isset($atts['helptext'])) {
?>
if ($option->helptext) {
?>
<div class="helptext">
<p class="description"><?php echo $atts['helptext']; ?></p>
<p class="description"><?php
echo $option->helptext;
?></p>
</div>
<?php
<?php
}
?>
</div>
<?php
}
?>

<div class="container">
<div class="submit">
<input type="submit" name="submit" id="submit" class="button button-primary" value="Save Changes">
</div>
<div class="container">
<input type="submit" name="reset" id="reset" class="button button-secondary" value="Reset to Defaults">
</div>

Expand Down
176 changes: 176 additions & 0 deletions class.geocoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php
/**
* Geocoder
*
* calls the specific geocoder function (chosen in admin or default: google_geocode)
*
* todo: candidate for separate class
*
* @param string $address the requested address to look up
*/

class Leaflet_Geocoder {
/**
* Geocoder should return this on error/not found
* @var array $not_found
*/
private $not_found = array('lat' => 0, 'lng' => 0);
/**
* Latitude
* @var float $lat
*/
public $lat = 0;
/**
* Longitude
* @var float $lng
*/
public $lng = 0;

public function __construct ($address) {
$settings = Leaflet_Map_Plugin_Settings::init();
$address = urlencode( $address );

$geocoder = $settings->get('geocoder');

$cached_address = 'leaflet_' . $geocoder . '_' . $address;

/* retrieve cached geocoded location */
$found_cache = get_option( $cached_address );

if ( $found_cache ) {
return $found_cache;
}

$geocoding_method = $geocoder . '_geocode';

try {
$location = (Object) $this->$geocoding_method( $address );

/* add location */
add_option($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);
} catch (Exception $e) {
$location = $this->not_found;
}
// set variables
$this->lat = $location->lat;
$this->lng = $location->lng;
}

/**
* Used by geocoders to make requests via curl or file_get_contents
*
* includes a try/catch
*
* @param string $url the urlencoded request url
* @return varies object from API or null (failed)
*/
private function get_url( $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_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

$data = curl_exec($ch);
curl_close($ch);

return $data;
} else if (ini_get('allow_url_fopen')) {
/* try file get contents */
return file_get_contents( $url );
}

$error_msg = 'Could not get url: ' . $url;
throw new Exception( $error_msg );
}

/**
* Google geocoder (https://developers.google.com/maps/documentation/geocoding/start)
*
* @param string $address the urlencoded address to look up
* @return varies object from API or null (failed)
*/

private function google_geocode ( $address ) {
$geocode_url = 'https://maps.googleapis.com/maps/api/geocode/json?address=';
$geocode_url .= $address;
$json = $this->get_url($geocode_url);

if ($json) {
$json = json_decode($json);
/* found location */
if ($json->{'status'} == 'OK') {

$location = $json->{'results'}[0]->{'geometry'}->{'location'};

return (Object) $location;
}
}

return $this->not_found;
}

/**
* OpenStreetMap geocoder Nominatim (https://nominatim.openstreetmap.org/)
*
* @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;
$json = $this->get_url($geocode_url);
$json = json_decode($json);

if (is_array($json) &&
is_object($json[0]) &&
$json[0]->lat) {
// found location
return (Object) array(
'lat' => $json[0]->lat,
'lng' => $json[0]->lon,
);
}

return $this->not_found;
}

/**
* Danish Addresses Web Application
* (https://dawa.aws.dk)
*
* @param string $address the urlencoded address to look up
* @return varies object from API or null (failed)
*/

private function dawa_geocode ( $address ) {
$geocode_url = 'https://dawa.aws.dk/adresser?format=json&q=';
$geocode_url .= $address;
$json = $this->get_url($geocode_url);
$json = json_decode($json);

if (is_array($json) &&
is_object($json[0]) &&
$json[0]->status == 1) {
/* found location */
$location = (Object) array(
'lat' => $json[0]->adgangsadresse->adgangspunkt->koordinater[1],
'lng' => $json[0]->adgangsadresse->adgangspunkt->koordinater[0]
);

return $location;
}

return $this->not_found;
}
}
Loading

0 comments on commit 6653761

Please sign in to comment.