wc-account-functions.php ➔ wc_get_account_orders_columns()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
eloc 8
nc 1
nop 0
1
<?php
2
/**
3
 * WooCommerce Account Functions
4
 *
5
 * Functions for account specific things.
6
 *
7
 * @author   WooThemes
8
 * @category Core
9
 * @package  WooCommerce/Functions
10
 * @version  2.6.0
11
 */
12
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Returns the url to the lost password endpoint url.
19
 *
20
 * @access public
21
 * @param  string $default_url
22
 * @return string
23
 */
24
function wc_lostpassword_url( $default_url = '' ) {
25
	$wc_password_reset_url = wc_get_page_permalink( 'myaccount' );
26
27
	if ( false !== $wc_password_reset_url ) {
28
		return wc_get_endpoint_url( 'lost-password', '', $wc_password_reset_url );
29
	} else {
30
		return $default_url;
31
	}
32
}
33
34
add_filter( 'lostpassword_url', 'wc_lostpassword_url', 10, 1 );
35
36
/**
37
 * Get the link to the edit account details page.
38
 *
39
 * @return string
40
 */
41
function wc_customer_edit_account_url() {
42
	$edit_account_url = wc_get_endpoint_url( 'edit-account', '', wc_get_page_permalink( 'myaccount' ) );
43
44
	return apply_filters( 'woocommerce_customer_edit_account_url', $edit_account_url );
45
}
46
47
/**
48
 * Get the edit address slug translation.
49
 *
50
 * @param  string  $id   Address ID.
51
 * @param  bool    $flip Flip the array to make it possible to retrieve the values ​​from both sides.
52
 *
53
 * @return string        Address slug i18n.
54
 */
55
function wc_edit_address_i18n( $id, $flip = false ) {
56
	$slugs = apply_filters( 'woocommerce_edit_address_slugs', array(
57
		'billing'  => sanitize_title( _x( 'billing', 'edit-address-slug', 'woocommerce' ) ),
58
		'shipping' => sanitize_title( _x( 'shipping', 'edit-address-slug', 'woocommerce' ) )
59
	) );
60
61
	if ( $flip ) {
62
		$slugs = array_flip( $slugs );
63
	}
64
65
	if ( ! isset( $slugs[ $id ] ) ) {
66
		return $id;
67
	}
68
69
	return $slugs[ $id ];
70
}
71
72
/**
73
 * Get My Account menu items.
74
 *
75
 * @since 2.6.0
76
 * @return array
77
 */
78
function wc_get_account_menu_items() {
79
	$items = array(
80
		'dashboard'       => __( 'Dashboard', 'woocommerce' ),
81
		'orders'          => __( 'Orders', 'woocommerce' ),
82
		'downloads'       => __( 'Downloads', 'woocommerce' ),
83
		'edit-address'    => __( 'Addresses', 'woocommerce' ),
84
		'payment-methods' => __( 'Payment Methods', 'woocommerce' ),
85
		'edit-account'    => __( 'Account Details', 'woocommerce' ),
86
		'customer-logout' => __( 'Logout', 'woocommerce' ),
87
	);
88
89
	// Remove empty items.
90
	$downloads = WC()->customer->get_downloadable_products();
91
92
	if ( ! sizeof( $downloads ) ) {
93
		unset( $items['downloads'] );
94
	}
95
96
	return apply_filters( 'woocommerce_account_menu_items', $items );
97
}
98
99
/**
100
 * Get account menu item classes.
101
 *
102
 * @since 2.6.0
103
 * @param string $endpoint
104
 * @return string
105
 */
