| 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 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class GravityView_Entry_Notes { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  | 			do_action( 'gravityview_log_error', __METHOD__ . ' - Note IDs not an array. Not processing delete request.', $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[] Integer-keyed array of note objects | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	public static function get_notes( $entry_id ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		$notes = GFFormsModel::get_lead_notes( $entry_id ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 		 * @filter `gravityview/entry_notes/get_notes` Modify the notes array for an entry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		 * @since 1.15 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		 * @param stdClass[] $notes Integer-keyed array of note objects | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 		 * @param int $entry_id Entry to get notes for | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 		$notes = apply_filters( 'gravityview/entry_notes/get_notes', $notes, $entry_id ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		return $notes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	 * Get a single note by note ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	 * @since 1.17 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	 * @param int $note_id The ID of the note in the `{prefix}_rg_lead_notes` table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	 * @return object|bool False if not found; note object otherwise. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  | 	public static function get_note( $note_id ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  | 		global $wpdb; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  | 		$notes_table = GFFormsModel::get_lead_notes_table_name(); | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  | 		$results = $wpdb->get_results( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  | 			$wpdb->prepare( | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  | 				" 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 | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  | 	              FROM $notes_table n | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  | 	              LEFT OUTER JOIN $wpdb->users u ON n.user_id = u.id | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  | 	              WHERE n.id=%d", $note_id | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  | 			) | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  | 		); | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  | 		return $results ? $results[0] : false; | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	 * Use the GravityView avatar for notes created by GravityView | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	 * 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' ) );` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	 * @since 1.15 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	 * @param string $avatar Avatar image, if available. 48px x 48px by default. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 	 * @param object $note Note object with id, user_id, date_created, value, note_type, user_name, user_email vars | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	 * @return string Possibly-modified avatar | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 	public static function filter_avatar( $avatar = '', $note ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 		if( 'gravityview' === $note->note_type && -1 === (int)$note->user_id ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 			$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 ) ) ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 		return $avatar; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 176 |  |  | new GravityView_Entry_Notes; | 
            
                        
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.