Skip to content

Commit

Permalink
Merge branch 'release/2.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
gsarig committed Jan 28, 2024
2 parents 39b40e2 + 471dbf2 commit e76ef5d
Show file tree
Hide file tree
Showing 22 changed files with 1,418 additions and 641 deletions.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Instead of manually adding coordinates for each one of your markers, just click-
* [Place search](https://www.gsarigiannidis.gr/openstreetmap-place-search/). Find locations by typing keywords.
* Remembers the zoom that you set when adding the markers and stores it so that you don't set it by hand (which you can do anyway if you prefer).
* OpenAI integration which allows you to add markers by using commands in natural language. Just say "please" to activate (e.g. "Please, show me where GOT was filmed"). Requires an OpenAI API key. [Read more](https://www.gsarigiannidis.gr/openstreetmap-openai-integration/).
* Query Maps: Supports creating a map out of maps added on other posts or post types. This can be quite powerful when, for example, you have a custom post type for "Places" with each place having its own map, and you want to dynamically gather-up all the places on a single map.
* Shortcode support: You can use the shortcode `[ootb_openstreetmap]` as an alternative way to use the aforementioned Query Maps feature (see the FAQ for more info).
* Adjust the map height.
* Change the default marker icon with a custom one.
* Enable or disable map dragging.
Expand Down Expand Up @@ -82,6 +84,56 @@ Check under the "Map behavior" section, at the blocks' settings at the sidebar o

First of all, you will need to create an account to [OpenAI](https://openai.com/) and get an API key. Then, go to the plugin's settings page and paste your key there. After that, you can start adding markers by using commands in natural language. Just say "please" to activate (e.g. "Please, show me where GOT was filmed"). Please keep in mind, though, that it's like asking ChatGPT: the answers you get might not always be 100% reliable, and you should always double-check to confirm their accuracy. [Read more](https://www.gsarigiannidis.gr/openstreetmap-openai-integration/).

### How can I query maps from other posts or post types?
On the block's side panel, Select the "Map data" panel and click on the "Fetch locations" button. This will automatically retrieve on the frontend all the markers from your posts (you can also select a specific post type from the dropdown). The block will be locked from editing, as the markers will be dynamically retrieved from the selected posts. If you don't want that, there is a "Stop syncing" button that will unlock the block, drop the markers on the map and allow you to edit.

### How can I use the shortcode?
The shortcode `[ootb_openstreetmap]` allows you to display a dynamic map, which retrieves markers from other posts or post types. Just add it to a post or page and you're good to go. By default, it will fetch the markers from the 100 most recent posts. The shortcode supports the following attributes:
* post_type: (Optional) The type of post to query. By default, it is set to `post`.
* posts_per_page: (Optional) The number of posts to be displayed on page. Default value is `100`.
* post_ids: (Optional) Comma-separated IDs of the posts to include in the query.
* height: (Optional) The desired height for the map. Default value is empty, which falls back to `400px`.
* provider: (Optional) Specifies the map provider. Options are: `openstreetmap`, `mapbox` and `stamen`. The default value is an empty string which falls back to `openstreetmap`.
* maptype: (Optional) Specifies the type of map. Options are: `markers`, `polygon` and `polyline`. The default value is an empty string, which will fall back to `markers`.
* touchzoom: (Optional) If set, touch zoom will be enabled on the map. It can be either `true` or `false`. The default value is an empty string, which falls back to `true`.
* scrollwheelzoom: (Optional) If set, enables zooming on the map with mouse scroll wheel. It can be either `true` or `false`. The default value is an empty string, which falls back to `true`.
* dragging: (Optional) If set, dragging is enabled on the map. It can be either `true` or `false`. The default value is an empty string, which falls back to `true`.
* doubleclickzoom: (Optional) If set, allows zooming in on the map with a double click. It can be either `true` or `false`. The default value is an empty string, which falls back to `true`.
* marker: (Optional) Specifies the marker for the map. This should correspond to the URL of the image that you want to use as the marker's icon (example: `https://www.example.com/my-custom-icon.png`). The default value is an empty string, which retrieves the default marker.

Here's an example of how you can use it:
```
[ootb_openstreetmap post_type="post" post_ids="1,2,3,4" height="400px" provider="mapbox" maptype="polygon" touchzoom="true" scrollwheelzoom="true" dragging="true" doubleclickzoom="true" marker="https://www.example.com/my-custom-icon.png"]
```

### I want more control. Are there any hooks that I could use?
Glad you asked! There are a few hooks that you can use to further customize the plugin's behavior. Here they are:
* `ootb_query_post_type`: Allows you to change the post type that the plugin will query for markers. By default, it is set to `post`. You can pass multiple post types as an array. Example:
```
add_filter( 'ootb_query_post_type', function() { return array( 'post', 'page' ); } );
```
* `ootb_query_posts_per_page`: Allows you to change the number of posts that the plugin will query for markers. By default, it is set to `100`. Example:
```
add_filter( 'ootb_query_posts_per_page', function() { return 500; } );
```
* `ootb_query_extra_args`: Allows you to add extra arguments to the query that the plugin will use to retrieve markers. By default, it is set to an empty array. Example:
```
add_filter(
'ootb_query_extra_args',
function() {
return [
'tax_query' => [
[
'taxonomy' => 'people',
'field' => 'slug',
'terms' => 'bob'
]
];
}
);
```
Keep in mind that the extra args will be merged with the default ones, so you don't have to worry about overriding them. In fact, the args that are required for the query to work, cannot be overridden.

## Screenshots

![The map editing screen](.wordpress-org/screenshot-3.jpg)
Expand Down
36 changes: 34 additions & 2 deletions assets/ootb-openstreetmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
const touchZoom = osmap.getAttribute('data-touchzoom');
const doubleClickZoom = osmap.getAttribute('data-doubleclickzoom');
const scrollWheelZoom = osmap.getAttribute('data-scrollwheelzoom');
const bounds = osmap.getAttribute('data-bounds');
const defaultIcon = JSON.parse(decodeURIComponent(escapedDefaultIcon));
const locations = JSON.parse(decodeURIComponent(escapedMarkers));
const mapType = osmap.getAttribute('data-maptype');
Expand All @@ -46,7 +45,7 @@
}
}

const map = L.map(osmap, mapOptions).setView(JSON.parse(bounds), parseInt(zoom));
const map = initializeMapView(osmap, mapOptions, zoom, locations, escapedDefaultIcon);

// Set the rest of the map options
if ('false' === dragging) {
Expand Down Expand Up @@ -102,4 +101,37 @@
marker.addTo(map);
}
}

