GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gv-shortcode-gventry.php
Go to the documentation of this file.
1 <?php
2 namespace GV\Shortcodes;
3 
4 /** If this file is called directly, abort. */
5 if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6  die();
7 }
8 
9 /**
10  * The [gventry] shortcode.
11  */
12 class gventry extends \GV\Shortcode {
13  /**
14  * {@inheritDoc}
15  */
16  public $name = 'gventry';
17 
18  /**
19  * Process and output the [gventry] shortcode.
20  *
21  * @param array $passed_atts The attributes passed.
22  * @param string $content The content inside the shortcode.
23  * @param string $tag The shortcode tag.
24  *
25  * @return string|null The output.
26  */
27  public function callback( $atts, $content = '', $tag = '' ) {
28  $request = gravityview()->request;
29 
30  if ( $request->is_admin() ) {
31  return apply_filters( 'gravityview/shortcodes/gventry/output', '', null, null, $atts );
32  }
33 
34  $atts = wp_parse_args( $atts, array(
35  'id' => 0,
36  'entry_id' => 0,
37  'view_id' => 0,
38  'edit' => 0,
39  ) );
40 
41  $atts = gv_map_deep( $atts, array( 'GravityView_Merge_Tags', 'replace_get_variables' ) );
42 
43  /**
44  * @filter `gravityview/shortcodes/gventry/atts` Filter the [gventry] shortcode attributes.
45  * @param array $atts The initial attributes.
46  * @since 2.0
47  */
48  $atts = apply_filters( 'gravityview/shortcodes/gventry/atts', $atts );
49 
50  $view = \GV\View::by_id( $atts['view_id'] );
51 
52  if ( ! $view ) {
53  gravityview()->log->error( 'View does not exist #{view_id}', array( 'view_id' => $atts['view_id'] ) );
54  return apply_filters( 'gravityview/shortcodes/gventry/output', '', null, null, $atts );
55  }
56 
57  $entry_id = ! empty( $atts['entry_id'] ) ? $atts['entry_id'] : $atts['id'];
58 
59  switch( $entry_id ):
60  case 'last':
61  if ( class_exists( '\GF_Query' ) ) {
62  /**
63  * @todo Remove once we refactor the use of get_view_entries_parameters.
64  *
65  * Since we're using \GF_Query shorthand initialization we have to reverse the order parameters here.
66  */
67  add_filter( 'gravityview_get_entries', $filter = function( $parameters, $args, $form_id ) {
68  if ( ! empty( $parameters['sorting'] ) ) {
69  /**
70  * Reverse existing sorts.
71  */
72  $sort = &$parameters['sorting'];
73  $sort['direction'] = $sort['direction'] == 'RAND' ? : ( $sort['direction'] == 'ASC' ? 'DESC' : 'ASC' );
74  } else {
75  /**
76  * Otherwise, sort by date_created.
77  */
78  $parameters['sorting'] = array(
79  'key' => 'id',
80  'direction' => 'ASC',
81  'is_numeric' => true
82  );
83  }
84  return $parameters;
85  }, 10, 3 );
86  $entries = $view->get_entries( null );
87  remove_filter( 'gravityview_get_entries', $filter );
88  } else {
89  $entries = $view->get_entries( null );
90 
91  /** If a sort already exists, reverse it. */
92  if ( $sort = end( $entries->sorts ) ) {
93  $entries = $entries->sort( new \GV\Entry_Sort( $sort->field, $sort->direction == \GV\Entry_Sort::RAND ? : ( $sort->direction == \GV\Entry_Sort::ASC ? \GV\Entry_Sort::DESC : \GV\Entry_Sort::ASC ) ), $sort->mode );
94  } else {
95  /** Otherwise, sort by date_created */
97  }
98  }
99 
100  if ( ! $entry = $entries->first() ) {
101  return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, null, $atts );
102  }
103  break;
104  case 'first':
105  if ( ! $entry = $view->get_entries( null )->first() ) {
106  return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, null, $atts );
107  }
108  break;
109  default:
110  if ( ! $entry = \GV\GF_Entry::by_id( $entry_id ) ) {
111  gravityview()->log->error( 'Entry #{entry_id} not found', array( 'view_id' => $atts['view_id'] ) );
112  return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, null, $atts );
113  }
114  endswitch;
115 
116  if ( $view->form->ID != $entry['form_id'] ) {
117  gravityview()->log->error( 'Entry does not belong to view (form mismatch)' );
118  return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, $entry, $atts );
119  }
120 
121  if ( post_password_required( $view->ID ) ) {
122  gravityview()->log->notice( 'Post password is required for View #{view_id}', array( 'view_id' => $view->ID ) );
123  return apply_filters( 'gravityview/shortcodes/gventry/output', get_the_password_form( $view->ID ), $view, $entry, $atts );
124  }
125 
126  if ( ! $view->form ) {
127  gravityview()->log->notice( 'View #{id} has no form attached to it.', array( 'id' => $view->ID ) );
128 
129  /**
130  * This View has no data source. There's nothing to show really.
131  * ...apart from a nice message if the user can do anything about it.
132  */
133  if ( \GVCommon::has_cap( array( 'edit_gravityviews', 'edit_gravityview' ), $view->ID ) ) {
134  $return = sprintf( __( 'This View is not configured properly. Start by <a href="%s">selecting a form</a>.', 'gk-gravityview' ), esc_url( get_edit_post_link( $view->ID, false ) ) );
135  return apply_filters( 'gravityview/shortcodes/gventry/output', $return, $view, $entry, $atts );
136  }
137 
138  return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, $entry, $atts );
139  }
140 
141  /** Private, pending, draft, etc. */
142  $public_states = get_post_stati( array( 'public' => true ) );
143  if ( ! in_array( $view->post_status, $public_states ) && ! \GVCommon::has_cap( 'read_gravityview', $view->ID ) ) {
144  gravityview()->log->notice( 'The current user cannot access this View #{view_id}', array( 'view_id' => $view->ID ) );
145  return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, $entry, $atts );
146  }
147 
148  /** Unapproved entries. */
149  if ( $entry['status'] != 'active' ) {
150  gravityview()->log->notice( 'Entry ID #{entry_id} is not active', array( 'entry_id' => $entry->ID ) );
151  return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, $entry, $atts );
152  }
153 
154  $is_admin_and_can_view = $view->settings->get( 'admin_show_all_statuses' ) && \GVCommon::has_cap('gravityview_moderate_entries', $view->ID );
155 
156  if ( $view->settings->get( 'show_only_approved' ) && ! $is_admin_and_can_view ) {
158  gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing', array( 'entry_id' => $entry->ID ) );
159  return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, $entry, $atts );
160  }
161  }
162 
163  if ( \GV\Utils::get( $_GET, 'edit' ) && \GV\Utils::get( $_GET, 'gvid' ) ) {
164  $atts['edit'] = 1;
165  }
166 
167  if ( $atts['edit'] ) {
168  /**
169  * Based on code in our unit-tests.
170  * Mocks old context, etc.
171  */
173  /** @var \GravityView_Edit_Entry_Render $render */
174  $render = $loader->instances['render'];
175 
176  // Override the \GV\Request::is_entry() check for the query var.
177  $_entry_query_var_backup = get_query_var( \GV\Entry::get_endpoint_name() );
178  set_query_var( \GV\Entry::get_endpoint_name(), $entry['id'] );
179  add_filter( 'gravityview_is_edit_entry', $use_entry = function() use ( $entry ) {
180  return $entry;
181  } );
182 
183  add_filter( 'gravityview/is_single_entry', '__return_true' );
184 
185  $form = \GFAPI::get_form( $entry['form_id'] );
186 
187  $data = \GravityView_View_Data::getInstance( $view );
189  'form' => $form,
190  'form_id' => $form['id'],
191  'view_id' => $view->ID,
192  'entries' => array( $entry ),
193  'atts' => \GVCommon::get_template_settings( $view->ID ),
194  ) );
195 
196  $_GET['edit'] = wp_create_nonce(
197  \GravityView_Edit_Entry::get_nonce_key( $view->ID, $form['id'], $entry['id'] )
198  );
199 
200  add_filter( 'gravityview/edit_entry/success', $callback = function ( $message, $_view_id, $_entry, $back_link, $redirect_url ) use ( $view, $entry, $atts ) {
201  /**
202  * @filter `gravityview/shortcodes/gventry/edit/success` Modify the edit entry success message in [gventry].
203  *
204  * @since develop
205  *
206  * @param string $message The message.
207  * @param \GV\View $view The View.
208  * @param \GV\Entry $entry The entry.
209  * @param array $atts The attributes.
210  * @param string $back_link URL to return to the original entry. @since 2.14.6
211  * @param string|null $redirect_url URL to return to after the update. @since 2.14.6
212  */
213  return apply_filters( 'gravityview/shortcodes/gventry/edit/success', $message, $view, $entry, $atts, $back_link, $redirect_url );
214  }, 10, 5 );
215 
216  ob_start() && $render->init( $data, \GV\GF_Entry::by_id( $entry['id'] ), $view );
217  $output = ob_get_clean(); // Render :)
218 
219  // Restore the \GV\Request::is_entry() check for the query var.
220  set_query_var( \GV\Entry::get_endpoint_name(), $_entry_query_var_backup );
221  remove_filter( 'gravityview_is_edit_entry', $use_entry );
222  remove_filter( 'gravityview/is_single_entry', '__return_true' );
223  remove_filter( 'gravityview/edit_entry/success', $callback );
224  } else {
225  /** Remove the back link. */
226  add_filter( 'gravityview/template/links/back/url', '__return_false' );
227 
228  $renderer = new \GV\Entry_Renderer();
229 
230  $request = new \GV\Mock_Request();
231  $request->returns['is_entry'] = $entry;
232 
233  $output = $renderer->render( $entry, $view, $request );
234 
235  remove_filter( 'gravityview/template/links/back/url', '__return_false' );
236  }
237 
238  /**
239  * @filter `gravityview/shortcodes/gventry/output` Filter the [gventry] output.
240  * @param string $output The output.
241  * @param \GV\View|null $view The View detected or null.
242  * @param \GV\Entry|null $entry The Entry or null.
243  *
244  * @since 2.0
245  */
246  return apply_filters( 'gravityview/shortcodes/gventry/output', $output, $view, $entry, $atts );
247  }
248 }
If this file is called directly, abort.
if(current_filter()==='gform_previous_button') if(current_filter()==='gform_next_button') $back_link
static getInstance( $passed_post=NULL)
const NUMERIC
static getInstance( $passed_post=NULL)
Definition: class-data.php:122
if(! isset( $gravityview)||empty( $gravityview->template)) $template
The entry loop for the list output.
If this file is called directly, abort.
$entries
if(gravityview() ->plugin->is_GF_25()) $form
const DESC
static get_template_settings( $post_id)
Get all the settings for a View.
gv_map_deep( $value, $callback)
Maps a function to all non-iterable elements of an array or an object.
const RAND
If this file is called directly, abort.
static get_endpoint_name()
Return the endpoint name for a single Entry.
const ASC
static by_id( $post_id)
Construct a instance from a post ID.
static get_nonce_key( $view_id, $form_id, $entry_id)
Return a well formatted nonce key according to GravityView Edit Entry protocol.
static by_id( $field_id)
Get a from an internal Gravity Forms field ID.
if(empty( $created_by)) $form_id
callback( $atts, $content='', $tag='')
The WordPress Shortcode API callback for this shortcode.
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
gravityview()
The main GravityView wrapper function.
static by_id( $entry_id, $form_id=0)
Construct a instance by ID.
static is_approved( $status)
static has_cap( $caps='', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()
$entry
Definition: notes.php:27
const meta_key
static getInstance()