Skip to content

Commit

Permalink
v1.3.0 - Overlay Properties
Browse files Browse the repository at this point in the history
**Overlay Properties** - Read GeoJSON feature properties when importing (Settings > Overlays > Properties). If Waymark finds data for the specified property keys they will stored upon import. These can be automatically appended to the Overlay Description, or accessed programatically via the `layer.feature.properties` Object. Thanks to [dariospace](https://github.com/dariospace) for the [requesting](#45) the return of this feature.
  • Loading branch information
morehawes committed Jun 13, 2024
1 parent ec0742e commit 09fc24b
Show file tree
Hide file tree
Showing 23 changed files with 576 additions and 339 deletions.
2 changes: 1 addition & 1 deletion Waymark.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Plugin Name: Waymark
Plugin URI: https://www.waymark.dev/
Description: Mapping with WordPress made easy. With Waymark enabled, click on the "Maps" link in the sidebar to create and edit Maps. Once you are happy with your Map, copy the Waymark shortcode and add it to your content.
Version: 1.2.0
Version: 1.3.0
Text Domain: waymark
Author: Joe Hawes
Author URI: https://www.morehawes.ca/
Expand Down
14 changes: 13 additions & 1 deletion assets/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,18 @@ body.wp-admin.post-type-waymark_map.post-php #waymark-admin-container {
}
/**
* ====================================
* ============== DEBUG ===============
* ============ PROPERTIES ============
* ====================================
*/
.waymark-settings-tab.waymark-settings-tab-properties pre {
border: 1px solid #f1f1f1;
background: #f9f9f9;
line-height: 90%;
}
.waymark-settings-tab.waymark-settings-tab-properties pre code {
background: none;
font-size: 10px;
}
.waymark-settings-tab.waymark-settings-tab-properties small code {
font-size: 10px;
}
2 changes: 1 addition & 1 deletion assets/css/admin.min.css

Large diffs are not rendered by default.

