GravityView  2.10.1
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', null, $instance['view_id'] );
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  * @param string $form_id Form ID int, as string
178  *
179  * @return array|GV\Entry[] $entries Multidimensional array of Gravity Forms entries or GravityView Entry objects
180  */
181  private function get_entries( $instance, $form_id ) {
182 
183  $view = \GV\View::by_id( $instance['view_id'] );
184 
185  $limit = isset( $instance['limit'] ) ? $instance['limit'] : 10;
186 
187  $view->settings->set( 'page_size', $limit );
188 
189  $entries = $view->get_entries();
190 
191  return $entries->all();
192  }
193 
194  /**
195  * @since 1.6
196  * @see WP_Widget::update()
197  *
198  * @param array $new_instance Widget form settings after update
199  * @param array $old_instance Widget form settings before update
200  *
201  * @return array Calculated widget settings after processing
202  */
203  public function update( $new_instance, $old_instance ) {
204 
205  $instance = $new_instance;
206 
207  // Force positive number
208  $instance['limit'] = empty( $instance['limit'] ) ? 10 : absint( $instance['limit'] );
209 
210  $instance['view_id'] = intval( $instance['view_id'] );
211 
212  $instance['link_format'] = trim( rtrim( $instance['link_format'] ) );
213 
214  $instance['link_format'] = empty( $instance['link_format'] ) ? $old_instance['link_format'] : $instance['link_format'];
215 
216  $instance['post_id'] = empty( $instance['post_id'] ) ? '' : intval( $instance['post_id'] );
217 
218  $is_valid_embed_id = GravityView_View_Data::is_valid_embed_id( $instance['post_id'], $instance['view_id'] );
219 
220  //check if post_id is a valid post with embedded View
221  $instance['error_post_id'] = is_wp_error( $is_valid_embed_id ) ? $is_valid_embed_id->get_error_message() : NULL;
222 
223  // Share that the widget isn't brand new
224  $instance['updated'] = 1;
225 
226  /**
227  * Modify the updated instance. This will allow for validating any added instance settings externally.
228  *
229  * @param array $instance Calculated widget settings after processing
230  * @param array $new_instance Widget form settings after update
231  * @param array $old_instance Widget form settings before update
232  */
233  $instance = apply_filters( 'gravityview/widget/update', $instance, $new_instance, $old_instance );
234 
235  return $instance;
236  }
237 
238  /**
239  * @since 1.6
240  * @see WP_Widget::form()
241  */
242  public function form( $instance ) {
243 
244  // Set up some default widget settings.
245  $defaults = array(
246  'title' => __('Recent Entries', 'gravityview'),
247  'view_id' => NULL,
248  'post_id' => NULL,
249  'limit' => 10,
250  'link_format' => __('Entry #{entry_id}', 'gravityview'),
251  'after_link' => ''
252  );
253 
254  $instance = wp_parse_args( (array) $instance, $defaults );
255 
256  ?>
257 
258  <!-- Title -->
259  <p>
260  <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'gravityview' ) ?></label>
261  <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'] ); ?>" />
262  </p>
263 
264  <!-- Download -->
265  <?php
266  $args = array(
267  'post_type' => 'gravityview',
268  'posts_per_page' => -1,
269  'post_status' => 'publish',
270  );
271  $views = get_posts( $args );
272 
273  // If there are no views set up yet, we get outta here.
274  if( empty( $views ) ) {
275  echo '<div id="select_gravityview_view"><div class="wrap">' . GravityView_Admin::no_views_text() . '</div></div>';
276  return;
277  }
278 
279  ?>
280 
281  <?php
282  /**
283  * Display errors generated for invalid embed IDs
284  * @see GravityView_View_Data::is_valid_embed_id
285  */
286  if( isset( $instance['updated'] ) && empty( $instance['view_id'] ) ) {
287  ?>
288  <div class="error inline hide-on-view-change">
289  <p><?php esc_html_e('Please select a View to search.', 'gravityview'); ?></p>
290  </div>
291  <?php
292  unset ( $error );
293  }
294  ?>
295 
296  <p>
297  <label for="<?php echo esc_attr( $this->get_field_id( 'view_id' ) ); ?>"><?php esc_html_e('Select a View', 'gravityview'); ?></label>
298  <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' ) ); ?>">
299  <option value=""><?php esc_html_e( '&mdash; Select a View as Entries Source &mdash;', 'gravityview' ); ?></option>
300  <?php
301 
302  foreach( $views as $view ) {
303  $title = empty( $view->post_title ) ? __('(no title)', 'gravityview') : $view->post_title;
304  echo '<option value="'. $view->ID .'"'.selected( absint( $instance['view_id'] ), $view->ID ).'>'. esc_html( sprintf('%s #%d', $title, $view->ID ) ) .'</option>';
305  }
306 
307  ?>
308  </select>
309  </p>
310 
311  <?php
312  /**
313  * Display errors generated for invalid embed IDs
314  * @see GravityView_View_Data::is_valid_embed_id
315  */
316  if( !empty( $instance['error_post_id'] ) ) {
317  ?>
318  <div class="error inline">
319  <p><?php echo $instance['error_post_id']; ?></p>
320  </div>
321  <?php
322  unset ( $error );
323  }
324  ?>
325 
326  <p>
327  <label for="<?php echo $this->get_field_id('post_id'); ?>"><?php esc_html_e( 'If Embedded, Page ID:', 'gravityview' ); ?></label>
328  <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'] ); ?>" />
329  <span class="howto"><?php
330  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' );
331  echo ' '.gravityview_get_link('https://docs.gravityview.co/article/222-the-search-widget', __('Learn more&hellip;', 'gravityview' ), 'target=_blank' );
332  ?></span>
333  </p>
334 
335  <p>
336  <label for="<?php echo $this->get_field_id( 'limit' ); ?>">
337  <span><?php _e( 'Number of entries to show:', 'gravityview' ); ?></span>
338  </label>
339  <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" />
340  </p>
341 
342  <p>
343  <label for="<?php echo $this->get_field_id( 'link_format' ); ?>">
344  <span><?php _e( 'Entry link text (required)', 'gravityview' ); ?></span>
345  </label>
346  <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" />
347  </p>
348 
349  <p>
350  <label for="<?php echo $this->get_field_id( 'after_link' ); ?>">
351  <span><?php _e( 'Text or HTML to display after the link (optional)', 'gravityview' ); ?></span>
352  </label>
353  <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>
354  </p>
355 
356  <?php
357 
358  /**
359  * @action `gravityview_recent_entries_widget_form` Displayed at the bottom of the Recent Entries widget admin form
360  * @param GravityView_Recent_Entries_Widget $this WP_Widget object
361  * @param array $instance Current widget instance
362  */
363  do_action( 'gravityview_recent_entries_widget_form' , $this, $instance );
364 
365  ?>
366 
367  <script>
368  // When the widget is saved or added, refresh the Merge Tags (here for backward compatibility)
369  // WordPress 3.9 added widget-added and widget-updated actions
370  jQuery('#<?php echo $this->get_field_id( 'view_id' ); ?>').trigger( 'change' );
371  </script>
372  <?php }
373 
374 }
If this file is called directly, abort.
gravityview_get_form( $form_id)
Returns the form object for a given Form ID.
get_entries( $instance, $form_id)
Get the entries that will be shown in the current widget.
$entries
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:286
if(gravityview() ->plugin->is_GF_25()) $form
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.
static by_id( $post_id)
Construct a instance from a post ID.
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.
const GRAVITYVIEW_FILE
Full path to the GravityView file "GRAVITYVIEW_FILE" "./gravityview.php".
Definition: gravityview.php:31
if(empty( $created_by)) $form_id
$title