abstracts
9 years ago
admin
9 years ago
api
9 years ago
cli
9 years ago
data-stores
9 years ago
emails
9 years ago
gateways
9 years ago
interfaces
9 years ago
legacy
9 years ago
libraries
9 years ago
log-handlers
9 years ago
payment-tokens
9 years ago
shipping
9 years ago
shortcodes
9 years ago
theme-support
9 years ago
vendor
9 years ago
walkers
9 years ago
widgets
9 years ago
class-wc-ajax.php
9 years ago
class-wc-api.php
9 years ago
class-wc-auth.php
9 years ago
class-wc-autoloader.php
9 years ago
class-wc-background-emailer.php
9 years ago
class-wc-background-updater.php
9 years ago
class-wc-breadcrumb.php
9 years ago
class-wc-cache-helper.php
9 years ago
class-wc-cart.php
9 years ago
class-wc-checkout.php
9 years ago
class-wc-cli.php
9 years ago
class-wc-comments.php
9 years ago
class-wc-countries.php
9 years ago
class-wc-coupon.php
9 years ago
class-wc-customer-download.php
9 years ago
class-wc-customer.php
9 years ago
class-wc-data-exception.php
9 years ago
class-wc-data-store.php
9 years ago
class-wc-datetime.php
9 years ago
class-wc-deprecated-action-hooks.php
9 years ago
class-wc-deprecated-filter-hooks.php
9 years ago
class-wc-download-handler.php
9 years ago
class-wc-emails.php
9 years ago
class-wc-embed.php
9 years ago
class-wc-form-handler.php
9 years ago
class-wc-frontend-scripts.php
9 years ago
class-wc-geo-ip.php
9 years ago
class-wc-geolocation.php
9 years ago
class-wc-https.php
9 years ago
class-wc-install.php
9 years ago
class-wc-integrations.php
9 years ago
class-wc-legacy-api.php
9 years ago
class-wc-log-levels.php
9 years ago
class-wc-logger.php
9 years ago
class-wc-order-factory.php
9 years ago
class-wc-order-item-coupon.php
9 years ago
class-wc-order-item-fee.php
9 years ago
class-wc-order-item-meta.php
9 years ago
class-wc-order-item-product.php
9 years ago
class-wc-order-item-shipping.php
9 years ago
class-wc-order-item-tax.php
9 years ago
class-wc-order-item.php
9 years ago
class-wc-order-refund.php
9 years ago
class-wc-order.php
9 years ago
class-wc-payment-gateways.php
9 years ago
class-wc-payment-tokens.php
9 years ago
class-wc-post-data.php
9 years ago
class-wc-post-types.php
9 years ago
class-wc-product-attribute.php
9 years ago
class-wc-product-download.php
9 years ago
class-wc-product-external.php
9 years ago
class-wc-product-factory.php
9 years ago
class-wc-product-grouped.php
9 years ago
class-wc-product-simple.php
9 years ago
class-wc-product-variable.php
9 years ago
class-wc-product-variation.php
9 years ago
class-wc-query.php
9 years ago
class-wc-register-wp-admin-settings.php
9 years ago
class-wc-session-handler.php
9 years ago
class-wc-shipping-rate.php
9 years ago
class-wc-shipping-zone.php
9 years ago
class-wc-shipping-zones.php
9 years ago
class-wc-shipping.php
9 years ago
class-wc-shortcodes.php
9 years ago
class-wc-structured-data.php
9 years ago
class-wc-tax.php
9 years ago
class-wc-template-loader.php
9 years ago
class-wc-tracker.php
9 years ago
class-wc-validation.php
9 years ago
class-wc-webhook.php
9 years ago
wc-account-functions.php
9 years ago
wc-attribute-functions.php
9 years ago
wc-cart-functions.php
9 years ago
wc-conditional-functions.php
9 years ago
wc-core-functions.php
9 years ago
wc-coupon-functions.php
9 years ago
wc-deprecated-functions.php
9 years ago
wc-formatting-functions.php
9 years ago
wc-notice-functions.php
9 years ago
wc-order-functions.php
9 years ago
wc-order-item-functions.php
9 years ago
wc-page-functions.php
9 years ago
wc-product-functions.php
9 years ago
wc-rest-functions.php
9 years ago
wc-stock-functions.php
9 years ago
wc-template-functions.php
9 years ago
wc-template-hooks.php
9 years ago
wc-term-functions.php
9 years ago
wc-update-functions.php
9 years ago
wc-user-functions.php
9 years ago
wc-webhook-functions.php
9 years ago
wc-widget-functions.php
9 years ago
wc-account-functions.php
332 lines
| 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 | // Don't redirect to the woocommerce endpoint on global network admin lost passwords. |
| 26 | if ( is_multisite() && isset( $_GET['redirect_to'] ) && false !== strpos( $_GET['redirect_to'], network_admin_url() ) ) { |
| 27 | return $default_url; |
| 28 | } |
| 29 | |
| 30 | $wc_account_page_url = wc_get_page_permalink( 'myaccount' ); |
| 31 | $wc_account_page_exists = wc_get_page_id( 'myaccount' ) > 0; |
| 32 | $lost_password_endpoint = get_option( 'woocommerce_myaccount_lost_password_endpoint' ); |
| 33 | |
| 34 | if ( $wc_account_page_exists && ! empty( $lost_password_endpoint ) ) { |
| 35 | return wc_get_endpoint_url( $lost_password_endpoint, '', $wc_account_page_url ); |
| 36 | } else { |
| 37 | return $default_url; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | add_filter( 'lostpassword_url', 'wc_lostpassword_url', 10, 1 ); |
| 42 | |
| 43 | /** |
| 44 | * Get the link to the edit account details page. |
| 45 | * |
| 46 | * @return string |
| 47 | */ |
| 48 | function wc_customer_edit_account_url() { |
| 49 | $edit_account_url = wc_get_endpoint_url( 'edit-account', '', wc_get_page_permalink( 'myaccount' ) ); |
| 50 | |
| 51 | return apply_filters( 'woocommerce_customer_edit_account_url', $edit_account_url ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get the edit address slug translation. |
| 56 | * |
| 57 | * @param string $id Address ID. |
| 58 | * @param bool $flip Flip the array to make it possible to retrieve the values from both sides. |
| 59 | * |
| 60 | * @return string Address slug i18n. |
| 61 | */ |
| 62 | function wc_edit_address_i18n( $id, $flip = false ) { |
| 63 | $slugs = apply_filters( 'woocommerce_edit_address_slugs', array( |
| 64 | 'billing' => sanitize_title( _x( 'billing', 'edit-address-slug', 'woocommerce' ) ), |
| 65 | 'shipping' => sanitize_title( _x( 'shipping', 'edit-address-slug', 'woocommerce' ) ), |
| 66 | ) ); |
| 67 | |
| 68 | if ( $flip ) { |
| 69 | $slugs = array_flip( $slugs ); |
| 70 | } |
| 71 | |
| 72 | if ( ! isset( $slugs[ $id ] ) ) { |
| 73 | return $id; |
| 74 | } |
| 75 | |
| 76 | return $slugs[ $id ]; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get My Account menu items. |
| 81 | * |
| 82 | * @since 2.6.0 |
| 83 | * @return array |
| 84 | */ |
| 85 | function wc_get_account_menu_items() { |
| 86 | $endpoints = array( |
| 87 | 'orders' => get_option( 'woocommerce_myaccount_orders_endpoint', 'orders' ), |
| 88 | 'downloads' => get_option( 'woocommerce_myaccount_downloads_endpoint', 'downloads' ), |
| 89 | 'edit-address' => get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ), |
| 90 | 'payment-methods' => get_option( 'woocommerce_myaccount_payment_methods_endpoint', 'payment-methods' ), |
| 91 | 'edit-account' => get_option( 'woocommerce_myaccount_edit_account_endpoint', 'edit-account' ), |
| 92 | 'customer-logout' => get_option( 'woocommerce_logout_endpoint', 'customer-logout' ), |
| 93 | ); |
| 94 | |
| 95 | $items = array( |
| 96 | 'dashboard' => __( 'Dashboard', 'woocommerce' ), |
| 97 | 'orders' => __( 'Orders', 'woocommerce' ), |
| 98 | 'downloads' => __( 'Downloads', 'woocommerce' ), |
| 99 | 'edit-address' => __( 'Addresses', 'woocommerce' ), |
| 100 | 'payment-methods' => __( 'Payment methods', 'woocommerce' ), |
| 101 | 'edit-account' => __( 'Account details', 'woocommerce' ), |
| 102 | 'customer-logout' => __( 'Logout', 'woocommerce' ), |
| 103 | ); |
| 104 | |
| 105 | // Remove missing endpoints. |
| 106 | foreach ( $endpoints as $endpoint_id => $endpoint ) { |
| 107 | if ( empty( $endpoint ) ) { |
| 108 | unset( $items[ $endpoint_id ] ); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Check if payment gateways support add new payment methods. |
| 113 | if ( isset( $items['payment-methods'] ) ) { |
| 114 | $support_payment_methods = false; |
| 115 | foreach ( WC()->payment_gateways->get_available_payment_gateways() as $gateway ) { |
| 116 | if ( $gateway->supports( 'add_payment_method' ) || $gateway->supports( 'tokenization' ) ) { |
| 117 | $support_payment_methods = true; |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if ( ! $support_payment_methods ) { |
| 123 | unset( $items['payment-methods'] ); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return apply_filters( 'woocommerce_account_menu_items', $items ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get account menu item classes. |
| 132 | * |
| 133 | * @since 2.6.0 |
| 134 | * @param string $endpoint |
| 135 | * @return string |
| 136 | */ |
| 137 | function wc_get_account_menu_item_classes( $endpoint ) { |
| 138 | global $wp; |
| 139 | |
| 140 | $classes = array( |
| 141 | 'woocommerce-MyAccount-navigation-link', |
| 142 | 'woocommerce-MyAccount-navigation-link--' . $endpoint, |
| 143 | ); |
| 144 | |
| 145 | // Set current item class. |
| 146 | $current = isset( $wp->query_vars[ $endpoint ] ); |
| 147 | if ( 'dashboard' === $endpoint && ( isset( $wp->query_vars['page'] ) || empty( $wp->query_vars ) ) ) { |
| 148 | $current = true; // Dashboard is not an endpoint, so needs a custom check. |
| 149 | } |
| 150 | |
| 151 | if ( $current ) { |
| 152 | $classes[] = 'is-active'; |
| 153 | } |
| 154 | |
| 155 | $classes = apply_filters( 'woocommerce_account_menu_item_classes', $classes, $endpoint ); |
| 156 | |
| 157 | return implode( ' ', array_map( 'sanitize_html_class', $classes ) ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Get account endpoint URL. |
| 162 | * |
| 163 | * @since 2.6.0 |
| 164 | * @param string $endpoint |
| 165 | * @return string |
| 166 | */ |
| 167 | function wc_get_account_endpoint_url( $endpoint ) { |
| 168 | if ( 'dashboard' === $endpoint ) { |
| 169 | return wc_get_page_permalink( 'myaccount' ); |
| 170 | } |
| 171 | |
| 172 | return wc_get_endpoint_url( $endpoint, '', wc_get_page_permalink( 'myaccount' ) ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get My Account > Orders columns. |
| 177 | * |
| 178 | * @since 2.6.0 |
| 179 | * @return array |
| 180 | */ |
| 181 | function wc_get_account_orders_columns() { |
| 182 | $columns = apply_filters( 'woocommerce_account_orders_columns', array( |
| 183 | 'order-number' => __( 'Order', 'woocommerce' ), |
| 184 | 'order-date' => __( 'Date', 'woocommerce' ), |
| 185 | 'order-status' => __( 'Status', 'woocommerce' ), |
| 186 | 'order-total' => __( 'Total', 'woocommerce' ), |
| 187 | 'order-actions' => ' ', |
| 188 | ) ); |
| 189 | |
| 190 | // Deprecated filter since 2.6.0. |
| 191 | return apply_filters( 'woocommerce_my_account_my_orders_columns', $columns ); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Get My Account > Downloads columns. |
| 196 | * |
| 197 | * @since 2.6.0 |
| 198 | * @return array |
| 199 | */ |
| 200 | function wc_get_account_downloads_columns() { |
| 201 | $columns = apply_filters( 'woocommerce_account_downloads_columns', array( |
| 202 | 'download-product' => __( 'Product', 'woocommerce' ), |
| 203 | 'download-remaining' => __( 'Downloads remaining', 'woocommerce' ), |
| 204 | 'download-expires' => __( 'Expires', 'woocommerce' ), |
| 205 | 'download-file' => __( 'File', 'woocommerce' ), |
| 206 | 'download-actions' => ' ', |
| 207 | ) ); |
| 208 | |
| 209 | if ( ! has_filter( 'woocommerce_account_download_actions' ) ) { |
| 210 | unset( $columns['download-actions'] ); |
| 211 | } |
| 212 | |
| 213 | return $columns; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Get My Account > Payment methods columns. |
| 218 | * |
| 219 | * @since 2.6.0 |
| 220 | * @return array |
| 221 | */ |
| 222 | function wc_get_account_payment_methods_columns() { |
| 223 | return apply_filters( 'woocommerce_account_payment_methods_columns', array( |
| 224 | 'method' => __( 'Method', 'woocommerce' ), |
| 225 | 'expires' => __( 'Expires', 'woocommerce' ), |
| 226 | 'actions' => ' ', |
| 227 | ) ); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Get My Account > Payment methods types |
| 232 | * |
| 233 | * @since 2.6.0 |
| 234 | * @return array |
| 235 | */ |
| 236 | function wc_get_account_payment_methods_types() { |
| 237 | return apply_filters( 'woocommerce_payment_methods_types', array( |
| 238 | 'cc' => __( 'Credit card', 'woocommerce' ), |
| 239 | 'echeck' => __( 'eCheck', 'woocommerce' ), |
| 240 | ) ); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Returns an array of a user's saved payments list for output on the account tab. |
| 245 | * |
| 246 | * @since 2.6 |
| 247 | * @param array $list List of payment methods passed from wc_get_customer_saved_methods_list() |
| 248 | * @param int $customer_id The customer to fetch payment methods for |
| 249 | * @return array Filtered list of customers payment methods |
| 250 | */ |
| 251 | function wc_get_account_saved_payment_methods_list( $list, $customer_id ) { |
| 252 | $payment_tokens = WC_Payment_Tokens::get_customer_tokens( $customer_id ); |
| 253 | foreach ( $payment_tokens as $payment_token ) { |
| 254 | $delete_url = wc_get_endpoint_url( 'delete-payment-method', $payment_token->get_id() ); |
| 255 | $delete_url = wp_nonce_url( $delete_url, 'delete-payment-method-' . $payment_token->get_id() ); |
| 256 | $set_default_url = wc_get_endpoint_url( 'set-default-payment-method', $payment_token->get_id() ); |
| 257 | $set_default_url = wp_nonce_url( $set_default_url, 'set-default-payment-method-' . $payment_token->get_id() ); |
| 258 | |
| 259 | $type = strtolower( $payment_token->get_type() ); |
| 260 | $list[ $type ][] = array( |
| 261 | 'method' => array( |
| 262 | 'gateway' => $payment_token->get_gateway_id(), |
| 263 | ), |
| 264 | 'expires' => esc_html__( 'N/A', 'woocommerce' ), |
| 265 | 'is_default' => $payment_token->is_default(), |
| 266 | 'actions' => array( |
| 267 | 'delete' => array( |
| 268 | 'url' => $delete_url, |
| 269 | 'name' => esc_html__( 'Delete', 'woocommerce' ), |
| 270 | ), |
| 271 | ), |
| 272 | ); |
| 273 | $key = key( array_slice( $list[ $type ], -1, 1, true ) ); |
| 274 | |
| 275 | if ( ! $payment_token->is_default() ) { |
| 276 | $list[ $type ][ $key ]['actions']['default'] = array( |
| 277 | 'url' => $set_default_url, |
| 278 | 'name' => esc_html__( 'Make default', 'woocommerce' ), |
| 279 | ); |
| 280 | } |
| 281 | |
| 282 | $list[ $type ][ $key ] = apply_filters( 'woocommerce_payment_methods_list_item', $list[ $type ][ $key ], $payment_token ); |
| 283 | } |
| 284 | return $list; |
| 285 | } |
| 286 | |
| 287 | add_filter( 'woocommerce_saved_payment_methods_list', 'wc_get_account_saved_payment_methods_list', 10, 2 ); |
| 288 | |
| 289 | /** |
| 290 | * Controls the output for credit cards on the my account page. |
| 291 | * |
| 292 | * @since 2.6 |
| 293 | * @param array $item Individual list item from woocommerce_saved_payment_methods_list |
| 294 | * @param WC_Payment_Token $payment_token The payment token associated with this method entry |
| 295 | * @return array Filtered item |
| 296 | */ |
| 297 | function wc_get_account_saved_payment_methods_list_item_cc( $item, $payment_token ) { |
| 298 | if ( 'cc' !== strtolower( $payment_token->get_type() ) ) { |
| 299 | return $item; |
| 300 | } |
| 301 | |
| 302 | $card_type = $payment_token->get_card_type(); |
| 303 | $item['method']['last4'] = $payment_token->get_last4(); |
| 304 | $item['method']['brand'] = ( ! empty( $card_type ) ? ucfirst( $card_type ) : esc_html__( 'Credit card', 'woocommerce' ) ); |
| 305 | $item['expires'] = $payment_token->get_expiry_month() . '/' . substr( $payment_token->get_expiry_year(), -2 ); |
| 306 | |
| 307 | return $item; |
| 308 | } |
| 309 | |
| 310 | add_filter( 'woocommerce_payment_methods_list_item', 'wc_get_account_saved_payment_methods_list_item_cc', 10, 2 ); |
| 311 | |
| 312 | /** |
| 313 | * Controls the output for eChecks on the my account page. |
| 314 | * |
| 315 | * @since 2.6 |
| 316 | * @param array $item Individual list item from woocommerce_saved_payment_methods_list |
| 317 | * @param WC_Payment_Token $payment_token The payment token associated with this method entry |
| 318 | * @return array Filtered item |
| 319 | */ |
| 320 | function wc_get_account_saved_payment_methods_list_item_echeck( $item, $payment_token ) { |
| 321 | if ( 'echeck' !== strtolower( $payment_token->get_type() ) ) { |
| 322 | return $item; |
| 323 | } |
| 324 | |
| 325 | $item['method']['last4'] = $payment_token->get_last4(); |
| 326 | $item['method']['brand'] = esc_html__( 'eCheck', 'woocommerce' ); |
| 327 | |
| 328 | return $item; |
| 329 | } |
| 330 | |
| 331 | add_filter( 'woocommerce_payment_methods_list_item', 'wc_get_account_saved_payment_methods_list_item_echeck', 10, 2 ); |
| 332 |