EmailActionHandler.php
171 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\FormActions\Actions; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | use Kirki\App\Constants\Form\FormEmailBodyPartTypes; |
| 8 | use Kirki\App\Contracts\FormActionHandler; |
| 9 | use Kirki\App\DTO\Form\FormConfigDTO; |
| 10 | |
| 11 | /** |
| 12 | * Sends a form submission over email, for a single configured `email` action. |
| 13 | */ |
| 14 | class EmailActionHandler implements FormActionHandler |
| 15 | { |
| 16 | public function handle(array $action, array $form_data, FormConfigDTO $form_config) |
| 17 | { |
| 18 | $this->register_shortcodes($action, $form_data); //@todo: maybe we can implement this without wp shortcodes but a different approach as current implementation might collide with other shortcodes registered |
| 19 | |
| 20 | return $this->send_single_email_action($action, $form_data, $form_config->name); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Register shortcodes referenced by the email action's fields. |
| 25 | * |
| 26 | * @param array $action |
| 27 | * @param array $form_data |
| 28 | * @return void |
| 29 | */ |
| 30 | protected function register_shortcodes($action, $form_data) |
| 31 | { |
| 32 | $this->register_shortcodes_from_field($action['emailList'] ?? '', $form_data); |
| 33 | $this->register_shortcodes_from_field($action['replyTo'] ?? '', $form_data); |
| 34 | $this->register_shortcodes_from_field($action['name'] ?? '', $form_data); |
| 35 | $this->register_shortcodes_from_field($action['subject'] ?? '', $form_data); |
| 36 | |
| 37 | add_shortcode( |
| 38 | 'admin_email', |
| 39 | function () { |
| 40 | return get_option('admin_email'); |
| 41 | } |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Register shortcodes referenced in a single email action field. |
| 47 | * |
| 48 | * @param string $field_value The field value containing shortcodes. |
| 49 | * @param array $form_data The form data to use for shortcode values. |
| 50 | * @return void |
| 51 | */ |
| 52 | protected function register_shortcodes_from_field($field_value, $form_data) |
| 53 | { |
| 54 | if (empty($field_value)) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | $regex = '/\[([^\]]+)\]/'; |
| 59 | preg_match_all($regex, $field_value, $matches); |
| 60 | |
| 61 | foreach (($matches[1] ?? []) as $match) { |
| 62 | if (isset($form_data[$match])) { |
| 63 | add_shortcode($match, fn() => $form_data[$match]); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Send a single email action. |
| 70 | * |
| 71 | * @param array $email_action Email action configuration. |
| 72 | * @param array $form_data Form data. |
| 73 | * @param string $form_name Form name. |
| 74 | * @return bool |
| 75 | */ |
| 76 | protected function send_single_email_action($email_action, $form_data, $form_name) |
| 77 | { |
| 78 | $body = $this->convert_form_data_into_html_for_email($form_data); |
| 79 | $reply_to = ''; |
| 80 | $name = ''; |
| 81 | $subject = 'New ' . $form_name; |
| 82 | $header = []; |
| 83 | |
| 84 | if (isset($email_action['body']) && is_array($email_action['body'])) { |
| 85 | $body = $this->build_email_body($email_action['body'], $form_data); |
| 86 | } |
| 87 | |
| 88 | if (isset($email_action['replyTo'])) { |
| 89 | $reply_to = do_shortcode($email_action['replyTo']); |
| 90 | } |
| 91 | |
| 92 | if (isset($email_action['name'])) { |
| 93 | $name = do_shortcode($email_action['name']); |
| 94 | } |
| 95 | |
| 96 | if (isset($email_action['subject'])) { |
| 97 | $subject = do_shortcode($email_action['subject']); |
| 98 | } |
| 99 | |
| 100 | if (strlen($reply_to) > 0 && strlen($name) > 0) { |
| 101 | $header = ['Reply-To: ' . $name . ' <' . $reply_to . '>']; |
| 102 | } |
| 103 | |
| 104 | if (isset($email_action['emailList']) && !empty($email_action['emailList'])) { |
| 105 | return $this->send_email_notification(do_shortcode($email_action['emailList']), $subject, $body, $header); |
| 106 | } |
| 107 | |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Build the email body from a body configuration. |
| 113 | * |
| 114 | * @param array $body_config Body configuration. |
| 115 | * @param array $form_data Form data. |
| 116 | * @return string Email body HTML. |
| 117 | */ |
| 118 | protected function build_email_body($body_config, $form_data) |
| 119 | { |
| 120 | $body_parts = []; |
| 121 | |
| 122 | foreach ($body_config as $body_data) { |
| 123 | if (!isset($body_data['type'], $body_data['value'])) { |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | if ($body_data['type'] === FormEmailBodyPartTypes::TEXT) { |
| 128 | $body_parts[] = $body_data['value']; |
| 129 | } elseif ($body_data['type'] === FormEmailBodyPartTypes::FORM && isset($form_data[$body_data['value']])) { |
| 130 | $body_parts[] = $form_data[$body_data['value']]; |
| 131 | } |
| 132 | } |
| 133 | return nl2br(implode('', $body_parts)); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Convert form data into an HTML list for email. |
| 138 | * |
| 139 | * @param array $form_data Form data. |
| 140 | * @return string |
| 141 | */ |
| 142 | protected function convert_form_data_into_html_for_email($form_data = []) |
| 143 | { |
| 144 | $html = '<ul>'; |
| 145 | |
| 146 | if (is_array($form_data)) { |
| 147 | foreach ($form_data as $key => $value) { |
| 148 | $html .= '<li>' . esc_html($key) . ': ' . esc_html($value) . '</li>'; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | $html .= '</ul>'; |
| 153 | return $html; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Send an email notification. |
| 158 | * |
| 159 | * @param string|string[] $to Address(es) to send to. |
| 160 | * @param string $subject Email subject. |
| 161 | * @param string $message Message contents. |
| 162 | * @param array $headers Email headers. |
| 163 | * @return bool |
| 164 | */ |
| 165 | protected function send_email_notification($to, $subject, $message, $headers = []) |
| 166 | { |
| 167 | $headers[] = 'Content-Type: text/html; charset=UTF-8'; |
| 168 | return wp_mail($to, $subject, $message, $headers); |
| 169 | } |
| 170 | } |
| 171 |