Completed
Push — master ( 022e44...5d3455 )
by Zack
08:11 queued 31s
created

GravityView_Entry_Notes   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 115
rs 10
wmc 10
lcom 0
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A add_hooks() 0 3 1
A add_note() 0 23 1
A delete_note() 0 3 1
A delete_notes() 0 8 2
A get_notes() 0 13 1
A filter_avatar() 0 8 3
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 7 and the first side effect is on line 123.

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.

Loading history...
2
3
/**
4
 * Class GravityView_Entry_Notes
5
 * @since 1.15
6
 */
7
class GravityView_Entry_Notes {
8
9
	/**
10
	 * GravityView_Entry_Notes constructor.
11
	 */
12
	public function __construct() {
13
		$this->add_hooks();
14
	}
15
16
	/**
17
	 * @since 1.15
18
	 */
19
	private function add_hooks() {
20
		add_filter( 'gform_notes_avatar', array( 'GravityView_Entry_Notes', 'filter_avatar' ), 10, 2 );
21
	}
22
23
24
	/**
25
	 * Alias for GFFormsModel::add_note() with default note_type of 'gravityview'
26
	 * @see GFFormsModel::add_note()
27
	 * @since 1.15
28
	 * @param int $lead_id ID of the Entry
29
	 * @param int $user_id ID of the user creating the note
30
	 * @param string $user_name User name of the user creating the note
31
	 * @param string $note Note content.
32
	 * @param string $note_type Type of note. Default: `gravityview`
33
	 */
34
	public static function add_note( $lead_id, $user_id, $user_name, $note = '', $note_type = 'gravityview' ) {
35
36
		$default_note = array(
37
			'lead_id' => 0,
38
			'user_id' => 0,
39
			'user_name' => '',
40
			'note' => '',
41
			'note_type' => 'gravityview',
42
		);
43
44
		/**
45
		 * @filter `gravityview/entry_notes/add_note` Modify note values before added using GFFormsModel::add_note()
46
		 * @see GFFormsModel::add_note
47
		 * @since 1.15.2
48
		 * @param array $note Array with `lead_id`, `user_id`, `user_name`, `note`, and `note_type` key value pairs
49
		 */
50
		$note = apply_filters( 'gravityview/entry_notes/add_note', compact( 'lead_id', 'user_id', 'user_name', 'note', 'note_type' ) );
51
52
		// Make sure the keys are all set
53
		$note = wp_parse_args( $note, $default_note );
54
55
		GFFormsModel::add_note( intval( $note['lead_id'] ), intval( $note['user_id'] ), esc_attr( $note['user_name'] ), $note['note'], esc_attr( $note['note_type'] ) );
56
	}
57
58
	/**
59
	 * Alias for GFFormsModel::delete_note()
60
	 * @see GFFormsModel::delete_note()
61
	 * @param int $note_id Entry note ID
62
	 */
63
	public static function delete_note( $note_id ) {
64
		GFFormsModel::delete_note( $note_id );
65
	}
66
67
	/**
68
	 * Delete an array of notes
69
	 * Alias for GFFormsModel::delete_notes()
70
	 * @todo Write more efficient delete note method using SQL
71
	 * @param int[] $note_ids Array of entry note ids
72
	 */
73
	public static function delete_notes( $note_ids = array() ) {
74
75
		if( !is_array( $note_ids ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
76
			do_action( 'gravityview_log_error', __METHOD__ . ' - Note IDs not an array', $note_ids );
77
		}
78
79
		GFFormsModel::delete_notes( $note_ids );
80
	}
81
82
	/**
83
	 * Alias for GFFormsModel::get_lead_notes()
84
	 *
85
	 * @see GFFormsModel::get_lead_notes
86
	 * @param int $entry_id Entry to get notes for
87
	 *
88
	 * @return stdClass[] Integer-keyed array of note objects
89
	 */
90
	public static function get_notes( $entry_id ) {
91
		$notes = GFFormsModel::get_lead_notes( $entry_id );
92
93
		/**
94
		 * @filter `gravityview/entry_notes/get_notes` Modify the notes array for an entry
95
		 * @since 1.15
96
		 * @param stdClass[] $notes Integer-keyed array of note objects
97
		 * @param int $entry_id Entry to get notes for
98
		 */
99
		$notes = apply_filters( 'gravityview/entry_notes/get_notes', $notes, $entry_id );
100
101
		return $notes;
102
	}
103
104
	/**
105
	 * Use the GravityView avatar for notes created by GravityView
106
	 * 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' ) );`
107
	 * @since 1.15
108
	 * @param string $avatar Avatar image, if available. 48px x 48px by default.
109
	 * @param object $note Note object with id, user_id, date_created, value, note_type, user_name, user_email vars
110
	 * @return string Possibly-modified avatar
111
	 */
112
	public static function filter_avatar( $avatar = '', $note ) {
113
114
		if( 'gravityview' === $note->note_type && -1 === (int)$note->user_id ) {
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
115
			$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 ) ) );
0 ignored issues
show
introduced by
Expected 1 space after "="; 2 found
Loading history...
116
		}
117
118
		return $avatar;
119
	}
120
121
}
122
123
new GravityView_Entry_Notes;