41 changes: 19 additions & 22 deletions assets/less/admin.less
Original file line number Diff line number Diff line change
Expand Up @@ -961,27 +961,24 @@ body.wp-admin {

/**
* ====================================
* ============== DEBUG ===============
* ============ PROPERTIES ============
* ====================================
*/
// body {
// div.waymark-settings-section-advanced {
// .form-table {
// tr:last-child {
// display: none;
// th {
// vertical-align: top !important;
// }
// }
// }
// }
// &.waymark-debug {
// div.waymark-settings-section-advanced {
// .form-table {
// tr:last-child {
// display: table-row;
// }
// }
// }
// }
// }

.waymark-settings-tab.waymark-settings-tab-properties {
pre {
border: 1px solid @waymark-grey;
background: #f9f9f9;
line-height: 90%;
code {
background: none;
font-size: 10px;
}
}

small {
code {
font-size: 10px;
}
}
}
14 changes: 14 additions & 0 deletions inc/Admin/Waymark_AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,21 @@ function read_file() {

//Good to proceed
if ($file_contents) {

// If GeoJSON
if ($file_contents['file_type'] == 'geojson' || $file_contents['file_type'] == 'json') {
// Valid, with features
$feature_collection = Waymark_GeoJSON::string_to_feature_collection($file_contents['file_contents']);
if ($feature_collection && Waymark_GeoJSON::get_feature_count($feature_collection)) {
// Process Import
$feature_collection = Waymark_GeoJSON::process_import($feature_collection);
$file_contents['file_contents'] = Waymark_GeoJSON::feature_collection_to_string($feature_collection);
}
}

// Add to response
$response = array_merge($response, $file_contents);

//Unknown error
} else {
$response['error'] = esc_html__('Could not read the file.', 'waymark');
Expand Down
82 changes: 82 additions & 0 deletions inc/Admin/Waymark_Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function __construct() {
'waymark-settings-tab-markers' => '-- ' . esc_html__('Markers', 'waymark'),
'waymark-settings-tab-lines' => '-- ' . esc_html__('Lines', 'waymark'),
'waymark-settings-tab-shapes' => '-- ' . esc_html__('Shapes', 'waymark'),
'waymark-settings-tab-properties' => '-- ' . esc_html__('Properties', 'waymark'),
'label_sources' => esc_html__('Sources'),
'waymark-settings-tab-submission' => '-- ' . esc_html__('Submissions', 'waymark'),
'waymark-settings-tab-misc' => esc_html__('Advanced', 'waymark'),
Expand Down Expand Up @@ -570,6 +571,87 @@ function __construct() {
),
);

/**
* ===========================================
* =============== PROPERTIES ================
* ===========================================
*/

$this->tabs['properties'] = array(
'name' => esc_html__('Properties', 'waymark'),
'description' => '',
'sections' => array(
'props' => array(
'repeatable' => true,
'title' => esc_html__('Properties', 'waymark'),
'description' => '<span class="waymark-lead">' . __('Read <b><a href="https://geojson.org/">GeoJSON</a></b> feature properties when importing.', 'waymark') . '</span><br /><br />' . __('If Waymark finds data for the property keys below it will stored when it is imported. These can be automatically appended to the Overlay Description, or accessed programatically the <code>layer.feature.properties</code> Object.', 'waymark'),
'footer' => '<small>' . __(sprintf('For example, the properties below can be accessed through the %s, %s, %s Property Keys:', '<code>created_date</code>', '<code>updated_date</code>', '<code>expires_date</code>'), 'waymark') . '</small>
<pre><code>{
"type": "FeatureCollection",
"features": [
{
"geometry": { ... },
"type": "Feature",
"properties": {
"created_date": "2021-07-15T00:42:41Z",
"updated_date": "2021-07-16T00:42:41Z",
"expires_date": "2021-07-17T00:42:41Z"
}
}
]
}
</code></pre>',

// 'help' => array(
// 'url' => esc_attr(Waymark_Helper::site_url('docs/meta')),
// 'text' => esc_attr__('Meta Docs &raquo;', 'waymark')
// ),
'fields' => array(
'property_key' => array(
'name' => 'property_key',
'id' => 'property_key',
'type' => 'text',
'class' => '',
'title' => '<u>' . esc_html__('Property', 'waymark') . '</u> ' . esc_html__('Key', 'waymark'),
'default' => Waymark_Config::get_setting('property', 'props', 'property_key'),
'tip' => esc_attr__('This is the key associated with the data you are trying to access, i.e. "properties": {"property_key": "Some content here"}', 'waymark'),
// 'class' => Waymark_Config::get_item('meta', 'inputs') ? 'waymark-uneditable' : '',
'input_processing' => array(
'preg_replace("/[^0-9a-zA-Z -_.]/", "", $param_value);',
),
),
'property_title' => array(
'name' => 'property_title',
'id' => 'property_title',
'type' => 'text',
'class' => '',
'title' => '<u><span class="waymark-invisible">' . esc_html__('Property', 'waymark') . '</span></u> ' . esc_html__('Title', 'waymark'),
'default' => Waymark_Config::get_setting('property', 'props', 'property_title'),
'tip' => esc_attr__('The value for this property will be added to the Overlay Description under this title.', 'waymark'),
// 'class' => Waymark_Config::get_item('meta', 'inputs') ? 'waymark-uneditable' : ''
),
),
),

'options' => array(
'title' => esc_html__('Options', 'waymark'),
'description' => 'Append listed Properties to the Overlay Description.',
'footer' => sprintf(__('<small><b>Pro Tip!</b> Properties are added to the Overlay Description with class names that can be used to target them, e.g. %s.</small>', 'waymark'), '<code class="waymark-code" style="display:inline">&lt;p class="waymark-property waymark-property-property_key"&gt;&lt;b&gt;property_title&lt;/b&gt;&lt;br&gt;proprty_value&lt;/p&gt;</code>'),
'fields' => array(
'description_append' => array(
'name' => 'description_append',
'id' => 'description_append',
'type' => 'boolean',
'title' => esc_html__('Append', 'waymark'),
'default' => Waymark_Config::get_setting('property', 'options', 'description_append'),
'tip' => esc_attr__('Append the property value to the Overlay Description.', 'waymark'),

),
),
),
),
);

//Prepare Basemap values for editor option
$tile_layers = Waymark_Config::get_item('tiles', 'layers', true);

Expand Down
2 changes: 1 addition & 1 deletion inc/Front/Waymark_HTTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function template_redirect() {
if ($Map = new Waymark_Map($_REQUEST['map_id'])) {
if (isset($Map->data['map_data']) && !empty($Map->data['map_data'])) {
//Clean
$map_data = Waymark_Helper::remove_unwanted_overlay_properties($Map->data['map_data']);
$map_data = Waymark_GeoJSON::remove_unwanted_overlay_properties($Map->data['map_data']);
//Invalid Map data
} else {
die("-1");
Expand Down
81 changes: 81 additions & 0 deletions inc/Helpers/Waymark_GeoJSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ static public function string_to_feature_collection($string = '') {
return false;
}

/**
* Convert a GeoJSON array to a string
*
* @param array $FeatureCollection GeoJSON array
* @return string GeoJSON string
*/

static public function feature_collection_to_string($FeatureCollection = []) {
if (is_array($FeatureCollection)) {
return json_encode($FeatureCollection);
}

return false;
}

/**
* Get count of features in a GeoJSON array
*
Expand Down Expand Up @@ -150,4 +165,70 @@ static public function features_by_overlay_type($FeatureCollection = []) {

return $overlays;
}

static public function remove_unwanted_overlay_properties($FeatureCollection = [], $wanted = []) {
if (is_string($FeatureCollection)) {
$FeatureCollection = self::string_to_feature_collection($FeatureCollection);
}

if (!sizeof($wanted)) {
$wanted = Waymark_Helper::get_overlay_properties();
}

$FeatureCollection = Waymark_Helper::stringify_numbers($FeatureCollection);

//Feature Collection
if ($FeatureCollection && isset($FeatureCollection['features'])) {
foreach ($FeatureCollection['features'] as &$feature) {
// Include only wanted properties
$properties_out = [];
foreach ($wanted as $key) {
if (isset($feature['properties'][$key])) {
$properties_out[$key] = (string) $feature['properties'][$key];
}
}

//Update
$feature['properties'] = $properties_out;
}
}

return $FeatureCollection;
}

static public function process_import($FeatureCollection = []) {
// Clean
$FeatureCollection = self::remove_unwanted_overlay_properties($FeatureCollection);

// Append to description?
if (Waymark_Config::get_setting('properties', 'options', 'description_append') == '1') {
// Iterate over features
foreach ($FeatureCollection['features'] as &$feature) {

// Modify description

// Ensure there is a description
if (!array_key_exists('description', $feature['properties'])) {
$feature['properties']['description'] = '';
}

// Additional GeoJSON Properties
$custom_props = Waymark_Config::get_item('properties', 'props', true);
$custom_props = Waymark_Helper::multi_use_as_key($custom_props, 'property_key');

if (sizeof($custom_props)) {
foreach ($custom_props as $custom_prop) {
// Ensure property exists
if (array_key_exists($custom_prop['property_key'], $feature['properties'])) {

// Format
$feature['properties']['description'] .= '<p class="waymark-property waymark-property-' . $custom_prop['property_key'] . '"><b>' . $custom_prop['property_title'] . '</b><br>' . $feature['properties'][$custom_prop['property_key']] . '</p>';
}
}
}
}
}

return $FeatureCollection;
}
}
51 changes: 16 additions & 35 deletions inc/Helpers/Waymark_Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,40 +454,6 @@ static public function stringify_numbers($obj) {
return $obj;
}

static public function remove_unwanted_overlay_properties($data_in = [], $wanted = []) {
if (!sizeof($wanted)) {
$wanted = Waymark_Config::get_item('overlay_properties');
}

$FeatureCollection = json_decode($data_in);

$FeatureCollection = self::stringify_numbers($FeatureCollection);

//self::debug($FeatureCollection);

if ($FeatureCollection && sizeof($FeatureCollection->features)) {
foreach ($FeatureCollection->features as &$feature) {
//No existing properties
if (!property_exists($feature, 'properties')) {
return json_encode($FeatureCollection);
}

$properties_out = new stdClass();
foreach ($wanted as $key) {
if (property_exists($feature->properties, $key)) {
$properties_out->{$key} = (string) $feature->properties->{$key};
}
}
//Update
$feature->properties = $properties_out;
}
}

$data_out = json_encode($FeatureCollection);

return $data_out;
}

static public function set_map_data_property($map_data, $key = false, $value = false, $append = false) {
$FeatureCollection = json_decode($map_data);

Expand Down Expand Up @@ -1240,7 +1206,7 @@ public static function build_overlay_content($feature = [], $feature_type = 'mar

//Expected Waymark properties
// i.e. array('radius', 'type', 'title', 'description', 'image_thumbnail_url', 'image_medium_url', 'image_large_url')
foreach (Waymark_Config::get_item('overlay_properties') as $property_key) {
foreach (Waymark_Helper::get_overlay_properties() as $property_key) {
//Property not set
if (!isset($feature['properties'][$property_key])) {
continue;
Expand Down Expand Up @@ -1490,4 +1456,19 @@ public static function overlays_list_html($overlays = []) {

return $out;
}

public static function get_overlay_properties() {
$default = Waymark_Config::get_item('overlay_properties');

// Additional GeoJSON Properties
$extra = Waymark_Config::get_item('properties', 'props', true);
$extra = Waymark_Helper::multi_use_as_key($extra, 'property_key');

if (sizeof($extra)) {
// Key an array of property keys
$extra = array_keys($extra);
}

return array_merge($default, $extra);
}
}
Loading

0 comments on commit 09fc24b

Please sign in to comment.