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 2c55cc1..4cd3d14 100644 --- a/source/php/AcfFields/json/modularity-open-street-map-settings.json +++ b/source/php/AcfFields/json/modularity-open-street-map-settings.json @@ -96,6 +96,63 @@ "ui_off_text": "", "ui": 1 }, + { + "key": "field_66a8961b184bd", + "label": "Order by", + "name": "mod_osm_order_by", + "aria-label": "", + "type": "select", + "instructions": "", + "required": 0, + "conditional_logic": 0, + "wrapper": { + "width": "50", + "class": "", + "id": "" + }, + "choices": { + "date": "Date", + "title": "Title", + "random": "Random" + }, + "default_value": "date", + "return_format": "value", + "multiple": 0, + "allow_custom": 0, + "search_placeholder": "", + "allow_null": 0, + "ui": 1, + "ajax": 0, + "placeholder": "" + }, + { + "key": "field_66a896fa184be", + "label": "Order", + "name": "mod_osm_order", + "aria-label": "", + "type": "select", + "instructions": "", + "required": 0, + "conditional_logic": 0, + "wrapper": { + "width": "50", + "class": "", + "id": "" + }, + "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_668d19163553d", "label": "Filter", 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 b93150c..3771129 100644 --- a/source/php/AcfFields/php/modularity-open-street-map-settings.php +++ b/source/php/AcfFields/php/modularity-open-street-map-settings.php @@ -101,6 +101,63 @@ 'ui' => 1, ), 4 => array( + 'key' => 'field_66a8961b184bd', + 'label' => __('Order by', 'modularity-open-street-map'), + 'name' => 'mod_osm_order_by', + 'aria-label' => '', + 'type' => 'select', + 'instructions' => '', + 'required' => 0, + 'conditional_logic' => 0, + 'wrapper' => array( + 'width' => '50', + 'class' => '', + 'id' => '', + ), + 'choices' => array( + 'date' => __('Date', 'modularity-open-street-map'), + 'title' => __('Title', 'modularity-open-street-map'), + 'random' => __('Random', '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' => 1, + 'ajax' => 0, + 'placeholder' => '', + ), + 5 => array( + 'key' => 'field_66a896fa184be', + 'label' => __('Order', 'modularity-open-street-map'), + 'name' => 'mod_osm_order', + 'aria-label' => '', + 'type' => 'select', + 'instructions' => '', + 'required' => 0, + 'conditional_logic' => 0, + 'wrapper' => array( + 'width' => '50', + 'class' => '', + 'id' => '', + ), + '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' => '', + ), + 6 => array( 'key' => 'field_668d19163553d', 'label' => __('Filter', 'modularity-open-street-map'), 'name' => 'mod_osm_filters', diff --git a/source/php/Api/OsmEndpoint.php b/source/php/Api/OsmEndpoint.php index bfab0bf..72da234 100644 --- a/source/php/Api/OsmEndpoint.php +++ b/source/php/Api/OsmEndpoint.php @@ -34,7 +34,7 @@ public function handleRequest(WP_REST_Request $request) } $argsInstance = new OsmQueryArgsCreator($this->settings); - $posts = (new OsmGetPosts($argsInstance->CreateQueryArgs()))->getPosts(); + $posts = (new OsmGetPosts($argsInstance->CreateQueryArgs(), $this->settings))->getPosts(); $postsHandlerInstance = new OsmTransformationHandler( $posts, $this->settings, diff --git a/source/php/Api/OsmGetPosts.php b/source/php/Api/OsmGetPosts.php index d2bad1d..658795d 100644 --- a/source/php/Api/OsmGetPosts.php +++ b/source/php/Api/OsmGetPosts.php @@ -3,13 +3,18 @@ namespace ModularityOpenStreetMap\Api; class OsmGetPosts { - public function __construct(private array $args) + public function __construct(private array $args, private SettingsInterface $settings) {} public function getPosts() { $query = new \WP_Query($this->args); + $posts = $query->posts ?? []; - return $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 index 44a2095..1ea43e9 100644 --- a/source/php/Api/OsmQueryArgsCreator.php +++ b/source/php/Api/OsmQueryArgsCreator.php @@ -14,7 +14,9 @@ public function CreateQueryArgs(): array 'posts_per_page' => $this->settings->getPostsPerPage(), 'paged' => $this->settings->getPage(), 'post_status' => 'publish', - 'tax_query' => $this->createTaxQuery() + 'tax_query' => $this->createTaxQuery(), + 'orderby' => $this->settings->getOrderBy(), + 'order' => $this->settings->getOrder() ]; } diff --git a/source/php/Api/Settings.php b/source/php/Api/Settings.php index a6070d3..65edcbd 100644 --- a/source/php/Api/Settings.php +++ b/source/php/Api/Settings.php @@ -6,19 +6,25 @@ class Settings implements SettingsInterface { - private ?string $postType = null; - private int $page = 1; - private int $postsPerPage = 20; - private ?array $taxonomies = []; - private bool $html = false; + private ?string $postType = null; + private int $page = 1; + private int $postsPerPage = 20; + private ?array $taxonomies = []; + private bool $html = false; + private string $orderBy = 'date'; + private string $order = 'DESC'; + private bool $randomize = false; public function __construct(array $settings) { - $this->postType = isset($settings['postType']) ? $settings['postType'] : $this->postType; - $this->page = isset($settings['page']) ? intval($settings['page']) : $this->page; + $this->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->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 @@ -45,4 +51,19 @@ 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 index 1c4ddfa..b23de4c 100644 --- a/source/php/Api/SettingsInterface.php +++ b/source/php/Api/SettingsInterface.php @@ -8,4 +8,7 @@ public function getPage(): int; public function getPostsPerPage(): int; public function getTaxonomies(): array; public function getHtml(): bool; + public function getOrder(): string; + public function getOrderBy(): string; + public function getRandomize(): bool; } \ No newline at end of file diff --git a/source/php/App.php b/source/php/App.php index d30953e..13a2cf8 100644 --- a/source/php/App.php +++ b/source/php/App.php @@ -63,13 +63,6 @@ public function termsToShow($field) return $field; } - - - - - - - public function enqueueBackend() { $placeTaxonomies = $this->getTaxonomiesInstance->getAllTaxonomiesForAllPlacePostTypes($this->getPlacePostTypeInstance->getPlacePostTypes()); 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/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 7837929..771cf47 100644 --- a/source/php/Module/OpenStreetMap.php +++ b/source/php/Module/OpenStreetMap.php @@ -4,6 +4,12 @@ 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 { @@ -14,9 +20,13 @@ class OpenStreetMap extends \Modularity\Module 'mode' => false ); - private string $taxonomyEndpointKey = 'taxonomies'; private GetTaxonomies $getTaxonomiesInstance; private GetPlacePostType $getPlacePostTypeInstance; + private CreateFilters $createFiltersInstance; + private EndpointDecoratorInterface $endpointOrder; + private EndpointDecoratorInterface $endpointOrderBy; + private EndpointDecoratorInterface $endpointPostType; + private EndpointDecoratorInterface $endpointTaxonomies; public function init() { @@ -26,7 +36,13 @@ public function init() $this->description = __("Outputs a map.", 'modularity-open-street-map'); $this->getPlacePostTypeInstance = new GetPlacePostType(); - $this->getTaxonomiesInstance = new GetTaxonomies($this->getPlacePostTypeInstance); + $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) { @@ -44,12 +60,9 @@ 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']; $data['mapStyle'] = $this->getMapStyle(); - $data['endpoint'] = $this->createEndpoint($postTypeToShow, $termsToShow); + $data['endpoint'] = $this->createEndpoint($fields); $data['startPosition'] = $this->getStartPosition($fields['map_start_values'] ?? []); $data['lang'] = [ 'noPostsFound' => __('No posts were found.', 'modularity-open-street-map'), @@ -61,41 +74,11 @@ public function data(): array ]; $data['sort'] = !empty($fields['mod_osm_sort']); - $data['filters'] = $this->createFilters($fields, $data['lang']['filterBy']); + $data['filters'] = $this->createFiltersInstance->create($fields, $data['lang']['filterBy']); return $data; } - private function createFilters($fields, string $filterByLang): array { - if (empty($fields['mod_osm_filters'])) { - return []; - } - - $filters = []; - foreach ($fields['mod_osm_filters'] as $filter) { - if (empty($filter['mod_osm_filter_taxonomy']) && is_string($filter['mod_osm_filter_taxonomy'])) { - continue; - } - - $terms = $this->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); - } - /** * Creates the endpoint URL for retrieving OpenStreetMap data based on the specified post type and terms. * @@ -103,19 +86,13 @@ private function getTermsFromTaxonomy(string $taxonomy): array * @param array $termsToShow An array of term IDs to filter the results by. * @return string The generated endpoint URL. */ - private function createEndpoint($postTypeToShow, $termsToShow): string + private function createEndpoint($fields): string { - $endpoint = rest_url(OSM_ENDPOINT . $postTypeToShow . '?'); - - $taxonomyToShow = []; - foreach ($termsToShow as $termId) { - $taxonomy = get_term($termId)->taxonomy; - $taxonomyToShow[$taxonomy][] = $termId; - } - - foreach ($taxonomyToShow as $taxonomy => $terms) { - $endpoint .= '&' . $this->taxonomyEndpointKey . '[' . $taxonomy . ']=' . implode(',', $terms); - } + $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; }