WC_GZD_Customer_Helper

The WooCommerce Germanized WC GZD Customer Helper class.

Defined (1)

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

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