custer fixed error replacing all emails arguments

This commit is contained in:
asus
2024-04-14 22:59:13 +02:00
parent f42ba9a1c3
commit 383de4ecb4
4 changed files with 44 additions and 58 deletions

View File

@@ -33,11 +33,6 @@ function prepare_emails_CIPF($email_name, $user_id) {
return false; return false;
} }
if (is_null($user_id)) {
$user_id = get_current_user_id();
}
$user = get_user_by('id', $user_id);
$user_email = $user->user_email;
$header_content_type = 'Content-Type: text/html; charset=UTF-8'; $header_content_type = 'Content-Type: text/html; charset=UTF-8';
@@ -59,7 +54,7 @@ function prepare_emails_CIPF($email_name, $user_id) {
$tmp_email['message'] = $email['notification_message']; $tmp_email['message'] = $email['notification_message'];
$tmp_email['headers'] = array(); $tmp_email['headers'] = array();
$tmp_email['headers'][] = $header_content_type; $tmp_email['headers'][] = $header_content_type;
$tmp_email['headers'][] = 'User_id: '.$user_id; $tmp_email['headers']['user_id'] = $user_id;
$from = $email['notification_from']; $from = $email['notification_from'];
if (empty($from)) { if (empty($from)) {
$from = get_option('admin_email'); $from = get_option('admin_email');
@@ -71,12 +66,20 @@ function prepare_emails_CIPF($email_name, $user_id) {
} }
if (isset($email['confirmation_send']) && $email['confirmation_send'] === 'on') { if (isset($email['confirmation_send']) && $email['confirmation_send'] === 'on') {
$tmp_email = array(); $tmp_email = array();
if (is_null($user_id)) {
$user_id = get_current_user_id();
}
if ($user_id === 0) {
return false;
}
$user = get_user_by('id', $user_id);
$user_email = $user->user_email;
$tmp_email['to'] = $user_email; $tmp_email['to'] = $user_email;
$tmp_email['subject'] = $email['confirmation_subject']; $tmp_email['subject'] = $email['confirmation_subject'];
$tmp_email['message'] = $email['confirmation_message']; $tmp_email['message'] = $email['confirmation_message'];
$tmp_email['headers'] = array(); $tmp_email['headers'] = array();
$tmp_email['headers'][] = $header_content_type; $tmp_email['headers'][] = $header_content_type;
$tmp_email['headers'][] = 'User_id: '.$user_id; $tmp_email['headers']['user_id'] = $user_id;
$from = $email['confirmation_from']; $from = $email['confirmation_from'];
if (empty($from)) { if (empty($from)) {
$from = get_option('admin_email'); $from = get_option('admin_email');

View File

@@ -4,7 +4,7 @@ Plugin Name: hggg_custer
Plugin URI: Plugin URI:
Description: customize user : output infos on page, on email, and change current user id momentarly Description: customize user : output infos on page, on email, and change current user id momentarly
Author: hugogogo Author: hugogogo
Version: 0.2.4 Version: 0.2.5
Author URI: Author URI:
*/ */

View File

@@ -24,33 +24,10 @@ function replace_words($matches, $user_id = null) {
$user_id = get_current_user_id(); $user_id = get_current_user_id();
$current_user = wp_get_current_user(); $current_user = wp_get_current_user();
} }
else {
return "";
}
if ($current_user === false) {
return "";
}
$query = $matches[1]; $query = $matches[1];
$result = \CUSTER\format_user_info($query, $user_id); $result = \CUSTER\format_user_info($query, $user_id);
/*
* if result is array, take the first element (not ideal)
*
*/
if (is_array($result)) {
$result = reset($result);
}
/*
* if no match, return $$<query>$$
*
*/
if (empty($result)) {
return $matches[0];
}
return $result; return $result;
} }
@@ -91,25 +68,32 @@ function filter_email_wp($args) {
$pattern = '/\$\$(.*?)\$\$/'; $pattern = '/\$\$(.*?)\$\$/';
$user_id = \CUSTER\find_user_id_from_headers($args); $user_id = \CUSTER\find_user_id_from_headers($args);
error_log("user_id: " . json_encode($user_id));
$old_to = $args['to']; /*
$new_to = preg_replace_callback($pattern, function($matches) use ($user_id) { * loop through args
return \CUSTER\replace_words($matches, $user_id); * if one arg is itself an array, loop through it
}, $old_to); * and replace all $$words$$ by their values
$args['to'] = $new_to; *
*/
$old_subject = $args['subject']; foreach ($args as $arg_key => $arg) {
$new_subject = preg_replace_callback($pattern, function($matches) use ($user_id) { if (is_array($arg)) {
return \CUSTER\replace_words($matches, $user_id); $new_arg = array();
}, $old_subject); foreach ($arg as $key => $value) {
$args['subject'] = $new_subject; $new_arg[$key] = preg_replace_callback($pattern, function($matches) use ($user_id) {
return \CUSTER\replace_words($matches, $user_id);
$old_message = $args['message']; }, $value);
$new_message = preg_replace_callback($pattern, function($matches) use ($user_id) { }
return \CUSTER\replace_words($matches, $user_id); }
}, $old_message); else {
$args['message'] = $new_message; $new_arg = preg_replace_callback($pattern, function($matches) use ($user_id) {
return \CUSTER\replace_words($matches, $user_id);
}, $arg);
}
$args[$arg_key] = $new_arg;
}
error_log("args: " . json_encode($args));
return $args; return $args;
} }
add_filter('wp_mail', __NAMESPACE__.'\filter_email_wp', 10, 1); add_filter('wp_mail', __NAMESPACE__.'\filter_email_wp', 10, 1);
@@ -145,15 +129,13 @@ function find_user_id_from_headers(&$args) {
} }
$user_id = null; $user_id = null;
$headers = $args['headers']; $headers = $args['headers'];
foreach ($headers as $key => $header) { // isset returns false if the key exists but its value is null : 'user_id'=>null : https://stackoverflow.com/questions/3803282/check-if-value-isset-and-null
$explode_header = explode(':', $header); if (array_key_exists('user_id', $headers)) {
$explode_header = array_map('trim', $explode_header); $user_id = $headers['user_id'];
if ($explode_header[0] === 'User_id') { unset($headers['user_id']);
$user_id = $explode_header[1]; $headers = array_values($headers);
unset($headers[$key]);
$headers = array_values($headers);
}
} }
$args['headers'] = $headers; $args['headers'] = $headers;
return $user_id; return $user_id;
} }

View File

@@ -60,8 +60,9 @@ function find_user_post_url($user_id) {
* *
* *
*/ */
function find_admin_email($user_id) { function find_admin_email($user_id = null) {
$admin_email = get_option( 'admin_email' ); $admin_email = get_option( 'admin_email' );
error_log("admin_email: " . json_encode($admin_email));
return $admin_email; return $admin_email;
} }
@@ -72,7 +73,7 @@ function find_admin_email($user_id) {
* *
* *
*/ */
function find_base_url($user_id) { function find_base_url($user_id = null) {
$base_url = home_url(); $base_url = home_url();
return $base_url; return $base_url;
} }