Passed
Push — master ( 6621fa...568391 )
by Brian
06:12
created

getpaid_get_subscription_periods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 24
rs 9.7666
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Contains subscription functions.
4
 *
5
 * @since 1.0.0
6
 * @package Invoicing
7
 */
8
9
/**
10
 * Queries the subscriptions database.
11
 *
12
 * @param array $args Query arguments.For a list of all supported args, refer to GetPaid_Subscriptions_Query::prepare_query()
13
 * @param string $return 'results' returns the found subscriptions, $count returns the total count while 'query' returns GetPaid_Subscriptions_Query
14
 *
15
 *
16
 * @return int|array|WPInv_Subscription[]|GetPaid_Subscriptions_Query
17
 */
18
function getpaid_get_subscriptions( $args = array(), $return = 'results' ) {
19
20
	// Do not retrieve all fields if we just want the count.
21
	if ( 'count' == $return ) {
22
		$args['fields'] = 'id';
23
		$args['number'] = 1;
24
	}
25
26
	// Do not count all matches if we just want the results.
27
	if ( 'results' == $return ) {
28
		$args['count_total'] = false;
29
	}
30
31
	$query = new GetPaid_Subscriptions_Query( $args );
32
33
	if ( 'results' == $return ) {
34
		return $query->get_results();
35
	}
36
37
	if ( 'count' == $return ) {
38
		return $query->get_total();
39
	}
40
41
	return $query;
42
}
43
44
/**
45
 * Returns an array of valid subscription statuses.
46
 *
47
 * @return array
48
 */
49
function getpaid_get_subscription_statuses() {
50
51
	return apply_filters(
52
		'getpaid_get_subscription_statuses',
53
		array(
54
			'pending'    => __( 'Pending', 'invoicing' ),
55
			'trialling'  => __( 'Trialing', 'invoicing' ),
56
			'active'     => __( 'Active', 'invoicing' ),
57
			'failing'    => __( 'Failing', 'invoicing' ),
58
			'expired'    => __( 'Expired', 'invoicing' ),
59
			'completed'  => __( 'Complete', 'invoicing' ),
60
			'cancelled'  => __( 'Cancelled', 'invoicing' ),
61
		)
62
	);
63
64
}
65
66
/**
67
 * Returns a subscription status label
68
 *
69
 * @return string
70
 */
71
function getpaid_get_subscription_status_label( $status ) {
72
	$statuses = getpaid_get_subscription_statuses();
73
	return isset( $statuses[ $status ] ) ? $statuses[ $status ] : ucfirst( sanitize_text_field( $status ) );
74
}
75
76
/**
77
 * Returns an array of valid subscription status classes.
78
 *
79
 * @return array
80
 */
81
function getpaid_get_subscription_status_classes() {
82
83
	return apply_filters(
84
		'getpaid_get_subscription_status_classes',
85
		array(
86
			'pending'    => 'getpaid-item-status-pending',
87
			'trialling'  => 'getpaid-item-status-trial',
88
			'active'     => 'getpaid-item-status-info',
89
			'failing'    => 'getpaid-item-status-failing',
90
			'expired'    => 'getpaid-item-status-expired',
91
			'completed'  => 'getpaid-item-status-success',
92
			'cancelled'  => 'getpaid-item-status-canceled',
93
		)
94
	);
95
96
}
97
98
/**
99
 * Counts subscriptions in each status.
100
 *
101
 * @return array
102
 */
103
function getpaid_get_subscription_status_counts( $args = array() ) {
104
105
	$statuses = array_keys( getpaid_get_subscription_statuses() );
106
	$counts   = array();
107
108
	foreach ( $statuses as $status ) {
109
		$_args             = wp_parse_args( "status=$status", $args );
110
		$counts[ $status ] = getpaid_get_subscriptions( $_args, 'count' );
111
	}
112
113
	return $counts;
114
115
}
116
117
/**
118
 * Returns valid subscription periods.
119
 *
120
 * @return array
121
 */
122
function getpaid_get_subscription_periods() {
123
124
	return apply_filters(
125
		'getpaid_get_subscription_periods',
126
		array(
127
128
			'day'   => array(
129
				'singular' => __( '%s day', 'invoicing' ),
130
				'plural'   => __( '%d days', 'invoicing' ),
131
			),
132
133
			'week'   => array(
134
				'singular' => __( '%s week', 'invoicing' ),
135
				'plural'   => __( '%d weeks', 'invoicing' ),
136
			),
137
138
			'month'   => array(
139
				'singular' => __( '%s month', 'invoicing' ),
140
				'plural'   => __( '%d months', 'invoicing' ),
141
			),
142
143
			'year'   => array(
144
				'singular' => __( '%s year', 'invoicing' ),
145
				'plural'   => __( '%d years', 'invoicing' ),
146
			),
147
148
		)
149
	);
150
151
}
152
153
/**
154
 * Given a subscription trial, e.g, 1 month, returns the interval (1)
155
 *
156
 * @param string $trial_period
157
 * @return int
158
 */
159
function getpaid_get_subscription_trial_period_interval( $trial_period ) {
160
	return (int) preg_replace( '/[^0-9]/', '', $trial_period );
161
}
162
163
/**
164
 * Given a subscription trial, e.g, 1 month, returns the period (month)
165
 *
166
 * @param string $trial_period
167
 * @return string
168
 */
169
function getpaid_get_subscription_trial_period_period( $trial_period ) {
170
	return preg_replace( '/[^a-z]/', '', strtolower( $trial_period ) );
171
}
172
173
/**
174
 * Returns a singular period label..
175
 *
176
 * @param string $period
177
 * @param int $interval
178
 * @return string
179
 */
