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