Skip to content

Commit e536da6

Browse files
author
Antti Kuosmanen
committed
Add form specific hooks with form slugs and ids
Fixes #52
1 parent 2934316 commit e536da6

2 files changed

Lines changed: 38 additions & 8 deletions

File tree

inc/wplf-ajax.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ function wplf_ajax_submit_handler() {
2020
// form existence has already been validated via filters
2121
$form = get_post( intval( $_POST['_form_id'] ) );
2222

23+
// form-specific validation
24+
$return->slug = $form->post_name;
25+
$return = apply_filters( "wplf_{$form->post_name}_validate_submission", $return );
26+
$return = apply_filters( "wplf_{$form->ID}_validate_submission", $return );
27+
}
28+
29+
if ( $return->ok ) {
2330
// the title is the value of whatever the first field was in the form
2431
$title_format = get_post_meta( $form->ID, '_wplf_title_format', true );
2532

@@ -77,6 +84,8 @@ function wplf_ajax_submit_handler() {
7784
// allow user to attach custom actions after the submission has been received
7885
// these could be confirmation emails, additional processing for the submission fields, e.g.
7986
do_action( 'wplf_post_validate_submission', $return );
87+
do_action( "wplf_{$form->post_name}_post_validate_submission", $return );
88+
do_action( "wplf_{$form->ID}_post_validate_submission", $return );
8089
}
8190

8291
// respond with json

inc/wplf-form-actions.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ function wplf_send_email_copy( $return, $submission_id = null ) {
1010
$form_id = intval( ( isset( $submission_id ) ) ?
1111
get_post_meta( $submission_id, '_form_id', true ) : $_POST['_form_id'] );
1212

13-
$form_title = esc_html( get_the_title( $form_id ) );
13+
$form = get_post( intval( $form_id ) );
14+
15+
$form_title = esc_html( get_the_title( $form ) );
1416
$form_meta = get_post_meta( $form_id );
1517

1618
$referrer = esc_url_raw( ( isset( $submission_id ) ) ?
@@ -56,12 +58,31 @@ function wplf_send_email_copy( $return, $submission_id = null ) {
5658
// @codingStandardsIgnoreEnd
5759
}
5860

59-
wp_mail(
60-
apply_filters( 'wplf_email_copy_to', $to ),
61-
apply_filters( 'wplf_email_copy_subject', $subject ),
62-
apply_filters( 'wplf_email_copy_content', $content ),
63-
apply_filters( 'wplf_email_copy_headers', '' ),
64-
apply_filters( 'wplf_email_copy_attachments', array() )
65-
);
61+
// default pre-filtered values for email headers and attachments
62+
$headers = '';
63+
$attachments = array();
64+
65+
// allow filtering email fields
66+
$to = apply_filters( 'wplf_email_copy_to', $to );
67+
$subject = apply_filters( 'wplf_email_copy_subject', $subject );
68+
$content = apply_filters( 'wplf_email_copy_content', $content );
69+
$headers = apply_filters( 'wplf_email_copy_headers', $headers );
70+
$attachments = apply_filters( 'wplf_email_copy_attachments', $attachments );
71+
72+
// form slug specific filters
73+
$to = apply_filters( "wplf_{$form->post_name}_email_copy_to", $to );
74+
$subject = apply_filters( "wplf_{$form->post_name}_email_copy_subject", $subject );
75+
$content = apply_filters( "wplf_{$form->post_name}_email_copy_content", $content );
76+
$headers = apply_filters( "wplf_{$form->post_name}_email_copy_headers", $headers );
77+
$attachments = apply_filters( "wplf_{$form->post_name}_email_copy_attachments", $attachment );
78+
79+
// form ID specific filters
80+
$to = apply_filters( "wplf_{$form->ID}_email_copy_to", $to );
81+
$subject = apply_filters( "wplf_{$form->ID}_email_copy_subject", $subject );
82+
$content = apply_filters( "wplf_{$form->ID}_email_copy_content", $content );
83+
$headers = apply_filters( "wplf_{$form->ID}_email_copy_headers", $headers );
84+
$attachments = apply_filters( "wplf_{$form->ID}_email_copy_attachments", $attachment );
85+
86+
wp_mail( $to, $subject, $content, $headers, $attachments );
6687
}
6788
}

0 commit comments

Comments
 (0)