| 1 |
<?php |
| 2 |
|
| 3 |
namespace AgeGate\Controller; |
| 4 |
|
| 5 |
use AgeGate\Utility\Cookie; |
| 6 |
use AgeGate\Common\Content; |
| 7 |
use AgeGate\Common\Form\Submit; |
| 8 |
use AgeGate\Presentation\ClassNames; |
| 9 |
use AgeGate\Common\Controller\AbstractController; |
| 10 |
use AgeGate\Common\Interfaces\ControllerInterface; |
| 11 |
|
| 12 |
class StandardController extends AbstractController implements ControllerInterface |
| 13 |
{ |
| 14 |
use ClassNames; |
| 15 |
|
| 16 |
public static $errors = []; |
| 17 |
|
| 18 |
public function __construct(Content $c) |
| 19 |
{ |
| 20 |
parent::__construct($c); |
| 21 |
|
| 22 |
$postData = wp_unslash($_POST); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 23 |
|
| 24 |
if (isset($postData['age_gate'])) { |
| 25 |
$response = (new Submit($postData, $this->settings))->validate(); |
| 26 |
|
| 27 |
if ($response['status'] === true) { |
| 28 |
|
| 29 |
if ($response['set_cookie']) { |
| 30 |
Cookie::set($this->settings->getCookieName(), $response['data']['user_age'], $response['cookieLength'] ?? 0); |
| 31 |
|
| 32 |
if (filter_var(wp_unslash($_COOKIE[$this->settings->getCookieName() . '_failed'] ?? false, FILTER_VALIDATE_BOOLEAN))) { |
| 33 |
Cookie::destroy($this->settings->getCookieName() . '_failed'); |
| 34 |
} |
| 35 |
} else { |
| 36 |
$_COOKIE[$this->settings->getCookieName()] = $response['data']['user_age']; |
| 37 |
} |
| 38 |
|
| 39 |
do_action('age_gate/passed', $response); |
| 40 |
wp_safe_redirect(esc_url_raw($response['redirect']), 303, 'AGE-GATE'); |
| 41 |
exit; |
| 42 |
} |
| 43 |
|
| 44 |
global $wp; |
| 45 |
if ($response['set_cookie']) { |
| 46 |
Cookie::set($this->settings->getCookieName() . '_failed', 1); |
| 47 |
} |
| 48 |
|
| 49 |
if ($wp->request !== $response['redirect']) { |
| 50 |
wp_redirect(esc_url_raw($response['redirect']), 303, 'AGE-GATE'); |
| 51 |
} |
| 52 |
|
| 53 |
if ($this->settings->method === 'standard' && !$this->settings->rechallenge) { |
| 54 |
$this->settings->lockout = true; |
| 55 |
|
| 56 |
StandardController::$errors = [ |
| 57 |
$this->settings->errorFailed, |
| 58 |
]; |
| 59 |
} |
| 60 |
|
| 61 |
self::$errors = $response['errors']; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public function init(): void |
| 66 |
{ |
| 67 |
$cookie = $this->settings->getCookieName() . '_failed'; |
| 68 |
|
| 69 |
if ($this->settings->method === 'standard' && !$this->settings->rechallenge && Cookie::get($cookie)) { |
| 70 |
|
| 71 |
remove_all_actions('age_gate/fields'); |
| 72 |
remove_all_actions('age_gate/custom/after'); |
| 73 |
|
| 74 |
$this->settings->lockout = true; |
| 75 |
|
| 76 |
StandardController::$errors = [ |
| 77 |
$this->settings->errorFailed, |
| 78 |
]; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
add_filter('template_include', [$this, 'template']); |
| 83 |
} |
| 84 |
|
| 85 |
public function assets(): void |
| 86 |
{ |
| 87 |
wp_register_script('age-gate-stepped', AGE_GATE_URL . 'dist/stepped.js', [], AGE_GATE_VERSION, true); |
| 88 |
wp_localize_script('age-gate-stepped', 'ag_stepped', ['age' => $this->content->getAge()]); |
| 89 |
|
| 90 |
if ($this->content->isRestricted()) { |
| 91 |
wp_enqueue_script('age-gate-standard', AGE_GATE_URL . 'dist/standard.js', [], AGE_GATE_VERSION, true); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
public function template($template) |
| 96 |
{ |
| 97 |
$show = apply_filters('age_gate/show', true, $this->content); |
| 98 |
if ($this->content->isRestricted() && $show) { |
| 99 |
wp_enqueue_style('age-gate'); |
| 100 |
|
| 101 |
return AGE_GATE_PATH . 'src/Controller/ViewController.php'; |
| 102 |
} |
| 103 |
|
| 104 |
return $template; |
| 105 |
} |
| 106 |
} |
| 107 |
|