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 future |
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
|
|
|
* |
37
|
|
|
* @api |
38
|
|
|
* @since future |
39
|
|
|
* @return \GV\Entry|null An instance of this entry or null if not found. |
40
|
|
|
*/ |
41
|
|
|
public static function by_id( $entry_id ) { |
42
|
|
|
$entry = \GFAPI::get_entry( $entry_id ); |
43
|
|
|
if ( !$entry ) { |
|
|
|
|
44
|
|
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return self::from_entry( $entry ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Construct a \GV\Entry instance from a Gravity Forms entry array. |
52
|
|
|
* |
53
|
|
|
* @param array $entry The array ID. |
54
|
|
|
* |
55
|
|
|
* @return \GV\Entry|null An instance of this entry or null if not found. |
56
|
|
|
*/ |
57
|
|
|
public static function from_entry( $entry ) { |
58
|
|
|
if ( empty( $entry['id'] ) ) { |
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$self = new self(); |
63
|
|
|
$self->entry = $entry; |
64
|
|
|
|
65
|
|
|
$self->ID = $self->entry['id']; |
66
|
|
|
|
67
|
|
|
return $self; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* ArrayAccess compatibility layer with a Gravity Forms entry array. |
72
|
|
|
* |
73
|
|
|
* @internal |
74
|
|
|
* @deprecated |
75
|
|
|
* @since future |
76
|
|
|
* @return bool Whether the offset exists or not. |
77
|
|
|
*/ |
78
|
|
|
public function offsetExists( $offset ) { |
|
|
|
|
79
|
|
|
return isset( $this->entry[$offset] ); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* ArrayAccess compatibility layer with a Gravity Forms entry array. |
84
|
|
|
* |
85
|
|
|
* Maps the old keys to the new data; |
86
|
|
|
* |
87
|
|
|
* @internal |
88
|
|
|
* @deprecated |
89
|
|
|
* @since future |
90
|
|
|
* |
91
|
|
|
* @return mixed The value of the requested entry data. |
92
|
|
|
*/ |
93
|
|
|
public function offsetGet( $offset ) { |
|
|
|
|
94
|
|
|
return $this->entry[$offset]; |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* ArrayAccess compatibility layer with a Gravity Forms entry array. |
99
|
|
|
* |
100
|
|
|
* @internal |
101
|
|
|
* @deprecated |
102
|
|
|
* @since future |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
public function offsetSet( $offset, $value ) { |
|
|
|
|
107
|
|
|
gravityview()->log->error( 'The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.' ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* ArrayAccess compatibility layer with a Gravity Forms entry array. |
112
|
|
|
* |
113
|
|
|
* @internal |
114
|
|
|
* @deprecated |
115
|
|
|
* @since future |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
public function offsetUnset( $offset ) { |
|
|
|
|
119
|
|
|
gravityview()->log->error( 'The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.' ); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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.