upgraded email filter to filter also wp hook

This commit is contained in:
asus
2024-03-05 09:55:54 +01:00
parent 63cc1f28c0
commit 39c9f492dc
2 changed files with 98 additions and 147 deletions

View File

@@ -0,0 +1,94 @@
<?php
/*
* filter emails in the form-builder hook, before the wp_mail hook
* it receives the id of the user, no need to have the user still logged-in
*/
function filter_email_fb_CIPF($reply_body, $post_array) {
$id = $post_array['ID'];
// pattern : anything surrounded by '$$', ex : $$value$$
$pattern = '/\$\$(.*?)\$\$/';
// old version, using an outside callback, better readibility but cannot pass $id
//$new_body = preg_replace_callback($pattern, 'replace_words_CIPF', $reply_body);
// inline callback, with 'use' to get the id
$new_body = preg_replace_callback($pattern, function($matches) use ($id) {
$current_user = get_user_by('id', $id);
$query = $matches[1];
$result = $current_user->$query;
return $result;
}, $reply_body);
return $new_body;
}
add_filter('df_confirmation_body', 'filter_email_fb_CIPF', 10, 2); // the receive an email
add_filter('df_notification_body', 'filter_email_fb_CIPF', 10, 2); // the administrator receive a notification
/*
* callback to provide the user info corresponding to the $$key_word$$
*/
function replace_words_CIPF($matches) {
$current_user = wp_get_current_user();
if ($current_user->ID === 0)
return "[no user logged in]";
$query = $matches[1];
$result = $current_user->$query;
return $result;
}
/*
* filter emails at the final point : the wp_mail hook
* it uses a callback that rely on the logged-in user
* it will works well most of the time, but it's possible
* that a user logged out before the email is sent
* or event that a different user has already logged in
*/
function filter_email_wp_CIPF($args) {
error_log("in filter_email_wp_CIPF");
// pattern : anything surrounded by '$$', ex : $$value$$
$pattern = '/\$\$(.*?)\$\$/';
$old_body = $args['message'];
$new_body = preg_replace_callback($pattern, 'replace_words_CIPF', $old_body);
$args['message'] = $new_body;
return $args;
}
add_filter('wp_mail', 'filter_email_wp_CIPF', 10, 1);
/*
all filters in form_builder :
1 $body = apply_filters( 'df_notification_body', $body, $post_array );
2 $email = apply_filters( 'df_notifcation_recipient', $email, $form_id, $post_array );
3 $title = apply_filters( 'wpml_translate_single_string', $title_get, 'divi-form-builder', 'Edit Post Button Title Text' );
4 $body = apply_filters( 'df_contact_body', $body, $post_array );
5 $body = apply_filters( 'df_contact_body', $body, $post_array );
6 $email = apply_filters( 'df_contact_recipient', $email, $form_id, $post_array );
7 $email = apply_filters( 'df_contact_recipient', $email, $form_id, $processed_post_array );
8 $reply_body = apply_filters( 'df_confirmation_body', $reply_body, $post_array );
9 $reply_body = apply_filters( 'df_confirmation_body', $reply_body, $post_array );
10 $reply_body = apply_filters( 'df_confirmation_body', $reply_body, $post_array );
11 $reply_body = apply_filters( 'df_confirmation_body', $reply_body, $post_array );
12 $message_content = apply_filters( 'the_content', get_post_field( 'post_content', $message_array['layout'] ) );
13 $content = apply_filters( 'the_content', get_post_field( 'post_content', $message_array['layout'] ) );
14 $content = apply_filters( 'the_content', get_post_field( 'post_content', $html_content_divi_layout) );
*/
?>