180
function getpaid_get_subscription_period_label( $period, $interval = 1, $singular_prefix = '1' ) {
181
	$label = (int) $interval > 1 ? getpaid_get_plural_subscription_period_label(  $period, $interval ) : getpaid_get_singular_subscription_period_label( $period, $singular_prefix );
182
	return strtolower( sanitize_text_field( $label ) );
183
}
184
185
/**
186
 * Returns a singular period label..
187
 *
188
 * @param string $period
189
 * @return string
190
 */
191
function getpaid_get_singular_subscription_period_label( $period, $singular_prefix = '1' ) {
192
193
	$periods = getpaid_get_subscription_periods();
194
	$period  = strtolower( $period );
195
196
	if ( isset( $periods[ $period ] ) ) {
197
		return sprintf( $periods[ $period ]['singular'], $singular_prefix );
198
	}
199
200
	// Backwards compatibility.
201
	foreach ( $periods as $key => $data ) {
202
		if ( strpos( $key, $period ) === 0 ) {
203
			return sprintf( $data['singular'], $singular_prefix );
204
		}
205
	}
206
207
	// Invalid string.
208
	return '';
209
}
210
211
/**
212
 * Returns a plural period label..
213
 *
214
 * @param string $period
215
 * @param int $interval
216
 * @return string
217
 */
218
function getpaid_get_plural_subscription_period_label( $period, $interval ) {
219
220
	$periods = getpaid_get_subscription_periods();
221
	$period  = strtolower( $period );
222
223
	if ( isset( $periods[ $period ] ) ) {
224
		return sprintf( $periods[ $period ]['plural'], $interval );
225
	}
226
227
	// Backwards compatibility.
228
	foreach ( $periods as $key => $data ) {
229
		if ( strpos( $key, $period ) === 0 ) {
230
			return sprintf( $data['plural'], $interval );
231
		}
232
	}
233
234
	// Invalid string.
235
	return '';
236
}
237
238
/**
239
 * Returns formatted subscription amout
240
 *
241
 * @param WPInv_Subscription $subscription
242
 * @return string
243
 */
244
function getpaid_get_formatted_subscription_amount( $subscription ) {
245
246
	$initial   = wpinv_price( wpinv_format_amount( $subscription->get_initial_amount() ), $subscription->get_parent_payment()->get_currency() );
247
	$recurring = wpinv_price( wpinv_format_amount( $subscription->get_recurring_amount() ), $subscription->get_parent_payment()->get_currency() );
248
	$period    = getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' );
249
250
	// Trial periods.
251
	if ( $subscription->has_trial_period() ) {
252
253
		$trial_period   = getpaid_get_subscription_trial_period_period( $subscription->get_trial_period() );
254
		$trial_interval = getpaid_get_subscription_trial_period_interval( $subscription->get_trial_period() );
255
		return sprintf(
256
257
			// translators: $1: is the initial amount, $2: is the trial period, $3: is the recurring amount, $4: is the recurring period
258
			_x( '%1$s trial for %2$s then %3$s / %4$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year)', 'invoicing' ),
259
			$initial,
260
			getpaid_get_subscription_period_label( $trial_period, $trial_interval ),
261
			$recurring,
262
			$period
263
264
		);
265
266
	}
267
268
	if ( $initial != $recurring ) {
269
270
		return sprintf(
271
272
			// translators: $1: is the initial amount, $2: is the recurring amount, $3: is the recurring period
273
			_x( 'Initial payment of %1$s which renews at %2$s / %3$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year)', 'invoicing' ),
274
			$initial,
275
			$recurring,
276
			$period
277
278
		);
279
280
	}
281
282
	return sprintf(
283
284
		// translators: $1: is the recurring amount, $2: is the recurring period
285
		_x( '%1$s / %2$s', 'Subscription amount. (e.g.: $120 / year)', 'invoicing' ),
286
		$initial,
287
		$period
288
289
	);
290
291
}
292
293
/**
294
 * Returns an invoice subscription.
295
 *
296
 * @param WPInv_Invoice $invoice
297
 * @return WPInv_Subscription|bool
298
 */
299
function getpaid_get_invoice_subscription( $invoice ) {
300
	return getpaid_subscriptions()->get_invoice_subscription( $invoice );
301
}
302
303
/**
304
 * Activates an invoice subscription.
305
 *
306
 * @param WPInv_Invoice $invoice
307
 */
308
function getpaid_activate_invoice_subscription( $invoice ) {
309
	$subscription = getpaid_get_invoice_subscription( $invoice );
310
	if ( is_a( $subscription, 'WPInv_Subscription' ) ) {
311
		$subscription->activate();
312
	}
313
}
314
315
/**
316
 * Returns the subscriptions controller.
317
 *
318
 * @return WPInv_Subscriptions
319
 */
320
function getpaid_subscriptions() {
321
	return getpaid()->get( 'subscriptions' );
322
}
323
324
/**
325
 * Fetchs an invoice subscription from the database.
326
 *
327
 * @return WPInv_Subscription|bool
328
 */
329
function wpinv_get_subscription( $invoice ) {
330
331
    // Retrieve the invoice.
332
    $invoice = new WPInv_Invoice( $invoice );
333
334
    // Ensure it is a recurring invoice.
335
    if ( ! $invoice->is_recurring() ) {
336
        return false;
337
    }
338
339
	// Fetch the invoiec subscription.
340
	$subscription = getpaid_get_subscriptions(
341
		array(
342
			'invoice_in' => $invoice->is_renewal() ? $invoice->get_parent_id() : $invoice->get_id(),
343
			'number'     => 1,
344
		)
345
	);
346
347
	return empty( $subscription ) ? false : $subscription[0];
348
349
}
350