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 3.9 3.9-beta 3.9.1 3.9.2 3.9.3 4.0 4.0.1 4.0.2 4.0.3 4.1 4.1-beta 4.1.1 4.1.2 4.2 4.2-beta 4.2.1 4.2.2 4.3 4.3.1 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6 4.6.1 4.7 4.8 4.8.1 4.9 4.9.1 4.9.2 5.0 5.0.1
contact-form-7 / includes / mail.php
contact-form-7 / includes Last commit date
css 12 years ago js 11 years ago capabilities.php 13 years ago contact-form-template.php 11 years ago contact-form.php 11 years ago controller.php 11 years ago formatting.php 12 years ago functions.php 11 years ago mail.php 11 years ago pipe.php 12 years ago shortcodes.php 12 years ago submission.php 11 years ago upgrade.php 11 years ago
mail.php
376 lines
1 <?php
2
3 class WPCF7_Mail {
4
5 private static $current = null;
6
7 private $name = '';
8 private $template = array();
9
10 public static function send( $template, $name = '' ) {
11 $instance = new self;
12 $instance->name = trim( $name );
13 $instance->setup_template( $template );
14
15 self::$current = $instance;
16
17 return $instance->compose();
18 }
19
20 private function __construct() {}
21
22 public static function get_current() {
23 return self::$current;
24 }
25
26 private function setup_template( $template ) {
27 $defaults = array(
28 'subject' => '', 'sender' => '', 'body' => '',
29 'recipient' => '', 'additional_headers' => '',
30 'attachments' => '', 'use_html' => false,
31 'exclude_blank' => false );
32
33 $this->template = wp_parse_args( $template, $defaults );
34 }
35
36 private function compose( $send = true ) {
37 $template = $this->template;
38 $use_html = (bool) $template['use_html'];
39
40 $subject = $this->replace_tags( $template['subject'] );
41 $sender = $this->replace_tags( $template['sender'] );
42 $recipient = $this->replace_tags( $template['recipient'] );
43 $additional_headers = $this->replace_tags( $template['additional_headers'] );
44
45 if ( $use_html ) {
46 $body = $this->replace_tags( $template['body'], true );
47 $body = wpautop( $body );
48 } else {
49 $body = $this->replace_tags( $template['body'] );
50 }
51
52 $attachments = $this->attachments( $template['attachments'] );
53
54 $components = compact( 'subject', 'sender', 'body',
55 'recipient', 'additional_headers', 'attachments' );
56
57 $components = apply_filters( 'wpcf7_mail_components',
58 $components, wpcf7_get_current_contact_form() );
59
60 $subject = wpcf7_strip_newline( $components['subject'] );
61 $sender = wpcf7_strip_newline( $components['sender'] );
62 $recipient = wpcf7_strip_newline( $components['recipient'] );
63 $body = $components['body'];
64 $additional_headers = trim( $components['additional_headers'] );
65 $attachments = $components['attachments'];
66
67 $headers = "From: $sender\n";
68
69 if ( $use_html ) {
70 $headers .= "Content-Type: text/html\n";
71 }
72
73 if ( $additional_headers ) {
74 $headers .= $additional_headers . "\n";
75 }
76
77 if ( $send ) {
78 return wp_mail( $recipient, $subject, $body, $headers, $attachments );
79 }
80
81 $components = compact( 'subject', 'sender', 'body',
82 'recipient', 'headers', 'attachments' );
83
84 return $components;
85 }
86
87 public function replace_tags( $content, $html = false ) {
88 $args = array(
89 'html' => $html,
90 'exclude_blank' => $this->template['exclude_blank'] );
91
92 return wpcf7_mail_replace_tags( $content, $args );
93 }
94
95 private function attachments( $template ) {
96 $attachments = array();
97
98 if ( $submission = WPCF7_Submission::get_instance() ) {
99 $uploaded_files = $submission->uploaded_files();
100
101 foreach ( (array) $uploaded_files as $name => $path ) {
102 if ( false !== strpos( $template, "[${name}]" )
103 && ! empty( $path ) ) {
104 $attachments[] = $path;
105 }
106 }
107 }
108
109 foreach ( explode( "\n", $template ) as $line ) {
110 $line = trim( $line );
111
112 if ( '[' == substr( $line, 0, 1 ) ) {
113 continue;
114 }
115
116 $path = path_join( WP_CONTENT_DIR, $line );
117
118 if ( @is_readable( $path ) && @is_file( $path ) ) {
119 $attachments[] = $path;
120 }
121 }
122
123 return $attachments;
124 }
125 }
126
127 function wpcf7_mail_replace_tags( $content, $args = '' ) {
128 $args = wp_parse_args( $args, array(
129 'html' => false,
130 'exclude_blank' => false ) );
131
132 if ( is_array( $content ) ) {
133 foreach ( $content as $key => $value ) {
134 $content[$key] = wpcf7_mail_replace_tags( $value, $args );
135 }
136
137 return $content;
138 }
139
140 $content = explode( "\n", $content );
141
142 foreach ( $content as $num => $line ) {
143 $line = new WPCF7_MailTaggedText( $line, $args );
144 $replaced = $line->replace_tags();
145
146 if ( $args['exclude_blank'] ) {
147 $replaced_tags = $line->get_replaced_tags();
148
149 if ( empty( $replaced_tags ) || array_filter( $replaced_tags ) ) {
150 $content[$num] = $replaced;
151 } else {
152 unset( $content[$num] ); // Remove a line.
153 }
154 } else {
155 $content[$num] = $replaced;
156 }
157 }
158
159 $content = implode( "\n", $content );
160
161 return $content;
162 }
163
164 class WPCF7_MailTaggedText {
165
166 private $html = false;
167 private $content = '';
168 private $replaced_tags = array();
169
170 public function __construct( $content, $args = '' ) {
171 $args = wp_parse_args( $args, array( 'html' => false ) );
172
173 $this->html = (bool) $args['html'];
174 $this->content = $content;
175 }
176
177 public function get_replaced_tags() {
178 return $this->replaced_tags;
179 }
180
181 public function replace_tags() {
182 $regex = '/(\[?)\[[\t ]*'
183 . '([a-zA-Z_][0-9a-zA-Z:._-]*)' // [2] = name
184 . '((?:[\t ]+"[^"]*"|[\t ]+\'[^\']*\')*)' // [3] = values
185 . '[\t ]*\](\]?)/';
186
187 if ( $this->html ) {
188 $callback = array( $this, 'replace_tags_callback_html' );
189 } else {
190 $callback = array( $this, 'replace_tags_callback' );
191 }
192
193 return preg_replace_callback( $regex, $callback, $this->content );
194 }
195
196 private function replace_tags_callback_html( $matches ) {
197 return $this->replace_tags_callback( $matches, true );
198 }
199
200 private function replace_tags_callback( $matches, $html = false ) {
201 // allow [[foo]] syntax for escaping a tag
202 if ( $matches[1] == '[' && $matches[4] == ']' ) {
203 return substr( $matches[0], 1, -1 );
204 }
205
206 $tag = $matches[0];
207 $tagname = $matches[2];
208 $values = $matches[3];
209
210 if ( ! empty( $values ) ) {
211 preg_match_all( '/"[^"]*"|\'[^\']*\'/', $values, $matches );
212 $values = wpcf7_strip_quote_deep( $matches[0] );
213 }
214
215 $do_not_heat = false;
216
217 if ( preg_match( '/^_raw_(.+)$/', $tagname, $matches ) ) {
218 $tagname = trim( $matches[1] );
219 $do_not_heat = true;
220 }
221
222 $format = '';
223
224 if ( preg_match( '/^_format_(.+)$/', $tagname, $matches ) ) {
225 $tagname = trim( $matches[1] );
226 $format = $values[0];
227 }
228
229 $submission = WPCF7_Submission::get_instance();
230 $submitted = $submission ? $submission->get_posted_data( $tagname ) : null;
231
232 if ( null !== $submitted ) {
233
234 if ( $do_not_heat ) {
235 $submitted = isset( $_POST[$tagname] ) ? $_POST[$tagname] : '';
236 }
237
238 $replaced = $submitted;
239
240 if ( ! empty( $format ) ) {
241 $replaced = $this->format( $replaced, $format );
242 }
243
244 $replaced = wpcf7_flat_join( $replaced );
245
246 if ( $html ) {
247 $replaced = esc_html( $replaced );
248 $replaced = wptexturize( $replaced );
249 }
250
251 $replaced = apply_filters( 'wpcf7_mail_tag_replaced',
252 $replaced, $submitted, $html );
253
254 $replaced = wp_unslash( trim( $replaced ) );
255
256 $this->replaced_tags[$tag] = $replaced;
257 return $replaced;
258 }
259
260 $special = apply_filters( 'wpcf7_special_mail_tags', '', $tagname, $html );
261
262 if ( ! empty( $special ) ) {
263 $this->replaced_tags[$tag] = $special;
264 return $special;
265 }
266
267 return $tag;
268 }
269
270 public function format( $original, $format ) {
271 $original = (array) $original;
272
273 foreach ( $original as $key => $value ) {
274 if ( preg_match( '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $value ) ) {
275 $original[$key] = mysql2date( $format, $value );
276 }
277 }
278
279 return $original;
280 }
281 }
282
283 /* Special Mail Tags */
284
285 add_filter( 'wpcf7_special_mail_tags', 'wpcf7_special_mail_tag', 10, 3 );
286
287 function wpcf7_special_mail_tag( $output, $name, $html ) {
288 $name = preg_replace( '/^wpcf7\./', '_', $name ); // for back-compat
289
290 $submission = WPCF7_Submission::get_instance();
291
292 if ( ! $submission ) {
293 return $output;
294 }
295
296 if ( '_remote_ip' == $name ) {
297 if ( $remote_ip = $submission->get_meta( 'remote_ip' ) ) {
298 return $remote_ip;
299 } else {
300 return '';
301 }
302 }
303
304 if ( '_user_agent' == $name ) {
305 if ( $user_agent = $submission->get_meta( 'user_agent' ) ) {
306 return $html ? esc_html( $user_agent ) : $user_agent;
307 } else {
308 return '';
309 }
310 }
311
312 if ( '_url' == $name ) {
313 if ( $url = $submission->get_meta( 'url' ) ) {
314 return esc_url( $url );
315 } else {
316 return '';
317 }
318 }
319
320 if ( '_date' == $name || '_time' == $name ) {
321 if ( $timestamp = $submission->get_meta( 'timestamp' ) ) {
322 if ( '_date' == $name ) {
323 return date_i18n( get_option( 'date_format' ), $timestamp );
324 }
325
326 if ( '_time' == $name ) {
327 return date_i18n( get_option( 'time_format' ), $timestamp );
328 }
329 }
330
331 return '';
332 }
333
334 if ( '_post_' == substr( $name, 0, 6 ) ) {
335 $unit_tag = $submission->get_meta( 'unit_tag' );
336
337 if ( $unit_tag
338 && preg_match( '/^wpcf7-f(\d+)-p(\d+)-o(\d+)$/', $unit_tag, $matches ) ) {
339 $post_id = absint( $matches[2] );
340
341 if ( $post = get_post( $post_id ) ) {
342 if ( '_post_id' == $name ) {
343 return (string) $post->ID;
344 }
345
346 if ( '_post_name' == $name ) {
347 return $post->post_name;
348 }
349
350 if ( '_post_title' == $name ) {
351 return $html ? esc_html( $post->post_title ) : $post->post_title;
352 }
353
354 if ( '_post_url' == $name ) {
355 return get_permalink( $post->ID );
356 }
357
358 $user = new WP_User( $post->post_author );
359
360 if ( '_post_author' == $name ) {
361 return $user->display_name;
362 }
363
364 if ( '_post_author_email' == $name ) {
365 return $user->user_email;
366 }
367 }
368 }
369
370 return '';
371 }
372
373 return $output;
374 }
375
376 ?>