Passed
Pull Request — master (#785)
by
unknown
04:48
created

GetPaid_Form_Item::set_price_id()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
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 = null;
18
19
	/**
20
	 * Stores the item quantity.
21
	 *
22
	 * @var float
23
	 */
24
	protected $quantity = 1;
25
26
	/**
27
	 * Stores the item meta.
28
	 *
29
	 * @var array
30
	 */
31
	protected $meta = array();
32
33
	/**
34
	 * Is this item required?
35
	 *
36
	 * @var int
37
	 */
38
	protected $is_required = true;
39
40
	/**
41
	 * Are quantities allowed?
42
	 *
43
	 * @var int
44
	 */
45
	protected $allow_quantities = false;
46
47
	/**
48
	 * Associated invoice.
49
	 *
50
	 * @var int
51
	 */
52
	public $invoice_id = 0;
53
54
	/**
55
	 * Item discount.
56
	 *
57
	 * @var float
58
	 */
59
	public $item_discount = 0;
60
61
	/**
62
	 * Recurring item discount.
63
	 *
64
	 * @var float
65
	 */
66
	public $recurring_item_discount = 0;
67
68
	/**
69
	 * Item tax.
70
	 *
71
	 * @var float
72
	 */
73
	public $item_tax = 0;
74
75
76
    /**
77
	 * Item price ID.
78
	 *
79
	 * @var int
80
	 */
81
	public $price_id = 0;
82
83
    /*
84
	|--------------------------------------------------------------------------
85
	| CRUD methods
86
	|--------------------------------------------------------------------------
87
	|
88
	| Methods which create, read, update and delete items from the object.
89
	|
90
    */
91
92
    /*
93
	|--------------------------------------------------------------------------
94
	| Getters
95
	|--------------------------------------------------------------------------
96
    */
97
98
    /**
99
	 * Get the item name.
100
	 *
101
	 * @since 1.0.19
102
	 * @param  string $context View or edit context.
103
	 * @return string
104
	 */
105
	public function get_name( $context = 'view' ) {
106
		$name = parent::get_name( $context );
107
		return $name . wpinv_get_item_suffix( $this );
108
	}
109
110
	/**
111
	 * Get the item name without a suffix.
112
	 *
113
	 * @since 1.0.19
114
	 * @param  string $context View or edit context.
115
	 * @return string
116
	 */
117
	public function get_raw_name( $context = 'view' ) {
118
		return parent::get_name( $context );
119
	}
120
121
	/**
122
	 * Get the item description.
123
	 *
124
	 * @since 1.0.19
125
	 * @param  string $context View or edit context.
126
	 * @return string
127
	 */
128
	public function get_description( $context = 'view' ) {
129
130
		if ( isset( $this->custom_description ) ) {
131
			return $this->custom_description;
132
		}
133
134
		return parent::get_description( $context );
135
	}
136
137
	/**
138
	 * Returns the sub total.
139
	 *
140
	 * @since 1.0.19
141
	 * @param  string $context View or edit context.
142
	 * @return float
143
	 */
144
	public function get_sub_total( $context = 'view', $price_id = null ) {
145
		return $this->get_quantity( $context ) * $this->get_initial_price( $context, $price_id );
146
	}
147
148
	/**
149
	 * Returns the recurring sub total.
150
	 *
151
	 * @since 1.0.19
152
	 * @param  string $context View or edit context.
153
	 * @return float
154
	 */
155
	public function get_recurring_sub_total( $context = 'view' ) {
156
157
		if ( $this->is_recurring() ) {
158
			return $this->get_quantity( $context ) * $this->get_price( $context );
159
		}
160
161
		return 0;
162
	}
163
164
	/**
165
	 * @deprecated
166
	 */
167
	public function get_qantity( $context = 'view' ) {
168
		return $this->get_quantity( $context );
169
	}
170
171
	/**
172
	 * Get the item quantity.
173
	 *
174
	 * @since 1.0.19
175
	 * @param  string $context View or edit context.
176
	 * @return float
177
	 */
178
	public function get_quantity( $context = 'view' ) {
179
		$quantity = (float) $this->quantity;
180
181
		if ( 'view' === $context ) {
182
			return apply_filters( 'getpaid_payment_form_item_quantity', $quantity, $this );
183
		}
184
185
		return $quantity;
186
	}
187
188
	/**
189
	 * Get the item meta data.
190
	 *
191
	 * @since 1.0.19
192
	 * @param  string $context View or edit context.
193
	 * @return meta
0 ignored issues
show
Bug introduced by
The type meta was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
194
	 */
195
	public function get_item_meta( $context = 'view' ) {
196
		$meta = $this->meta;
197
198
		if ( 'view' === $context ) {
199
			return apply_filters( 'getpaid_payment_form_item_meta', $meta, $this );
0 ignored issues
show
Bug Best Practice introduced by
The expression return apply_filters('ge...em_meta', $meta, $this) also could return the type array which is incompatible with the documented return type meta.
Loading history...
200
		}
201
202
		return $meta;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $meta returns the type array which is incompatible with the documented return type meta.
Loading history...
203
	}
204
205
	/**
206
	 * Returns whether or not customers can update the item quantity.
207
	 *
208
	 * @since 1.0.19
209
	 * @param  string $context View or edit context.
210
	 * @return bool
211
	 */
212
	public function get_allow_quantities( $context = 'view' ) {
213
		$allow_quantities = (bool) $this->allow_quantities;
214
215
		if ( 'view' === $context ) {
216
			return apply_filters( 'getpaid_payment_form_item_allow_quantities', $allow_quantities, $this );
217
		}
218
219
		return $allow_quantities;
220
	}
221
222
	/**
223
	 * Returns whether or not the item is required.
224
	 *
225
	 * @since 1.0.19
226
	 * @param  string $context View or edit context.
227
	 * @return bool
228
	 */
229
	public function get_is_required( $context = 'view' ) {
230
		$is_required = (bool) $this->is_required;
231
232
		if ( 'view' === $context ) {
233
			return apply_filters( 'getpaid_payment_form_item_is_required', $is_required, $this );
234
		}
235
236
		return $is_required;
237
	}
238
239
    /**
240
	 * Returns whether or not customers can update the item quantity.
241
	 *
242
	 * @since 1.0.19
243
	 * @param  string $context View or edit context.
244
	 * @return int
245
	 */
246
	public function get_price_id( $context = 'view' ) {
247
		$price_id = (int) $this->price_id;
248
249
		if ( 'view' === $context ) {
250
			return apply_filters( 'getpaid_payment_form_item_price_id', $price_id, $this );
251
		}
252
253
		return $price_id;
254
	}
255
256
	/**
257
	 * Prepares form data for use.
258
	 *
259
	 * @since 1.0.19
260
	 * @return array
261
	 */
262
	public function prepare_data_for_use( $required = null ) {
263
264
		$required = is_null( $required ) ? $this->is_required() : $required;
265
		return array(
266
			'title'            => wp_strip_all_tags( $this->get_name() ),
267
			'id'               => $this->get_id(),
268
			'price'            => $this->get_price(),
269
			'recurring'        => $this->is_recurring(),
270
			'description'      => $this->get_description(),
271
			'allow_quantities' => $this->allows_quantities(),
272
			'price_id'         => $this->get_price_id(),
273
			'required'         => $required,
274
		);
275
	}
276
277
	/**
278
	 * Prepares form data for ajax use.
279
	 *
280
	 * @since 1.0.19
281
	 * @return array
282
	 */
283
	public function prepare_data_for_invoice_edit_ajax( $currency = '', $is_renewal = false ) {
284
285
		$description = getpaid_item_recurring_price_help_text( $this, $currency );
286
287
		if ( $description ) {
288
			$description = "<div class='getpaid-subscription-help-text'>$description</div>";
289
		}
290
291
		$price    = ! $is_renewal ? $this->get_price() : $this->get_recurring_price();
292
		$subtotal = ! $is_renewal ? $this->get_sub_total() : $this->get_recurring_sub_total();
293
		return array(
294
			'id'     => $this->get_id(),
295
			'texts'  => array(
296
				'item-name'        => sanitize_text_field( $this->get_name() ),
297
				'item-description' => wp_kses_post( $this->get_description() ) . $description,
298
				'item-quantity'    => floatval( $this->get_quantity() ),
299
				'item-price'       => wpinv_price( $price, $currency ),
300
				'item-total'       => wpinv_price( $subtotal, $currency ),
301
			),
302
			'inputs' => array(
303
				'item-id'          => $this->get_id(),
304
				'item-name'        => sanitize_text_field( $this->get_name() ),
305
				'item-description' => wp_kses_post( $this->get_description() ),
306
				'item-quantity'    => floatval( $this->get_quantity() ),
307
				'item-price'       => $price,
308
			),
309
		);
310
	}
311
312
	/**
313
	 * Prepares form data for saving (cart_details).
314
	 *
315
	 * @since 1.0.19
316
	 * @return array
317
	 */
318
	public function prepare_data_for_saving() {
319
320
		return array(
321
			'post_id'          => $this->invoice_id,
322
			'item_id'          => $this->get_id(),
323
			'item_name'        => sanitize_text_field( $this->get_raw_name( 'edit' ) ),
324
			'item_description' => $this->get_description( 'edit' ),
325
			'tax'              => $this->item_tax,
326
			'item_price'       => $this->get_price( 'edit' ),
327
			'quantity'         => (float) $this->get_quantity( 'edit' ),
328
			'discount'         => $this->item_discount,
329
			'subtotal'         => $this->get_sub_total( 'edit' ),
330
			'price'            => $this->get_sub_total( 'edit' ) + $this->item_tax - $this->item_discount,
331
			'price_id'         => (int) $this->get_price_id( 'edit' ),
332
			'meta'             => $this->get_item_meta( 'edit' ),
333
		);
334
	}
335
336
    /*
337
	|--------------------------------------------------------------------------
338
	| Setters
339
	|--------------------------------------------------------------------------
340
	|
341
	| Functions for setting order data. These should not update anything in the
342
	| database itself and should only change what is stored in the class
343
	| object.
344
    */
345
346
	/**
347
	 * Set the item qantity.
348
	 *
349
	 * @since 1.0.19
350
	 * @param  float $quantity The item quantity.
351
	 */
352
	public function set_quantity( $quantity ) {
353
354
		if ( ! is_numeric( $quantity ) ) {
0 ignored issues
show
introduced by
The condition is_numeric($quantity) is always true.
Loading history...
355
			$quantity = 1;
356
		}
357
358
		$this->quantity = (float) $quantity;
359
    }
360
361
    /**
362
	 * Set the item price ID.
363
	 *
364
	 * @since 1.0.19
365
	 * @param  float $price_id The item price ID.
366
	 */
367
	public function set_price_id( $price_id ) {
368
369
		if ( ! is_numeric( $price_id ) ) {
0 ignored issues
show
introduced by
The condition is_numeric($price_id) is always true.
Loading history...
370
			$price_id = $this->get_default_price_id();
371
		}
372
373
		$this->price_id = (int) $price_id;
374
	}
375
376
	/**
377
	 * Set the item meta data.
378
	 *
379
	 * @since 1.0.19
380
	 * @param  array $meta The item meta data.
381
	 */
382
	public function set_item_meta( $meta ) {
383
		$this->meta = maybe_unserialize( $meta );
0 ignored issues
show
Bug introduced by
$meta of type array is incompatible with the type string expected by parameter $data of maybe_unserialize(). ( Ignorable by Annotation )

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

383
		$this->meta = maybe_unserialize( /** @scrutinizer ignore-type */ $meta );
Loading history...
384
	}
385
386
	/**
387
	 * Set whether or not the quantities are allowed.
388
	 *
389
	 * @since 1.0.19
390
	 * @param  bool $allow_quantities
391
	 */
392
	public function set_allow_quantities( $allow_quantities ) {
393
		$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...
394
	}
395
396
	/**
397
	 * Set whether or not the item is required.
398
	 *
399
	 * @since 1.0.19
400
	 * @param  bool $is_required
401
	 */
402
	public function set_is_required( $is_required ) {
403
		$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...
404
	}
405
406
	/**
407
	 * Sets the custom item description.
408
	 *
409
	 * @since 1.0.19
410
	 * @param  string $description
411
	 */
412
	public function set_custom_description( $description ) {
413
		$this->custom_description = $description;
414
	}
415
416
    /**
417
     * We do not want to save items to the database.
418
     *
419
	 * @return int item id
420
     */
421
    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

421
    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...
422
        return $this->get_id();
423
	}
424
425
    /*
426
	|--------------------------------------------------------------------------
427
	| Conditionals
428
	|--------------------------------------------------------------------------
429
	|
430
	| Checks if a condition is true or false.
431
	|
432
	*/
433
434
    /**
435
	 * Checks whether the item has enabled dynamic pricing.
436
	 *
437
	 * @since 1.0.19
438
	 * @return bool
439
	 */
440
	public function is_required() {
441
        return (bool) $this->get_is_required();
442
	}
443
444
	/**
445
	 * Checks whether users can edit the quantities.
446
	 *
447
	 * @since 1.0.19
448
	 * @return bool
449
	 */
450
	public function allows_quantities() {
451
        return (bool) $this->get_allow_quantities();
452
	}
453
}
454