| 1 |
<?php |
| 2 |
|
| 3 |
namespace AgeGate\Shortcode; |
| 4 |
|
| 5 |
use Asylum\Utility\View; |
| 6 |
use AgeGate\Utility\Cookie; |
| 7 |
use AgeGate\Common\Settings; |
| 8 |
use AgeGate\Controller\StandardController; |
| 9 |
use AgeGate\Utility\Encrypt; |
| 10 |
use Asylum\Validation\Validator; |
| 11 |
use AgeGate\Presentation\Attribute; |
| 12 |
use Asylum\Utility\Facades\Parsedown; |
| 13 |
use Asylum\Utility\Facades\StringTemplate; |
| 14 |
use Asylum\Common\Shortcode as AbstractShortcode; |
| 15 |
use AgeGate\Shortcode\ShortcodeContent as Content; |
| 16 |
use AgeGate\Utility\Crawler; |
| 17 |
|
| 18 |
class Shortcode extends AbstractShortcode |
| 19 |
{ |
| 20 |
protected $shortcode = 'age-gate'; |
| 21 |
|
| 22 |
private static $count = 0; |
| 23 |
|
| 24 |
public function registerShortcode($atts, $content, $tag) |
| 25 |
{ |
| 26 |
$settings = Settings::getInstance(); |
| 27 |
self::$count = self::$count + 1; |
| 28 |
|
| 29 |
$atts = shortcode_atts( |
| 30 |
[ |
| 31 |
'age' => $settings->defaultAge, |
| 32 |
'title' => $settings->labelButtons, |
| 33 |
'type' => $settings->inputType, |
| 34 |
'poster' => false, |
| 35 |
'force' => false, |
| 36 |
], |
| 37 |
$atts, |
| 38 |
'age-gate' |
| 39 |
); |
| 40 |
|
| 41 |
|
| 42 |
if ($this->status($settings, $atts) === true || $settings->method === 'standard' && Crawler::isBot()) { |
| 43 |
return $content; |
| 44 |
} |
| 45 |
|
| 46 |
if (!is_singular()) { |
| 47 |
|
| 48 |
return ''; |
| 49 |
} |
| 50 |
|
| 51 |
$shortcodeSettings = clone($settings); |
| 52 |
$shortcodeSettings->set('inputType', $atts['type']); |
| 53 |
$shortcodeSettings->set('labelButtons', $atts['title']); |
| 54 |
|
| 55 |
if ($atts['poster'] ?? false) { |
| 56 |
if (is_numeric($atts['poster'])) { |
| 57 |
$shortcodeSettings->set('poster', wp_get_attachment_url($atts['poster'])); |
| 58 |
} else { |
| 59 |
$shortcodeSettings->set('poster', $atts['poster']); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
$shortcodeContent = new Content(null, 'shortcode', $shortcodeSettings); |
| 64 |
$shortcodeContent->setAge($atts['age']); |
| 65 |
|
| 66 |
$view = $this->view($shortcodeSettings, $shortcodeContent); |
| 67 |
|
| 68 |
if ($settings->method === 'js') { |
| 69 |
wp_enqueue_script('age-gate-shortcode'); |
| 70 |
$view->addData([ |
| 71 |
'restrictedContent' => base64_encode($content), |
| 72 |
'options' => base64_encode(json_encode(array_merge($atts, ['cookieName' => $settings->getCookieName(), 'cookieDomain' => Cookie::getDomain()]))), |
| 73 |
'c' => self::$count, |
| 74 |
]); |
| 75 |
} else { |
| 76 |
$view->addData([ |
| 77 |
'e' => StandardController::$errors, |
| 78 |
'c' => self::$count, |
| 79 |
// Nonce cannot be generated in cached sites |
| 80 |
'postData' => wp_unslash($_POST ?? []), // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 81 |
]); |
| 82 |
} |
| 83 |
|
| 84 |
$markup = $view->compile('shortcode/shortcode-' . $settings->method); |
| 85 |
|
| 86 |
// remove the attribute |
| 87 |
return $markup; |
| 88 |
} |
| 89 |
|
| 90 |
private function view($settings, $content) |
| 91 |
{ |
| 92 |
$map = [ |
| 93 |
'y' => 'Year', |
| 94 |
'm' => 'Month', |
| 95 |
'd' => 'Day' |
| 96 |
]; |
| 97 |
|
| 98 |
$view = new View(AGE_GATE_PATH . 'src/Resources/views/public'); |
| 99 |
$view |
| 100 |
->addData( |
| 101 |
[ |
| 102 |
'settings' => $settings, |
| 103 |
'content' => $content, |
| 104 |
'encrypt' => new Encrypt(), |
| 105 |
'errors' => [], |
| 106 |
] |
| 107 |
) |
| 108 |
->addData( |
| 109 |
[ |
| 110 |
'fields' => collect(explode(' ', $settings->dateFormat))->mapWithKeys(function ($item) use ($map, $settings) { |
| 111 |
$key = strtolower($item[0]); |
| 112 |
$label = 'label' . $map[$key]; |
| 113 |
$placeholder = 'placeholder' . $map[$key]; |
| 114 |
|
| 115 |
return [ |
| 116 |
$key => [ |
| 117 |
'label' => $settings->$label, |
| 118 |
'placeholder' => $settings->$placeholder, |
| 119 |
'value' => sanitize_text_field(wp_unslash($_POST['age_gate'][$key] ?? apply_filters('age_gate/field/' . strtolower($map[$key]) . '/value', ''))), // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 120 |
'errors' => Validator::get_instance()->get_errors_array(), |
| 121 |
'options' => $settings->inputType === 'selects' ? $this->getOptions($key, $settings) : [], |
| 122 |
] |
| 123 |
]; |
| 124 |
})->toArray() |
| 125 |
] |
| 126 |
) |
| 127 |
->addFunction('attr', [Attribute::class, 'attr']) |
| 128 |
->addFunction('stringTemplate', [StringTemplate::class, 'render']) |
| 129 |
->addFunction('mdLine', [Parsedown::class, 'line']) |
| 130 |
->addFunction('mdText', [Parsedown::class, 'text']) |
| 131 |
->addFolder('theme', AGE_GATE_PATH . 'src/Resources/views/empty'); |
| 132 |
return $view; |
| 133 |
} |
| 134 |
|
| 135 |
private function getOptions($key, $settings) |
| 136 |
{ |
| 137 |
switch ($key) { |
| 138 |
case 'm': |
| 139 |
$display = apply_filters('age_gate/form/select/month/format', 'M'); |
| 140 |
$range = range(1, 12); |
| 141 |
return collect($range)->mapWithKeys(function ($month) use ($display) { |
| 142 |
$month = str_pad($month, 2, "0", STR_PAD_LEFT); |
| 143 |
return [$month => date_i18n($display, strtotime("2022-$month-01"))]; |
| 144 |
})->toArray(); |
| 145 |
|
| 146 |
case 'd': |
| 147 |
return collect(range(1, 31))->mapWithKeys(function ($day) { |
| 148 |
$day = str_pad($day, 2, "0", STR_PAD_LEFT); |
| 149 |
return [$day => $day]; |
| 150 |
})->toArray(); |
| 151 |
case 'y': |
| 152 |
$years = collect(range(apply_filters('age_gate/form/select/year/min', 1900), 2022))->mapWithKeys(function ($year) { |
| 153 |
return[$year => $year]; |
| 154 |
}); |
| 155 |
|
| 156 |
if ($settings->yearOrder === 'high-low') { |
| 157 |
return $years->reverse()->toArray(); |
| 158 |
} else { |
| 159 |
return $years->toArray(); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
private function status($settings, $atts) |
| 165 |
{ |
| 166 |
if ($settings->method === 'js') { |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
$cookie = Cookie::get($settings->getCookieName()); |
| 171 |
|
| 172 |
if ($settings->anonymous && $cookie) { |
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
if ($cookie >= $atts['age']) { |
| 177 |
return true; |
| 178 |
} |
| 179 |
|
| 180 |
return false; |
| 181 |
} |
| 182 |
} |
| 183 |
|