GravityView  1.22.6
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-recent-entries-widget.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-recent-entries-widget.php
4  */
5 
6 /**
7  * Class GravityView_Recent_Entries_Widget
8  * @since 1.6
9  */
10 class GravityView_Recent_Entries_Widget extends WP_Widget {
11 
12 
13  function __construct( ) {
14 
15  $name = __('GravityView Recent Entries', 'gravityview');
16 
17  $widget_options = array(
18  'description' => __( 'Display the most recent entries for a View', 'gravityview' ),
19  );
20 
21  parent::__construct( 'gv_recent_entries', $name, $widget_options );
22 
23  $this->initialize();
24  }
25 
26  private function initialize() {
27 
28  add_action('admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts') );
29 
30  add_action( 'wp_ajax_gv_get_view_merge_tag_data', array( $this, 'ajax_get_view_merge_tag_data' ) );
31 
32  }
33 
34  /**
35  * When the widget View is changed, update the Merge Tag data
36  *
37  * @since 1.6
38  */
40 
41  if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'gravityview_ajax_widget' ) ) {
42  exit( false );
43  }
44 
45  $form_id = gravityview_get_form_id( $_POST['view_id'] );
46 
47  $form = RGFormsModel::get_form_meta( $form_id );
48 
49  $output = array(
50  'form' => array(
51  'id' => $form['id'],
52  'title' => $form['title'],
53  'fields' => $form['fields'],
54  ),
55  'mergeTags' => GFCommon::get_merge_tags( $form['fields'], '', false ),
56  );
57 
58  echo json_encode( $output );
59 
60  exit;
61  }
62 
63  /**
64  * Enable the merge tags functionality
65  *
66  * @since 1.6
67  */
68  function admin_enqueue_scripts() {
69  global $pagenow;
70 
71  if( $pagenow === 'widgets.php' ) {
72 
73  $script_debug = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';
74 
75  wp_enqueue_script( 'gravityview_widgets', plugins_url('assets/js/admin-widgets'.$script_debug.'.js', GRAVITYVIEW_FILE), array( 'jquery', 'gform_gravityforms' ), GravityView_Plugin::version );
76 
77  wp_localize_script( 'gravityview_widgets', 'GVWidgets', array(
78  'nonce' => wp_create_nonce( 'gravityview_ajax_widget' )
79  ));
80 
81  wp_enqueue_style( 'gravityview_views_styles', plugins_url('assets/css/admin-views.css', GRAVITYVIEW_FILE), array('dashicons' ), GravityView_Plugin::version );
82  }
83 
84  }
85 
86  /**
87  * @since 1.6
88  * @see WP_Widget::widget()
89  *
90  * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
91  * @param array $instance The settings for the particular instance of the widget.
92  */
93  function widget( $args, $instance ) {
94 
95  // Don't have the Customizer render too soon.
96  if( empty( $instance['view_id'] ) ) {
97  return;
98  }
99 
100  $args['id'] = ( isset( $args['id'] ) ) ? $args['id'] : 'gv_recent_entries';
101  $instance['title'] = ( isset( $instance['title'] ) ) ? $instance['title'] : '';
102 
103  $title = apply_filters( 'widget_title', $instance[ 'title' ], $instance, $args['id'] );
104 
105  echo $args['before_widget'];
106 
107  if ( !empty( $title ) ) {
108  echo $args['before_title'] . $title . $args['after_title'];
109  }
110 
111  /**
112  * @action `gravityview/widget/recent-entries/before_widget` Before recent entries are displayed in the WordPress widget
113  * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
114  * @param array $instance The settings for the particular instance of the widget.
115  */
116  do_action( 'gravityview/widget/recent-entries/before_widget', $args, $instance );
117 
118  // Print the entry list
119  echo $this->get_output( $instance );
120 
121  /**
122  * @action `gravityview/widget/recent-entries/after_widget` After recent entries are displayed in the WordPress widget
123  * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
124  * @param array $instance The settings for the particular instance of the widget.
125  */
126  do_action( 'gravityview/widget/recent-entries/after_widget', $args, $instance );
127 
128  echo $args['after_widget'];
129  }
130 
131  /**
132  * Get the HTML output for the entry list.
133  *
134  * @since 1.7.2
135  *
136  * @param array $instance The settings for the particular instance of the widget.
137  *
138  * @return string
139  */
140  private function get_output( $instance ) {
141 
142  $form_id = gravityview_get_form_id( $instance['view_id'] );
143 
145 
146  $entries = $this->get_entries( $instance, $form_id );
147 
148  /**
149  * @since 1.6.1
150  * @var int $entry_link_post_id The ID to use as the parent post for the entry
151  */
152  $entry_link_post_id = ( empty( $instance['error_post_id'] ) && !empty( $instance['post_id'] ) ) ? $instance['post_id'] : $instance['view_id'];
153 
154  /**
155  * Generate list output
156  * @since 1.7.2
157  */
158  $List = new GravityView_Entry_List( $entries, $entry_link_post_id, $form, $instance['link_format'], $instance['after_link'], 'recent-entries-widget' );
159 
160  $output = $List->get_output();
161 
162  /**
163  * Modify the HTML before it's echo'd
164  * @param string $output HTML to be displayed
165  * @param array $instance Widget settings
166  */
167  $output = apply_filters( 'gravityview/widget/recent-entries/output', $output, $instance );
168 
169  return $output;
170  }
171 
172 
173  /**
174  * Get the entries that will be shown in the current widget
175  *
176  * @param array $instance Settings for the current widget
177  *
178  * @return array $entries Multidimensional array of Gravity Forms entries
179  */
180  private function get_entries( $instance, $form_id ) {
181 
182  // Get the settings for the View ID
183  $view_settings = gravityview_get_template_settings( $instance['view_id'] );
184 
185  // Set the context view ID to avoid conflicts with the Advanced Filter extension.
186  $criteria['context_view_id'] = $instance['view_id'];
187 
188  $instance['limit'] = isset( $instance['limit'] ) ? $instance['limit'] : 10;
189  $view_settings['id'] = $instance['view_id'];
190  $view_settings['page_size'] = $instance['limit'];
191 
192  // Prepare paging criteria
193  $criteria['paging'] = array(
194  'offset' => 0,
195  'page_size' => $instance['limit']
196  );
197 
198  // Prepare Search Criteria
199  $criteria['search_criteria'] = array( 'field_filters' => array() );
201  $criteria['search_criteria']['status'] = apply_filters( 'gravityview_status', 'active', $view_settings );
202 
203  /**
204  * Modify the search parameters before the entries are fetched
205  */
206  $criteria = apply_filters('gravityview/widget/recent-entries/criteria', $criteria, $instance, $form_id );
207 
209 
210  return $results;
211  }
212 
213  /**
214  * @since 1.6
215  * @see WP_Widget::update()
216  *
217  * @param array $new_instance Widget form settings after update
218  * @param array $old_instance Widget form settings before update
219  *
220  * @return array Calculated widget settings after processing
221  */
222  public function update( $new_instance, $old_instance ) {
223 
224  $instance = $new_instance;
225 
226  // Force positive number
227  $instance['limit'] = empty( $instance['limit'] ) ? 10 : absint( $instance['limit'] );
228 
229  $instance['view_id'] = intval( $instance['view_id'] );
230 
231  $instance['link_format'] = trim( rtrim( $instance['link_format'] ) );
232 
233  $instance['link_format'] = empty( $instance['link_format'] ) ? $old_instance['link_format'] : $instance['link_format'];
234 
235  $instance['post_id'] = empty( $instance['post_id'] ) ? '' : intval( $instance['post_id'] );
236 
237  $is_valid_embed_id = GravityView_View_Data::is_valid_embed_id( $instance['post_id'], $instance['view_id'] );
238 
239  //check if post_id is a valid post with embedded View
240  $instance['error_post_id'] = is_wp_error( $is_valid_embed_id ) ? $is_valid_embed_id->get_error_message() : NULL;
241 
242  // Share that the widget isn't brand new
243  $instance['updated'] = 1;
244 
245  /**
246  * Modify the updated instance. This will allow for validating any added instance settings externally.
247  *
248  * @param array $instance Calculated widget settings after processing
249  * @param array $new_instance Widget form settings after update
250  * @param array $old_instance Widget form settings before update
251  */
252  $instance = apply_filters( 'gravityview/widget/update', $instance, $new_instance, $old_instance );
253 
254  return $instance;
255  }
256 
257  /**
258  * @since 1.6
259  * @see WP_Widget::form()
260  */
261  public function form( $instance ) {
262 
263  // Set up some default widget settings.
264  $defaults = array(
265  'title' => __('Recent Entries', 'gravityview'),
266  'view_id' => NULL,
267  'post_id' => NULL,
268  'limit' => 10,
269  'link_format' => __('Entry #{entry_id}', 'gravityview'),
270  'after_link' => ''
271  );
272 
273  $instance = wp_parse_args( (array) $instance, $defaults );
274 
275  ?>
276 
277  <!-- Title -->
278  <p>
279  <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'gravityview' ) ?></label>
280  <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
281  </p>
282 
283  <!-- Download -->
284  <?php
285  $args = array(
286  'post_type' => 'gravityview',
287  'posts_per_page' => -1,
288  'post_status' => 'publish',
289  );
290  $views = get_posts( $args );
291 
292  // If there are no views set up yet, we get outta here.
293  if( empty( $views ) ) {
294  echo '<div id="select_gravityview_view"><div class="wrap">' . GravityView_Admin::no_views_text() . '</div></div>';
295  return;
296  }
297 
298  ?>
299 
300  <?php
301  /**
302  * Display errors generated for invalid embed IDs
303  * @see GravityView_View_Data::is_valid_embed_id
304  */
305  if( isset( $instance['updated'] ) && empty( $instance['view_id'] ) ) {
306  ?>
307  <div class="error inline hide-on-view-change">
308  <p><?php esc_html_e('Please select a View to search.', 'gravityview'); ?></p>
309  </div>
310  <?php
311  unset ( $error );
312  }
313  ?>
314 
315  <p>
316  <label for="<?php echo esc_attr( $this->get_field_id( 'view_id' ) ); ?>"><?php esc_html_e('Select a View', 'gravityview'); ?></label>
317  <select class="widefat gv-recent-entries-select-view" name="<?php echo esc_attr( $this->get_field_name( 'view_id' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'view_id' ) ); ?>">
318  <option value=""><?php esc_html_e( '&mdash; Select a View as Entries Source &mdash;', 'gravityview' ); ?></option>
319  <?php
320 
321  foreach( $views as $view ) {
322  $title = empty( $view->post_title ) ? __('(no title)', 'gravityview') : $view->post_title;
323  echo '<option value="'. $view->ID .'"'.selected( absint( $instance['view_id'] ), $view->ID ).'>'. esc_html( sprintf('%s #%d', $title, $view->ID ) ) .'</option>';
324  }
325 
326  ?>
327  </select>
328  </p>
329 
330  <?php
331  /**
332  * Display errors generated for invalid embed IDs
333  * @see GravityView_View_Data::is_valid_embed_id
334  */
335  if( !empty( $instance['error_post_id'] ) ) {
336  ?>
337  <div class="error inline">
338  <p><?php echo $instance['error_post_id']; ?></p>
339  </div>
340  <?php
341  unset ( $error );
342  }
343  ?>
344 
345  <p>
346  <label for="<?php echo $this->get_field_id('post_id'); ?>"><?php esc_html_e( 'If Embedded, Page ID:', 'gravityview' ); ?></label>
347  <input class="code" size="3" id="<?php echo $this->get_field_id('post_id'); ?>" name="<?php echo $this->get_field_name('post_id'); ?>" type="text" value="<?php echo esc_attr( $instance['post_id'] ); ?>" />
348  <span class="howto"><?php
349  esc_html_e('To have a search performed on an embedded View, enter the ID of the post or page where the View is embedded.', 'gravityview' );
350  echo ' '.gravityview_get_link('http://docs.gravityview.co/article/222-the-search-widget', __('Learn more&hellip;', 'gravityview' ), 'target=_blank' );
351  ?></span>
352  </p>
353 
354  <p>
355  <label for="<?php echo $this->get_field_id( 'limit' ); ?>">
356  <span><?php _e( 'Number of entries to show:', 'gravityview' ); ?></span>
357  </label>
358  <input class="code" id="<?php echo $this->get_field_id( 'limit' ); ?>" name="<?php echo $this->get_field_name( 'limit' ); ?>" type="number" value="<?php echo intval( $instance['limit'] ); ?>" size="3" />
359  </p>
360 
361  <p>
362  <label for="<?php echo $this->get_field_id( 'link_format' ); ?>">
363  <span><?php _e( 'Entry link text (required)', 'gravityview' ); ?></span>
364  </label>
365  <input id="<?php echo $this->get_field_id( 'link_format' ); ?>" name="<?php echo $this->get_field_name( 'link_format' ); ?>" type="text" value="<?php echo esc_attr( $instance['link_format'] ); ?>" class="widefat merge-tag-support mt-position-right mt-hide_all_fields" />
366  </p>
367 
368  <p>
369  <label for="<?php echo $this->get_field_id( 'after_link' ); ?>">
370  <span><?php _e( 'Text or HTML to display after the link (optional)', 'gravityview' ); ?></span>
371  </label>
372  <textarea id="<?php echo $this->get_field_id( 'after_link' ); ?>" name="<?php echo $this->get_field_name( 'after_link' ); ?>" rows="5" class="widefat code merge-tag-support mt-position-right mt-hide_all_fields"><?php echo esc_textarea( $instance['after_link'] ); ?></textarea>
373  </p>
374 
375  <?php
376 
377  /**
378  * @action `gravityview_recent_entries_widget_form` Displayed at the bottom of the Recent Entries widget admin form
379  * @param GravityView_Recent_Entries_Widget $this WP_Widget object
380  * @param array $instance Current widget instance
381  */
382  do_action( 'gravityview_recent_entries_widget_form' , $this, $instance );
383 
384  ?>
385 
386  <script>
387  // When the widget is saved or added, refresh the Merge Tags (here for backward compatibility)
388  // WordPress 3.9 added widget-added and widget-updated actions
389  jQuery('#<?php echo $this->get_field_id( 'view_id' ); ?>').trigger( 'change' );
390  </script>
391  <?php }
392 
393 }
If this file is called directly, abort.
static process_search_only_approved( $args, $search_criteria)
Process the approved only search criteria according to the View settings.
gravityview_get_form( $form_id)
Returns the form object for a given Form ID.
gravityview_get_template_settings( $post_id)
Get all the settings for a View.
get_entries( $instance, $form_id)
Get the entries that will be shown in the current widget.
$entries
$view_settings
static is_valid_embed_id( $post_id='', $view_id='', $empty_is_valid=false)
Checks if the passed post id has the passed View id embedded.
Definition: class-data.php:613
$criteria['paging']
Modify the search parameters before the entries are fetched.
Class GravityView_Recent_Entries_Widget.
static no_views_text()
Get text for no views found.
Definition: class-admin.php:65
admin_enqueue_scripts()
Enable the merge tags functionality.
gravityview_get_form_id( $view_id)
Get the connected form ID from a View ID.
ajax_get_view_merge_tag_data()
When the widget View is changed, update the Merge Tag data.
static get_entries( $form_ids=null, $passed_criteria=null, &$total=null)
Retrieve entries given search, sort, paging criteria.
if(empty( $created_by)) $form_id
const GRAVITYVIEW_FILE(! defined( 'ABSPATH'))
Plugin Name: GravityView Plugin URI: https://gravityview.co Description: The best, easiest way to display Gravity Forms entries on your website.
Definition: gravityview.php:26
$title