Completed
Push — develop ( 72a6d4...3b1747 )
by Zack
08:01
created

GravityView_Entry_List   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 304
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 60.81%

Importance

Changes 0
Metric Value
dl 0
loc 304
ccs 45
cts 74
cp 0.6081
rs 10
c 0
b 0
f 0
wmc 24
lcom 1
cbo 4

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A set_post_id() 0 3 1
A set_link_format() 0 3 1
A set_skip_current_entry() 0 3 1
A set_after_link() 0 3 1
A set_empty_message() 0 3 1
A set_context() 0 3 1
A set_wrapper_tag() 0 3 1
A set_item_tag() 0 3 1
A output() 0 8 1
B get_output() 0 41 6
A skip_entry() 0 18 5
A get_item_output() 0 43 2
1
<?php
2
3
/** If this file is called directly, abort. */
4
if ( ! defined( 'ABSPATH' ) ) {
5
	die;
6
}
7
8
/**
9
 * Generate linked list output for a list of entries.
10
 *
11
 * @since 1.7.2
12
 */
13
class GravityView_Entry_List {
14
15
	/**
16
	 * @var array
17
	 */
18
	private $entries = array();
19
20
	/**
21
	 * @var int
22
	 */
23
	private $post_id = 0;
24
25
	/**
26
	 * @var array
27
	 */
28
	private $form = array();
29
30
	/**
31
	 * @var string
32
	 */
33
	private $link_format = '';
34
35
	/**
36
	 * HTML or text to display after the link to the entry
37
	 * @var string
38
	 */
39
	private $after_link = '';
40
41
	/**
42
	 * The message when there are no entries to display
43
	 * @var string
44
	 */
45
	private $empty_message = '';
46
47
	/**
48
	 * Whether to skip the entry currently being displayed, if any.
49
	 * @var bool
50
	 */
51
	private $skip_current_entry = true;
52
53
	/**
54
	 * Optional. Set the context for the output to allow for easier filtering output.
55
	 * @var string
56
	 */
57
	private $context = '';
58
59
	/**
60
	 * HTML tag to wrap output inside
61
	 * @var string
62
	 */
63
	private $wrapper_tag = 'ul';
64
65
	/**
66
	 * HTML tag to wrap each entry inside
67
	 * @var string
68
	 */
69
	private $item_tag = 'li';
70
71
	/**
72
	 * The context this list is operating in.
73
	 * @todo Deprecate this class altogether.
74
	 * @since 2.0
75
	 * @var \GV\Template_Context
76
	 */
77
	public $template_context;
78
79
	/**
80
	 * The ID of the View connected to the entries being displayed
81
	 * @since 2.7.2
82
	 * @var int
83
	 */
84
	public $view_id = 0;
85
86
	/**
87
	 * @since 2.0 Added $template_context parameter
88
	 * @since 2.7.2 Added $view_id parameter
89
	 *
90
	 * @param array|GV\Entry[] $entries
91
	 * @param int $post_id
92
	 * @param array $form
93
	 * @param string $link_format
94
	 * @param string $after_link
95
	 * @param \GV\Template_Context $template_context The context
96
	 * @param int|null $view_id View to link to when displaying on a page with multiple Views
97
	 */
98 1
	function __construct( $entries = array(), $post_id = 0, $form = array(), $link_format = '', $after_link = '', $context = '', $template_context = null, $view_id = 0 ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
99 1
		$this->entries = $entries;
100 1
		$this->post_id = $post_id;
101 1
		$this->form = $form;
102 1
		$this->link_format = $link_format;
103 1
		$this->after_link = $after_link;
104 1
		$this->context = $context;
105 1
		$this->template_context = $template_context;
106 1
		$this->view_id = $view_id;
107 1
		$this->empty_message = function_exists( 'gv_no_results' ) ? gv_no_results( $template_context ) : __( 'No entries match your request.', 'gravityview' );
108 1
	}
109
110
	/**
111
	 * @param int $post_id
112
	 */
113
	public function set_post_id( $post_id ) {
114
		$this->post_id = $post_id;
115
	}
116
117
	/**
118
	 * @param string $link_format
119
	 */
120
	public function set_link_format( $link_format ) {
121
		$this->link_format = $link_format;
122
	}
123
124
	/**
125
	 * @param boolean $skip_current_entry
126
	 */
127
	public function set_skip_current_entry( $skip_current_entry ) {
128
		$this->skip_current_entry = (bool)$skip_current_entry;
129
	}
130
131
	/**
132
	 * @param string $after_link
133
	 */
134
	public function set_after_link( $after_link ) {
135
		$this->after_link = $after_link;
136
	}
137
138
	/**
139
	 * Set the message when there are no entries to display
140
	 * @param string $empty_message
141
	 */
142
	public function set_empty_message( $empty_message ) {
143
		$this->empty_message = $empty_message;
144
	}
145
146
	/**
147
	 * Set the context in which this entry list is being displayed.
148
	 * @param string $context
149
	 */
150
	public function set_context( $context ) {
151
		$this->context = $context;
152
	}
153
154
	/**
155
	 * @param string $wrapper_tag
156
	 */
157
	public function set_wrapper_tag( $wrapper_tag ) {
158
		$this->wrapper_tag = esc_attr( $wrapper_tag );
159
	}
160
161
	/**
162
	 *
163
	 * @param string $item_tag
164
	 */
165
	public function set_item_tag( $item_tag ) {
166
		$this->item_tag = esc_attr( $item_tag );
167
	}
168
169
	/**
170
	 * Echo the output generated by get_output()
171
	 *
172
	 * @see get_output()
173
	 *
174
	 * @return string HTML output for entry list
175
	 */
176 1
	public function output() {
177
178 1
		$output = $this->get_output();
179
180 1
		echo $output;
181
182 1
		return $output;
183
	}
184
185
	/**
186
	 * Get the HTML output
187
	 *
188
	 * @return string HTML output for entry list
189
	 */
190 1
	public function get_output() {
191
192
		// No Entries
193 1
		if( empty( $this->entries ) ) {
194 1
			return '<div class="gv-no-results">'.$this->empty_message.'</div>';
195
		}
196
197 1
		$output = '';
198
199 1
		if ( $this->template_context instanceof \GV\Template_Context ) {
200 1
			$current_entry = $this->template_context->entry->as_entry();
201
		} else {
202
			$current_entry = GravityView_View::getInstance()->getCurrentEntry();
203
		}
204
205 1
		$output .= '<'. $this->wrapper_tag .'>';
206
207 1
		foreach( $this->entries as $entry ) {
208
209 1
			if ( $entry instanceof \GV\Entry ) {
210
				$entry = $entry->as_entry();
211
			}
212
213 1
			if( $this->skip_entry( $entry, $current_entry ) ) {
214
				continue;
215
			}
216
217 1
			$output .= $this->get_item_output( $entry );
218
		}
219
220 1
		$output .= '</'. $this->wrapper_tag .'>';
221
222
		/**
223
		 * @filter `gravityview/widget/recent-entries/output` Modify the HTML of the Recent Entries widget output
224
		 * @param string $output HTML to be displayed
225
		 * @param GravityView_Entry_List $this The current class instance
226
		 */
227 1
		$output = apply_filters( 'gravityview/widget/recent-entries/output', $output, $this );
228
229 1
		return $output;
230
	}
231
232
	/**
233
	 * Should the current entry be skipped while showing the list of entries?
234
	 *
235
	 * @param array $entry GF Entry array
236
	 * @param array|int  $current_entry As returned by GravityView_View::getCurrentEntry()
237
	 *
238
	 * @return bool True: Skip entry; False: don't skip entry
239
	 */
240 1
	private function skip_entry( $entry, $current_entry ) {
241
242
		// If skip entry is off, or there's no current entry, return false
243 1
		if( empty( $this->skip_current_entry ) || empty( $current_entry ) ) {
244
			return false;
245
		}
246
247
		// If in Single or Edit mode, $current_entry will be an array.
248 1
		$current_entry_id = is_array( $current_entry ) ? $current_entry['id'] : $current_entry;
249
250
		// If the entry ID matches the current entry, yes: skip
251 1
		if( $entry['id'] === $current_entry_id ) {
252
			return true;
253
		}
254
255
		// Otherwise, return false
256 1
		return false;
257
	}
258
259
	/**
260
	 * Get the output for a specific entry
261
	 *
262
	 * @param array $entry GF Entry array
263
	 *
264
	 * @since 1.7.2
265
	 *
266
	 * @uses gravityview_get_link
267
	 * @uses GravityView_API::entry_link
268
	 * @uses GravityView_API::replace_variables
269
	 *
270
	 * @return string HTML output for the entry
271
	 */
272 1
	private function get_item_output( $entry ) {
273
274 1
		$link = GravityView_API::entry_link( $entry, $this->post_id, true, $this->view_id );
275
276
		/**
277
		 * @filter `gravityview/entry-list/link` The link to this other entry now.
278
		 * @param[in,out] string $link The link.
279
		 * @param array $entry The entry.
280
		 * @param \GravityView_Entry_List $this The current entry list object.
281
		 */
282 1
		$link = apply_filters( 'gravityview/entry-list/link', $link, $entry, $this );
283
284 1
		$item_output = gravityview_get_link( $link, $this->link_format );
285
286 1
		if( !empty( $this->after_link ) ) {
287
288
			/**
289
			 * @filter `gravityview/entry-list/after-link` Modify the content displayed after the entry link in an entry list
290
			 * @since 1.7.2
291
			 * @param string $item_output The HTML output for the after_link content
292
			 * @param array $entry Gravity Forms entry array
293
			 * @param GravityView_Entry_List $this The current class instance
294
			 */
295 1
			$after_link = apply_filters( 'gravityview/entry-list/after-link', '<div>'.$this->after_link.'</div>', $entry, $this );
296
297 1
			$item_output .= $after_link;
298
		}
299
300 1
		$item_output = GravityView_API::replace_variables( $item_output, $this->form, $entry );
301
302 1
		$item_output = '<'. $this->item_tag .'>'. $item_output .'</'. $this->item_tag .'>';
303
304
		/**
305
		 * @filter `gravityview/entry-list/item` Modify each item's output in an entry list
306
		 * @since 1.7.2
307
		 * @param string $item_output The HTML output for the item
308
		 * @param array $entry Gravity Forms entry array
309
		 * @param GravityView_Entry_List $this The current class instance
310
		 */
311 1
		$item_output = apply_filters( 'gravityview/entry-list/item', $item_output, $entry, $this );
312
313 1
		return $item_output;
314
	}
315
316
}
317