Email Entry Notes

Adds an “Email Notes” option to the bulk action dropdown in the Notes box on the entry detail screen. Select one or more notes, choose “Email Notes”, click Apply, then pick the recipient in the dialog that appears. The dropdown lists the email addresses found in the entry, and (optionally) lets you type a different address.

Installation:

  1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
  2. Optionally set form and field IDs in the configuration at the bottom
new GW_Email_Entry_Notes( array(
    'form_id'                => 123,               // Optional. Limit to one form. Leave false for all forms.
    'email_field_id'         => 4,                 // Optional. Field listed first / used as the default recipient.
    'allow_custom_recipient' => true,              // Set to false to only allow addresses found in the entry.
    'from_email'             => 'current_user',    // Optional. Use "current_user" or a hard-coded email address.
    'from_name'              => 'Gravity Wiz',     // Optional. Defaults to the current user's display name when using "current_user".
    'sort_order'             => 'ASC',             // Optional. Use "ASC" or "DESC" to sort notes chronologically.
) );

Instructions

See “Where do I put snippets?” in our documentation for installation instructions.

Code

Filename: gw-email-entry-notes.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
<?php
/**
 * Gravity Wiz // Gravity Forms // Email Entry Notes
 *
 * Adds an "Email Notes" option to the bulk action dropdown in the Notes box on the
 * entry detail screen. Select one or more notes, choose "Email Notes", click Apply,
 * then pick the recipient in the dialog that appears. The dropdown lists the email
 * addresses found in the entry, and (optionally) lets you type a different address.
 *
 * Installation:
 *   1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
 *   2. Optionally set form and field IDs in the configuration at the bottom
 *
 *     new GW_Email_Entry_Notes( array(
 *         'form_id'                => 123,               // Optional. Limit to one form. Leave false for all forms.
 *         'email_field_id'         => 4,                 // Optional. Field listed first / used as the default recipient.
 *         'allow_custom_recipient' => true,              // Set to false to only allow addresses found in the entry.
 *         'from_email'             => 'current_user',    // Optional. Use "current_user" or a hard-coded email address.
 *         'from_name'              => 'Gravity Wiz',     // Optional. Defaults to the current user's display name when using "current_user".
 *         'sort_order'             => 'ASC',             // Optional. Use "ASC" or "DESC" to sort notes chronologically.
 *     ) );
 */
class GW_Email_Entry_Notes {

	const ACTION = 'gw_email_notes';
	const FIELD  = 'gw_email_notes_recipient';

	private $_args    = array();
	private $_notices = array();

	public function __construct( $args = array() ) {

		$this->_args = wp_parse_args( $args, array(
			'form_id'                => false,
			'email_field_id'         => false,
			'allow_custom_recipient' => true,
			'from_email'             => false,
			'from_name'              => false,
			'sort_order'             => 'ASC',
			// Merge tags: {entry_id}, {form_title}
			'subject'                => 'Notes for {form_title} (Entry #{entry_id})',
			'action_label'           => 'Email Notes',
		) );

		add_action( 'admin_init', array( $this, 'maybe_email_notes' ) );
		add_action( 'admin_footer', array( $this, 'output_script' ) );
		add_action( 'admin_notices', array( $this, 'display_notices' ) );

	}

	/**
	 * The entry and form for the current entry detail screen, or false if this isn't
	 * that screen (or the snippet isn't enabled for the form).
	 */
	private function get_context() {

		if ( ! is_admin() || rgget( 'page' ) !== 'gf_entries' || rgget( 'view' ) !== 'entry' ) {
			return false;
		}

		$entry = GFAPI::get_entry( absint( rgget( 'lid' ) ) );
		if ( is_wp_error( $entry ) ) {
			return false;
		}

		$form = GFAPI::get_form( $entry['form_id'] );
		if ( ! $form || ( $this->_args['form_id'] && (int) $this->_args['form_id'] !== (int) $form['id'] ) ) {
			return false;
		}

		return array( $form, $entry );
	}

