Passed
Pull Request — master (#375)
by Brian
104:55
created

GetPaid_Form_Item::get_description()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Form Item Class
8
 *
9
 */
10
class GetPaid_Form_Item  extends WPInv_Item {
11
12
    /**
13
	 * Stores a custom description for the item.
14
	 *
15
	 * @var string
16
	 */
17
	protected $custom_description = '';
18
19
	/**
20
	 * Stores the item quantity.
21
	 *
22
	 * @var int
23
	 */
24
	protected $quantity = 1;
25
26
	/**
27
	 * Is this item required?
28
	 *
29
	 * @var int
30
	 */
31
	protected $is_required = true;
32
33
	/**
34
	 * Are quantities allowed?
35
	 *
36
	 * @var int
37
	 */
38
	protected $allow_quantities = false;
39
40
    /*
41
	|--------------------------------------------------------------------------
42
	| CRUD methods
43
	|--------------------------------------------------------------------------
44
	|
45
	| Methods which create, read, update and delete items from the object.
46
	|
47
    */
48
49
    /*
50
	|--------------------------------------------------------------------------
51
	| Getters
52
	|--------------------------------------------------------------------------
53
    */
54
55
    /**
56
	 * Get the item name.
57
	 *
58
	 * @since 1.0.19
59
	 * @param  string $context View or edit context.
60
	 * @return string
61
	 */
62
	public function get_name( $context = 'view' ) {
63
		$name = parent::get_name( $context );
64
		return $name . wpinv_get_item_suffix( $this );
65
	}
66
67
	/**
68
	 * Get the item description.
69
	 *
70
	 * @since 1.0.19
71
	 * @param  string $context View or edit context.
72
	 * @return string
73
	 */
74
	public function get_description( $context = 'view' ) {
75
76
		if ( ! empty( $this->custom_description ) ) {
77
			return $this->custom_description;
78
		}
79
80
		return parent::get_description( $context );
81
	}
82
	
83
	/**
84
	 * Returns the sub total.
85
	 *
86
	 * @since 1.0.19
87
	 * @param  string $context View or edit context.
88
	 * @return int
89
	 */
90
	public function get_sub_total( $context = 'view' ) {
91
		return $this->get_quantity( $context ) * $this->get_price( $context );
92
	}
93
94
	/**
95
	 * @deprecated
96
	 */
97
	public function get_qantity( $context = 'view' ) {
98
		return $this->get_quantity( $context );
99
	}
100
101
	/**
102
	 * Get the item quantity.
103
	 *
104
	 * @since 1.0.19
105
	 * @param  string $context View or edit context.
106
	 * @return int
107
	 */
108
	public function get_quantity( $context = 'view' ) {
109
		$quantity = (int) $this->quantity;
110
111
		if ( empty( $quantity ) || 1 > $quantity ) {
112
			$quantity = 1;
113
		}
114
115
		if ( 'view' == $context ) {
116
			return apply_filters( 'getpaid_payment_form_item_quanity', $quantity, $this );
117
		}
118
119
		return $quantity;
120
121
	}
122
123
	/**
124
	 * Returns whether or not customers can update the item quantity.
125
	 *
126
	 * @since 1.0.19
127
	 * @param  string $context View or edit context.
128
	 * @return bool
129
	 */
130
	public function get_allow_quantities( $context = 'view' ) {
131
		$allow_quantities = (bool) $this->allow_quantities;
132
133
		if ( 'view' == $context ) {
134
			return apply_filters( 'getpaid_payment_form_item_allow_quantities', $allow_quantities, $this );
135
		}
136
137
		return $allow_quantities;
138
139
	}
140
141
	/**
142
	 * Returns whether or not the item is required.
143
	 *
144
	 * @since 1.0.19
145
	 * @param  string $context View or edit context.
146
	 * @return bool
147
	 */
148
	public function get_is_required( $context = 'view' ) {
149
		$is_required = (bool) $this->is_required;
150
151
		if ( 'view' == $context ) {
152
			return apply_filters( 'getpaid_payment_form_item_is_required', $is_required, $this );
153
		}
154
155
		return $is_required;
156
157
	}
158
159
	/**
160
	 * Prepares form data for use.
161
	 *
162
	 * @since 1.0.19
163
	 * @param  string $context View or edit context.
164
	 * @return array
165
	 */
166
	public function prepare_data_for_use() {
167
168
		return array(
169
			'title'            => sanitize_text_field( $this->get_name() ),
170
			'id'               => $this->get_id(),
171
			'price'            => $this->get_price(),
172
			'recurring'        => $this->is_recurring(),
173
			'description'      => $this->get_description(),
174
			'allow_quantities' => $this->allows_quantities(),
175
			'required'         => $this->is_required(),
176
        );
177
	}
178
179
    /*
180
	|--------------------------------------------------------------------------
181
	| Setters
182
	|--------------------------------------------------------------------------
183
	|
184
	| Functions for setting order data. These should not update anything in the
185
	| database itself and should only change what is stored in the class
186
	| object.
187
    */
188
189
	/**
190
	 * Set the item qantity.
191
	 *
192
	 * @since 1.0.19
193
	 * @param  int $quantity The item quantity.
194
	 */
195
	public function set_quantity( $quantity ) {
196
197
		if ( empty( $quantity ) || ! is_numeric( $quantity ) ) {
198
			$quantity = 1;
199
		}
200
201
		$this->quantity = $quantity;
202
203
	}
204
205
	/**
206
	 * Set whether or not the quantities are allowed.
207
	 *
208
	 * @since 1.0.19
209
	 * @param  bool $allow_quantities
210
	 */
211
	public function set_allow_quantities( $allow_quantities ) {
212
		$this->allow_quantities = (bool) $allow_quantities;
0 ignored issues
show
Documentation Bug introduced by
The property $allow_quantities was declared of type integer, but (bool)$allow_quantities is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
213
	}
214
215
	/**
216
	 * Set whether or not the item is required.
217
	 *
218
	 * @since 1.0.19
219
	 * @param  bool $is_required
220
	 */
221
	public function set_is_required( $is_required ) {
222
		$this->is_required = (bool) $is_required;
0 ignored issues
show
Documentation Bug introduced by
The property $is_required was declared of type integer, but (bool)$is_required is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
223
	}
224
225
	/**
226
	 * Sets the custom item description.
227
	 *
228
	 * @since 1.0.19
229
	 * @param  string $description
230
	 */
231
	public function set_custom_description( $description ) {
232
		$this->custom_description = $description;
233
	}
234
235
    /**
236
     * We do not want to save items to the database.
237
     * 
238
	 * @return int item id
239
     */
240
    public function save( $data = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

240
    public function save( /** @scrutinizer ignore-unused */ $data = array() ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
241
        return $this->get_id();
242
	}
243
244
    /*
245
	|--------------------------------------------------------------------------
246
	| Conditionals
247
	|--------------------------------------------------------------------------
248
	|
249
	| Checks if a condition is true or false.
250
	|
251
	*/
252
253
    /**
254
	 * Checks whether the item has enabled dynamic pricing.
255
	 *
256
	 * @since 1.0.19
257
	 * @return bool
258
	 */
259
	public function is_required() {
260
        return (bool) $this->get_is_required();
261
	}
262
263
	/**
264
	 * Checks whether users can edit the quantities.
265
	 *
266
	 * @since 1.0.19
267
	 * @return bool
268
	 */
269
	public function allows_quantities() {
270
        return (bool) $this->get_allow_quantities();
271
	}
272
273
}
274