GravityView  2.17
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', 'gk-gravityview');
16 
17  $widget_options = array(
18  'description' => __( 'Display the most recent entries for a View', 'gk-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' ), GV_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' ), GV_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  if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
101  return false;
102  }
103 
104  $args['id'] = ( isset( $args['id'] ) ) ? $args['id'] : 'gv_recent_entries';
105  $instance['title'] = ( isset( $instance['title'] ) ) ? $instance['title'] : '';
106 
107  $title = apply_filters( 'widget_title', $instance[ 'title' ], $instance, $args['id'] );
108 
109  echo $args['before_widget'];
110 
111  if ( !empty( $title ) ) {
112  echo $args['before_title'] . $title . $args['after_title'];
113  }
114 
115  /**
116  * @action `gravityview/widget/recent-entries/before_widget` Before recent entries are displayed in the WordPress widget
117  * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
118  * @param array $instance The settings for the particular instance of the widget.
119  */
120  do_action( 'gravityview/widget/recent-entries/before_widget', $args, $instance );
121 
122  // Print the entry list
123  echo $this->get_output( $instance );
124 
125  /**
126  * @action `gravityview/widget/recent-entries/after_widget` After recent entries are displayed in the WordPress widget
127  * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
128  * @param array $instance The settings for the particular instance of the widget.
129  */
130  do_action( 'gravityview/widget/recent-entries/after_widget', $args, $instance );
131 
132  echo $args['after_widget'];
133  }
134 
135  /**
136  * Get the HTML output for the entry list.
137  *
138  * @since 1.7.2
139  *
140  * @param array $instance The settings for the particular instance of the widget.
141  *
142  * @return string
143  */
144  private function get_output( $instance ) {
145 
146  $form_id = gravityview_get_form_id( $instance['view_id'] );
147 
149 
150  $entries = $this->get_entries( $instance, $form_id );
151 
152  /**
153  * @since 1.6.1
154  * @var int $entry_link_post_id The ID to use as the parent post for the entry
155  */
156  $entry_link_post_id = ( empty( $instance['error_post_id'] ) && !empty( $instance['post_id'] ) ) ? $instance['post_id'] : $instance['view_id'];
157 
158  /**
159  * Generate list output
160  * @since 1.7.2
161  */
162  $List = new GravityView_Entry_List( $entries, $entry_link_post_id, $form, $instance['link_format'], $instance['after_link'], 'recent-entries-widget', null, $instance['view_id'] );
163 
164  $output = $List->get_output();
165 
166  /**
167  * Modify the HTML before it's echo'd
168  * @param string $output HTML to be displayed
169  * @param array $instance Widget settings
170  */
171  $output = apply_filters( 'gravityview/widget/recent-entries/output', $output, $instance );
172 
173  return $output;
174  }
175 
176 
177  /**
178  * Get the entries that will be shown in the current widget
179  *
180  * @param array $instance Settings for the current widget
181  * @param string $form_id Form ID int, as string
182  *
183  * @return array|GV\Entry[] $entries Multidimensional array of Gravity Forms entries or GravityView Entry objects
184  */
185  private function get_entries( $instance, $form_id ) {
186 
187  $view = \GV\View::by_id( $instance['view_id'] );
188 
189  $limit = isset( $instance['limit'] ) ? $instance['limit'] : 10;
190 
191  $view->settings->set( 'page_size', $limit );
192 
193  $entries = $view->get_entries();
194 
195  return $entries->all();
196  }
197 
198  /**
199  * @since 1.6
200  * @see WP_Widget::update()
201  *
202  * @param array $new_instance Widget form settings after update
203  * @param array $old_instance Widget form settings before update
204  *
205  * @return array Calculated widget settings after processing
206  */
207  public function update( $new_instance, $old_instance ) {
208 
209  $instance = $new_instance;
210 
211  // Force positive number
212  $instance['limit'] = empty( $instance['limit'] ) ? 10 : absint( $instance['limit'] );
213 
214  $instance['view_id'] = intval( $instance['view_id'] );
215 
216  $instance['link_format'] = trim( rtrim( $instance['link_format'] ) );
217 
218  $instance['link_format'] = empty( $instance['link_format'] ) ? $old_instance['link_format'] : $instance['link_format'];
219 
220  $instance['post_id'] = empty( $instance['post_id'] ) ? '' : intval( $instance['post_id'] );
221 
222  $is_valid_embed_id = GravityView_View_Data::is_valid_embed_id( $instance['post_id'], $instance['view_id'], true );
223 
224  //check if post_id is a valid post with embedded View
225  $instance['error_post_id'] = is_wp_error( $is_valid_embed_id ) ? $is_valid_embed_id->get_error_message() : NULL;
226 
227  // Share that the widget isn't brand new
228  $instance['updated'] = 1;
229 
230  /**
231  * Modify the updated instance. This will allow for validating any added instance settings externally.
232  *
233  * @param array $instance Calculated widget settings after processing
234  * @param array $new_instance Widget form settings after update
235  * @param array $old_instance Widget form settings before update
236  */
237  $instance = apply_filters( 'gravityview/widget/update', $instance, $new_instance, $old_instance );
238 
239  return $instance;
240  }
241 
242  /**
243  * @since 1.6
244  * @see WP_Widget::form()
245  */
246  public function form( $instance ) {
247 
248  // Set up some default widget settings.
249  $defaults = array(
250  'title' => __('Recent Entries', 'gk-gravityview'),
251  'view_id' => NULL,
252  'post_id' => NULL,
253  'limit' => 10,
254  'link_format' => __('Entry #{entry_id}', 'gk-gravityview'),
255  'after_link' => ''
256  );
257 
258  $instance = wp_parse_args( (array) $instance, $defaults );
259 
260  ?>
261 
262  <!-- Title -->
263  <p>
264  <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'gk-gravityview' ) ?></label>
265  <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'] ); ?>" />
266  </p>
267 
268  <!-- Download -->
269  <?php
270  $args = array(
271  'post_type' => 'gravityview',
272  'posts_per_page' => -1,
273  'post_status' => 'publish',
274  );
275  $views = get_posts( $args );
276 
277  // If there are no views set up yet, we get outta here.
278  if( empty( $views ) ) {
279  echo '<div id="select_gravityview_view"><div class="wrap">' . GravityView_Admin::no_views_text() . '</div></div>';
280  return;
281  }
282 
283  ?>
284 
285  <?php
286  /**
287  * Display errors generated for invalid embed IDs
288  * @see GravityView_View_Data::is_valid_embed_id
289  */
290  if( isset( $instance['updated'] ) && empty( $instance['view_id'] ) ) {
291  ?>
292  <div class="error inline hide-on-view-change">
293  <p><?php esc_html_e('Please select a View to search.', 'gk-gravityview'); ?></p>
294  </div>
295  <?php
296  unset ( $error );
297  }
298  ?>
299 
300  <p>
301  <label for="<?php echo esc_attr( $this->get_field_id( 'view_id' ) ); ?>"><?php esc_html_e('Select a View', 'gk-gravityview'); ?></label>
302  <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' ) ); ?>">
303  <option value=""><?php esc_html_e( '&mdash; Select a View as Entries Source &mdash;', 'gk-gravityview' ); ?></option>
304  <?php
305 
306  foreach( $views as $view ) {
307  $title = empty( $view->post_title ) ? __('(no title)', 'gk-gravityview') : $view->post_title;
308  echo '<option value="'. $view->ID .'"'.selected( absint( $instance['view_id'] ), $view->ID ).'>'. esc_html( sprintf('%s #%d', $title, $view->ID ) ) .'</option>';
309  }
310 
311  ?>
312  </select>
313  </p>
314 
315  <?php
316  /**
317  * Display errors generated for invalid embed IDs
318  * @see GravityView_View_Data::is_valid_embed_id
319  */
320  if( !empty( $instance['error_post_id'] ) ) {
321  ?>
322  <div class="error inline">
323  <p><?php echo $instance['error_post_id']; ?></p>
324  </div>
325  <?php
326  unset ( $error );
327  }
328  ?>
329 
330  <p>
331  <label for="<?php echo $this->get_field_id('post_id'); ?>"><?php esc_html_e( 'If Embedded, Page ID:', 'gk-gravityview' ); ?></label>
332  <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'] ); ?>" />
333  <span class="howto gv-howto"><?php
334  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.', 'gk-gravityview' );
335  echo ' '.gravityview_get_link('https://docs.gravityview.co/article/222-the-search-widget', __('Learn more&hellip;', 'gk-gravityview' ), 'target=_blank' );
336  ?></span>
337  </p>
338 
339  <p>
340  <label for="<?php echo $this->get_field_id( 'limit' ); ?>">
341  <span><?php _e( 'Number of entries to show:', 'gk-gravityview' ); ?></span>
342  </label>
343  <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" />
344  </p>
345 
346  <p>
347  <label for="<?php echo $this->get_field_id( 'link_format' ); ?>">
348  <span><?php _e( 'Entry link text (required)', 'gk-gravityview' ); ?></span>
349  </label>
350  <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" />
351  </p>
352 
353  <p>
354  <label for="<?php echo $this->get_field_id( 'after_link' ); ?>">
355  <span><?php _e( 'Text or HTML to display after the link (optional)', 'gk-gravityview' ); ?></span>
356  </label>
357  <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>
358  </p>
359 
360  <?php
361 
362  /**
363  * @action `gravityview_recent_entries_widget_form` Displayed at the bottom of the Recent Entries widget admin form
364  * @param GravityView_Recent_Entries_Widget $this WP_Widget object
365  * @param array $instance Current widget instance
366  */
367  do_action( 'gravityview_recent_entries_widget_form' , $this, $instance );
368 
369  ?>
370 
371  <script>
372  // When the widget is saved or added, refresh the Merge Tags (here for backward compatibility)
373  // WordPress 3.9 added widget-added and widget-updated actions
374  jQuery('#<?php echo $this->get_field_id( 'view_id' ); ?>').trigger( 'change' );
375  </script>
376  <?php }
377 
378 }
const GV_PLUGIN_VERSION(! GravityKit\GravityView\Foundation\meets_min_php_version_requirement(__FILE__, '7.2.0'))
Constants.
Definition: gravityview.php:34
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:55
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:40
if(empty( $created_by)) $form_id
$title