WC_Payment_Token_Data_Store::get_tokens()publicWC 3.0.0

Returns an array of objects (stdObject) matching specific token criteria. Accepts token_id, user_id, gateway_id, and type. Each object should contain the fields token_id, gateway_id, token, user_id, type, is_default.

Method of the class: WC_Payment_Token_Data_Store{}

No Hooks.

Return

Array.

Usage

$WC_Payment_Token_Data_Store = new WC_Payment_Token_Data_Store();
$WC_Payment_Token_Data_Store->get_tokens( $args );
$args(array) (required)
List of accepted args: token_id, gateway_id, user_id, type.

Changelog

Since 3.0.0 Introduced.

WC_Payment_Token_Data_Store::get_tokens() code WC 9.4.2

public function get_tokens( $args ) {
	global $wpdb;
	$args = wp_parse_args(
		$args,
		array(
			'token_id'   => '',
			'user_id'    => '',
			'gateway_id' => '',
			'type'       => '',
		)
	);

	$sql   = "SELECT * FROM {$wpdb->prefix}woocommerce_payment_tokens";
	$where = array( '1=1' );

	if ( $args['token_id'] ) {
		$token_ids = array_map( 'absint', is_array( $args['token_id'] ) ? $args['token_id'] : array( $args['token_id'] ) );
		$where[]   = "token_id IN ('" . implode( "','", array_map( 'esc_sql', $token_ids ) ) . "')";
	}

	if ( $args['user_id'] ) {
		$where[] = $wpdb->prepare( 'user_id = %d', absint( $args['user_id'] ) );
	}

	if ( $args['gateway_id'] ) {
		$gateway_ids = array( $args['gateway_id'] );
	} else {
		$gateways    = WC_Payment_Gateways::instance();
		$gateway_ids = $gateways->get_payment_gateway_ids();
	}

	$page           = isset( $args['page'] ) ? absint( $args['page'] ) : 1;
	$posts_per_page = absint( isset( $args['limit'] ) ? $args['limit'] : get_option( 'posts_per_page' ) );

	$pgstrt = absint( ( $page - 1 ) * $posts_per_page ) . ', ';
	$limits = 'LIMIT ' . $pgstrt . $posts_per_page;

	$gateway_ids[] = '';
	$where[]       = "gateway_id IN ('" . implode( "','", array_map( 'esc_sql', $gateway_ids ) ) . "')";

	if ( $args['type'] ) {
		$where[] = $wpdb->prepare( 'type = %s', $args['type'] );
	}

	// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
	$token_results = $wpdb->get_results( $sql . ' WHERE ' . implode( ' AND ', $where ) . ' ' . $limits );

	return $token_results;
}