GravityView  2.5
The best, easiest way to display Gravity Forms entries on your website.
class-gv-shortcode-gvfield.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 [gvfield] shortcode.
11  */
12 class gvfield extends \GV\Shortcode {
13  /**
14  * {@inheritDoc}
15  */
16  public $name = 'gvfield';
17 
18  /**
19  * Process and output the [gvfield] 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/gvfield/output', '', null, null, $atts );
32  }
33 
34  $atts = wp_parse_args( $atts, array(
35  'view_id' => null,
36  'entry_id' => null,
37  'field_id' => null,
38  ) );
39 
40  $atts = gv_map_deep( $atts, array( 'GravityView_Merge_Tags', 'replace_get_variables' ) );
41 
42  /**
43  * @filter `gravityview/shortcodes/gvfield/atts` Filter the [gvfield] shortcode attributes.
44  * @param array $atts The initial attributes.
45  * @since 2.0
46  */
47  $atts = apply_filters( 'gravityview/shortcodes/gvfield/atts', $atts );
48 
49  if ( ! $view = \GV\View::by_id( $atts['view_id'] ) ) {
50  gravityview()->log->error( 'View #{view_id} not found', array( 'view_id' => $atts['view_id'] ) );
51  return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, null, null, $atts );
52  }
53 
54  switch( $atts['entry_id'] ):
55  case 'last':
56  if ( gravityview()->plugin->supports( \GV\Plugin::FEATURE_GFQUERY ) ) {
57  /**
58  * @todo Remove once we refactor the use of get_view_entries_parameters.
59  *
60  * Since we're using \GF_Query shorthand initialization we have to reverse the order parameters here.
61  */
62  add_filter( 'gravityview_get_entries', $filter = function( $parameters, $args, $form_id ) {
63  if ( ! empty( $parameters['sorting'] ) ) {
64  /**
65  * Reverse existing sorts.
66  */
67  $sort = &$parameters['sorting'];
68  $sort['direction'] = $sort['direction'] == 'RAND' ? : ( $sort['direction'] == 'ASC' ? 'DESC' : 'ASC' );
69  } else {
70  /**
71  * Otherwise, sort by date_created.
72  */
73  $parameters['sorting'] = array(
74  'key' => 'id',
75  'direction' => 'ASC',
76  'is_numeric' => true
77  );
78  }
79  return $parameters;
80  }, 10, 3 );
81  $entries = $view->get_entries( null );
82  remove_filter( 'gravityview_get_entries', $filter );
83  } else {
84  $entries = $view->get_entries( null );
85 
86  /** If a sort already exists, reverse it. */
87  if ( $sort = end( $entries->sorts ) ) {
88  $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 );
89  } else {
90  /** Otherwise, sort by date_created */
92  }
93  }
94 
95  if ( ! $entry = $entries->first() ) {
96  return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, null, $atts );
97  }
98  break;
99  case 'first':
100  if ( ! $entry = $view->get_entries( null )->first() ) {
101  return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, null, $atts );
102  }
103  break;
104  default:
105  if ( ! $entry = \GV\GF_Entry::by_id( $atts['entry_id'] ) ) {
106  gravityview()->log->error( 'Entry #{entry_id} not found', array( 'view_id' => $atts['view_id'] ) );
107  return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, null, $atts );
108  }
109  endswitch;
110 
111  $field = is_numeric( $atts['field_id'] ) ? \GV\GF_Field::by_id( $view->form, $atts['field_id'] ) : \GV\Internal_Field::by_id( $atts['field_id'] );
112 
113  if ( ! $field ) {
114  gravityview()->log->error( 'Field #{field_id} not found', array( 'view_id' => $atts['field_id'] ) );
115  return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $field, $atts );
116  }
117 
118  if ( $view->form->ID != $entry['form_id'] ) {
119  gravityview()->log->error( 'Entry does not belong to view (form mismatch)' );
120  return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts );
121  }
122 
123  if ( post_password_required( $view->ID ) ) {
124  gravityview()->log->notice( 'Post password is required for View #{view_id}', array( 'view_id' => $view->ID ) );
125  return apply_filters( 'gravityview/shortcodes/gvfield/output', get_the_password_form( $view->ID ), $view, $entry, $atts );
126  }
127 
128  if ( ! $view->form ) {
129  gravityview()->log->notice( 'View #{id} has no form attached to it.', array( 'id' => $view->ID ) );
130 
131  /**
132  * This View has no data source. There's nothing to show really.
133  * ...apart from a nice message if the user can do anything about it.
134  */
135  if ( \GVCommon::has_cap( array( 'edit_gravityviews', 'edit_gravityview' ), $view->ID ) ) {
136  $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' );
137  return apply_filters( 'gravityview/shortcodes/gvfield/output', $return, $view, $entry, $atts );
138  }
139 
140  return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts );
141  }
142 
143  /** Private, pending, draft, etc. */
144  $public_states = get_post_stati( array( 'public' => true ) );
145  if ( ! in_array( $view->post_status, $public_states ) && ! \GVCommon::has_cap( 'read_gravityview', $view->ID ) ) {
146  gravityview()->log->notice( 'The current user cannot access this View #{view_id}', array( 'view_id' => $view->ID ) );
147  return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts );
148  }
149 
150  /** Unapproved entries. */
151  if ( $entry['status'] != 'active' ) {
152  gravityview()->log->notice( 'Entry ID #{entry_id} is not active', array( 'entry_id' => $entry->ID ) );
153  return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts );
154  }
155 
156  if ( $view->settings->get( 'show_only_approved' ) ) {
158  gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing', array( 'entry_id' => $entry->ID ) );
159  return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts );
160  }
161  }
162 
163  $field->update_configuration( $atts );
164 
165  $renderer = new \GV\Field_Renderer();
166  $output = $renderer->render( $field, $view, is_numeric( $field->ID ) ? $view->form : new \GV\Internal_Source(), $entry, gravityview()->request );
167 
168  /**
169  * @filter `gravityview/shortcodes/gvfield/output` Filter the [gvfield] output.
170  * @param string $output The output.
171  * @param \GV\View|null $view The View detected or null.
172  * @param \GV\Entry|null $entry The Entry or null.
173  * @param \GV\Field|null $field The Field or null.
174  *
175  * @since 2.0
176  */
177  return apply_filters( 'gravityview/shortcodes/gvfield/output', $output, $view, $entry, $field, $atts );
178  }
179 }
const NUMERIC
If this file is called directly, abort.
If this file is called directly, abort.
$entries
gravityview()
Definition: _stubs.php:26
static by_id( $form, $field_id)
Get a by and Field ID.
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
If this file is called directly, abort.
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
const FEATURE_GFQUERY
static by_id( $entry_id, $form_id=0)
Construct a instance by ID.
callback( $atts, $content='', $tag='')
Process and output the [gvfield] shortcode.
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
$field
Definition: gquiz_grade.php:11