@@ -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+ }
0 commit comments