You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

95 lines
2.4 KiB

<?php
namespace Zoomyboy\Stepper;
use GuzzleHttp\Client;
use Dotenv\Dotenv;
class Stepper
{
public string $url;
public string $scriptUrl;
public function loadEnv(string $dir): void
{
$env = Dotenv::createImmutable($dir);
$env->safeLoad();
}
public function initFrontend()
{
add_action('wp_enqueue_scripts', [$this, 'enqueue']);
add_action('wp_ajax_nopriv_stepper_submit', [$this, 'onSubmit']);
add_action('wp_ajax_stepper_submit', [$this, 'onSubmit']);
add_action('wp_footer', [$this, 'enqueueOverwrite']);
if (!is_admin()) {
add_shortcode('stepper', [$this, 'handle']);
}
}
public function enqueueOverwrite(): void
{
if (is_admin()) {
return;
}
ob_start();
require(__DIR__ . '/../templates/overwrite.htm');
echo ob_get_clean();
}
public function enqueue()
{
wp_enqueue_script('stepper-js', $this->scriptUrl . 'app.js', [], false, true);
wp_enqueue_style('stepper-css', $this->scriptUrl . 'app.css');
}
public function handle()
{
require(__DIR__ . '/../templates/stepper.htm');
}
public function validate($payload)
{
$errors = [];
if (!$payload['datenschutz']) {
$errors['datenschutz'] = 'Bitte akzeptieren Sie die Datenschutzerklärung.';
}
if (!$payload['firstname']) {
$errors['firstname'] = 'Bitte füllen Sie dieses Feld aus.';
}
if (!$payload['lastname']) {
$errors['lastname'] = 'Bitte füllen Sie dieses Feld aus.';
}
if (!$payload['phone']) {
$errors['phone'] = 'Bitte füllen Sie dieses Feld aus.';
}
if (!$payload['email']) {
$errors['email'] = 'Bitte füllen Sie dieses Feld aus.';
}
if ($payload['email'] && !filter_var($payload['email'], FILTER_VALIDATE_EMAIL)) {
$errors['email'] = 'Dies ist keine richtige E-Mail-Adresse';
}
return $errors;
}
public function onSubmit()
{
$client = new Client();
$payload = json_decode(file_get_contents('php://input'), true);
$client->post(STEPPER_URL, [
'json' => $payload,
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . STEPPER_TOKEN,
]
]);
}
}