|
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
|
75 |
|
private function __construct() { |
|
27
|
75 |
|
if ( ! class_exists( 'GFAPI' ) ) { |
|
28
|
|
|
gravityview()->log->error( 'Gravity Forms plugin not active.' ); |
|
29
|
|
|
} |
|
30
|
75 |
|
} |
|
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
|
65 |
|
public static function by_id( $entry_id, $form_id = 0 ) { |
|
43
|
65 |
|
$entry = null; |
|
44
|
|
|
|
|
45
|
|
|
/** Always try to grab by numeric ID first. */ |
|
46
|
65 |
|
if ( is_numeric( $entry_id ) ) { |
|
47
|
65 |
|
$entry = \GFAPI::get_entry( $entry_id ); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
65 |
|
if ( ! $entry || is_wp_error( $entry ) ) { |
|
51
|
|
|
/** Hmm, slugs? Must be. */ |
|
52
|
4 |
|
if ( apply_filters( 'gravityview_custom_entry_slug', false ) ) { |
|
53
|
2 |
|
return self::by_slug( $entry_id, $form_id ); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
3 |
|
return null; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
64 |
|
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
|
2 |
|
public static function by_slug( $entry_slug, $form_id = 0 ) { |
|
73
|
2 |
|
global $wpdb; |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
2 |
|
if ( version_compare( \GFFormsModel::get_database_version(), '2.3-dev-1', '>=' ) ) { |
|
76
|
2 |
|
$entry_meta = \GFFormsModel::get_entry_meta_table_name(); |
|
77
|
2 |
|
$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
|
2 |
|
$sql = "$sql WHERE meta_key = 'gravityview_unique_id' AND"; |
|
84
|
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
86
|
2 |
|
if ( $form_id = apply_filters( 'gravityview/common/get_entry_id_from_slug/form_id', $form_id ) ) { |
|
87
|
1 |
|
$sql = $wpdb->prepare( "$sql meta_value = %s AND form_id = %s", $entry_slug, $form_id ); |
|
88
|
|
|
} else { |
|
89
|
1 |
|
$sql = $wpdb->prepare( "$sql meta_value = %s", $entry_slug ); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
2 |
|
$entry_id = $wpdb->get_var( $sql ); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
2 |
|
if ( ! is_numeric( $entry_id ) ) { |
|
95
|
1 |
|
return null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
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 array ID. |
|
105
|
|
|
* |
|
106
|
|
|
* @return \GV\GF_Entry|null An instance of this entry or null if not found. |
|
107
|
|
|
*/ |
|
108
|
75 |
|
public static function from_entry( $entry ) { |
|
109
|
75 |
|
if ( empty( $entry['id'] ) ) { |
|
110
|
|
|
return null; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
75 |
|
$self = new self(); |
|
114
|
75 |
|
$self->entry = $entry; |
|
115
|
|
|
|
|
116
|
75 |
|
$self->ID = $self->entry['id']; |
|
117
|
75 |
|
$self->slug = \GravityView_API::get_entry_slug( $self->ID, $self->as_entry() ); |
|
|
|
|
|
|
118
|
|
|
|
|
119
|
75 |
|
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
|
3 |
|
public function offsetExists( $offset ) { |
|
|
|
|
|
|
131
|
3 |
|
return isset( $this->entry[$offset] ); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* ArrayAccess compatibility layer with a Gravity Forms entry array. |
|
136
|
|
|
* |
|
137
|
|
|
* Maps the old keys to the new data; |
|
138
|
|
|
* |
|
139
|
|
|
* @internal |
|
140
|
|
|
* @deprecated |
|
141
|
|
|
* @since 2.0 |
|
142
|
|
|
* |
|
143
|
|
|
* @return mixed The value of the requested entry data. |
|
144
|
|
|
*/ |
|
145
|
58 |
|
public function offsetGet( $offset ) { |
|
|
|
|
|
|
146
|
58 |
|
return $this->entry[$offset]; |
|
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* ArrayAccess compatibility layer with a Gravity Forms entry array. |
|
151
|
|
|
* |
|
152
|
|
|
* @internal |
|
153
|
|
|
* @deprecated |
|
154
|
|
|
* @since 2.0 |
|
155
|
|
|
* |
|
156
|
|
|
* @return void |
|
157
|
|
|
*/ |
|
158
|
|
|
public function offsetSet( $offset, $value ) { |
|
|
|
|
|
|
159
|
|
|
gravityview()->log->error( 'The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.' ); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* ArrayAccess compatibility layer with a Gravity Forms entry array. |
|
164
|
|
|
* |
|
165
|
|
|
* @internal |
|
166
|
|
|
* @deprecated |
|
167
|
|
|
* @since 2.0 |
|
168
|
|
|
* @return void |
|
169
|
|
|
*/ |
|
170
|
|
|
public function offsetUnset( $offset ) { |
|
|
|
|
|
|
171
|
|
|
gravityview()->log->error( 'The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.' ); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.