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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
<?php
namespace SIW\Newsletter;
use SIW\Email\Template;
use SIW\Properties;
use SIW\HTML;
use SIW\Newsletter\Hash;
class Confirmation_Email {
protected $email;
protected $list_id;
protected $properties;
protected $email_settings;
public function __construct( string $email, int $list_id, array $properties ) {
$this->email = $email;
$this->list_id = $list_id;
$properties = wp_parse_args(
$properties,
[
'firstname' => '',
'lastname' => ''
]
);
$this->properties = $properties;
$this->email_settings = siw_get_email_settings( 'newsletter' );
}
public function send() {
$email_hash = sha1( $this->email );
if ( get_transient( "siw_newsletter_email_{$email_hash}" ) ) {
return false;
}
$headers = [
'Content-Type: text/html; charset=UTF-8',
sprintf( 'From: %s <%s>', Properties::NAME, $this->email_settings['email'] ),
];
$result = wp_mail(
$this->email,
__( 'Bevestig je aanmelding voor onze nieuwsbrief', 'siw' ),
$this->generate_message(),
$headers
);
set_transient( "siw_newsletter_email_{$email_hash}", true, HOUR_IN_SECONDS );
return $result;
}
protected function generate_message() {
$template_args = [
'subject' => __( 'Aanmelding nieuwsbrief', 'siw' ),
'message' => implode(
'',
[
sprintf( __('Beste %s,', 'siw' ), $this->properties['firstname'] ). BR2,
__( 'Bedankt voor je aanmelding voor de SIW-nieuwsbrief!', 'siw' ) . SPACE,
__( 'Om zeker te weten dat je inschrijving correct is, vragen we je je aanmelding te bevestigen.', 'siw' ) . BR2,
HTML::generate_link(
$this->generate_confirmation_url(),
__( 'Klik hier om je aanmelding voor onze nieuwsbrief direct te bevestigen.', 'siw' )
) . BR2,
sprintf( __( 'Tip: voeg %s toe aan je adresboek.', 'siw' ), $this->email_settings['email'] ) . SPACE,
__( 'Zo mis je nooit meer nieuws over onze infodagen, ervaringsverhalen of projecten.', 'siw' ),
]
),
'show_signature' => true,
'signature_name' => $this->email_settings['name'],
'signature_title' => $this->email_settings['title'],
];
$template = new Template( $template_args );
return $template->generate();
}
protected function generate_confirmation_url() {
$data = [
'email' => $this->email,
'list_id' => $this->list_id,
'properties' => $this->properties,
];
$json_data = json_encode( $data );
return add_query_arg(
[
'nl_confirmation' => true,
'nl_data' => urlencode( base64_encode( $json_data ) ),
'nl_hash' => urlencode( Hash::generate_hash( $json_data ) ),
],
SIW_SITE_URL
);
}
}