PageRenderTime 29ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-my-account.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 415 lines | 232 code | 77 blank | 106 comment | 43 complexity | 8e263433b8e964dca7762819620ea497 MD5 | raw file
  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() || isset( $wp->query_vars['lost-password'] ) ) {
  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. ),
  99. $atts,
  100. 'woocommerce_my_account'
  101. );
  102. wc_get_template(
  103. 'myaccount/my-account.php',
  104. array(
  105. 'current_user' => get_user_by( 'id', get_current_user_id() ),
  106. 'order_count' => 'all' === $args['order_count'] ? -1 : $args['order_count'],
  107. )
  108. );
  109. }
  110. /**
  111. * View order page.
  112. *
  113. * @param int $order_id Order ID.
  114. */
  115. public static function view_order( $order_id ) {
  116. $order = wc_get_order( $order_id );
  117. if ( ! $order || ! current_user_can( 'view_order', $order_id ) ) {
  118. 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>';
  119. return;
  120. }
  121. // Backwards compatibility.
  122. $status = new stdClass();
  123. $status->name = wc_get_order_status_name( $order->get_status() );
  124. wc_get_template(
  125. 'myaccount/view-order.php',
  126. array(
  127. 'status' => $status, // @deprecated 2.2.
  128. 'order' => $order,
  129. 'order_id' => $order->get_id(),
  130. )
  131. );
  132. }
  133. /**
  134. * Edit account details page.
  135. */
  136. public static function edit_account() {
  137. wc_get_template( 'myaccount/form-edit-account.php', array( 'user' => get_user_by( 'id', get_current_user_id() ) ) );
  138. }
  139. /**
  140. * Edit address page.
  141. *
  142. * @param string $load_address Type of address to load.
  143. */
  144. public static function edit_address( $load_address = 'billing' ) {
  145. $current_user = wp_get_current_user();
  146. $load_address = sanitize_key( $load_address );
  147. $country = get_user_meta( get_current_user_id(), $load_address . '_country', true );
  148. if ( ! $country ) {
  149. $country = WC()->countries->get_base_country();
  150. }
  151. if ( 'billing' === $load_address ) {
  152. $allowed_countries = WC()->countries->get_allowed_countries();
  153. if ( ! array_key_exists( $country, $allowed_countries ) ) {
  154. $country = current( array_keys( $allowed_countries ) );
  155. }
  156. }
  157. if ( 'shipping' === $load_address ) {
  158. $allowed_countries = WC()->countries->get_shipping_countries();
  159. if ( ! array_key_exists( $country, $allowed_countries ) ) {
  160. $country = current( array_keys( $allowed_countries ) );
  161. }
  162. }
  163. $address = WC()->countries->get_address_fields( $country, $load_address . '_' );
  164. // Enqueue scripts.
  165. wp_enqueue_script( 'wc-country-select' );
  166. wp_enqueue_script( 'wc-address-i18n' );
  167. // Prepare values.
  168. foreach ( $address as $key => $field ) {
  169. $value = get_user_meta( get_current_user_id(), $key, true );
  170. if ( ! $value ) {
  171. switch ( $key ) {
  172. case 'billing_email':
  173. case 'shipping_email':
  174. $value = $current_user->user_email;
  175. break;
  176. }
  177. }
  178. $address[ $key ]['value'] = apply_filters( 'woocommerce_my_account_edit_address_field_value', $value, $key, $load_address );
  179. }
  180. wc_get_template(
  181. 'myaccount/form-edit-address.php',
  182. array(
  183. 'load_address' => $load_address,
  184. 'address' => apply_filters( 'woocommerce_address_to_edit', $address, $load_address ),
  185. )
  186. );
  187. }
  188. /**
  189. * Lost password page handling.
  190. */
  191. public static function lost_password() {
  192. /**
  193. * After sending the reset link, don't show the form again.
  194. */
  195. if ( ! empty( $_GET['reset-link-sent'] ) ) { // WPCS: input var ok, CSRF ok.
  196. return wc_get_template( 'myaccount/lost-password-confirmation.php' );
  197. /**
  198. * Process reset key / login from email confirmation link
  199. */
  200. } elseif ( ! empty( $_GET['show-reset-form'] ) ) { // WPCS: input var ok, CSRF ok.
  201. if ( isset( $_COOKIE[ 'wp-resetpass-' . COOKIEHASH ] ) && 0 < strpos( $_COOKIE[ 'wp-resetpass-' . COOKIEHASH ], ':' ) ) { // @codingStandardsIgnoreLine
  202. list( $rp_id, $rp_key ) = array_map( 'wc_clean', explode( ':', wp_unslash( $_COOKIE[ 'wp-resetpass-' . COOKIEHASH ] ), 2 ) ); // @codingStandardsIgnoreLine
  203. $userdata = get_userdata( absint( $rp_id ) );
  204. $rp_login = $userdata ? $userdata->user_login : '';
  205. $user = self::check_password_reset_key( $rp_key, $rp_login );
  206. // Reset key / login is correct, display reset password form with hidden key / login values.
  207. if ( is_object( $user ) ) {
  208. return wc_get_template(
  209. 'myaccount/form-reset-password.php',
  210. array(
  211. 'key' => $rp_key,
  212. 'login' => $rp_login,
  213. )
  214. );
  215. }
  216. }
  217. }
  218. // Show lost password form by default.
  219. wc_get_template(
  220. 'myaccount/form-lost-password.php',
  221. array(
  222. 'form' => 'lost_password',
  223. )
  224. );
  225. }
  226. /**
  227. * Handles sending password retrieval email to customer.
  228. *
  229. * Based on retrieve_password() in core wp-login.php.
  230. *
  231. * @uses $wpdb WordPress Database object
  232. * @return bool True: when finish. False: on error
  233. */
  234. public static function retrieve_password() {
  235. $login = isset( $_POST['user_login'] ) ? sanitize_user( wp_unslash( $_POST['user_login'] ) ) : ''; // WPCS: input var ok, CSRF ok.
  236. if ( empty( $login ) ) {
  237. wc_add_notice( __( 'Enter a username or email address.', 'woocommerce' ), 'error' );
  238. return false;
  239. } else {
  240. // Check on username first, as customers can use emails as usernames.
  241. $user_data = get_user_by( 'login', $login );
  242. }
  243. // If no user found, check if it login is email and lookup user based on email.
  244. if ( ! $user_data && is_email( $login ) && apply_filters( 'woocommerce_get_username_from_email', true ) ) {
  245. $user_data = get_user_by( 'email', $login );
  246. }
  247. $errors = new WP_Error();
  248. do_action( 'lostpassword_post', $errors, $user_data );
  249. if ( $errors->get_error_code() ) {
  250. wc_add_notice( $errors->get_error_message(), 'error' );
  251. return false;
  252. }
  253. if ( ! $user_data ) {
  254. wc_add_notice( __( 'Invalid username or email.', 'woocommerce' ), 'error' );
  255. return false;
  256. }
  257. if ( is_multisite() && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) {
  258. wc_add_notice( __( 'Invalid username or email.', 'woocommerce' ), 'error' );
  259. return false;
  260. }
  261. // Redefining user_login ensures we return the right case in the email.
  262. $user_login = $user_data->user_login;
  263. do_action( 'retrieve_password', $user_login );
  264. $allow = apply_filters( 'allow_password_reset', true, $user_data->ID );
  265. if ( ! $allow ) {
  266. wc_add_notice( __( 'Password reset is not allowed for this user', 'woocommerce' ), 'error' );
  267. return false;
  268. } elseif ( is_wp_error( $allow ) ) {
  269. wc_add_notice( $allow->get_error_message(), 'error' );
  270. return false;
  271. }
  272. // Get password reset key (function introduced in WordPress 4.4).
  273. $key = get_password_reset_key( $user_data );
  274. // Send email notification.
  275. WC()->mailer(); // Load email classes.
  276. do_action( 'woocommerce_reset_password_notification', $user_login, $key );
  277. return true;
  278. }
  279. /**
  280. * Retrieves a user row based on password reset key and login.
  281. *
  282. * @uses $wpdb WordPress Database object.
  283. * @param string $key Hash to validate sending user's password.
  284. * @param string $login The user login.
  285. * @return WP_User|bool User's database row on success, false for invalid keys
  286. */
  287. public static function check_password_reset_key( $key, $login ) {
  288. // Check for the password reset key.
  289. // Get user data or an error message in case of invalid or expired key.
  290. $user = check_password_reset_key( $key, $login );
  291. if ( is_wp_error( $user ) ) {
  292. wc_add_notice( __( 'This key is invalid or has already been used. Please reset your password again if needed.', 'woocommerce' ), 'error' );
  293. return false;
  294. }
  295. return $user;
  296. }
  297. /**
  298. * Handles resetting the user's password.
  299. *
  300. * @param object $user The user.
  301. * @param string $new_pass New password for the user in plaintext.
  302. */
  303. public static function reset_password( $user, $new_pass ) {
  304. do_action( 'password_reset', $user, $new_pass );
  305. wp_set_password( $new_pass, $user->ID );
  306. self::set_reset_password_cookie();
  307. if ( ! apply_filters( 'woocommerce_disable_password_change_notification', false ) ) {
  308. wp_password_change_notification( $user );
  309. }
  310. }
  311. /**
  312. * Set or unset the cookie.
  313. *
  314. * @param string $value Cookie value.
  315. */
  316. public static function set_reset_password_cookie( $value = '' ) {
  317. $rp_cookie = 'wp-resetpass-' . COOKIEHASH;
  318. $rp_path = isset( $_SERVER['REQUEST_URI'] ) ? current( explode( '?', wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) : ''; // WPCS: input var ok, sanitization ok.
  319. if ( $value ) {
  320. setcookie( $rp_cookie, $value, 0, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
  321. } else {
  322. setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
  323. }
  324. }
  325. /**
  326. * Show the add payment method page.
  327. */
  328. public static function add_payment_method() {
  329. if ( ! is_user_logged_in() ) {
  330. wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) );
  331. exit();
  332. } else {
  333. do_action( 'before_woocommerce_add_payment_method' );
  334. wc_get_template( 'myaccount/form-add-payment-method.php' );
  335. do_action( 'after_woocommerce_add_payment_method' );
  336. }
  337. }
  338. }