1 | <?php |
---|
2 | |
---|
3 | class WPCF7_ContactForm { |
---|
4 | |
---|
5 | use WPCF7_SWV_SchemaHolder; |
---|
6 | use WPCF7_PipesHolder; |
---|
7 | |
---|
8 | const post_type = 'wpcf7_contact_form'; |
---|
9 | |
---|
10 | private static $found_items = 0; |
---|
11 | private static $current = null; |
---|
12 | |
---|
13 | private $id; |
---|
14 | private $name; |
---|
15 | private $title; |
---|
16 | private $locale; |
---|
17 | private $properties = array(); |
---|
18 | private $unit_tag; |
---|
19 | private $responses_count = 0; |
---|
20 | private $scanned_form_tags; |
---|
21 | private $shortcode_atts = array(); |
---|
22 | private $hash = ''; |
---|
23 | |
---|
24 | |
---|
25 | /** |
---|
26 | * Returns count of contact forms found by the previous retrieval. |
---|
27 | * |
---|
28 | * @return int Count of contact forms. |
---|
29 | */ |
---|
30 | public static function count() { |
---|
31 | return self::$found_items; |
---|
32 | } |
---|
33 | |
---|
34 | |
---|
35 | /** |
---|
36 | * Returns the contact form that is currently processed. |
---|
37 | * |
---|
38 | * @return WPCF7_ContactForm|null Current contact form object. Null if unset. |
---|
39 | */ |
---|
40 | public static function get_current() { |
---|
41 | return self::$current; |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | /** |
---|
46 | * Registers the post type for contact forms. |
---|
47 | */ |
---|
48 | public static function register_post_type() { |
---|
49 | register_post_type( self::post_type, array( |
---|
50 | 'labels' => array( |
---|
51 | 'name' => __( 'Contact Forms', 'contact-form-7' ), |
---|
52 | 'singular_name' => __( 'Contact Form', 'contact-form-7' ), |
---|
53 | ), |
---|
54 | 'rewrite' => false, |
---|
55 | 'query_var' => false, |
---|
56 | 'public' => false, |
---|
57 | 'capability_type' => 'page', |
---|
58 | 'capabilities' => array( |
---|
59 | 'edit_post' => 'wpcf7_edit_contact_form', |
---|
60 | 'read_post' => 'wpcf7_read_contact_form', |
---|
61 | 'delete_post' => 'wpcf7_delete_contact_form', |
---|
62 | 'edit_posts' => 'wpcf7_edit_contact_forms', |
---|
63 | 'edit_others_posts' => 'wpcf7_edit_contact_forms', |
---|
64 | 'publish_posts' => 'wpcf7_edit_contact_forms', |
---|
65 | 'read_private_posts' => 'wpcf7_edit_contact_forms', |
---|
66 | ), |
---|
67 | ) ); |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | /** |
---|
72 | * Retrieves contact form data that match given conditions. |
---|
73 | * |
---|
74 | * @param string|array $args Optional. Arguments to be passed to WP_Query. |
---|
75 | * @return array Array of WPCF7_ContactForm objects. |
---|
76 | */ |
---|
77 | public static function find( $args = '' ) { |
---|
78 | $defaults = array( |
---|
79 | 'post_status' => 'any', |
---|
80 | 'posts_per_page' => -1, |
---|
81 | 'offset' => 0, |
---|
82 | 'orderby' => 'ID', |
---|
83 | 'order' => 'ASC', |
---|
84 | ); |
---|
85 | |
---|
86 | $args = wp_parse_args( $args, $defaults ); |
---|
87 | |
---|
88 | $args['post_type'] = self::post_type; |
---|
89 | |
---|
90 | $q = new WP_Query(); |
---|
91 | $posts = $q->query( $args ); |
---|
92 | |
---|
93 | self::$found_items = $q->found_posts; |
---|
94 | |
---|
95 | $objs = array(); |
---|
96 | |
---|
97 | foreach ( (array) $posts as $post ) { |
---|
98 | $objs[] = new self( $post ); |
---|
99 | } |
---|
100 | |
---|
101 | return $objs; |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | /** |
---|
106 | * Returns a contact form data filled by default template contents. |
---|
107 | * |
---|
108 | * @param string|array $options Optional. Contact form options. |
---|
109 | * @return WPCF7_ContactForm A new contact form object. |
---|
110 | */ |
---|
111 | public static function get_template( $options = '' ) { |
---|
112 | $options = wp_parse_args( $options, array( |
---|
113 | 'locale' => null, |
---|
114 | 'title' => __( 'Untitled', 'contact-form-7' ), |
---|
115 | ) ); |
---|
116 | |
---|
117 | if ( ! isset( $options['locale'] ) ) { |
---|
118 | $options['locale'] = determine_locale(); |
---|
119 | } |
---|
120 | |
---|
121 | $callback = static function ( $options ) { |
---|
122 | $contact_form = new self; |
---|
123 | $contact_form->title = $options['title']; |
---|
124 | $contact_form->locale = $options['locale']; |
---|
125 | |
---|
126 | $properties = $contact_form->get_properties(); |
---|
127 | |
---|
128 | foreach ( $properties as $key => $value ) { |
---|
129 | $default_template = WPCF7_ContactFormTemplate::get_default( $key ); |
---|
130 | |
---|
131 | if ( isset( $default_template ) ) { |
---|
132 | $properties[$key] = $default_template; |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | $contact_form->properties = $properties; |
---|
137 | |
---|
138 | return $contact_form; |
---|
139 | }; |
---|
140 | |
---|
141 | $contact_form = wpcf7_switch_locale( |
---|
142 | $options['locale'], |
---|
143 | $callback, |
---|
144 | $options |
---|
145 | ); |
---|
146 | |
---|
147 | self::$current = apply_filters( 'wpcf7_contact_form_default_pack', |
---|
148 | $contact_form, $options |
---|
149 | ); |
---|
150 | |
---|
151 | return self::$current; |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | /** |
---|
156 | * Creates a WPCF7_ContactForm object and sets it as the current instance. |
---|
157 | * |
---|
158 | * @param WPCF7_ContactForm|WP_Post|int $post Object or post ID. |
---|
159 | * @return WPCF7_ContactForm|null Contact form object. Null if unset. |
---|
160 | */ |
---|
161 | public static function get_instance( $post ) { |
---|
162 | $contact_form = null; |
---|
163 | |
---|
164 | if ( $post instanceof self ) { |
---|
165 | $contact_form = $post; |
---|
166 | } elseif ( ! empty( $post ) ) { |
---|
167 | $post = get_post( $post ); |
---|
168 | |
---|
169 | if ( isset( $post ) and self::post_type === get_post_type( $post ) ) { |
---|
170 | $contact_form = new self( $post ); |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | return self::$current = $contact_form; |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | /** |
---|
179 | * Generates a "unit-tag" for the given contact form ID. |
---|
180 | * |
---|
181 | * @return string Unit-tag. |
---|
182 | */ |
---|
183 | private static function generate_unit_tag( $id = 0 ) { |
---|
184 | static $global_count = 0; |
---|
185 | |
---|
186 | $global_count += 1; |
---|
187 | |
---|
188 | if ( in_the_loop() ) { |
---|
189 | $unit_tag = sprintf( 'wpcf7-f%1$d-p%2$d-o%3$d', |
---|
190 | absint( $id ), |
---|
191 | get_the_ID(), |
---|
192 | $global_count |
---|
193 | ); |
---|
194 | } else { |
---|
195 | $unit_tag = sprintf( 'wpcf7-f%1$d-o%2$d', |
---|
196 | absint( $id ), |
---|
197 | $global_count |
---|
198 | ); |
---|
199 | } |
---|
200 | |
---|
201 | return $unit_tag; |
---|
202 | } |
---|
203 | |
---|
204 | |
---|
205 | /** |
---|
206 | * Constructor. |
---|
207 | */ |
---|
208 | private function __construct( $post = null ) { |
---|
209 | $post = get_post( $post ); |
---|
210 | |
---|
211 | if ( $post |
---|
212 | and self::post_type === get_post_type( $post ) ) { |
---|
213 | $this->id = $post->ID; |
---|
214 | $this->name = $post->post_name; |
---|
215 | $this->title = $post->post_title; |
---|
216 | $this->locale = get_post_meta( $post->ID, '_locale', true ); |
---|
217 | $this->hash = get_post_meta( $post->ID, '_hash', true ); |
---|
218 | |
---|
219 | $this->construct_properties( $post ); |
---|
220 | $this->upgrade(); |
---|
221 | } else { |
---|
222 | $this->construct_properties(); |
---|
223 | } |
---|
224 | |
---|
225 | do_action( 'wpcf7_contact_form', $this ); |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | /** |
---|
230 | * Magic method for property overloading. |
---|
231 | */ |
---|
232 | public function __get( $name ) { |
---|
233 | $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' ); |
---|
234 | |
---|
235 | if ( 'id' == $name ) { |
---|
236 | if ( WP_DEBUG ) { |
---|
237 | trigger_error( |
---|
238 | sprintf( $message, 'id', 'id()' ), |
---|
239 | E_USER_DEPRECATED |
---|
240 | ); |
---|
241 | } |
---|
242 | |
---|
243 | return $this->id; |
---|
244 | } elseif ( 'title' == $name ) { |
---|
245 | if ( WP_DEBUG ) { |
---|
246 | trigger_error( |
---|
247 | sprintf( $message, 'title', 'title()' ), |
---|
248 | E_USER_DEPRECATED |
---|
249 | ); |
---|
250 | } |
---|
251 | |
---|
252 | return $this->title; |
---|
253 | } elseif ( $prop = $this->prop( $name ) ) { |
---|
254 | if ( WP_DEBUG ) { |
---|
255 | trigger_error( |
---|
256 | sprintf( $message, $name, 'prop(\'' . $name . '\')' ), |
---|
257 | E_USER_DEPRECATED |
---|
258 | ); |
---|
259 | } |
---|
260 | |
---|
261 | return $prop; |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | |
---|
266 | /** |
---|
267 | * Returns true if this contact form is not yet saved to the database. |
---|
268 | */ |
---|
269 | public function initial() { |
---|
270 | return empty( $this->id ); |
---|
271 | } |
---|
272 | |
---|
273 | |
---|
274 | /** |
---|
275 | * Constructs contact form properties. This is called only once |
---|
276 | * from the constructor. |
---|
277 | */ |
---|
278 | private function construct_properties( $post = null ) { |
---|
279 | $builtin_properties = array( |
---|
280 | 'form' => '', |
---|
281 | 'mail' => array(), |
---|
282 | 'mail_2' => array(), |
---|
283 | 'messages' => array(), |
---|
284 | 'additional_settings' => '', |
---|
285 | ); |
---|
286 | |
---|
287 | $properties = apply_filters( |
---|
288 | 'wpcf7_pre_construct_contact_form_properties', |
---|
289 | $builtin_properties, $this |
---|
290 | ); |
---|
291 | |
---|
292 | // Filtering out properties with invalid name |
---|
293 | $properties = array_filter( |
---|
294 | $properties, |
---|
295 | static function ( $key ) { |
---|
296 | $sanitized_key = sanitize_key( $key ); |
---|
297 | return $key === $sanitized_key; |
---|
298 | }, |
---|
299 | ARRAY_FILTER_USE_KEY |
---|
300 | ); |
---|
301 | |
---|
302 | foreach ( $properties as $name => $val ) { |
---|
303 | $prop = $this->retrieve_property( $name ); |
---|
304 | |
---|
305 | if ( isset( $prop ) ) { |
---|
306 | $properties[$name] = $prop; |
---|
307 | } |
---|
308 | } |
---|
309 | |
---|
310 | $this->properties = $properties; |
---|
311 | |
---|
312 | foreach ( $properties as $name => $val ) { |
---|
313 | $properties[$name] = apply_filters( |
---|
314 | "wpcf7_contact_form_property_{$name}", |
---|
315 | $val, $this |
---|
316 | ); |
---|
317 | } |
---|
318 | |
---|
319 | $this->properties = $properties; |
---|
320 | |
---|
321 | $properties = (array) apply_filters( |
---|
322 | 'wpcf7_contact_form_properties', |
---|
323 | $properties, $this |
---|
324 | ); |
---|
325 | |
---|
326 | $this->properties = $properties; |
---|
327 | } |
---|
328 | |
---|
329 | |
---|
330 | /** |
---|
331 | * Retrieves contact form property of the specified name from the database. |
---|
332 | * |
---|
333 | * @param string $name Property name. |
---|
334 | * @return array|string|null Property value. Null if property does not exist. |
---|
335 | */ |
---|
336 | private function retrieve_property( $name ) { |
---|
337 | $property = null; |
---|
338 | |
---|
339 | if ( ! $this->initial() ) { |
---|
340 | $post_id = $this->id; |
---|
341 | |
---|
342 | if ( metadata_exists( 'post', $post_id, '_' . $name ) ) { |
---|
343 | $property = get_post_meta( $post_id, '_' . $name, true ); |
---|
344 | } elseif ( metadata_exists( 'post', $post_id, $name ) ) { |
---|
345 | $property = get_post_meta( $post_id, $name, true ); |
---|
346 | } |
---|
347 | } |
---|
348 | |
---|
349 | return $property; |
---|
350 | } |
---|
351 | |
---|
352 | |
---|
353 | /** |
---|
354 | * Returns the value for the given property name. |
---|
355 | * |
---|
356 | * @param string $name Property name. |
---|
357 | * @return array|string|null Property value. Null if property does not exist. |
---|
358 | */ |
---|
359 | public function prop( $name ) { |
---|
360 | $props = $this->get_properties(); |
---|
361 | return isset( $props[$name] ) ? $props[$name] : null; |
---|
362 | } |
---|
363 | |
---|
364 | |
---|
365 | /** |
---|
366 | * Returns all the properties. |
---|
367 | * |
---|
368 | * @return array This contact form's properties. |
---|
369 | */ |
---|
370 | public function get_properties() { |
---|
371 | return (array) $this->properties; |
---|
372 | } |
---|
373 | |
---|
374 | |
---|
375 | /** |
---|
376 | * Updates properties. |
---|
377 | * |
---|
378 | * @param array $properties New properties. |
---|
379 | */ |
---|
380 | public function set_properties( $properties ) { |
---|
381 | $defaults = $this->get_properties(); |
---|
382 | |
---|
383 | $properties = wp_parse_args( $properties, $defaults ); |
---|
384 | $properties = array_intersect_key( $properties, $defaults ); |
---|
385 | |
---|
386 | $this->properties = $properties; |
---|
387 | } |
---|
388 | |
---|
389 | |
---|
390 | /** |
---|
391 | * Returns ID of this contact form. |
---|
392 | * |
---|
393 | * @return int The ID. |
---|
394 | */ |
---|
395 | public function id() { |
---|
396 | return $this->id; |
---|
397 | } |
---|
398 | |
---|
399 | |
---|
400 | /** |
---|
401 | * Returns unit-tag for this contact form. |
---|
402 | * |
---|
403 | * @return string Unit-tag. |
---|
404 | */ |
---|
405 | public function unit_tag() { |
---|
406 | return $this->unit_tag; |
---|
407 | } |
---|
408 | |
---|
409 | |
---|
410 | /** |
---|
411 | * Returns name (slug) of this contact form. |
---|
412 | * |
---|
413 | * @return string Name. |
---|
414 | */ |
---|
415 | public function name() { |
---|
416 | return $this->name; |
---|
417 | } |
---|
418 | |
---|
419 | |
---|
420 | /** |
---|
421 | * Returns title of this contact form. |
---|
422 | * |
---|
423 | * @return string Title. |
---|
424 | */ |
---|
425 | public function title() { |
---|
426 | return $this->title; |
---|
427 | } |
---|
428 | |
---|
429 | |
---|
430 | /** |
---|
431 | * Set a title for this contact form. |
---|
432 | * |
---|
433 | * @param string $title Title. |
---|
434 | */ |
---|
435 | public function set_title( $title ) { |
---|
436 | $title = strip_tags( $title ); |
---|
437 | $title = trim( $title ); |
---|
438 | |
---|
439 | if ( '' === $title ) { |
---|
440 | $title = __( 'Untitled', 'contact-form-7' ); |
---|
441 | } |
---|
442 | |
---|
443 | $this->title = $title; |
---|
444 | } |
---|
445 | |
---|
446 | |
---|
447 | /** |
---|
448 | * Returns the locale code of this contact form. |
---|
449 | * |
---|
450 | * @return string Locale code. Empty string if no valid locale is set. |
---|
451 | */ |
---|
452 | public function locale() { |
---|
453 | if ( wpcf7_is_valid_locale( $this->locale ) ) { |
---|
454 | return $this->locale; |
---|
455 | } else { |
---|
456 | return ''; |
---|
457 | } |
---|
458 | } |
---|
459 | |
---|
460 | |
---|
461 | /** |
---|
462 | * Sets a locale for this contact form. |
---|
463 | * |
---|
464 | * @param string $locale Locale code. |
---|
465 | */ |
---|
466 | public function set_locale( $locale ) { |
---|
467 | $locale = trim( $locale ); |
---|
468 | |
---|
469 | if ( wpcf7_is_valid_locale( $locale ) ) { |
---|
470 | $this->locale = $locale; |
---|
471 | } else { |
---|
472 | $this->locale = 'en_US'; |
---|
473 | } |
---|
474 | } |
---|
475 | |
---|
476 | |
---|
477 | /** |
---|
478 | * Retrieves the random hash string tied to this contact form. |
---|
479 | * |
---|
480 | * @param int $length Length of hash string. |
---|
481 | * @return string Hash string unique to this contact form. |
---|
482 | */ |
---|
483 | public function hash( $length = 7 ) { |
---|
484 | return substr( $this->hash, 0, absint( $length ) ); |
---|
485 | } |
---|
486 | |
---|
487 | |
---|
488 | /** |
---|
489 | * Returns the specified shortcode attribute value. |
---|
490 | * |
---|
491 | * @param string $name Shortcode attribute name. |
---|
492 | * @return string|null Attribute value. Null if the attribute does not exist. |
---|
493 | */ |
---|
494 | public function shortcode_attr( $name ) { |
---|
495 | if ( isset( $this->shortcode_atts[$name] ) ) { |
---|
496 | return (string) $this->shortcode_atts[$name]; |
---|
497 | } |
---|
498 | } |
---|
499 | |
---|
500 | |
---|
501 | /** |
---|
502 | * Returns true if this contact form is identical to the submitted one. |
---|
503 | */ |
---|
504 | public function is_posted() { |
---|
505 | if ( ! WPCF7_Submission::get_instance() ) { |
---|
506 | return false; |
---|
507 | } |
---|
508 | |
---|
509 | if ( empty( $_POST['_wpcf7_unit_tag'] ) ) { |
---|
510 | return false; |
---|
511 | } |
---|
512 | |
---|
513 | return $this->unit_tag() === $_POST['_wpcf7_unit_tag']; |
---|
514 | } |
---|
515 | |
---|
516 | |
---|
517 | /** |
---|
518 | * Generates HTML that represents a form. |
---|
519 | * |
---|
520 | * @param string|array $options Optional. Form options. |
---|
521 | * @return string HTML output. |
---|
522 | */ |
---|
523 | public function form_html( $options = '' ) { |
---|
524 | $options = wp_parse_args( $options, array( |
---|
525 | 'html_id' => '', |
---|
526 | 'html_name' => '', |
---|
527 | 'html_title' => '', |
---|
528 | 'html_class' => '', |
---|
529 | 'output' => 'form', |
---|
530 | ) ); |
---|
531 | |
---|
532 | $this->shortcode_atts = $options; |
---|
533 | |
---|
534 | if ( 'raw_form' == $options['output'] ) { |
---|
535 | return sprintf( |
---|
536 | '<pre class="wpcf7-raw-form"><code>%s</code></pre>', |
---|
537 | esc_html( $this->prop( 'form' ) ) |
---|
538 | ); |
---|
539 | } |
---|
540 | |
---|
541 | if ( $this->is_true( 'subscribers_only' ) |
---|
542 | and ! current_user_can( 'wpcf7_submit', $this->id() ) ) { |
---|
543 | $notice = __( |
---|
544 | "This contact form is available only for logged in users.", |
---|
545 | 'contact-form-7' |
---|
546 | ); |
---|
547 | |
---|
548 | $notice = sprintf( |
---|
549 | '<p class="wpcf7-subscribers-only">%s</p>', |
---|
550 | esc_html( $notice ) |
---|
551 | ); |
---|
552 | |
---|
553 | return apply_filters( 'wpcf7_subscribers_only_notice', $notice, $this ); |
---|
554 | } |
---|
555 | |
---|
556 | $this->unit_tag = self::generate_unit_tag( $this->id ); |
---|
557 | |
---|
558 | $action_url = wpcf7_get_request_uri(); |
---|
559 | |
---|
560 | if ( $frag = strstr( $action_url, '#' ) ) { |
---|
561 | $action_url = substr( $action_url, 0, -strlen( $frag ) ); |
---|
562 | } |
---|
563 | |
---|
564 | $action_url .= '#' . $this->unit_tag(); |
---|
565 | |
---|
566 | $action_url = apply_filters( 'wpcf7_form_action_url', $action_url ); |
---|
567 | |
---|
568 | if ( |
---|
569 | str_starts_with( $action_url, '//' ) or |
---|
570 | ! str_starts_with( $action_url, '/' ) and |
---|
571 | ! str_starts_with( $action_url, home_url() ) |
---|
572 | ) { |
---|
573 | return sprintf( |
---|
574 | '<p class="wpcf7-invalid-action-url"><strong>%1$s</strong> %2$s</p>', |
---|
575 | esc_html( __( 'Error:', 'contact-form-7' ) ), |
---|
576 | esc_html( __( "Invalid action URL is detected.", 'contact-form-7' ) ) |
---|
577 | ); |
---|
578 | } |
---|
579 | |
---|
580 | $lang_tag = str_replace( '_', '-', $this->locale ); |
---|
581 | |
---|
582 | if ( preg_match( '/^([a-z]+-[a-z]+)-/i', $lang_tag, $matches ) ) { |
---|
583 | $lang_tag = $matches[1]; |
---|
584 | } |
---|
585 | |
---|
586 | $html = "\n" . sprintf( '<div %s>', |
---|
587 | wpcf7_format_atts( array( |
---|
588 | 'class' => 'wpcf7 no-js', |
---|
589 | 'id' => $this->unit_tag(), |
---|
590 | ( get_option( 'html_type' ) == 'text/html' ) ? 'lang' : 'xml:lang' |
---|
591 | => $lang_tag, |
---|
592 | 'dir' => wpcf7_is_rtl( $this->locale ) ? 'rtl' : 'ltr', |
---|
593 | ) ) |
---|
594 | ); |
---|
595 | |
---|
596 | $html .= "\n" . $this->screen_reader_response() . "\n"; |
---|
597 | |
---|
598 | $id_attr = apply_filters( 'wpcf7_form_id_attr', |
---|
599 | preg_replace( '/[^A-Za-z0-9:._-]/', '', $options['html_id'] ) |
---|
600 | ); |
---|
601 | |
---|
602 | $name_attr = apply_filters( 'wpcf7_form_name_attr', |
---|
603 | preg_replace( '/[^A-Za-z0-9:._-]/', '', $options['html_name'] ) |
---|
604 | ); |
---|
605 | |
---|
606 | $title_attr = apply_filters( 'wpcf7_form_title_attr', $options['html_title'] ); |
---|
607 | |
---|
608 | $class = 'wpcf7-form'; |
---|
609 | |
---|
610 | if ( $this->is_posted() ) { |
---|
611 | $submission = WPCF7_Submission::get_instance(); |
---|
612 | |
---|
613 | $data_status_attr = $this->form_status_class_name( |
---|
614 | $submission->get_status() |
---|
615 | ); |
---|
616 | |
---|
617 | $class .= sprintf( ' %s', $data_status_attr ); |
---|
618 | } else { |
---|
619 | $data_status_attr = 'init'; |
---|
620 | $class .= ' init'; |
---|
621 | } |
---|
622 | |
---|
623 | if ( $options['html_class'] ) { |
---|
624 | $class .= ' ' . $options['html_class']; |
---|
625 | } |
---|
626 | |
---|
627 | if ( $this->in_demo_mode() ) { |
---|
628 | $class .= ' demo'; |
---|
629 | } |
---|
630 | |
---|
631 | $class = explode( ' ', $class ); |
---|
632 | $class = array_map( 'sanitize_html_class', $class ); |
---|
633 | $class = array_filter( $class ); |
---|
634 | $class = array_unique( $class ); |
---|
635 | $class = implode( ' ', $class ); |
---|
636 | $class = apply_filters( 'wpcf7_form_class_attr', $class ); |
---|
637 | |
---|
638 | $enctype = wpcf7_enctype_value( apply_filters( 'wpcf7_form_enctype', '' ) ); |
---|
639 | $autocomplete = apply_filters( 'wpcf7_form_autocomplete', '' ); |
---|
640 | |
---|
641 | $atts = array( |
---|
642 | 'action' => esc_url( $action_url ), |
---|
643 | 'method' => 'post', |
---|
644 | 'class' => ( '' !== $class ) ? $class : null, |
---|
645 | 'id' => ( '' !== $id_attr ) ? $id_attr : null, |
---|
646 | 'name' => ( '' !== $name_attr ) ? $name_attr : null, |
---|
647 | 'aria-label' => ( '' !== $title_attr ) |
---|
648 | ? $title_attr : __( 'Contact form', 'contact-form-7' ), |
---|
649 | 'enctype' => ( '' !== $enctype ) ? $enctype : null, |
---|
650 | 'autocomplete' => ( '' !== $autocomplete ) ? $autocomplete : null, |
---|
651 | 'novalidate' => true, |
---|
652 | 'data-status' => $data_status_attr, |
---|
653 | ); |
---|
654 | |
---|
655 | $atts += (array) apply_filters( 'wpcf7_form_additional_atts', array() ); |
---|
656 | |
---|
657 | $html .= sprintf( '<form %s>', wpcf7_format_atts( $atts ) ) . "\n"; |
---|
658 | $html .= $this->form_hidden_fields(); |
---|
659 | $html .= $this->form_elements(); |
---|
660 | |
---|
661 | if ( ! $this->responses_count ) { |
---|
662 | $html .= $this->form_response_output(); |
---|
663 | } |
---|
664 | |
---|
665 | $html .= "\n" . '</form>'; |
---|
666 | $html .= "\n" . '</div>'; |
---|
667 | |
---|
668 | return $html . "\n"; |
---|
669 | } |
---|
670 | |
---|
671 | |
---|
672 | /** |
---|
673 | * Returns the class name that matches the given form status. |
---|
674 | */ |
---|
675 | private function form_status_class_name( $status ) { |
---|
676 | switch ( $status ) { |
---|
677 | case 'init': |
---|
678 | $class = 'init'; |
---|
679 | break; |
---|
680 | case 'validation_failed': |
---|
681 | $class = 'invalid'; |
---|
682 | break; |
---|
683 | case 'acceptance_missing': |
---|
684 | $class = 'unaccepted'; |
---|
685 | break; |
---|
686 | case 'spam': |
---|
687 | $class = 'spam'; |
---|
688 | break; |
---|
689 | case 'aborted': |
---|
690 | $class = 'aborted'; |
---|
691 | break; |
---|
692 | case 'mail_sent': |
---|
693 | $class = 'sent'; |
---|
694 | break; |
---|
695 | case 'mail_failed': |
---|
696 | $class = 'failed'; |
---|
697 | break; |
---|
698 | default: |
---|
699 | $class = sprintf( |
---|
700 | 'custom-%s', |
---|
701 | preg_replace( '/[^0-9a-z]+/i', '-', $status ) |
---|
702 | ); |
---|
703 | } |
---|
704 | |
---|
705 | return $class; |
---|
706 | } |
---|
707 | |
---|
708 | |
---|
709 | /** |
---|
710 | * Returns a set of hidden fields. |
---|
711 | */ |
---|
712 | private function form_hidden_fields() { |
---|
713 | $hidden_fields = array( |
---|
714 | '_wpcf7' => $this->id(), |
---|
715 | '_wpcf7_version' => WPCF7_VERSION, |
---|
716 | '_wpcf7_locale' => $this->locale(), |
---|
717 | '_wpcf7_unit_tag' => $this->unit_tag(), |
---|
718 | '_wpcf7_container_post' => 0, |
---|
719 | '_wpcf7_posted_data_hash' => '', |
---|
720 | ); |
---|
721 | |
---|
722 | if ( in_the_loop() ) { |
---|
723 | $hidden_fields['_wpcf7_container_post'] = (int) get_the_ID(); |
---|
724 | } |
---|
725 | |
---|
726 | if ( $this->nonce_is_active() and is_user_logged_in() ) { |
---|
727 | $hidden_fields['_wpnonce'] = wpcf7_create_nonce(); |
---|
728 | } |
---|
729 | |
---|
730 | $hidden_fields += (array) apply_filters( |
---|
731 | 'wpcf7_form_hidden_fields', array() |
---|
732 | ); |
---|
733 | |
---|
734 | $content = ''; |
---|
735 | |
---|
736 | foreach ( $hidden_fields as $name => $value ) { |
---|
737 | $content .= sprintf( |
---|
738 | '<input type="hidden" name="%1$s" value="%2$s" />', |
---|
739 | esc_attr( $name ), |
---|
740 | esc_attr( $value ) |
---|
741 | ) . "\n"; |
---|
742 | } |
---|
743 | |
---|
744 | return '<div style="display: none;">' . "\n" . $content . '</div>' . "\n"; |
---|
745 | } |
---|
746 | |
---|
747 | |
---|
748 | /** |
---|
749 | * Returns the visible response output for a form submission. |
---|
750 | */ |
---|
751 | public function form_response_output() { |
---|
752 | $status = 'init'; |
---|
753 | $class = 'wpcf7-response-output'; |
---|
754 | $content = ''; |
---|
755 | |
---|
756 | if ( $this->is_posted() ) { // Post response output for non-AJAX |
---|
757 | $submission = WPCF7_Submission::get_instance(); |
---|
758 | $status = $submission->get_status(); |
---|
759 | $content = $submission->get_response(); |
---|
760 | } |
---|
761 | |
---|
762 | $atts = array( |
---|
763 | 'class' => trim( $class ), |
---|
764 | 'aria-hidden' => 'true', |
---|
765 | ); |
---|
766 | |
---|
767 | $output = sprintf( '<div %1$s>%2$s</div>', |
---|
768 | wpcf7_format_atts( $atts ), |
---|
769 | esc_html( $content ) |
---|
770 | ); |
---|
771 | |
---|
772 | $output = apply_filters( 'wpcf7_form_response_output', |
---|
773 | $output, $class, $content, $this, $status |
---|
774 | ); |
---|
775 | |
---|
776 | $this->responses_count += 1; |
---|
777 | |
---|
778 | return $output; |
---|
779 | } |
---|
780 | |
---|
781 | |
---|
782 | /** |
---|
783 | * Returns the response output that is only accessible from screen readers. |
---|
784 | */ |
---|
785 | public function screen_reader_response() { |
---|
786 | $primary_response = ''; |
---|
787 | $validation_errors = array(); |
---|
788 | |
---|
789 | if ( $this->is_posted() ) { // Post response output for non-AJAX |
---|
790 | $submission = WPCF7_Submission::get_instance(); |
---|
791 | $primary_response = $submission->get_response(); |
---|
792 | |
---|
793 | if ( $invalid_fields = $submission->get_invalid_fields() ) { |
---|
794 | foreach ( (array) $invalid_fields as $name => $field ) { |
---|
795 | $list_item = esc_html( $field['reason'] ); |
---|
796 | |
---|
797 | if ( $field['idref'] ) { |
---|
798 | $list_item = sprintf( |
---|
799 | '<a href="#%1$s">%2$s</a>', |
---|
800 | esc_attr( $field['idref'] ), |
---|
801 | $list_item |
---|
802 | ); |
---|
803 | } |
---|
804 | |
---|
805 | $validation_error_id = wpcf7_get_validation_error_reference( |
---|
806 | $name, |
---|
807 | $this->unit_tag() |
---|
808 | ); |
---|
809 | |
---|
810 | if ( $validation_error_id ) { |
---|
811 | $list_item = sprintf( |
---|
812 | '<li id="%1$s">%2$s</li>', |
---|
813 | esc_attr( $validation_error_id ), |
---|
814 | $list_item |
---|
815 | ); |
---|
816 | |
---|
817 | $validation_errors[] = $list_item; |
---|
818 | } |
---|
819 | } |
---|
820 | } |
---|
821 | } |
---|
822 | |
---|
823 | $primary_response = sprintf( |
---|
824 | '<p role="status" aria-live="polite" aria-atomic="true">%s</p>', |
---|
825 | esc_html( $primary_response ) |
---|
826 | ); |
---|
827 | |
---|
828 | $validation_errors = sprintf( |
---|
829 | '<ul>%s</ul>', |
---|
830 | implode( "\n", $validation_errors ) |
---|
831 | ); |
---|
832 | |
---|
833 | $output = sprintf( |
---|
834 | '<div class="screen-reader-response">%1$s %2$s</div>', |
---|
835 | $primary_response, |
---|
836 | $validation_errors |
---|
837 | ); |
---|
838 | |
---|
839 | return $output; |
---|
840 | } |
---|
841 | |
---|
842 | |
---|
843 | /** |
---|
844 | * Returns a validation error for the specified input field. |
---|
845 | * |
---|
846 | * @param string $name Input field name. |
---|
847 | */ |
---|
848 | public function validation_error( $name ) { |
---|
849 | $error = ''; |
---|
850 | |
---|
851 | if ( $this->is_posted() ) { |
---|
852 | $submission = WPCF7_Submission::get_instance(); |
---|
853 | |
---|
854 | if ( $invalid_field = $submission->get_invalid_field( $name ) ) { |
---|
855 | $error = trim( $invalid_field['reason'] ); |
---|
856 | } |
---|
857 | } |
---|
858 | |
---|
859 | if ( ! $error ) { |
---|
860 | return $error; |
---|
861 | } |
---|
862 | |
---|
863 | $atts = array( |
---|
864 | 'class' => 'wpcf7-not-valid-tip', |
---|
865 | 'aria-hidden' => 'true', |
---|
866 | ); |
---|
867 | |
---|
868 | $error = sprintf( |
---|
869 | '<span %1$s>%2$s</span>', |
---|
870 | wpcf7_format_atts( $atts ), |
---|
871 | esc_html( $error ) |
---|
872 | ); |
---|
873 | |
---|
874 | return apply_filters( 'wpcf7_validation_error', $error, $name, $this ); |
---|
875 | } |
---|
876 | |
---|
877 | |
---|
878 | /** |
---|
879 | * Replaces all form-tags in the form template with corresponding HTML. |
---|
880 | * |
---|
881 | * @return string Replaced form content. |
---|
882 | */ |
---|
883 | public function replace_all_form_tags() { |
---|
884 | $manager = WPCF7_FormTagsManager::get_instance(); |
---|
885 | $form = $this->prop( 'form' ); |
---|
886 | |
---|
887 | if ( wpcf7_autop_or_not() ) { |
---|
888 | $form = $manager->replace_with_placeholders( $form ); |
---|
889 | $form = wpcf7_autop( $form ); |
---|
890 | $form = $manager->restore_from_placeholders( $form ); |
---|
891 | } |
---|
892 | |
---|
893 | $form = $manager->replace_all( $form ); |
---|
894 | $this->scanned_form_tags = $manager->get_scanned_tags(); |
---|
895 | |
---|
896 | return $form; |
---|
897 | } |
---|
898 | |
---|
899 | |
---|
900 | /** |
---|
901 | * Replaces all form-tags in the form template with corresponding HTML. |
---|
902 | * |
---|
903 | * @deprecated 4.6 Use replace_all_form_tags() |
---|
904 | * |
---|
905 | * @return string Replaced form content. |
---|
906 | */ |
---|
907 | public function form_do_shortcode() { |
---|
908 | wpcf7_deprecated_function( __METHOD__, '4.6', |
---|
909 | 'WPCF7_ContactForm::replace_all_form_tags' |
---|
910 | ); |
---|
911 | |
---|
912 | return $this->replace_all_form_tags(); |
---|
913 | } |
---|
914 | |
---|
915 | |
---|
916 | /** |
---|
917 | * Scans form-tags from the form template. |
---|
918 | * |
---|
919 | * @param string|array|null $cond Optional. Filters. Default null. |
---|
920 | * @return array Form-tags matching the given filter conditions. |
---|
921 | */ |
---|
922 | public function scan_form_tags( $cond = null ) { |
---|
923 | $manager = WPCF7_FormTagsManager::get_instance(); |
---|
924 | |
---|
925 | if ( empty( $this->scanned_form_tags ) ) { |
---|
926 | $this->scanned_form_tags = $manager->scan( $this->prop( 'form' ) ); |
---|
927 | } |
---|
928 | |
---|
929 | $tags = $this->scanned_form_tags; |
---|
930 | |
---|
931 | return $manager->filter( $tags, $cond ); |
---|
932 | } |
---|
933 | |
---|
934 | |
---|
935 | /** |
---|
936 | * Scans form-tags from the form template. |
---|
937 | * |
---|
938 | * @deprecated 4.6 Use scan_form_tags() |
---|
939 | * |
---|
940 | * @param string|array|null $cond Optional. Filters. Default null. |
---|
941 | * @return array Form-tags matching the given filter conditions. |
---|
942 | */ |
---|
943 | public function form_scan_shortcode( $cond = null ) { |
---|
944 | wpcf7_deprecated_function( __METHOD__, '4.6', |
---|
945 | 'WPCF7_ContactForm::scan_form_tags' |
---|
946 | ); |
---|
947 | |
---|
948 | return $this->scan_form_tags( $cond ); |
---|
949 | } |
---|
950 | |
---|
951 | |
---|
952 | /** |
---|
953 | * Replaces all form-tags in the form template with corresponding HTML. |
---|
954 | * |
---|
955 | * @return string Replaced form content. wpcf7_form_elements filters applied. |
---|
956 | */ |
---|
957 | public function form_elements() { |
---|
958 | return apply_filters( 'wpcf7_form_elements', |
---|
959 | $this->replace_all_form_tags() |
---|
960 | ); |
---|
961 | } |
---|
962 | |
---|
963 | |
---|
964 | /** |
---|
965 | * Collects mail-tags available for this contact form. |
---|
966 | * |
---|
967 | * @param string|array $options Optional. Search options. |
---|
968 | * @return array Mail-tag names. |
---|
969 | */ |
---|
970 | public function collect_mail_tags( $options = '' ) { |
---|
971 | $manager = WPCF7_FormTagsManager::get_instance(); |
---|
972 | |
---|
973 | $options = wp_parse_args( $options, array( |
---|
974 | 'include' => array(), |
---|
975 | 'exclude' => $manager->collect_tag_types( 'not-for-mail' ), |
---|
976 | ) ); |
---|
977 | |
---|
978 | $tags = $this->scan_form_tags(); |
---|
979 | $mailtags = array(); |
---|
980 | |
---|
981 | foreach ( (array) $tags as $tag ) { |
---|
982 | $type = $tag->basetype; |
---|
983 | |
---|
984 | if ( empty( $type ) ) { |
---|
985 | continue; |
---|
986 | } elseif ( ! empty( $options['include'] ) ) { |
---|
987 | if ( ! in_array( $type, $options['include'] ) ) { |
---|
988 | continue; |
---|
989 | } |
---|
990 | } elseif ( ! empty( $options['exclude'] ) ) { |
---|
991 | if ( in_array( $type, $options['exclude'] ) ) { |
---|
992 | continue; |
---|
993 | } |
---|
994 | } |
---|
995 | |
---|
996 | $mailtags[] = $tag->name; |
---|
997 | } |
---|
998 | |
---|
999 | $mailtags = array_unique( $mailtags ); |
---|
1000 | $mailtags = array_filter( $mailtags ); |
---|
1001 | $mailtags = array_values( $mailtags ); |
---|
1002 | |
---|
1003 | return apply_filters( 'wpcf7_collect_mail_tags', $mailtags, $options, $this ); |
---|
1004 | } |
---|
1005 | |
---|
1006 | |
---|
1007 | /** |
---|
1008 | * Prints a mail-tag suggestion list. |
---|
1009 | * |
---|
1010 | * @param string $template_name Optional. Mail template name. Default 'mail'. |
---|
1011 | */ |
---|
1012 | public function suggest_mail_tags( $template_name = 'mail' ) { |
---|
1013 | $mail = wp_parse_args( $this->prop( $template_name ), |
---|
1014 | array( |
---|
1015 | 'active' => false, |
---|
1016 | 'recipient' => '', |
---|
1017 | 'sender' => '', |
---|
1018 | 'subject' => '', |
---|
1019 | 'body' => '', |
---|
1020 | 'additional_headers' => '', |
---|
1021 | 'attachments' => '', |
---|
1022 | 'use_html' => false, |
---|
1023 | 'exclude_blank' => false, |
---|
1024 | ) |
---|
1025 | ); |
---|
1026 | |
---|
1027 | $mail = array_filter( $mail ); |
---|
1028 | |
---|
1029 | foreach ( (array) $this->collect_mail_tags() as $mail_tag ) { |
---|
1030 | $pattern = sprintf( |
---|
1031 | '/\[(_[a-z]+_)?%s([ \t]+[^]]+)?\]/', |
---|
1032 | preg_quote( $mail_tag, '/' ) |
---|
1033 | ); |
---|
1034 | |
---|
1035 | $used = preg_grep( $pattern, $mail ); |
---|
1036 | |
---|
1037 | echo sprintf( |
---|
1038 | '<span class="%1$s">[%2$s]</span>', |
---|
1039 | 'mailtag code ' . ( $used ? 'used' : 'unused' ), |
---|
1040 | esc_html( $mail_tag ) |
---|
1041 | ); |
---|
1042 | } |
---|
1043 | } |
---|
1044 | |
---|
1045 | |
---|
1046 | /** |
---|
1047 | * Submits this contact form. |
---|
1048 | * |
---|
1049 | * @param string|array $options Optional. Submission options. Default empty. |
---|
1050 | * @return array Result of submission. |
---|
1051 | */ |
---|
1052 | public function submit( $options = '' ) { |
---|
1053 | $options = wp_parse_args( $options, array( |
---|
1054 | 'skip_mail' => |
---|
1055 | ( $this->in_demo_mode() |
---|
1056 | || $this->is_true( 'skip_mail' ) |
---|
1057 | || ! empty( $this->skip_mail ) ), |
---|
1058 | ) ); |
---|
1059 | |
---|
1060 | if ( $this->is_true( 'subscribers_only' ) |
---|
1061 | and ! current_user_can( 'wpcf7_submit', $this->id() ) ) { |
---|
1062 | $result = array( |
---|
1063 | 'contact_form_id' => $this->id(), |
---|
1064 | 'status' => 'error', |
---|
1065 | 'message' => __( |
---|
1066 | "This contact form is available only for logged in users.", |
---|
1067 | 'contact-form-7' |
---|
1068 | ), |
---|
1069 | ); |
---|
1070 | |
---|
1071 | return $result; |
---|
1072 | } |
---|
1073 | |
---|
1074 | $submission = WPCF7_Submission::get_instance( $this, array( |
---|
1075 | 'skip_mail' => $options['skip_mail'], |
---|
1076 | ) ); |
---|
1077 | |
---|
1078 | $result = array( |
---|
1079 | 'contact_form_id' => $this->id(), |
---|
1080 | ); |
---|
1081 | |
---|
1082 | $result += $submission->get_result(); |
---|
1083 | |
---|
1084 | if ( $this->in_demo_mode() ) { |
---|
1085 | $result['demo_mode'] = true; |
---|
1086 | } |
---|
1087 | |
---|
1088 | do_action( 'wpcf7_submit', $this, $result ); |
---|
1089 | |
---|
1090 | return $result; |
---|
1091 | } |
---|
1092 | |
---|
1093 | |
---|
1094 | /** |
---|
1095 | * Returns message used for given status. |
---|
1096 | * |
---|
1097 | * @param string $status Status. |
---|
1098 | * @param bool $filter Optional. Whether filters are applied. Default true. |
---|
1099 | * @return string Message. |
---|
1100 | */ |
---|
1101 | public function message( $status, $filter = true ) { |
---|
1102 | $messages = $this->prop( 'messages' ); |
---|
1103 | $message = isset( $messages[$status] ) ? $messages[$status] : ''; |
---|
1104 | |
---|
1105 | if ( $filter ) { |
---|
1106 | $message = $this->filter_message( $message, $status ); |
---|
1107 | } |
---|
1108 | |
---|
1109 | return $message; |
---|
1110 | } |
---|
1111 | |
---|
1112 | |
---|
1113 | /** |
---|
1114 | * Filters a message. |
---|
1115 | * |
---|
1116 | * @param string $message Message to filter. |
---|
1117 | * @param string $status Optional. Status. Default empty. |
---|
1118 | * @return string Filtered message. |
---|
1119 | */ |
---|
1120 | public function filter_message( $message, $status = '' ) { |
---|
1121 | $message = wpcf7_mail_replace_tags( $message ); |
---|
1122 | $message = apply_filters( 'wpcf7_display_message', $message, $status ); |
---|
1123 | $message = wp_strip_all_tags( $message ); |
---|
1124 | |
---|
1125 | return $message; |
---|
1126 | } |
---|
1127 | |
---|
1128 | |
---|
1129 | /** |
---|
1130 | * Returns the additional setting value searched by name. |
---|
1131 | * |
---|
1132 | * @param string $name Name of setting. |
---|
1133 | * @return string Additional setting value. |
---|
1134 | */ |
---|
1135 | public function pref( $name ) { |
---|
1136 | $settings = $this->additional_setting( $name ); |
---|
1137 | |
---|
1138 | if ( $settings ) { |
---|
1139 | return $settings[0]; |
---|
1140 | } |
---|
1141 | } |
---|
1142 | |
---|
1143 | |
---|
1144 | /** |
---|
1145 | * Returns additional setting values searched by name. |
---|
1146 | * |
---|
1147 | * @param string $name Name of setting. |
---|
1148 | * @param int $max Maximum result item count. |
---|
1149 | * @return array Additional setting values. |
---|
1150 | */ |
---|
1151 | public function additional_setting( $name, $max = 1 ) { |
---|
1152 | $settings = (array) explode( "\n", $this->prop( 'additional_settings' ) ); |
---|
1153 | |
---|
1154 | $pattern = '/^([a-zA-Z0-9_]+)[\t ]*:(.*)$/'; |
---|
1155 | $count = 0; |
---|
1156 | $values = array(); |
---|
1157 | |
---|
1158 | foreach ( $settings as $setting ) { |
---|
1159 | if ( preg_match( $pattern, $setting, $matches ) ) { |
---|
1160 | if ( $matches[1] != $name ) { |
---|
1161 | continue; |
---|
1162 | } |
---|
1163 | |
---|
1164 | if ( ! $max or $count < (int) $max ) { |
---|
1165 | $values[] = trim( $matches[2] ); |
---|
1166 | $count += 1; |
---|
1167 | } |
---|
1168 | } |
---|
1169 | } |
---|
1170 | |
---|
1171 | return $values; |
---|
1172 | } |
---|
1173 | |
---|
1174 | |
---|
1175 | /** |
---|
1176 | * Returns true if the specified setting has a truthy string value. |
---|
1177 | * |
---|
1178 | * @param string $name Name of setting. |
---|
1179 | * @return bool True if the setting value is 'on', 'true', or '1'. |
---|
1180 | */ |
---|
1181 | public function is_true( $name ) { |
---|
1182 | return in_array( |
---|
1183 | $this->pref( $name ), |
---|
1184 | array( 'on', 'true', '1' ), |
---|
1185 | true |
---|
1186 | ); |
---|
1187 | } |
---|
1188 | |
---|
1189 | |
---|
1190 | /** |
---|
1191 | * Returns true if this contact form is in the demo mode. |
---|
1192 | */ |
---|
1193 | public function in_demo_mode() { |
---|
1194 | return $this->is_true( 'demo_mode' ); |
---|
1195 | } |
---|
1196 | |
---|
1197 | |
---|
1198 | /** |
---|
1199 | * Returns true if nonce is active for this contact form. |
---|
1200 | */ |
---|
1201 | public function nonce_is_active() { |
---|
1202 | $is_active = WPCF7_VERIFY_NONCE; |
---|
1203 | |
---|
1204 | if ( $this->is_true( 'subscribers_only' ) ) { |
---|
1205 | $is_active = true; |
---|
1206 | } |
---|
1207 | |
---|
1208 | return (bool) apply_filters( 'wpcf7_verify_nonce', $is_active, $this ); |
---|
1209 | } |
---|
1210 | |
---|
1211 | |
---|
1212 | /** |
---|
1213 | * Returns true if the specified setting has a falsey string value. |
---|
1214 | * |
---|
1215 | * @param string $name Name of setting. |
---|
1216 | * @return bool True if the setting value is 'off', 'false', or '0'. |
---|
1217 | */ |
---|
1218 | public function is_false( $name ) { |
---|
1219 | return in_array( |
---|
1220 | $this->pref( $name ), |
---|
1221 | array( 'off', 'false', '0' ), |
---|
1222 | true |
---|
1223 | ); |
---|
1224 | } |
---|
1225 | |
---|
1226 | |
---|
1227 | /** |
---|
1228 | * Upgrades this contact form properties. |
---|
1229 | */ |
---|
1230 | private function upgrade() { |
---|
1231 | $mail = $this->prop( 'mail' ); |
---|
1232 | |
---|
1233 | if ( is_array( $mail ) |
---|
1234 | and ! isset( $mail['recipient'] ) ) { |
---|
1235 | $mail['recipient'] = get_option( 'admin_email' ); |
---|
1236 | } |
---|
1237 | |
---|
1238 | $this->properties['mail'] = $mail; |
---|
1239 | |
---|
1240 | $messages = $this->prop( 'messages' ); |
---|
1241 | |
---|
1242 | if ( is_array( $messages ) ) { |
---|
1243 | foreach ( wpcf7_messages() as $key => $arr ) { |
---|
1244 | if ( ! isset( $messages[$key] ) ) { |
---|
1245 | $messages[$key] = $arr['default']; |
---|
1246 | } |
---|
1247 | } |
---|
1248 | } |
---|
1249 | |
---|
1250 | $this->properties['messages'] = $messages; |
---|
1251 | } |
---|
1252 | |
---|
1253 | |
---|
1254 | /** |
---|
1255 | * Stores this contact form properties to the database. |
---|
1256 | * |
---|
1257 | * @return int The post ID on success. The value 0 on failure. |
---|
1258 | */ |
---|
1259 | public function save() { |
---|
1260 | $title = wp_slash( $this->title ); |
---|
1261 | $props = wp_slash( $this->get_properties() ); |
---|
1262 | |
---|
1263 | $post_content = implode( "\n", wpcf7_array_flatten( $props ) ); |
---|
1264 | |
---|
1265 | if ( $this->initial() ) { |
---|
1266 | $post_id = wp_insert_post( array( |
---|
1267 | 'post_type' => self::post_type, |
---|
1268 | 'post_status' => 'publish', |
---|
1269 | 'post_title' => $title, |
---|
1270 | 'post_content' => trim( $post_content ), |
---|
1271 | ) ); |
---|
1272 | } else { |
---|
1273 | $post_id = wp_update_post( array( |
---|
1274 | 'ID' => (int) $this->id, |
---|
1275 | 'post_status' => 'publish', |
---|
1276 | 'post_title' => $title, |
---|
1277 | 'post_content' => trim( $post_content ), |
---|
1278 | ) ); |
---|
1279 | } |
---|
1280 | |
---|
1281 | if ( $post_id ) { |
---|
1282 | foreach ( $props as $prop => $value ) { |
---|
1283 | update_post_meta( $post_id, '_' . $prop, |
---|
1284 | wpcf7_normalize_newline_deep( $value ) |
---|
1285 | ); |
---|
1286 | } |
---|
1287 | |
---|
1288 | if ( wpcf7_is_valid_locale( $this->locale ) ) { |
---|
1289 | update_post_meta( $post_id, '_locale', $this->locale ); |
---|
1290 | } |
---|
1291 | |
---|
1292 | add_post_meta( $post_id, '_hash', |
---|
1293 | wpcf7_generate_contact_form_hash( $post_id ), |
---|
1294 | true // Unique |
---|
1295 | ); |
---|
1296 | |
---|
1297 | if ( $this->initial() ) { |
---|
1298 | $this->id = $post_id; |
---|
1299 | do_action( 'wpcf7_after_create', $this ); |
---|
1300 | } else { |
---|
1301 | do_action( 'wpcf7_after_update', $this ); |
---|
1302 | } |
---|
1303 | |
---|
1304 | do_action( 'wpcf7_after_save', $this ); |
---|
1305 | } |
---|
1306 | |
---|
1307 | return $post_id; |
---|
1308 | } |
---|
1309 | |
---|
1310 | |
---|
1311 | /** |
---|
1312 | * Makes a copy of this contact form. |
---|
1313 | * |
---|
1314 | * @return WPCF7_ContactForm New contact form object. |
---|
1315 | */ |
---|
1316 | public function copy() { |
---|
1317 | $new = new self; |
---|
1318 | $new->title = $this->title . '_copy'; |
---|
1319 | $new->locale = $this->locale; |
---|
1320 | $new->properties = $this->properties; |
---|
1321 | |
---|
1322 | return apply_filters( 'wpcf7_copy', $new, $this ); |
---|
1323 | } |
---|
1324 | |
---|
1325 | |
---|
1326 | /** |
---|
1327 | * Deletes this contact form. |
---|
1328 | */ |
---|
1329 | public function delete() { |
---|
1330 | if ( $this->initial() ) { |
---|
1331 | return; |
---|
1332 | } |
---|
1333 | |
---|
1334 | if ( wp_delete_post( $this->id, true ) ) { |
---|
1335 | $this->id = 0; |
---|
1336 | return true; |
---|
1337 | } |
---|
1338 | |
---|
1339 | return false; |
---|
1340 | } |
---|
1341 | |
---|
1342 | |
---|
1343 | /** |
---|
1344 | * Returns a WordPress shortcode for this contact form. |
---|
1345 | */ |
---|
1346 | public function shortcode( $options = '' ) { |
---|
1347 | $options = wp_parse_args( $options, array( |
---|
1348 | 'use_old_format' => false |
---|
1349 | ) ); |
---|
1350 | |
---|
1351 | $title = str_replace( array( '"', '[', ']' ), '', $this->title ); |
---|
1352 | |
---|
1353 | if ( $options['use_old_format'] ) { |
---|
1354 | $old_unit_id = (int) get_post_meta( $this->id, '_old_cf7_unit_id', true ); |
---|
1355 | |
---|
1356 | if ( $old_unit_id ) { |
---|
1357 | $shortcode = sprintf( |
---|
1358 | '[contact-form %1$d "%2$s"]', |
---|
1359 | $old_unit_id, |
---|
1360 | $title |
---|
1361 | ); |
---|
1362 | } else { |
---|
1363 | $shortcode = ''; |
---|
1364 | } |
---|
1365 | } else { |
---|
1366 | $shortcode = sprintf( |
---|
1367 | '[contact-form-7 id="%1$s" title="%2$s"]', |
---|
1368 | $this->hash(), |
---|
1369 | $title |
---|
1370 | ); |
---|
1371 | } |
---|
1372 | |
---|
1373 | return apply_filters( 'wpcf7_contact_form_shortcode', |
---|
1374 | $shortcode, $options, $this |
---|
1375 | ); |
---|
1376 | } |
---|
1377 | } |
---|