From 383de4ecb47242a32fbc72eadce52fe57891dc2e Mon Sep 17 00:00:00 2001 From: asus Date: Sun, 14 Apr 2024 22:59:13 +0200 Subject: [PATCH] custer fixed error replacing all emails arguments --- plugins/cipf_plugin/php/_actions_emails.php | 17 +++-- plugins/custer/custer.php | 2 +- plugins/custer/filter_mail.php | 78 ++++++++------------- plugins/custer/queries.php | 5 +- 4 files changed, 44 insertions(+), 58 deletions(-) diff --git a/plugins/cipf_plugin/php/_actions_emails.php b/plugins/cipf_plugin/php/_actions_emails.php index 7945926..bf7396b 100644 --- a/plugins/cipf_plugin/php/_actions_emails.php +++ b/plugins/cipf_plugin/php/_actions_emails.php @@ -33,11 +33,6 @@ function prepare_emails_CIPF($email_name, $user_id) { 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'; @@ -59,7 +54,7 @@ function prepare_emails_CIPF($email_name, $user_id) { $tmp_email['message'] = $email['notification_message']; $tmp_email['headers'] = array(); $tmp_email['headers'][] = $header_content_type; - $tmp_email['headers'][] = 'User_id: '.$user_id; + $tmp_email['headers']['user_id'] = $user_id; $from = $email['notification_from']; if (empty($from)) { $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') { $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['subject'] = $email['confirmation_subject']; $tmp_email['message'] = $email['confirmation_message']; $tmp_email['headers'] = array(); $tmp_email['headers'][] = $header_content_type; - $tmp_email['headers'][] = 'User_id: '.$user_id; + $tmp_email['headers']['user_id'] = $user_id; $from = $email['confirmation_from']; if (empty($from)) { $from = get_option('admin_email'); diff --git a/plugins/custer/custer.php b/plugins/custer/custer.php index 67a4c7e..cb985c8 100644 --- a/plugins/custer/custer.php +++ b/plugins/custer/custer.php @@ -4,7 +4,7 @@ Plugin Name: hggg_custer Plugin URI: Description: customize user : output infos on page, on email, and change current user id momentarly Author: hugogogo -Version: 0.2.4 +Version: 0.2.5 Author URI: */ diff --git a/plugins/custer/filter_mail.php b/plugins/custer/filter_mail.php index 45ae3f9..34454fe 100644 --- a/plugins/custer/filter_mail.php +++ b/plugins/custer/filter_mail.php @@ -24,33 +24,10 @@ function replace_words($matches, $user_id = null) { $user_id = get_current_user_id(); $current_user = wp_get_current_user(); } - else { - return ""; - } - - if ($current_user === false) { - return ""; - } $query = $matches[1]; $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 $$$$ - * - */ - if (empty($result)) { - return $matches[0]; - } - return $result; } @@ -91,25 +68,32 @@ function filter_email_wp($args) { $pattern = '/\$\$(.*?)\$\$/'; $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) { - return \CUSTER\replace_words($matches, $user_id); - }, $old_to); - $args['to'] = $new_to; - - $old_subject = $args['subject']; - $new_subject = preg_replace_callback($pattern, function($matches) use ($user_id) { - return \CUSTER\replace_words($matches, $user_id); - }, $old_subject); - $args['subject'] = $new_subject; - - $old_message = $args['message']; - $new_message = preg_replace_callback($pattern, function($matches) use ($user_id) { - return \CUSTER\replace_words($matches, $user_id); - }, $old_message); - $args['message'] = $new_message; + /* + * loop through args + * if one arg is itself an array, loop through it + * and replace all $$words$$ by their values + * + */ + foreach ($args as $arg_key => $arg) { + if (is_array($arg)) { + $new_arg = array(); + foreach ($arg as $key => $value) { + $new_arg[$key] = preg_replace_callback($pattern, function($matches) use ($user_id) { + return \CUSTER\replace_words($matches, $user_id); + }, $value); + } + } + else { + $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; } add_filter('wp_mail', __NAMESPACE__.'\filter_email_wp', 10, 1); @@ -145,15 +129,13 @@ function find_user_id_from_headers(&$args) { } $user_id = null; $headers = $args['headers']; - foreach ($headers as $key => $header) { - $explode_header = explode(':', $header); - $explode_header = array_map('trim', $explode_header); - if ($explode_header[0] === 'User_id') { - $user_id = $explode_header[1]; - unset($headers[$key]); - $headers = array_values($headers); - } + // 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 + if (array_key_exists('user_id', $headers)) { + $user_id = $headers['user_id']; + unset($headers['user_id']); + $headers = array_values($headers); } + $args['headers'] = $headers; return $user_id; } diff --git a/plugins/custer/queries.php b/plugins/custer/queries.php index 65d4ca4..c6f5189 100644 --- a/plugins/custer/queries.php +++ b/plugins/custer/queries.php @@ -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' ); +error_log("admin_email: " . json_encode($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(); return $base_url; }