Skip to content

Commit 90b99e4

Browse files
authored
Merge pull request #57 from OpenWebconcept/feature/AS-21249
Feature/as 21249
2 parents 456da31 + 7641ee3 commit 90b99e4

File tree

7 files changed

+190
-57
lines changed

7 files changed

+190
-57
lines changed

openkaarten-base.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Plugin Name: OpenKaarten Base
1414
* Plugin URI: https://www.openwebconcept.nl
1515
* Description: The OpenKaarten Base plugin.
16-
* Version: 0.2.3
16+
* Version: 0.2.4
1717
* Author: Acato
1818
* Author URI: https://www.acato.nl
1919
* License: EUPL-1.2
@@ -27,7 +27,7 @@
2727
die;
2828
}
2929

30-
define( 'OWC_OPENKAARTEN_BASE_VERSION', '0.2.3' );
30+
define( 'OWC_OPENKAARTEN_BASE_VERSION', '0.2.4' );
3131

3232
if ( ! defined( 'OWC_OPENKAARTEN_BASE_ABSPATH' ) ) {
3333
define( 'OWC_OPENKAARTEN_BASE_ABSPATH', plugin_dir_path( __FILE__ ) );

src/admin/class-admin.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ private function __construct() {
5151
add_action( 'admin_enqueue_scripts', [ 'Openkaarten_Base_Plugin\Admin\Admin', 'admin_enqueue_scripts' ] );
5252
add_action( 'admin_notices', [ 'Openkaarten_Base_Plugin\Admin\Admin', 'admin_notices' ] );
5353
add_action( 'admin_init', [ 'Openkaarten_Base_Plugin\Admin\Admin', 'check_plugin_dependency' ] );
54+
add_action( 'admin_menu', [ 'Openkaarten_Base_Plugin\Admin\Admin', 'add_admin_menu' ], 50 );
55+
add_action( 'admin_init', [ 'Openkaarten_Base_Plugin\Admin\Admin', 'register_plugin_settings' ] );
5456
add_action( 'after_setup_theme', [ 'Openkaarten_Base_Plugin\Admin\Admin', 'after_setup_theme' ] );
5557

5658
add_action( 'manage_owc_ok_location_posts_custom_column', [ 'Openkaarten_Base_Plugin\Admin\Admin', 'location_posts_columns' ], 10, 2 );
@@ -200,6 +202,119 @@ public static function admin_notices() {
200202
}
201203
}
202204

205+
/**
206+
* This function is used to create the settings page for Owc_Openkaarten_Base_Plugin
207+
*
208+
* @return void
209+
*/
210+
public static function add_admin_menu() {
211+
add_options_page(
212+
__( 'OpenKaarten Settings', 'openkaarten-base' ),
213+
__( 'OpenKaarten', 'openkaarten-base' ),
214+
'manage_options',
215+
'openkaarten-base-settings',
216+
[ self::class, 'settings_page' ]
217+
);
218+
}
219+
220+
/**
221+
* This function is used to create the settings group
222+
*
223+
* @return void
224+
*/
225+
public static function register_plugin_settings() {
226+
$args = [
227+
'type' => 'float',
228+
'sanitize_callback' => 'floatval',
229+
];
230+
$args_lat = array_merge(
231+
$args,
232+
[
233+
'default' => 52.0, // Default to the Netherlands area.
234+
]
235+
);
236+
$args_lon = array_merge(
237+
$args,
238+
[
239+
'default' => 4.75, // Default to the Netherlands area.
240+
]
241+
);
242+
register_setting( 'openkaarten-base-settings-group', 'openkaarten_base_default_lat', $args_lat );
243+
register_setting( 'openkaarten-base-settings-group', 'openkaarten_base_default_lng', $args_lon );
244+
register_setting(
245+
'openkaarten-base-settings-group',
246+
'openkaarten_base_default_zoom',
247+
[
248+
'type' => 'integer',
249+
'sanitize_callback' => 'intval',
250+
'default' => 8,
251+
]
252+
);
253+
}
254+
255+
/**
256+
* This function add the html for the options page
257+
*
258+
* @return void
259+
*/
260+
public static function settings_page() {
261+
?>
262+
<div class="wrap">
263+
<h1><?php esc_html_e( 'OpenKaarten Base Settings', 'openkaarten-base' ); ?></h1>
264+
<form method="post" action="options.php">
265+
<?php settings_fields( 'openkaarten-base-settings-group' ); ?>
266+
<?php do_settings_sections( 'openkaarten-base-settings-group' ); ?>
267+
<table class="form-table">
268+
<tr valign="top">
269+
<th scope="row">
270+
<label for="openkaarten_base_default_lat"><?php esc_html_e( 'Default Latitude', 'openkaarten-base' ); ?></label>
271+
</th>
272+
<td>
273+
<input
274+
type="text"
275+
id="openkaarten_base_default_lat"
276+
name="openkaarten_base_default_lat"
277+
value="<?php echo esc_attr( get_option( 'openkaarten_base_default_lat', 52.0 ) ); ?>"
278+
/>
279+
<p class="description"><?php esc_html_e( 'Set the default latitude for maps.', 'openkaarten-base' ); ?></p>
280+
</td>
281+
</tr>
282+
<tr valign="top">
283+
<th scope="row">
284+
<label for="openkaarten_base_default_lng"><?php esc_html_e( 'Default Longitude', 'openkaarten-base' ); ?></label>
285+
</th>
286+
<td>
287+
<input
288+
type="text"
289+
id="openkaarten_base_default_lng"
290+
name="openkaarten_base_default_lng"
291+
value="<?php echo esc_attr( get_option( 'openkaarten_base_default_lng', 4.75 ) ); ?>"
292+
/>
293+
<p class="description"><?php esc_html_e( 'Set the default longitude for maps.', 'openkaarten-base' ); ?></p>
294+
</td>
295+
</tr>
296+
<tr valign="top">
297+
<th scope="row">
298+
<label for="openkaarten_base_default_zoom"><?php esc_html_e( 'Default Zoom Level', 'openkaarten-base' ); ?></label>
299+
</th>
300+
<td>
301+
<input
302+
type="number"
303+
id="openkaarten_base_default_zoom"
304+
name="openkaarten_base_default_zoom"
305+
value="<?php echo esc_attr( get_option( 'openkaarten_base_default_zoom', 8 ) ); ?>"
306+
min="0"
307+
max="19"
308+
/>
309+
<p class="description"><?php esc_html_e( 'Set the default zoom level for maps (0-19).', 'openkaarten-base' ); ?></p>
310+
</td>
311+
</table>
312+
<?php submit_button(); ?>
313+
</form>
314+
</div>
315+
<?php
316+
}
317+
203318
/**
204319
* Check if CMB2 plugin is installed and activated
205320
*

src/admin/class-cmb2.php

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,19 @@ public static function cmb2_render_openstreetmap_field_type( $field, $escaped_va
137137
return;
138138
}
139139

140+
// Set default min and max lat/lng.
140141
$min_lat = null;
141142
$max_lat = null;
142143
$min_long = null;
143144
$max_long = null;
144145

145-
$locations = [];
146+
// Set settings options for the map.
147+
$center_lat = get_option( 'openkaarten_base_default_lat', 52.0 );
148+
$center_long = get_option( 'openkaarten_base_default_lng', 4.75 );
149+
$default_zoom = get_option( 'openkaarten_base_default_zoom', 8 );
150+
151+
$locations = [];
152+
$fit_bounds = false;
146153

147154
try {
148155
// Parse the feature collection.
@@ -151,62 +158,65 @@ public static function cmb2_render_openstreetmap_field_type( $field, $escaped_va
151158
// Get the bounding box of the geometry.
152159
$bbox = $geom->getBBox();
153160

154-
// Set min and max values for the map.
155-
$min_lat = ( null === $min_lat || $bbox['miny'] < $min_lat ) ? $bbox['miny'] : $min_lat;
156-
$max_lat = ( null === $max_lat || $bbox['maxy'] > $max_lat ) ? $bbox['maxy'] : $max_lat;
157-
$min_long = ( null === $min_long || $bbox['minx'] < $min_long ) ? $bbox['minx'] : $min_long;
158-
$max_long = ( null === $max_long || $bbox['maxx'] > $max_long ) ? $bbox['maxx'] : $max_long;
159-
160-
// Get average lat and long for the center of the map.
161-
$center_lat = ( $min_lat + $max_lat ) / 2;
162-
$center_long = ( $min_long + $max_long ) / 2;
163-
164-
// Loop through all the components of the geometry and add them to the locations array with the right properties.
165-
foreach ( $geom->getComponents() as $component ) {
166-
// Use json output to plot the geometry on the map.
167-
$location_output = $component->out( 'json' );
168-
$location_output = json_decode( $location_output, true );
169-
170-
if ( isset( $location_output['geometry'] ) ) {
171-
$geometry = $location_output['geometry'];
172-
} else {
173-
$geometry = $location_output;
174-
}
175-
176-
$location = [
177-
'feature' => $geometry,
178-
'content' => '',
179-
'icon' => '',
180-
'color' => '',
181-
];
182-
183-
if ( ! empty( $location_output['properties'] ) ) {
184-
185-
// Get location properties.
186-
$location_properties = $location_output['properties'];
187-
if ( 'live' === $datalayer_url_type ) {
188-
$location_data_for_marker = $location_output['properties'];
161+
if ( ! empty( $bbox ) ) {
162+
// Set min and max values for the map.
163+
$min_lat = $bbox['miny'];
164+
$max_lat = $bbox['maxy'];
165+
$min_long = $bbox['minx'];
166+
$max_long = $bbox['maxx'];
167+
168+
// Get average lat and long for the center of the map.
169+
$center_lat = ( $min_lat + $max_lat ) / 2;
170+
$center_long = ( $min_long + $max_long ) / 2;
171+
172+
// Loop through all the components of the geometry and add them to the locations array with the right properties.
173+
foreach ( $geom->getComponents() as $component ) {
174+
// Use json output to plot the geometry on the map.
175+
$location_output = $component->out( 'json' );
176+
$location_output = json_decode( $location_output, true );
177+
178+
if ( isset( $location_output['geometry'] ) ) {
179+
$geometry = $location_output['geometry'];
189180
} else {
190-
$location_data_for_marker = $location_output;
181+
$geometry = $location_output;
191182
}
192183

193-
// Get marker information.
194-
$item_marker = Locations::get_location_marker( $object_id, false, $location_data_for_marker );
195-
$geom_marker = [
196-
'color' => $item_marker['color'],
197-
'icon' => Locations::get_location_marker_url( $item_marker['icon'] ),
184+
$location = [
185+
'feature' => $geometry,
186+
'content' => '',
187+
'icon' => '',
188+
'color' => '',
198189
];
199190

200-
// Get title information based on title field mapping.
201-
$title_fields = get_post_meta( $object_id, 'title_field_mapping', true );
202-
$title = Importer::create_title_from_mapping( $location_properties, $title_fields );
191+
if ( ! empty( $location_output['properties'] ) ) {
192+
193+
// Get location properties.
194+
$location_properties = $location_output['properties'];
195+
if ( 'live' === $datalayer_url_type ) {
196+
$location_data_for_marker = $location_output['properties'];
197+
} else {
198+
$location_data_for_marker = $location_output;
199+
}
200+
201+
// Get marker information.
202+
$item_marker = Locations::get_location_marker( $object_id, false, $location_data_for_marker );
203+
$geom_marker = [
204+
'color' => $item_marker['color'],
205+
'icon' => Locations::get_location_marker_url( $item_marker['icon'] ),
206+
];
207+
208+
// Get title information based on title field mapping.
209+
$title_fields = get_post_meta( $object_id, 'title_field_mapping', true );
210+
$title = Importer::create_title_from_mapping( $location_properties, $title_fields );
211+
212+
$location['content'] = $title;
213+
$location['icon'] = $geom_marker['icon'] ?? '';
214+
$location['color'] = $geom_marker['color'] ?? '';
215+
}
203216

204-
$location['content'] = $title;
205-
$location['icon'] = $geom_marker['icon'] ?? '';
206-
$location['color'] = $geom_marker['color'] ?? '';
217+
$locations[] = $location;
218+
$fit_bounds = true;
207219
}
208-
209-
$locations[] = $location;
210220
}
211221
} catch ( \Exception $e ) {
212222
// Add error message via admin notice.
@@ -226,8 +236,8 @@ public static function cmb2_render_openstreetmap_field_type( $field, $escaped_va
226236
'maxLong' => esc_attr( $max_long ),
227237
'centerLat' => esc_attr( $center_lat ),
228238
'centerLong' => esc_attr( $center_long ),
229-
'defaultZoom' => 10,
230-
'fitBounds' => true,
239+
'defaultZoom' => $default_zoom,
240+
'fitBounds' => $fit_bounds,
231241
]
232242
);
233243

src/admin/class-importer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,16 @@ public static function update_post_meta( $meta_id, $post_id, $meta_key, $meta_va
186186
}
187187

188188
// Force update the datalayer url, but without update_post_meta, because this will create an infinite loop.
189+
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom query is required here.
189190
$wpdb->update(
190191
$wpdb->postmeta,
191192
[
193+
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- We need to update the meta value.
192194
'meta_value' => $meta_value,
193195
],
194196
[
195197
'post_id' => $post_id,
198+
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- We need to update the meta key.
196199
'meta_key' => 'datalayer_url',
197200
]
198201
);

src/admin/class-locations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ public static function get_location_tooltip( $datalayer_id, $location_id = false
427427
if ( ! empty( $source_fields ) ) {
428428
foreach ( $source_fields as $source_field ) {
429429
// Include only fields that are set to show.
430-
if ( ! isset( $source_field['field_show'] ) || ! in_array( $source_field['field_show'], [ 1, 'on' ] ) ) {
430+
if ( ! isset( $source_field['field_show'] ) || ! in_array( $source_field['field_show'], [ 1, 'on' ], true ) ) {
431431
continue;
432432
}
433433

src/rest_api/class-openkaarten-controller.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ public function get_items( $request ) {
246246

247247
$response = [
248248
'type' => 'DatasetCollection',
249+
'settings' => [
250+
'default_lat' => get_option( 'openkaarten_base_default_lat' ),
251+
'default_lng' => get_option( 'openkaarten_base_default_lng' ),
252+
'default_zoom' => get_option( 'openkaarten_base_default_zoom' ),
253+
],
249254
'datasets' => $posts,
250255
'pagination' => [
251256
'total' => $posts_query->found_posts,

src/scripts/openstreetmap-base.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function initializeMap() {
3434
}
3535

3636
const map = new L.Map( 'map-base', {
37-
center: [config.centerY, config.centerX],
37+
center: [config.centerX, config.centerY],
3838
zoom: config.defaultZoom,
3939
minZoom: config.minimumZoom,
4040
maxZoom: config.maximumZoom,
@@ -57,7 +57,7 @@ function initializeMap() {
5757
} ).addTo( map );
5858

5959
// Add locations to the map as markers.
60-
if (locationItems.length !== 0) {
60+
if (locationItems.length > 0) {
6161
for (let i = 0; i < locationItems.length; i++) {
6262
const location = locationItems[i];
6363
const geojsonData = location.feature;

0 commit comments

Comments
 (0)