GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-entry-list.php
Go to the documentation of this file.
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  */
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  */
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  function __construct( $entries = array(), $post_id = 0, $form = array(), $link_format = '', $after_link = '', $context = '', $template_context = null, $view_id = 0 ) {
99  $this->entries = $entries;
100  $this->post_id = $post_id;
101  $this->form = $form;
102  $this->link_format = $link_format;
103  $this->after_link = $after_link;
104  $this->context = $context;
105  $this->template_context = $template_context;
106  $this->view_id = $view_id;
107  $this->empty_message = function_exists( 'gv_no_results' ) ? gv_no_results( $template_context ) : __( 'No entries match your request.', 'gk-gravityview' );
108  }
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  */
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  public function output() {
177 
178  $output = $this->get_output();
179 
180  echo $output;
181 
182  return $output;
183  }
184 
185  /**
186  * Get the HTML output
187  *
188  * @return string HTML output for entry list
189  */
190  public function get_output() {
191 
192  // No Entries
193  if( empty( $this->entries ) ) {
194  return '<div class="gv-no-results">'.$this->empty_message.'</div>';
195  }
196 
197  $output = '';
198 
199  if ( $this->template_context instanceof \GV\Template_Context ) {
200  $current_entry = $this->template_context->entry->as_entry();
201  } else {
202  $current_entry = GravityView_View::getInstance()->getCurrentEntry();
203  }
204 
205  $output .= '<'. $this->wrapper_tag .'>';
206 
207  foreach( $this->entries as $entry ) {
208 
209  if ( $entry instanceof \GV\Entry ) {
210  $entry = $entry->as_entry();
211  }
212 
213  if( $this->skip_entry( $entry, $current_entry ) ) {
214  continue;
215  }
216 
217  $output .= $this->get_item_output( $entry );
218  }
219 
220  $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  $output = apply_filters( 'gravityview/widget/recent-entries/output', $output, $this );
228 
229  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  private function skip_entry( $entry, $current_entry ) {
241 
242  // If skip entry is off, or there's no current entry, return false
243  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  $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  if( $entry['id'] === $current_entry_id ) {
252  return true;
253  }
254 
255  // Otherwise, return false
256  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  private function get_item_output( $entry ) {
273 
274  $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 string $link The link.
279  * @param array $entry The entry.
280  * @param \GravityView_Entry_List $this The current entry list object.
281  */
282  $link = apply_filters( 'gravityview/entry-list/link', $link, $entry, $this );
283 
284  $item_output = gravityview_get_link( $link, $this->link_format );
285 
286  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  $after_link = apply_filters( 'gravityview/entry-list/after-link', '<div>'.$this->after_link.'</div>', $entry, $this );
296 
297  $item_output .= $after_link;
298  }
299 
300  $item_output = GravityView_API::replace_variables( $item_output, $this->form, $entry );
301 
302  $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  $item_output = apply_filters( 'gravityview/entry-list/item', $item_output, $entry, $this );
312 
313  return $item_output;
314  }
315 
316 }
gv_no_results( $wpautop=true, $context=null)
Definition: class-api.php:942
set_after_link( $after_link)
set_context( $context)
Set the context in which this entry list is being displayed.
set_post_id( $post_id)
static getInstance( $passed_post=NULL)
$skip_current_entry
If this file is called directly, abort.
skip_entry( $entry, $current_entry)
Should the current entry be skipped while showing the list of entries?
$wrapper_tag
gravityview_get_link( $href='', $anchor_text='', $atts=array())
Generate an HTML anchor tag with a list of supported attributes.
if( $add_query_args) $link
__construct( $entries=array(), $post_id=0, $form=array(), $link_format='', $after_link='', $context='', $template_context=null, $view_id=0)
$empty_message
set_wrapper_tag( $wrapper_tag)
$form
set_link_format( $link_format)
set_empty_message( $empty_message)
Set the message when there are no entries to display.
$link_format
$after_link
$item_tag
$view_id
static replace_variables( $text, $form=array(), $entry=array(), $url_encode=false, $esc_html=true, $nl2br=true, $format='html', $aux_data=array())
Alias for GravityView_Merge_Tags::replace_variables()
Definition: class-api.php:118
$entries
get_item_output( $entry)
Get the output for a specific entry.
set_skip_current_entry( $skip_current_entry)
$post_id
$context
set_item_tag( $item_tag)
get_output()
Get the HTML output.
$entry
Definition: notes.php:27
static entry_link( $entry, $post_id=NULL, $add_directory_args=true, $view_id=0)
return href for single entry
Definition: class-api.php:656
$template_context
output()
Echo the output generated by get_output()