106
function wc_get_account_menu_item_classes( $endpoint ) {
107
	global $wp;
108
109
	$classes = array(
110
		'woocommerce-MyAccount-navigation-link',
111
		'woocommerce-MyAccount-navigation-link--' . $endpoint,
112
	);
113
114
	// Set current item class.
115
	$current = isset( $wp->query_vars[ $endpoint ] );
116
	if ( 'dashboard' === $endpoint && ( isset( $wp->query_vars['page'] ) || empty( $wp->query_vars ) ) ) {
117
		$current = true; // Dashboard is not an endpoint, so needs a custom check.
118
	}
119
120
	if ( $current ) {
121
		$classes[] = 'is-active';
122
	}
123
124
	$classes = apply_filters( 'woocommerce_account_menu_item_classes', $classes, $endpoint );
125
126
	return implode( ' ', array_map( 'sanitize_html_class', $classes ) );
127
}
128
129
/**
130
 * Get account endpoint URL.
131
 *
132
 * @since 2.6.0
133
 * @param string $endpoint
134
 * @return string
135
 */
136
function wc_get_account_endpoint_url( $endpoint ) {
137
	if ( 'dashboard' === $endpoint ) {
138
		return wc_get_page_permalink( 'myaccount' );
139
	}
140
141
	return wc_get_endpoint_url( $endpoint );
142
}
143
144
/**
145
 * Get My Account > Orders columns.
146
 *
147
 * @since 2.6.0
148
 * @return array
149
 */
150
function wc_get_account_orders_columns() {
151
	$columns = apply_filters( 'woocommerce_account_orders_columns', array(
152
		'order-number'  => __( 'Order', 'woocommerce' ),
153
		'order-date'    => __( 'Date', 'woocommerce' ),
154
		'order-status'  => __( 'Status', 'woocommerce' ),
155
		'order-total'   => __( 'Total', 'woocommerce' ),
156
		'order-actions' => '&nbsp;',
157
	) );
158
159
	// Deprecated filter since 2.6.0.
160
	return apply_filters( 'woocommerce_my_account_my_orders_columns', $columns );
161
}
162
163
/**
164
 * Get My Account > Downloads columns.
165
 *
166
 * @since 2.6.0
167
 * @return array
168
 */
169
function wc_get_account_downloads_columns() {
170
	return apply_filters( 'woocommerce_account_downloads_columns', array(
171
		'download-file'      => __( 'File', 'woocommerce' ),
172
		'download-remaining' => __( 'Remaining', 'woocommerce' ),
173
		'download-expires'   => __( 'Expires', 'woocommerce' ),
174
		'download-actions'   => '&nbsp;',
175
	) );
176
}
177
178
/**
179
 * Get My Account > Payment methods columns.
180
 *
181
 * @since 2.6.0
182
 * @return array
183
 */
184
function wc_get_account_payment_methods_columns() {
185
	return apply_filters( 'woocommerce_account_payment_methods_columns', array(
186
		'method'  => __( 'Method', 'woocommerce' ),
187
		'expires' => __( 'Expires', 'woocommerce' ),
188
		'actions' => '&nbsp;',
189
	) );
190
}
191
192
/**
193
 * Get My Account > Payment methods types
194
 *
195
 * @since 2.6.0
196
 * @return array
197
 */
198
function wc_get_account_payment_methods_types() {
199
	return apply_filters( 'woocommerce_payment_methods_types', array(
200
		'cc'     => __( 'Credit Card', 'woocommerce' ),
201
		'echeck' => __( 'eCheck', 'woocommerce' ),
202
	) );
203
}
204
205
/**
206
 * Returns an array of a user's saved payments list for output on the account tab.
207
 *
208
 * @since  2.6
209
 * @param  array $list         List of payment methods passed from wc_get_customer_saved_methods_list()
210
 * @param  int   $customer_id  The customer to fetch payment methods for
211
 * @return array               Filtered list of customers payment methods
212
 */
