GravityView  2.1.1
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 $atts The attributes passed.
22  * @param string $content The content inside the shortcode.
23  *
24  * @return string|null The output.
25  */
26  public function callback( $atts, $content = null ) {
27 
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  ) );
39 
40  $atts = gv_map_deep( $atts, array( 'GravityView_Merge_Tags', 'replace_get_variables' ) );
41 
42  /**
43  * @filter `gravityview/shortcodes/gventry/atts` Filter the [gventry] shortcode attributes.
44  * @param array $atts The initial attributes.
45  * @since 2.0
46  */
47  $atts = apply_filters( 'gravityview/shortcodes/gventry/atts', $atts );
48 
49  $view = \GV\View::by_id( $atts['view_id'] );
50 
51  if ( ! $view ) {
52  gravityview()->log->error( 'View does not exist #{view_id}', array( 'view_id' => $atts['view_id'] ) );
53  return apply_filters( 'gravityview/shortcodes/gventry/output', '', null, null, $atts );
54  }
55 
56  $entry_id = ! empty( $atts['entry_id'] ) ? $atts['entry_id'] : $atts['id'];
57 
58  switch( $entry_id ):
59  case 'last':
60  if ( class_exists( '\GF_Query' ) ) {
61  /**
62  * @todo Remove once we refactor the use of get_view_entries_parameters.
63  *
64  * Since we're using \GF_Query shorthand initialization we have to reverse the order parameters here.
65  */
66  add_filter( 'gravityview_get_entries', $filter = function( $parameters, $args, $form_id ) {
67  if ( ! empty( $parameters['sorting'] ) ) {
68  /**
69  * Reverse existing sorts.
70  */
71  $sort = &$parameters['sorting'];
72  $sort['direction'] = $sort['direction'] == 'RAND' ? : ( $sort['direction'] == 'ASC' ? 'DESC' : 'ASC' );
73  } else {
74  /**
75  * Otherwise, sort by date_created.
76  */
77  $parameters['sorting'] = array(
78  'key' => 'id',
79  'direction' => 'ASC',
80  'is_numeric' => true
81  );
82  }
83  return $parameters;
84  }, 10, 3 );
85  $entries = $view->get_entries( null );
86  remove_filter( 'gravityview_get_entries', $filter );
87  } else {
88  $entries = $view->get_entries( null );
89 
90  /** If a sort already exists, reverse it. */
91  if ( $sort = end( $entries->sorts ) ) {
92  $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 );
93  } else {
94  /** Otherwise, sort by date_created */
96  }
97  }
98 
99  if ( ! $entry = $entries->first() ) {
100  return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, null, $atts );
101  }
102  break;
103  case 'first':
104  if ( ! $entry = $view->get_entries( null )->first() ) {
105  return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, null, $atts );
106  }
107  break;
108  default:
109  if ( ! $entry = \GV\GF_Entry::by_id( $entry_id ) ) {
110  gravityview()->log->error( 'Entry #{entry_id} not found', array( 'view_id' => $atts['view_id'] ) );
111  return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, null, $atts );
112  }
113  endswitch;
114 
115  if ( $view->form->ID != $entry['form_id'] ) {
116  gravityview()->log->error( 'Entry does not belong to view (form mismatch)' );
117  return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, $entry, $atts );
118  }
119 
120  if ( post_password_required( $view->ID ) ) {
121  gravityview()->log->notice( 'Post password is required for View #{view_id}', array( 'view_id' => $view->ID ) );
122  return apply_filters( 'gravityview/shortcodes/gventry/output', get_the_password_form( $view->ID ), $view, $entry, $atts );
123  }
124 
125  if ( ! $view->form ) {
126  gravityview()->log->notice( 'View #{id} has no form attached to it.', array( 'id' => $view->ID ) );
127 
128  /**
129  * This View has no data source. There's nothing to show really.
130  * ...apart from a nice message if the user can do anything about it.
131  */
132  if ( \GVCommon::has_cap( array( 'edit_gravityviews', 'edit_gravityview' ), $view->ID ) ) {
133  $return = __( sprintf( 'This View is not configured properly. Start by <a href="%s">selecting a form</a>.', esc_url( get_edit_post_link( $view->ID, false ) ) ), 'gravityview' );
134  return apply_filters( 'gravityview/shortcodes/gventry/output', $return, $view, $entry, $atts );
135  }
136 
137  return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, $entry, $atts );
138  }
139 
140  /** Private, pending, draft, etc. */
141  $public_states = get_post_stati( array( 'public' => true ) );
142  if ( ! in_array( $view->post_status, $public_states ) && ! \GVCommon::has_cap( 'read_gravityview', $view->ID ) ) {
143  gravityview()->log->notice( 'The current user cannot access this View #{view_id}', array( 'view_id' => $view->ID ) );
144  return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, $entry, $atts );
145  }
146 
147  /** Unapproved entries. */
148  if ( $entry['status'] != 'active' ) {
149  gravityview()->log->notice( 'Entry ID #{entry_id} is not active', array( 'entry_id' => $entry->ID ) );
150  return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, $entry, $atts );
151  }
152 
153  $is_admin_and_can_view = $view->settings->get( 'admin_show_all_statuses' ) && \GVCommon::has_cap('gravityview_moderate_entries', $view->ID );
154 
155  if ( $view->settings->get( 'show_only_approved' ) && ! $is_admin_and_can_view ) {
157  gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing', array( 'entry_id' => $entry->ID ) );
158  return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, $entry, $atts );
159  }
160  }
161 
162  /** Remove the back link. */
163  add_filter( 'gravityview/template/links/back/url', '__return_false' );
164 
165  $renderer = new \GV\Entry_Renderer();
166 
167  $request = new \GV\Mock_Request();
168  $request->returns['is_entry'] = $entry;
169 
170  $output = $renderer->render( $entry, $view, $request );
171 
172  remove_filter( 'gravityview/template/links/back/url', '__return_false' );
173 
174  /**
175  * @filter `gravityview/shortcodes/gventry/output` Filter the [gventry] output.
176  * @param string $output The output.
177  * @param \GV\View|null $view The View detected or null.
178  * @param \GV\Entry|null $entry The Entry or null.
179  *
180  * @since 2.0
181  */
182  return apply_filters( 'gravityview/shortcodes/gventry/output', $output, $view, $entry, $atts );
183  }
184 }
If this file is called directly, abort.
const NUMERIC
callback( $atts, $content=null)
Process and output the [gventry] shortcode.
If this file is called directly, abort.
$entries
gravityview()
Definition: _stubs.php:26
const DESC
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.
const ASC
static by_id( $post_id)
Construct a instance from a post ID.
static by_id( $field_id)
Get a from an internal Gravity Forms field ID.
if(empty( $created_by)) $form_id
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