Skip to content

Commit 658e6ae

Browse files
timiwahalahtianttiviljami
authored andcommitted
Bulk action to resend submission email copies
1 parent 317afc9 commit 658e6ae

3 files changed

Lines changed: 65 additions & 13 deletions

File tree

classes/class-cpt-wplf-submission.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ private function __construct() {
3030
add_action( 'manage_posts_custom_column' , array( $this, 'custom_columns_display_cpt' ), 10, 2 );
3131
add_action( 'restrict_manage_posts', array( $this, 'form_filter_dropdown' ) );
3232
add_filter( 'pre_get_posts', array( $this, 'filter_by_form' ) );
33+
34+
// add custom bulk actions
35+
add_filter( 'bulk_actions-edit-wplf-submission', array( $this, 'register_wplf_submission_bulk_actions' ) );
36+
add_filter( 'handle_bulk_actions-edit-wplf-submission', array( $this, 'wplf_submission_bulk_action_handler' ), 10, 3 );
37+
add_action( 'admin_notices', array( $this, 'wplf_submission_bulk_action_admin_notice' ) );
3338
}
3439

3540
public static function register_cpt() {
@@ -162,6 +167,39 @@ function filter_by_form( $query ) {
162167
return $query;
163168
}
164169

170+
function register_wplf_submission_bulk_actions( $bulk_actions ) {
171+
$bulk_actions['wplf_resend_copy'] = __('Resend email copy', 'wp-libre-form');
172+
return $bulk_actions;
173+
}
174+
175+
function wplf_submission_bulk_action_handler( $redirect_to, $doaction, $post_ids ) {
176+
if( $doaction !== 'wplf_resend_copy' ) {
177+
return $redirect_to;
178+
}
179+
180+
foreach( $post_ids as $post_id ) {
181+
$return = new stdClass();
182+
$return->ok = 1;
183+
184+
wplf_send_email_copy( $return, $post_id );
185+
}
186+
187+
$redirect_to = add_query_arg( 'wplf_resent', count( $post_ids ), $redirect_to );
188+
return $redirect_to;
189+
}
190+
191+
function wplf_submission_bulk_action_admin_notice() {
192+
if( !empty( $_REQUEST['wplf_resent'] ) ) {
193+
$count = intval( $_REQUEST['wplf_resent'] );
194+
printf( '<div id="wplf-submission-bulk-resend-message" class="notice notice-success"><p>' .
195+
_n( 'Resent email copy of %s submission.',
196+
'Resent email copy of %s submissions.',
197+
$count,
198+
'wp-libre-form'
199+
) . '</p></div>', $count );
200+
}
201+
}
202+
165203
/**
166204
* Add meta box to show fields in form
167205
*/

inc/wplf-ajax.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ function wplf_ajax_submit_handler() {
1919
if( $return->ok ) {
2020
// form existence has already been validated via filters
2121

22-
2322
$form = get_post( intval( $_POST['_form_id'] ) );
2423

2524
// the title is the value of whatever the first field was in the form
@@ -46,11 +45,11 @@ function wplf_ajax_submit_handler() {
4645

4746
// add submission data as meta values
4847
foreach( $_POST as $key => $value ) {
49-
if( !is_array($value) ) {
50-
add_post_meta($post_id, $key, esc_html( $value ), true);
48+
if( !is_array( $value ) ) {
49+
add_post_meta( $post_id, $key, esc_html( $value ), true );
5150
}
5251
else {
53-
add_post_meta($post_id, $key, esc_html( json_encode( $value ) ), true);
52+
add_post_meta( $post_id, $key, esc_html( json_encode( $value ) ), true );
5453
}
5554
}
5655

@@ -63,7 +62,10 @@ function wplf_ajax_submit_handler() {
6362
add_post_meta( $post_id, $key . "_attachment", $attach_id );
6463
}
6564

66-
65+
// save email copy address to submission meta for later use
66+
$to = get_post_meta( $form->ID, '_wplf_email_copy_to', true );
67+
$to = !empty( $to ) ? $to : get_option( 'admin_email' );
68+
add_post_meta( $post_id, '_wplf_email_copy_to', apply_filters( 'wplf_email_copy_to', $to ) );
6769

6870
$return->submission_id = $post_id;
6971
$return->submission_title = $post_title;
@@ -74,8 +76,7 @@ function wplf_ajax_submit_handler() {
7476

7577
// allow user to attach custom actions after the submission has been received
7678
// these could be confirmation emails, additional processing for the submission fields, e.g.
77-
do_action('wplf_post_validate_submission', $return);
78-
79+
do_action( 'wplf_post_validate_submission', $return );
7980
}
8081

8182
// respond with json

inc/wplf-form-actions.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,37 @@
44
* Send a copy of the form fields email if feature is enabled
55
*/
66
add_action( 'wplf_post_validate_submission', 'wplf_send_email_copy', 20 );
7-
function wplf_send_email_copy( $return ) {
7+
function wplf_send_email_copy( $return, $submission_id = null ) {
88
// do nothing if form validation failed
99
if( ! $return->ok ) {
1010
return;
1111
}
1212

13-
$form_id = intval( $_POST['_form_id'] ); // _form_id is already validated and we know it exists by this point
13+
$form_id = intval( ( isset( $submission_id ) ) ? get_post_meta( $submission_id, '_form_id', true ) : $_POST['_form_id'] ); // _form_id is already validated and we know it exists by this point
1414
$form_title = esc_html( get_the_title( $form_id ) );
1515
$form_meta = get_post_meta( $form_id );
16-
$referrer = esc_url_raw( $_POST['referrer'] );
16+
$referrer = esc_url_raw( ( isset( $submission_id ) ) ? get_post_meta( $submission_id, 'referrer', true ) : $_POST['referrer'] );
1717

18-
if( isset($form_meta['_wplf_email_copy_enabled']) && $form_meta['_wplf_email_copy_enabled'][0] ) {
19-
$to = isset($form_meta['_wplf_email_copy_to']) ? $form_meta['_wplf_email_copy_to'][0] : get_option( 'admin_email' );
18+
if( ( isset( $form_meta['_wplf_email_copy_enabled'] ) && $form_meta['_wplf_email_copy_enabled'][0] ) || isset( $submission_id ) ) {
19+
20+
$to = $form_meta['_wplf_email_copy_to'][0];
2021
$subject = wp_sprintf( __('New submission from %s', 'wp-libre-form'), $referrer );
22+
23+
if( isset( $submission_id ) ) {
24+
$to = get_post_meta( $submission_id, '_wplf_email_copy_to', true );
25+
$subject = wp_sprintf( __('Submission from %s', 'wp-libre-form'), $referrer );
26+
}
27+
28+
$to = !empty( $to ) ? $to : get_option( 'admin_email' );
2129
$content = wp_sprintf( __('Form "%s" (ID %d) was submitted with values below: ', 'wp-libre-form'), $form_title, $form_id );
2230
$content = apply_filters( 'wplf_email_copy_content_start', $content, $form_title, $form_id ). "\n\n";
2331

24-
foreach( $_POST as $key => $value ) {
32+
$fields = $_POST;
33+
if( isset( $submission_id ) ) {
34+
$fields = get_post_meta( $submission_id );
35+
}
36+
37+
foreach( $fields as $key => $value ) {
2538
if( '_' === $key[0] ) {
2639
continue;
2740
}

0 commit comments

Comments
 (0)