1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Contains discount functions. |
4
|
|
|
* |
5
|
|
|
* @since 1.0.0 |
6
|
|
|
* @package Invoicing |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
defined( 'ABSPATH' ) || exit; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Returns an array of discount type. |
13
|
|
|
* |
14
|
|
|
* @return array |
15
|
|
|
*/ |
16
|
|
|
function wpinv_get_discount_types() { |
17
|
|
|
return apply_filters( |
18
|
|
|
'wpinv_discount_types', |
19
|
|
|
array( |
20
|
|
|
'percent' => __( 'Percentage', 'invoicing' ), |
21
|
|
|
'flat' => __( 'Flat Amount', 'invoicing' ), |
22
|
|
|
) |
23
|
|
|
); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns the name of a discount type. |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
function wpinv_get_discount_type_name( $type = '' ) { |
32
|
|
|
$types = wpinv_get_discount_types(); |
33
|
|
|
return isset( $types[ $type ] ) ? $types[ $type ] : $type; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Deletes a discount via the admin page. |
38
|
|
|
* |
39
|
|
|
*/ |
40
|
|
|
function wpinv_delete_discount( $data ) { |
41
|
|
|
|
42
|
|
|
$discount = new WPInv_Discount( absint( $data['discount'] ) ); |
43
|
|
|
$discount->delete( true ); |
44
|
|
|
|
45
|
|
|
} |
46
|
|
|
add_action( 'getpaid_authenticated_admin_action_delete_discount', 'wpinv_delete_discount' ); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Deactivates a discount via the admin page. |
50
|
|
|
*/ |
51
|
|
|
function wpinv_activate_discount( $data ) { |
52
|
|
|
|
53
|
|
|
$discount = new WPInv_Discount( absint( $data['discount'] ) ); |
54
|
|
|
$discount->set_status( 'publish' ); |
55
|
|
|
$discount->save(); |
56
|
|
|
|
57
|
|
|
} |
58
|
|
|
add_action( 'getpaid_authenticated_admin_action_activate_discount', 'wpinv_activate_discount' ); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Activates a discount via the admin page. |
62
|
|
|
*/ |
63
|
|
|
function wpinv_deactivate_discount( $data ) { |
64
|
|
|
|
65
|
|
|
$discount = new WPInv_Discount( absint( $data['discount'] ) ); |
66
|
|
|
$discount->set_status( 'pending' ); |
67
|
|
|
$discount->save(); |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
add_action( 'getpaid_authenticated_admin_action_deactivate_discount', 'wpinv_deactivate_discount' ); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Fetches a discount object. |
74
|
|
|
* |
75
|
|
|
* @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
76
|
|
|
* @since 1.0.15 |
77
|
|
|
* @return WPInv_Discount |
78
|
|
|
*/ |
79
|
|
|
function wpinv_get_discount( $discount ) { |
80
|
|
|
return new WPInv_Discount( $discount ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Fetches a discount object. |
85
|
|
|
* |
86
|
|
|
* @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
87
|
|
|
* @since 1.0.15 |
88
|
|
|
* @return WPInv_Discount |
89
|
|
|
*/ |
90
|
|
|
function wpinv_get_discount_obj( $discount = 0 ) { |
91
|
|
|
return new WPInv_Discount( $discount ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Fetch a discount from the db/cache using a given field. |
96
|
|
|
* |
97
|
|
|
* @param string $deprecated deprecated |
98
|
|
|
* @param string|int $value The field value |
99
|
|
|
* @return bool|WPInv_Discount |
100
|
|
|
*/ |
101
|
|
|
function wpinv_get_discount_by( $deprecated = null, $value = '' ) { |
102
|
|
|
$discount = new WPInv_Discount( $value ); |
103
|
|
|
|
104
|
|
|
if ( $discount->get_id() != 0 ) { |
105
|
|
|
return $discount; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns an array discount statuses. |
113
|
|
|
* |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
function wpinv_get_discount_statuses() { |
117
|
|
|
|
118
|
|
|
return array( |
119
|
|
|
'expired' => __( 'Expired', 'invoicing' ), |
120
|
|
|
'publish' => __( 'Active', 'invoicing' ), |
121
|
|
|
'inactive' => __( 'Inactive', 'invoicing' ), |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Retrieves an invoice status label. |
128
|
|
|
*/ |
129
|
|
|
function wpinv_discount_status( $status ) { |
130
|
|
|
$statuses = wpinv_get_discount_statuses(); |
131
|
|
|
return isset( $statuses[ $status ] ) ? $statuses[ $status ] : __( 'Inactive', 'invoicing' ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Checks if a discount is recurring. |
136
|
|
|
* |
137
|
|
|
* @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
138
|
|
|
* @param int|array|string|WPInv_Discount $code discount data, object, ID or code. |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
function wpinv_discount_is_recurring( $discount = 0, $code = 0 ) { |
142
|
|
|
|
143
|
|
|
if( ! empty( $discount ) ) { |
144
|
|
|
$discount = wpinv_get_discount_obj( $discount ); |
145
|
|
|
} else { |
146
|
|
|
$discount = wpinv_get_discount_obj( $code ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $discount->get_is_recurring(); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|