css
13 years ago
js
12 years ago
capabilities.php
13 years ago
classes.php
13 years ago
controller.php
12 years ago
deprecated.php
14 years ago
formatting.php
13 years ago
functions.php
13 years ago
pipe.php
14 years ago
shortcodes.php
13 years ago
classes.php
882 lines
| 1 | <?php |
| 2 | |
| 3 | class WPCF7_ContactForm { |
| 4 | |
| 5 | const post_type = 'wpcf7_contact_form'; |
| 6 | |
| 7 | public static $found_items = 0; |
| 8 | |
| 9 | var $initial = false; |
| 10 | |
| 11 | var $id; |
| 12 | var $title; |
| 13 | |
| 14 | var $unit_tag; |
| 15 | |
| 16 | var $responses_count = 0; |
| 17 | var $scanned_form_tags; |
| 18 | |
| 19 | var $posted_data; |
| 20 | var $uploaded_files = array(); |
| 21 | |
| 22 | var $skip_mail = false; |
| 23 | |
| 24 | public static function register_post_type() { |
| 25 | register_post_type( self::post_type, array( |
| 26 | 'labels' => array( |
| 27 | 'name' => __( 'Contact Forms', 'wpcf7' ), |
| 28 | 'singular_name' => __( 'Contact Form', 'wpcf7' ) ), |
| 29 | 'rewrite' => false, |
| 30 | 'query_var' => false ) ); |
| 31 | } |
| 32 | |
| 33 | public static function find( $args = '' ) { |
| 34 | $defaults = array( |
| 35 | 'post_status' => 'any', |
| 36 | 'posts_per_page' => -1, |
| 37 | 'offset' => 0, |
| 38 | 'orderby' => 'ID', |
| 39 | 'order' => 'ASC' ); |
| 40 | |
| 41 | $args = wp_parse_args( $args, $defaults ); |
| 42 | |
| 43 | $args['post_type'] = self::post_type; |
| 44 | |
| 45 | $q = new WP_Query(); |
| 46 | $posts = $q->query( $args ); |
| 47 | |
| 48 | self::$found_items = $q->found_posts; |
| 49 | |
| 50 | $objs = array(); |
| 51 | |
| 52 | foreach ( (array) $posts as $post ) |
| 53 | $objs[] = new self( $post ); |
| 54 | |
| 55 | return $objs; |
| 56 | } |
| 57 | |
| 58 | public function __construct( $post = null ) { |
| 59 | $this->initial = true; |
| 60 | |
| 61 | $this->form = ''; |
| 62 | $this->mail = array(); |
| 63 | $this->mail_2 = array(); |
| 64 | $this->messages = array(); |
| 65 | $this->additional_settings = ''; |
| 66 | |
| 67 | $post = get_post( $post ); |
| 68 | |
| 69 | if ( $post && self::post_type == get_post_type( $post ) ) { |
| 70 | $this->initial = false; |
| 71 | $this->id = $post->ID; |
| 72 | $this->title = $post->post_title; |
| 73 | |
| 74 | $props = $this->get_properties(); |
| 75 | |
| 76 | foreach ( $props as $prop => $value ) { |
| 77 | if ( metadata_exists( 'post', $post->ID, '_' . $prop ) ) |
| 78 | $this->{$prop} = get_post_meta( $post->ID, '_' . $prop, true ); |
| 79 | else |
| 80 | $this->{$prop} = get_post_meta( $post->ID, $prop, true ); |
| 81 | } |
| 82 | |
| 83 | $this->upgrade(); |
| 84 | } |
| 85 | |
| 86 | do_action_ref_array( 'wpcf7_contact_form', array( &$this ) ); |
| 87 | } |
| 88 | |
| 89 | function get_properties() { |
| 90 | $prop_names = array( 'form', 'mail', 'mail_2', 'messages', 'additional_settings' ); |
| 91 | |
| 92 | $properties = array(); |
| 93 | |
| 94 | foreach ( $prop_names as $prop_name ) |
| 95 | $properties[$prop_name] = isset( $this->{$prop_name} ) ? $this->{$prop_name} : ''; |
| 96 | |
| 97 | return apply_filters( 'wpcf7_contact_form_properties', $properties, $this ); |
| 98 | } |
| 99 | |
| 100 | // Return true if this form is the same one as currently POSTed. |
| 101 | function is_posted() { |
| 102 | if ( ! isset( $_POST['_wpcf7_unit_tag'] ) || empty( $_POST['_wpcf7_unit_tag'] ) ) |
| 103 | return false; |
| 104 | |
| 105 | if ( $this->unit_tag == $_POST['_wpcf7_unit_tag'] ) |
| 106 | return true; |
| 107 | |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | function clear_post() { |
| 112 | $fes = $this->form_scan_shortcode(); |
| 113 | |
| 114 | foreach ( $fes as $fe ) { |
| 115 | if ( ! isset( $fe['name'] ) || empty( $fe['name'] ) ) |
| 116 | continue; |
| 117 | |
| 118 | $name = $fe['name']; |
| 119 | |
| 120 | if ( isset( $_POST[$name] ) ) |
| 121 | unset( $_POST[$name] ); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /* Generating Form HTML */ |
| 126 | |
| 127 | function form_html() { |
| 128 | global $wpcf7; |
| 129 | |
| 130 | $form = '<div class="wpcf7" id="' . $this->unit_tag . '">'; |
| 131 | |
| 132 | $url = wpcf7_get_request_uri(); |
| 133 | |
| 134 | if ( $frag = strstr( $url, '#' ) ) |
| 135 | $url = substr( $url, 0, -strlen( $frag ) ); |
| 136 | |
| 137 | $url .= '#' . $this->unit_tag; |
| 138 | |
| 139 | $url = apply_filters( 'wpcf7_form_action_url', $url ); |
| 140 | |
| 141 | $class = 'wpcf7-form'; |
| 142 | |
| 143 | if ( $this->is_posted() ) { |
| 144 | if ( empty( $wpcf7->result['valid'] ) ) |
| 145 | $class .= ' invalid'; |
| 146 | elseif ( ! empty( $wpcf7->result['spam'] ) ) |
| 147 | $class .= ' spam'; |
| 148 | elseif ( ! empty( $wpcf7->result['mail_sent'] ) ) |
| 149 | $class .= ' sent'; |
| 150 | else |
| 151 | $class .= ' failed'; |
| 152 | } |
| 153 | |
| 154 | $class = apply_filters( 'wpcf7_form_class_attr', $class ); |
| 155 | |
| 156 | $enctype = apply_filters( 'wpcf7_form_enctype', '' ); |
| 157 | |
| 158 | $novalidate = apply_filters( 'wpcf7_form_novalidate', |
| 159 | wpcf7_support_html5() ? ' novalidate="novalidate"' : '' ); |
| 160 | |
| 161 | $form .= '<form action="' . esc_url_raw( $url ) . '" method="post"' |
| 162 | . ' class="' . esc_attr( $class ) . '"' |
| 163 | . $enctype . $novalidate . '>' . "\n"; |
| 164 | |
| 165 | $form .= $this->form_hidden_fields(); |
| 166 | |
| 167 | $form .= $this->form_elements(); |
| 168 | |
| 169 | if ( ! $this->responses_count ) |
| 170 | $form .= $this->form_response_output(); |
| 171 | |
| 172 | $form .= '</form>'; |
| 173 | |
| 174 | $form .= '</div>'; |
| 175 | |
| 176 | return $form; |
| 177 | } |
| 178 | |
| 179 | function form_hidden_fields() { |
| 180 | $hidden_fields = array( |
| 181 | '_wpcf7' => $this->id, |
| 182 | '_wpcf7_version' => WPCF7_VERSION, |
| 183 | '_wpcf7_unit_tag' => $this->unit_tag ); |
| 184 | |
| 185 | if ( WPCF7_VERIFY_NONCE ) |
| 186 | $hidden_fields['_wpnonce'] = wpcf7_create_nonce( $this->id ); |
| 187 | |
| 188 | $content = ''; |
| 189 | |
| 190 | foreach ( $hidden_fields as $name => $value ) { |
| 191 | $content .= '<input type="hidden"' |
| 192 | . ' name="' . esc_attr( $name ) . '"' |
| 193 | . ' value="' . esc_attr( $value ) . '" />' . "\n"; |
| 194 | } |
| 195 | |
| 196 | return '<div style="display: none;">' . "\n" . $content . '</div>' . "\n"; |
| 197 | } |
| 198 | |
| 199 | function form_response_output() { |
| 200 | global $wpcf7; |
| 201 | |
| 202 | $class = 'wpcf7-response-output'; |
| 203 | $content = ''; |
| 204 | |
| 205 | if ( $this->is_posted() ) { // Post response output for non-AJAX |
| 206 | |
| 207 | if ( empty( $wpcf7->result['valid'] ) ) |
| 208 | $class .= ' wpcf7-validation-errors'; |
| 209 | elseif ( ! empty( $wpcf7->result['spam'] ) ) |
| 210 | $class .= ' wpcf7-spam-blocked'; |
| 211 | elseif ( ! empty( $wpcf7->result['mail_sent'] ) ) |
| 212 | $class .= ' wpcf7-mail-sent-ok'; |
| 213 | else |
| 214 | $class .= ' wpcf7-mail-sent-ng'; |
| 215 | |
| 216 | if ( ! empty( $wpcf7->result['message'] ) ) |
| 217 | $content = $wpcf7->result['message']; |
| 218 | |
| 219 | } else { |
| 220 | $class .= ' wpcf7-display-none'; |
| 221 | } |
| 222 | |
| 223 | $class = trim( $class ); |
| 224 | |
| 225 | $output = sprintf( '<div class="%1$s">%2$s</div>', |
| 226 | $class, esc_html( $content ) ); |
| 227 | |
| 228 | return apply_filters( 'wpcf7_form_response_output', |
| 229 | $output, $class, $content, $this ); |
| 230 | } |
| 231 | |
| 232 | function validation_error( $name ) { |
| 233 | global $wpcf7; |
| 234 | |
| 235 | if ( ! $this->is_posted() ) |
| 236 | return ''; |
| 237 | |
| 238 | if ( ! isset( $wpcf7->result['invalid_reasons'][$name] ) ) |
| 239 | return ''; |
| 240 | |
| 241 | $ve = trim( $wpcf7->result['invalid_reasons'][$name] ); |
| 242 | |
| 243 | if ( empty( $ve ) ) |
| 244 | return ''; |
| 245 | |
| 246 | $ve = '<span class="wpcf7-not-valid-tip-no-ajax">' . esc_html( $ve ) . '</span>'; |
| 247 | return apply_filters( 'wpcf7_validation_error', $ve, $name, $this ); |
| 248 | } |
| 249 | |
| 250 | /* Form Elements */ |
| 251 | |
| 252 | function form_do_shortcode() { |
| 253 | global $wpcf7_shortcode_manager; |
| 254 | |
| 255 | $form = $this->form; |
| 256 | |
| 257 | if ( WPCF7_AUTOP ) { |
| 258 | $form = $wpcf7_shortcode_manager->normalize_shortcode( $form ); |
| 259 | $form = wpcf7_autop( $form ); |
| 260 | } |
| 261 | |
| 262 | $form = $wpcf7_shortcode_manager->do_shortcode( $form ); |
| 263 | $this->scanned_form_tags = $wpcf7_shortcode_manager->scanned_tags; |
| 264 | |
| 265 | return $form; |
| 266 | } |
| 267 | |
| 268 | function form_scan_shortcode( $cond = null ) { |
| 269 | global $wpcf7_shortcode_manager; |
| 270 | |
| 271 | if ( ! empty( $this->scanned_form_tags ) ) { |
| 272 | $scanned = $this->scanned_form_tags; |
| 273 | } else { |
| 274 | $scanned = $wpcf7_shortcode_manager->scan_shortcode( $this->form ); |
| 275 | $this->scanned_form_tags = $scanned; |
| 276 | } |
| 277 | |
| 278 | if ( empty( $scanned ) ) |
| 279 | return null; |
| 280 | |
| 281 | if ( ! is_array( $cond ) || empty( $cond ) ) |
| 282 | return $scanned; |
| 283 | |
| 284 | for ( $i = 0, $size = count( $scanned ); $i < $size; $i++ ) { |
| 285 | |
| 286 | if ( isset( $cond['type'] ) ) { |
| 287 | if ( is_string( $cond['type'] ) && ! empty( $cond['type'] ) ) { |
| 288 | if ( $scanned[$i]['type'] != $cond['type'] ) { |
| 289 | unset( $scanned[$i] ); |
| 290 | continue; |
| 291 | } |
| 292 | } elseif ( is_array( $cond['type'] ) ) { |
| 293 | if ( ! in_array( $scanned[$i]['type'], $cond['type'] ) ) { |
| 294 | unset( $scanned[$i] ); |
| 295 | continue; |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | if ( isset( $cond['name'] ) ) { |
| 301 | if ( is_string( $cond['name'] ) && ! empty( $cond['name'] ) ) { |
| 302 | if ( $scanned[$i]['name'] != $cond['name'] ) { |
| 303 | unset ( $scanned[$i] ); |
| 304 | continue; |
| 305 | } |
| 306 | } elseif ( is_array( $cond['name'] ) ) { |
| 307 | if ( ! in_array( $scanned[$i]['name'], $cond['name'] ) ) { |
| 308 | unset( $scanned[$i] ); |
| 309 | continue; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | return array_values( $scanned ); |
| 316 | } |
| 317 | |
| 318 | function form_elements() { |
| 319 | return apply_filters( 'wpcf7_form_elements', $this->form_do_shortcode() ); |
| 320 | } |
| 321 | |
| 322 | function setup_posted_data() { |
| 323 | $posted_data = (array) $_POST; |
| 324 | |
| 325 | $fes = $this->form_scan_shortcode(); |
| 326 | |
| 327 | foreach ( $fes as $fe ) { |
| 328 | if ( empty( $fe['name'] ) ) |
| 329 | continue; |
| 330 | |
| 331 | $name = $fe['name']; |
| 332 | $value = ''; |
| 333 | |
| 334 | if ( isset( $posted_data[$name] ) ) |
| 335 | $value = $posted_data[$name]; |
| 336 | |
| 337 | $pipes = $fe['pipes']; |
| 338 | |
| 339 | if ( WPCF7_USE_PIPE && is_a( $pipes, 'WPCF7_Pipes' ) && ! $pipes->zero() ) { |
| 340 | if ( is_array( $value) ) { |
| 341 | $new_value = array(); |
| 342 | |
| 343 | foreach ( $value as $v ) |
| 344 | $new_value[] = $pipes->do_pipe( stripslashes( $v ) ); |
| 345 | |
| 346 | $value = $new_value; |
| 347 | } else { |
| 348 | $value = $pipes->do_pipe( stripslashes( $value ) ); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | $posted_data[$name] = $value; |
| 353 | } |
| 354 | |
| 355 | $this->posted_data = apply_filters( 'wpcf7_posted_data', $posted_data ); |
| 356 | |
| 357 | return $this->posted_data; |
| 358 | } |
| 359 | |
| 360 | function submit( $ajax = false ) { |
| 361 | $result = array( |
| 362 | 'valid' => true, |
| 363 | 'invalid_reasons' => array(), |
| 364 | 'spam' => false, |
| 365 | 'message' => '', |
| 366 | 'mail_sent' => false, |
| 367 | 'scripts_on_sent_ok' => null, |
| 368 | 'scripts_on_submit' => null ); |
| 369 | |
| 370 | $this->setup_posted_data(); |
| 371 | |
| 372 | $validation = $this->validate(); |
| 373 | |
| 374 | if ( ! $validation['valid'] ) { // Validation error occured |
| 375 | $result['valid'] = false; |
| 376 | $result['invalid_reasons'] = $validation['reason']; |
| 377 | $result['message'] = $this->message( 'validation_error' ); |
| 378 | |
| 379 | } elseif ( ! $this->accepted() ) { // Not accepted terms |
| 380 | $result['message'] = $this->message( 'accept_terms' ); |
| 381 | |
| 382 | } elseif ( $this->spam() ) { // Spam! |
| 383 | $result['message'] = $this->message( 'spam' ); |
| 384 | $result['spam'] = true; |
| 385 | |
| 386 | } elseif ( $this->mail() ) { |
| 387 | $result['mail_sent'] = true; |
| 388 | $result['message'] = $this->message( 'mail_sent_ok' ); |
| 389 | |
| 390 | do_action_ref_array( 'wpcf7_mail_sent', array( &$this ) ); |
| 391 | |
| 392 | if ( $ajax ) { |
| 393 | $on_sent_ok = $this->additional_setting( 'on_sent_ok', false ); |
| 394 | |
| 395 | if ( ! empty( $on_sent_ok ) ) |
| 396 | $result['scripts_on_sent_ok'] = array_map( 'wpcf7_strip_quote', $on_sent_ok ); |
| 397 | } else { |
| 398 | $this->clear_post(); |
| 399 | } |
| 400 | |
| 401 | } else { |
| 402 | $result['message'] = $this->message( 'mail_sent_ng' ); |
| 403 | |
| 404 | do_action_ref_array( 'wpcf7_mail_failed', array( &$this ) ); |
| 405 | } |
| 406 | |
| 407 | if ( $ajax ) { |
| 408 | $on_submit = $this->additional_setting( 'on_submit', false ); |
| 409 | |
| 410 | if ( ! empty( $on_submit ) ) |
| 411 | $result['scripts_on_submit'] = array_map( 'wpcf7_strip_quote', $on_submit ); |
| 412 | } |
| 413 | |
| 414 | // remove upload files |
| 415 | foreach ( (array) $this->uploaded_files as $name => $path ) { |
| 416 | @unlink( $path ); |
| 417 | } |
| 418 | |
| 419 | return $result; |
| 420 | } |
| 421 | |
| 422 | /* Validate */ |
| 423 | |
| 424 | function validate() { |
| 425 | $fes = $this->form_scan_shortcode(); |
| 426 | |
| 427 | $result = array( 'valid' => true, 'reason' => array() ); |
| 428 | |
| 429 | foreach ( $fes as $fe ) { |
| 430 | $result = apply_filters( 'wpcf7_validate_' . $fe['type'], $result, $fe ); |
| 431 | } |
| 432 | |
| 433 | $result = apply_filters( 'wpcf7_validate', $result ); |
| 434 | |
| 435 | return $result; |
| 436 | } |
| 437 | |
| 438 | function accepted() { |
| 439 | $accepted = true; |
| 440 | |
| 441 | return apply_filters( 'wpcf7_acceptance', $accepted ); |
| 442 | } |
| 443 | |
| 444 | function spam() { |
| 445 | $spam = false; |
| 446 | |
| 447 | if ( WPCF7_VERIFY_NONCE && ! $this->verify_nonce() ) |
| 448 | $spam = true; |
| 449 | |
| 450 | if ( $this->blacklist_check() ) |
| 451 | $spam = true; |
| 452 | |
| 453 | return apply_filters( 'wpcf7_spam', $spam ); |
| 454 | } |
| 455 | |
| 456 | function verify_nonce() { |
| 457 | return wpcf7_verify_nonce( $_POST['_wpnonce'], $this->id ); |
| 458 | } |
| 459 | |
| 460 | function blacklist_check() { |
| 461 | $target = wpcf7_array_flatten( $this->posted_data ); |
| 462 | $target[] = $_SERVER['REMOTE_ADDR']; |
| 463 | $target[] = $_SERVER['HTTP_USER_AGENT']; |
| 464 | |
| 465 | $target = implode( "\n", $target ); |
| 466 | |
| 467 | return wpcf7_blacklist_check( $target ); |
| 468 | } |
| 469 | |
| 470 | /* Mail */ |
| 471 | |
| 472 | function mail() { |
| 473 | if ( $this->in_demo_mode() ) |
| 474 | $this->skip_mail = true; |
| 475 | |
| 476 | do_action_ref_array( 'wpcf7_before_send_mail', array( &$this ) ); |
| 477 | |
| 478 | if ( $this->skip_mail ) |
| 479 | return true; |
| 480 | |
| 481 | $result = $this->compose_mail( $this->setup_mail_template( $this->mail, 'mail' ) ); |
| 482 | |
| 483 | if ( $result ) { |
| 484 | $additional_mail = array(); |
| 485 | |
| 486 | if ( $this->mail_2['active'] ) |
| 487 | $additional_mail[] = $this->setup_mail_template( $this->mail_2, 'mail_2' ); |
| 488 | |
| 489 | $additional_mail = apply_filters_ref_array( 'wpcf7_additional_mail', |
| 490 | array( $additional_mail, &$this ) ); |
| 491 | |
| 492 | foreach ( $additional_mail as $mail ) |
| 493 | $this->compose_mail( $mail ); |
| 494 | |
| 495 | return true; |
| 496 | } |
| 497 | |
| 498 | return false; |
| 499 | } |
| 500 | |
| 501 | function setup_mail_template( $mail_template, $name = '' ) { |
| 502 | $defaults = array( |
| 503 | 'subject' => '', 'sender' => '', 'body' => '', |
| 504 | 'recipient' => '', 'additional_headers' => '', |
| 505 | 'attachments' => '', 'use_html' => false ); |
| 506 | |
| 507 | $mail_template = wp_parse_args( $mail_template, $defaults ); |
| 508 | |
| 509 | $name = trim( $name ); |
| 510 | |
| 511 | if ( ! empty( $name ) ) |
| 512 | $mail_template['name'] = $name; |
| 513 | |
| 514 | return $mail_template; |
| 515 | } |
| 516 | |
| 517 | function compose_mail( $mail_template, $send = true ) { |
| 518 | $this->mail_template_in_process = $mail_template; |
| 519 | |
| 520 | $use_html = (bool) $mail_template['use_html']; |
| 521 | |
| 522 | $subject = $this->replace_mail_tags( $mail_template['subject'] ); |
| 523 | $sender = $this->replace_mail_tags( $mail_template['sender'] ); |
| 524 | $recipient = $this->replace_mail_tags( $mail_template['recipient'] ); |
| 525 | $additional_headers = $this->replace_mail_tags( $mail_template['additional_headers'] ); |
| 526 | |
| 527 | if ( $use_html ) { |
| 528 | $body = $this->replace_mail_tags( $mail_template['body'], true ); |
| 529 | $body = wpautop( $body ); |
| 530 | } else { |
| 531 | $body = $this->replace_mail_tags( $mail_template['body'] ); |
| 532 | } |
| 533 | |
| 534 | $attachments = array(); |
| 535 | |
| 536 | foreach ( (array) $this->uploaded_files as $name => $path ) { |
| 537 | if ( false === strpos( $mail_template['attachments'], "[${name}]" ) || empty( $path ) ) |
| 538 | continue; |
| 539 | |
| 540 | $attachments[] = $path; |
| 541 | } |
| 542 | |
| 543 | $components = compact( |
| 544 | 'subject', 'sender', 'body', 'recipient', 'additional_headers', 'attachments' ); |
| 545 | |
| 546 | $components = apply_filters_ref_array( 'wpcf7_mail_components', |
| 547 | array( $components, &$this ) ); |
| 548 | |
| 549 | extract( $components ); |
| 550 | |
| 551 | $headers = "From: $sender\n"; |
| 552 | |
| 553 | if ( $use_html ) |
| 554 | $headers .= "Content-Type: text/html\n"; |
| 555 | |
| 556 | $additional_headers = trim( $additional_headers ); |
| 557 | |
| 558 | if ( $additional_headers ) |
| 559 | $headers .= $additional_headers . "\n"; |
| 560 | |
| 561 | if ( $send ) |
| 562 | return @wp_mail( $recipient, $subject, $body, $headers, $attachments ); |
| 563 | |
| 564 | return compact( 'subject', 'sender', 'body', 'recipient', 'headers', 'attachments' ); |
| 565 | } |
| 566 | |
| 567 | function replace_mail_tags( $content, $html = false ) { |
| 568 | $regex = '/(\[?)\[\s*([a-zA-Z_][0-9a-zA-Z:._-]*)\s*\](\]?)/'; |
| 569 | |
| 570 | if ( $html ) |
| 571 | $callback = array( &$this, 'mail_callback_html' ); |
| 572 | else |
| 573 | $callback = array( &$this, 'mail_callback' ); |
| 574 | |
| 575 | return preg_replace_callback( $regex, $callback, $content ); |
| 576 | } |
| 577 | |
| 578 | function mail_callback_html( $matches ) { |
| 579 | return $this->mail_callback( $matches, true ); |
| 580 | } |
| 581 | |
| 582 | function mail_callback( $matches, $html = false ) { |
| 583 | // allow [[foo]] syntax for escaping a tag |
| 584 | if ( $matches[1] == '[' && $matches[3] == ']' ) |
| 585 | return substr( $matches[0], 1, -1 ); |
| 586 | |
| 587 | $tag = $matches[0]; |
| 588 | $tagname = $matches[2]; |
| 589 | |
| 590 | $do_not_heat = false; |
| 591 | |
| 592 | if ( preg_match( '/^_raw_(.+)$/', $tagname, $matches ) ) { |
| 593 | $tagname = trim( $matches[1] ); |
| 594 | $do_not_heat = true; |
| 595 | } |
| 596 | |
| 597 | if ( isset( $this->posted_data[$tagname] ) ) { |
| 598 | |
| 599 | if ( $do_not_heat ) |
| 600 | $submitted = isset( $_POST[$tagname] ) ? $_POST[$tagname] : ''; |
| 601 | else |
| 602 | $submitted = $this->posted_data[$tagname]; |
| 603 | |
| 604 | if ( is_array( $submitted ) ) |
| 605 | $replaced = join( ', ', $submitted ); |
| 606 | else |
| 607 | $replaced = $submitted; |
| 608 | |
| 609 | if ( $html ) { |
| 610 | $replaced = strip_tags( $replaced ); |
| 611 | $replaced = wptexturize( $replaced ); |
| 612 | } |
| 613 | |
| 614 | $replaced = apply_filters( 'wpcf7_mail_tag_replaced', $replaced, |
| 615 | $submitted, $html ); |
| 616 | |
| 617 | return stripslashes( $replaced ); |
| 618 | } |
| 619 | |
| 620 | $special = apply_filters( 'wpcf7_special_mail_tags', '', $tagname, $html ); |
| 621 | |
| 622 | if ( ! empty( $special ) ) |
| 623 | return $special; |
| 624 | |
| 625 | return $tag; |
| 626 | } |
| 627 | |
| 628 | /* Message */ |
| 629 | |
| 630 | function message( $status ) { |
| 631 | $messages = $this->messages; |
| 632 | $message = isset( $messages[$status] ) ? $messages[$status] : ''; |
| 633 | |
| 634 | $message = $this->replace_mail_tags( $message, true ); |
| 635 | |
| 636 | return apply_filters( 'wpcf7_display_message', $message, $status ); |
| 637 | } |
| 638 | |
| 639 | /* Additional settings */ |
| 640 | |
| 641 | function additional_setting( $name, $max = 1 ) { |
| 642 | $tmp_settings = (array) explode( "\n", $this->additional_settings ); |
| 643 | |
| 644 | $count = 0; |
| 645 | $values = array(); |
| 646 | |
| 647 | foreach ( $tmp_settings as $setting ) { |
| 648 | if ( preg_match('/^([a-zA-Z0-9_]+)[\t ]*:(.*)$/', $setting, $matches ) ) { |
| 649 | if ( $matches[1] != $name ) |
| 650 | continue; |
| 651 | |
| 652 | if ( ! $max || $count < (int) $max ) { |
| 653 | $values[] = trim( $matches[2] ); |
| 654 | $count += 1; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | return $values; |
| 660 | } |
| 661 | |
| 662 | function in_demo_mode() { |
| 663 | $settings = $this->additional_setting( 'demo_mode', false ); |
| 664 | |
| 665 | foreach ( $settings as $setting ) { |
| 666 | if ( in_array( $setting, array( 'on', 'true', '1' ) ) ) |
| 667 | return true; |
| 668 | } |
| 669 | |
| 670 | return false; |
| 671 | } |
| 672 | |
| 673 | /* Upgrade */ |
| 674 | |
| 675 | function upgrade() { |
| 676 | if ( is_array( $this->mail ) ) { |
| 677 | if ( ! isset( $this->mail['recipient'] ) ) |
| 678 | $this->mail['recipient'] = get_option( 'admin_email' ); |
| 679 | } |
| 680 | |
| 681 | if ( is_array( $this->messages ) ) { |
| 682 | foreach ( wpcf7_messages() as $key => $arr ) { |
| 683 | if ( ! isset( $this->messages[$key] ) ) |
| 684 | $this->messages[$key] = $arr['default']; |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | /* Save */ |
| 690 | |
| 691 | function save() { |
| 692 | $props = $this->get_properties(); |
| 693 | |
| 694 | $post_content = implode( "\n", wpcf7_array_flatten( $props ) ); |
| 695 | |
| 696 | if ( $this->initial ) { |
| 697 | $post_id = wp_insert_post( array( |
| 698 | 'post_type' => self::post_type, |
| 699 | 'post_status' => 'publish', |
| 700 | 'post_title' => $this->title, |
| 701 | 'post_content' => trim( $post_content ) ) ); |
| 702 | } else { |
| 703 | $post_id = wp_update_post( array( |
| 704 | 'ID' => (int) $this->id, |
| 705 | 'post_status' => 'publish', |
| 706 | 'post_title' => $this->title, |
| 707 | 'post_content' => trim( $post_content ) ) ); |
| 708 | } |
| 709 | |
| 710 | if ( $post_id ) { |
| 711 | foreach ( $props as $prop => $value ) |
| 712 | update_post_meta( $post_id, '_' . $prop, wpcf7_normalize_newline_deep( $value ) ); |
| 713 | |
| 714 | if ( $this->initial ) { |
| 715 | $this->initial = false; |
| 716 | $this->id = $post_id; |
| 717 | do_action_ref_array( 'wpcf7_after_create', array( &$this ) ); |
| 718 | } else { |
| 719 | do_action_ref_array( 'wpcf7_after_update', array( &$this ) ); |
| 720 | } |
| 721 | |
| 722 | do_action_ref_array( 'wpcf7_after_save', array( &$this ) ); |
| 723 | } |
| 724 | |
| 725 | return $post_id; |
| 726 | } |
| 727 | |
| 728 | function copy() { |
| 729 | $new = new WPCF7_ContactForm(); |
| 730 | $new->initial = true; |
| 731 | $new->title = $this->title . '_copy'; |
| 732 | |
| 733 | $props = $this->get_properties(); |
| 734 | |
| 735 | foreach ( $props as $prop => $value ) |
| 736 | $new->{$prop} = $value; |
| 737 | |
| 738 | $new = apply_filters_ref_array( 'wpcf7_copy', array( &$new, &$this ) ); |
| 739 | |
| 740 | return $new; |
| 741 | } |
| 742 | |
| 743 | function delete() { |
| 744 | if ( $this->initial ) |
| 745 | return; |
| 746 | |
| 747 | if ( wp_delete_post( $this->id, true ) ) { |
| 748 | $this->initial = true; |
| 749 | $this->id = null; |
| 750 | return true; |
| 751 | } |
| 752 | |
| 753 | return false; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | function wpcf7_contact_form( $id ) { |
| 758 | $contact_form = new WPCF7_ContactForm( $id ); |
| 759 | |
| 760 | if ( $contact_form->initial ) |
| 761 | return false; |
| 762 | |
| 763 | return $contact_form; |
| 764 | } |
| 765 | |
| 766 | function wpcf7_get_contact_form_by_old_id( $old_id ) { |
| 767 | global $wpdb; |
| 768 | |
| 769 | $q = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_old_cf7_unit_id'" |
| 770 | . $wpdb->prepare( " AND meta_value = %d", $old_id ); |
| 771 | |
| 772 | if ( $new_id = $wpdb->get_var( $q ) ) |
| 773 | return wpcf7_contact_form( $new_id ); |
| 774 | } |
| 775 | |
| 776 | function wpcf7_get_contact_form_by_title( $title ) { |
| 777 | $page = get_page_by_title( $title, OBJECT, WPCF7_ContactForm::post_type ); |
| 778 | |
| 779 | if ( $page ) |
| 780 | return wpcf7_contact_form( $page->ID ); |
| 781 | |
| 782 | return null; |
| 783 | } |
| 784 | |
| 785 | function wpcf7_get_contact_form_default_pack( $args = '' ) { |
| 786 | global $l10n; |
| 787 | |
| 788 | $defaults = array( 'locale' => null, 'title' => '' ); |
| 789 | $args = wp_parse_args( $args, $defaults ); |
| 790 | |
| 791 | $locale = $args['locale']; |
| 792 | $title = $args['title']; |
| 793 | |
| 794 | if ( $locale && $locale != get_locale() ) { |
| 795 | $mo_orig = $l10n['wpcf7']; |
| 796 | unset( $l10n['wpcf7'] ); |
| 797 | |
| 798 | if ( 'en_US' != $locale ) { |
| 799 | $mofile = wpcf7_plugin_path( 'languages/wpcf7-' . $locale . '.mo' ); |
| 800 | if ( ! load_textdomain( 'wpcf7', $mofile ) ) { |
| 801 | $l10n['wpcf7'] = $mo_orig; |
| 802 | unset( $mo_orig ); |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | $contact_form = new WPCF7_ContactForm(); |
| 808 | $contact_form->initial = true; |
| 809 | |
| 810 | $contact_form->title = ( $title ? $title : __( 'Untitled', 'wpcf7' ) ); |
| 811 | |
| 812 | $props = $contact_form->get_properties(); |
| 813 | |
| 814 | foreach ( $props as $prop => $value ) |
| 815 | $contact_form->{$prop} = wpcf7_get_default_template( $prop ); |
| 816 | |
| 817 | if ( isset( $mo_orig ) ) |
| 818 | $l10n['wpcf7'] = $mo_orig; |
| 819 | |
| 820 | $contact_form = apply_filters_ref_array( 'wpcf7_contact_form_default_pack', |
| 821 | array( &$contact_form, $args ) ); |
| 822 | |
| 823 | return $contact_form; |
| 824 | } |
| 825 | |
| 826 | function wpcf7_get_current_contact_form() { |
| 827 | global $wpcf7_contact_form; |
| 828 | |
| 829 | if ( ! is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) ) |
| 830 | return null; |
| 831 | |
| 832 | return $wpcf7_contact_form; |
| 833 | } |
| 834 | |
| 835 | function wpcf7_is_posted() { |
| 836 | if ( ! $contact_form = wpcf7_get_current_contact_form() ) |
| 837 | return false; |
| 838 | |
| 839 | return $contact_form->is_posted(); |
| 840 | } |
| 841 | |
| 842 | function wpcf7_get_validation_error( $name ) { |
| 843 | if ( ! $contact_form = wpcf7_get_current_contact_form() ) |
| 844 | return ''; |
| 845 | |
| 846 | return $contact_form->validation_error( $name ); |
| 847 | } |
| 848 | |
| 849 | function wpcf7_get_message( $status ) { |
| 850 | if ( ! $contact_form = wpcf7_get_current_contact_form() ) |
| 851 | return ''; |
| 852 | |
| 853 | return $contact_form->message( $status ); |
| 854 | } |
| 855 | |
| 856 | function wpcf7_scan_shortcode( $cond = null ) { |
| 857 | if ( ! $contact_form = wpcf7_get_current_contact_form() ) |
| 858 | return null; |
| 859 | |
| 860 | return $contact_form->form_scan_shortcode( $cond ); |
| 861 | } |
| 862 | |
| 863 | function wpcf7_form_controls_class( $type, $default = '' ) { |
| 864 | $type = trim( $type ); |
| 865 | $default = array_filter( explode( ' ', $default ) ); |
| 866 | |
| 867 | $classes = array_merge( array( 'wpcf7-form-control' ), $default ); |
| 868 | |
| 869 | $typebase = rtrim( $type, '*' ); |
| 870 | $required = ( '*' == substr( $type, -1 ) ); |
| 871 | |
| 872 | $classes[] = 'wpcf7-' . $typebase; |
| 873 | |
| 874 | if ( $required ) |
| 875 | $classes[] = 'wpcf7-validates-as-required'; |
| 876 | |
| 877 | $classes = array_unique( $classes ); |
| 878 | |
| 879 | return implode( ' ', $classes ); |
| 880 | } |
| 881 | |
| 882 | ?> |