Passed
Push — develop ( 8ebc76...5a0ded )
by Reüel
05:00 queued 17s
created

get_pronamic_subscriptions_by_meta()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 26
rs 8.8571
1
<?php
2
/**
3
 * Functions
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
use Pronamic\WordPress\Pay\Payments\Payment;
12
use Pronamic\WordPress\Pay\Subscriptions\Subscription;
13
14
/**
15
 * Get payment by specified post ID.
16
 *
17
 * @param int|string $post_id A payment post ID.
18
 * @return Payment
19
 */
20
function get_pronamic_payment( $post_id ) {
21
	$payment = new Payment( $post_id );
0 ignored issues
show
Bug introduced by
It seems like $post_id can also be of type string; however, parameter $post_id of Pronamic\WordPress\Pay\P...\Payment::__construct() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

21
	$payment = new Payment( /** @scrutinizer ignore-type */ $post_id );
Loading history...
22
23
	return $payment;
24
}
25
26
/**
27
 * Get payment by specified meta key and value.
28
 *
29
 * @param string $meta_key   The meta key to query for.
30
 * @param string $meta_value The Meta value to query for.
31
 * @return Payment|null
32
 */
33
function get_pronamic_payment_by_meta( $meta_key, $meta_value ) {
34
	global $wpdb;
35
36
	$payment = null;
37
38
	$db_query = $wpdb->prepare( "
39
		SELECT
40
			post_id
41
		FROM
42
			$wpdb->postmeta
43
		WHERE
44
			meta_key = %s
45
				AND
46
			meta_value = %s
47
			;
48
	", $meta_key, $meta_value );
49
50
	$post_id = $wpdb->get_var( $db_query ); // WPCS: unprepared SQL ok.
51
52
	if ( $post_id ) {
53
		$payment = new Payment( $post_id );
54
	}
55
56
	return $payment;
57
}
58
59
/**
60
 * Get payments by specified meta key and value.
61
 *
62
 * @param string $meta_key   The meta key to query for.
63
 * @param string $meta_value The Meta value to query for.
64
 * @return array
65
 */
66
function get_pronamic_payments_by_meta( $meta_key, $meta_value ) {
67
	global $wpdb;
68
69
	$payments = array();
70
71
	$db_query = $wpdb->prepare( "
72
		SELECT
73
			post_id
74
		FROM
75
			$wpdb->postmeta
76
		WHERE
77
			meta_key = %s
78
				AND
79
			meta_value = %s
80
		ORDER BY
81
			meta_id ASC
82
			;
83
	", $meta_key, $meta_value );
84
85
	$results = $wpdb->get_results( $db_query ); // WPCS: unprepared SQL ok.
86
87
	foreach ( $results as $result ) {
88
		$payments[] = new Payment( $result->post_id );
89
	}
90
91
	return $payments;
92
}
93
94
/**
95
 * Get payment by the specified purchase ID.
96
 *
97
 * @param string $purchase_id The purchase ID to query for.
98
 * @return Payment|null
99
 */
100
function get_pronamic_payment_by_purchase_id( $purchase_id ) {
101
	return get_pronamic_payment_by_meta( '_pronamic_payment_purchase_id', $purchase_id );
102
}
103
104
/**
105
 * Get payment by the specified transaction ID.
106
 *
107
 * @param string $transaction_id The transaction ID to query for.
108
 * @param string $entrance_code  The entrance code to query for.
109
 * @return Payment|null
110
 */
111
function get_pronamic_payment_by_transaction_id( $transaction_id, $entrance_code = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $entrance_code 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

111
function get_pronamic_payment_by_transaction_id( $transaction_id, /** @scrutinizer ignore-unused */ $entrance_code = null ) {

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...
112
	return get_pronamic_payment_by_meta( '_pronamic_payment_transaction_id', $transaction_id );
113
}
114
115
/**
116
 * Get subscription by the specified post ID.
117
 *
118
 * @param int $post_id A subscription post ID.
119
 * @return Subscription|null
120
 */
121
function get_pronamic_subscription( $post_id ) {
122
	if ( empty( $post_id ) ) {
123
		return null;
124
	}
125
126
	$subscription = new Subscription( $post_id );
127
128
	if ( ! isset( $subscription->post ) ) {
129
		return null;
130
	}
131
132
	return $subscription;
133
}
134
135
/**
136
 * Get subscription by the specified meta key and value.
137
 *
138
 * @param string $meta_key   The meta key to query for.
139
 * @param string $meta_value The Meta value to query for.
140
 * @return Subscription|null
141
 */
142
function get_pronamic_subscription_by_meta( $meta_key, $meta_value ) {
143
	global $wpdb;
144
145
	$subscription = null;
146
147
	$db_query = $wpdb->prepare( "
148
		SELECT
149
			post_id
150
		FROM
151
			$wpdb->postmeta
152
		WHERE
153
			meta_key = %s
154
				AND
155
			meta_value = %s
156
			;
157
	", $meta_key, $meta_value );
158
159
	$post_id = $wpdb->get_var( $db_query ); // WPCS: unprepared SQL ok.
160
161
	if ( $post_id ) {
162
		$subscription = new Subscription( $post_id );
163
	}
164
165
	return $subscription;
166
}
167
168
/**
169
 * Get subscriptions by specified meta key and value.
170
 *
171
 * @param string $meta_key   The meta key to query for.
172
 * @param string $meta_value The Meta value to query for.
173
 * @return array
174
 */
175
function get_pronamic_subscriptions_by_meta( $meta_key, $meta_value ) {
176
	global $wpdb;
177
178
	$subscriptions = array();
179
180
	$db_query = $wpdb->prepare( "
181
		SELECT
182
			post_id
183
		FROM
184
			$wpdb->postmeta
185
		WHERE
186
			meta_key = %s
187
				AND
188
			meta_value = %s
189
		ORDER BY
190
			meta_id ASC
191
			;
192
	", $meta_key, $meta_value );
193
194
	$results = $wpdb->get_results( $db_query ); // WPCS: unprepared SQL ok.
195
196
	foreach ( $results as $result ) {
197
		$subscriptions[] = new Subscription( $result->post_id );
198
	}
199
200
	return $subscriptions;
201
}
202
203
/**
204
 * Bind the global providers and gateways together.
205
 */
206
function bind_providers_and_gateways() {
207
	global $pronamic_pay_providers;
208
209
	global $pronamic_ideal;
210
211
	foreach ( $pronamic_ideal->gateway_integrations as $integration ) {
212
		if ( isset( $pronamic_pay_providers[ $integration->provider ] ) ) {
213
			$provider =& $pronamic_pay_providers[ $integration->provider ];
214
215
			if ( ! isset( $provider['integrations'] ) ) {
216
				$provider['integrations'] = array();
217
			}
218
219
			$provider['integrations'][] = $integration;
220
		}
221
	}
222
}
223
224
/**
225
 * Let to num function.
226
 *
227
 * This function transforms the php.ini notation for numbers (like '2M') to an integer.
228
 *
229
 * @see https://github.com/woothemes/woocommerce/blob/v2.0.20/woocommerce-core-functions.php#L1779
230
 * @access public
231
 * @param string $size A php.ini notation for nubmer to convert to an integer.
232
 * @return int
233
 */
234
function pronamic_pay_let_to_num( $size ) {
235
	$l   = substr( $size, -1 );
236
	$ret = substr( $size, 0, -1 );
237
238
	switch ( strtoupper( $l ) ) {
239
		case 'P':
240
			$ret *= 1024;
241
			// no break.
242
		case 'T':
243
			$ret *= 1024;
244
			// no break.
245
		case 'G':
246
			$ret *= 1024;
247
			// no break.
248
		case 'M':
249
			$ret *= 1024;
250
			// no break.
251
		case 'K':
252
			$ret *= 1024;
253
			// no break.
254
	}
255
256
	return $ret;
257
}
258
259
/**
260
 * Return the thousand separator.
261
 *
262
 * @return string
263
 */
264
function pronamic_pay_get_thousands_separator() {
265
	global $wp_locale;
266
267
	// Seperator.
268
	$separator = get_option( 'pronamic_pay_thousands_sep' );
269
270
	// WordPress.
271
	if ( false === $separator ) {
272
		// WordPress locale number format was introduced in WordPress version 2.3.
273
		// @see https://github.com/WordPress/WordPress/blob/2.3/wp-includes/locale.php#L90-L100.
274
		$separator = $wp_locale->number_format['thousands_sep'];
275
	}
276
277
	return $separator;
278
}
279
280
/**
281
 * Return the decimal separator.
282
 *
283
 * @return string
284
 */
285
function pronamic_pay_get_decimal_separator() {
286
	global $wp_locale;
287
288
	// Seperator.
289
	$separator = get_option( 'pronamic_pay_decimal_sep' );
290
291
	// WordPress.
292
	if ( false === $separator ) {
293
		// WordPress locale number format was introduced in WordPress version 2.3.
294
		// @see https://github.com/WordPress/WordPress/blob/2.3/wp-includes/locale.php#L90-L100.
295
		$separator = $wp_locale->number_format['decimal_point'];
296
	}
297
298
	return $separator ? $separator : '.';
299
}
300
301
/**
302
 * Pronamic Pay get page ID.
303
 *
304
 * @see https://github.com/woothemes/woocommerce/blob/v2.0.16/woocommerce-core-functions.php#L344
305
 *
306
 * @param string $page Pronamic Pay page identifier slug.
307
 * @return int
308
 */
309
function pronamic_pay_get_page_id( $page ) {
310
	$option = sprintf( 'pronamic_pay_%s_page_id', $page );
311
312
	return get_option( $option, -1 );
313
}
314
315
/**
316
 * Helper function to update post meta data.
317
 *
318
 * @see http://codex.wordpress.org/Function_Reference/update_post_meta
319
 * @param int   $post_id The post ID to update the specified meta data for.
320
 * @param array $data    The data array with meta keys/values.
321
 */
322
function pronamic_pay_update_post_meta_data( $post_id, array $data ) {
323
	/*
324
	 * Post meta values are passed through the stripslashes() function
325
	 * upon being stored, so you will need to be careful when passing
326
	 * in values such as JSON that might include \ escaped characters.
327
	 */
328
	$data = wp_slash( $data );
329
330
	// Meta.
331
	foreach ( $data as $key => $value ) {
332
		if ( isset( $value ) && '' !== $value ) {
333
			update_post_meta( $post_id, $key, $value );
334
		} else {
335
			delete_post_meta( $post_id, $key );
336
		}
337
	}
338
}
339
340