GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gv-entry-gravityforms.php
Go to the documentation of this file.
1 <?php
2 namespace GV;
3 
4 /** If this file is called directly, abort. */
5 if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6  die();
7 }
8 
9 /**
10  * The Gravity Forms Entry class implementation.
11  *
12  * Accessible as an array for back-compatibility.
13  */
14 class GF_Entry extends Entry implements \ArrayAccess {
15 
16  /**
17  * @var string The identifier of the backend used for this entry.
18  * @api
19  * @since 2.0
20  */
21  public static $backend = 'gravityforms';
22 
23  /**
24  * Initialization.
25  */
26  private function __construct() {
27  if ( ! class_exists( 'GFAPI' ) ) {
28  gravityview()->log->error( 'Gravity Forms plugin not active.' );
29  }
30  }
31 
32  /**
33  * Construct a \GV\Entry instance by ID.
34  *
35  * @param int|string $entry_id The internal entry ID.
36  * @param int $form_id The form ID, since slugs can be non-unique. Default: 0.
37  *
38  * @api
39  * @since 2.0
40  * @return \GV\GF_Entry|null An instance of this entry or null if not found.
41  */
42  public static function by_id( $entry_id, $form_id = 0 ) {
43  $entry = null;
44 
45  /** Always try to grab by numeric ID first. */
46  if ( is_numeric( $entry_id ) ) {
47  $entry = \GFAPI::get_entry( $entry_id );
48  }
49 
50  if ( ! $entry || is_wp_error( $entry ) ) {
51  /** Hmm, slugs? Must be. */
52  if ( apply_filters( 'gravityview_custom_entry_slug', false ) ) {
53  return self::by_slug( $entry_id, $form_id );
54  }
55 
56  return null;
57  }
58 
59  return self::from_entry( $entry );
60  }
61 
62  /**
63  * Construct a \GV\Entry instance by slug name.
64  *
65  * @param int|string $entry_slug The registered slug for the entry.
66  * @param int $form_id The form ID, since slugs can be non-unique. Default: 0.
67  *
68  * @api
69  * @since 2.0
70  * @return \GV\GF_Entry|null An instance of this entry or null if not found.
71  */
72  public static function by_slug( $entry_slug, $form_id = 0 ) {
73  global $wpdb;
74 
75  if ( version_compare( \GFFormsModel::get_database_version(), '2.3-dev-1', '>=' ) ) {
76  $entry_meta = \GFFormsModel::get_entry_meta_table_name();
77  $sql = "SELECT entry_id FROM $entry_meta";
78  } else {
79  $lead_meta = \GFFormsModel::get_lead_meta_table_name();
80  $sql = "SELECT lead_id FROM $lead_meta";
81  }
82 
83  $sql = "$sql WHERE meta_key = 'gravityview_unique_id' AND";
84 
85 
86  if ( $form_id = apply_filters( 'gravityview/common/get_entry_id_from_slug/form_id', $form_id ) ) {
87  $sql = $wpdb->prepare( "$sql meta_value = %s AND form_id = %s", $entry_slug, $form_id );
88  } else {
89  $sql = $wpdb->prepare( "$sql meta_value = %s", $entry_slug );
90  }
91 
92  $entry_id = $wpdb->get_var( $sql );
93 
94  if ( ! is_numeric( $entry_id ) ) {
95  return null;
96  }
97 
98  return self::by_id( $entry_id );
99  }
100 
101  /**
102  * Construct a \GV\Entry instance from a Gravity Forms entry array.
103  *
104  * @param array $entry The entry array
105  *
106  * @return \GV\GF_Entry|null An instance of this entry or null if not found.
107  */
108  public static function from_entry( $entry ) {
109  if ( empty( $entry['id'] ) ) {
110  return null;
111  }
112 
113  $self = new self();
114  $self->entry = $entry;
115 
116  $self->ID = $self->entry['id'];
117  $self->slug = $self->get_slug();
118 
119  return $self;
120  }
121 
122  /**
123  * ArrayAccess compatibility layer with a Gravity Forms entry array.
124  *
125  * @internal
126  * @deprecated
127  * @since 2.0
128  * @return bool Whether the offset exists or not.
129  */
130  #[\ReturnTypeWillChange]
131  public function offsetExists( $offset ) {
132  return isset( $this->entry[$offset] );
133  }
134 
135  /**
136  * ArrayAccess compatibility layer with a Gravity Forms entry array.
137  *
138  * Maps the old keys to the new data;
139  *
140  * @internal
141  * @deprecated
142  * @since 2.0
143  *
144  * @return mixed The value of the requested entry data.
145  */
146  #[\ReturnTypeWillChange]
147  public function offsetGet( $offset ) {
148  return $this->entry[$offset];
149  }
150 
151  /**
152  * ArrayAccess compatibility layer with a Gravity Forms entry array.
153  *
154  * @internal
155  * @deprecated
156  * @since 2.0
157  *
158  * @return void
159  */
160  #[\ReturnTypeWillChange]
161  public function offsetSet( $offset, $value ) {
162  gravityview()->log->error( 'The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.' );
163  }
164 
165  /**
166  * ArrayAccess compatibility layer with a Gravity Forms entry array.
167  *
168  * @internal
169  * @deprecated
170  * @since 2.0
171  * @return void
172  */
173  #[\ReturnTypeWillChange]
174  public function offsetUnset( $offset ) {
175  gravityview()->log->error( 'The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.' );
176  }
177 }
static by_slug( $entry_slug, $form_id=0)
Construct a instance by slug name.
__construct()
Initialization.
static from_entry( $entry)
Construct a instance from a Gravity Forms entry array.
offsetExists( $offset)
ArrayAccess compatibility layer with a Gravity Forms entry array.
offsetGet( $offset)
ArrayAccess compatibility layer with a Gravity Forms entry array.
If this file is called directly, abort.
offsetSet( $offset, $value)
ArrayAccess compatibility layer with a Gravity Forms entry array.
if(empty( $created_by)) $form_id
If this file is called directly, abort.
gravityview()
The main GravityView wrapper function.
static by_id( $entry_id, $form_id=0)
Construct a instance by ID.
$entry_slug
Definition: notes.php:30
$entry
Definition: notes.php:27
offsetUnset( $offset)
ArrayAccess compatibility layer with a Gravity Forms entry array.