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