Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
annalinneajohansson committed Nov 9, 2023
2 parents 19c2125 + 1525347 commit d86b56e
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 43 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@
"Municipio\\": "library/"
}
},
"version": "3.24.0"
"version": "3.26.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,51 @@ public function modifyDefaultPostMetadata($value, int $objectId, $metaKey, $sing

private function prepareQueryArgsForRequest(array $queryArgs, object $resource): array
{
if (isset($queryArgs['post__in']) && !empty(array_filter($queryArgs['post__in']))) {
$postIn = isset($queryArgs['post__in']) && is_array($queryArgs['post__in']) ? array_filter($queryArgs['post__in'], fn($id) => !empty($id)) : [];
if (!empty($postIn)) {
$queryArgs['post__in'] = array_map(
fn ($id) => $this->prepareIdForRequest($id, $resource),
$queryArgs['post__in']
$postIn
);
}

if( isset($queryArgs['tax_query']) && is_array($queryArgs['tax_query']) && !empty($queryArgs['tax_query']) ) {
foreach($queryArgs['tax_query'] as $key => $taxQuery) {

if( isset($taxQuery['taxonomy']) && is_string($taxQuery['taxonomy']) && !empty($taxQuery['taxonomy']) ) {
$queryArgs['tax_query'][$key]['taxonomy'] = $this->possiblyConvertLocalTaxonomyToRemote($taxQuery['taxonomy']);
}

if( isset($taxQuery['terms']) && is_array($taxQuery['terms']) && !empty($taxQuery['terms']) ) {
$queryArgs['tax_query'][$key]['terms'] = array_map(
function ($id) {
$taxResource = $this->getResourceFromPostId($id);
return !empty($taxResource) ? $this->prepareIdForRequest($id, $taxResource) : $id;
},
$taxQuery['terms']
);
}
}
}

return $queryArgs;
}

private function possiblyConvertLocalTaxonomyToRemote (string $taxonomy): string
{
$localTaxonomyResource = $this->resourceRegistry->getRegisteredTaxonomy($taxonomy);

if (
!empty($localTaxonomyResource) &&
isset($localTaxonomyResource->originalName) &&
!empty($localTaxonomyResource->originalName)
) {
return $localTaxonomyResource->originalName;
}

return $taxonomy;
}

private function prepareIdForRequest(int $id, object $resource): int
{
return (int)str_replace((string)$resource->resourceID, '', (string)absint($id));
Expand Down Expand Up @@ -216,17 +251,20 @@ private function getResourceFromPostId($postId): ?object
}

$registeredPostTypes = $this->resourceRegistry->getRegisteredPostTypes();
$registeredTaxonomies = $this->resourceRegistry->getRegisteredTaxonomies();

$registeredResources = array_merge($registeredPostTypes, $registeredTaxonomies);

if (empty($registeredPostTypes)) {
if (empty($registeredResources)) {
return null;
}

foreach ($registeredPostTypes as $registeredPostType) {
$needle = (string)$registeredPostType->resourceID;
foreach ($registeredResources as $registeredResource) {
$needle = (string)$registeredResource->resourceID;
$haystack = (string)absint($postId);

if (str_starts_with($haystack, $needle)) {
return $registeredPostType;
return $registeredResource;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private static function getSingleUrl($id, object $resource): ?string

private static function getCollectionUrl(object $resource, ?array $queryArgs = null): ?string
{
$resourceUrl = get_field('post_type_source', $resource->resourceID);
$resourceUrl = $resource->collectionUrl;

if (empty($resourceUrl)) {
return null;
Expand Down
64 changes: 60 additions & 4 deletions library/Content/ResourceFromApi/ResourcePostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public function addHooks(): void
add_filter('acf/load_field/name=taxonomy_source', [$this, 'loadTaxonomySourceOptions']);
add_action('acf/save_post', [$this, 'setPostTypeResourcePostTitleFromAcf'], 10);
add_action('acf/save_post', [$this, 'setTaxonomyResourcePostTitleFromAcf'], 10);
add_action('acf/save_post', [$this, 'setPostTypeApiMeta'], 10);
add_action('acf/save_post', [$this, 'setTaxonomyApiMeta'], 10);
add_filter('acf/update_value/name=post_type_key', [$this, 'sanitizePostTypeKeyBeforeSave'], 10, 4);
add_filter('acf/update_value/name=taxonomy_key', [$this, 'sanitizeTaxonomyKeyBeforeSave'], 10, 4);
}
Expand Down Expand Up @@ -94,8 +96,9 @@ public function loadPostTypeSourceOptions($field)
continue;
}

$value = trailingslashit($url) . $type->rest_base;
$label = "{$type->slug}: {$value}";
$value = trailingslashit($url) . ",{$type->slug}" . ",{$type->rest_base}";
$labelParenthesis = trailingslashit($url) . $type->rest_base;
$label = "{$type->slug}: {$labelParenthesis}";
$choices[$value] = $label;
}
}
Expand Down Expand Up @@ -149,8 +152,9 @@ public function loadTaxonomySourceOptions($field)
continue;
}

$value = trailingslashit($url) . $type->rest_base;
$label = "{$type->slug}: {$value}";
$value = trailingslashit($url) . ",{$type->slug}" . ",{$type->rest_base}";
$labelParenthesis = trailingslashit($url) . $type->rest_base;
$label = "{$type->slug}: {$labelParenthesis}";
$choices[$value] = $label;
}
}
Expand Down Expand Up @@ -208,6 +212,58 @@ public function setTaxonomyResourcePostTitleFromAcf($postId)
]);
}

