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