diff --git a/Public.php b/Public.php index 3ce8929..c4c7e30 100644 --- a/Public.php +++ b/Public.php @@ -7,6 +7,7 @@ if (!function_exists('open_street_map_render_blade_view')) { function open_street_map_render_blade_view($view, $data = [], $compress = true) { + $markup = ''; $componentLibrary = new ComponentLibraryInit([]); $bladeEngine = $componentLibrary->getEngine(); $data = array_merge($data, array('errorMessage' => false)); diff --git a/modularity-open-street-map.php b/modularity-open-street-map.php index 2e1a5d7..9f32577 100644 --- a/modularity-open-street-map.php +++ b/modularity-open-street-map.php @@ -4,7 +4,7 @@ * Plugin Name: Modularity Open Street Map * Plugin URI: https://github.com/NiclasNorin/modularity-open-street-map * Description: A map based of OSM. - * Version: 2.1.3 + * Version: 2.1.3 * Author: Niclas Norin * Author URI: https://github.com/NiclasNorin * License: MIT @@ -24,6 +24,9 @@ define('MODULARITYOPENSTREETMAP_VIEW_PATH', MODULARITYOPENSTREETMAP_PATH . 'views/'); define('MODULARITYOPENSTREETMAP_MODULE_VIEW_PATH', MODULARITYOPENSTREETMAP_PATH . 'source/php/Module/views'); +// Endpoint address +define('OSM_ENDPOINT', 'osm/v1/'); + require_once MODULARITYOPENSTREETMAP_PATH . 'Public.php'; // Register the autoloader diff --git a/package-lock.json b/package-lock.json index 619b5de..e40b992 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,6 +23,8 @@ "postcss-loader": "^6.2.1", "sass": "^1.49.7", "sass-loader": "^12.4.0", + "ts-loader": "^9.5.1", + "typescript": "^5.5.3", "webpack": "^5.68.0", "webpack-bundle-analyzer": "^4.5.0", "webpack-cli": "^4.9.2", @@ -1972,6 +1974,19 @@ "integrity": "sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==", "dev": true }, + "node_modules/micromatch": { + "version": "4.0.7", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.7.tgz", + "integrity": "sha512-LPP/3KorzCwBxfeUuZmaR6bG2kdeHSbe0P2tY3FLRU4vYrjYz5hI4QZwV0njUx3jeuKe67YukQ1LSPZBKDqO/Q==", + "dev": true, + "dependencies": { + "braces": "^3.0.3", + "picomatch": "^2.3.1" + }, + "engines": { + "node": ">=8.6" + } + }, "node_modules/mime-db": { "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", @@ -3396,6 +3411,109 @@ "node": ">=6" } }, + "node_modules/ts-loader": { + "version": "9.5.1", + "resolved": "https://registry.npmjs.org/ts-loader/-/ts-loader-9.5.1.tgz", + "integrity": "sha512-rNH3sK9kGZcH9dYzC7CewQm4NtxJTjSEVRJ2DyBZR7f8/wcta+iV44UPCXc5+nzDzivKtlzV6c9P4e+oFhDLYg==", + "dev": true, + "dependencies": { + "chalk": "^4.1.0", + "enhanced-resolve": "^5.0.0", + "micromatch": "^4.0.0", + "semver": "^7.3.4", + "source-map": "^0.7.4" + }, + "engines": { + "node": ">=12.0.0" + }, + "peerDependencies": { + "typescript": "*", + "webpack": "^5.0.0" + } + }, + "node_modules/ts-loader/node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dev": true, + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/ts-loader/node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dev": true, + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/ts-loader/node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/ts-loader/node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true + }, + "node_modules/ts-loader/node_modules/source-map": { + "version": "0.7.4", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.4.tgz", + "integrity": "sha512-l3BikUxvPOcn5E74dZiq5BGsTb5yEwhaTSzccU6t4sDOH8NWJCstKO5QT2CvtFoK6F0saL7p9xHAqHOlCPJygA==", + "dev": true, + "engines": { + "node": ">= 8" + } + }, + "node_modules/ts-loader/node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dev": true, + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/typescript": { + "version": "5.5.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.3.tgz", + "integrity": "sha512-/hreyEujaB0w76zKo6717l3L0o/qEUtRgdvUBvlkhoWeOVMjMuHNHk0BRBzikzuGDqNmPQbg5ifMEqsHLiIUcQ==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, "node_modules/undici-types": { "version": "5.26.5", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", diff --git a/package.json b/package.json index c2b1b16..4f70445 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,8 @@ "postcss-loader": "^6.2.1", "sass": "^1.49.7", "sass-loader": "^12.4.0", + "ts-loader": "^9.5.1", + "typescript": "^5.5.3", "webpack": "^5.68.0", "webpack-bundle-analyzer": "^4.5.0", "webpack-cli": "^4.9.2", @@ -58,4 +60,4 @@ "leaflet": "^1.9.3", "leaflet.markercluster": "^1.5.3" } -} \ No newline at end of file +} diff --git a/source/js/main.ts b/source/js/main.ts new file mode 100644 index 0000000..fd863f4 --- /dev/null +++ b/source/js/main.ts @@ -0,0 +1,47 @@ +import { StorageInterface } from "./storageInterface"; +import TaxonomySelectUpdater from "./taxonomySelectUpdater"; + +class Main { + constructor( + private acf: any, + private postTypeSelectFieldKey: string, + private filterSelectFieldKey: string, + private storageInstance: StorageInterface, + private taxonomySelectUpdaterInstance: TaxonomySelectUpdater + ) { + const postTypeSelect = document.querySelector(`[data-key="${this.postTypeSelectFieldKey}"] select`) as HTMLSelectElement|undefined; + + if (postTypeSelect) { + if (postTypeSelect.value) { + this.taxonomySelectUpdaterInstance.updateExistsingRows(postTypeSelect.value); + } + + this.listenForPostTypeChanges(postTypeSelect); + this.listenForFilterRowsAdded(postTypeSelect); + } + } + + private listenForPostTypeChanges(postTypeSelect: HTMLSelectElement): void { + postTypeSelect.addEventListener('change', (event) => { + const selectedValue = (event.target as HTMLSelectElement).value; + this.taxonomySelectUpdaterInstance.updateExistsingRows(selectedValue); + }); + } + + private listenForFilterRowsAdded(postTypeSelect: HTMLSelectElement): void + { + this.acf.addAction('append', ( jqueryEl: any ) => { + if (!jqueryEl.hasOwnProperty(0)) { + return; + } + + const selectField = jqueryEl[0].querySelector(`[data-key="${this.filterSelectFieldKey}"] select`); + + if (selectField) { + this.taxonomySelectUpdaterInstance.addSelectFieldOptions(selectField, 0, this.storageInstance.getTaxonomies(postTypeSelect.value), []); + } + }); + } +} + +export default Main; \ No newline at end of file diff --git a/source/js/modularity-open-street-map.js b/source/js/modularity-open-street-map.js index e69de29..51c9201 100644 --- a/source/js/modularity-open-street-map.js +++ b/source/js/modularity-open-street-map.js @@ -0,0 +1,31 @@ +import TaxonomySelectUpdater from "./taxonomySelectUpdater"; +import Storage from "./storage"; +import Main from "./main"; + +document.addEventListener('DOMContentLoaded', function() { + const filterSelectFieldKey = 'field_668d1cc80d853'; + const postTypeSelectFieldKey = 'field_642a8818a908c'; + + if (typeof acf !== 'undefined') { + const [taxonomies, selected] = getOsm(); + const storageInstance = new Storage(taxonomies, selected); + const taxonomySelectUpdaterInstance = new TaxonomySelectUpdater(filterSelectFieldKey, storageInstance); + new Main(acf, postTypeSelectFieldKey, filterSelectFieldKey, storageInstance, taxonomySelectUpdaterInstance); + } +}); + +function getOsm() { + let parsedData = {}; + try { + parsedData = JSON.parse(osm); + } catch (error) { + console.error('Error parsing OSM data', error); + } + + console.log(parsedData); + + return [ + parsedData.hasOwnProperty('taxonomies') ? parsedData.taxonomies : [], + parsedData.hasOwnProperty('selected') ? parsedData.selected : [] + ]; +} \ No newline at end of file diff --git a/source/js/storage.ts b/source/js/storage.ts new file mode 100644 index 0000000..9ee409d --- /dev/null +++ b/source/js/storage.ts @@ -0,0 +1,25 @@ +import { StorageInterface } from "./storageInterface"; +class Storage implements StorageInterface { + constructor(private taxonomies: { [key: string]: string[] }, private selected: { [key: string]: string[] }) { + console.log(this.selected); + } + + public getSelected(postType: string): string[] { + console.log(this.selected); + if (this.selected.hasOwnProperty(postType)) { + return this.selected[postType]; + } + + return []; + } + + public getTaxonomies(postType: string): string[] { + if (this.taxonomies.hasOwnProperty(postType)) { + return this.taxonomies[postType]; + } + + return []; + } +} + +export default Storage; \ No newline at end of file diff --git a/source/js/storageInterface.ts b/source/js/storageInterface.ts new file mode 100644 index 0000000..13b2b29 --- /dev/null +++ b/source/js/storageInterface.ts @@ -0,0 +1,4 @@ +export interface StorageInterface { + getSelected(postType: string): string[]; + getTaxonomies(postType: string): string[]; +} \ No newline at end of file diff --git a/source/js/taxonomySelectUpdater.ts b/source/js/taxonomySelectUpdater.ts new file mode 100644 index 0000000..446c78b --- /dev/null +++ b/source/js/taxonomySelectUpdater.ts @@ -0,0 +1,36 @@ +import { StorageInterface } from "./storageInterface"; +class TaxonomySelectUpdater { + + constructor( + private selectFieldKey: string, + private storage: StorageInterface + + ) {} + + public updateExistsingRows(postType: string): void { + const taxonomies = this.storage.getTaxonomies(postType); + const selectedValues = this.storage.getSelected(postType); + + [...document.querySelectorAll(`tr.acf-row:not(.acf-clone) [data-key="${this.selectFieldKey}"] select`)].forEach((selectField, index) => { + selectField.innerHTML = ""; + this.addSelectFieldOptions(selectField, index, taxonomies, selectedValues); + }); + } + + public addSelectFieldOptions( + selectField: Element, + index: number, + taxonomies: string[], + selectedValues: string[], + ): void { + for (const key in taxonomies) { + const option = document.createElement('option'); + option.selected = selectedValues[index] && selectedValues[index] === key ? true : false; + option.value = key; + option.text = taxonomies[key]; + selectField.appendChild(option); + } + } +} + +export default TaxonomySelectUpdater; \ No newline at end of file diff --git a/source/php/AcfFields/json/modularity-open-street-map-settings.json b/source/php/AcfFields/json/modularity-open-street-map-settings.json index b9ea016..4cd3d14 100644 --- a/source/php/AcfFields/json/modularity-open-street-map-settings.json +++ b/source/php/AcfFields/json/modularity-open-street-map-settings.json @@ -17,9 +17,8 @@ "id": "" }, "choices": { - "place": "Platser", - "infopoint": "Infopoints", - "test": "Test Post Type" + "place": "Places", + "news": "News" }, "default_value": "place", "return_format": "value", @@ -51,20 +50,39 @@ "default_value": [], "return_format": "value", "multiple": 1, + "allow_custom": 0, + "placeholder": "", "allow_null": 0, - "ui": 0, + "ui": 1, "ajax": 0, - "placeholder": "", - "allow_custom": 0, "search_placeholder": "" }, { - "key": "field_6424301cc4fd8", - "label": "Full width", - "name": "mod_osm_full_width", + "key": "field_644b51b53a332", + "label": "Starting point and zoom level", + "name": "map_start_values", + "aria-label": "", + "type": "google_map", + "instructions": "Click on the map to place a marker for the default center of the map. Use the plus- and minus-signs (+\/-) to set the default zoom level.", + "required": 0, + "conditional_logic": 0, + "wrapper": { + "width": "", + "class": "", + "id": "" + }, + "center_lat": "56.048767661222115", + "center_lng": "12.703459264197518", + "zoom": "", + "height": "" + }, + { + "key": "field_66a7398c8aa52", + "label": "Add sorting functionality", + "name": "mod_osm_sort", "aria-label": "", "type": "true_false", - "instructions": "", + "instructions": "Allows the user to sort the posts", "required": 0, "conditional_logic": 0, "wrapper": { @@ -74,68 +92,74 @@ }, "message": "", "default_value": 0, - "ui": 0, "ui_on_text": "", - "ui_off_text": "" + "ui_off_text": "", + "ui": 1 }, { - "key": "field_642434d17fdaa", - "label": "Post columns", - "name": "mod_osm_post_columns", + "key": "field_66a8961b184bd", + "label": "Order by", + "name": "mod_osm_order_by", "aria-label": "", "type": "select", - "instructions": "This does not affect inner blocks", + "instructions": "", "required": 0, "conditional_logic": 0, "wrapper": { - "width": "", + "width": "50", "class": "", "id": "" }, "choices": { - "grid-md-12": "1", - "grid-md-6": "2", - "grid-md-4": "3" + "date": "Date", + "title": "Title", + "random": "Random" }, - "default_value": "grid-md-12", + "default_value": "date", "return_format": "value", "multiple": 0, + "allow_custom": 0, + "search_placeholder": "", "allow_null": 0, - "ui": 0, + "ui": 1, "ajax": 0, - "placeholder": "", - "allow_custom": 0, - "search_placeholder": "" + "placeholder": "" }, { - "key": "field_6437f4c860c15", - "label": "Per page", - "name": "mod_osm_per_page", + "key": "field_66a896fa184be", + "label": "Order", + "name": "mod_osm_order", "aria-label": "", - "type": "number", + "type": "select", "instructions": "", "required": 0, "conditional_logic": 0, "wrapper": { - "width": "", + "width": "50", "class": "", "id": "" }, - "default_value": 8, - "min": "", - "max": "", - "placeholder": "", - "step": "", - "prepend": "", - "append": "" + "choices": { + "ASC": "Ascending", + "DESC": "Descending" + }, + "default_value": "DESC", + "return_format": "value", + "multiple": 0, + "allow_custom": 0, + "search_placeholder": "", + "allow_null": 0, + "ui": 1, + "ajax": 0, + "placeholder": "" }, { - "key": "field_644b51b53a332", - "label": "Starting point and zoom level", - "name": "map_start_values", + "key": "field_668d19163553d", + "label": "Filter", + "name": "mod_osm_filters", "aria-label": "", - "type": "google_map", - "instructions": "Click on the map to place a marker for the default center of the map. Use the plus- and minus-signs (+\/-) to set the default zoom level.", + "type": "repeater", + "instructions": "", "required": 0, "conditional_logic": 0, "wrapper": { @@ -143,10 +167,63 @@ "class": "", "id": "" }, - "center_lat": "56.048767661222115", - "center_lng": "12.703459264197518", - "zoom": "", - "height": "" + "acfe_repeater_stylised_button": 0, + "layout": "table", + "pagination": 0, + "min": 0, + "max": 0, + "collapsed": "", + "button_label": "Add Row", + "rows_per_page": 20, + "sub_fields": [ + { + "key": "field_668d1cb10d852", + "label": "Text", + "name": "mod_osm_filter_text", + "aria-label": "", + "type": "text", + "instructions": "", + "required": 0, + "conditional_logic": 0, + "wrapper": { + "width": "", + "class": "", + "id": "" + }, + "default_value": "", + "maxlength": "", + "placeholder": "", + "prepend": "", + "append": "", + "parent_repeater": "field_668d19163553d" + }, + { + "key": "field_668d1cc80d853", + "label": "select", + "name": "mod_osm_filter_taxonomy", + "aria-label": "", + "type": "select", + "instructions": "", + "required": 0, + "conditional_logic": 0, + "wrapper": { + "width": "", + "class": "", + "id": "" + }, + "choices": [], + "default_value": false, + "return_format": "value", + "multiple": 0, + "allow_null": 0, + "ui": 0, + "ajax": 0, + "placeholder": "", + "allow_custom": 0, + "search_placeholder": "", + "parent_repeater": "field_668d19163553d" + } + ] } ], "location": [ diff --git a/source/php/AcfFields/php/modularity-open-street-map-settings.php b/source/php/AcfFields/php/modularity-open-street-map-settings.php index 37a3afa..3771129 100644 --- a/source/php/AcfFields/php/modularity-open-street-map-settings.php +++ b/source/php/AcfFields/php/modularity-open-street-map-settings.php @@ -20,9 +20,8 @@ 'id' => '', ), 'choices' => array( - 'place' => __('Platser', 'modularity-open-street-map'), - 'infopoint' => __('Infopoints', 'modularity-open-street-map'), - 'test' => __('Test Post Type', 'modularity-open-street-map'), + 'place' => __('Places', 'modularity-open-street-map'), + 'news' => __('News', 'modularity-open-street-map'), ), 'default_value' => __('place', 'modularity-open-street-map'), 'return_format' => 'value', @@ -55,20 +54,39 @@ ), 'return_format' => 'value', 'multiple' => 1, + 'allow_custom' => 0, + 'placeholder' => '', 'allow_null' => 0, - 'ui' => 0, + 'ui' => 1, 'ajax' => 0, - 'placeholder' => '', - 'allow_custom' => 0, 'search_placeholder' => '', ), 2 => array( - 'key' => 'field_6424301cc4fd8', - 'label' => __('Full width', 'modularity-open-street-map'), - 'name' => 'mod_osm_full_width', + 'key' => 'field_644b51b53a332', + 'label' => __('Starting point and zoom level', 'modularity-open-street-map'), + 'name' => 'map_start_values', + 'aria-label' => '', + 'type' => 'google_map', + 'instructions' => __('Click on the map to place a marker for the default center of the map. Use the plus- and minus-signs (+/-) to set the default zoom level.', 'modularity-open-street-map'), + 'required' => 0, + 'conditional_logic' => 0, + 'wrapper' => array( + 'width' => '', + 'class' => '', + 'id' => '', + ), + 'center_lat' => '56.048767661222115', + 'center_lng' => '12.703459264197518', + 'zoom' => '', + 'height' => '', + ), + 3 => array( + 'key' => 'field_66a7398c8aa52', + 'label' => __('Add sorting functionality', 'modularity-open-street-map'), + 'name' => 'mod_osm_sort', 'aria-label' => '', 'type' => 'true_false', - 'instructions' => '', + 'instructions' => __('Allows the user to sort the posts', 'modularity-open-street-map'), 'required' => 0, 'conditional_logic' => 0, 'wrapper' => array( @@ -78,68 +96,74 @@ ), 'message' => '', 'default_value' => 0, - 'ui' => 0, 'ui_on_text' => '', 'ui_off_text' => '', + 'ui' => 1, ), - 3 => array( - 'key' => 'field_642434d17fdaa', - 'label' => __('Post columns', 'modularity-open-street-map'), - 'name' => 'mod_osm_post_columns', + 4 => array( + 'key' => 'field_66a8961b184bd', + 'label' => __('Order by', 'modularity-open-street-map'), + 'name' => 'mod_osm_order_by', 'aria-label' => '', 'type' => 'select', - 'instructions' => __('This does not affect inner blocks', 'modularity-open-street-map'), + 'instructions' => '', 'required' => 0, 'conditional_logic' => 0, 'wrapper' => array( - 'width' => '', + 'width' => '50', 'class' => '', 'id' => '', ), 'choices' => array( - 'grid-md-12' => __('1', 'modularity-open-street-map'), - 'grid-md-6' => __('2', 'modularity-open-street-map'), - 'grid-md-4' => __('3', 'modularity-open-street-map'), + 'date' => __('Date', 'modularity-open-street-map'), + 'title' => __('Title', 'modularity-open-street-map'), + 'random' => __('Random', 'modularity-open-street-map'), ), - 'default_value' => __('grid-md-12', 'modularity-open-street-map'), + 'default_value' => __('date', 'modularity-open-street-map'), 'return_format' => 'value', 'multiple' => 0, + 'allow_custom' => 0, + 'search_placeholder' => '', 'allow_null' => 0, - 'ui' => 0, + 'ui' => 1, 'ajax' => 0, 'placeholder' => '', - 'allow_custom' => 0, - 'search_placeholder' => '', ), - 4 => array( - 'key' => 'field_6437f4c860c15', - 'label' => __('Per page', 'modularity-open-street-map'), - 'name' => 'mod_osm_per_page', + 5 => array( + 'key' => 'field_66a896fa184be', + 'label' => __('Order', 'modularity-open-street-map'), + 'name' => 'mod_osm_order', 'aria-label' => '', - 'type' => 'number', + 'type' => 'select', 'instructions' => '', 'required' => 0, 'conditional_logic' => 0, 'wrapper' => array( - 'width' => '', + 'width' => '50', 'class' => '', 'id' => '', ), - 'default_value' => 8, - 'min' => '', - 'max' => '', + 'choices' => array( + 'ASC' => __('Ascending', 'modularity-open-street-map'), + 'DESC' => __('Descending', 'modularity-open-street-map'), + ), + 'default_value' => __('DESC', 'modularity-open-street-map'), + 'return_format' => 'value', + 'multiple' => 0, + 'allow_custom' => 0, + 'search_placeholder' => '', + 'allow_null' => 0, + 'ui' => 1, + 'ajax' => 0, 'placeholder' => '', - 'step' => '', - 'prepend' => '', - 'append' => '', ), - 5 => array( - 'key' => 'field_644b51b53a332', - 'label' => __('Starting point and zoom level', 'modularity-open-street-map'), - 'name' => 'map_start_values', + 6 => array( + 'key' => 'field_668d19163553d', + 'label' => __('Filter', 'modularity-open-street-map'), + 'name' => 'mod_osm_filters', 'aria-label' => '', - 'type' => 'google_map', - 'instructions' => __('Click on the map to place a marker for the default center of the map. Use the plus- and minus-signs (+/-) to set the default zoom level.', 'modularity-open-street-map'), + 'type' => 'repeater', + 'instructions' => '', 'required' => 0, 'conditional_logic' => 0, 'wrapper' => array( @@ -147,10 +171,64 @@ 'class' => '', 'id' => '', ), - 'center_lat' => '56.048767661222115', - 'center_lng' => '12.703459264197518', - 'zoom' => '', - 'height' => '', + 'acfe_repeater_stylised_button' => 0, + 'layout' => 'table', + 'pagination' => 0, + 'min' => 0, + 'max' => 0, + 'collapsed' => '', + 'button_label' => __('Add Row', 'modularity-open-street-map'), + 'rows_per_page' => 20, + 'sub_fields' => array( + 0 => array( + 'key' => 'field_668d1cb10d852', + 'label' => __('Text', 'modularity-open-street-map'), + 'name' => 'mod_osm_filter_text', + 'aria-label' => '', + 'type' => 'text', + 'instructions' => '', + 'required' => 0, + 'conditional_logic' => 0, + 'wrapper' => array( + 'width' => '', + 'class' => '', + 'id' => '', + ), + 'default_value' => '', + 'maxlength' => '', + 'placeholder' => '', + 'prepend' => '', + 'append' => '', + 'parent_repeater' => 'field_668d19163553d', + ), + 1 => array( + 'key' => 'field_668d1cc80d853', + 'label' => __('select', 'modularity-open-street-map'), + 'name' => 'mod_osm_filter_taxonomy', + 'aria-label' => '', + 'type' => 'select', + 'instructions' => '', + 'required' => 0, + 'conditional_logic' => 0, + 'wrapper' => array( + 'width' => '', + 'class' => '', + 'id' => '', + ), + 'choices' => array( + ), + 'default_value' => false, + 'return_format' => 'value', + 'multiple' => 0, + 'allow_null' => 0, + 'ui' => 0, + 'ajax' => 0, + 'placeholder' => '', + 'allow_custom' => 0, + 'search_placeholder' => '', + 'parent_repeater' => 'field_668d19163553d', + ), + ), ), ), 'location' => array( diff --git a/source/php/Api/OsmEndpoint.php b/source/php/Api/OsmEndpoint.php new file mode 100644 index 0000000..72da234 --- /dev/null +++ b/source/php/Api/OsmEndpoint.php @@ -0,0 +1,47 @@ +[a-zA-Z0-9-]+)'; + private SettingsInterface|null $settings = null; + + public function handleRegisterRestRoute(): bool { + return register_rest_route(OSM_ENDPOINT, self::ROUTE, array( + 'methods' => 'GET', + 'callback' => array($this, 'handleRequest'), + 'permission_callback' => '__return_true' + )); + } + + public function handleRequest(WP_REST_Request $request) + { + $this->settings = new Settings($request->get_params()); + + if (!$this->settings->getPostType()) { + return new WP_REST_Response(null, 400); + } + + $argsInstance = new OsmQueryArgsCreator($this->settings); + $posts = (new OsmGetPosts($argsInstance->CreateQueryArgs(), $this->settings))->getPosts(); + $postsHandlerInstance = new OsmTransformationHandler( + $posts, + $this->settings, + new DefaultTransformer($this->settings), + new HtmlTransformer($this->settings) + ); + + return new WP_REST_Response($postsHandlerInstance->getTransformedPosts(), 200); + } +} \ No newline at end of file diff --git a/source/php/Api/OsmGetPosts.php b/source/php/Api/OsmGetPosts.php new file mode 100644 index 0000000..658795d --- /dev/null +++ b/source/php/Api/OsmGetPosts.php @@ -0,0 +1,20 @@ +args); + $posts = $query->posts ?? []; + + if (!empty($posts) && $this->settings->getRandomize()) { + shuffle($posts); + } + + return $posts; + } +} \ No newline at end of file diff --git a/source/php/Api/OsmQueryArgsCreator.php b/source/php/Api/OsmQueryArgsCreator.php new file mode 100644 index 0000000..1ea43e9 --- /dev/null +++ b/source/php/Api/OsmQueryArgsCreator.php @@ -0,0 +1,45 @@ + $this->settings->getPostType(), + 'posts_per_page' => $this->settings->getPostsPerPage(), + 'paged' => $this->settings->getPage(), + 'post_status' => 'publish', + 'tax_query' => $this->createTaxQuery(), + 'orderby' => $this->settings->getOrderBy(), + 'order' => $this->settings->getOrder() + ]; + } + + private function createTaxQuery(): array + { + $taxonomies = $this->settings->getTaxonomies(); + + if (empty($taxonomies)) { + return []; + } + + $taxQuery = [ + 'relation' => 'OR', + ]; + + foreach ($taxonomies as $taxonomy => $termsString) { + $taxQuery[] = [ + 'taxonomy' => $taxonomy, + 'field' => 'term_id', + 'terms' => explode(',', $termsString) + ]; + } + + return $taxQuery; + } +} \ No newline at end of file diff --git a/source/php/Api/OsmTransformationHandler.php b/source/php/Api/OsmTransformationHandler.php new file mode 100644 index 0000000..ae735df --- /dev/null +++ b/source/php/Api/OsmTransformationHandler.php @@ -0,0 +1,46 @@ +cacheKey = $settings->getHtml() ? 'osm_html' : 'osm_post'; + } + + public function getTransformedPosts(): array + { + if (empty($this->posts)) { + return []; + } + + $transformedPosts = []; + foreach ($this->posts as $post) { + $id = $post->ID; + wp_cache_flush(); + $cachedPost = wp_cache_get($id, $this->cacheKey); + + if (!$cachedPost) { + $post = $this->default->transform($post); + $post = $this->html->transform($post); + + wp_cache_set($id, $post, $this->cacheKey, 7 * \DAY_IN_SECONDS); + $cachedPost = $post; + } + + $transformedPosts[] = $cachedPost; + } + + return $transformedPosts; + } +} \ No newline at end of file diff --git a/source/php/Api/PostTransformer/DefaultTransformer.php b/source/php/Api/PostTransformer/DefaultTransformer.php new file mode 100644 index 0000000..a2e3b71 --- /dev/null +++ b/source/php/Api/PostTransformer/DefaultTransformer.php @@ -0,0 +1,37 @@ +osmFilterValues = $this->addOsmFilteringCapabilities($post); + + return $post; + } + + private function addOsmFilteringCapabilities($post): string + { + $filterTermsStructure = []; + $attributeString = ""; + + if (!empty($post->termsUnlinked)) { + foreach ($post->termsUnlinked as $term) { + $filterTermsStructure[$term['taxonomy']][] = $term['slug']; + } + + foreach ($filterTermsStructure as $taxonomy => $term) { + $attributeString .= ' osm-filter-item-' . $taxonomy . '=' . implode(',', $term); + } + } + + return $attributeString; + } +} \ No newline at end of file diff --git a/source/php/Api/PostTransformer/HtmlTransformer.php b/source/php/Api/PostTransformer/HtmlTransformer.php new file mode 100644 index 0000000..64dc487 --- /dev/null +++ b/source/php/Api/PostTransformer/HtmlTransformer.php @@ -0,0 +1,19 @@ +settings->getHtml()) { + return $post; + } + + return open_street_map_render_blade_view('html', ['place' => $post]); + } +} \ No newline at end of file diff --git a/source/php/Api/PostTransformer/PostTransformerInterface.php b/source/php/Api/PostTransformer/PostTransformerInterface.php new file mode 100644 index 0000000..ae471be --- /dev/null +++ b/source/php/Api/PostTransformer/PostTransformerInterface.php @@ -0,0 +1,8 @@ +postType = isset($settings['postType']) ? $settings['postType'] : $this->postType; + $this->page = isset($settings['page']) ? intval($settings['page']) : $this->page; + $this->postsPerPage = isset($settings['postsPerPage']) ? intval($settings['postsPerPage']) : $this->postsPerPage; + $this->taxonomies = isset($settings['taxonomies']) ? $settings['taxonomies'] : $this->taxonomies; + $this->html = isset($settings['html']) ? true : $this->html; + $this->order = isset($settings['order']) ? $settings['order'] : $this->order; + $this->orderBy = isset($settings['orderBy']) ? $settings['orderBy'] : $this->orderBy; + $this->randomize = isset($settings['randomize']) ? true : false; + } + + public function getPostType(): ?string + { + return $this->postType; + } + + public function getPage(): int + { + return $this->page; + } + + public function getPostsPerPage(): int + { + return $this->postsPerPage; + } + + public function getTaxonomies(): array + { + return $this->taxonomies; + } + + public function getHtml(): bool + { + return $this->html; + } + + public function getOrder(): string + { + return $this->order; + } + + public function getOrderBy(): string + { + return $this->orderBy; + } + + public function getRandomize(): bool + { + return $this->randomize; + } +} \ No newline at end of file diff --git a/source/php/Api/SettingsInterface.php b/source/php/Api/SettingsInterface.php new file mode 100644 index 0000000..b23de4c --- /dev/null +++ b/source/php/Api/SettingsInterface.php @@ -0,0 +1,14 @@ +getPlacePostTypeInstance = new GetPlacePostType(); + $this->getTaxonomiesInstance = new GetTaxonomies($this->getPlacePostTypeInstance); + $this->getSelectedTaxonomiesInstance = new GetSelectedTaxonomies(); + + + RestApiEndpointsRegistry::add(new \ModularityOpenStreetMap\Api\OsmEndpoint()); $this->cacheBust = new \ModularityOpenStreetMap\Helper\CacheBust(); } public function postTypes($field) { - $postTypes = get_post_types(); - $arr = []; - foreach ($postTypes as $postType) { - $contentType = \Municipio\Helper\ContentType::getContentType($postType); - if (is_object($contentType) && $contentType->getKey() == 'place') { - $postTypeObject = get_post_type_object($postType); - $arr[$postTypeObject->name] = $postTypeObject->label; - } - } - reset($arr); - $first_key = key($arr); - $field['default_value'] = $first_key; + $postTypes = $this->getPlacePostTypeInstance->getPlacePostTypes(); + + $field['default_value'] = key($postTypes); + $field['choices'] = $postTypes; - $field['choices'] = $arr; return $field; } public function termsToShow($field) { $postType = get_field('mod_osm_post_type'); - $arr = TaxonomiesHelper::getTerms($postType); + $taxonomies = $this->getTaxonomiesInstance->getTaxonomiesFromPostTypeArchive($postType); - if (empty($arr)) { - $arr = ['none' => 'No post found']; + if (empty($taxonomies)) { + $taxonomies = ['none' => 'No post found']; } - $field['choices'] = $arr; + $mergedTerms = []; + foreach ($taxonomies as $slug => $label) { + $mergedTerms += $this->getTaxonomiesInstance->getAllTermsFromTaxonomy($slug, 'id'); + + } + + $field['choices'] = $mergedTerms; return $field; } - public function handleFullWidthField($field) + public function enqueueBackend() { - if (get_post_type() == 'page') { - return false; - } - return $field; + $placeTaxonomies = $this->getTaxonomiesInstance->getAllTaxonomiesForAllPlacePostTypes($this->getPlacePostTypeInstance->getPlacePostTypes()); + + $selected = $this->getSelectedTaxonomiesInstance->getSelectedTaxonomies(); + + wp_register_script( + 'modularity-open-street-map-js', + MODULARITYOPENSTREETMAP_URL . '/dist/' . + $this->cacheBust->name('js/modularity-open-street-map.js'), + ['jquery', 'acf-input'] + ); + + wp_localize_script( + 'modularity-open-street-map-js', + 'osm', + json_encode(['taxonomies' => $placeTaxonomies, 'selected' => $selected]) + ); + + + wp_enqueue_script('modularity-open-street-map-js'); } /** @@ -78,14 +99,6 @@ public function enqueueFrontend() ); wp_enqueue_style('modularity-open-street-map-css'); - - wp_register_script( - 'modularity-open-street-map-js', - MODULARITYOPENSTREETMAP_URL . '/dist/' . - $this->cacheBust->name('js/modularity-open-street-map.js') - ); - - wp_enqueue_script('modularity-open-street-map-js'); } /** diff --git a/source/php/Decorator/EndpointDecoratorInterface.php b/source/php/Decorator/EndpointDecoratorInterface.php new file mode 100644 index 0000000..31cb550 --- /dev/null +++ b/source/php/Decorator/EndpointDecoratorInterface.php @@ -0,0 +1,7 @@ +getRandomizedOrder()); + } + + private function getRandomizedOrder(): string + { + $possibleOrders = ['ASC', 'DESC']; + $randomOrderKey = array_rand($possibleOrders); + + return $possibleOrders[$randomOrderKey]; + } +} \ No newline at end of file diff --git a/source/php/Decorator/EndpointOrderBy.php b/source/php/Decorator/EndpointOrderBy.php new file mode 100644 index 0000000..a9f4f5d --- /dev/null +++ b/source/php/Decorator/EndpointOrderBy.php @@ -0,0 +1,24 @@ +getRandomizedOrderBy()); + } + + private function getRandomizedOrderBy(): string + { + $possibleOrderBys = ['title', 'date', 'modified', 'ID', 'author']; + $randomOrderByKey = array_rand($possibleOrderBys); + + return $possibleOrderBys[$randomOrderByKey] . '&randomize=true'; + } +} \ No newline at end of file diff --git a/source/php/Decorator/EndpointPostType.php b/source/php/Decorator/EndpointPostType.php new file mode 100644 index 0000000..3fafe7f --- /dev/null +++ b/source/php/Decorator/EndpointPostType.php @@ -0,0 +1,16 @@ +' . print_r( $termsToShow, true ) . '';die; + if (empty($termsToShow)) { + return $endpoint; + } + + $taxonomyToShow = []; + foreach ($termsToShow as $termId) { + $taxonomy = get_term($termId)->taxonomy; + $taxonomyToShow[$taxonomy][] = $termId; + } + + foreach ($taxonomyToShow as $taxonomy => $terms) { + $endpoint .= '&' . 'taxonomies' . '[' . $taxonomy . ']=' . implode(',', $terms); + } + + return $endpoint; + } +} \ No newline at end of file diff --git a/source/php/Helper/GetPlacePostType.php b/source/php/Helper/GetPlacePostType.php new file mode 100644 index 0000000..81c02ed --- /dev/null +++ b/source/php/Helper/GetPlacePostType.php @@ -0,0 +1,37 @@ +placePostTypes) ? $this->placePostTypes : $this->filterPostTypes(); + + return $postTypes; + } + + private function filterPostTypes(): array + { + $postTypes = get_post_types(); + if (empty($postTypes)) { + return []; + } + + $filteredPostTypes = []; + foreach ($postTypes as $postType) { + $contentType = \Municipio\Helper\ContentType::getContentType($postType); + if (is_object($contentType) && $contentType->getKey() == 'place') { + $postTypeObject = get_post_type_object($postType); + $filteredPostTypes[$postTypeObject->name] = $postTypeObject->label; + } + } + + $this->placePostTypes = $filteredPostTypes; + return $filteredPostTypes; + } + +} diff --git a/source/php/Helper/GetSelectedTaxonomies.php b/source/php/Helper/GetSelectedTaxonomies.php new file mode 100644 index 0000000..8601227 --- /dev/null +++ b/source/php/Helper/GetSelectedTaxonomies.php @@ -0,0 +1,23 @@ +postTypesTaxonomies)) { + return $this->postTypesTaxonomies; + } + + foreach ($postTypes as $slug => $label) { + $this->postTypesTaxonomies[$slug] = $this->getTaxonomiesFromPostTypeArchive($slug); + + } + + return $this->postTypesTaxonomies; + } + + public function getTaxonomiesFromPostTypeArchive(string $postType): array + { + $activeTaxonomiesForPostType = get_theme_mod('archive_' . $postType . '_taxonomies_to_display', []); + $taxonomies = $this->getTaxonomiesFromSlug($activeTaxonomiesForPostType); + + return $taxonomies; + } + + private function getTaxonomiesFromSlug(array $activeTaxonomiesForPostType): array + { + $arr = []; + if (empty($activeTaxonomiesForPostType)) { + return $arr; + } + + foreach ($activeTaxonomiesForPostType as $taxonomy) { + $taxonomyObject = get_taxonomy($taxonomy); + $arr[$taxonomyObject->name] = $taxonomyObject->label; + } + + return $arr; + } + + public function getAllTermsFromPostTypeArchiveTaxonomies(string $postType) + { + $placePostTypesTaxonomies = $this->getAllTaxonomiesForAllPlacePostTypes($this->getPlacePostTypeInstance->getPlacePostTypes()); + + if (empty($placePostTypesTaxonomies[$postType])) { + return []; + } + + $terms = []; + foreach ($placePostTypesTaxonomies[$postType] as $slug => $label) { + $terms[$slug] = $this->getAllTermsFromTaxonomy($slug); + } + + return $terms; + } + + public function getAllTermsFromTaxonomy(string $taxonomy, string $output = 'name'): array + { + $filteredTerms = []; + + $terms = get_terms([ + 'taxonomy' => $taxonomy, + 'hide_empty' => true, + ]); + + + if (!empty($terms)) { + if ($output === 'object') { + return $terms; + } + + foreach($terms as $term) { + $filteredTerms[$output === 'id' ? $term->term_id : $term->slug] = $term->name; + } + } + + return $filteredTerms; + } + +} \ No newline at end of file diff --git a/source/php/Helper/Taxonomies.php b/source/php/Helper/Taxonomies.php deleted file mode 100644 index 7035fd8..0000000 --- a/source/php/Helper/Taxonomies.php +++ /dev/null @@ -1,26 +0,0 @@ - $taxonomy, - 'hide_empty' => false - ]); - - foreach ($terms as $term) { - if(empty($term->parent)) { - $arr[$term->term_id] = $term->name; - } - } - } - - return $arr; - } -} \ No newline at end of file diff --git a/source/php/Module/CreateFilters.php b/source/php/Module/CreateFilters.php new file mode 100644 index 0000000..89b9b76 --- /dev/null +++ b/source/php/Module/CreateFilters.php @@ -0,0 +1,40 @@ +getTermsFromTaxonomy($filter['mod_osm_filter_taxonomy']); + + if ($terms) { + $filters[] = [ + 'label' => $filter['mod_osm_filter_text'] ?? $filterByLang . $filter['mod_osm_filter_taxonomy'], + 'taxonomy' => $filter['mod_osm_filter_taxonomy'], + 'terms' => $terms + ]; + } + } + + return $filters; + } + + private function getTermsFromTaxonomy(string $taxonomy): array + { + return $this->getTaxonomiesInstance->getAllTermsFromTaxonomy($taxonomy); + } +} \ No newline at end of file diff --git a/source/php/Module/OpenStreetMap.php b/source/php/Module/OpenStreetMap.php index b7dbaae..771cf47 100644 --- a/source/php/Module/OpenStreetMap.php +++ b/source/php/Module/OpenStreetMap.php @@ -2,6 +2,15 @@ namespace ModularityOpenStreetMap\Module; +use ModularityOpenStreetMap\Helper\GetTaxonomies as GetTaxonomies; +use ModularityOpenStreetMap\Helper\GetPlacePostType as GetPlacePostType; +use ModularityOpenStreetMap\Decorator\EndpointDecoratorInterface as EndpointDecoratorInterface; +use ModularityOpenStreetMap\Decorator\EndpointOrder as EndpointOrder; +use ModularityOpenStreetMap\Decorator\EndpointPostType as EndpointPostType; +use ModularityOpenStreetMap\Decorator\EndpointTaxonomies as EndpointTaxonomies; +use ModularityOpenStreetMap\Decorator\EndpointOrderBy as EndpointOrderBy; +use ModularityOpenStreetMap\Module\CreateFilters as CreateFilters; + class OpenStreetMap extends \Modularity\Module { public $slug = 'open-street-map'; @@ -11,6 +20,14 @@ class OpenStreetMap extends \Modularity\Module 'mode' => false ); + private GetTaxonomies $getTaxonomiesInstance; + private GetPlacePostType $getPlacePostTypeInstance; + private CreateFilters $createFiltersInstance; + private EndpointDecoratorInterface $endpointOrder; + private EndpointDecoratorInterface $endpointOrderBy; + private EndpointDecoratorInterface $endpointPostType; + private EndpointDecoratorInterface $endpointTaxonomies; + public function init() { //Define module @@ -18,28 +35,21 @@ public function init() $this->namePlural = __("OpenStreetMaps", 'modularity-open-street-map'); $this->description = __("Outputs a map.", 'modularity-open-street-map'); + $this->getPlacePostTypeInstance = new GetPlacePostType(); + $this->getTaxonomiesInstance = new GetTaxonomies($this->getPlacePostTypeInstance); + $this->createFiltersInstance = new CreateFilters($this->getTaxonomiesInstance); + + $this->endpointOrder = new EndpointOrder(); + $this->endpointOrderBy = new EndpointOrderBy(); + $this->endpointPostType = new EndpointPostType(); + $this->endpointTaxonomies = new EndpointTaxonomies(); + add_filter('Modularity/Block/Settings', function ($blockSettings, $slug) { if ($slug == $this->slug) { $blockSettings['mode'] = 'edit'; } return $blockSettings; }, 10, 2); - - add_filter('wpPageForTerm/secondaryQueryArgs', array($this, 'setPostsPerPage'), 10, 1); - add_filter('Municipio/Controller/Singular/displaySecondaryQuery', array($this, 'replaceArchivePosts'), 10, 1); - } - - public function setPostsPerPage($secondaryQueryArgs) { - if ($this->hasModule()) { - $secondaryQueryArgs['posts_per_page'] = 999; - } - - return $secondaryQueryArgs; - } - - public function replaceArchivePosts($item) - { - return !$this->hasModule(); } /** @@ -51,79 +61,70 @@ public function data(): array $fields = get_fields($this->ID); $data['ID'] = !empty($this->ID) ? $this->ID : uniqid(); - $termsToShow = $fields['mod_osm_terms_to_show']; - $postTypeToShow = $fields['mod_osm_post_type']; - $taxonomyToShow = []; - - foreach ($termsToShow as $term) { - $taxonomy = get_term($term)->taxonomy; - $taxonomyToShow[$taxonomy][] = $term; - } - $places = $this->getPlacePosts($termsToShow, $taxonomyToShow, $postTypeToShow); - - $data['isFullWidth'] = $fields['mod_osm_full_width'] ?? false; - $data['places'] = $places; $data['mapStyle'] = $this->getMapStyle(); - $data['perPage'] = !empty($fields['mod_osm_per_page']) ? $fields['mod_osm_per_page'] : 8; + $data['endpoint'] = $this->createEndpoint($fields); + $data['startPosition'] = $this->getStartPosition($fields['map_start_values'] ?? []); + $data['lang'] = [ + 'noPostsFound' => __('No posts were found.', 'modularity-open-street-map'), + 'filterBy' => __('Filter by', 'modularity-open-street-map'), + 'descending' => __('Descending', 'modularity-open-street-map'), + 'ascending' => __('Ascending', 'modularity-open-street-map'), + 'randomized' => __('Randomized', 'modularity-open-street-map'), + 'sort' => __('Sort', 'modularity-open-street-map'), + ]; - $mapStartValues = $fields['map_start_values'] ?? []; - $data['startPosition'] = []; - if (!empty($mapStartValues)) { - foreach ($mapStartValues as $key => $value) { - $data['startPosition'][$key] = $value; - } - $data['startPosition'] = $data['startPosition']; - } else { - $data['startPosition'] = [ - 'lat' => '56.046029', - 'lng' => '12.693904', - 'zoom' => '14' - ]; - } + $data['sort'] = !empty($fields['mod_osm_sort']); + $data['filters'] = $this->createFiltersInstance->create($fields, $data['lang']['filterBy']); return $data; } - private function getMapStyle() + /** + * Creates the endpoint URL for retrieving OpenStreetMap data based on the specified post type and terms. + * + * @param string $postTypeToShow The post type to show in the endpoint URL. + * @param array $termsToShow An array of term IDs to filter the results by. + * @return string The generated endpoint URL. + */ + private function createEndpoint($fields): string { - if (function_exists('get_theme_mod')) { - return get_theme_mod('osm_map_style', 'default'); - } else { - return 'default'; - } + $endpoint = rest_url(OSM_ENDPOINT); + $endpoint = $this->endpointPostType->decorate($endpoint, $fields); + $endpoint = $this->endpointTaxonomies->decorate($endpoint, $fields); + $endpoint = $this->endpointOrder->decorate($endpoint, $fields); + $endpoint = $this->endpointOrderBy->decorate($endpoint, $fields); + + return $endpoint; } - private function getPlacePosts($termsToShow, $taxonomyToShow, $postTypeToShow) + /** + * Retrieves the start position for the OpenStreetMap module. + * + * If the $mapStartValues array is empty, the default start position is returned. + * + * @param array $mapStartValues An array containing the latitude, longitude, and zoom level for the start position. + * @return array The start position for the OpenStreetMap module. + */ + private function getStartPosition(array $mapStartValues) { - $args = [ - 'post_type' => $postTypeToShow, - 'posts_per_page' => 999, - 'tax_query' => [ - 'relation' => 'OR', - ] - ]; - - foreach ($taxonomyToShow as $taxonomy => $terms) { - $args['tax_query'][] = [ - 'taxonomy' => $taxonomy, - 'field' => 'term_id', - 'terms' => $terms + if (empty($mapStartValues)) { + return [ + 'lat' => '56.046029', + 'lng' => '12.693904', + 'zoom' => '14' ]; } - $posts = get_posts($args); - - return $this->buildPlacePosts($posts); + return $mapStartValues; } - private function buildPlacePosts($posts) + private function getMapStyle() { - foreach ($posts as &$post) { - $post = \Municipio\Helper\ContentType::complementPlacePost($post); - $post = \Municipio\Helper\Post::preparePostObject($post); + if (function_exists('get_theme_mod')) { + return get_theme_mod('osm_map_style', 'default'); + } else { + return 'default'; } - - return $posts; } public function template(): string diff --git a/source/php/Module/views/open-street-map.blade.php b/source/php/Module/views/open-street-map.blade.php index e0cb188..161c05a 100644 --- a/source/php/Module/views/open-street-map.blade.php +++ b/source/php/Module/views/open-street-map.blade.php @@ -1,12 +1,17 @@ +@includeWhen(!empty($filters), 'partials.filters') @openStreetMap([ 'startPosition' => $startPosition, 'mapStyle' => $mapStyle, - 'fullWidth' => (!empty($blockData['align']) && $blockData['align'] == 'full') || $isFullWidth ? true : false, + 'fullWidth' => true, 'containerAware' => true, + 'attributeList' => [ + 'data-js-map-posts-endpoint' => $endpoint, + ] ]) - @if($places) @slot('sidebarContent') + @includeWhen($sort, 'partials.sort') +