← All changes
|
includes/blocks/form-builder/actions/send-email.php
+60
-18
2.9.1
→
2.14.0
View file →
| @@ -50,8 +50,9 @@ | ||
| 50 | 50 | * @return void |
| 51 | 51 | */ |
| 52 | 52 | public function action() { |
| 53 | 53 | |
| 54 | + // wp_send_json($this->validateformdata->form_info['info']); | |
| 54 | 55 | // if already subscribe, dont send any email |
| 55 | 56 | if ( |
| 56 | 57 | $this->validateformdata->form_info['info']['type'] === 'subscription' && |
| 57 | 58 | array_key_exists( 'dont_send_subs_email', $this->validateformdata->state_data ) |
| @@ -61,15 +62,16 @@ | ||
| 61 | 62 | |
| 62 | 63 | // send email verification email to user |
| 63 | 64 | $this->send_verification_email(); |
| 64 | 65 | |
| 65 | - if ( $this->form_type === 'subscription' ) { | |
| 66 | + if ( $this->form_type === 'subscription' && | |
| 67 | + ( $this->validateformdata->form_info['info']['config']['adminNotify'] ?? true ) | |
| 68 | + ) { | |
| 66 | 69 | $this->subscription_form_send_email(); |
| 67 | 70 | } |
| 68 | 71 | |
| 69 | - $this->contact_form_send_email(); | |
| 70 | - $this->contact_form_send_email( 'Two' ); | |
| 71 | - | |
| 72 | + // $this->contact_form_send_email(); | |
| 73 | + // $this->contact_form_send_email( 'Two' ); | |
| 72 | 74 | } |
| 73 | 75 | |
| 74 | 76 | private function send_verification_email() { |
| 75 | 77 | $id = $this->validateformdata->state_data['submission_id'] ?? null; |
| @@ -109,8 +111,9 @@ | ||
| 109 | 111 | * @param string $num |
| 110 | 112 | * @return void |
| 111 | 113 | */ |
| 112 | 114 | private function contact_form_send_email( string $num = 'One' ): void { |
| 115 | + $this->user_email = $this->apply_vars( $this->user_email ); | |
| 113 | 116 | // check email notification is enabled or not |
| 114 | 117 | $nums = [ |
| 115 | 118 | 'One' => '', |
| 116 | 119 | 'Two' => 2, |
| @@ -123,28 +126,30 @@ | ||
| 123 | 126 | return; |
| 124 | 127 | } |
| 125 | 128 | $config = $this->validateformdata->form_info['info']['config']; |
| 126 | 129 | |
| 127 | - $to_email = sanitize_text_field( $config[ 'email' . $num . 'To' ] ?? $this->admin_email ); | |
| 130 | + $to_email = $this->apply_vars( sanitize_text_field( $config[ 'email' . $num . 'To' ] ?? $this->admin_email ) ); | |
| 128 | 131 | |
| 129 | - $subject = sanitize_text_field( $config[ 'email' . $num . 'Subject' ] ?? '' ); | |
| 132 | + $subject = $this->apply_vars( sanitize_text_field( $config[ 'email' . $num . 'Subject' ] ?? '' ) ); | |
| 130 | 133 | // translators: %s is email |
| 131 | 134 | $subject = $subject ? $subject : ( $this->user_email ? sprintf( __( 'You have a message from %s', 'ablocks' ), $this->user_email ) : __( 'You have a message', 'ablocks' ) ); |
| 132 | 135 | |
| 133 | 136 | $type = strtolower( sanitize_text_field( $config[ 'email' . $num . 'Type' ] ?? 'html' ) ); |
| 134 | - $message = sanitize_text_field( $config[ 'email' . $num . 'Message' ] ?? '' ); | |
| 137 | + $message = $this->apply_vars( sanitize_text_field( $config[ 'email' . $num . 'Message' ] ?? '' ) ); | |
| 135 | 138 | // check {all-fields} exists or not |
| 139 | + $message = empty( $message ) ? '{all-fields}' : $message; | |
| 136 | 140 | if ( preg_match( '|\{all\-fields\}|im', $message ) ) { |
| 137 | 141 | // if exist then replace this text to data table |
| 138 | 142 | $message = str_replace( '{all-fields}', $this->get_data_as_table_format( $type ), $message ); |
| 139 | - } elseif ( $message ) { | |
| 140 | - // if message is defined by user and {all-fields} is not available | |
| 141 | - // then insert datatable at the end of msg | |
| 142 | - $message .= $this->get_data_as_table_format( $type ); | |
| 143 | - } else { | |
| 144 | - // if msg is not defined by user, the add only datatable | |
| 145 | - $message = $this->get_data_as_table_format( $type ); | |
| 146 | 143 | } |
| 144 | + // elseif ( $message ) { | |
| 145 | + // if message is defined by user and {all-fields} is not available | |
| 146 | + // then insert datatable at the end of msg | |
| 147 | + // $message .= $this->get_data_as_table_format( $type ); | |
| 148 | + // } else { | |
| 149 | + // if msg is not defined by user, the add only datatable | |
| 150 | + // $message = $this->get_data_as_table_format( $type ); | |
| 151 | + // } | |
| 147 | 152 | $headers = []; |
| 148 | 153 | |
| 149 | 154 | $from_email = sanitize_text_field( $config[ 'email' . $num . 'FormEmail' ] ?? '' ); |
| 150 | 155 | $from_name = sanitize_text_field( $config[ 'email' . $num . 'FormName' ] ?? '' ); |
| @@ -159,16 +164,26 @@ | ||
| 159 | 164 | |
| 160 | 165 | if ( $reply_to ) { |
| 161 | 166 | $headers[] = 'Reply-To: ' . $reply_to; |
| 162 | 167 | } |
| 163 | - | |
| 164 | 168 | if ( $cc ) { |
| 165 | - $headers[] = 'CC: ' . $cc; | |
| 169 | + $headers[] = 'CC: ' . implode( ',', array_unique( | |
| 170 | + preg_split( | |
| 171 | + '|[,\s]|', $this->apply_vars( strval( $cc ) ) | |
| 172 | + ) | |
| 173 | + ) ); | |
| 166 | 174 | } |
| 167 | 175 | if ( $bcc ) { |
| 168 | - $headers[] = 'BCC: ' . $bcc; | |
| 176 | + $headers[] = 'BCC: ' . implode( ',', array_unique( | |
| 177 | + preg_split( | |
| 178 | + '|[,\s]|', $this->apply_vars( strval( $bcc ) ) | |
| 179 | + ) | |
| 180 | + ) ); | |
| 169 | 181 | } |
| 170 | 182 | |
| 183 | + if ( $type === 'plain' ) { | |
| 184 | + $headers[] = 'Content-Type: text/plain; charset=UTF-8'; | |
| 185 | + } | |
| 171 | 186 | $template = $type === 'plain' ? 'email/plain-text/contact-email.php' : 'email/contact-email.php'; |
| 172 | 187 | |
| 173 | 188 | ob_start(); |
| 174 | 189 | \ABlocks\Helper::get_template($template, [ |
| @@ -176,10 +191,37 @@ | ||
| 176 | 191 | 'message' => $message, |
| 177 | 192 | ]); |
| 178 | 193 | |
| 179 | 194 | $data = ob_get_clean(); |
| 195 | + foreach ( array_unique( preg_split( '|[,\s]|', strval( $to_email ) ) ) as $email ) { | |
| 196 | + $email = trim( $email ); | |
| 197 | + if ( ! empty( $email ) ) { | |
| 198 | + $this->send_email( $subject, $data, $email, $headers ); | |
| 199 | + } | |
| 200 | + } | |
| 180 | 201 | |
| 181 | - $this->send_email( $subject, $data, $to_email, $headers ); | |
| 202 | + } | |
| 203 | + | |
| 204 | + private function apply_vars( ?string $msg ): ?string { | |
| 205 | + $admin_email = get_option( 'admin_email' ); | |
| 206 | + $current_user = wp_get_current_user(); | |
| 207 | + $current_user_email = $current_user->user_email; | |
| 208 | + | |
| 209 | + $fields = array_merge( | |
| 210 | + $this->validateformdata->form_info['data'] ?? [], | |
| 211 | + [ | |
| 212 | + 'admin_email' => [ 'value' => $admin_email ], | |
| 213 | + 'user_email' => [ 'value' => $current_user_email ] | |
| 214 | + ] | |
| 215 | + ); | |
| 216 | + // wp_send_json($fields); | |
| 217 | + foreach ( $fields as $key => [ 'value' => $val ] ) { | |
| 218 | + if ( is_array( $val ) ) { | |
| 219 | + $val = implode( ', ', $val ); | |
| 220 | + } | |
| 221 | + $msg = str_replace( "{{$key}}", $val, $msg ); | |
| 222 | + } | |
| 223 | + return $msg; | |
| 182 | 224 | } |
| 183 | 225 | |
| 184 | 226 | /** |
| 185 | 227 | * Send email to admin from subscription form |