	/**
	 * Email addresses found in the entry, as array( email => field label ).
	 * The configured email_field_id (if any) comes first, then the form's Email fields.
	 */
	private function get_candidates( $form, $entry ) {

		$field_ids = array();

		if ( $this->_args['email_field_id'] ) {
			$field_ids[] = (string) $this->_args['email_field_id'];
		}

		foreach ( GFAPI::get_fields_by_type( $form, 'email' ) as $field ) {
			$field_ids[] = (string) $field->id;
		}

		$candidates = array();

		foreach ( array_unique( $field_ids ) as $field_id ) {
			$value = trim( (string) rgar( $entry, $field_id ) );
			if ( ! is_email( $value ) || isset( $candidates[ $value ] ) ) {
				continue;
			}

			$field                = GFAPI::get_field( $form, $field_id );
			$candidates[ $value ] = $field ? GFCommon::get_label( $field ) : 'Field ' . $field_id;
		}

		return $candidates;
	}

	/**
	 * Gravity Forms doesn't offer a filter for the note bulk actions, so inject the option
	 * (and the recipient dialog) via JS.
	 */
	public function output_script() {

		$context = $this->get_context();
		if ( ! $context ) {
			return;
		}

		list( $form, $entry ) = $context;

		$candidates = $this->get_candidates( $form, $entry );

		// Nothing to send to and no way to type an address; don't offer the action at all.
		if ( empty( $candidates ) && ! $this->_args['allow_custom_recipient'] ) {
			return;
		}

		$config = array(
			'action'      => self::ACTION,
			'field'       => self::FIELD,
			'label'       => $this->_args['action_label'],
			'candidates'  => $candidates,
			'allowCustom' => (bool) $this->_args['allow_custom_recipient'],
		);

		?>
			<script type="text/javascript">
			jQuery( function( $ ) {

				var cfg      = <?php echo wp_json_encode( $config ); ?>;
				var $selects = $( 'select[name="bulk_action"], select[name="bulk_action2"]' );

				if ( ! $selects.length ) {
					return;
				}

				$selects.append( $( '<option>', {
					value: cfg.action,
					text: cfg.label
				} ) );

				var $form   = $selects.first().closest( 'form' );
				var $hidden = $( '<input type="hidden">' )
					.attr( 'name', cfg.field )
					.appendTo( $form );

				// Recipient dialog.
				var $dialog = $( '<dialog style="padding:20px 24px;border:1px solid #c3c4c7;border-radius:4px;min-width:320px;"></dialog>' );
				var $title  = $( '<p style="margin:0 0 12px;font-weight:600;">Email selected notes to:</p>' );
				var $choice = $( '<select style="width:100%;margin-bottom:8px;"></select>' );
				var $custom = $( '<input type="email" multiple placeholder="name@example.com, name@example.org" style="width:100%;margin-bottom:8px;display:none;">' );
				var $send   = $( '<button type="button" class="button button-primary" style="margin-right:6px;">Send</button>' );
				var $cancel = $( '<button type="button" class="button">Cancel</button>' );

				$.each( cfg.candidates, function( email, label ) {
					$choice.append( $( '<option>', {
						value: email,
						text: label + ' — ' + email
					} ) );
				} );

				if ( cfg.allowCustom ) {
					$choice.append( $( '<option>', {
						value: '__other__',
						text: 'Other address…'
					} ) );
				}

				function toggleCustom() {
					$custom.toggle( $choice.val() === '__other__' );
				}

				$choice.on( 'change', toggleCustom );
				toggleCustom();

				$dialog.append(
					$title,
					$choice,
					$custom,
					$send,
					$cancel
				).appendTo( 'body' );

				$cancel.on( 'click', function() {
					$dialog[0].close();
				} );

				$send.on( 'click', function() {
					var isCustom = $choice.val() === '__other__';

					if ( isCustom && ! $custom[0].checkValidity() ) {
						$custom[0].reportValidity();
						return;
					}

					$hidden.val(
						isCustom ? $.trim( $custom.val() ) : $choice.val()
					);

					$dialog[0].close();

					// Bypass any "submit" input shadowing the native method.
					HTMLFormElement.prototype.submit.call( $form[0] );
				} );

				// Intercept Apply when "Email Notes" is selected and no recipient has been chosen yet.
				$form.on( 'submit', function( e ) {

					var isEmailAction = $selects.filter( function() {
						return $( this ).val() === cfg.action;
					} ).length > 0;

					if ( ! isEmailAction || $hidden.val() ) {
						return;
					}

					e.preventDefault();

					if ( ! $form.find( 'input[name="note[]"]:checked' ).length ) {
						window.alert( 'Please select at least one note to email.' );
						return;
					}

					$dialog[0].showModal();
				} );

			} );
		</script>
			<?php

	}

