GravityView  1.22.6
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  * @param array $entries
73  * @param int $post_id
74  * @param array $form
75  * @param string $link_format
76  * @param string $after_link
77  */
78  function __construct( $entries = array(), $post_id = 0, $form = array(), $link_format = '', $after_link = '', $context = '' ) {
79 
80  $this->entries = $entries;
81  $this->post_id = $post_id;
82  $this->form = $form;
83  $this->link_format = $link_format;
84  $this->after_link = $after_link;
85  $this->context = $context;
86  $this->empty_message = function_exists( 'gv_no_results' ) ? gv_no_results() : __('No entries match your request.', 'gravityview');
87  }
88 
89  /**
90  * @param int $post_id
91  */
92  public function set_post_id( $post_id ) {
93  $this->post_id = $post_id;
94  }
95 
96  /**
97  * @param string $link_format
98  */
99  public function set_link_format( $link_format ) {
100  $this->link_format = $link_format;
101  }
102 
103  /**
104  * @param boolean $skip_current_entry
105  */
107  $this->skip_current_entry = (bool)$skip_current_entry;
108  }
109 
110  /**
111  * @param string $after_link
112  */
113  public function set_after_link( $after_link ) {
114  $this->after_link = $after_link;
115  }
116 
117  /**
118  * Set the message when there are no entries to display
119  * @param string $empty_message
120  */
121  public function set_empty_message( $empty_message ) {
122  $this->empty_message = $empty_message;
123  }
124 
125  /**
126  * Set the context in which this entry list is being displayed.
127  * @param string $context
128  */
129  public function set_context( $context ) {
130  $this->context = $context;
131  }
132 
133  /**
134  * @param string $wrapper_tag
135  */
136  public function set_wrapper_tag( $wrapper_tag ) {
137  $this->wrapper_tag = esc_attr( $wrapper_tag );
138  }
139 
140  /**
141  *
142  * @param string $item_tag
143  */
144  public function set_item_tag( $item_tag ) {
145  $this->item_tag = esc_attr( $item_tag );
146  }
147 
148  /**
149  * Echo the output generated by get_output()
150  *
151  * @see get_output()
152  *
153  * @return string HTML output for entry list
154  */
155  public function output() {
156 
157  $output = $this->get_output();
158 
159  echo $output;
160 
161  return $output;
162  }
163 
164  /**
165  * Get the HTML output
166  *
167  * @return string HTML output for entry list
168  */
169  public function get_output() {
170 
171  // No Entries
172  if( empty( $this->entries ) ) {
173  return '<div class="gv-no-results">'.$this->empty_message.'</div>';
174  }
175 
176  $output = '';
177  $current_entry = GravityView_View::getInstance()->getCurrentEntry();
178 
179  $output .= '<'. $this->wrapper_tag .'>';
180 
181  foreach( $this->entries as $entry ) {
182 
183  if( $this->skip_entry( $entry, $current_entry ) ) {
184  continue;
185  }
186 
187  $output .= $this->get_item_output( $entry );
188  }
189 
190  $output .= '</'. $this->wrapper_tag .'>';
191 
192  /**
193  * @filter `gravityview/widget/recent-entries/output` Modify the HTML of the Recent Entries widget output
194  * @param string $output HTML to be displayed
195  * @param GravityView_Entry_List $this The current class instance
196  */
197  $output = apply_filters( 'gravityview/widget/recent-entries/output', $output, $this );
198 
199  return $output;
200  }
201 
202  /**
203  * Should the current entry be skipped while showing the list of entries?
204  *
205  * @param array $entry GF Entry array
206  * @param array|int $current_entry As returned by GravityView_View::getCurrentEntry()
207  *
208  * @return bool True: Skip entry; False: don't skip entry
209  */
210  private function skip_entry( $entry, $current_entry ) {
211 
212  // If skip entry is off, or there's no current entry, return false
213  if( empty( $this->skip_current_entry ) || empty( $current_entry ) ) {
214  return false;
215  }
216 
217  // If in Single or Edit mode, $current_entry will be an array.
218  $current_entry_id = is_array( $current_entry ) ? $current_entry['id'] : $current_entry;
219 
220  // If the entry ID matches the current entry, yes: skip
221  if( $entry['id'] === $current_entry_id ) {
222  return true;
223  }
224 
225  // Otherwise, return false
226  return false;
227  }
228 
229  /**
230  * Get the output for a specific entry
231  *
232  * @param array $entry GF Entry array
233  *
234  * @since 1.7.2
235  *
236  * @uses gravityview_get_link
237  * @uses GravityView_API::entry_link
238  * @uses GravityView_API::replace_variables
239  *
240  * @return string HTML output for the entry
241  */
242  private function get_item_output( $entry ) {
243 
244  $link = GravityView_API::entry_link( $entry, $this->post_id );
245 
246  $item_output = gravityview_get_link( $link, $this->link_format );
247 
248  if( !empty( $this->after_link ) ) {
249 
250  /**
251  * @filter `gravityview/entry-list/after-link` Modify the content displayed after the entry link in an entry list
252  * @since 1.7.2
253  * @param string $item_output The HTML output for the after_link content
254  * @param array $entry Gravity Forms entry array
255  * @param GravityView_Entry_List $this The current class instance
256  */
257  $after_link = apply_filters( 'gravityview/entry-list/after-link', '<div>'.$this->after_link.'</div>', $entry, $this );
258 
259  $item_output .= $after_link;
260  }
261 
262  $item_output = GravityView_API::replace_variables( $item_output, $this->form, $entry );
263 
264  $item_output = '<'. $this->item_tag .'>'. $item_output .'</'. $this->item_tag .'>';
265 
266  /**
267  * @filter `gravityview/entry-list/item` Modify each item's output in an entry list
268  * @since 1.7.2
269  * @param string $item_output The HTML output for the item
270  * @param array $entry Gravity Forms entry array
271  * @param GravityView_Entry_List $this The current class instance
272  */
273  $item_output = apply_filters( 'gravityview/entry-list/item', $item_output, $entry, $this );
274 
275  return $item_output;
276  }
277 
278 }
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.
gv_no_results($wpautop=true)
Definition: class-api.php:833
$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.
static entry_link( $entry, $post_id=NULL, $add_directory_args=true)
return href for single entry
Definition: class-api.php:663
$link_format
$after_link
$item_tag
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:108
$entries
get_item_output( $entry)
Get the output for a specific entry.
set_skip_current_entry( $skip_current_entry)
$post_id
__construct( $entries=array(), $post_id=0, $form=array(), $link_format='', $after_link='', $context='')
$context
set_item_tag( $item_tag)
get_output()
Get the HTML output.
$entry
Definition: notes.php:27
output()
Echo the output generated by get_output()