public function setPostTypeApiMeta($postId) {
if( !is_string(get_post_type($postId)) || get_post_type($postId) !== 'api-resource' || !function_exists('get_field') ) {
return;
}

$postTypeSource = get_field('post_type_source', $postId);

if( !is_string($postTypeSource) || empty($postTypeSource) ) {
return;
}

$parts = explode(',', $postTypeSource);

if( sizeof($parts) !== 3 ) {
return;
}

$url = $parts[0];
$originalName = $parts[1];
$baseName = $parts[2];

update_post_meta($postId, 'api_resource_url', $url);
update_post_meta($postId, 'api_resource_original_name', $originalName);
update_post_meta($postId, 'api_resource_base_name', $baseName);
}

public function setTaxonomyApiMeta($postId) {
if( !is_string(get_post_type($postId)) || get_post_type($postId) !== 'api-resource' || !function_exists('get_field') ) {
return;
}

$taxonomySource = get_field('taxonomy_source', $postId);

if( !is_string($taxonomySource) || empty($taxonomySource) ) {
return;
}

$parts = explode(',', $taxonomySource);

if( sizeof($parts) !== 3 ) {
return;
}

$url = $parts[0];
$originalName = $parts[1];
$baseName = $parts[2];

update_post_meta($postId, 'api_resource_url', $url);
update_post_meta($postId, 'api_resource_original_name', $originalName);
update_post_meta($postId, 'api_resource_base_name', $baseName);
}

public function sanitizePostTypeKeyBeforeSave($value, $postId, $field, $originalValue)
{
return sanitize_title(substr($value, 0, 19));
Expand Down
31 changes: 11 additions & 20 deletions library/Content/ResourceFromApi/ResourceRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,13 @@ private function getPostTypes(): array
$arguments = $this->getPostTypeArguments($resource->ID);
$name = $this->getPostTypeName($resource->ID);
$resourceID = $resource->ID;
$collectionUrl = self::getCollectionUrl($resourceID, 'postType');

$resourceUrl = get_post_meta($resourceID, 'api_resource_url', true);
$originalName = get_post_meta($resourceID, 'api_resource_original_name', true);
$baseName = get_post_meta($resourceID, 'api_resource_base_name', true);
$collectionUrl = $resourceUrl . $baseName;

return (object)['name' => $name, 'resourceID' => $resourceID, 'arguments' => $arguments, 'collectionUrl' => $collectionUrl];
return (object)['name' => $name, 'resourceID' => $resourceID, 'arguments' => $arguments, 'collectionUrl' => $collectionUrl, 'originalName' => $originalName, 'baseName' => $baseName];
}, $resources);
}

Expand All @@ -202,9 +206,12 @@ private function getTaxonomies(): array {
$arguments = $this->getTaxonomyArguments($resource->ID);
$name = $this->getTaxonomyName($resource->ID);
$resourceID = $resource->ID;
$collectionUrl = self::getCollectionUrl($resourceID, 'taxonomy');
$resourceUrl = get_post_meta($resourceID, 'api_resource_url', true);
$originalName = get_post_meta($resourceID, 'api_resource_original_name', true);
$baseName = get_post_meta($resourceID, 'api_resource_base_name', true);
$collectionUrl = $resourceUrl . $baseName;

return (object)['name' => $name, 'resourceID' => $resourceID, 'arguments' => $arguments, 'collectionUrl' => $collectionUrl];
return (object)['name' => $name, 'resourceID' => $resourceID, 'arguments' => $arguments, 'collectionUrl' => $collectionUrl, 'originalName' => $originalName, 'baseName' => $baseName];
}, $resources);
}

