GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-entry-notes.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @package GravityView
4  * @license GPL2+
5  * @since 1.15
6  * @author Katz Web Services, Inc.
7  * @link http://gravityview.co
8  * @copyright Copyright 2016, Katz Web Services, Inc.
9  */
10 
11 /**
12  * Class GravityView_Entry_Notes
13  * @since 1.15
14  */
16 
17  /**
18  * GravityView_Entry_Notes constructor.
19  */
20  public function __construct() {
21  $this->add_hooks();
22  }
23 
24  /**
25  * @since 1.15
26  */
27  private function add_hooks() {
28  add_filter( 'gform_notes_avatar', array( 'GravityView_Entry_Notes', 'filter_avatar' ), 10, 2 );
29  }
30 
31 
32  /**
33  * Alias for GFFormsModel::add_note() with default note_type of 'gravityview'
34  *
35  * @see GFFormsModel::add_note()
36  *
37  * @since 1.15
38  * @since 1.17 Added return value
39  *
40  * @param int $lead_id ID of the Entry
41  * @param int $user_id ID of the user creating the note
42  * @param string $user_name User name of the user creating the note
43  * @param string $note Note content.
44  * @param string $note_type Type of note. Default: `gravityview`
45  *
46  * @return int|WP_Error Note ID, if success. WP_Error with $wpdb->last_error message, if failed.
47  */
48  public static function add_note( $lead_id, $user_id, $user_name, $note = '', $note_type = 'gravityview' ) {
49  global $wpdb;
50 
51  $default_note = array(
52  'lead_id' => 0,
53  'user_id' => 0,
54  'user_name' => '',
55  'note' => '',
56  'note_type' => 'gravityview',
57  );
58 
59  /**
60  * @filter `gravityview/entry_notes/add_note` Modify note values before added using GFFormsModel::add_note()
61  * @see GFFormsModel::add_note
62  * @since 1.15.2
63  * @param array $note Array with `lead_id`, `user_id`, `user_name`, `note`, and `note_type` key value pairs
64  */
65  $note = apply_filters( 'gravityview/entry_notes/add_note', compact( 'lead_id', 'user_id', 'user_name', 'note', 'note_type' ) );
66 
67  // Make sure the keys are all set
68  $note = wp_parse_args( $note, $default_note );
69 
70  GFFormsModel::add_note( intval( $note['lead_id'] ), intval( $note['user_id'] ), esc_attr( $note['user_name'] ), $note['note'], esc_attr( $note['note_type'] ) );
71 
72  // If last_error is empty string, there was no error.
73  if( empty( $wpdb->last_error ) ) {
74  $return = $wpdb->insert_id;
75  } else {
76  $return = new WP_Error( 'gravityview-add-note', $wpdb->last_error );
77  }
78 
79  return $return;
80  }
81 
82  /**
83  * Alias for GFFormsModel::delete_note()
84  * @see GFFormsModel::delete_note()
85  * @param int $note_id Entry note ID
86  */
87  public static function delete_note( $note_id ) {
88  GFFormsModel::delete_note( $note_id );
89  }
90 
91  /**
92  * Delete an array of notes
93  * Alias for GFFormsModel::delete_notes()
94  * @todo Write more efficient delete note method using SQL
95  * @param int[] $note_ids Array of entry note ids
96  */
97  public static function delete_notes( $note_ids = array() ) {
98 
99  if( !is_array( $note_ids ) ) {
100 
101  gravityview()->log->error( 'Note IDs not an array. Not processing delete request.', array( 'data' => $note_ids ) );
102 
103  return;
104  }
105 
106  GFFormsModel::delete_notes( $note_ids );
107  }
108 
109  /**
110  * Alias for GFFormsModel::get_lead_notes()
111  *
112  * @see GFFormsModel::get_lead_notes
113  * @param int $entry_id Entry to get notes for
114  *
115  * @return stdClass[]|null Integer-keyed array of note objects
116  */
117  public static function get_notes( $entry_id ) {
118 
119  $notes = GFFormsModel::get_lead_notes( $entry_id );
120 
121  /**
122  * @filter `gravityview/entry_notes/get_notes` Modify the notes array for an entry
123  * @since 1.15
124  * @param stdClass[]|null $notes Integer-keyed array of note objects
125  * @param int $entry_id Entry to get notes for
126  */
127  $notes = apply_filters( 'gravityview/entry_notes/get_notes', $notes, $entry_id );
128 
129  return $notes;
130  }
131 
132  /**
133  * Get a single note by note ID
134  *
135  * @since 1.17
136  *
137  * @param int $note_id The ID of the note in the `{prefix}_rg_lead_notes` table
138  *
139  * @return object|bool False if not found; note object otherwise.
140  */
141  public static function get_note( $note_id ) {
142  global $wpdb;
143 
144  if ( version_compare( GravityView_GFFormsModel::get_database_version(), '2.3-dev-1', '>=' )
145  && method_exists( 'GFFormsModel', 'get_entry_notes_table_name' ) ) {
146  $notes_table = GFFormsModel::get_entry_notes_table_name();
147  } else {
148  $notes_table = GFFormsModel::get_lead_notes_table_name();
149  }
150 
151  $results = $wpdb->get_results(
152  $wpdb->prepare(
153  " SELECT n.id, n.user_id, n.date_created, n.value, n.note_type, ifnull(u.display_name,n.user_name) as user_name, u.user_email
154  FROM $notes_table n
155  LEFT OUTER JOIN $wpdb->users u ON n.user_id = u.id
156  WHERE n.id=%d", $note_id
157  )
158  );
159 
160  return $results ? $results[0] : false;
161  }
162 
163  /**
164  * Use the GravityView avatar for notes created by GravityView
165  * Note: The function is static so that it's easier to remove the filter: `remove_filter( 'gform_notes_avatar', array( 'GravityView_Entry_Notes', 'filter_avatar' ) );`
166  * @since 1.15
167  * @param string $avatar Avatar image, if available. 48px x 48px by default.
168  * @param object $note Note object with id, user_id, date_created, value, note_type, user_name, user_email vars.
169  * @return string Possibly-modified avatar.
170  */
171  public static function filter_avatar( $avatar = '', $note = null ) {
172 
173  if( 'gravityview' === $note->note_type && -1 === (int)$note->user_id ) {
174  $avatar = sprintf( '<img src="%s" width="48" height="48" alt="GravityView" class="avatar avatar-48 gravityview-avatar" />', esc_url_raw( plugins_url( 'assets/images/floaty-avatar.png', GRAVITYVIEW_FILE ) ) );
175  }
176 
177  return $avatar;
178  }
179 
180 }
181 
static delete_notes( $note_ids=array())
Delete an array of notes Alias for GFFormsModel::delete_notes()
add_hooks()
__construct()
GravityView_Entry_Notes constructor.
static filter_avatar( $avatar='', $note=null)
Use the GravityView avatar for notes created by GravityView Note: The function is static so that it&#39;s...
static get_notes( $entry_id)
Alias for GFFormsModel::get_lead_notes()
const GRAVITYVIEW_FILE
Full path to the GravityView file "GRAVITYVIEW_FILE" "./gravityview.php".
Definition: gravityview.php:40
static delete_note( $note_id)
Alias for GFFormsModel::delete_note()
static get_note( $note_id)
Get a single note by note ID.
Class GravityView_Entry_Notes.
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;.
gravityview()
The main GravityView wrapper function.
static get_database_version()
Make sure the method exists, regardless of GF version.
new GravityView_Entry_Notes
$notes
Definition: notes.php:28