Skip to content

Commit c6b9451

Browse files
huublnlemoine
authored andcommitted
fix(acf): bootstrap Advanced Custom Fields integration
AdvancedCustomFields.php was procedural top-level add_action code; the file was never loaded because Composer PSR-4 only autoloads class files and it is not listed in autoload.files. As a result the ACF location type was never registered and `page_type == <cpt>_page` rules always evaluated to false on ACF Pro 6.x. Refactor the file into a class-based integration consistent with the existing Polylang/WordPressSeo/Wpml/Autodescription composites: - Implement IntegrationInterface with isSupported() + registerHooks(). - Guard isSupported() on class_exists('ACF_Location_Page_Type') so the hook is only registered when ACF Pro's parent class is loaded. - Register the integration in Container as a service factory. - Add it to Plugin::getIntegrations() so plugin.php bootstraps it. - Extend PluginTest and ContainerTest to cover the new integration.
1 parent 9061105 commit c6b9451

5 files changed

Lines changed: 43 additions & 8 deletions

File tree

src/Container.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use n5s\PageForCustomPostType\Core\RewriteManager;
1111
use n5s\PageForCustomPostType\Frontend\Handler;
1212
use n5s\PageForCustomPostType\Frontend\QueryFilter;
13+
use n5s\PageForCustomPostType\Integration\AdvancedCustomFields;
1314
use n5s\PageForCustomPostType\Integration\Autodescription;
1415
use n5s\PageForCustomPostType\Integration\Polylang;
1516
use n5s\PageForCustomPostType\Integration\WordPressSeo;
@@ -141,6 +142,7 @@ public function __construct()
141142
),
142143

143144
// Integration composites
145+
AdvancedCustomFields\AdvancedCustomFields::class => static fn (): AdvancedCustomFields\AdvancedCustomFields => new AdvancedCustomFields\AdvancedCustomFields(),
144146
Polylang\Polylang::class => fn (): Polylang\Polylang => new Polylang\Polylang(
145147
$this->get(Polylang\UrlTranslation::class),
146148
$this->get(Polylang\Translation::class),

src/Integration/AdvancedCustomFields/AdvancedCustomFields.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,34 @@
44

55
namespace n5s\PageForCustomPostType\Integration\AdvancedCustomFields;
66

7-
add_action('acf/include_location_rules', static function (int $acfMajorVersion): void {
8-
if ($acfMajorVersion !== 5) {
9-
return;
7+
use n5s\PageForCustomPostType\Integration\IntegrationInterface;
8+
9+
/**
10+
* Advanced Custom Fields integration composite.
11+
*
12+
* Registers a custom location type that exposes `<cpt>_page` values on the
13+
* `page_type` rule, so field groups can target PFCPT-bound pages.
14+
*/
15+
final class AdvancedCustomFields implements IntegrationInterface
16+
{
17+
public function isSupported(): bool
18+
{
19+
return \class_exists('ACF_Location_Page_Type');
20+
}
21+
22+
public function registerHooks(): void
23+
{
24+
add_action('acf/include_location_rules', [$this, 'registerLocationRules']);
1025
}
1126

12-
require_once __DIR__ . '/LocationPageType.php';
27+
public function registerLocationRules(int $acfFieldApiVersion): void
28+
{
29+
if ($acfFieldApiVersion !== 5) {
30+
return;
31+
}
1332

14-
$store = acf_get_store('location-types');
15-
$locationType = new LocationPageType();
16-
$store->set($locationType->name, $locationType);
17-
});
33+
$store = acf_get_store('location-types');
34+
$locationType = new LocationPageType();
35+
$store->set($locationType->name, $locationType);
36+
}
37+
}

src/Plugin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use n5s\PageForCustomPostType\Core\RewriteManager;
1010
use n5s\PageForCustomPostType\Frontend\Handler;
1111
use n5s\PageForCustomPostType\Frontend\QueryFilter;
12+
use n5s\PageForCustomPostType\Integration\AdvancedCustomFields;
1213
use n5s\PageForCustomPostType\Integration\Autodescription;
1314
use n5s\PageForCustomPostType\Integration\IntegrationInterface;
1415
use n5s\PageForCustomPostType\Integration\Polylang;
@@ -184,6 +185,7 @@ public function onTemplateRedirect(): void
184185
public function getIntegrations(): array
185186
{
186187
return [
188+
AdvancedCustomFields\AdvancedCustomFields::class,
187189
Polylang\Polylang::class,
188190
WordPressSeo\WordPressSeo::class,
189191
Wpml\Wpml::class,

tests/Integration/PluginTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use n5s\PageForCustomPostType\Container;
88
use n5s\PageForCustomPostType\Core\Api;
99
use n5s\PageForCustomPostType\Core\RewriteManager;
10+
use n5s\PageForCustomPostType\Integration\AdvancedCustomFields\AdvancedCustomFields;
1011
use n5s\PageForCustomPostType\Integration\IntegrationInterface;
1112
use n5s\PageForCustomPostType\Plugin;
1213
use n5s\PageForCustomPostType\Tests\Fixtures\TestCase;
@@ -62,6 +63,7 @@ public function testGetIntegrationsReturnsArrayOfIntegrationClassStrings(): void
6263

6364
$this->assertIsArray($integrations);
6465
$this->assertNotEmpty($integrations);
66+
$this->assertContains(AdvancedCustomFields::class, $integrations);
6567

6668
foreach ($integrations as $integration) {
6769
$this->assertIsString($integration);

tests/Unit/ContainerTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use n5s\PageForCustomPostType\Container;
99
use n5s\PageForCustomPostType\Core\Api;
1010
use n5s\PageForCustomPostType\Core\RewriteManager;
11+
use n5s\PageForCustomPostType\Integration\AdvancedCustomFields\AdvancedCustomFields;
1112
use n5s\PageForCustomPostType\Tests\Fixtures\TestCase;
1213

1314
class ContainerTest extends TestCase
@@ -27,6 +28,13 @@ public function testGetReturnsCorrectTypeForApi(): void
2728
$this->assertInstanceOf(Api::class, $service);
2829
}
2930

31+
public function testGetReturnsCorrectTypeForAdvancedCustomFieldsIntegration(): void
32+
{
33+
$service = $this->container->get(AdvancedCustomFields::class);
34+
35+
$this->assertInstanceOf(AdvancedCustomFields::class, $service);
36+
}
37+
3038
public function testGetReturnsSameInstanceOnRepeatedCalls(): void
3139
{
3240
$first = $this->container->get(Api::class);
@@ -39,6 +47,7 @@ public function testHasReturnsTrueForKnownServices(): void
3947
{
4048
$this->assertTrue($this->container->has(Api::class));
4149
$this->assertTrue($this->container->has(RewriteManager::class));
50+
$this->assertTrue($this->container->has(AdvancedCustomFields::class));
4251
}
4352

4453
public function testHasReturnsFalseForUnknownServices(): void

0 commit comments

Comments
 (0)