GravityView  1.19.4
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-field-notes.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Notes Field
4  *
5  * @package GravityView
6  * @license GPL2+
7  * @since 1.17
8  * @author Katz Web Services, Inc.
9  * @link https://gravityview.co
10  * @copyright Copyright 2016, Katz Web Services, Inc.
11  */
12 
13 /**
14  * Add Entry Notes
15  * @since 1.17
16  */
18 
19  /**
20  * @var string Current __FILE__
21  * @since 1.17
22  */
23  static $file;
24 
25  /**
26  * @var string plugin_dir_path() of the current field file
27  * @since 1.17
28  */
29  static $path;
30 
31  /**
32  * @var bool Are we doing an AJAX request?
33  * @since 1.17
34  */
35  private $doing_ajax = false;
36 
37  /**
38  * The name of the GravityView field type
39  * @var string
40  */
41  var $name = 'notes';
42 
43  function __construct() {
44 
45  self::$path = plugin_dir_path( __FILE__ );
46  self::$file = __FILE__;
47 
48  $this->doing_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX;
49 
50  $this->add_hooks();
51 
52  parent::__construct();
53  }
54 
55  /**
56  * Add AJAX hooks, [gv_note_add] shortcode, and template loading paths
57  *
58  * @since 1.17
59  *
60  * @return void
61  */
62  private function add_hooks() {
63 
64  add_shortcode( 'gv_note_add', array( 'GravityView_Field_Notes', 'get_add_note_part' ) );
65 
66  add_action( 'wp', array( $this, 'maybe_delete_notes'), 1000 );
67  add_action( 'wp_ajax_nopriv_gv_delete_notes', array( $this, 'maybe_delete_notes') );
68  add_action( 'wp_ajax_gv_delete_notes', array( $this, 'maybe_delete_notes') );
69 
70  add_action( 'wp', array( $this, 'maybe_add_note'), 1000 );
71  add_action( 'wp_ajax_nopriv_gv_note_add', array( $this, 'maybe_add_note') );
72  add_action( 'wp_ajax_gv_note_add', array( $this, 'maybe_add_note') );
73 
74  // add template path to check for field
75  add_filter( 'gravityview_template_paths', array( $this, 'add_template_path' ) );
76 
77  add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts') );
78  add_action( 'gravityview/field/notes/scripts', array( $this, 'enqueue_scripts' ) );
79 
80  add_filter( 'gravityview_entry_default_fields', array( $this, 'add_entry_default_field' ), 10, 3 );
81  }
82 
83 
84  /**
85  * Add Entry Notes to the Add Field picker in Edit View
86  *
87  * @see GravityView_Admin_Views::get_entry_default_fields()
88  *
89  * @since 1.17
90  *
91  * @param array $entry_default_fields Fields configured to show in the picker
92  * @param array $form Gravity Forms form array
93  * @param string $zone Current context: `directory`, `single`, `edit`
94  *
95  * @return array Fields array with notes added, if in Multiple Entries or Single Entry context
96  */
97  public function add_entry_default_field( $entry_default_fields, $form, $zone ) {
98 
99  if( in_array( $zone, array( 'directory', 'single' ) ) ) {
100  $entry_default_fields['notes'] = array(
101  'label' => __( 'Entry Notes', 'gravityview' ),
102  'type' => 'notes',
103  'desc' => __( 'Display, add, and delete notes for an entry.', 'gravityview' ),
104  );
105  }
106 
107  return $entry_default_fields;
108  }
109 
110  /**
111  * Register scripts and styles used by the Notes field
112  *
113  * @since 1.17
114  *
115  * @return void
116  */
117  public function register_scripts() {
118  $css_file = gravityview_css_url( 'entry-notes.css', GravityView_Field_Notes::$path . 'assets/css/' );
119  wp_register_style( 'gravityview-notes', $css_file, array(), GravityView_Plugin::version );
120  wp_register_script( 'gravityview-notes', plugins_url( '/assets/js/entry-notes.js', GravityView_Field_Notes::$file ), array( 'jquery' ), GravityView_Plugin::version, true );
121  }
122 
123  /**
124  * Enqueue, localize field scripts and styles
125  *
126  * @since 1.17
127  *
128  * @return void
129  */
130  public function enqueue_scripts() {
131  global $wp_actions;
132 
133  if( ! wp_script_is( 'gravityview-notes', 'enqueued' ) ) {
134  wp_enqueue_style( 'gravityview-notes' );
135  wp_enqueue_script( 'gravityview-notes' );
136  }
137 
138  if( ! wp_script_is( 'gravityview-notes', 'done' ) ) {
139 
140  $strings = self::strings();
141 
142  wp_localize_script( 'gravityview-notes', 'GVNotes', array(
143  'ajaxurl' => admin_url( 'admin-ajax.php' ),
144  'text' => array(
145  'processing' => $strings['processing'],
146  'delete_confirm' => $strings['delete-confirm'],
147  'note_added' => $strings['added-note'],
148  'error_invalid' => $strings['error-invalid'],
149  'error_empty_note' => $strings['error-empty-note'],
150  ),
151  ) );
152  }
153  }
154 
155  /**
156  * Verify permissions, check if $_POST is set and as expected. If so, use process_add_note
157  *
158  * @since 1.17
159  *
160  * @see process_add_note
161  *
162  * @return void
163  */
164  function maybe_add_note() {
165 
166  if( ! GVCommon::has_cap( 'gravityview_add_entry_notes' ) ) {
167  do_action( 'gravityview_log_error', __METHOD__ . ': The user isnt allowed to add entry notes.' );
168  return;
169  }
170 
171  if( 'gv_note_add' === rgpost('action') ) {
172 
173  $post = wp_unslash( $_POST );
174 
175  if( $this->doing_ajax ) {
176  parse_str( $post['data'], $data );
177  } else {
178  $data = $post;
179  }
180 
181  $this->process_add_note( (array) $data );
182  }
183  }
184 
185  /**
186  * Handle adding a note.
187  *
188  * Verify the request. If valid, add the note. If AJAX request, send response JSON.
189  *
190  * @since 1.17
191  *
192  * @var array $data {
193  * @type string $action "gv_note_add"
194  * @type string $entry-slug Entry slug or ID to add note to
195  * @type string $gv_note_add Nonce with action "gv_note_add_{entry slug}" and name "gv_note_add"
196  * @type string $_wp_http_referer Relative URL to submitting page ('/view/example/entry/123/')
197  * @type string $gv-note-content Note content
198  * @type string $add_note Submit button value ('Add Note')
199  * }
200  *
201  * @return void
202  */
203  private function process_add_note( $data ) {
204 
205  $error = false;
206  $success = false;
207 
208  if( empty( $data['entry-slug'] ) ) {
209 
210  $error = self::strings('error-invalid');
211  do_action( 'gravityview_log_error', __METHOD__ . ': The note is missing an Entry ID.' );
212 
213  } else {
214 
215  $valid = wp_verify_nonce( $data['gv_note_add'], 'gv_note_add_' . $data['entry-slug'] );
216 
217  $has_cap = GVCommon::has_cap( 'gravityview_add_entry_notes' );
218 
219  if( ! $has_cap ) {
220  $error = self::strings( 'error-cap-add' );
221  do_action( 'gravityview_log_error', __METHOD__ . ': Adding a note failed: the user does not have the "gravityview_add_entry_notes" capability.' );
222  } elseif ( $valid ) {
223 
224  $entry = gravityview_get_entry( $data['entry-slug'], true, false );
225 
226  $added = $this->add_note( $entry, $data );
227 
228  // Error adding note
229  if ( is_wp_error( $added ) ) {
230 
231  $error = $added->get_error_message();
232 
233  } else {
234 
235  // Confirm the note was added, because GF doesn't return note ID on success
236  $note = GravityView_Entry_Notes::get_note( $added );
237 
238  // Possibly email peeps about this great new note
239  $this->maybe_send_entry_notes( $note, $entry, $data );
240 
241  if ( $note ) {
242  $success = self::display_note( $note, ! empty( $data['show-delete'] ) );
243  do_action( 'gravityview_log_debug', __METHOD__ . ': The note was successfully created', compact('note', 'data') );
244  } else {
245  $error = self::strings('error-add-note');
246  do_action( 'gravityview_log_error', __METHOD__ . ': The note was not successfully created', compact('note', 'data') );
247  }
248  }
249  } else {
250  $error = self::strings('error-invalid');
251  do_action( 'gravityview_log_error', __METHOD__ . ': Nonce validation failed; the note was not created' );
252  }
253  }
254 
255 
256  if( $this->doing_ajax ) {
257  if( $success ) {
258  wp_send_json_success( array( 'html' => $success ) );
259  } else {
260  $error = $error ? $error : self::strings( 'error-invalid' );
261  wp_send_json_error( array( 'error' => esc_html( $error ) ) );
262  }
263  }
264  }
265 
266  /**
267  * Possibly delete notes, if request is proper.
268  *
269  * Verify permissions. Check expected $_POST. Parse args, then send to process_delete_notes
270  *
271  * @since 1.17
272  *
273  * @see process_delete_notes
274  *
275  * @return void
276  */
277  function maybe_delete_notes() {
278 
279  if ( ! GVCommon::has_cap( 'gravityview_delete_entry_notes' ) ) {
280  return;
281  }
282 
283  if ( isset( $_POST['action'] ) && 'gv_delete_notes' === $_POST['action'] ) {
284 
285  $post = wp_unslash( $_POST );
286  if ( $this->doing_ajax ) {
287  parse_str( $post['data'], $data );
288  } else {
289  $data = $post;
290  }
291 
292  $required_args = array(
293  'gv_delete_notes' => '',
294  'entry-slug' => '',
295  );
296 
297  $data = wp_parse_args( $data, $required_args );
298 
299  $this->process_delete_notes( $data );
300  }
301  }
302 
303  /**
304  * Handle deleting notes
305  *
306  * @var array $data {
307  * @type string $action "gv_delete_notes"
308  * @type string $entry-slug Entry slug or ID to add note to
309  * @type string $gv_delete_notes Nonce with action "gv_delete_notes_{entry slug}" and name "gv_delete_notes"
310  * @type string $_wp_http_referer Relative URL to submitting page ('/view/example/entry/123/')
311  * @type int[] $note Array of Note IDs to be deleted
312  * }
313  *
314  * @return void
315  */
316  function process_delete_notes( $data ) {
317 
318  $valid = wp_verify_nonce( $data['gv_delete_notes'], 'gv_delete_notes_' . $data['entry-slug'] );
319  $has_cap = GVCommon::has_cap( 'gravityview_delete_entry_notes' );
320  $success = false;
321 
322  if ( $valid && $has_cap ) {
323  GravityView_Entry_Notes::delete_notes( $data['note'] );
324  $success = true;
325  }
326 
327  if( $this->doing_ajax ) {
328 
329  if( $success ) {
330  wp_send_json_success();
331  } else {
332  if ( ! $valid ) {
333  $error_message = self::strings( 'error-invalid' );
334  } else {
335  $error_message = self::strings( 'error-permission-delete' );
336  }
337 
338  wp_send_json_error( array( 'error' => $error_message ) );
339  }
340  }
341  }
342 
343  /**
344  * Include this extension templates path
345  *
346  * @since 1.17
347  *
348  * @param array $file_paths List of template paths ordered
349  *
350  * @return array File paths with `./` and `./partials/` paths added
351  */
352  public function add_template_path( $file_paths ) {
353 
354  $file_paths[ 172 ] = self::$path;
355  $file_paths[ 173 ] = self::$path . 'partials/';
356 
357  return $file_paths;
358  }
359 
360  public function field_options( $field_options, $template_id, $field_id, $context, $input_type ) {
361 
362  unset( $field_options['show_as_link'] );
363 
364  $notes_options = array(
365  'notes' => array(
366  'type' => 'checkboxes',
367  'label' => __('Note Settings', 'gravityview'),
368  'desc' => sprintf( _x('Only users with specific capabilities will be able to view, add and delete notes. %sRead more%s.', '%s is opening and closing HTML link', 'gravityview' ), '<a href="https://docs.gravityview.co/article/311-gravityview-capabilities">', '</a>' ),
369  'options' => array(
370  'view' => array(
371  'label' => __( 'Display notes?', 'gravityview' ),
372  ),
373  'view_loggedout' => array(
374  'label' => __( 'Display notes to users who are not logged-in?', 'gravityview' ),
375  'requires' => 'view',
376  ),
377  'add' => array(
378  'label' => __( 'Enable adding notes?', 'gravityview' ),
379  ),
380  'email' => array(
381  'label' => __( 'Allow emailing notes?', 'gravityview' ),
382  'requires' => 'add',
383  ),
384  'delete' => array(
385  'label' => __( 'Allow deleting notes?', 'gravityview' ),
386  ),
387  ),
388  'value' => array( 'view' => 1, 'add' => 1, 'email' => 1 ),
389  ),
390  );
391 
392  return $notes_options + $field_options;
393  }
394 
395  /**
396  * Get strings used by the Entry Notes field
397  *
398  * Use `gravityview/field/notes/strings` filter to modify the strings
399  *
400  * @since 1.17
401  *
402  * @param string $key If set, return the string with the key of $key
403  *
404  * @return array|string Array of strings with keys and values. If $key is set, returns string. If missing $strings[ $key ], empty string.
405  */
406  static public function strings( $key = '' ) {
407 
408  $strings = array(
409  'add-note' => __( 'Add Note', 'gravityview' ),
410  'added-note' => __( 'Note added.', 'gravityview' ),
411  'content-label' => __( 'Note Content', 'gravityview' ),
412  'delete' => __( 'Delete', 'gravityview' ),
413  'delete-confirm' => __( 'Are you sure you want to delete the selected notes?', 'gravityview' ),
414  'caption' => __( 'Notes for this entry', 'gravityview' ),
415  'toggle-notes' => __( 'Toggle all notes', 'gravityview' ),
416  'no-notes' => __( 'There are no notes.', 'gravityview' ),
417  'processing' => __( 'Processing&hellip;', 'gravityview' ),
418  'other-email' => __( 'Other email address', 'gravityview' ),
419  'email-label' => __( 'Email address', 'gravityview' ),
420  'email-placeholder' => _x('you@example.com', 'Example email address used as a placeholder', 'gravityview'),
421  'subject-label' => __( 'Subject', 'gravityview' ),
422  'subject' => __( 'Email subject', 'gravityview' ),
423  'default-email-subject' => __( 'New entry note', 'gravityview' ),
424  'email-footer' => __( 'This note was sent from {url}', 'gravityview' ),
425  'also-email' => __( 'Also email this note to', 'gravityview' ),
426  'error-add-note' => __( 'There was an error adding the note.', 'gravityview' ),
427  'error-invalid' => __( 'The request was invalid. Refresh the page and try again.', 'gravityview' ),
428  'error-empty-note' => _x( 'Note cannot be blank.', 'Message to display when submitting a note without content.', 'gravityview' ),
429  'error-cap-delete' => __( 'You don\'t have the ability to delete notes.', 'gravityview' ),
430  'error-cap-add' => __( 'You don\'t have the ability to add notes.', 'gravityview' ),
431  );
432 
433  /**
434  * @filter `gravityview/field/notes/strings` Modify the text used in the Entry Notes field. Sanitized by `esc_html` after return.
435  * @since 1.17
436  * @param array $strings Text in key => value pairs
437  */
438  $strings = gv_map_deep( apply_filters( 'gravityview/field/notes/strings', $strings ), 'esc_html' );
439 
440  if( $key ) {
441  return isset( $strings[ $key ] ) ? $strings[ $key ] : '';
442  }
443 
444  return $strings;
445  }
446 
447  /**
448  * Generate HTML output for a single note
449  *
450  * @since 1.17
451  *
452  * @param object $note Note object with id, user_id, date_created, value, note_type, user_name, user_email vars
453  * @param bool $show_delete Whether to show the bulk delete inputs
454  *
455  * @return string HTML
456  */
457  static public function display_note( $note, $show_delete = false ) {
458 
459  if( ! is_object( $note ) ) {
460  return '';
461  }
462 
463  $note_content = array(
464  'avatar' => get_avatar( $note->user_id, 48 ),
465  'user_name' => $note->user_name,
466  'user_email' => $note->user_email,
467  'added_on' => esc_html__( 'added on {date_created_formatted}', 'gravityview' ),
468  'value' => wpautop( esc_html( $note->value ) ),
469  'date_created' => $note->date_created,
470  'date_created_formatted' => GFCommon::format_date( $note->date_created, false ),
471  'user_id' => intval( $note->user_id ),
472  'note_type' => $note->note_type,
473  'note_id' => intval( $note->id ),
474  );
475 
476  /**
477  * @filter `gravityview/field/notes/content` Modify the note content before rendering in the template
478  * @since 1.17
479  * @param array $note_content Array of note content that will be replaced in template files
480  * @param object $note Note object with id, user_id, date_created, value, note_type, user_name, user_email vars
481  * @param boolean $show_delete True: Notes are editable. False: no editing notes.
482  */
483  $note_content = apply_filters( 'gravityview/field/notes/content', $note_content, $note, $show_delete );
484 
485  ob_start();
486  GravityView_View::getInstance()->get_template_part( 'note', 'detail' );
487  $note_detail_html = ob_get_clean();
488 
489  foreach ( $note_content as $tag => $value ) {
490  $note_detail_html = str_replace( '{' . $tag . '}', $value, $note_detail_html );
491  }
492 
493  $note_row_template = ( $show_delete && GVCommon::has_cap( 'gravityview_delete_entry_notes' ) ) ? 'row-editable' : 'row';
494 
495  ob_start();
496  GravityView_View::getInstance()->get_template_part( 'note', $note_row_template );
497  $note_row = ob_get_clean();
498 
499  $replacements = array(
500  '{note_id}' => $note_content['note_id'],
501  '{row_class}' => 'gv-note',
502  '{note_detail}' => $note_detail_html
503  );
504 
505  // Strip extra whitespace in template
506  $output = gravityview_strip_whitespace( $note_row );
507 
508  foreach ( $replacements as $tag => $replacement ) {
509  $output = str_replace( $tag, $replacement, $output );
510  }
511 
512  return $output;
513  }
514 
515  /**
516  * Add a note.
517  *
518  * @since 1.17
519  *
520  * @see GravityView_Entry_Notes::add_note This method is mostly a wrapper
521  *
522  * @param array $entry
523  * @param array $data Note details array
524  *
525  * @return int|WP_Error
526  */
527  private function add_note( $entry, $data ) {
528  global $current_user, $wpdb;
529 
530  $user_data = get_userdata( $current_user->ID );
531 
532  $note_content = trim( $data['gv-note-content'] );
533 
534  if( empty( $note_content ) ) {
535  return new WP_Error( 'gv-add-note-empty', __( 'The note is empty.', 'gravityview' ) );
536  }
537 
538  $return = GravityView_Entry_Notes::add_note( $entry['id'], $user_data->ID, $user_data->display_name, $note_content, 'gravityview/field/notes' );
539 
540  return $return;
541  }
542 
543  /**
544  * Get the Add Note form HTML
545  *
546  * @todo Allow passing entry_id as a shortcode parameter to set entry from shortcode
547  *
548  * @since 1.17
549  *
550  * @return string HTML of the Add Note form, or empty string if the user doesn't have the `gravityview_add_entry_notes` cap
551  */
552  public static function get_add_note_part() {
553 
554  if( ! GVCommon::has_cap( 'gravityview_add_entry_notes' ) ) {
555  do_action( 'gravityview_log_error', __METHOD__ . ': User does not have permission to add entry notes ("gravityview_add_entry_notes").' );
556  return '';
557  }
558 
560 
561  ob_start();
562  $gravityview_view->get_template_part( 'note', 'add-note' );
563  $add_note_html = ob_get_clean();
564 
565  // Strip extra whitespace in template
566  $add_note_html = gravityview_strip_whitespace( $add_note_html );
567 
568  $visibility_settings = $gravityview_view->getCurrentFieldSetting( 'notes' );
569  $entry = $gravityview_view->getCurrentEntry();
571  $nonce_field = wp_nonce_field( 'gv_note_add_' . $entry_slug, 'gv_note_add', false, false );
572 
573  // Only generate the dropdown if the field settings allow it
574  $email_fields = '';
575  if( ! empty( $visibility_settings['email'] ) ) {
576  $email_fields = self::get_note_email_fields( $entry_slug );
577  }
578 
579  $add_note_html = str_replace( '{entry_slug}', $entry_slug, $add_note_html );
580  $add_note_html = str_replace( '{nonce_field}', $nonce_field, $add_note_html );
581  $add_note_html = str_replace( '{show_delete}', intval( $visibility_settings['delete'] ), $add_note_html );
582  $add_note_html = str_replace( '{email_fields}', $email_fields, $add_note_html );
583  $add_note_html = str_replace( '{url}', esc_url_raw( add_query_arg( array() ) ), $add_note_html );
584 
585  return $add_note_html;
586  }
587 
588  /**
589  * Get array of emails addresses from the stored entry
590  *
591  * @since 1.17
592  *
593  * @return array Array of email addresses connected to the entry
594  */
595  private static function get_note_emails_array() {
596 
598 
599  //getting email values
600  $email_fields = GFCommon::get_email_fields( $gravityview_view->getForm() );
601 
602  $entry = $gravityview_view->getCurrentEntry();
603 
604  $note_emails = array();
605 
606  foreach ( $email_fields as $email_field ) {
607  if ( ! empty( $entry["{$email_field->id}"] ) && is_email( $entry["{$email_field->id}"] ) ) {
608  $note_emails[] = $entry["{$email_field->id}"];
609  }
610  }
611 
612  /**
613  * @filter `gravityview/field/notes/emails` Modify the dropdown values displayed in the "Also email note to" dropdown
614  * @since 1.17
615  * @param array $note_emails Array of email addresses connected to the entry
616  * @param array $entry Current entry
617  */
618  $note_emails = apply_filters( 'gravityview/field/notes/emails', $note_emails, $entry );
619 
620  return (array) $note_emails;
621  }
622 
623  /**
624  * Generate a HTML dropdown of email values based on email fields from the current form
625  *
626  * @uses get_note_emails_array
627  *
628  * @since 1.17
629  *
630  * @param int|string $entry_slug Current entry unique ID
631  *
632  * @return string HTML output
633  */
634  private static function get_note_email_fields( $entry_slug = '' ) {
635 
636  if( ! GVCommon::has_cap( 'gravityview_email_entry_notes' ) ) {
637  do_action( 'gravityview_log_error', __METHOD__ . ': User does not have permission to email entry notes ("gravityview_email_entry_notes").' );
638  return '';
639  }
640 
641  $entry_slug_esc = esc_attr( $entry_slug );
642 
643  $note_emails = self::get_note_emails_array();
644 
645  $strings = self::strings();
646 
647  /**
648  * @filter `gravityview/field/notes/custom-email` Whether to include a Custom Email option for users to define a custom email to mail notes to
649  * @since 1.17
650  * @param bool $include_custom Default: true
651  */
652  $include_custom = apply_filters( 'gravityview/field/notes/custom-email', true );
653 
654  ob_start();
655 
656  if ( ! empty( $note_emails ) || $include_custom ) { ?>
657  <div class="gv-note-email-container">
658  <label for="gv-note-email-to-<?php echo $entry_slug_esc; ?>" class="screen-reader-text"><?php echo $strings['also-email']; ?></label>
659  <select class="gv-note-email-to" name="gv-note-to" id="gv-note-email-to-<?php echo $entry_slug_esc; ?>">
660  <option value=""><?php echo $strings['also-email']; ?></option>
661  <?php foreach ( $note_emails as $email ) {
662  ?>
663  <option value="<?php echo esc_attr( $email ); ?>"><?php echo esc_html( $email ); ?></option>
664  <?php }
665  if( $include_custom ) { ?>
666  <option value="custom"><?php echo self::strings('other-email'); ?></option>
667  <?php } ?>
668  </select>
669  <fieldset class="gv-note-to-container">
670  <?php if( $include_custom ) { ?>
671  <div class='gv-note-to-custom-container'>
672  <label for="gv-note-email-to-custom-<?php echo $entry_slug_esc; ?>"><?php echo $strings['email-label']; ?></label>
673  <input type="text" name="gv-note-to-custom" placeholder="<?php echo $strings['email-placeholder']; ?>" id="gv-note-to-custom-<?php echo $entry_slug_esc; ?>" value="" />
674  </div>
675  <?php } ?>
676  <div class='gv-note-subject-container'>
677  <label for="gv-note-subject-<?php echo $entry_slug_esc; ?>"><?php echo $strings['subject-label']; ?></label>
678  <input type="text" name="gv-note-subject" placeholder="<?php echo $strings['subject']; ?>" id="gv-note-subject-<?php echo $entry_slug_esc; ?>" value="" />
679  </div>
680  </fieldset>
681  </div>
682  <?php }
683 
684  // TODO: Add a filter
685  return ob_get_clean();
686  }
687 
688  /**
689  * If note has an email to send, and the user has the right caps, send it
690  *
691  * @since 1.17
692  *
693  * @param false|object $note If note was created, object. Otherwise, false.
694  * @param array $entry Entry data
695  * @param array $data $_POST data
696  *
697  * @return void Tap in to Gravity Forms' `gform_after_email` action if you want a return result from sending the email.
698  */
699  private function maybe_send_entry_notes( $note = false, $entry, $data ) {
700 
701  if( ! $note || ! GVCommon::has_cap('gravityview_email_entry_notes') ) {
702  do_action( 'gravityview_log_debug', __METHOD__ . ': User doesnt have "gravityview_email_entry_notes" cap, or $note is empty', $note );
703  return;
704  }
705 
706  do_action( 'gravityview_log_debug', __METHOD__ . ': $data', $data );
707 
708  //emailing notes if configured
709  if ( ! empty( $data['gv-note-to'] ) ) {
710 
711  $default_data = array(
712  'gv-note-to' => '',
713  'gv-note-to-custom' => '',
714  'gv-note-subject' => '',
715  'gv-note-content' => '',
716  'current-url' => '',
717  );
718 
719  $current_user = wp_get_current_user();
720  $email_data = wp_parse_args( $data, $default_data );
721 
722  $from = $current_user->user_email;
723  $to = $email_data['gv-note-to'];
724 
725  /**
726  * Documented in get_note_email_fields
727  * @see get_note_email_fields
728  */
729  $include_custom = apply_filters( 'gravityview/field/notes/custom-email', true );
730 
731  if( 'custom' === $to && $include_custom ) {
732  $to = $email_data['gv-note-to-custom'];
733  do_action( 'gravityview_log_debug', __METHOD__ . ': Sending note to a custom email address: ' . $to );
734  }
735 
736  if ( ! is_email( $to ) ) {
737  do_action( 'gravityview_log_error', __METHOD__ . ': $to not a valid email address: ' . $to, $email_data );
738  return;
739  }
740 
741  $bcc = false;
742  $reply_to = $from;
743  $subject = trim( $email_data['gv-note-subject'] );
744 
745  // We use empty() here because GF uses empty to check against, too. `0` isn't a valid subject to GF
746  $subject = empty( $subject ) ? self::strings( 'default-email-subject' ) : $subject;
747  $message = $email_data['gv-note-content'];
748  $email_footer = self::strings( 'email-footer' );
749  $from_name = $current_user->display_name;
750  $message_format = 'html';
751 
752  /**
753  * @filter `gravityview/field/notes/email_content` Modify the values passed when sending a note email
754  * @see GVCommon::send_email
755  * @since 1.17
756  * @param[in,out] array $email_settings Values being passed to the GVCommon::send_email() method: 'from', 'to', 'bcc', 'reply_to', 'subject', 'message', 'from_name', 'message_format', 'entry', 'email_footer'
757  */
758  $email_content = apply_filters( 'gravityview/field/notes/email_content', compact( 'from', 'to', 'bcc', 'reply_to', 'subject', 'message', 'from_name', 'message_format', 'entry', 'email_footer' ) );
759 
760  extract( $email_content );
761 
762  $is_html = ( 'html' === $message_format );
763 
764  // Add the message footer
765  $message .= $this->get_email_footer( $email_footer, $is_html, $email_data );
766 
767  /**
768  * @filter `gravityview/field/notes/wpautop_email` Should the message content have paragraphs added automatically, if using HTML message format
769  * @since 1.18
770  * @param bool $wpautop_email True: Apply wpautop() to the email message if using; False: Leave as entered (Default: true)
771  */
772  $wpautop_email = apply_filters( 'gravityview/field/notes/wpautop_email', true );
773 
774  if ( $is_html && $wpautop_email ) {
775  $message = wpautop( $message );
776  }
777 
778  GVCommon::send_email( $from, $to, $bcc, $reply_to, $subject, $message, $from_name, $message_format, '', $entry, false );
779 
780  $form = isset( $entry['form_id'] ) ? GFAPI::get_form( $entry['form_id'] ) : array();
781 
782  /**
783  * @see https://www.gravityhelp.com/documentation/article/10146-2/ It's here for compatibility with Gravity Forms
784  */
785  do_action( 'gform_post_send_entry_note', __METHOD__, $to, $from, $subject, $message, $form, $entry );
786  }
787  }
788 
789  /**
790  * Get the footer for Entry Note emails
791  *
792  * `{url}` is replaced by the URL of the page where the note form was embedded
793  *
794  * @since 1.18
795  * @see GravityView_Field_Notes::strings The default value of $message_footer is set here, with the key 'email-footer'
796  *
797  * @param string $email_footer The message footer value
798  * @param bool $is_html True: Email is being sent as HTML; False: sent as text
799  *
800  * @return string If email footer is not empty, return the message with placeholders replaced with dynamic values
801  */
802  private function get_email_footer( $email_footer = '', $is_html = true, $email_data = array() ) {
803 
804  $output = '';
805 
806  if( ! empty( $email_footer ) ) {
807  $url = rgar( $email_data, 'current-url' );
808  $url = html_entity_decode( $url );
809  $url = site_url( $url );
810 
811  $content = $is_html ? "<a href='{$url}'>{$url}</a>" : $url;
812 
813  $email_footer = str_replace( '{url}', $content, $email_footer );
814 
815  $output .= "\n\n$email_footer";
816  }
817 
818  return $output;
819  }
820 }
821 
$url
Definition: post_image.php:25
Modify field settings by extending this class.
gravityview_css_url($css_file= '', $dir_path= '')
Functions that don&#39;t require GravityView or Gravity Forms API access but are used in the plugin to ex...
get_email_footer($email_footer= '', $is_html=true, $email_data=array())
Get the footer for Entry Note emails.
static send_email($from, $to, $bcc, $reply_to, $subject, $message, $from_name= '', $message_format= 'html', $attachments= '', $entry=false, $notification=false)
Send email using GFCommon::send_email()
static has_cap($caps= '', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()
enqueue_scripts()
Enqueue, localize field scripts and styles.
maybe_delete_notes()
Possibly delete notes, if request is proper.
maybe_send_entry_notes($note=false, $entry, $data)
If note has an email to send, and the user has the right caps, send it.
static get_add_note_part()
Get the Add Note form HTML.
add_hooks()
Add AJAX hooks, [gv_note_add] shortcode, and template loading paths.
register_scripts()
Register scripts and styles used by the Notes field.
add_template_path($file_paths)
Include this extension templates path.
field_options($field_options, $template_id, $field_id, $context, $input_type)
static get_note_email_fields($entry_slug= '')
Generate a HTML dropdown of email values based on email fields from the current form.
static display_note($note, $show_delete=false)
Generate HTML output for a single note.
maybe_add_note()
Verify permissions, check if $_POST is set and as expected.
$field_id
Definition: time.php:17
if(!function_exists( 'gravityview_sanitize_html_class')) gravityview_strip_whitespace($string)
Replace multiple newlines, tabs, and spaces with a single space.
add_note($entry, $data)
Add a note.
gravityview_get_entry($entry_slug, $force_allow_ids=false, $check_entry_display=true)
Return a single entry object.
static get_note($note_id)
Get a single note by note ID.
$visibility_settings
Definition: notes.php:11
global $post
static get_note_emails_array()
Get array of emails addresses from the stored entry.
add_entry_default_field($entry_default_fields, $form, $zone)
Add Entry Notes to the Add Field picker in Edit View.
static getInstance($passed_post=NULL)
static add_note($lead_id, $user_id, $user_name, $note= '', $note_type= 'gravityview')
Alias for GFFormsModel::add_note() with default note_type of &#39;gravityview&#39;.
static strings($key= '')
Get strings used by the Entry Notes field.
static delete_notes($note_ids=array())
Delete an array of notes Alias for GFFormsModel::delete_notes()
$entry_slug
Definition: notes.php:30
$entry
Definition: notes.php:27
gv_map_deep($value, $callback)
Maps a function to all non-iterable elements of an array or an object.
static get_entry_slug($id_or_string, $entry=array())
Get the entry slug for the entry.
Definition: class-api.php:567
$strings
Definition: notes.php:29
if(empty($field_settings['content'])) $content
Definition: custom.php:37
$show_delete
Definition: notes.php:33