94 lines
2.4 KiB
PHP
94 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace FecaMailshots\WordPress;
|
|
|
|
final class FixtureWordPressFacade implements WordPressFacade
|
|
{
|
|
/** @var array<string, list<callable>> */
|
|
private array $actions = [];
|
|
/** @var array<string, mixed> */
|
|
private array $options = [];
|
|
private int $currentUserId = 1;
|
|
|
|
public function addAction(string $hook, callable $callback): void
|
|
{
|
|
if (!isset($this->actions[$hook])) {
|
|
$this->actions[$hook] = [];
|
|
}
|
|
$this->actions[$hook][] = $callback;
|
|
}
|
|
|
|
public function addMenuPage(string $pageTitle, string $menuTitle, string $capability, string $slug, callable $callback): void
|
|
{
|
|
$this->addAction('fixture_menu:' . $slug, $callback);
|
|
}
|
|
|
|
public function addSubmenuPage(string $parentSlug, string $pageTitle, string $menuTitle, string $capability, string $slug, callable $callback): void
|
|
{
|
|
$this->addAction('fixture_submenu:' . $slug, $callback);
|
|
}
|
|
|
|
public function currentUserCan(string $capability): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function verifyNonce(string $nonce, string $action): bool
|
|
{
|
|
return $nonce !== '';
|
|
}
|
|
|
|
public function requestParam(string $name, ?string $default = null): ?string
|
|
{
|
|
if (isset($_POST[$name])) {
|
|
return (string) $_POST[$name];
|
|
}
|
|
if (isset($_GET[$name])) {
|
|
return (string) $_GET[$name];
|
|
}
|
|
return $default;
|
|
}
|
|
|
|
public function sendJson(array $payload, int $statusCode = 200): void
|
|
{
|
|
http_response_code($statusCode);
|
|
header('Content-Type: application/json');
|
|
echo json_encode($payload, JSON_UNESCAPED_SLASHES);
|
|
}
|
|
|
|
public function adminUrl(string $path = ''): string
|
|
{
|
|
return '/wp-admin/' . ltrim($path, '/');
|
|
}
|
|
|
|
public function currentUserId(): int
|
|
{
|
|
return $this->currentUserId;
|
|
}
|
|
|
|
public function getOption(string $name, $default = null)
|
|
{
|
|
return $this->options[$name] ?? $default;
|
|
}
|
|
|
|
public function updateOption(string $name, $value): bool
|
|
{
|
|
$this->options[$name] = $value;
|
|
return true;
|
|
}
|
|
|
|
public function dispatch(string $hook): void
|
|
{
|
|
foreach ($this->actions[$hook] ?? [] as $callback) {
|
|
$callback();
|
|
}
|
|
}
|
|
|
|
public function setCurrentUserId(int $userId): void
|
|
{
|
|
$this->currentUserId = max(1, $userId);
|
|
}
|
|
}
|