213
function wc_get_account_saved_payment_methods_list( $list, $customer_id ) {
214
	$payment_tokens = WC_Payment_Tokens::get_customer_tokens( $customer_id );
215
	foreach ( $payment_tokens as $payment_token ) {
216
		$delete_url      = wc_get_endpoint_url( 'delete-payment-method', $payment_token->get_id() );
217
		$delete_url      = wp_nonce_url( $delete_url, 'delete-payment-method-' . $payment_token->get_id() );
218
		$set_default_url = wc_get_endpoint_url( 'set-default-payment-method', $payment_token->get_id() );
219
		$set_default_url = wp_nonce_url( $set_default_url, 'set-default-payment-method-' . $payment_token->get_id() );
220
221
		$type            = strtolower( $payment_token->get_type() );
222
		$list[ $type ][] = array(
223
			'method' => array(
224
				'gateway' => $payment_token->get_gateway_id(),
225
			),
226
			'expires'    => esc_html__( 'N/A', 'woocommerce' ),
227
			'is_default' => $payment_token->is_default(),
228
			'actions'    => array(
229
				'delete' => array(
230
					'url'  => $delete_url,
231
					'name' => esc_html__( 'Delete', 'woocommerce' ),
232
				),
233
			),
234
		);
235
		$key = key( array_slice( $list[ $type ], -1, 1, true ) );
236
237
		if ( ! $payment_token->is_default() ) {
238
			$list[ $type ][$key]['actions']['default'] = array(
239
				'url' => $set_default_url,
240
				'name' => esc_html__( 'Make Default', 'woocommerce' ),
241
			);
242
		}
243
244
		$list[ $type ][ $key ] = apply_filters( 'woocommerce_payment_methods_list_item', $list[ $type ][ $key ], $payment_token );
245
	}
246
	return $list;
247
}
248
249
add_filter( 'woocommerce_saved_payment_methods_list', 'wc_get_account_saved_payment_methods_list', 10, 2 );
250
251
/**
252
 * Controls the output for credit cards on the my account page.
253
 *
254
 * @since 2.6
255
 * @param  array             $item         Individual list item from woocommerce_saved_payment_methods_list
256
 * @param  WC_Payment_Token $payment_token The payment token associated with this method entry
257
 * @return array                           Filtered item
258
 */
259
function wc_get_account_saved_payment_methods_list_item_cc( $item, $payment_token ) {
260
	if ( 'cc' !== strtolower( $payment_token->get_type() ) ) {
261
		return $item;
262
	}
263
264
	$card_type               = $payment_token->get_card_type();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class WC_Payment_Token as the method get_card_type() does only exist in the following sub-classes of WC_Payment_Token: WC_Payment_Token_CC. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
265
	$item['method']['last4'] = $payment_token->get_last4();
266
	$item['method']['brand'] = ( ! empty( $card_type ) ? ucfirst( $card_type ) : esc_html__( 'Credit Card', 'woocommerce' ) );
267
	$item['expires']         = $payment_token->get_expiry_month() . '/' . substr( $payment_token->get_expiry_year(), -2 );
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class WC_Payment_Token as the method get_expiry_month() does only exist in the following sub-classes of WC_Payment_Token: WC_Payment_Token_CC. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
Bug introduced by
It seems like you code against a specific sub-type and not the parent class WC_Payment_Token as the method get_expiry_year() does only exist in the following sub-classes of WC_Payment_Token: WC_Payment_Token_CC. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
268
269
	return $item;
270
}
271
272
add_filter( 'woocommerce_payment_methods_list_item', 'wc_get_account_saved_payment_methods_list_item_cc', 10, 2 );
273
274
/**
275
 * Controls the output for eChecks on the my account page.
276
 *
277
 * @since 2.6
278
 * @param  array             $item         Individual list item from woocommerce_saved_payment_methods_list
279
 * @param  WC_Payment_Token $payment_token The payment token associated with this method entry
280
 * @return array                           Filtered item
281
 */
282
function wc_get_account_saved_payment_methods_list_item_echeck( $item, $payment_token ) {
283
	if ( 'echeck' !== strtolower( $payment_token->get_type() ) ) {
284
		return $item;
285
	}
286
287
	$item['method']['last4'] = $payment_token->get_last4();
288
	$item['method']['brand'] =  esc_html__( 'eCheck', 'woocommerce' );
289
290
	return $item;
291
}
292
293
add_filter( 'woocommerce_payment_methods_list_item', 'wc_get_account_saved_payment_methods_list_item_echeck', 10, 2 );
294