Completed
Push — master ( cd1ec1...578283 )
by Zack
243:03 queued 200:08
created

GF_Entry::offsetUnset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 6.

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.

Loading history...
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 {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
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 );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
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() );
0 ignored issues
show
Bug introduced by
The property slug does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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 ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetExists is in camel caps, but expected offset_exists instead as per the coding standard.
Loading history...
131 3
		return isset( $this->entry[$offset] );
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
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 ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetGet is in camel caps, but expected offset_get instead as per the coding standard.
Loading history...
146 58
		return $this->entry[$offset];
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
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 ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetSet is in camel caps, but expected offset_set instead as per the coding standard.
Loading history...
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 ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetUnset is in camel caps, but expected offset_unset instead as per the coding standard.
Loading history...
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