function initializeMapView(osmap, mapOptions, zoom, locations, defaultIconString) {
const map = L.map(osmap, mapOptions);
const boundsCheck = JSON.parse(osmap.getAttribute('data-bounds'));

if (boundsCheck[0] !== null && boundsCheck[1] !== null) {
map.setView(boundsCheck, parseInt(zoom));
} else {
let markers = [];
locations.forEach(location => {
const defaultIcon = JSON.parse(decodeURIComponent(defaultIconString));
const markerIcon = structuredClone(defaultIcon);
if (location.icon) {
markerIcon.iconUrl = location.icon.url;
}
let marker = L.marker([location.lat, location.lng], {icon: L.icon(markerIcon)});
if (location.text) {
marker.bindPopup(location.text);
}
markers.push(marker);
marker.addTo(map);
});

// Use markers to calculate bounds.
if (markers.length) {
// Create a new feature group.
let group = new L.featureGroup(markers);
// Adjust the map to show all markers.
map.fitBounds(group.getBounds());
}
}
return map;
}
})();
10 changes: 10 additions & 0 deletions build/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@
"openAImode": {
"type": "string",
"default": ""
},
"queryArgs": {
"type": "object",
"default": {
"post_type": "post"
}
},
"serverSideRender": {
"type": "boolean",
"default": false
}
},
"textdomain": "ootb-openstreetmap",
Expand Down
2 changes: 1 addition & 1 deletion build/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'react-dom', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n'), 'version' => 'd14ede070655749920c9');
<?php return array('dependencies' => array('react', 'react-dom', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n'), 'version' => '9960ceea625e1d6537e9');
Loading

0 comments on commit e76ef5d

Please sign in to comment.