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