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