Completed
Push — master ( 7b2bed...f1df0d )
by Stephanie
02:58
created

FrmFieldValue::get_field_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @since 2.04
5
 */
6
class FrmFieldValue {
7
8
	/**
9
	 * @since 2.04
10
	 *
11
	 * @var stdClass
12
	 */
13
	protected $field = null;
14
15
	/**
16
	 * @since 2.04
17
	 *
18
	 * @var stdClass
19
	 */
20
	protected $entry = null;
21
22
	/**
23
	 * @since 2.04
24
	 *
25
	 * @var string
26
	 */
27
	protected $source = '';
28
29
	/**
30
	 * @since 2.04
31
	 *
32
	 * @var mixed
33
	 */
34
	protected $saved_value = '';
35
36
	/**
37
	 * @since 2.04
38
	 *
39
	 * @var mixed
40
	 */
41
	protected $displayed_value = '';
42
43
	/**
44
	 * FrmFieldValue constructor.
45
	 *
46
	 * @param stdClass $field
47
	 * @param stdClass $entry
48
	 * @param array $atts
49
	 */
50
	public function __construct( $field, $entry, $atts = array() ) {
51
		if ( ! is_object( $field ) || ! is_object( $entry ) || ! isset( $entry->metas ) ) {
52
			return;
53
		}
54
55
		$this->field = $field;
56
		$this->entry = $entry;
57
		$this->init_source( $atts );
58
		$this->init_saved_value();
59
		$this->init_displayed_value();
60
	}
61
62
	/**
63
	 * Initialize the source property
64
	 *
65
	 * @since 2.04
66
	 *
67
	 * @param array $atts
68
	 */
69 View Code Duplication
	protected function init_source( $atts ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
		if ( isset( $atts['source'] ) && is_string( $atts['source'] ) && $atts['source'] !== '' ) {
71
			$this->source = (string) $atts['source'];
72
		} else {
73
			$this->source = 'general';
74
		}
75
	}
76
77
	/**
78
	 * Initialize the saved_value property
79
	 *
80
	 * @since 2.04
81
	 */
82
	protected function init_saved_value() {
83
		if ( isset( $this->entry->metas[ $this->field->id ] ) ) {
84
			$this->saved_value = $this->entry->metas[ $this->field->id ];
85
		} else {
86
			$this->saved_value = '';
87
		}
88
89
		$this->clean_saved_value();
90
	}
91
92
	/**
93
	 * Initialize a field's displayed value
94
	 *
95
	 * @since 2.04
96
	 */
97
	protected function init_displayed_value() {
98
		$this->displayed_value = $this->saved_value;
99
100
		$this->filter_displayed_value();
101
	}
102
103
	/**
104
	 * Get the saved_value property
105
	 *
106
	 * @since 2.04
107
	 */
108
	public function get_saved_value() {
109
		return $this->saved_value;
110
	}
111
112
	/**
113
	 * Get the field property's label
114
	 *
115
	 * @since 2.04
116
	 */
117
	public function get_field_label() {
118
		return $this->field->name;
119
	}
120
121
	/**
122
	 * Get the field property's id
123
	 *
124
	 * @since 2.05
125
	 */
126
	public function get_field_id() {
127
		return $this->field->id;
128
	}
129
130
	/**
131
	 * Get the field property's key
132
	 *
133
	 * @since 2.04
134
	 */
135
	public function get_field_key() {
136
		return $this->field->field_key;
137
	}
138
139
	/**
140
	 * Get the field property's type
141
	 *
142
	 * @since 2.04
143
	 */
144
	public function get_field_type() {
145
		return $this->field->type;
146
	}
147
148
	/**
149
	 * Get the displayed_value property
150
	 *
151
	 * @since 2.04
152
	 */
153
	public function get_displayed_value() {
154
		return $this->displayed_value;
155
	}
156
157
	/**
158
	 * Filter the displayed_value property
159
	 *
160
	 * @since 2.04
161
	 */
162
	protected function filter_displayed_value() {
163
164
		if ( $this->source === 'entry_formatter' ) {
165
			// Deprecated frm_email_value hook
166
			$meta                  = array(
167
				'item_id'    => $this->entry->id,
168
				'field_id'   => $this->field->id,
169
				'meta_value' => $this->saved_value,
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
170
				'field_type' => $this->field->type,
171
			);
172
			$this->displayed_value = apply_filters( 'frm_email_value', $this->displayed_value, (object) $meta, $this->entry, array(
173
				'field' => $this->field,
174
			) );
175
			if ( has_filter( 'frm_email_value' ) ) {
176
				_deprecated_function( 'The frm_email_value filter', '2.04', 'the frm_display_{fieldtype}_value_custom filter' );
177
			}
178
		}
179
180
		// frm_display_{fieldtype}_value_custom hook
181
		$this->displayed_value = apply_filters( 'frm_display_' . $this->field->type . '_value_custom', $this->displayed_value, array(
182
			'field' => $this->field, 'entry' => $this->entry,
183
		) );
184
	}
185
186
	/**
187
	 * Clean a field's saved value
188
	 *
189
	 * @since 2.04
190
	 */
191
	protected function clean_saved_value() {
192
		if ( $this->saved_value !== '' ) {
193
194
			$this->saved_value = maybe_unserialize( $this->saved_value );
195
196
			if ( is_array( $this->saved_value ) && empty( $this->saved_value ) ) {
197
				$this->saved_value = '';
198
			}
199
		}
200
	}
201
}