Skip to content

Commit 7726a4b

Browse files
timiwahalahtianttiviljami
authored andcommitted
Better emails (#74)
first version of metabox, template tag handling and mail manipulatio
1 parent c8658d5 commit 7726a4b

6 files changed

Lines changed: 281 additions & 64 deletions

File tree

assets/scripts/wplf-admin-form.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ $(document).ready(function() {
5656
// display email copy field if the feature is enabled
5757
function toggleEmailCopy() {
5858
if( $('input[name="wplf_email_copy_enabled"]:checked').length > 0 ) {
59-
$('input[name="wplf_email_copy_to"]').show();
59+
$('p.wplf-email-copy-to-field').show();
6060
} else {
61-
$('input[name="wplf_email_copy_to"]').hide();
61+
$('p.wplf-email-copy-to-field').hide();
6262
}
6363
}
6464
$('input[name="wplf_email_copy_enabled"]').change(toggleEmailCopy);

classes/class-cpt-wplf-form.php

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ function add_meta_boxes_cpt() {
283283
__( 'Emails', 'wp-libre-form' ),
284284
array( $this, 'metabox_submit_email' ),
285285
'wplf-form',
286-
'side'
286+
'normal',
287+
'high'
287288
);
288289

289290
// Submission title format meta box
@@ -351,6 +352,16 @@ function metabox_submit_email( $post ) {
351352
$meta = get_post_meta( $post->ID );
352353
$email_enabled = isset( $meta['_wplf_email_copy_enabled'] ) ? $meta['_wplf_email_copy_enabled'][0] : true;
353354
$email_copy_to = isset( $meta['_wplf_email_copy_to'] ) ? $meta['_wplf_email_copy_to'][0] : '';
355+
$email_copy_from = isset( $meta['_wplf_email_copy_from'] ) ? $meta['_wplf_email_copy_from'][0] : '';
356+
$email_copy_from_address = isset( $meta['_wplf_email_copy_from_address'] ) ? $meta['_wplf_email_copy_from_address'][0] : '';
357+
$email_copy_subject = isset( $meta['_wplf_email_copy_subject'] ) ? $meta['_wplf_email_copy_subject'][0] : '';
358+
$email_copy_content = isset( $meta['_wplf_email_copy_content'] ) ? $meta['_wplf_email_copy_content'][0] : '';
359+
360+
$sitename = strtolower( $_SERVER['SERVER_NAME'] );
361+
if ( substr( $sitename, 0, 4 ) == 'www.' ) {
362+
$sitename = substr( $sitename, 4 );
363+
}
364+
$email_copy_from_default = 'wordpress@' . $sitename;
354365
?>
355366
<p>
356367
<label for="wplf_email_copy_enabled">
@@ -363,15 +374,64 @@ function metabox_submit_email( $post ) {
363374
<?php esc_html_e( 'Send an email copy when a form is submitted?', 'wp-libre-form' ); ?>
364375
</label>
365376
</p>
366-
<p>
377+
<p class="wplf-email-copy-to-field">
378+
<?php esc_attr_e( 'You may use any form field values and following global tags: submission-id, referrer, form-title, form-id, user-id, timestamp, datetime, language, all-form-data. All field values and tags should be enclosed in "%" markers.', 'wp-libre-form' ); ?>
379+
</p>
380+
<p class="wplf-email-copy-to-field">
381+
<label for="wplf_email_copy_to" style="display:inline-block;width:100px;font-weight:600;"><?php esc_attr_e( 'Send copy to', 'wp-libre-form' ); ?></label>
367382
<input
368383
type="text"
369384
name="wplf_email_copy_to"
370385
value="<?php echo esc_attr( $email_copy_to ); ?>"
371386
placeholder="<?php echo esc_attr( get_option( 'admin_email' ) ); ?>"
372-
style="width:100%;display:none"
387+
style="width:80%;"
388+
>
389+
</p>
390+
<p class="wplf-email-copy-to-field">
391+
<label for="wplf_email_copy_from" style="display:inline-block;width:100px;font-weight:600;"><?php esc_attr_e( 'Sender name', 'wp-libre-form' ); ?></label>
392+
<input
393+
type="text"
394+
name="wplf_email_copy_from"
395+
value="<?php echo esc_attr( $email_copy_from ); ?>"
396+
placeholder="WordPress"
397+
style="width:80%;"
398+
>
399+
</p>
400+
<p class="wplf-email-copy-to-field">
401+
<label for="wplf_email_copy_from_address" style="display:inline-block;width:100px;font-weight:600;"><?php esc_attr_e( 'Sender email', 'wp-libre-form' ); ?></label>
402+
<input
403+
type="text"
404+
name="wplf_email_copy_from_address"
405+
value="<?php echo esc_attr( $email_copy_from_address ); ?>"po
406+
placeholder="<?php echo esc_attr( $email_copy_from_default ); ?>"
407+
style="width:80%;"
373408
>
374409
</p>
410+
<p class="wplf-email-copy-to-field">
411+
<label for="wplf_email_copy_subject" style="display:inline-block;width:100px;font-weight:600;"><?php esc_attr_e( 'Subject', 'wp-libre-form' ); ?></label>
412+
<?php // @codingStandardsIgnoreStart ?>
413+
<input
414+
type="text"
415+
name="wplf_email_copy_subject"
416+
value="<?php echo esc_attr( $email_copy_subject ); ?>"
417+
placeholder="<?php esc_attr_e( '[%submission-id%] Submission from %referrer%', 'wp-libre-form' ); ?>"
418+
style="width:80%;"
419+
>
420+
<?php // @codingStandardsIgnoreEnd ?>
421+
</p>
422+
<p class="wplf-email-copy-to-field" style="display:table;width:100%;">
423+
<label for="wplf_email_copy_content" style="display:table-cell;width:105px;font-weight:600;vertical-align:top;"><?php esc_attr_e( 'Content', 'wp-libre-form' ); ?></label>
424+
<?php // @codingStandardsIgnoreStart ?>
425+
<textarea
426+
name="wplf_email_copy_content"
427+
placeholder="<?php esc_attr_e( 'Form %form-title% (ID %form-id%) was submitted with values below', 'wp-libre-form' ); ?>:
428+
429+
%all-form-data%"
430+
style="display:table-cell;width:94%;"
431+
rows="10"
432+
><?php echo esc_attr( $email_copy_content ); ?></textarea>
433+
<?php // @codingStandardsIgnoreEnd ?>
434+
</p>
375435
<?php
376436
}
377437

@@ -471,6 +531,33 @@ function save_cpt( $post_id ) {
471531
}
472532
}
473533

534+
// save email copy from
535+
if ( isset( $_POST['wplf_email_copy_from'] ) && ! empty( $_POST['wplf_email_copy_from'] ) ) {
536+
update_post_meta( $post_id, '_wplf_email_copy_from', sanitize_text_field( $_POST['wplf_email_copy_from'] ) );
537+
} else {
538+
delete_post_meta( $post_id, '_wplf_email_copy_from' );
539+
}
540+
541+
if ( isset( $_POST['wplf_email_copy_from_address'] ) && ! empty( $_POST['wplf_email_copy_from_address'] ) ) {
542+
update_post_meta( $post_id, '_wplf_email_copy_from_address', sanitize_text_field( $_POST['wplf_email_copy_from_address'] ) );
543+
} else {
544+
delete_post_meta( $post_id, '_wplf_email_copy_from_address' );
545+
}
546+
547+
// save email copy subject
548+
if ( isset( $_POST['wplf_email_copy_subject'] ) && ! empty( $_POST['wplf_email_copy_subject'] ) ) {
549+
update_post_meta( $post_id, '_wplf_email_copy_subject', sanitize_text_field( $_POST['wplf_email_copy_subject'] ) );
550+
} else {
551+
delete_post_meta( $post_id, '_wplf_email_copy_subject' );
552+
}
553+
554+
// save email copy content
555+
if ( isset( $_POST['wplf_email_copy_content'] ) && ! empty( $_POST['wplf_email_copy_content'] ) ) {
556+
update_post_meta( $post_id, '_wplf_email_copy_content', wp_kses_post( $_POST['wplf_email_copy_content'] ) );
557+
} else {
558+
delete_post_meta( $post_id, '_wplf_email_copy_content' );
559+
}
560+
474561
// save title format
475562
if ( isset( $_POST['wplf_title_format'] ) ) {
476563
$safe_title_format = $_POST['wplf_title_format']; // TODO: are there any applicable sanitize functions?

inc/wplf-ajax.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function wplf_ajax_submit_handler() {
5454
if ( ! is_array( $value ) ) {
5555
add_post_meta( $post_id, $key, esc_html( $value ), true );
5656
} else {
57-
add_post_meta( $post_id, $key, esc_html( wp_json_encode( $value ) ), true );
57+
add_post_meta( $post_id, $key, $value, true );
5858
}
5959
}
6060

inc/wplf-form-actions.php

Lines changed: 110 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,58 @@ function wplf_send_email_copy( $return, $submission_id = null ) {
77
}
88

99
// _form_id is already validated and we know it exists by this point
10-
$form_id = intval( ( isset( $submission_id ) ) ?
11-
get_post_meta( $submission_id, '_form_id', true ) : $_POST['_form_id'] );
10+
$form_id = intval( ( isset( $submission_id ) ) ? get_post_meta( $submission_id, '_form_id', true ) : $_POST['_form_id'] );
1211

1312
$form = get_post( intval( $form_id ) );
1413

1514
$form_title = esc_html( get_the_title( $form ) );
1615
$form_meta = get_post_meta( $form_id );
1716

18-
$referrer = esc_url_raw( ( isset( $submission_id ) ) ?
19-
get_post_meta( $submission_id, 'referrer', true ) : $_POST['referrer'] );
17+
$referrer = esc_url_raw( ( isset( $submission_id ) ) ? get_post_meta( $submission_id, 'referrer', true ) : $_POST['referrer'] );
2018

21-
if ( ( isset( $form_meta['_wplf_email_copy_enabled'] ) && $form_meta['_wplf_email_copy_enabled'][0] )
22-
|| isset( $submission_id ) ) {
19+
if ( ( isset( $form_meta['_wplf_email_copy_enabled'] ) && $form_meta['_wplf_email_copy_enabled'][0] ) || isset( $submission_id ) ) {
2320

24-
$to = isset( $form_meta['_wplf_email_copy_to'] ) ?
25-
$form_meta['_wplf_email_copy_to'][0] : get_option( 'admin_email' );
21+
$to = isset( $form_meta['_wplf_email_copy_to'] ) ? $form_meta['_wplf_email_copy_to'][0] : get_option( 'admin_email' );
2622

27-
// translators: %1$s is submission ID, %2$s is URL where form was submitted
28-
$subject = wp_sprintf( __( '[%1$s] New submission to %2$s', 'wp-libre-form' ), $submission_id, $referrer );
29-
30-
if ( isset( $submission_id ) ) {
31-
$to = get_post_meta( $submission_id, '_wplf_email_copy_to', true );
32-
// translators: %1$s is submission ID, %2$s is URL where form was submitted
33-
$subject = wp_sprintf( __( '[%1$s] Submission from %2$s', 'wp-libre-form' ), $submission_id, $referrer );
23+
// translators: %submission-id% is replaced with submission id and %referrer% with referrer url
24+
$subject = __( '[%submission-id%] New submission from %referrer%', 'wp-libre-form' );
25+
if ( isset( $form_meta['_wplf_email_copy_subject'] ) ) {
26+
$subject = $form_meta['_wplf_email_copy_subject'][0];
3427
}
3528

3629
$to = empty( $to ) ? get_option( 'admin_email' ) : $to;
37-
$content = wp_sprintf(
38-
// translators: %1$s is form title, %2$d is form ID
39-
__( 'Form "%1$s" (ID %2$d) was submitted with values below: ', 'wp-libre-form' ), $form_title, $form_id );
30+
31+
// translators: %form-title% is replaced with form title and %form-id% with form id
32+
// @codingStandardsIgnoreStart
33+
// %f gets detected as a placeholder for wp_sprintf
34+
$content = __( 'Form %form-title% (ID %form-id%) was submitted with values below: ', 'wp-libre-form' );
35+
// @codingStandardsIgnoreEnd
4036
$content = apply_filters( 'wplf_email_copy_content_start', $content, $form_title, $form_id ) . "\n\n";
4137

4238
$fields = $_POST;
4339
if ( isset( $submission_id ) ) {
4440
$fields = get_post_meta( $submission_id );
4541
}
4642

47-
foreach ( $fields as $key => $value ) {
48-
if ( '_' === $key[0] ) {
49-
continue;
50-
}
51-
if ( is_array( $value ) ) { // in case input type="radio" submits an array
52-
$value = implode( ', ', $value );
53-
}
54-
// @codingStandardsIgnoreStart
55-
// WP coding standards don't like print_r
56-
// @TODO: come up with a prettier format for default mail output
57-
$content .= esc_html( $key ) . ': ' . esc_html( print_r( $value, true ) ) . "\n";
58-
// @codingStandardsIgnoreEnd
59-
}
43+
$content .= wplf_email_copy_make_fields_key_value_list( $fields, $form->ID, $form->post_name );
6044

6145
// default pre-filtered values for email headers and attachments
6246
$headers = '';
6347
$attachments = array();
6448

49+
if ( isset( $form_meta['_wplf_email_copy_from'][0] ) ) {
50+
$headers[] = 'From: ' . wplf_email_copy_replace_tags( $form_meta['_wplf_email_copy_from'][0], $form, $submission_id ) . '<' . wplf_email_copy_replace_tags( $form_meta['_wplf_email_copy_from_address'][0], $form, $submission_id ) . '>';
51+
}
52+
53+
if ( isset( $form_meta['_wplf_email_copy_content'] ) ) {
54+
$content = $form_meta['_wplf_email_copy_content'][0];
55+
}
56+
57+
// maybe replace template tags with real content
58+
$to = wplf_email_copy_replace_tags( $to, $form, $submission_id );
59+
$subject = wplf_email_copy_replace_tags( $subject, $form, $submission_id );
60+
$content = wplf_email_copy_replace_tags( $content, $form, $submission_id );
61+
6562
// allow filtering email fields
6663
$to = apply_filters( 'wplf_email_copy_to', $to );
6764
$subject = apply_filters( 'wplf_email_copy_subject', $subject );
@@ -86,3 +83,85 @@ function wplf_send_email_copy( $return, $submission_id = null ) {
8683
wp_mail( $to, $subject, $content, $headers, $attachments );
8784
}
8885
}
86+
87+
function wplf_email_copy_make_fields_key_value_list( $fields, $form_id = 0, $form_name = '' ) {
88+
$list = '';
89+
90+
foreach ( $fields as $key => $value ) {
91+
if ( '_' === $key[0] ) {
92+
continue;
93+
}
94+
95+
$value = $value[0];
96+
$value = wplf_email_maybe_implode_serialized_value( $value, $form_id, $form_name );
97+
98+
// @codingStandardsIgnoreStart
99+
// WP coding standards don't like print_r
100+
// @TODO: come up with a prettier format for default mail output
101+
$list .= esc_html( $key ) . ': ' . esc_html( print_r( $value, true ) ) . "\n";
102+
// @codingStandardsIgnoreEnd
103+
}
104+
105+
return $list;
106+
}
107+
108+
function wplf_email_copy_replace_tags( $content, $form = null, $submission_id = null ) {
109+
if ( ! $form || ! $submission_id ) {
110+
return $content;
111+
}
112+
113+
$fields = $_POST;
114+
if ( isset( $submission_id ) ) {
115+
$fields = get_post_meta( $submission_id );
116+
}
117+
118+
$fields_key_value = wplf_email_copy_make_fields_key_value_list( $fields, $form->ID, $form->post_name );
119+
120+
$defaults_store = array(
121+
'submission-id' => $submission_id,
122+
'referrer' => esc_url_raw( ( null !== $submission_id ) ? get_post_meta( $submission_id, 'referrer', true ) : $_POST['referrer'] ),
123+
'form-title' => esc_html( get_the_title( $form ) ),
124+
'form-id' => $form->ID,
125+
'user-id' => ( null !== get_current_user_id() ) ? wp_get_current_user()->display_name . ' (ID ' . get_current_user_id() . ')' : __( 'No user logged in', 'wp-libre-form' ),
126+
'timestamp' => current_time( 'mysql' ),
127+
'datetime' => current_time( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ),
128+
'language' => ( function_exists( 'pll_current_language' ) ) ? pll_current_language( 'locale' ) : get_locale(),
129+
'all-form-data' => $fields_key_value,
130+
);
131+
132+
$fields = $_POST;
133+
if ( null !== $submission_id ) {
134+
$fields = get_post_meta( $submission_id );
135+
}
136+
137+
preg_match_all( '/%(.+?)%/', $content, $matches );
138+
foreach ( $matches[0] as $match ) {
139+
// match contains the braces, get rid of them.
140+
$string = trim( str_replace( array( '%' ), array( '' ), $match ) );
141+
142+
if ( isset( $fields[ $string ] ) ) {
143+
$value = $fields[ $string ][0];
144+
} else if ( isset( $defaults_store[ $string ] ) ) {
145+
$value = $defaults_store[ $string ];
146+
}
147+
148+
$value = wplf_email_maybe_implode_serialized_value( $value, $form->ID, $form->post_name );
149+
$content = str_replace( $match, $value, $content );
150+
}
151+
152+
return $content;
153+
}
154+
155+
function wplf_email_maybe_implode_serialized_value( $value, $form_id = 0, $form_name = '' ) {
156+
$value = maybe_unserialize( $value );
157+
158+
if ( is_array( $value ) ) {
159+
$implode_glue = apply_filters( 'wplf_email_array_field_implode_glue', ', ' );
160+
$implode_glue = apply_filters( "wplf_{$form->post_name}_email_array_field_implode_glue", $implode_glue );
161+
$implode_glue = apply_filters( "wplf_{$form->ID}_email_array_field_implode_glue", $implode_glue );
162+
163+
$value = implode( $implode_glue, $value );
164+
}
165+
166+
return $value;
167+
}

lang/wp-libre-form-fi.mo

1.04 KB
Binary file not shown.

0 commit comments

Comments
 (0)