Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ jobs:
- test:wordpress-seo
- test:polylang
- test:autodescription
- test:wpml
- test:advanced-custom-fields
include:
- php: '8.2'
wp: 'trunk'
Expand Down Expand Up @@ -87,6 +89,8 @@ jobs:
run: PLUGINS=wordpress-seo php -d pcov.enabled=1 vendor/bin/phpunit --testsuite=plugin-integration --coverage-php=build/coverage-wordpress-seo.cov
- name: Coverage (autodescription)
run: PLUGINS=autodescription php -d pcov.enabled=1 vendor/bin/phpunit --testsuite=plugin-integration --coverage-php=build/coverage-autodescription.cov
- name: Coverage (advanced-custom-fields)
run: PLUGINS=advanced-custom-fields php -d pcov.enabled=1 vendor/bin/phpunit --testsuite=plugin-integration --coverage-php=build/coverage-advanced-custom-fields.cov
- name: Merge coverage
run: |
composer require --dev phpunit/phpcov --quiet
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@
"@test:wordpress-seo",
"@test:polylang",
"@test:wpml",
"@test:autodescription"
"@test:autodescription",
"@test:advanced-custom-fields"
],
"test:advanced-custom-fields": "PLUGINS=advanced-custom-fields phpunit --testsuite=unit,integration,plugin-integration",
"test:autodescription": "PLUGINS=autodescription phpunit --testsuite=unit,integration,plugin-integration",
"test:core": "phpunit --testsuite=unit,integration",
"test:coverage": "php -d pcov.enabled=1 phpunit --testsuite=unit,integration --coverage-text",
Expand Down
4 changes: 4 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use n5s\PageForCustomPostType\Core\RewriteManager;
use n5s\PageForCustomPostType\Frontend\Handler;
use n5s\PageForCustomPostType\Frontend\QueryFilter;
use n5s\PageForCustomPostType\Integration\AdvancedCustomFields;
use n5s\PageForCustomPostType\Integration\Autodescription;
use n5s\PageForCustomPostType\Integration\Polylang;
use n5s\PageForCustomPostType\Integration\WordPressSeo;
Expand Down Expand Up @@ -141,6 +142,9 @@ public function __construct()
),

// Integration composites
AdvancedCustomFields\AdvancedCustomFields::class => fn (): AdvancedCustomFields\AdvancedCustomFields => new AdvancedCustomFields\AdvancedCustomFields(
$this->get(Api::class)
),
Polylang\Polylang::class => fn (): Polylang\Polylang => new Polylang\Polylang(
$this->get(Polylang\UrlTranslation::class),
$this->get(Polylang\Translation::class),
Expand Down
88 changes: 80 additions & 8 deletions src/Integration/AdvancedCustomFields/AdvancedCustomFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,86 @@

namespace n5s\PageForCustomPostType\Integration\AdvancedCustomFields;

add_action('acf/include_location_rules', static function (int $acfMajorVersion): void {
if ($acfMajorVersion !== 5) {
return;
use n5s\PageForCustomPostType\Core\Api;
use n5s\PageForCustomPostType\Integration\IntegrationInterface;

/**
* Advanced Custom Fields integration.
*
* Extends the built-in `page_type` location rule with `<cpt>_page` values so
* field groups can target PFCPT-bound pages.
*/
final class AdvancedCustomFields implements IntegrationInterface
{
public function __construct(
private readonly Api $api
) {
}

public function isSupported(): bool
{
return \function_exists('acf_get_location_type');
}

public function registerHooks(): void
{
add_filter('acf/location/rule_values/type=page_type', [$this, 'addPageTypeValues'], 10, 2);
add_filter('acf/location/match_rule/type=page_type', [$this, 'matchPageType'], 10, 4);
}

/**
* Add `<cpt>_page` options to the Page Type rule values dropdown.
*
* @param array<string, string> $values
* @param array<string, mixed> $rule
* @return array<string, string>
*/
public function addPageTypeValues(array $values, array $rule): array
{
foreach (array_keys($this->api->getPageIds()) as $postType) {
$postTypeObject = get_post_type_object($postType);

if ($postTypeObject && \is_string($postTypeObject->labels->archives)) {
$values[$postType . '_page'] = $postTypeObject->labels->archives;
}
}

return $values;
}

require_once __DIR__ . '/LocationPageType.php';
/**
* Match a `page_type == <cpt>_page` rule against the current screen.
*
* @param array<string, mixed> $rule
* @param array<string, mixed> $screen
* @param array<string, mixed> $fieldGroup
*/
public function matchPageType(bool $match, array $rule, array $screen, array $fieldGroup): bool
{
if ($match) {
return true;
}

if (!isset($screen['post_id'])) {
return false;
}

$post = get_post($screen['post_id']);

if (!$post instanceof \WP_Post) {
return false;
}

$store = acf_get_store('location-types');
$locationType = new LocationPageType();
$store->set($locationType->name, $locationType);
});
foreach ($this->api->getPageIds() as $postType => $pageId) {
if ($rule['value'] !== $postType . '_page') {
continue;
}

$result = ($pageId === $post->ID);

return ($rule['operator'] === '!=') ? !$result : $result;
}

return false;
}
}
119 changes: 0 additions & 119 deletions src/Integration/AdvancedCustomFields/LocationPageType.php

This file was deleted.

2 changes: 2 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use n5s\PageForCustomPostType\Core\RewriteManager;
use n5s\PageForCustomPostType\Frontend\Handler;
use n5s\PageForCustomPostType\Frontend\QueryFilter;
use n5s\PageForCustomPostType\Integration\AdvancedCustomFields;
use n5s\PageForCustomPostType\Integration\Autodescription;
use n5s\PageForCustomPostType\Integration\IntegrationInterface;
use n5s\PageForCustomPostType\Integration\Polylang;
Expand Down Expand Up @@ -184,6 +185,7 @@ public function onTemplateRedirect(): void
public function getIntegrations(): array
{
return [
AdvancedCustomFields\AdvancedCustomFields::class,
Polylang\Polylang::class,
WordPressSeo\WordPressSeo::class,
Wpml\Wpml::class,
Expand Down
Loading
Loading