Skip to content

Commit

Permalink
feat: refactoring and added randomization
Browse files Browse the repository at this point in the history
  • Loading branch information
NiclasNorin committed Jul 30, 2024
1 parent 64ff4a7 commit e84d676
Show file tree
Hide file tree
Showing 15 changed files with 326 additions and 69 deletions.
57 changes: 57 additions & 0 deletions source/php/AcfFields/json/modularity-open-street-map-settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
57 changes: 57 additions & 0 deletions source/php/AcfFields/php/modularity-open-street-map-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion source/php/Api/OsmEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions source/php/Api/OsmGetPosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 3 additions & 1 deletion source/php/Api/OsmQueryArgsCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
];
}

Expand Down
39 changes: 30 additions & 9 deletions source/php/Api/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
3 changes: 3 additions & 0 deletions source/php/Api/SettingsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 0 additions & 7 deletions source/php/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ public function termsToShow($field)
return $field;
}








public function enqueueBackend()
{
$placeTaxonomies = $this->getTaxonomiesInstance->getAllTaxonomiesForAllPlacePostTypes($this->getPlacePostTypeInstance->getPlacePostTypes());
Expand Down
7 changes: 7 additions & 0 deletions source/php/Decorator/EndpointDecoratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace ModularityOpenStreetMap\Decorator;

interface EndpointDecoratorInterface {
public function decorate(string $endpoint, array $fields): string;
}
25 changes: 25 additions & 0 deletions source/php/Decorator/EndpointOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace ModularityOpenStreetMap\Decorator;

class EndpointOrder implements EndpointDecoratorInterface
{
public function __construct()
{}

public function decorate(string $endpoint, array $fields): string
{
$orderBy = $fields['mod_osm_order_by'];
$order = $fields['mod_osm_order'];

return $endpoint . '&order=' . ($orderBy !== 'random' ? $order : $this->getRandomizedOrder());
}

private function getRandomizedOrder(): string
{
$possibleOrders = ['ASC', 'DESC'];
$randomOrderKey = array_rand($possibleOrders);

return $possibleOrders[$randomOrderKey];
}
}
24 changes: 24 additions & 0 deletions source/php/Decorator/EndpointOrderBy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace ModularityOpenStreetMap\Decorator;

class EndpointOrderBy implements EndpointDecoratorInterface
{
public function __construct()
{}

public function decorate(string $endpoint, array $fields): string
{
$orderBy = $fields['mod_osm_order_by'] ?? 'date';

return $endpoint . '&orderby=' . ($orderBy !== 'random' ? $orderBy : $this->getRandomizedOrderBy());
}

private function getRandomizedOrderBy(): string
{
$possibleOrderBys = ['title', 'date', 'modified', 'ID', 'author'];
$randomOrderByKey = array_rand($possibleOrderBys);

return $possibleOrderBys[$randomOrderByKey] . '&randomize=true';
}
}
16 changes: 16 additions & 0 deletions source/php/Decorator/EndpointPostType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace ModularityOpenStreetMap\Decorator;

class EndpointPostType implements EndpointDecoratorInterface
{
public function __construct()
{}

public function decorate(string $endpoint, array $fields): string
{
$postTypeToShow = $fields['mod_osm_post_type'] ?? '';

return $endpoint . $postTypeToShow . '?';
}
}
30 changes: 30 additions & 0 deletions source/php/Decorator/EndpointTaxonomies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace ModularityOpenStreetMap\Decorator;

class EndpointTaxonomies implements EndpointDecoratorInterface
{
public function __construct()
{}

public function decorate(string $endpoint, array $fields): string
{
$termsToShow = $fields['mod_osm_terms_to_show'];
// echo '<pre>' . print_r( $termsToShow, true ) . '</pre>';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;
}
}
Loading

0 comments on commit e84d676

Please sign in to comment.