	/**
	 * Handle the submitted bulk action.
	 */
	public function maybe_email_notes() {

		$bulk_action = rgpost( 'bulk_action' )
			? rgpost( 'bulk_action' )
			: rgpost( 'bulk_action2' );

		if ( rgpost( 'action' ) !== 'bulk' || $bulk_action !== self::ACTION ) {
			return;
		}

		$context = $this->get_context();
		if ( ! $context ) {
			return;
		}

		list( $form, $entry ) = $context;

		// Same nonce Gravity Forms uses for its own note actions.
		check_admin_referer( 'gforms_update_note', 'gforms_update_note' );

		if ( ! GFCommon::current_user_can_any( 'gravityforms_edit_entry_notes' ) ) {
			wp_die( esc_html__( 'You do not have permission to do this.', 'gravityforms' ) );
		}

		$note_ids = array_filter(
			array_map( 'absint', (array) rgpost( 'note' ) )
		);

		if ( empty( $note_ids ) ) {
			return $this->add_notice(
				'Please select at least one note to email.',
				'error'
			);
		}

		// The custom address input accepts a comma-separated list.
		$to = array_filter(
			array_map(
				'trim',
				explode( ',', (string) rgpost( self::FIELD ) )
			)
		);

		$candidates = $this->get_candidates( $form, $entry );

		foreach ( $to as $email ) {
			if ( ! is_email( $email ) ) {
				return $this->add_notice(
					sprintf(
						'"%s" is not a valid email address.',
						$email
					),
					'error'
				);
			}

			if (
				! $this->_args['allow_custom_recipient']
				&& ! isset( $candidates[ $email ] )
			) {
				return $this->add_notice(
					sprintf(
						'"%s" is not an allowed email address.',
						$email
					),
					'error'
				);
			}
		}

		if ( empty( $to ) ) {
			return $this->add_notice(
				'No valid email address was provided.',
				'error'
			);
		}

		$notes = array();

		foreach ( GFFormsModel::get_lead_notes( $entry['id'] ) as $note ) {
			if ( in_array( (int) $note->id, $note_ids, true ) ) {
				$notes[] = $note;
			}
		}

		if ( empty( $notes ) ) {
			return $this->add_notice(
				'The selected notes could not be found.',
				'error'
			);
		}

		$this->sort_notes( $notes );

		$subject = str_replace(
			array( '{entry_id}', '{form_title}' ),
			array( $entry['id'], $form['title'] ),
			$this->_args['subject']
		);

		$headers    = array( 'Content-Type: text/html; charset=UTF-8' );
		$from_email = $this->get_from_email();

		if ( $from_email && ! is_email( $from_email ) ) {
			return $this->add_notice(
				sprintf(
					'"%s" is not a valid from email address.',
					$from_email
				),
				'error'
			);
		}

		if ( $from_email ) {
			$from_name = $this->get_from_name();

			$headers[] = $from_name
				? sprintf(
					'From: "%s" <%s>',
					$from_name,
					$from_email
				)
				: 'From: ' . $from_email;
		}

		$sent = wp_mail(
			$to,
			$subject,
			$this->build_message( $notes ),
			$headers
		);

		if ( $sent ) {
			$this->add_notice(
				sprintf(
					'%d note(s) emailed to %s.',
					count( $notes ),
					implode( ', ', $to )
				),
				'success'
			);
		} else {
			$this->add_notice(
				'The email could not be sent. Please check your site\'s email configuration.',
				'error'
			);
		}

	}

