WC_Shortcode_My_Account

My Account Shortcodes.

Defined (1)

The class is defined in the following location(s).

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