| 1 |
<?php |
| 2 |
|
| 3 |
namespace AgeGate\Controller; |
| 4 |
|
| 5 |
use AgeGate\App\AgeGate; |
| 6 |
use Asylum\Utility\View; |
| 7 |
use AgeGate\Common\Settings; |
| 8 |
use AgeGate\Utility\Encrypt; |
| 9 |
use Asylum\Validation\Validator; |
| 10 |
use AgeGate\Presentation\Attribute; |
| 11 |
use AgeGate\Presentation\ClassNames; |
| 12 |
use League\CommonMark\MarkdownConverter; |
| 13 |
use Asylum\Utility\Facades\StringTemplate; |
| 14 |
use League\CommonMark\CommonMarkConverter; |
| 15 |
use League\CommonMark\Environment\Environment; |
| 16 |
use League\CommonMark\Extension\InlinesOnly\InlinesOnlyExtension; |
| 17 |
|
| 18 |
class ViewController |
| 19 |
{ |
| 20 |
private $empty = AGE_GATE_PATH . 'src/Resources/views/empty'; |
| 21 |
|
| 22 |
protected $view; |
| 23 |
|
| 24 |
private static $errors = []; |
| 25 |
|
| 26 |
public function __construct($settings = null, $content = null) |
| 27 |
{ |
| 28 |
|
| 29 |
$theme = is_dir(get_stylesheet_directory() . '/age-gate') ? get_stylesheet_directory() . '/age-gate' : $this->empty; |
| 30 |
|
| 31 |
$settings = Settings::getInstance(); |
| 32 |
|
| 33 |
$map = [ |
| 34 |
'y' => 'Year', |
| 35 |
'm' => 'Month', |
| 36 |
'd' => 'Day' |
| 37 |
]; |
| 38 |
|
| 39 |
$this->view = new View(AGE_GATE_PATH . 'src/Resources/views/public'); |
| 40 |
$this->view |
| 41 |
->addData( |
| 42 |
[ |
| 43 |
'settings' => $settings, |
| 44 |
'content' => AgeGate::getContent(), |
| 45 |
'encrypt' => new Encrypt, |
| 46 |
'errors' => StandardController::$errors, |
| 47 |
] |
| 48 |
) |
| 49 |
->addData( |
| 50 |
[ |
| 51 |
'fields' => collect(explode(' ', $settings->dateFormat))->mapWithKeys(function ($item) use ($map, $settings) { |
| 52 |
$key = strtolower($item[0]); |
| 53 |
$label = 'label' . $map[$key]; |
| 54 |
$placeholder = 'placeholder' . $map[$key]; |
| 55 |
|
| 56 |
return [ |
| 57 |
$key => [ |
| 58 |
'label' => $settings->$label, |
| 59 |
'placeholder' => $settings->$placeholder, |
| 60 |
'value' => ((int) (wp_unslash($_POST['age_gate'][$key] ?? 0))) ?: apply_filters('age_gate/field/' . strtolower($map[$key]) . '/value', ''), // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 61 |
'errors' => Validator::get_instance()->get_errors_array(), |
| 62 |
'options' => $settings->inputType === 'selects' ? $this->getOptions($key, $settings) : [], |
| 63 |
] |
| 64 |
]; |
| 65 |
})->toArray() |
| 66 |
] |
| 67 |
) |
| 68 |
->addFunction('attr', [Attribute::class, 'attr']) |
| 69 |
->addFunction('stringTemplate', [StringTemplate::class, 'render']) |
| 70 |
->addFunction('mdLine', [$this, 'line']) |
| 71 |
->addFunction('mdText', [$this, 'text']); |
| 72 |
|
| 73 |
if (is_dir($theme)) { |
| 74 |
$this->view->addFolder('theme', $theme); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
public function line($string) |
| 79 |
{ |
| 80 |
// Define your configuration, if needed |
| 81 |
$config = []; |
| 82 |
|
| 83 |
// Create a new, empty environment |
| 84 |
$environment = new Environment($config); |
| 85 |
|
| 86 |
// Add this extension |
| 87 |
$environment->addExtension(new InlinesOnlyExtension()); |
| 88 |
|
| 89 |
// Instantiate the converter engine and start converting some Markdown! |
| 90 |
$converter = new MarkdownConverter($environment); |
| 91 |
return $converter->convert($string); |
| 92 |
return (new CommonMarkConverter) |
| 93 |
->convert($string); |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
public function text($string) |
| 98 |
{ |
| 99 |
return (new CommonMarkConverter) |
| 100 |
->convert($string); |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
public function getView() |
| 105 |
{ |
| 106 |
return $this->view; |
| 107 |
} |
| 108 |
|
| 109 |
public function render() |
| 110 |
{ |
| 111 |
$this->view |
| 112 |
->render('age-gate'); |
| 113 |
} |
| 114 |
|
| 115 |
public static function setErrors($errors) |
| 116 |
{ |
| 117 |
self::$errors = $errors; |
| 118 |
} |
| 119 |
|
| 120 |
private function getOptions($key, $settings) |
| 121 |
{ |
| 122 |
switch ($key) { |
| 123 |
case 'm': |
| 124 |
$display = apply_filters('age_gate/form/select/month/format', 'M'); |
| 125 |
$range = range(1, 12); |
| 126 |
return collect($range)->mapWithKeys(function ($month) use ($display) { |
| 127 |
$month = str_pad($month, 2, "0", STR_PAD_LEFT); |
| 128 |
return [$month => date_i18n($display, strtotime("2022-$month-01"))]; |
| 129 |
})->toArray(); |
| 130 |
|
| 131 |
case 'd': |
| 132 |
return collect(range(1, 31))->mapWithKeys(function ($day) { |
| 133 |
$day = str_pad($day, 2, "0", STR_PAD_LEFT); |
| 134 |
return [$day => $day]; |
| 135 |
})->toArray(); |
| 136 |
case 'y': |
| 137 |
$years = collect(range(apply_filters('age_gate/form/select/year/min', 1900), 2022))->mapWithKeys(function ($year) { |
| 138 |
return[$year => $year]; |
| 139 |
}); |
| 140 |
|
| 141 |
if ($settings->yearOrder === 'high-low') { |
| 142 |
return $years->reverse()->toArray(); |
| 143 |
} else { |
| 144 |
return $years->toArray(); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// |
| 151 |
(new ViewController)->render(); |
| 152 |
|