Cancelled
Push — master ( 86817a...a519f9 )
by Roy
38s queued 38s
created

legacy/class-wc-gateway-stripe-saved-cards.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * WC_Gateway_Stripe_Saved_Cards class.
8
 */
9
class WC_Gateway_Stripe_Saved_Cards {
10
11
	/**
12
	 * Constructor
13
	 */
14
	public function __construct() {
15
		add_action( 'wp', array( $this, 'delete_card' ) );
16
		add_action( 'woocommerce_after_my_account', array( $this, 'output' ) );
17
		add_action( 'wp', array( $this, 'default_card' ) );
18
	}
19
20
	/**
21
	 * Display saved cards
22
	 */
23
	public function output() {
24
		if ( ! is_user_logged_in() ) {
25
			return;
26
		}
27
28
		$stripe_customer = new WC_Stripe_Customer( get_current_user_id() );
29
		$stripe_cards    = $stripe_customer->get_cards();
30
		$default_card    = $stripe_customer->get_default_card();
31
32
		if ( $stripe_cards ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $stripe_cards of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
33
			wc_get_template( 'saved-cards.php', array( 'cards' => $stripe_cards, 'default_card' => $default_card ), 'woocommerce-gateway-stripe/', untrailingslashit( plugin_dir_path( WC_STRIPE_MAIN_FILE ) ) . '/includes/legacy/templates/' );
34
		}
35
	}
36
37
	/**
38
	 * Delete a card
39
	 */
40 View Code Duplication
	public function delete_card() {
41
		if ( ! isset( $_POST['stripe_delete_card'] ) || ! is_account_page() ) {
42
			return;
43
		}
44
45
		$stripe_customer    = new WC_Stripe_Customer( get_current_user_id() );
46
		$stripe_customer_id = $stripe_customer->get_id();
47
		$delete_card        = sanitize_text_field( $_POST['stripe_delete_card'] );
48
49
		if ( ! is_user_logged_in() || ! $stripe_customer_id || ! wp_verify_nonce( $_POST['_wpnonce'], "stripe_del_card" ) ) {
50
			wp_die( __( 'Unable to make default card, please try again', 'woocommerce-gateway-stripe' ) );
51
		}
52
53
		if ( ! $stripe_customer->delete_card( $delete_card ) ) {
54
			wc_add_notice( __( 'Unable to delete card.', 'woocommerce-gateway-stripe' ), 'error' );
55
		} else {
56
			wc_add_notice( __( 'Card deleted.', 'woocommerce-gateway-stripe' ), 'success' );
57
		}
58
	}
59
60
	/**
61
	 * Make a card as default method
62
	 */
63 View Code Duplication
	public function default_card() {
64
		if ( ! isset( $_POST['stripe_default_card'] ) || ! is_account_page() ) {
65
			return;
66
		}
67
68
		$stripe_customer    = new WC_Stripe_Customer( get_current_user_id() );
69
		$stripe_customer_id = $stripe_customer->get_id();
70
		$default_source     = sanitize_text_field( $_POST['stripe_default_card'] );
71
72
		if ( ! is_user_logged_in() || ! $stripe_customer_id || ! wp_verify_nonce( $_POST['_wpnonce'], "stripe_default_card" ) ) {
73
			wp_die( __( 'Unable to make default card, please try again', 'woocommerce-gateway-stripe' ) );
74
		}
75
76
		if ( ! $stripe_customer->set_default_card( $default_source ) ) {
77
			wc_add_notice( __( 'Unable to update default card.', 'woocommerce-gateway-stripe' ), 'error' );
78
		} else {
79
			wc_add_notice( __( 'Default card updated.', 'woocommerce-gateway-stripe' ), 'success' );
80
		}
81
	}
82
}
83
new WC_Gateway_Stripe_Saved_Cards();
84