PluginProbe
Hello Plus / 1.7.0
Hello Plus v1.7.0
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.6.0 1.6.1 1.6.2 1.7.0 1.7.1 1.7.2 1.7.3 All 30 releases
hello-plus / modules / forms / actions / email.php

email.php in Hello Plus 1.7.0, at modules/forms/actions/email.php

190 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace HelloPlus\Modules\Forms\Actions;
3
4 use Elementor\Controls_Manager;
5 use HelloPlus\Modules\Forms\Classes\Action_Base;
6 use HelloPlus\Modules\Forms\Classes\Form_Record;
7 use HelloPlus\Modules\Forms\Components\Ajax_Handler;
8 use HelloPlus\Modules\Forms\Module;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly.
12 }
13
14 class Email extends Action_Base {
15
16 public function get_name(): string {
17 return 'ehp-email';
18 }
19
20 public function get_label(): string {
21 return esc_html__( 'Email', 'hello-plus' );
22 }
23
24 public function register_settings_section( $widget ) {
25 }
26
27 public function on_export( $element ) {
28 $controls_to_unset = [
29 'email_to',
30 'email_from',
31 'email_from_name',
32 'email_subject',
33 'email_reply_to',
34 'email_to_cc',
35 'email_to_bcc',
36 ];
37
38 foreach ( $controls_to_unset as $base_id ) {
39 $control_id = $this->get_control_id( $base_id );
40 unset( $element['settings'][ $control_id ] );
41 }
42
43 return $element;
44 }
45
46 public function run( $record, $ajax_handler ) {
47 $settings = $record->get( 'form_settings' );
48 $send_html = 'plain' !== $settings[ $this->get_control_id( 'email_content_type' ) ];
49 $line_break = $send_html ? '<br>' : "\n";
50
51 $fields = [
52 'email_to' => get_option( 'admin_email' ),
53 /* translators: %s: Site title. */
54 'email_subject' => sprintf( esc_html__( 'New message from [%s]', 'hello-plus' ), get_bloginfo( 'name' ) ),
55 'email_content' => '[all-fields]',
56 'email_from_name' => get_bloginfo( 'name' ),
57 'email_from' => get_bloginfo( 'admin_email' ),
58 'email_reply_to' => 'noreply@' . Module::get_site_domain(),
59 'email_to_cc' => '',
60 'email_to_bcc' => '',
61 ];
62
63 foreach ( $fields as $key => $default ) {
64 $setting = trim( $settings[ $this->get_control_id( $key ) ] );
65 $setting = $record->replace_setting_shortcodes( $setting );
66 if ( ! empty( $setting ) ) {
67 $fields[ $key ] = $setting;
68 }
69 }
70
71 $email_reply_to = $this->get_reply_to( $record, $fields );
72
73 $fields['email_content'] = $this->replace_content_shortcodes( $fields['email_content'], $record, $line_break );
74
75 $email_meta = '';
76
77 $form_metadata_settings = $settings[ $this->get_control_id( 'form_metadata' ) ];
78
79 foreach ( $record->get( 'meta' ) as $id => $field ) {
80 if ( in_array( $id, $form_metadata_settings, true ) ) {
81 $email_meta .= $this->field_formatted( $field ) . $line_break;
82 }
83 }
84
85 if ( ! empty( $email_meta ) ) {
86 $fields['email_content'] .= $line_break . '---' . $line_break . $line_break . $email_meta;
87 }
88
89 $headers = sprintf( 'From: %s <%s>' . "\r\n", $fields['email_from_name'], $fields['email_from'] );
90 $headers .= sprintf( 'Reply-To: %s' . "\r\n", $email_reply_to );
91
92 if ( $send_html ) {
93 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
94 }
95
96 $cc_header = '';
97 if ( ! empty( $fields['email_to_cc'] ) ) {
98 $cc_header = 'Cc: ' . $fields['email_to_cc'] . "\r\n";
99 }
100
101 $email_sent = wp_mail(
102 $fields['email_to'],
103 $fields['email_subject'],
104 $fields['email_content'],
105 $headers . $cc_header,
106 );
107
108 if ( ! empty( $fields['email_to_bcc'] ) ) {
109 $bcc_emails = explode( ',', $fields['email_to_bcc'] );
110 foreach ( $bcc_emails as $bcc_email ) {
111 wp_mail(
112 trim( $bcc_email ),
113 $fields['email_subject'],
114 $fields['email_content'],
115 $headers,
116 );
117 }
118 }
119
120 if ( ! $email_sent ) {
121 $message = Ajax_Handler::get_default_message( Ajax_Handler::SERVER_ERROR, $settings );
122
123 $ajax_handler->add_error_message( $message );
124
125 throw new \Exception( esc_html( $message ) );
126 }
127 }
128
129 private function field_formatted( $field ) {
130 $formatted = '';
131 if ( ! empty( $field['title'] ) ) {
132 $formatted = sprintf( '%s: %s', $field['title'], $field['value'] );
133 } elseif ( ! empty( $field['value'] ) ) {
134 $formatted = sprintf( '%s', $field['value'] );
135 }
136
137 return $formatted;
138 }
139
140 // Allow overwrite the control_id with a prefix, @see Email2
141 protected function get_control_id( $control_id ) {
142 return $control_id;
143 }
144
145 protected function get_reply_to( $record, $fields ) {
146 $email_reply_to = '';
147
148 if ( ! empty( $fields['email_reply_to'] ) ) {
149 $sent_data = $record->get( 'sent_data' );
150 foreach ( $record->get( 'fields' ) as $field_index => $field ) {
151 if ( $field_index === $fields['email_reply_to'] && ! empty( $sent_data[ $field_index ] ) && is_email( $sent_data[ $field_index ] ) ) {
152 $email_reply_to = $sent_data[ $field_index ];
153 break;
154 }
155 }
156 }
157
158 return $email_reply_to;
159 }
160
161 /**
162 * @param string $email_content
163 * @param Form_Record $record
164 *
165 * @return string
166 */
167 private function replace_content_shortcodes( $email_content, $record, $line_break ) {
168 $email_content = do_shortcode( $email_content );
169 $all_fields_shortcode = '[all-fields]';
170
171 if ( false !== strpos( $email_content, $all_fields_shortcode ) ) {
172 $text = '';
173 foreach ( $record->get( 'fields' ) as $field ) {
174
175 $formatted = $this->field_formatted( $field );
176 if ( ( 'textarea' === $field['type'] ) && ( '<br>' === $line_break ) ) {
177 $formatted = str_replace( [ "\r\n", "\n", "\r" ], '<br />', $formatted );
178 }
179
180 $text .= $formatted . $line_break;
181 }
182
183 $email_content = str_replace( $all_fields_shortcode, $text, $email_content );
184
185 }
186
187 return $email_content;
188 }
189 }
190