PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/woocommerce/includes/wc-account-functions.php

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