| 1 |
<?php |
| 2 |
|
| 3 |
namespace AgeGate\Controller; |
| 4 |
|
| 5 |
use AgeGate\Utility\Cookie; |
| 6 |
use AgeGate\Common\Controller\AbstractController; |
| 7 |
use AgeGate\Common\Interfaces\ControllerInterface; |
| 8 |
|
| 9 |
class JsController extends AbstractController implements ControllerInterface |
| 10 |
{ |
| 11 |
|
| 12 |
protected $templateHook = 'wp_footer'; |
| 13 |
|
| 14 |
public function init(): void |
| 15 |
{ |
| 16 |
$this->templateHook = $this->settings->inHeader ? 'wp_head' : 'wp_footer'; |
| 17 |
add_action($this->templateHook, [$this, 'template'], 2); |
| 18 |
add_action('wp_head', [$this, 'user']); |
| 19 |
|
| 20 |
add_action('wp_enqueue_scripts', function(){ |
| 21 |
if ($this->settings->jsHooks) { |
| 22 |
$path = sprintf('%s%s%s', AGE_GATE_URL, 'dist', ($this->settings->rawAssets ? '/raw' : '')); |
| 23 |
|
| 24 |
wp_enqueue_script('age-gate-hooks', $path . '/hooks.js', [], AGE_GATE_VERSION, !$this->settings->inHeader); |
| 25 |
} |
| 26 |
|
| 27 |
}, 1); |
| 28 |
} |
| 29 |
|
| 30 |
public function assets(): void |
| 31 |
{ |
| 32 |
if ($this->content->getRestricted()) { |
| 33 |
$path = sprintf('%s%s%s', AGE_GATE_URL, 'dist', ($this->settings->rawAssets ? '/raw' : '')); |
| 34 |
|
| 35 |
wp_register_script('age-gate-stepped', $path . '/stepped.js', [], AGE_GATE_VERSION, !$this->settings->inHeader); |
| 36 |
|
| 37 |
wp_enqueue_style('age-gate'); |
| 38 |
wp_enqueue_script('age-gate', $path . '/age-gate.js', $this->getScriptDependencies(), AGE_GATE_VERSION, !$this->settings->inHeader); |
| 39 |
|
| 40 |
$data = [ |
| 41 |
'cookieDomain' => Cookie::getDomain(), |
| 42 |
'cookieName' => $this->settings->getCookieName(), |
| 43 |
'age' => $this->content->getAge(), |
| 44 |
'css' => $this->settings->cssType, |
| 45 |
'userAgents' => array_filter(apply_filters('age_gate/settings/bots', preg_split('/\r\n|\r|\n/', $this->settings->userAgents ?: ''))), |
| 46 |
'switchTitle' => false, |
| 47 |
'rechallenge' => $this->settings->rechallenge, |
| 48 |
'error' => $this->settings->errorFailed, |
| 49 |
'generic' => $this->settings->errorGeneric, |
| 50 |
'uri' => rest_url('/age-gate/v3/check'), |
| 51 |
'useLocalStorage' => $this->settings->useLocalStorage, |
| 52 |
]; |
| 53 |
|
| 54 |
wp_localize_script('age-gate-stepped', 'ag_stepped', ['age' => $this->content->getAge()]); |
| 55 |
|
| 56 |
if ($this->settings->switchTitle) { |
| 57 |
$data['customTitle'] = $this->title->getTitle(); |
| 58 |
} |
| 59 |
|
| 60 |
if ($this->settings->viewport) { |
| 61 |
$data['viewport'] = true; |
| 62 |
} |
| 63 |
|
| 64 |
if (!$this->settings->disableAjaxFallback) { |
| 65 |
$data['fallback'] = esc_url(admin_url('admin-ajax.php')); |
| 66 |
} |
| 67 |
|
| 68 |
if (!$this->settings->munge) { |
| 69 |
wp_localize_script('age-gate', 'age_gate', $data); |
| 70 |
} else { |
| 71 |
// add_action($this->templateHook, function () use ($data) { |
| 72 |
if ($data['customTitle'] ?? false) { |
| 73 |
age_gate_add_attribute('age-gate', 'data-title', $data['customTitle']); |
| 74 |
} |
| 75 |
echo '<meta name="age-gate" content="" data-ag-munge="' . base64_encode(json_encode($data)) . '" />'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 76 |
// }, 1); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
public function template() |
| 82 |
{ |
| 83 |
// add dialog attributes |
| 84 |
age_gate_add_attribute('age-gate', 'role', "dialog"); |
| 85 |
age_gate_add_attribute('age-gate', 'aria-modal', "true"); |
| 86 |
age_gate_add_attribute('age-gate', 'aria-label', $this->settings->labelAria); |
| 87 |
|
| 88 |
require_once AGE_GATE_PATH . 'src/Controller/ViewController.php'; |
| 89 |
} |
| 90 |
|
| 91 |
public function user() |
| 92 |
{ |
| 93 |
if ($this->settings->ignoreLogged && is_user_logged_in()) { |
| 94 |
echo '<script>var ag_logged_in = true;</script>'; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
private function getScriptDependencies() |
| 99 |
{ |
| 100 |
$deps = [ |
| 101 |
]; |
| 102 |
|
| 103 |
if ($this->settings->jsHooks) { |
| 104 |
$deps[] = 'age-gate-hooks'; |
| 105 |
} |
| 106 |
// if ($this->settings->stepped) { |
| 107 |
// $deps[] = 'age-gate-stepped'; |
| 108 |
// } |
| 109 |
|
| 110 |
// if ($this->settings->focus) { |
| 111 |
// $deps[] = 'age-gate-focus'; |
| 112 |
// } |
| 113 |
|
| 114 |
// if ($this->settings->devTools) { |
| 115 |
// $deps[] = 'age-gate-interaction'; |
| 116 |
// } |
| 117 |
|
| 118 |
// if ($this->settings->inputAutoTab) { |
| 119 |
// $deps[] = 'age-gate-autotab'; |
| 120 |
// } |
| 121 |
|
| 122 |
return $deps; |
| 123 |
} |
| 124 |
} |
| 125 |
|