diff --git a/src/Integrations/Core/CoreIntegration.php b/src/Integrations/Core/CoreIntegration.php new file mode 100644 index 0000000..18883c6 --- /dev/null +++ b/src/Integrations/Core/CoreIntegration.php @@ -0,0 +1,34 @@ +guidesDirectory)), + new SiteStatusAbility(new EnvironmentProbe(), new Access()), + ]; + } +} diff --git a/src/Integrations/Core/EnvironmentProbe.php b/src/Integrations/Core/EnvironmentProbe.php new file mode 100644 index 0000000..7e059b7 --- /dev/null +++ b/src/Integrations/Core/EnvironmentProbe.php @@ -0,0 +1,36 @@ + */ + public function components(): array + { + $theme = wp_get_theme(); + $isDivi = $theme->get_template() === 'Divi'; + $parent = $theme->parent(); + $diviVersion = $isDivi ? (string) ($parent ? $parent->get('Version') : $theme->get('Version')) : null; + + $tecActive = class_exists('Tribe__Events__Main'); + + return [ + ['id' => 'divi', 'active' => $isDivi, 'version' => $diviVersion], + [ + 'id' => 'pods', + 'active' => defined('PODS_VERSION'), + 'version' => defined('PODS_VERSION') ? (string) PODS_VERSION : null, + ], + [ + 'id' => 'the-events-calendar', + 'active' => $tecActive, + 'version' => $tecActive ? (string) constant('Tribe__Events__Main::VERSION') : null, + ], + ]; + } +} diff --git a/src/Integrations/Core/SiteStatusAbility.php b/src/Integrations/Core/SiteStatusAbility.php new file mode 100644 index 0000000..fd43c2a --- /dev/null +++ b/src/Integrations/Core/SiteStatusAbility.php @@ -0,0 +1,80 @@ + 'object', 'properties' => [], 'additionalProperties' => false]; + } + + public function outputSchema(): ?array + { + return [ + 'type' => 'object', + 'properties' => [ + 'site' => ['type' => 'object'], + 'wordpress' => ['type' => 'string'], + 'user' => ['type' => 'object'], + 'allowed_roles' => ['type' => 'array', 'items' => ['type' => 'string']], + 'components' => ['type' => 'array'], + ], + 'required' => ['site', 'wordpress', 'user', 'allowed_roles', 'components'], + ]; + } + + public function annotations(): array + { + return ['readonly' => true, 'destructive' => false, 'idempotent' => true]; + } + + public function isDirect(): bool + { + return true; + } + + public function permission(array $input): bool|WP_Error + { + return true; + } + + public function execute(array $input): mixed + { + $user = wp_get_current_user(); + + return [ + 'site' => ['name' => get_bloginfo('name'), 'url' => home_url()], + 'wordpress' => get_bloginfo('version'), + 'user' => ['id' => $user->ID, 'roles' => array_values($user->roles)], + 'allowed_roles' => $this->access->allowedRoles(), + 'components' => $this->probe->components(), + ]; + } +} diff --git a/tests/Unit/Integrations/Core/CoreIntegrationTest.php b/tests/Unit/Integrations/Core/CoreIntegrationTest.php new file mode 100644 index 0000000..a93088d --- /dev/null +++ b/tests/Unit/Integrations/Core/CoreIntegrationTest.php @@ -0,0 +1,23 @@ +id()); + self::assertTrue($core->isAvailable()); + self::assertSame( + ['lit/guide', 'lit/site-status'], + array_map(static fn ($a) => $a->name(), $core->abilities()) + ); + } +} diff --git a/tests/Unit/Integrations/Core/SiteStatusAbilityTest.php b/tests/Unit/Integrations/Core/SiteStatusAbilityTest.php new file mode 100644 index 0000000..4382bc8 --- /dev/null +++ b/tests/Unit/Integrations/Core/SiteStatusAbilityTest.php @@ -0,0 +1,36 @@ +alias(static fn (string $key) => ['name' => 'Kunde', 'version' => '7.0.3'][$key]); + Functions\when('home_url')->justReturn('https://kunde.de'); + Functions\when('wp_get_current_user')->justReturn(new WP_User(3, ['editor'])); + Functions\when('get_option')->justReturn(['administrator', 'editor']); + + // EnvironmentProbe is deliberately non-final so it can be mocked; Access is final, so use the real one. + $probe = Mockery::mock(EnvironmentProbe::class); + $probe->allows('components')->andReturn([['id' => 'divi', 'active' => true, 'version' => '5.2.1']]); + + $result = (new SiteStatusAbility($probe, new Access()))->execute([]); + + self::assertSame(['name' => 'Kunde', 'url' => 'https://kunde.de'], $result['site']); + self::assertSame('7.0.3', $result['wordpress']); + self::assertSame(['id' => 3, 'roles' => ['editor']], $result['user']); + self::assertSame(['administrator', 'editor'], $result['allowed_roles']); + self::assertSame('divi', $result['components'][0]['id']); + } +}