class-wc-shortcode-my-account.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. /**
  3. * My Account Shortcodes
  4. *
  5. * Shows the 'my account' section where the customer can view past orders and update their information.
  6. *
  7. * @package WooCommerce/Shortcodes/My_Account
  8. * @version 2.0.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * Shortcode my account class.
  13. */
  14. class WC_Shortcode_My_Account {
  15. /**
  16. * Get the shortcode content.
  17. *
  18. * @param array $atts Shortcode attributes.
  19. *
  20. * @return string
  21. */
  22. public static function get( $atts ) {
  23. return WC_Shortcodes::shortcode_wrapper( array( __CLASS__, 'output' ), $atts );
  24. }
  25. /**
  26. * Output the shortcode.
  27. *
  28. * @param array $atts Shortcode attributes.
  29. */
  30. public static function output( $atts ) {
  31. global $wp;
  32. // Check cart class is loaded or abort.
  33. if ( is_null( WC()->cart ) ) {
  34. return;
  35. }
  36. if ( ! is_user_logged_in() ) {
  37. $message = apply_filters( 'woocommerce_my_account_message', '' );
  38. if ( ! empty( $message ) ) {
  39. wc_add_notice( $message );
  40. }
  41. // After password reset, add confirmation message.
  42. if ( ! empty( $_GET['password-reset'] ) ) { // WPCS: input var ok, CSRF ok.
  43. wc_add_notice( __( 'Your password has been reset successfully.', 'woocommerce' ) );
  44. }
  45. if ( isset( $wp->query_vars['lost-password'] ) ) {
  46. self::lost_password();
  47. } else {
  48. wc_get_template( 'myaccount/form-login.php' );
  49. }
  50. } else {
  51. // Start output buffer since the html may need discarding for BW compatibility.
  52. ob_start();
  53. if ( isset( $wp->query_vars['customer-logout'] ) ) {
  54. /* translators: %s: logout url */
  55. wc_add_notice( sprintf( __( 'Are you sure you want to log out? <a href="%s">Confirm and log out</a>', 'woocommerce' ), wc_logout_url() ) );
  56. }
  57. // Collect notices before output.
  58. $notices = wc_get_notices();
  59. // Output the new account page.
  60. self::my_account( $atts );
  61. /**
  62. * Deprecated my-account.php template handling. This code should be
  63. * removed in a future release.
  64. *
  65. * If woocommerce_account_content did not run, this is an old template
  66. * so we need to render the endpoint content again.
  67. */
  68. if ( ! did_action( 'woocommerce_account_content' ) ) {
  69. if ( ! empty( $wp->query_vars ) ) {
  70. foreach ( $wp->query_vars as $key => $value ) {
  71. if ( 'pagename' === $key ) {
  72. continue;
  73. }
  74. if ( has_action( 'woocommerce_account_' . $key . '_endpoint' ) ) {
  75. ob_clean(); // Clear previous buffer.
  76. wc_set_notices( $notices );
  77. wc_print_notices();
  78. do_action( 'woocommerce_account_' . $key . '_endpoint', $value );
  79. break;
  80. }
  81. }
  82. wc_deprecated_function( 'Your theme version of my-account.php template', '2.6', 'the latest version, which supports multiple account pages and navigation, from WC 2.6.0' );
  83. }
  84. }
  85. // Send output buffer.
  86. ob_end_flush();
  87. }
  88. }
  89. /**
  90. * My account page.
  91. *
  92. * @param array $atts Shortcode attributes.
  93. */
  94. private static function my_account( $atts ) {
  95. $args = shortcode_atts(
  96. array(
  97. 'order_count' => 15, // @deprecated 2.6.0. Keep for backward compatibility.
  98. ), $atts, 'woocommerce_my_account'
  99. );
  100. wc_get_template(
  101. 'myaccount/my-account.php', array(
  102. 'current_user' => get_user_by( 'id', get_current_user_id() ),
  103. 'order_count' => 'all' === $args['order_count'] ? -1 : $args['order_count'],
  104. )
  105. );
  106. }
  107. /**
  108. * View order page.
  109. *
  110. * @param int $order_id Order ID.
  111. */
  112. public static function view_order( $order_id ) {
  113. $order = wc_get_order( $order_id );
  114. if ( ! current_user_can( 'view_order', $order_id ) ) {
  115. echo '<div class="woocommerce-error">' . esc_html__( 'Invalid order.', 'woocommerce' ) . ' <a href="' . esc_url( wc_get_page_permalink( 'myaccount' ) ) . '" class="wc-forward">' . esc_html__( 'My account', 'woocommerce' ) . '</a></div>';
  116. return;
  117. }
  118. // Backwards compatibility.
  119. $status = new stdClass();
  120. $status->name = wc_get_order_status_name( $order->get_status() );
  121. wc_get_template(
  122. 'myaccount/view-order.php', array(
  123. 'status' => $status, // @deprecated 2.2.
  124. 'order' => wc_get_order( $order_id ),
  125. 'order_id' => $order_id,
  126. )
  127. );
  128. }
  129. /**
  130. * Edit account details page.
  131. */
  132. public static function edit_account() {
  133. wc_get_template( 'myaccount/form-edit-account.php', array( 'user' => get_user_by( 'id', get_current_user_id() ) ) );
  134. }
  135. /**
  136. * Edit address page.
  137. *
  138. * @param string $load_address Type of address to load.
  139. */
  140. public static function edit_address( $load_address = 'billing' ) {
  141. $current_user = wp_get_current_user();
  142. $load_address = sanitize_key( $load_address );
  143. $address = WC()->countries->get_address_fields( get_user_meta( get_current_user_id(), $load_address . '_country', true ), $load_address . '_' );
  144. // Enqueue scripts.
  145. wp_enqueue_script( 'wc-country-select' );
  146. wp_enqueue_script( 'wc-address-i18n' );
  147. // Prepare values.
  148. foreach ( $address as $key => $field ) {
  149. $value = get_user_meta( get_current_user_id(), $key, true );
  150. if ( ! $value ) {
  151. switch ( $key ) {
  152. case 'billing_email':
  153. case 'shipping_email':
  154. $value = $current_user->user_email;
  155. break;
  156. case 'billing_country':
  157. case 'shipping_country':
  158. $value = WC()->countries->get_base_country();
  159. break;
  160. case 'billing_state':
  161. case 'shipping_state':
  162. $value = WC()->countries->get_base_state();
  163. break;
  164. }
  165. }
  166. $address[ $key ]['value'] = apply_filters( 'woocommerce_my_account_edit_address_field_value', $value, $key, $load_address );
  167. }
  168. wc_get_template(
  169. 'myaccount/form-edit-address.php', array(
  170. 'load_address' => $load_address,
  171. 'address' => apply_filters( 'woocommerce_address_to_edit', $address, $load_address ),
  172. )
  173. );
  174. }
  175. /**
  176. * Lost password page handling.
  177. */
  178. public static function lost_password() {
  179. /**
  180. * After sending the reset link, don't show the form again.
  181. */
  182. if ( ! empty( $_GET['reset-link-sent'] ) ) { // WPCS: input var ok, CSRF ok.
  183. return wc_get_template( 'myaccount/lost-password-confirmation.php' );
  184. /**
  185. * Process reset key / login from email confirmation link
  186. */
  187. } elseif ( ! empty( $_GET['show-reset-form'] ) ) { // WPCS: input var ok, CSRF ok.
  188. if ( isset( $_COOKIE[ 'wp-resetpass-' . COOKIEHASH ] ) && 0 < strpos( $_COOKIE[ 'wp-resetpass-' . COOKIEHASH ], ':' ) ) { // @codingStandardsIgnoreLine
  189. list( $rp_login, $rp_key ) = array_map( 'wc_clean', explode( ':', wp_unslash( $_COOKIE[ 'wp-resetpass-' . COOKIEHASH ] ), 2 ) ); // @codingStandardsIgnoreLine
  190. $user = self::check_password_reset_key( $rp_key, $rp_login );
  191. // Reset key / login is correct, display reset password form with hidden key / login values.
  192. if ( is_object( $user ) ) {
  193. return wc_get_template(
  194. 'myaccount/form-reset-password.php', array(
  195. 'key' => $rp_key,
  196. 'login' => $rp_login,
  197. )
  198. );
  199. }
  200. }
  201. }
  202. // Show lost password form by default.
  203. wc_get_template(
  204. 'myaccount/form-lost-password.php', array(
  205. 'form' => 'lost_password',
  206. )
  207. );
  208. }
  209. /**
  210. * Handles sending password retrieval email to customer.
  211. *
  212. * Based on retrieve_password() in core wp-login.php.
  213. *
  214. * @uses $wpdb WordPress Database object
  215. * @return bool True: when finish. False: on error
  216. */
  217. public static function retrieve_password() {
  218. $login = isset( $_POST['user_login'] ) ? sanitize_user( wp_unslash( $_POST['user_login'] ) ) : ''; // WPCS: input var ok, CSRF ok.
  219. if ( empty( $login ) ) {
  220. wc_add_notice( __( 'Enter a username or email address.', 'woocommerce' ), 'error' );
  221. return false;
  222. } else {
  223. // Check on username first, as customers can use emails as usernames.
  224. $user_data = get_user_by( 'login', $login );
  225. }
  226. // If no user found, check if it login is email and lookup user based on email.
  227. if ( ! $user_data && is_email( $login ) && apply_filters( 'woocommerce_get_username_from_email', true ) ) {
  228. $user_data = get_user_by( 'email', $login );
  229. }
  230. $errors = new WP_Error();
  231. do_action( 'lostpassword_post', $errors );
  232. if ( $errors->get_error_code() ) {
  233. wc_add_notice( $errors->get_error_message(), 'error' );
  234. return false;
  235. }
  236. if ( ! $user_data ) {
  237. wc_add_notice( __( 'Invalid username or email.', 'woocommerce' ), 'error' );
  238. return false;
  239. }
  240. if ( is_multisite() && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) {
  241. wc_add_notice( __( 'Invalid username or email.', 'woocommerce' ), 'error' );
  242. return false;
  243. }
  244. // Redefining user_login ensures we return the right case in the email.
  245. $user_login = $user_data->user_login;
  246. do_action( 'retrieve_password', $user_login );
  247. $allow = apply_filters( 'allow_password_reset', true, $user_data->ID );
  248. if ( ! $allow ) {
  249. wc_add_notice( __( 'Password reset is not allowed for this user', 'woocommerce' ), 'error' );
  250. return false;
  251. } elseif ( is_wp_error( $allow ) ) {
  252. wc_add_notice( $allow->get_error_message(), 'error' );
  253. return false;
  254. }
  255. // Get password reset key (function introduced in WordPress 4.4).
  256. $key = get_password_reset_key( $user_data );
  257. // Send email notification.
  258. WC()->mailer(); // Load email classes.
  259. do_action( 'woocommerce_reset_password_notification', $user_login, $key );
  260. return true;
  261. }
  262. /**
  263. * Retrieves a user row based on password reset key and login.
  264. *
  265. * @uses $wpdb WordPress Database object.
  266. * @param string $key Hash to validate sending user's password.
  267. * @param string $login The user login.
  268. * @return WP_User|bool User's database row on success, false for invalid keys
  269. */
  270. public static function check_password_reset_key( $key, $login ) {
  271. // Check for the password reset key.
  272. // Get user data or an error message in case of invalid or expired key.
  273. $user = check_password_reset_key( $key, $login );
  274. if ( is_wp_error( $user ) ) {
  275. wc_add_notice( __( 'This key is invalid or has already been used. Please reset your password again if needed.', 'woocommerce' ), 'error' );
  276. return false;
  277. }
  278. return $user;
  279. }
  280. /**
  281. * Handles resetting the user's password.
  282. *
  283. * @param object $user The user.
  284. * @param string $new_pass New password for the user in plaintext.
  285. */
  286. public static function reset_password( $user, $new_pass ) {
  287. do_action( 'password_reset', $user, $new_pass );
  288. wp_set_password( $new_pass, $user->ID );
  289. self::set_reset_password_cookie();
  290. wp_password_change_notification( $user );
  291. }
  292. /**
  293. * Set or unset the cookie.
  294. *
  295. * @param string $value Cookie value.
  296. */
  297. public static function set_reset_password_cookie( $value = '' ) {
  298. $rp_cookie = 'wp-resetpass-' . COOKIEHASH;
  299. $rp_path = isset( $_SERVER['REQUEST_URI'] ) ? current( explode( '?', wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) : ''; // WPCS: input var ok, sanitization ok.
  300. if ( $value ) {
  301. setcookie( $rp_cookie, $value, 0, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
  302. } else {
  303. setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
  304. }
  305. }
  306. /**
  307. * Show the add payment method page.
  308. */
  309. public static function add_payment_method() {
  310. if ( ! is_user_logged_in() ) {
  311. wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) );
  312. exit();
  313. } else {
  314. do_action( 'before_woocommerce_add_payment_method' );
  315. wc_print_notices();
  316. wc_get_template( 'myaccount/form-add-payment-method.php' );
  317. do_action( 'after_woocommerce_add_payment_method' );
  318. }
  319. }
  320. }