Skip to content

Commit

Permalink
Admin notification email template.
Browse files Browse the repository at this point in the history
Available meta shortcodes from las user message.
  • Loading branch information
reksar committed Apr 7, 2022
1 parent ae0f014 commit 7e9b4b4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ rmlog-mail:
- rm tmp/smtp/log/maillog

.PHONY: log
log: log-wp log-mail
log: log-mail log-wp

.PHONY: rmlog
rmlog: rmlog-wp rmlog-mail
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
function notify_email_template()
{
printf(
'<p>%s %s</p><p>%s: <b>[email]</b></p>',
'<p>%s %s</p><p>%s: <b>[email] [_date] [_time] [_remote_ip]</b></p>',
__('Admin notify email template.'),
__('HTML format is available.'),
__('Sortcodes'));
Expand Down
8 changes: 7 additions & 1 deletion cormorant/core/actions/clear-expired-contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ function run()
$expired_contacts = \contact\find_unconfirmed_in($days_to_confirm);

foreach ($expired_contacts as $contact) {
$contact->delete_related_messages();
delete_related_messages($contact);
$contact->delete();
}
}

function delete_related_messages($contact)
{
foreach ($contact->related_messages() as $message)
$message->delete();
}
32 changes: 31 additions & 1 deletion cormorant/core/contact/admin-notify-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
const HEADERS = "Content-Type: text/html; charset=UTF-8\n";
const EMAIL_SHORTCODE = '[email]';

// Available shortcodes based on these keys: "[_<key>]".
// See `replace_meta_shortcodes()`.
const META_KEYS = [
'date',
'time',
'remote_ip',
];

function send($contact)
{
$admin_email = get_option('admin_email');
Expand All @@ -14,9 +22,31 @@ function send($contact)
}

// TODO: more shortcodes from CF7.
// TODO: date/time format.
function body($contact)
{
$template = \settings\get('notify_email_template') ?: EMAIL_SHORTCODE;

$messages = $contact->related_messages();
$last_message = end($messages);
$message_meta = $last_message->meta;
$text = replace_meta_shortcodes($template, $message_meta);

$email = $contact->email();
return str_replace(EMAIL_SHORTCODE , $email, $template);
return str_replace(EMAIL_SHORTCODE, $email, $text);
}

function replace_meta_shortcodes($template, $message_meta)
{
$meta = array_intersect_key($message_meta, array_flip(META_KEYS));

return array_reduce(array_keys($meta),

function($text, $key) use ($meta) {
$shortcode = "[_$key]";
$value = $meta[$key];
return str_replace($shortcode, $value, $text);
},

$template);
}
5 changes: 2 additions & 3 deletions cormorant/core/contact/class-contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ public function delete()
$this->contact->delete();
}

public function delete_related_messages()
public function related_messages()
{
foreach (\flamingo\find_messages_from($this->email()) as $message)
$message->delete();
return \flamingo\find_messages_from($this->email());
}

public function is_confirmed()
Expand Down

0 comments on commit 7e9b4b4

Please sign in to comment.