handel request
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Dabestaniha\AuthenticateBridge\Requests;
|
||||
|
||||
use Dabestaniha\AuthenticateBridge\Support\Collection;
|
||||
|
||||
abstract class BaseFormRequest
|
||||
{
|
||||
public static function make(): static
|
||||
{
|
||||
return new static();
|
||||
}
|
||||
|
||||
protected array $url;
|
||||
protected array $form;
|
||||
protected Collection $validated;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->url = parse_url($_SERVER['REQUEST_URI']);
|
||||
$this->form = [
|
||||
...$_GET,
|
||||
...$_POST,
|
||||
];
|
||||
}
|
||||
|
||||
public function getRoutePath(): string
|
||||
{
|
||||
return $this->url['path'];
|
||||
}
|
||||
|
||||
public function validate(): void
|
||||
{
|
||||
$data = [];
|
||||
|
||||
foreach ($this->rules() as $attribute => $rules) {
|
||||
$value = $this->form[$attribute] ?? null;
|
||||
|
||||
foreach ($rules as $rule) {
|
||||
if (!$this->validateValue($value, $rule)) {
|
||||
wp_die(view('invalid-token'), 'Invalid Token', ['response' => 403]);
|
||||
}
|
||||
|
||||
$data[$attribute] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$this->validated = Collection::make($data);
|
||||
}
|
||||
|
||||
abstract public function rules(): array;
|
||||
|
||||
protected function validateValue($value, $rule): bool
|
||||
{
|
||||
return match ($rule) {
|
||||
'required' => !empty($value),
|
||||
default => false,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public function string(string $key, string $default = ''): string
|
||||
{
|
||||
return sanitize_text_field($this->validated->get($key, $default));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Dabestaniha\AuthenticateBridge\Requests;
|
||||
|
||||
class DabestanihaAuthenticateRequest extends BaseFormRequest
|
||||
{
|
||||
|
||||
public function shouldContinue(): bool
|
||||
{
|
||||
return $this->getRoutePath() === '/dabestaniha/login';
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'token' => ['required'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Dabestaniha\AuthenticateBridge\Support;
|
||||
|
||||
class Collection
|
||||
{
|
||||
public static function make(array $items): static
|
||||
{
|
||||
return new static($items);
|
||||
}
|
||||
|
||||
public function __construct(private array $items)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function get(string $key, $default = null)
|
||||
{
|
||||
return $this->items[$key] ?? $default;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
if (!function_exists('view')) {
|
||||
function view(string $view): string
|
||||
{
|
||||
$basedir = plugin_dir_path(__FILE__);
|
||||
return file_get_contents("{$basedir}resources/views/$view.php");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
|
||||
<!-- views/invalid-token.php -->
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>توکن نامعتبر</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>توکن ورود نامعتبر است</h1>
|
||||
<p>لینک ورود شما معتبر نیست یا منقضی شده است.</p>
|
||||
<a href="<?= home_url() ?>">بازگشت به صفحه اصلی</a>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user