/includes/class-wc-gzd-customer-helper.php

  1. <?php 
  2.  
  3. class WC_GZD_Customer_Helper { 
  4.  
  5. protected static $_instance = null; 
  6.  
  7. public static function instance() { 
  8. if ( is_null( self::$_instance ) ) 
  9. self::$_instance = new self(); 
  10. return self::$_instance; 
  11.  
  12. /** 
  13. * Cloning is forbidden. 
  14. * 
  15. * @since 1.0 
  16. */ 
  17. public function __clone() { 
  18. _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce-germanized' ), '1.0' ); 
  19.  
  20. /** 
  21. * Unserializing instances of this class is forbidden. 
  22. * 
  23. * @since 1.0 
  24. */ 
  25. public function __wakeup() { 
  26. _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce-germanized' ), '1.0' ); 
  27.  
  28. public function __construct() { 
  29.  
  30. // Customer Account checkbox 
  31. add_action( 'template_redirect', array( $this, 'init_gettext_replacement' ) ); 
  32. // Send customer account notification 
  33. add_action( 'woocommerce_email', array( $this, 'email_hooks' ), 0, 1 ); 
  34. // Add Title to user profile 
  35. add_filter( 'woocommerce_customer_meta_fields', array( $this, 'profile_field_title' ), 10, 1 );  
  36.  
  37. if ( $this->is_double_opt_in_enabled() ) { 
  38.  
  39. // Check for customer activation 
  40. add_action( 'template_redirect', array( $this, 'customer_account_activation_check' ) ); 
  41. // Cronjob to delete unactivated users 
  42. add_action( 'woocommerce_gzd_customer_cleanup', array( $this, 'account_cleanup' ) ); 
  43.  
  44. add_action( 'woocommerce_created_customer', array( $this, 'set_customer_activation_meta' ), 10, 3 ); 
  45.  
  46. if ( $this->is_double_opt_in_login_enabled() ) { 
  47. // Disable login for unactivated users 
  48. add_filter( 'wp_authenticate_user', array( $this, 'login_restriction' ) , 10, 2 ); 
  49. // Disable auto login after registration 
  50. add_filter( 'woocommerce_registration_auth_new_customer', array( $this, 'disable_registration_auto_login' ), 10, 2 );  
  51. // Redirect customers that are not logged in to customer account page 
  52. add_action( 'template_redirect', array( $this, 'disable_checkout' ), 10 ); 
  53. // Show notices on customer account page 
  54. add_action( 'template_redirect', array( $this, 'show_disabled_checkout_notice' ), 20 ); 
  55. // Redirect customers to checkout after login 
  56. add_filter( 'woocommerce_login_redirect', array( $this, 'login_redirect' ), 10, 2 ); 
  57. // Disable customer signup if customer has forced guest checkout 
  58. add_action( 'woocommerce_checkout_init', array( $this, 'disable_signup' ), 10, 1 ); 
  59. // Remove the checkout signup cookie if customer logs out 
  60. add_action( 'wp_logout', array( $this, 'delete_checkout_signup_cookie' ) ); 
  61. // WC Social Login comp 
  62. add_filter( 'wc_social_login_set_auth_cookie', array( $this, 'social_login_activation_check' ), 10, 2 ); 
  63.  
  64.  
  65.  
  66. public function social_login_activation_check( $message, $user ) { 
  67. $user_id = $user->ID; 
  68. if ( ! wc_gzd_is_customer_activated( $user_id ) ) { 
  69. return __( 'Please activate your account through clicking on the activation link received via email.', 'woocommerce-germanized' ); 
  70. return $message; 
  71.  
  72. public function profile_field_title( $fields ) { 
  73.  
  74. if ( get_option( 'woocommerce_gzd_checkout_address_field' ) !== 'yes' ) 
  75. return $fields; 
  76.  
  77. $fields[ 'billing' ][ 'fields' ][ 'billing_title' ] = array( 
  78. 'label' => __( 'Title', 'woocommerce-germanized' ),  
  79. 'type' => 'select',  
  80. 'options' => apply_filters( 'woocommerce_gzd_title_options', array( 1 => __( 'Mr.', 'woocommerce-germanized' ), 2 => __( 'Ms.', 'woocommerce-germanized' ) ) ),  
  81. 'description' => '' 
  82. ); 
  83.  
  84. $fields[ 'shipping' ][ 'fields' ][ 'shipping_title' ] = array( 
  85. 'label' => __( 'Title', 'woocommerce-germanized' ),  
  86. 'type' => 'select',  
  87. 'options' => apply_filters( 'woocommerce_gzd_title_options', array( 1 => __( 'Mr.', 'woocommerce-germanized' ), 2 => __( 'Ms.', 'woocommerce-germanized' ) ) ),  
  88. 'description' => '' 
  89. ); 
  90.  
  91. return $fields; 
  92.  
  93. public function is_double_opt_in_enabled() { 
  94. return get_option( 'woocommerce_gzd_customer_activation' ) === 'yes'; 
  95.  
  96. public function is_double_opt_in_login_enabled() { 
  97. return get_option( 'woocommerce_gzd_customer_activation_login_disabled' ) === 'yes'; 
  98.  
  99. public function delete_checkout_signup_cookie() { 
  100. unset( WC()->session->disable_checkout_signup ); 
  101. unset( WC()->session->login_redirect ); 
  102.  
  103. public function disable_signup( $checkout ) { 
  104.  
  105. if ( WC()->session->get( 'disable_checkout_signup' ) ) 
  106. $checkout->enable_signup = false; 
  107.  
  108.  
  109. public function login_redirect( $redirect, $user ) { 
  110.  
  111. if ( WC()->session->get( 'login_redirect' ) && 'checkout' === WC()->session->get( 'login_redirect' ) ) { 
  112. return wc_gzd_get_page_permalink( 'checkout' ); 
  113.  
  114. return $redirect; 
  115.  
  116.  
  117. public function disable_checkout() { 
  118.  
  119. $user_id = get_current_user_id(); 
  120.  
  121. if ( is_cart() ) { 
  122.  
  123. // On accessing cart - reset disable checkout signup so that the customer is rechecked before redirecting him to the checkout. 
  124. unset( WC()->session->disable_checkout_signup ); 
  125.  
  126.  
  127. if ( get_option( 'woocommerce_enable_guest_checkout' ) === 'yes' && isset( $_GET[ 'force-guest' ] ) ) { 
  128.  
  129. // Disable registration 
  130. WC()->session->set( 'disable_checkout_signup', true ); 
  131.  
  132. } elseif ( ! WC()->session->get( 'disable_checkout_signup' ) ) { 
  133.  
  134. if ( is_checkout() && ( ! is_user_logged_in() || ! wc_gzd_is_customer_activated() ) ) { 
  135.  
  136. WC()->session->set( 'login_redirect', 'checkout' ); 
  137. wp_safe_redirect( wc_gzd_get_page_permalink( 'myaccount' ) ); 
  138. exit; 
  139.  
  140. } elseif ( is_checkout() ) { 
  141.  
  142. unset( WC()->session->login_redirect ); 
  143.  
  144.  
  145. public function show_disabled_checkout_notice() { 
  146.  
  147. if ( ! is_user_logged_in() && isset( $_GET[ 'account' ] ) && 'activate' === $_GET[ 'account' ] ) { 
  148. wc_add_notice( __( 'Please activate your account through clicking on the activation link received via email.', 'woocommerce-germanized' ), 'notice' ); 
  149. return; 
  150.  
  151. if ( is_account_page() && WC()->session->get( 'login_redirect' ) ) { 
  152.  
  153. if ( ! is_user_logged_in() ) { 
  154.  
  155. if ( get_option( 'woocommerce_enable_guest_checkout' ) === 'yes' ) { 
  156. wc_add_notice( sprintf( __( 'Continue without creating an account? <a href="%s">Click here</a>', 'woocommerce-germanized' ), add_query_arg( array( 'force-guest' => 'yes' ), wc_gzd_get_page_permalink( 'checkout' ) ) ), 'notice' ); 
  157. } else { 
  158. wc_add_notice( __( 'Please create an account or login before continuing to checkout', 'woocommerce-germanized' ), 'notice' ); 
  159.  
  160. } else { 
  161.  
  162. // Redirect to checkout 
  163. unset( WC()->session->login_redirect ); 
  164. wp_safe_redirect( wc_gzd_get_page_permalink( 'checkout' ) ); 
  165. exit; 
  166.  
  167.  
  168. public function registration_redirect( $redirect ) { 
  169. return add_query_arg( array( 'account' => 'activate' ), wc_gzd_get_page_permalink( 'myaccount' ) ); 
  170.  
  171. public function disable_registration_auto_login( $result, $user_id ) { 
  172.  
  173. if ( is_a( $user_id, 'WP_User' ) ) { 
  174. $user_id = $user_id->ID; 
  175.  
  176. // Has not been activated yet 
  177. if ( ! wc_gzd_is_customer_activated( $user_id ) ) { 
  178. add_filter( 'woocommerce_registration_redirect', array( $this, 'registration_redirect' ) ); 
  179. return false; 
  180.  
  181. return true; 
  182.  
  183.  
  184. public function login_restriction( $user, $password ) { 
  185.  
  186. // Has not been activated yet 
  187. if ( ! wc_gzd_is_customer_activated( $user->ID ) ) 
  188. return new WP_Error( 'woocommerce_gzd_login', __( 'Please activate your account through clicking on the activation link received via email.', 'woocommerce-germanized' ) ); 
  189.  
  190. return $user; 
  191.  
  192.  
  193. /** 
  194. * Check for activation codes on my account page 
  195. */ 
  196. public function customer_account_activation_check() { 
  197.  
  198. if ( is_account_page() ) { 
  199.  
  200. if ( isset( $_GET[ 'activate' ] ) ) { 
  201.  
  202. $activation_code = sanitize_text_field( $_GET[ 'activate' ] ); 
  203.  
  204. if ( ! empty( $activation_code ) ) { 
  205.  
  206. if ( $this->customer_account_activate( $activation_code, true ) ) { 
  207.  
  208. wc_add_notice( __( 'Thank you. You have successfully activated your account.', 'woocommerce-germanized' ), 'notice' ); 
  209. return; 
  210.  
  211. wc_add_notice( __( 'Sorry, but this activation code cannot be found.', 'woocommerce-germanized' ), 'error' ); 
  212.  
  213. /** 
  214. * Check for customer that didn't activate their accounts within a couple of time and delete them 
  215. */ 
  216. public function account_cleanup() { 
  217.  
  218. if ( ! get_option( 'woocommerce_gzd_customer_cleanup_interval' ) || get_option( 'woocommerce_gzd_customer_cleanup_interval' ) == 0 ) 
  219. return; 
  220.  
  221. $user_query = new WP_User_Query( 
  222. array( 'role' => 'Customer', 'meta_query' => 
  223. array( 
  224. array( 
  225. 'key' => '_woocommerce_activation',  
  226. 'compare' => 'EXISTS',  
  227. ),  
  228. ),  
  229. ); 
  230.  
  231. if ( ! empty( $user_query->results ) ) { 
  232.  
  233. foreach ( $user_query->results as $user ) { 
  234.  
  235. // Check date interval 
  236. $registered = $user->data->user_registered; 
  237. $date_diff = WC_germanized()->get_date_diff( $registered, date( 'Y-m-d' ) ); 
  238. if ( $date_diff[ 'd' ] >= (int) get_option( 'woocommerce_gzd_customer_cleanup_interval' ) ) { 
  239. require_once( ABSPATH . 'wp-admin/includes/user.php' ); 
  240. wp_delete_user( $user->ID ); 
  241.  
  242.  
  243. /** 
  244. * Activate customer account based on activation code 
  245. *  
  246. * @param string $activation_code hashed activation code 
  247. * @return boolean  
  248. */ 
  249. public function customer_account_activate( $activation_code, $login = false ) { 
  250.  
  251. $user_query = new WP_User_Query( apply_filters( 'woocommerce_gzd_customer_account_activation_query', array(  
  252. 'role' => 'Customer',  
  253. 'number' => 1,  
  254. 'meta_query' => array( 
  255. array( 
  256. 'key' => '_woocommerce_activation',  
  257. 'value' => $activation_code,  
  258. 'compare' => '=',  
  259. ),  
  260. ),  
  261. ), $activation_code, $login ) ); 
  262.  
  263. if ( ! empty( $user_query->results ) ) { 
  264.  
  265. foreach ( $user_query->results as $user ) { 
  266.  
  267. do_action( 'woocommerce_gzd_customer_opted_in', $user ); 
  268. delete_user_meta( $user->ID, '_woocommerce_activation' ); 
  269. WC()->mailer()->customer_new_account( $user->ID ); 
  270.  
  271. if ( $login && ! is_user_logged_in() ) 
  272. wc_set_customer_auth_cookie( $user->ID ); 
  273.  
  274. return true; 
  275.  
  276.  
  277. return false; 
  278.  
  279. public function init_gettext_replacement() { 
  280. if ( is_checkout() && get_option( 'woocommerce_gzd_customer_account_checkout_checkbox' ) == 'yes' ) 
  281. add_filter( 'gettext', array( $this, 'set_customer_account_checkbox_text' ), 10, 3 ); 
  282.  
  283. public function set_customer_account_checkbox_text( $translated, $original, $domain ) { 
  284. $search = "Create an account?"; 
  285. if ( $domain === 'woocommerce' && $original === $search ) { 
  286. remove_filter( 'gettext', array( $this, 'set_customer_account_checkbox_text' ), 10, 3 ); 
  287. return wc_gzd_get_legal_text( get_option( 'woocommerce_gzd_customer_account_text' ) ); 
  288. return $translated; 
  289.  
  290. public function email_hooks( $mailer ) { 
  291.  
  292. // Add new customer activation 
  293. if ( get_option( 'woocommerce_gzd_customer_activation' ) == 'yes' ) { 
  294.  
  295. remove_action( 'woocommerce_created_customer_notification', array( $mailer, 'customer_new_account' ), 10 ); 
  296. add_action( 'woocommerce_created_customer_notification', array( $this, 'customer_new_account_activation' ), 9, 3 ); 
  297.  
  298.  
  299.  
  300. /** 
  301. * Customer new account activation email. 
  302. * 
  303. * @param int $customer_id 
  304. * @param array $new_customer_data 
  305. */ 
  306. public function customer_new_account_activation( $customer_id, $new_customer_data = array(), $password_generated = false ) { 
  307.  
  308. if ( ! $customer_id ) 
  309. return; 
  310.  
  311. if ( ! wc_gzd_get_dependencies()->woocommerce_version_supports_crud() ) { 
  312. $this->set_customer_activation_meta( $customer_id, $new_customer_data, $password_generated ); 
  313.  
  314. $user_pass = ! empty( $new_customer_data['user_pass'] ) ? $new_customer_data['user_pass'] : ''; 
  315.  
  316. $user_activation = get_user_meta( $customer_id, '_woocommerce_activation', true ); 
  317. $user_activation_url = apply_filters( 'woocommerce_gzd_customer_activation_url', add_query_arg( array( 'activate' => $user_activation ), wc_gzd_get_page_permalink( 'myaccount' ) ) ); 
  318.  
  319. if ( $email = WC_germanized()->emails->get_email_instance_by_id( 'customer_new_account_activation' ) ) 
  320. $email->trigger( $customer_id, $user_activation, $user_activation_url, $user_pass, $password_generated ); 
  321.  
  322.  
  323. public function set_customer_activation_meta( $customer_id, $new_customer_data, $password_generated ) { 
  324.  
  325. global $wp_hasher; 
  326.  
  327. if ( ! $customer_id ) 
  328. return; 
  329.  
  330. if ( empty( $wp_hasher ) ) { 
  331. require_once ABSPATH . WPINC . '/class-phpass.php'; 
  332. $wp_hasher = new PasswordHash( 8, true ); 
  333.  
  334. $user_activation = $wp_hasher->HashPassword( wp_generate_password( 20 ) ); 
  335.  
  336. add_user_meta( $customer_id, '_woocommerce_activation', $user_activation ); 
  337.  
  338.  
  339. WC_GZD_Customer_Helper::instance(); 
.