1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
<?php
namespace SIW\Newsletter;
use SIW\Util;
use SIW\Newsletter\Hash;
class Confirmation_Page {
protected $message;
public static function init() {
$self = new self();
add_action( 'wp', [ $self, 'maybe_process_confirmation'] );
add_filter( 'template_include', [ $self, 'load_template' ] );
add_action( 'siw_newsletter_confirmation', [ $self, 'show_message'] );
}
public function maybe_process_confirmation() {
if ( ! $this->is_newsletter_confirmation() ) {
return;
}
$hash = urldecode( Util::get_request_parameter( 'nl_hash' ) );
$data = base64_decode( urldecode( Util::get_request_parameter( 'nl_data' ) ) );
if ( ! Hash::data_is_valid( $data, $hash ) ) {
$this->message = __( 'Helaas is er iets misgegaan met de aanmelding.', 'siw' );
return;
}
if ( get_transient( "siw_newsletter_confirm_{$hash}" ) ) {
$this->message = __( 'Je bent al aangemeld voor de SIW-nieuwsbrief.', 'siw' );
return;
}
$data = json_decode( $data, true );
if ( siw_newsletter_subscribe( $data['email'], $data['list_id'], $data['properties'] ) ) {
$this->message = __( 'Gefeliciteerd! Je bent nu aangemeld voor de SIW-nieuwsbrief.', 'siw' );
set_transient( "siw_newsletter_confirm_{$hash}", true, DAY_IN_SECONDS );
}
else {
$this->message = __( 'Helaas is er iets misgegaan met de aanmelding.', 'siw' );
}
}
public function load_template( $template ) {
if ( $this->is_newsletter_confirmation() ) {
$template = SIW_TEMPLATES_DIR . '/newsletter-confirmation.php';
}
return $template;
}
public function show_message() {
echo wp_kses_post( $this->message );
}
protected function is_newsletter_confirmation() {
return is_front_page() && (bool) Util::get_request_parameter( 'nl_confirmation');
}
}