	/**
	 * Get the configured sender email address.
	 *
	 * @return string|false
	 */
	private function get_from_email() {

		if ( $this->_args['from_email'] === 'current_user' ) {
			$user = wp_get_current_user();

			return $user->exists()
				? $user->user_email
				: false;
		}

		return $this->_args['from_email']
			? trim( (string) $this->_args['from_email'] )
			: false;
	}

	/**
	 * Get the configured sender name.
	 *
	 * If no name is configured and the sender email uses the current user,
	 * use that user's WordPress display name.
	 *
	 * @return string|false
	 */
	private function get_from_name() {

		$from_name = $this->_args['from_name'];

		if (
			! $from_name
			&& $this->_args['from_email'] === 'current_user'
		) {
			$user      = wp_get_current_user();
			$from_name = $user->exists()
				? $user->display_name
				: false;
		}

		return $from_name
			? wp_strip_all_tags(
				trim( (string) $from_name ),
				true
			)
			: false;
	}

	/**
	 * Sort the selected notes.
	 *
	 * @param array $notes Notes to sort.
	 */
	private function sort_notes( &$notes ) {

		$order = strtoupper( (string) $this->_args['sort_order'] ) === 'DESC'
			? -1
			: 1;

		usort( $notes, function( $a, $b ) use ( $order ) {
			$date_comparison = strcmp(
				$a->date_created,
				$b->date_created
			);

			if ( $date_comparison !== 0 ) {
				return $date_comparison * $order;
			}

			return (
				(int) $a->id <=> (int) $b->id
			) * $order;
		} );
	}

	/**
	 * Build the HTML email body.
	 *
	 * @param array $notes Notes to include.
	 *
	 * @return string
	 */
	private function build_message( $notes ) {

		$date_format = get_option( 'date_format' )
			. ' '
			. get_option( 'time_format' );

		$html = '';

		foreach ( $notes as $note ) {
			$html .= sprintf(
				'<div style="margin:0 0 20px;padding:0 0 15px;border-bottom:1px solid #ddd;"><p style="margin:0 0 8px;color:#666;font-size:13px;">%s &middot; %s</p>%s</div>',
				esc_html(
					$note->user_name
						? $note->user_name
						: 'System'
				),
				esc_html(
					get_date_from_gmt(
						$note->date_created,
						$date_format
					)
				),
				wpautop( esc_html( $note->value ) )
			);
		}

		return $html;
	}

	/**
	 * Add an admin notice.
	 *
	 * @param string $message Notice text.
	 * @param string $type    Notice type.
	 */
	private function add_notice( $message, $type = 'success' ) {

		$this->_notices[] = array(
			'message' => $message,
			'type'    => $type,
		);
	}

	/**
	 * Display queued admin notices.
	 */
	public function display_notices() {

		foreach ( $this->_notices as $notice ) {
			printf(
				'<div class="notice notice-%s is-dismissible"><p>%s</p></div>',
				esc_attr( $notice['type'] ),
				esc_html( $notice['message'] )
			);
		}
	}

}

# Configuration
new GW_Email_Entry_Notes( array(
	// 'form_id'                => 123, // Optional. Limit to one form. Leave false for all forms.
	// 'email_field_id'         => 4, // Optional. Sets the default recipient.
	// 'allow_custom_recipient' => false, // Optional. Set to false to only allow addresses found in the entry.
	// 'from_email'             => 'current_user', // Optional. Use "current_user" or a hard-coded email address.
	// 'from_name'              => 'Gravity Wiz', // Optional. Defaults to the current user's display name when using "current_user".
	// 'sort_order'             => 'DESC', // Optional. Use "ASC" or "DESC" to sort notes chronologically.
) );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.