Completed
Push — master ( 690cd0...210430 )
by Mike
07:47
created

WC_Payment_Tokens::get_order_tokens()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 18
rs 9.4285
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 17 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * WooCommerce Payment Tokens.
8
 *
9
 * An API for storing and managing tokens for gateways and customers.
10
 *
11
 * @class 		WC_Payment_Tokens
12
 * @since		2.6.0
13
 * @package		WooCommerce/Classes
14
 * @category	Class
15
 * @author		WooThemes
16
 */
17
class WC_Payment_Tokens {
18
19
	/**
20
	 * Gets valid tokens from the database based on user defined criteria.
21
	 * @param  array $args
22
	 * @return array
23
	 */
24
	public static function get_tokens( $args ) {
25
		global $wpdb;
26
27
		$args = wp_parse_args( $args, array(
28
			'token_id'   => '',
29
			'user_id'    => '',
30
			'gateway_id' => '',
31
			'type'       => '',
32
		) );
33
34
		$sql   = "SELECT * FROM {$wpdb->prefix}woocommerce_payment_tokens";
35
		$where = array( '1=1' );
36
37
		if ( $args['token_id'] ) {
38
			$token_ids = array_map( 'absint', is_array( $args['token_id'] ) ? $args['token_id'] : array( $args['token_id'] ) );
39
			$where[]   = "token_id IN ('" . implode( "','", array_map( 'esc_sql', $token_ids ) ) . "')";
40
		}
41
42
		if ( $args['user_id'] ) {
43
			$where[] = 'user_id = ' . absint( $args['user_id'] );
44
		}
45
46
		if ( $args['gateway_id'] ) {
47
			$gateway_ids = array( $args['gateway_id'] );
48
		} else {
49
			$gateways    = WC_Payment_Gateways::instance();
50
			$gateway_ids = $gateways->get_payment_gateway_ids();
51
		}
52
53
		$gateway_ids[] = '';
54
		$where[]       = "gateway_id IN ('" . implode( "','", array_map( 'esc_sql', $gateway_ids ) ) . "')";
55
56
		if ( $args['type'] ) {
57
			$where[] = 'type = ' . esc_sql( $args['type'] );
58
		}
59
60
		$token_results = $wpdb->get_results( $sql . ' WHERE ' . implode( ' AND ', $where ) );
61
		$tokens        = array();
62
63
		if ( ! empty( $token_results ) ) {
64
			foreach ( $token_results as $token_result ) {
65
				$_token = self::get( $token_result->token_id, $token_result );
66
				if ( ! empty( $_token ) ) {
67
					$tokens[ $token_result->token_id ] = $_token;
68
				}
69
			}
70
		}
71
72
		return $tokens;
73
	}
74
75
	/**
76
	 * Returns an array of payment token objects associated with the passed customer ID.
77
	 * @since 2.6.0
78
	 * @param  int    $customer_id  Customer ID
79
	 * @param  string $gateway      Optional Gateway ID for getting tokens for a specific gateway
0 ignored issues
show
Documentation introduced by
There is no parameter named $gateway. Did you maybe mean $gateway_id?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
80
	 * @return array                Array of token objects
81
	 */
82
	public static function get_customer_tokens( $customer_id, $gateway_id = '' ) {
83
		if ( $customer_id < 1 ) {
84
			return array();
85
		}
86
87
		$tokens = self::get_tokens( array(
88
			'user_id'    => $customer_id,
89
			'gateway_id' => $gateway_id
90
		) );
91
92
		return apply_filters( 'woocommerce_get_customer_payment_tokens', $tokens, $customer_id, $gateway_id );
93
	}
94
95
	/**
96
	 * Returns a customers default token or NULL if there is no default token.
97
	 * @since 2.6.0
98
	 * @param  int $customer_id
99
	 * @return WC_Payment_Token|null
100
	 */
101
	public static function get_customer_default_token( $customer_id ) {
102
		if ( $customer_id < 1 ) {
103
			return null;
104
		}
105
106
		global $wpdb;
107
108
		$token = $wpdb->get_row( $wpdb->prepare(
109
			"SELECT * FROM {$wpdb->prefix}woocommerce_payment_tokens WHERE user_id = %d AND is_default = 1",
110
			$customer_id
111
		) );
112
113
		if ( $token ) {
114
			return self::get( $token->token_id, $token );
115
		} else {
116
			return null;
117
		}
118
	}
119
120
	/**
121
	 * Returns an array of payment token objects associated with the passed order ID.
122
	 * @since 2.6.0
123
	 * @param int $order_id Order ID
124
	 * @return array Array of token objects
125
	 */
126
	public static function get_order_tokens( $order_id ) {
127
		$order = wc_get_order( $order_id );
128
129
		if ( ! $order ) {
130
			return array();
131
		}
132
133
		$token_ids = get_post_meta( $order_id, '_payment_tokens', true );
134
		if ( empty( $token_ids ) ) {
135
			return array();
136
		}
137
138
		$tokens = self::get_tokens( array(
139
			'token_id' => $token_ids
140
		) );
141
142
		return apply_filters( 'woocommerce_get_order_payment_tokens', $tokens, $order_id );
143
	}
144
145
	/**
146
	 * Get a token object by ID.
147
	 * @since 2.6.0
148
	 * @param  int $token_id Token ID
149
	 * @return WC_Payment_Token|null Returns a valid payment token or null if no token can be found
150
	 */
151
	public static function get( $token_id, $token_result = null ) {
152
		global $wpdb;
153
		if ( is_null( $token_result ) ) {
154
			$token_result = $wpdb->get_row( $wpdb->prepare(
155
				"SELECT * FROM {$wpdb->prefix}woocommerce_payment_tokens WHERE token_id = %d",
156
				$token_id
157
			) );
158
			// Still empty? Token doesn't exist? Don't continue
159
			if ( empty( $token_result ) ) {
160
				return null;
161
			}
162
		}
163
		$token_class = 'WC_Payment_Token_' . $token_result->type;
164
		if ( class_exists( $token_class ) ) {
165
			$meta =  get_metadata( 'payment_token', $token_id );
166
			$passed_meta = array();
167
			if ( ! empty( $meta ) ) {
168
				foreach( $meta as $meta_key => $meta_value ) {
169
					$passed_meta[ $meta_key ] = $meta_value[0];
170
				}
171
			}
172
			return new $token_class( $token_id, (array) $token_result, $passed_meta );
173
		}
174
	}
175
176
	/**
177
	 * Remove a payment token from the database by ID.
178
	 * @since 2.6.0
179
	 * @param WC_Payment_Token $token_id Token ID
180
	 */
181
	public static function delete( $token_id ) {
182
		$type = self::get_token_type_by_id( $token_id );
0 ignored issues
show
Documentation introduced by
$token_id is of type object<WC_Payment_Token>, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
183
		if ( ! empty ( $type ) ) {
184
			$class = 'WC_Payment_Token_' . $type;
185
			$token = new $class( $token_id );
186
			$token->delete();
187
		}
188
	}
189
190
	/**
191
	 * Loops through all of a users payment tokens and sets is_default to false for all but a specific token.
192
	 * @since 2.6.0
193
	 * @param int $user_id  User to set a default for
194
	 * @param int $token_id The ID of the token that should be default
195
	 */
196
	public static function set_users_default( $user_id, $token_id ) {
197
		global $wpdb; // DB queries so we avoid an  infinite loop (update & create use this function)
198
		$users_tokens = self::get_customer_tokens( $user_id );
199
		foreach ( $users_tokens as $token ) {
200
			if ( $token_id === $token->get_id() ) {
201
				$token->set_default( true );
202
				$wpdb->update(
203
					$wpdb->prefix . 'woocommerce_payment_tokens',
204
					array( 'is_default' => 1 ),
205
					array( 'token_id' => $token->get_id(),
206
				) );
207
			} else {
208
				$token->set_default( false );
209
				$wpdb->update(
210
					$wpdb->prefix . 'woocommerce_payment_tokens',
211
					array( 'is_default' => 0 ),
212
					array( 'token_id' => $token->get_id(),
213
				) );
214
			}
215
		}
216
		do_action( 'woocommerce_payment_token_set_default', $token_id );
217
	}
218
219
	/**
220
	 * Returns what type (credit card, echeck, etc) of token a token is by ID.
221
	 * @since 2.6.0
222
	 * @param  int $token_id Token ID
223
	 * @return string        Type
224
	 */
225
	public static function get_token_type_by_id( $token_id ) {
226
		global $wpdb;
227
		$type = $wpdb->get_var( $wpdb->prepare(
228
			"SELECT type FROM {$wpdb->prefix}woocommerce_payment_tokens WHERE token_id = %d",
229
			$token_id
230
		) );
231
		return $type;
232
	}
233
234
}
235