Skip to content

Commit

Permalink
fix: missing acf fields (#742)
Browse files Browse the repository at this point in the history
* fix: load local acf fields on init

* fix: posts from api initialized too early
  • Loading branch information
thorbrink authored Nov 8, 2023
1 parent 10ad307 commit cf5061b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion library/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function __construct()
$resourcePostType->addHooks();

$resourceRegistry = new \Municipio\Content\ResourceFromApi\ResourceRegistry();
$resourceRegistry->initialize();
$resourceRegistry->addHooks();

$postTypeQueriesModifier = new \Municipio\Content\ResourceFromApi\PostType\PostTypeQueriesModifier($resourceRegistry);
$postTypeQueriesModifier->addHooks();
Expand Down
2 changes: 1 addition & 1 deletion library/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
/**
* Acf auto import and export
*/
add_action('acf/init', function () {
add_action('init', function () {
$acfExportManager = new \AcfExportManager\AcfExportManager();
$acfExportManager->setTextdomain('municipio');
$acfExportManager->setExportFolder(MUNICIPIO_PATH . 'library/AcfFields');
Expand Down
6 changes: 3 additions & 3 deletions library/Content/ResourceFromApi/ResourcePostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ResourcePostType
public function addHooks(): void
{
add_action('init', [$this, 'addPostType']);
add_action('acf/init', [$this, 'addOptionsPage']);
add_action('init', [$this, 'addOptionsPage']);
add_filter('acf/load_field/name=post_type_source', [$this, 'loadPostTypeSourceOptions']);
add_filter('acf/load_field/name=taxonomy_source', [$this, 'loadTaxonomySourceOptions']);
add_action('acf/save_post', [$this, 'setPostTypeResourcePostTitleFromAcf'], 10);
Expand Down Expand Up @@ -63,7 +63,7 @@ public function loadPostTypeSourceOptions($field)

$endpoints = get_field('api_resources_apis', 'options');

if (empty($endpoints)) {
if (!is_array($endpoints) || empty($endpoints)) {
return $field;
}

Expand Down Expand Up @@ -118,7 +118,7 @@ public function loadTaxonomySourceOptions($field)

$endpoints = get_field('api_resources_apis', 'options');

if (empty($endpoints)) {
if (!is_array($endpoints) || empty($endpoints)) {
return $field;
}

Expand Down
23 changes: 16 additions & 7 deletions library/Content/ResourceFromApi/ResourceRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,30 @@ class ResourceRegistry implements ResourceRegistryInterface
public static ?object $registry = null;
private string $resourcePostTypeName = 'api-resource';

public function initialize()
public function __construct()
{
if (is_object(self::$registry)) {
// Already initialized.
if ($this->isInitialized()) {
return;
}

self::$registry = new stdClass();
self::$registry->postTypes = [];
self::$registry->taxonomies = [];
self::$registry->attachments = [];
self::$registry->attachments = [];
}

$this->registerPostTypes();
$this->registerPostTypesRewriteRules();
$this->registerTaxonomies();
public function addHooks()
{
add_action('init', function () {
$this->registerPostTypes();
$this->registerPostTypesRewriteRules();
$this->registerTaxonomies();
});
}

private function isInitialized(): bool
{
return is_object(self::$registry);
}

public function getRegisteredPostTypes(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Municipio\Content\ResourceFromApi;

interface ResourceRegistryInterface {
public function initialize();
public function addHooks();
public function getRegisteredPostTypes(): array;
public function getRegisteredPostType(string $postTypeName): ?object;
public function getRegisteredTaxonomies(): array;
Expand Down

0 comments on commit cf5061b

Please sign in to comment.