2012-12-31 18:25:09 +00:00
< ? php
/**
2015-11-03 13:53:50 +00:00
* My Account Shortcodes
2012-12-31 18:25:09 +00:00
*
* Shows the 'my account' section where the customer can view past orders and update their information .
*
2018-03-09 16:11:52 +00:00
* @ package WooCommerce / Shortcodes / My_Account
* @ version 2.0 . 0
*/
defined ( 'ABSPATH' ) || exit ;
/**
* Shortcode my account class .
2012-12-31 18:25:09 +00:00
*/
class WC_Shortcode_My_Account {
/**
* Get the shortcode content .
*
2018-03-09 16:11:52 +00:00
* @ param array $atts Shortcode attributes .
2017-10-10 11:14:35 +00:00
*
2012-12-31 18:25:09 +00:00
* @ return string
*/
public static function get ( $atts ) {
2013-08-09 16:11:15 +00:00
return WC_Shortcodes :: shortcode_wrapper ( array ( __CLASS__ , 'output' ), $atts );
2012-12-31 18:25:09 +00:00
}
/**
* Output the shortcode .
*
2018-03-09 16:11:52 +00:00
* @ param array $atts Shortcode attributes .
2012-12-31 18:25:09 +00:00
*/
public static function output ( $atts ) {
2014-06-08 20:33:11 +00:00
global $wp ;
2012-12-31 18:25:09 +00:00
2018-03-09 16:11:52 +00:00
// Check cart class is loaded or abort.
2014-02-26 15:27:26 +00:00
if ( is_null ( WC () -> cart ) ) {
return ;
}
2012-12-31 18:25:09 +00:00
if ( ! is_user_logged_in () ) {
2013-12-31 12:38:33 +00:00
$message = apply_filters ( 'woocommerce_my_account_message' , '' );
2013-07-30 12:38:45 +00:00
2014-04-23 10:35:43 +00:00
if ( ! empty ( $message ) ) {
2013-11-13 04:32:29 +00:00
wc_add_notice ( $message );
2014-04-23 10:35:43 +00:00
}
2013-07-30 12:38:45 +00:00
2016-07-27 11:47:41 +00:00
// After password reset, add confirmation message.
2018-03-09 16:11:52 +00:00
if ( ! empty ( $_GET [ 'password-reset' ] ) ) { // WPCS: input var ok, CSRF ok.
2016-07-27 11:47:41 +00:00
wc_add_notice ( __ ( 'Your password has been reset successfully.' , 'woocommerce' ) );
}
2013-07-23 16:05:01 +00:00
if ( isset ( $wp -> query_vars [ 'lost-password' ] ) ) {
self :: lost_password ();
} else {
2013-11-25 12:45:04 +00:00
wc_get_template ( 'myaccount/form-login.php' );
2013-07-23 16:05:01 +00:00
}
2017-06-12 13:02:29 +00:00
} else {
2018-03-09 16:11:52 +00:00
// Start output buffer since the html may need discarding for BW compatibility.
2016-06-14 10:33:28 +00:00
ob_start ();
2017-06-12 13:02:29 +00:00
if ( isset ( $wp -> query_vars [ 'customer-logout' ] ) ) {
2018-03-09 16:11:52 +00:00
/* translators: %s: logout url */
2017-06-12 13:02:29 +00:00
wc_add_notice ( sprintf ( __ ( 'Are you sure you want to log out? <a href="%s">Confirm and log out</a>' , 'woocommerce' ), wc_logout_url () ) );
}
2018-03-09 16:11:52 +00:00
// Collect notices before output.
2016-06-14 10:33:28 +00:00
$notices = wc_get_notices ();
2018-03-09 16:11:52 +00:00
// Output the new account page.
2016-06-14 10:33:28 +00:00
self :: my_account ( $atts );
2016-06-06 11:55:07 +00:00
/**
2016-06-06 12:54:17 +00:00
* Deprecated my - account . php template handling . This code should be
2016-06-06 11:55:07 +00:00
* removed in a future release .
*
* If woocommerce_account_content did not run , this is an old template
* so we need to render the endpoint content again .
*/
if ( ! did_action ( 'woocommerce_account_content' ) ) {
2017-10-10 11:14:35 +00:00
if ( ! empty ( $wp -> query_vars ) ) {
foreach ( $wp -> query_vars as $key => $value ) {
if ( 'pagename' === $key ) {
continue ;
}
if ( has_action ( 'woocommerce_account_' . $key . '_endpoint' ) ) {
2018-03-09 16:11:52 +00:00
ob_clean (); // Clear previous buffer.
2017-10-10 11:14:35 +00:00
wc_set_notices ( $notices );
wc_print_notices ();
do_action ( 'woocommerce_account_' . $key . '_endpoint' , $value );
break ;
}
2016-06-06 11:55:07 +00:00
}
2016-06-06 12:54:17 +00:00
2017-10-10 11:14:35 +00:00
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' );
}
2016-06-06 11:55:07 +00:00
}
2018-03-09 16:11:52 +00:00
// Send output buffer.
2016-06-06 11:55:07 +00:00
ob_end_flush ();
2012-12-31 18:25:09 +00:00
}
}
2013-06-04 16:33:43 +00:00
/**
2015-11-03 13:31:20 +00:00
* My account page .
2013-06-04 16:33:43 +00:00
*
2018-03-09 16:11:52 +00:00
* @ param array $atts Shortcode attributes .
2013-06-04 16:33:43 +00:00
*/
2013-11-19 16:26:18 +00:00
private static function my_account ( $atts ) {
2018-03-09 16:11:52 +00:00
$args = shortcode_atts (
array (
'order_count' => 15 , // @deprecated 2.6.0. Keep for backward compatibility.
2019-01-07 16:03:23 +00:00
),
$atts ,
'woocommerce_my_account'
2018-03-09 16:11:52 +00:00
);
wc_get_template (
2019-01-07 16:03:23 +00:00
'myaccount/my-account.php' ,
array (
2018-03-09 16:11:52 +00:00
'current_user' => get_user_by ( 'id' , get_current_user_id () ),
'order_count' => 'all' === $args [ 'order_count' ] ? - 1 : $args [ 'order_count' ],
)
);
2013-06-04 16:33:43 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* View order page .
2013-06-04 16:33:43 +00:00
*
2018-03-09 16:11:52 +00:00
* @ param int $order_id Order ID .
2013-06-04 16:33:43 +00:00
*/
2016-01-14 20:35:31 +00:00
public static function view_order ( $order_id ) {
2017-10-10 11:14:35 +00:00
$order = wc_get_order ( $order_id );
2013-06-04 16:33:43 +00:00
2019-08-19 15:47:17 +00:00
if ( ! $order || ! current_user_can ( 'view_order' , $order_id ) ) {
2018-03-09 16:11:52 +00:00
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>' ;
2017-10-10 11:14:35 +00:00
2013-06-04 16:33:43 +00:00
return ;
}
2018-03-09 16:11:52 +00:00
// Backwards compatibility.
2014-05-30 16:43:21 +00:00
$status = new stdClass ();
$status -> name = wc_get_order_status_name ( $order -> get_status () );
2018-03-09 16:11:52 +00:00
wc_get_template (
2019-01-07 16:03:23 +00:00
'myaccount/view-order.php' ,
array (
2018-03-09 16:11:52 +00:00
'status' => $status , // @deprecated 2.2.
2019-08-19 21:19:36 +00:00
'order' => $order ,
'order_id' => $order -> get_id (),
2018-03-09 16:11:52 +00:00
)
);
2013-06-04 16:33:43 +00:00
}
2013-06-05 11:07:23 +00:00
/**
2015-11-03 13:31:20 +00:00
* Edit account details page .
2013-06-05 11:07:23 +00:00
*/
2016-01-14 20:35:31 +00:00
public static function edit_account () {
2013-11-25 12:45:04 +00:00
wc_get_template ( 'myaccount/form-edit-account.php' , array ( 'user' => get_user_by ( 'id' , get_current_user_id () ) ) );
2013-06-05 11:07:23 +00:00
}
2013-07-23 16:05:01 +00:00
/**
* Edit address page .
*
2018-03-09 16:11:52 +00:00
* @ param string $load_address Type of address to load .
2013-07-23 16:05:01 +00:00
*/
2016-01-14 20:35:31 +00:00
public static function edit_address ( $load_address = 'billing' ) {
2016-01-18 14:41:58 +00:00
$current_user = wp_get_current_user ();
2013-09-10 13:04:07 +00:00
$load_address = sanitize_key ( $load_address );
2019-01-07 16:03:23 +00:00
$country = get_user_meta ( get_current_user_id (), $load_address . '_country' , true );
if ( ! $country ) {
$country = WC () -> countries -> get_base_country ();
}
if ( 'billing' === $load_address ) {
$allowed_countries = WC () -> countries -> get_allowed_countries ();
if ( ! array_key_exists ( $country , $allowed_countries ) ) {
$country = current ( array_keys ( $allowed_countries ) );
}
}
2013-07-23 16:05:01 +00:00
2019-01-07 16:03:23 +00:00
if ( 'shipping' === $load_address ) {
$allowed_countries = WC () -> countries -> get_shipping_countries ();
if ( ! array_key_exists ( $country , $allowed_countries ) ) {
$country = current ( array_keys ( $allowed_countries ) );
}
}
$address = WC () -> countries -> get_address_fields ( $country , $load_address . '_' );
2013-07-23 16:05:01 +00:00
2018-03-09 16:11:52 +00:00
// Enqueue scripts.
2013-12-27 12:20:51 +00:00
wp_enqueue_script ( 'wc-country-select' );
2014-01-08 14:38:17 +00:00
wp_enqueue_script ( 'wc-address-i18n' );
2013-12-27 12:20:51 +00:00
2018-03-09 16:11:52 +00:00
// Prepare values.
2013-09-19 13:39:49 +00:00
foreach ( $address as $key => $field ) {
$value = get_user_meta ( get_current_user_id (), $key , true );
if ( ! $value ) {
2016-08-27 04:23:02 +00:00
switch ( $key ) {
2018-03-09 16:11:52 +00:00
case 'billing_email' :
case 'shipping_email' :
2013-09-19 13:39:49 +00:00
$value = $current_user -> user_email ;
2017-10-10 11:14:35 +00:00
break ;
2013-09-19 13:39:49 +00:00
}
}
$address [ $key ][ 'value' ] = apply_filters ( 'woocommerce_my_account_edit_address_field_value' , $value , $key , $load_address );
}
2018-03-09 16:11:52 +00:00
wc_get_template (
2019-01-07 16:03:23 +00:00
'myaccount/form-edit-address.php' ,
array (
2018-03-09 16:11:52 +00:00
'load_address' => $load_address ,
'address' => apply_filters ( 'woocommerce_address_to_edit' , $address , $load_address ),
)
);
2013-07-23 16:05:01 +00:00
}
/**
2016-04-27 15:00:30 +00:00
* Lost password page handling .
2013-07-23 16:05:01 +00:00
*/
public static function lost_password () {
2016-04-27 15:00:30 +00:00
/**
* After sending the reset link , don ' t show the form again .
*/
2018-03-09 16:11:52 +00:00
if ( ! empty ( $_GET [ 'reset-link-sent' ] ) ) { // WPCS: input var ok, CSRF ok.
2016-04-27 15:00:30 +00:00
return wc_get_template ( 'myaccount/lost-password-confirmation.php' );
2017-10-10 11:14:35 +00:00
/**
* Process reset key / login from email confirmation link
*/
2018-03-09 16:11:52 +00:00
} elseif ( ! empty ( $_GET [ 'show-reset-form' ] ) ) { // WPCS: input var ok, CSRF ok.
if ( isset ( $_COOKIE [ 'wp-resetpass-' . COOKIEHASH ] ) && 0 < strpos ( $_COOKIE [ 'wp-resetpass-' . COOKIEHASH ], ':' ) ) { // @codingStandardsIgnoreLine
2019-01-21 16:02:28 +00:00
list ( $rp_id , $rp_key ) = array_map ( 'wc_clean' , explode ( ':' , wp_unslash ( $_COOKIE [ 'wp-resetpass-' . COOKIEHASH ] ), 2 ) ); // @codingStandardsIgnoreLine
$userdata = get_userdata ( absint ( $rp_id ) );
$rp_login = $userdata ? $userdata -> user_login : '' ;
$user = self :: check_password_reset_key ( $rp_key , $rp_login );
2016-06-24 14:06:01 +00:00
2018-03-09 16:11:52 +00:00
// Reset key / login is correct, display reset password form with hidden key / login values.
2016-06-24 14:06:01 +00:00
if ( is_object ( $user ) ) {
2018-03-09 16:11:52 +00:00
return wc_get_template (
2019-01-07 16:03:23 +00:00
'myaccount/form-reset-password.php' ,
array (
2018-03-09 16:11:52 +00:00
'key' => $rp_key ,
'login' => $rp_login ,
)
);
2016-06-24 14:06:01 +00:00
}
}
2013-07-23 16:05:01 +00:00
}
2018-03-09 16:11:52 +00:00
// Show lost password form by default.
wc_get_template (
2019-01-07 16:03:23 +00:00
'myaccount/form-lost-password.php' ,
array (
2018-03-09 16:11:52 +00:00
'form' => 'lost_password' ,
)
);
2013-07-23 16:05:01 +00:00
}
/**
* Handles sending password retrieval email to customer .
*
2015-11-03 13:31:20 +00:00
* Based on retrieve_password () in core wp - login . php .
2014-08-19 09:43:48 +00:00
*
2013-07-23 16:05:01 +00:00
* @ uses $wpdb WordPress Database object
* @ return bool True : when finish . False : on error
*/
public static function retrieve_password () {
2018-03-09 16:11:52 +00:00
$login = isset ( $_POST [ 'user_login' ] ) ? sanitize_user ( wp_unslash ( $_POST [ 'user_login' ] ) ) : '' ; // WPCS: input var ok, CSRF ok.
2015-09-07 22:08:32 +00:00
if ( empty ( $login ) ) {
2013-07-23 16:05:01 +00:00
2016-10-12 10:16:30 +00:00
wc_add_notice ( __ ( 'Enter a username or email address.' , 'woocommerce' ), 'error' );
2017-10-10 11:14:35 +00:00
2014-11-07 04:08:48 +00:00
return false ;
2013-07-23 16:05:01 +00:00
} else {
2014-08-08 06:07:11 +00:00
// Check on username first, as customers can use emails as usernames.
2014-02-25 11:40:49 +00:00
$user_data = get_user_by ( 'login' , $login );
2013-07-23 16:05:01 +00:00
}
2014-11-07 04:08:48 +00:00
// If no user found, check if it login is email and lookup user based on email.
2015-09-07 22:08:32 +00:00
if ( ! $user_data && is_email ( $login ) && apply_filters ( 'woocommerce_get_username_from_email' , true ) ) {
$user_data = get_user_by ( 'email' , $login );
2014-08-08 06:07:11 +00:00
}
2017-03-14 12:34:20 +00:00
2017-03-11 00:43:21 +00:00
$errors = new WP_Error ();
2013-07-23 16:05:01 +00:00
2017-03-14 12:34:20 +00:00
do_action ( 'lostpassword_post' , $errors );
if ( $errors -> get_error_code () ) {
2017-04-11 17:29:55 +00:00
wc_add_notice ( $errors -> get_error_message (), 'error' );
2017-10-10 11:14:35 +00:00
2017-03-11 00:43:21 +00:00
return false ;
}
2013-07-23 16:05:01 +00:00
if ( ! $user_data ) {
2016-10-12 10:16:30 +00:00
wc_add_notice ( __ ( 'Invalid username or email.' , 'woocommerce' ), 'error' );
2017-10-10 11:14:35 +00:00
2013-07-23 16:05:01 +00:00
return false ;
}
2015-02-15 19:02:27 +00:00
if ( is_multisite () && ! is_user_member_of_blog ( $user_data -> ID , get_current_blog_id () ) ) {
2016-10-12 10:16:30 +00:00
wc_add_notice ( __ ( 'Invalid username or email.' , 'woocommerce' ), 'error' );
2017-10-10 11:14:35 +00:00
2015-01-06 14:08:43 +00:00
return false ;
}
2018-03-09 16:11:52 +00:00
// Redefining user_login ensures we return the right case in the email.
2013-07-23 16:05:01 +00:00
$user_login = $user_data -> user_login ;
2014-08-08 06:07:11 +00:00
do_action ( 'retrieve_password' , $user_login );
2013-07-23 16:05:01 +00:00
2014-08-19 09:43:48 +00:00
$allow = apply_filters ( 'allow_password_reset' , true , $user_data -> ID );
2013-07-23 16:05:01 +00:00
if ( ! $allow ) {
2014-02-17 13:14:41 +00:00
wc_add_notice ( __ ( 'Password reset is not allowed for this user' , 'woocommerce' ), 'error' );
2017-10-10 11:14:35 +00:00
2013-07-23 16:05:01 +00:00
return false ;
} elseif ( is_wp_error ( $allow ) ) {
2014-09-12 12:36:17 +00:00
wc_add_notice ( $allow -> get_error_message (), 'error' );
2017-10-10 11:14:35 +00:00
2013-07-23 16:05:01 +00:00
return false ;
}
2016-10-05 22:01:59 +00:00
// Get password reset key (function introduced in WordPress 4.4).
$key = get_password_reset_key ( $user_data );
2013-07-23 16:05:01 +00:00
2018-03-09 16:11:52 +00:00
// Send email notification.
WC () -> mailer (); // Load email classes.
2013-07-23 16:05:01 +00:00
do_action ( 'woocommerce_reset_password_notification' , $user_login , $key );
return true ;
}
/**
2015-11-03 13:31:20 +00:00
* Retrieves a user row based on password reset key and login .
2013-07-23 16:05:01 +00:00
*
2018-03-09 16:11:52 +00:00
* @ uses $wpdb WordPress Database object .
* @ param string $key Hash to validate sending user ' s password .
* @ param string $login The user login .
2016-10-05 22:01:59 +00:00
* @ return WP_User | bool User ' s database row on success , false for invalid keys
2013-07-23 16:05:01 +00:00
*/
public static function check_password_reset_key ( $key , $login ) {
2016-10-05 22:01:59 +00:00
// Check for the password reset key.
// Get user data or an error message in case of invalid or expired key.
$user = check_password_reset_key ( $key , $login );
2013-07-23 16:05:01 +00:00
2016-10-05 22:01:59 +00:00
if ( is_wp_error ( $user ) ) {
2017-10-26 14:22:25 +00:00
wc_add_notice ( __ ( 'This key is invalid or has already been used. Please reset your password again if needed.' , 'woocommerce' ), 'error' );
2013-07-23 16:05:01 +00:00
return false ;
}
2016-10-05 22:01:59 +00:00
return $user ;
2013-07-23 16:05:01 +00:00
}
/**
* Handles resetting the user ' s password .
*
2018-03-09 16:11:52 +00:00
* @ param object $user The user .
* @ param string $new_pass New password for the user in plaintext .
2013-07-23 16:05:01 +00:00
*/
public static function reset_password ( $user , $new_pass ) {
do_action ( 'password_reset' , $user , $new_pass );
wp_set_password ( $new_pass , $user -> ID );
2016-06-24 14:06:01 +00:00
self :: set_reset_password_cookie ();
2013-07-23 16:05:01 +00:00
2019-07-15 15:00:13 +00:00
if ( ! apply_filters ( 'woocommerce_disable_password_change_notification' , false ) ) {
wp_password_change_notification ( $user );
}
2013-07-23 16:05:01 +00:00
}
2013-11-05 21:02:13 +00:00
2016-06-24 14:06:01 +00:00
/**
* Set or unset the cookie .
2017-05-15 11:50:52 +00:00
*
2018-03-09 16:11:52 +00:00
* @ param string $value Cookie value .
2016-06-24 14:06:01 +00:00
*/
public static function set_reset_password_cookie ( $value = '' ) {
$rp_cookie = 'wp-resetpass-' . COOKIEHASH ;
2018-07-25 23:33:11 +00:00
$rp_path = isset ( $_SERVER [ 'REQUEST_URI' ] ) ? current ( explode ( '?' , wp_unslash ( $_SERVER [ 'REQUEST_URI' ] ) ) ) : '' ; // WPCS: input var ok, sanitization ok.
2016-06-24 14:06:01 +00:00
if ( $value ) {
setcookie ( $rp_cookie , $value , 0 , $rp_path , COOKIE_DOMAIN , is_ssl (), true );
} else {
setcookie ( $rp_cookie , ' ' , time () - YEAR_IN_SECONDS , $rp_path , COOKIE_DOMAIN , is_ssl (), true );
}
}
2013-11-05 21:02:13 +00:00
/**
2015-11-03 13:31:20 +00:00
* Show the add payment method page .
2013-11-05 21:02:13 +00:00
*/
2016-01-14 20:35:31 +00:00
public static function add_payment_method () {
2013-11-05 21:02:13 +00:00
if ( ! is_user_logged_in () ) {
2015-02-15 19:13:22 +00:00
wp_safe_redirect ( wc_get_page_permalink ( 'myaccount' ) );
2013-11-05 21:02:13 +00:00
exit ();
} else {
do_action ( 'before_woocommerce_add_payment_method' );
2013-11-25 12:45:04 +00:00
wc_get_template ( 'myaccount/form-add-payment-method.php' );
2013-11-05 21:02:13 +00:00
do_action ( 'after_woocommerce_add_payment_method' );
}
}
2013-11-19 16:26:18 +00:00
}