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