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 | ?> |