Expand All @@ -231,22 +238,6 @@ private function getPostTypeArguments(int $resourceId): array
return get_field('post_type_arguments', $resourceId) ?? [];
}

private function getCollectionUrl(int $resourceId, string $type = 'postType'): ?string
{
if (!function_exists('get_field')) {
return null;
}

$fieldName = $type === 'postType' ? 'post_type_source' : 'taxonomy_source';
$resourceUrl = get_field($fieldName, $resourceId);

if (empty($resourceUrl)) {
return null;
}

return $resourceUrl;
}

public function getTaxonomyArguments(int $resourceId): array {
if (!function_exists('get_field')) {
return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,15 @@ private static function getSingleUrl($id, object $resource):?string {

private static function getCollectionUrl(object $resource, ?array $queryArgs = null): ?string
{
$resourceUrl = get_field('taxonomy_source', $resource->resourceID);

if (empty($resourceUrl)) {
if (empty($resource->collectionUrl)) {
return null;
}

$restParams = !empty($queryArgs)
? '?' . WPTermQueryToRestParamsConverter::convertToRestParamsString($queryArgs)
: '';

return $resourceUrl . $restParams;
return $resource->collectionUrl . $restParams;
}

private static function getLocalID(int $id, object $resource): int
Expand Down
2 changes: 2 additions & 0 deletions library/Controller/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public function init()
//Show filter?
$this->data['showFilter'] = $this->showFilter($this->data['archiveProps']);



//Archive menu
$archiveMenu = new \Municipio\Helper\Navigation('archive-menu');
$this->data['archiveMenuItems'] = $archiveMenu->getMenuItems(
Expand Down
2 changes: 2 additions & 0 deletions library/Controller/ArchiveContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public function init()
if (ContentType::hasContentType('place', get_post_type(), true)) {
$this->setupOpenStreetMap();
}


}

private function setupOpenStreetMap()
Expand Down
27 changes: 25 additions & 2 deletions library/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,31 @@ public function __construct()

//Main Navigation
$this->data['primaryMenuItems'] = $primary->getMenuItems('main-menu', $this->getPageID(), \Kirki::get_option('primary_menu_pagetree_fallback'), true, !$this->data['customizer']->primaryMenuDropdown);
$this->data['secondaryMenuItems'] = $secondary->getMenuItems('secondary-menu', $this->getPageID(), \Kirki::get_option('secondary_menu_pagetree_fallback'), false, false);


/**
* Get the secondary menu items based on the content type or post type.
* If a content type is found, it will look for a secondary menu with the key {content_type}-secondary-menu.
* If not found, it will look for a secondary menu with the key {post_type}-secondary-menu.
* If still not found, it will fallback to the secondary menu with the key 'secondary-menu'.
*
* @return void
*/
$contentType = \Municipio\Helper\ContentType::getContentType(get_post_type());
if($contentType) {
$contentTypeSecondaryMenu = $secondary->getMenuItems( $contentType->getKey() . '-secondary-menu', $this->getPageID());
if( !empty( $contentTypeSecondaryMenu ) ) {
$this->data['secondaryMenuItems'] = $contentTypeSecondaryMenu;
}
} else {
$posttypeSecondaryMenuItems = $secondary->getMenuItems( get_post_type() . '-secondary-menu', $this->getPageID());
if(!empty($posttypeSecondaryMenuItems)) {
$this->data['secondaryMenuItems'] = $posttypeSecondaryMenuItems;
} else {
$this->data['secondaryMenuItems'] = $secondary->getMenuItems('secondary-menu', $this->getPageID(), \Kirki::get_option('secondary_menu_pagetree_fallback'), false, false);
}
}


$this->data['mobileMenuItems'] = $mobileMenu->getMenuItems('secondary-menu', $this->getPageID(), \Kirki::get_option('mobile_menu_pagetree_fallback'), true, false);
$this->data['megaMenuItems'] = $megaMenu->getMenuItems('mega-menu', $this->getPageID(), \Kirki::get_option('mega_menu_pagetree_fallback'), true, false);

Expand Down
Loading

0 comments on commit d86b56e

Please sign in to comment.